/** * {@inheritdoc} */ public function import(Row $row, array $old_destination_id_values = array()) { $identifier = $row->getDestinationProperty('identifier'); $disqus = disqus_api(); if ($disqus) { try { $thread = $disqus->threads->details(array('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.', array('@identifier' => $identifier)); $thread = NULL; } if (!isset($thread->id)) { try { $thread = $disqus->threads->create(array('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.', array('@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('author_url'); $ip_address = $row->getDestinationProperty('ip_address'); if (empty($author_name) || empty($author_email)) { // Post comment as created by site's moderator. $disqus->posts->create(array('message' => $message, 'thread' => $thread->id, 'access_token' => $this->config->get('advanced.disqus_useraccesstoken'), 'date' => $date, 'ip_address' => $ip_address)); } else { // Cannot create comment as anonymous user, needs 'api_key' // (api_key is not the public key). $disqus->posts->create(array('thread' => $thread, 'message' => $message, 'author_name' => $author_name, 'author_email' => $author_email, 'author_url' => $author_url, 'date' => $date, 'ip_address' => $ip_address)); } return TRUE; } catch (\Exception $exception) { $this->logger->error('Error creating post on thread @thread.', array('@thread' => $thread->id)); } return FALSE; } }
/** * {@inheritdoc} */ public function initializeIterator() { if ($disqus = disqus_api()) { try { $posts = $disqus->forums->listPosts(array('forum' => $this->config->get('disqus_domain'))); } catch (\Exception $exception) { drupal_set_message(t('There was an error loading the forum details. Please check you API keys and try again.', 'error')); $this->logger->error('Error loading the Disqus PHP API. Check your forum name.', array()); return FALSE; } $items = array(); foreach ($posts as $post) { $id = $post['id']; $items[$id]['id'] = $id; $items[$id]['pid'] = $post['parent']; $thread = $disqus->threads->details(array('thread' => $post['thread'])); $items[$id]['identifier'] = $thread['identifier']; $items[$id]['name'] = $post['author']['name']; $items[$id]['email'] = $post['author']['email']; $items[$id]['user_id'] = $post['author']['id']; $items[$id]['url'] = $post['author']['url']; $items[$id]['ipAddress'] = $post['ipAddress']; $items[$id]['isAnonymous'] = $post['author']['isAnonymous']; $items[$id]['createdAt'] = $post['createdAt']; $items[$id]['comment'] = $post['message']; $items[$id]['isEdited'] = $post['isEdited']; } } return new \ArrayIterator($items); }