You are here

class DisqusComment in Disqus 8

Same name in this branch
  1. 8 src/Plugin/migrate/source/DisqusComment.php \Drupal\disqus\Plugin\migrate\source\DisqusComment
  2. 8 src/Plugin/migrate/destination/DisqusComment.php \Drupal\disqus\Plugin\migrate\destination\DisqusComment

Disqus comment destination.

Plugin annotation


@MigrateDestination(
  id = "disqus_destination"
)

Hierarchy

Expanded class hierarchy of DisqusComment

File

src/Plugin/migrate/destination/DisqusComment.php, line 20

Namespace

Drupal\disqus\Plugin\migrate\destination
View source
class DisqusComment extends DestinationBase implements ContainerFactoryPluginInterface {

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The disqus.settings configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs Disqus comments destination plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implemetation definition.
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   *   The migration.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, LoggerInterface $logger, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
    $this->logger = $logger;
    $this->config = $config_factory
      ->get('disqus.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
      ->get('logger.factory')
      ->get('disqus'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function fields(MigrationInterface $migration = NULL) {
    return [
      'disqus_id' => $this
        ->t('The disqus ID'),
      'message' => $this
        ->t('The comment body.'),
      'parent' => $this
        ->t('Parent comment ID. If set to null, this comment is not a reply to an existing comment.'),
      'identifier' => $this
        ->t('The disqus identifier to look up the correct thread.'),
      'title' => $this
        ->t("The title of the comment's thread page."),
      'author_email' => $this
        ->t("The comments author's email."),
      'author_name' => $this
        ->t("The comments author's name."),
      'author_url' => $this
        ->t("The comments author's url."),
      'date' => $this
        ->t('The time that the comment was posted as a Unix timestamp.'),
      'ip_address' => $this
        ->t("The IP address that the comment was posted from."),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['disqus_id']['type'] = 'string';
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function import(Row $row, array $old_destination_id_values = []) {
    $identifier = $row
      ->getDestinationProperty('identifier');
    $disqus = disqus_api();
    if ($disqus) {
      try {
        $thread = $disqus->threads
          ->details([
          'forum' => $this->config
            ->get('disqus_domain'),
          'thread:ident' => $identifier,
          'thread' => '1',
        ]);
      } catch (\Exception $exception) {
        $this->logger
          ->error('Error loading thread details for entity : @identifier. Check your API keys.', [
          '@identifier' => $identifier,
        ]);
        $thread = NULL;
      }
      if (!isset($thread->id)) {
        try {
          $thread = $disqus->threads
            ->create([
            'forum' => $this->config
              ->get('disqus_domain'),
            'access_token' => $this->config
              ->get('advanced.disqus_useraccesstoken'),
            'title' => $row
              ->getDestinationProperty('title'),
            'identifier' => $identifier,
          ]);
        } catch (\Exception $exception) {
          $this->logger
            ->error('Error creating thread for entity : @identifier. Check your user access token.', [
            '@identifier' => $identifier,
          ]);
        }
      }
      try {
        $message = $row
          ->getDestinationProperty('message');
        $author_name = $row
          ->getDestinationProperty('author_name');
        $author_email = $row
          ->getDestinationProperty('author_email');
        $author_url = $row
          ->getDestinationProperty('author_url');
        $date = $row
          ->getDestinationProperty('date');
        $ip_address = $row
          ->getDestinationProperty('ip_address');
        $ids = FALSE;
        if (empty($author_name) || empty($author_email)) {

          // Post comment as created by site's moderator.
          $ids = [
            'disqus_id' => $disqus->posts
              ->create([
              'message' => $message,
              'thread' => $thread->id,
              'access_token' => $this->config
                ->get('advanced.disqus_useraccesstoken'),
              'date' => $date,
              'ip_address' => $ip_address,
            ])->id,
          ];
        }
        else {

          // Cannot create comment as anonymous user, needs 'api_key'
          // (api_key is not the public key).
          $ids = [
            'disqus_id' => $disqus->posts
              ->create([
              'thread' => $thread->id,
              'message' => $message,
              'author_name' => $author_name,
              'author_email' => $author_email,
              'author_url' => $author_url,
              'api_key' => $this->config
                ->get('advanced.disqus_publickey'),
            ]),
          ];
        }
        return $ids;
      } catch (\Exception $exception) {
        $this->logger
          ->error('Error creating post on thread @thread, error: @error', [
          '@thread' => $thread->id,
          '@error' => $exception
            ->getMessage(),
        ]);
      }
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function rollback(array $destination_identifier) {
    $disqus = disqus_api();
    if ($disqus) {
      try {
        $post = $disqus->posts
          ->details([
          'post' => $destination_identifier['disqus_id'],
        ]);
      } catch (\Exception $exception) {
        $this->logger
          ->error('Error loading thread details for entity : @identifier. Check your API keys.', [
          '@identifier' => $destination_identifier['disqus_id'],
        ]);
        $post = NULL;
      }
      if (!isset($post->id)) {
        $this->logger
          ->notice('Unable to find post: @identifier when trying to delete it. Maybe it was already deleted?', [
          '@identifier' => $destination_identifier['disqus_id'],
        ]);
        return;
      }
      try {
        $disqus->posts
          ->remove([
          'access_token' => $this->config
            ->get('advanced.disqus_useraccesstoken'),
          'post' => $destination_identifier['disqus_id'],
        ]);
      } catch (\Exception $exception) {
        $this->logger
          ->error('Error deleting post @identifier.', [
          '@identifier' => $destination_identifier['disqus_id'],
        ]);
      }
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DestinationBase::$migration protected property The migration.
DestinationBase::$rollbackAction protected property The rollback action to be saved for the last imported item.
DestinationBase::$supportsRollback protected property Indicates whether the destination can be rolled back.
DestinationBase::checkRequirements public function Checks if requirements for this plugin are OK. Overrides RequirementsInterface::checkRequirements
DestinationBase::getDestinationModule public function Gets the destination module handling the destination data. Overrides MigrateDestinationInterface::getDestinationModule 1
DestinationBase::rollbackAction public function The rollback action for the last imported item. Overrides MigrateDestinationInterface::rollbackAction
DestinationBase::setRollbackAction protected function For a destination item being updated, set the appropriate rollback action.
DestinationBase::supportsRollback public function Whether the destination can be rolled back or not. Overrides MigrateDestinationInterface::supportsRollback
DisqusComment::$config protected property The disqus.settings configuration.
DisqusComment::$logger protected property A logger instance.
DisqusComment::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DisqusComment::fields public function Returns an array of destination fields. Overrides MigrateDestinationInterface::fields
DisqusComment::getIds public function Gets the destination IDs. Overrides MigrateDestinationInterface::getIds
DisqusComment::import public function Import the row. Overrides MigrateDestinationInterface::import
DisqusComment::rollback public function Delete the specified destination object from the target Drupal. Overrides DestinationBase::rollback
DisqusComment::__construct public function Constructs Disqus comments destination plugin. Overrides DestinationBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.