class DisqusComment in Disqus 8
Same name in this branch
- 8 src/Plugin/migrate/source/DisqusComment.php \Drupal\disqus\Plugin\migrate\source\DisqusComment
- 8 src/Plugin/migrate/destination/DisqusComment.php \Drupal\disqus\Plugin\migrate\destination\DisqusComment
Disqus comment destination.
Plugin annotation
@MigrateDestination(
id = "disqus_destination"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\Plugin\migrate\destination\DestinationBase implements MigrateDestinationInterface, RequirementsInterface
- class \Drupal\disqus\Plugin\migrate\destination\DisqusComment implements ContainerFactoryPluginInterface
- class \Drupal\migrate\Plugin\migrate\destination\DestinationBase implements MigrateDestinationInterface, RequirementsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DisqusComment
File
- src/
Plugin/ migrate/ destination/ DisqusComment.php, line 20
Namespace
Drupal\disqus\Plugin\migrate\destinationView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DestinationBase:: |
protected | property | The migration. | |
DestinationBase:: |
protected | property | The rollback action to be saved for the last imported item. | |
DestinationBase:: |
protected | property | Indicates whether the destination can be rolled back. | |
DestinationBase:: |
public | function |
Checks if requirements for this plugin are OK. Overrides RequirementsInterface:: |
|
DestinationBase:: |
public | function |
Gets the destination module handling the destination data. Overrides MigrateDestinationInterface:: |
1 |
DestinationBase:: |
public | function |
The rollback action for the last imported item. Overrides MigrateDestinationInterface:: |
|
DestinationBase:: |
protected | function | For a destination item being updated, set the appropriate rollback action. | |
DestinationBase:: |
public | function |
Whether the destination can be rolled back or not. Overrides MigrateDestinationInterface:: |
|
DisqusComment:: |
protected | property | The disqus.settings configuration. | |
DisqusComment:: |
protected | property | A logger instance. | |
DisqusComment:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
DisqusComment:: |
public | function |
Returns an array of destination fields. Overrides MigrateDestinationInterface:: |
|
DisqusComment:: |
public | function |
Gets the destination IDs. Overrides MigrateDestinationInterface:: |
|
DisqusComment:: |
public | function |
Import the row. Overrides MigrateDestinationInterface:: |
|
DisqusComment:: |
public | function |
Delete the specified destination object from the target Drupal. Overrides DestinationBase:: |
|
DisqusComment:: |
public | function |
Constructs Disqus comments destination plugin. Overrides DestinationBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |