diff --git a/src/Plugin/migrate/destination/DisqusComment.php b/src/Plugin/migrate/destination/DisqusComment.php index 33153d3..33c7b40 100644 --- a/src/Plugin/migrate/destination/DisqusComment.php +++ b/src/Plugin/migrate/destination/DisqusComment.php @@ -74,6 +74,7 @@ class DisqusComment extends DestinationBase implements ContainerFactoryPluginInt */ public function fields(MigrationInterface $migration = NULL) { return array( + '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.'), @@ -90,7 +91,7 @@ class DisqusComment extends DestinationBase implements ContainerFactoryPluginInt * {@inheritdoc} */ public function getIds() { - $ids['message']['type'] = 'string'; + $ids['disqus_id']['type'] = 'string'; return $ids; } @@ -134,22 +135,23 @@ class DisqusComment extends DestinationBase implements ContainerFactoryPluginInt $author_name = $row->getDestinationProperty('author_name'); $author_email = $row->getDestinationProperty('author_email'); $author_url = $row->getDestinationProperty('author_url'); - $date = $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. - $disqus->posts->create(array( + $ids = ['disqus_id' => $disqus->posts->create(array( '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). - $disqus->posts->create(array( + $ids = ['disqus_id' => $disqus->posts->create([ 'thread' => $thread, 'message' => $message, 'author_name' => $author_name, @@ -157,9 +159,9 @@ class DisqusComment extends DestinationBase implements ContainerFactoryPluginInt 'author_url' => $author_url, 'date' => $date, 'ip_address' => $ip_address, - )); + ])->id]; } - return TRUE; + return $ids; } catch (\Exception $exception) { $this->logger->error('Error creating post on thread @thread.', array('@thread' => $thread->id)); @@ -168,4 +170,39 @@ class DisqusComment extends DestinationBase implements ContainerFactoryPluginInt } } + /** + * {@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.', array('@identifier' => $identifier)); + $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; + } + } }