You are here

function disqus_node_delete in Disqus 7

Implements hook_node_delete().

File

./disqus.module, line 330
The Disqus Drupal module.

Code

function disqus_node_delete($node) {
  db_delete('disqus')
    ->condition('nid', $node->nid)
    ->execute();

  // Close/remove the thread on disqus if required.
  $action = variable_get('disqus_api_delete', DISQUS_API_NO_ACTION);
  if (isset($node->disqus) && $action != DISQUS_API_NO_ACTION) {
    $disqus = disqus_api();
    if ($disqus) {
      try {

        // Load the thread data from disqus. Passing thread is required to allow the thread:ident call to work correctly. There is a pull request to fix this issue.
        $thread = $disqus->threads
          ->details(array(
          'forum' => $node->disqus['domain'],
          'thread:ident' => $node->disqus['identifier'],
          'thread' => '1',
          'version' => '3.0',
        ));
      } catch (Exception $exception) {
        drupal_set_message(t('There was an error loading the thread details from Disqus.'), 'error');
        watchdog('disqus', 'Error loading thread details for node @nid. Check your API keys.', array(
          '@nid' => $node->nid,
        ), WATCHDOG_ERROR, 'admin/config/services/disqus');
      }
      if (isset($thread->id)) {
        if ($action == DISQUS_API_CLOSE) {
          try {
            $disqus->threads
              ->close(array(
              'access_token' => variable_get('disqus_useraccesstoken', ''),
              'thread' => $thread->id,
              'forum' => $node->disqus['domain'],
              'version' => '3.0',
            ));
          } catch (Exception $exception) {
            drupal_set_message(t('There was an error closing the thread on Disqus.'), 'error');
            watchdog('disqus', 'Error closing thread for node @nid. Check your user access token.', array(
              '@nid' => $node->nid,
            ), WATCHDOG_ERROR, 'admin/config/services/disqus');
          }
        }
        if ($action == DISQUS_API_REMOVE) {
          try {
            $disqus->threads
              ->remove(array(
              'access_token' => variable_get('disqus_useraccesstoken', ''),
              'thread' => $thread->id,
              'forum' => $node->disqus['domain'],
              'version' => '3.0',
            ));
          } catch (Exception $exception) {
            drupal_set_message(t('There was an error removing the thread on Disqus.'), 'error');
            watchdog('disqus', 'Error removing thread for node @nid. Check your user access token.', array(
              '@nid' => $node->nid,
            ), WATCHDOG_ERROR, 'admin/config/services/disqus');
          }
        }
      }
    }
  }
}