You are here

disqus.module in Disqus 5

Same filename and directory in other branches
  1. 8 disqus.module
  2. 6 disqus.module
  3. 7 disqus.module

File

disqus.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function disqus_help($section) {
  switch ($section) {
    case 'admin/help#disqus':
      $output = '<p>' . t('Uses the <a href="@disqus">Disqus</a> comment system to enhance comments.', array(
        '@disqus' => 'http://disqus.com',
      )) . '</p>';
      $output .= '<h3>' . t('Installation') . '</h3>';
      $output .= '<ol><li>' . t('Register your site information at <a href="http://disqus.com">Disqus</a>') . '</li>';
      $output .= '<li>' . t('In the <a href="@configuration">Disqus configuration</a>, set the domain to what you registered with Disqus, and what node types you would like to have comments', array(
        '@configuration' => url('admin/settings/disqus'),
      )) . '</li>';
      $output .= '<li>' . t('Visit the <a href="@permissions">permissions</a>, and set which users you would like to have the ability to view Disqus threads (recommended for role)', array(
        '@permissions' => url('admin/user/access', NULL, 'module-disqus'),
      )) . '</li></ol>';
      return $output;
    case 'admin/settings/disqus':
      return '<p>' . t('The following provides the configuration options for the <a href="@disqus">Disqus</a> comment web service.', array(
        '@disqus' => 'http://disqus.com',
      )) . '</p>';
  }
}

/**
 * Implementation of hook_perm().
 */
function disqus_perm() {
  return array(
    'administer disqus',
    'view disqus comments',
  );
}

/**
 * Implementation of hook_menu().
 */
function disqus_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/disqus',
      'title' => t('Disqus'),
      'description' => t('Provides configuration options for the Disqus comment system.'),
      'access' => user_access('administer disqus'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'disqus_admin_settings_menu',
      ),
    );
  }
  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function disqus_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':

      // See if Disqus is active on this node.
      $types = variable_get('disqus_nodetypes', array());
      $arg2 = arg(2);
      if (!empty($types[$node->type]) && empty($arg2) || $arg2 == 'view') {

        // Check which Disqus domain to use.
        $domain = variable_get('disqus_domain', '');
        if (!empty($domain)) {

          // Save the data to the node object.
          $node->disqus = array(
            'domain' => $domain,
          );

          // Build the absolute URL without the alias for the disqus_url flag.
          global $base_url;
          $node->disqus['url'] = $base_url . '/node/' . $node->nid;

          // Build the message excerpt.
          $message = nl2br($node->teaser);
          $message = str_replace("\r", ' ', $message);
          $message = str_replace("\n", ' ', $message);
          $message = strip_tags($message);
          $node->disqus['message'] = check_plain($message);

          // Build the title.
          $node->disqus['title'] = check_plain(strip_tags($node->title));

          // Provide the identifier.
          $node->disqus['identifier'] = 'node/' . $node->nid;

          // The developer flag must always be set when the node is unpublished.
          if ($node->status == 0) {
            $node->disqus['developer'] = 1;
          }
          elseif ($developer = variable_get('disqus_developer', FALSE)) {
            $node->disqus['developer'] = intval($developer);
          }
        }
      }
      break;
    case 'view':

      // See if we are to display Disqus in the current context.
      if (!$a3 && $a4 && isset($node->disqus)) {
        if (user_access('view disqus comments') && variable_get('disqus_location', 'content_area') == 'content_area') {

          // Inject the comments into the node content area.
          $node->content['disqus'] = array(
            '#value' => theme('disqus_comments', $node->disqus),
            '#weight' => variable_get('disqus_weight', 50),
          );
        }
      }
      break;
  }
}

/**
 * Implementation of hook_footer().
 *
 * @param $disqus_options
 *   The options to pass off to the Disqus comments.
 */
function disqus_footer($main = 0, $disqus_options = NULL) {
  static $disqus = FALSE;
  if (isset($disqus_options)) {
    $disqus = $disqus_options;
  }
  if ($disqus) {
    $domain = variable_get('disqus_domain', '');
    if (!empty($domain)) {
      $output = '<script type="text/javascript">';
      if (isset($disqus['developer'])) {
        $output .= 'var disqus_developer = 1;';
      }
      $output .= 'var disqus_url = ' . drupal_to_js($disqus['url']) . ';';
      $output .= 'var disqus_title = ' . drupal_to_js($disqus['title']) . ';';
      $output .= 'var disqus_message = ' . drupal_to_js($disqus['message']) . ';';
      $output .= 'var disqus_identifier = ' . drupal_to_js($disqus['identifier']) . ';';
      global $user;
      if ($user->uid > 0) {
        $output .= 'var disqus_def_name = ' . drupal_to_js($user->name) . ';';
        $output .= 'var disqus_def_email = ' . drupal_to_js($user->mail) . ';';
      }
      $output .= "</script><script type='text/javascript' src='http://disqus.com/forums/{$domain}/embed.js'></script>";
      return $output;
    }
  }
}

