You are here

sharethis.module in ShareThis 5

Provides the ShareThis service.

File

sharethis.module
View source
<?php

/**
 * @file
 * Provides the ShareThis service.
 */

/**
 * Implementation of hook_help().
 */
function sharethis_help($path) {
  switch ($path) {
    case 'admin/help#sharethis':
      return '<p>' . t('Provides functionality to interface with the <a href="@sharethis">ShareThis</a> web service.', array(
        '@sharethis' => 'http://sharethis.com',
      ));
    case 'admin/settings/sharethis':
      return '<p>' . t('Below provides the settings for interfacing with the <a href="@sharethis">ShareThis</a> web service.', array(
        '@sharethis' => 'http://sharethis.com',
      ));
  }
}

/**
 * Implementation of hook_perm().
 */
function sharethis_perm() {
  return array(
    'administer sharethis',
    'use share this',
  );
}

/**discovered a site that puts up a free song every day, and I was looking back
 * Implementation of hook_menu().
 */
function sharethis_menu() {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/sharethis',
    'title' => t('ShareThis'),
    'description' => t('Administer settings related to the ShareThis web service.'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'sharethis_admin_settings',
    ),
    'access' => user_access('administer sharethis'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Menu callback; Settings administration.
 */
function sharethis_admin_settings() {
  $form['sharethis_this_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('ShareThis API Code (STAPI)'),
    '#description' => t('The following javascript code can be modified to enhance the appearance of your ShareThis popup, and to determine which Social Networking sites are displayed.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $sharethis_code = '<script type="text/javascript" src="http://w.sharethis.com/widget/?tabs=web&amp;charset=utf-8&amp;style=default&amp;publisher=79afd7f6-da74-4c6e-9bd9-3c199659901d&amp;headerbg=%2337a037"></script>';
  $form['sharethis_this_settings']['sharethis_sharethis_this_code'] = array(
    '#type' => 'textarea',
    '#title' => t('Share This Code'),
    '#description' => t('Javascript code provided by Share This. You can generate your own code !here.', array(
      '!here' => l(t('here'), 'http://sharethis.com/publisher?type=stapi'),
    )),
    '#default_value' => variable_get('sharethis_sharethis_this_code', $sharethis_code),
  );
  $form['where_to_sharethis_sharethis_this'] = array(
    // Where to Share
    '#type' => 'fieldset',
    '#title' => t('Node Types &amp; Positioning'),
    '#description' => t('Set the node types and categories you want to display links for.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['where_to_sharethis_sharethis_this']['sharethis_sharethis_this_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node types'),
    '#default_value' => variable_get('sharethis_sharethis_this_node_types', array()),
    '#options' => node_get_types('names'),
  );
  if (module_exists('taxonomy')) {
    $terms = taxonomy_form_all();
    if (!empty($terms)) {
      $form['where_to_sharethis_sharethis_this']['sharethis_sharethis_this_category_types'] = array(
        '#type' => 'select',
        '#multiple' => TRUE,
        '#title' => t('Categories'),
        '#default_value' => variable_get('sharethis_sharethis_this_category_types', array()),
        '#options' => $terms,
      );
    }
  }
  $form['where_to_sharethis_sharethis_this']['sharethis_teaser'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display in teaser view'),
    '#description' => t('If enabled, the ShareThis links will appear in node teasers. If disabled, they will only appear on the full node page.'),
    '#default_value' => variable_get('sharethis_teaser', 1),
  );
  $form['where_to_sharethis_sharethis_this']['sharethis_sharethis_this_where'] = array(
    '#type' => 'select',
    '#title' => t('Where to display'),
    '#description' => t('Select where you would like the ShareThis link to appear.'),
    '#default_value' => variable_get('sharethis_sharethis_this_where', 'links'),
    '#options' => array(
      'links' => t('Links'),
      'nodes' => t('Nodes'),
    ),
  );
  $form['where_to_sharethis_sharethis_this']['sharethis_sharethis_weight'] = array(
    '#type' => 'weight',
    '#delta' => 10,
    '#description' => t('Only applies when node type display is selected.'),
    '#title' => t('Weight'),
    '#default_value' => variable_get('sharethis_sharethis_weight', 0),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_nodeapi().
 */
function sharethis_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      if (!$teaser || variable_get('sharethis_teaser', 1)) {
        if (variable_get('sharethis_sharethis_this_where', 'links') == 'nodes') {
          if (_sharethis_sharethis_this_show($node->type, $node->nid) && user_access('use share this')) {
            if (sharethis_add_js()) {
              $sharethis = sharethis_get_js($node->title, url('node/' . $node->nid, NULL, NULL, TRUE));
              $node->content['sharethis_sharethis_this'] = array(
                '#value' => $sharethis,
                '#weight' => variable_get('sharethis_sharethis_weight', 0),
              );
            }
          }
        }
      }
      break;
  }
}

/**
 * Implementation of hook_link().
 */
function sharethis_link($type, $node = NULL, $teaser = FALSE) {
  if (!$teaser || variable_get('sharethis_teaser', 1)) {
    if (variable_get('sharethis_sharethis_this_where', 'links') == 'links') {
      if (_sharethis_sharethis_this_show($node->type, $node->nid) && user_access('use share this')) {
        if (sharethis_add_js()) {
          $sharethis = sharethis_get_js($node->title, url('node/' . $node->nid, NULL, NULL, TRUE));
          $links['sharethis_sharethis_this'] = array(
            'title' => $sharethis,
            'html' => TRUE,
            'attributes' => array(
              'id' => 'sharethis_' . $node->nid,
            ),
          );
          return $links;
        }
      }
    }
  }
}

/**
 * Check if the service links should be displayed for the node type/category.
 */
function _sharethis_sharethis_this_show($type, $nid) {

  // Check to see if the node type is to display the ShareThis.
  $node_type = in_array($type, variable_get('sharethis_sharethis_this_node_types', array()), TRUE);
  $category_type = FALSE;

  // Check to see if Taxonomy is enabled and the node has a vocabulary.
  if (module_exists('taxonomy') && isset($node->vid)) {
    $terms = taxonomy_node_get_terms($nid);
    foreach ($terms as $term) {
      $category_type = in_array($term->tid, variable_get('sharethis_sharethis_this_category_types', array()), FALSE);
      if ($category_type) {

        // Stop looping because we found that the category is to be displayed.
        break;
      }
    }
  }
  return $node_type || $category_type;
}

/**
 * Adds the ShareThis JavaScript header code.
 *
 * @return
 *   Whether or not the code was added.
 */
function sharethis_add_js() {
  static $added = FALSE;
  if ($added == FALSE) {
    $added = TRUE;
    $code = variable_get('sharethis_sharethis_this_code', '');
    if (empty($code)) {
      $code = 'http://w.sharethis.com/button/sharethis.js#publisher=00000000-0000-0000-0000-000000000000&amp;type=website';
    }
    else {
      $code = str_replace(array(
        '<script type="text/javascript" src="',
        '"></script>',
      ), array(
        '',
        '',
      ), $code);
    }
    drupal_add_js(drupal_get_path('module', 'sharethis') . '/sharethis.js');
    drupal_set_html_head('<script type="text/javascript" src="' . $code . '&amp;button=false"></script>');
  }
  return $added;
}

/**
 * Creates the complete script tag to create SHARETHIS elements.
 *
 * See  for the options available.
 *
 * @param $title
 *   The name of the article.
 * @param $options
 *   The options to be passed (http://sharethis.com/publisher?type=stapi).
 * @param $chicklet
 *   (optional) The name of the chicklet to pass if desired (email, twitter, etc).
 */
function sharethis_get_js($title, $url, $chicklet = 'sharethis') {
  sharethis_add_js();
  $title = check_plain($title);
  $chickletname = $chicklet == 'sharethis' ? t('ShareThis') : ucfirst($chicklet);
  $image = drupal_get_path('module', 'sharethis') . "/images/{$chicklet}.gif";
  return theme('sharethis', $title, $url, $chicklet, $chickletname, $image);
}
function theme_sharethis($title, $url, $chicklet = 'sharethis', $chickletname = 'ShareThis', $image) {
  $image = base_path() . $image;
  return "<a href='{$url}' title='{$title}' rel='nofollow' name='{$chicklet}' class='sharethis-button'><img src='{$image}' /> {$chickletname}</a>";
}

Functions

Namesort descending Description
sharethis_add_js Adds the ShareThis JavaScript header code.
sharethis_admin_settings Menu callback; Settings administration.
sharethis_get_js Creates the complete script tag to create SHARETHIS elements.
sharethis_help Implementation of hook_help().
sharethis_link Implementation of hook_link().
sharethis_menu
sharethis_nodeapi Implementation of hook_nodeapi().
sharethis_perm Implementation of hook_perm().
theme_sharethis
_sharethis_sharethis_this_show Check if the service links should be displayed for the node type/category.