/**
 * Implementation of hook_link().
 */
function disqus_link($type, $node = NULL, $teaser = FALSE) {
  global $base_url;
  $links = array();
  if ($type == 'node' && $teaser == TRUE) {
    $types = variable_get('disqus_nodetypes', array());
    if (!empty($types[$node->type]) && user_access('view disqus comments')) {
      $links['disqus_comments'] = array(
        'title' => t('Comments'),
        'href' => $base_url . '/node/' . $node->nid . '#disqus_thread',
        'attributes' => array(
          'title' => t('Jump to the comments of this posting.'),
        ),
      );
      static $disqus_js_added = FALSE;
      if ($disqus_js_added === FALSE) {
        $disqus_js_added = TRUE;
        $domain = variable_get('disqus_domain', '');
        $disqus_js = <<<EOT
//<[CDATA[
(function() {
    var links = document.getElementsByTagName('a');
    var query = '?';
    for(var i = 0; i < links.length; i++) {
      if(links[i].href.indexOf('#disqus_thread') >= 0) {
        query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
      }
    }
    document.write('<script type="text/javascript" src="http://disqus.com/forums/{<span class="php-variable">$domain</span>}/get_num_replies.js' + query + '"></' + 'script>');
  })();
//]]>
EOT;
        drupal_add_js($disqus_js, 'inline', 'footer');
      }
    }
  }
  return $links;
}

/**
 * Menu callback; Displays the administration settings for Disqus.
 */
function disqus_admin_settings_menu() {
  require_once drupal_get_path('module', 'disqus') . '/disqus.admin.inc';
  return disqus_admin_settings();
}

/**
 * Implementation of hook_block().
 */
function disqus_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      return array(
        'disqus_recent_comments' => array(
          'info' => t('Disqus Recent Comments'),
        ),
        'disqus_popular_threads' => array(
          'info' => t('Disqus Popular Threads'),
        ),
        'disqus_top_commenters' => array(
          'info' => t('Disqus Top Commenters'),
        ),
        'disqus_combination_widget' => array(
          'info' => t('Disqus Combination Widget'),
        ),
        'disqus_comments' => array(
          'info' => t('Disqus: Comments'),
        ),
      );
    case 'configure':
      $form = array();
      if ($delta != 'disqus_comments') {
        $form[$delta . '_items'] = array(
          '#type' => 'select',
          '#title' => t('Number of items to show'),
          '#options' => array(
            1 => 1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
          ),
          '#default_value' => variable_get($delta . '_items', 5),
        );
        $form[$delta . '_showavatars'] = array(
          '#type' => 'select',
          '#title' => t('Show avatars'),
          '#options' => array(
            FALSE => t('No'),
            TRUE => t('Yes'),
          ),
          '#default_value' => variable_get($delta . '_showavatars', TRUE),
          '#access' => $delta == 'disqus_recent_comments' || $delta == 'disqus_top_commenters',
        );
        $form[$delta . '_avatarsize'] = array(
          '#type' => 'select',
          '#title' => t('Avatar size'),
          '#options' => array(
            24 => t('X-Small (24px)'),
            32 => t('Small (32px)'),
            48 => t('Medium (48px)'),
            92 => t('Large (92px)'),
            128 => t('X-Large (128px)'),
          ),
          '#default_value' => variable_get($delta . '_avatarsize', 32),
          '#access' => $form[$delta . '_showavatars']['#access'],
        );
        $form[$delta . '_colortheme'] = array(
          '#type' => 'select',
          '#title' => t('Color Theme'),
          '#options' => array(
            'blue' => t('Blue'),
            'grey' => t('Grey'),
            'green' => t('Green'),
            'red' => t('Red'),
            'orange' => t('Orange'),
          ),
          '#default_value' => variable_get($delta . '_colortheme', 'blue'),
          '#access' => $delta == 'disqus_combination_widget',
        );
        $form[$delta . '_defaulttabview'] = array(
          '#type' => 'select',
          '#title' => t('Default Tab View'),
          '#options' => array(
            'people' => t('People'),
            'recent' => t('Recent'),
            'popular' => t('Popular'),
          ),
          '#default_value' => variable_get($delta . '_defaulttabview', 'people'),
          '#access' => $delta == 'disqus_combination_widget',
        );
      }
      return $form;
    case 'save':
      if ($delta != $disqus_comments) {
        variable_set($delta . '_items', $edit[$delta . '_items']);
        variable_set($delta . '_showavatars', $edit[$delta . '_showavatars']);
        variable_set($delta . '_avatarsize', $edit[$delta . '_avatarsize']);
        variable_set($delta . '_colortheme', $edit[$delta . '_colortheme']);
        variable_set($delta . '_defaulttabview', $edit[$delta . '_defaulttabview']);
      }
      break;
    case 'view':
      $num_items = variable_get($delta . '_items', 5);
      $avatars = variable_get($delta . '_showavatars', TRUE) ? '&amp;avatar_size=' . variable_get($delta . '_avatarsize', 32) : '&hide_avatars=1';
      $color = variable_get($delta . '_colortheme', 'blue');
      $default_tab = variable_get($delta . '_defaulttabview', 'people');
      $domain = variable_get('disqus_domain', '');
      if (!empty($domain)) {
        $subject = '';
        $content = '';
        switch ($delta) {
          case 'disqus_recent_comments':
            $content = <<<EOT
<div id="dsq-recentcomments" class="dsq-widget"><script type="text/javascript" src="http://disqus.com/forums/{<span class="php-variable">$domain</span>}/recent_comments_widget.js?num_items={<span class="php-variable">$num_items</span>}{<span class="php-variable">$avatars</span>}"></script></div>
EOT;
            $subject = t('Recent Comments');
            break;
          case 'disqus_popular_threads':
            $subject = t('Popular Threads');
            $content = <<<EOT
<div id="dsq-popthreads" class="dsq-widget"><script type="text/javascript" src="http://disqus.com/forums/{<span class="php-variable">$domain</span>}/popular_threads_widget.js?num_items={<span class="php-variable">$num_items</span>}"></script></div>
EOT;
            break;
          case 'disqus_top_commenters':
            $subject = t('Top Commenters');
            $content = <<<EOT
<div id="dsq-topcommenters" class="dsq-widget"><script type="text/javascript" src="http://disqus.com/forums/{<span class="php-variable">$domain</span>}/top_commenters_widget.js?num_items={<span class="php-variable">$num_items</span>}{<span class="php-variable">$avatars</span>}"></script></div>
EOT;
            break;
          case 'disqus_combination_widget':
            $subject = t('Comments');
            $content = <<<EOT
<script type="text/javascript" src="http://disqus.com/forums/{<span class="php-variable">$domain</span>}/combination_widget.js?num_items={<span class="php-variable">$num_items</span>}&color={<span class="php-variable">$color</span>}&default_tab={<span class="php-variable">$default_tab</span>}"></script>
EOT;
            break;
          case 'disqus_comments':

            // Only display Diqus comments in the block if desired.
            if (variable_get('disqus_location', 'content_area') == 'block' && user_access('view disqus comments')) {

              // Check for displaying on nodes.
              switch (arg(0)) {
                case 'node':
                  $nid = arg(1);
                  if (is_numeric($nid)) {
                    $node = node_load($nid);
                    if (isset($node->disqus)) {
                      $content = theme('disqus_comments', $node->disqus);
                    }
                  }
                  break;
              }
            }
            break;
        }
        return array(
          'subject' => $subject,
          'content' => $content,
        );
      }
      break;
  }
}

/**
 * Renders the JavaScript to display the Disqus thread for the current page.
 * @param $options
 *   An array containing the following items:
 *     - "domain": The domain associated with this Disqus account.
 *     - "title": The title of the thread.
 *     - "message": The teaser of the thread.
 *     - "developer": Whether or not testing is enabled.
 *     - "url": The disqus_url variable (http://disqus.com/docs/help/#faq-16).
 */
function theme_disqus_comments($options = array()) {
  disqus_footer(0, $options);
  return '<div id="disqus_thread"></div><noscript><div class="disqus-noscript"><a href="http://' . $options['domain'] . '.disqus.com/?url=' . urlencode($options['url']) . '">' . t('View the discussion thread.') . '</a></div></noscript>';
}

Functions

Namesort descending Description
disqus_admin_settings_menu Menu callback; Displays the administration settings for Disqus.
disqus_block Implementation of hook_block().
disqus_footer Implementation of hook_footer().
disqus_help Implementation of hook_help().
disqus_link Implementation of hook_link().
disqus_menu Implementation of hook_menu().
disqus_nodeapi Implementation of hook_nodeapi().
disqus_perm Implementation of hook_perm().
theme_disqus_comments Renders the JavaScript to display the Disqus thread for the current page.