You are here

addthis.module in AddThis 6.2

Stand alone module file to handle AddThis button integration

File

addthis.module
View source
<?php

/**
 * @file
 * Stand alone module file to handle AddThis button integration
 */

/**
 * Implementation of hook_perm().
 */
function addthis_perm() {
  $perms[] = 'administer addthis';
  $perms[] = 'view addthis';
  return $perms;
}

/**
 * Implementation of hook_link().
 */
function addthis_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  if ($type === 'node' && user_access('view addthis')) {
    if ($node && variable_get('addthis_nodetype_' . $node->type, 1) && ($teaser && variable_get('addthis_display_in_teasers', '0') || !$teaser && variable_get('addthis_display_in_links', '0'))) {
      $links['addthis'] = array(
        'title' => _addthis_create_button($node, $teaser),
        'html' => TRUE,
      );
    }
  }
  return $links;
}

/**
 * Implementation of hook_menu().
 */
function addthis_menu() {
  $items = array();
  $items['admin/settings/addthis'] = array(
    'title' => 'AddThis',
    'description' => 'Set username and customize look and feel for <a href="http://www.addthis.com/">AddThis</a> button.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'addthis_admin_settings',
    ),
    'access arguments' => array(
      'administer addthis',
    ),
    'file' => 'addthis.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function addthis_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('AddThis button');
    return $blocks;
  }
  elseif ($op == 'view' && user_access('view addthis')) {
    $block['subject'] = t('AddThis');
    $block['content'] = _addthis_create_button();
    return $block;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function addthis_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['workflow']['addthis_nodetype'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show AddThis button on this type of node'),
      '#default_value' => variable_get('addthis_nodetype_' . $form['#node_type']->type, 1),
      '#description' => t('Select whether the AddThis button should be shown or not on this type of node.'),
    );
  }
}

/**
 * Implementation of hook_theme().
 */
function addthis_theme() {
  return array(
    'addthis_button' => array(
      'arguments' => array(
        'node' => NULL,
        'teaser' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_views_api().
 */
function addthis_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'addthis'),
  );
}

/**
 * Implementation of hook_ds_fields().
 */
function addthis_ds_fields($type_name, $build_mode, $extra) {
  if (variable_get('addthis_nodetype_' . $type_name, 1)) {
    return array(
      'nd' => array(
        'addthis' => array(
          'title' => t('AddThis'),
          'type' => DS_FIELD_TYPE_FUNCTION,
          'status' => DS_FIELD_STATUS_STATIC,
          'properties' => array(
            'formatters' => array(
              '_addthis_ds_create_button' => t('AddThis'),
            ),
          ),
        ),
      ),
    );
  }
}

/**
 * Internal function for Display Suite to return the code for AddThis button.
 */
function _addthis_ds_create_button($field) {
  if (isset($field['object'])) {
    return _addthis_create_button($field['object'], TRUE);
  }
}

/**
 * Internal function to generate code for AddThis button
 *
 * @return
 *   String containing html code for the button
 */
function _addthis_create_button($node = NULL, $teaser = FALSE) {
  global $_addthis_counter;
  $_addthis_counter++;
  $brand = variable_get('addthis_brand', '');
  if ($brand == '') {
    $brand = variable_get('site_name', 'Drupal');
  }
  $addthis_options = variable_get('addthis_options', 'favorites, email, digg, delicious, myspace, facebook, google, live, more');
  $addthis_options = str_replace("\n", "", $addthis_options);
  $addthis_options = str_replace("\r", "", $addthis_options);
  if ($_addthis_counter == 1) {
    drupal_add_css(drupal_get_path('module', 'addthis') . '/addthis.css');
    drupal_add_js(sprintf('
	    addthis_pub = \'%s\';
	    addthis_logo = \'%s\';
	    addthis_logo_background = \'%s\';
	    addthis_logo_color = \'%s\';
	    addthis_brand = \'%s\';
	    addthis_options = \'%s\';
	    addthis_disable_flash = \'%s\';
	', addslashes(check_plain(variable_get('addthis_username', 'my-username'))), addslashes(check_plain(variable_get('addthis_logo', 'http://www.addthis.com/images/yourlogo.png'))), addslashes(check_plain(variable_get('addthis_logo_background', 'EFEFFF'))), addslashes(check_plain(variable_get('addthis_logo_color', '666699'))), addslashes(check_plain($brand)), addslashes(check_plain($addthis_options)), variable_get('addthis_disable_flash', FALSE) ? 'true' : 'false'), 'inline');
  }
  return theme('addthis_button', $node, $teaser);
}

/**
 * Theme the AddThis button.
 */
function theme_addthis_button($node, $teaser) {
  global $_addthis_counter;
  if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
    $secure = TRUE;
    $image_url = addslashes(check_plain(variable_get('addthis_image_secure', 'https://secure.addthis.com/button1-share.gif')));
  }
  else {
    $secure = FALSE;
    $image_url = addslashes(check_plain(variable_get('addthis_image', 'http://s9.addthis.com/button1-share.gif')));
  }

  // Fix IE's bug.
  if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
    $protocol = $secure ? 'https://' : 'http://';
    drupal_add_link(array(
      'rel' => 'stylesheet',
      'type' => 'text/css',
      'href' => $protocol . "s7.addthis.com/static/r07/widget02.css",
    ));
  }
  if (variable_get('addthis_dropdown_disabled', '0')) {
    $button = sprintf('
      <a class="addthis-button" href="http://www.addthis.com/bookmark.php"
        onclick="addthis_url   = location.href; addthis_title = document.title; return addthis_click(this);">
      <img src="%s" width="%d" height="%d" alt="%s" class="%s" /></a>', $image_url, variable_get('addthis_image_width', '125'), variable_get('addthis_image_height', '16'), trim(check_plain(strip_tags(variable_get('addthis_image_attributes_alt', 'share')))), trim(check_plain(strip_tags(variable_get('addthis_image_attributes_class', 'addthisimage')))));
  }
  else {
    $button = sprintf('
      <a class="addthis-button" href="http://www.addthis.com/bookmark.php"
        onfocus="return addthis_open(this, \'\', \'%s\', \'%s\')"
        onmouseover="return addthis_open(this, \'\', \'%s\', \'%s\')"
        onblur="addthis_close()"
        onmouseout="addthis_close()"
        onclick="return addthis_sendto()"><img src="%s" width="%d" height="%d" alt="%s" class="%s" /></a>', $teaser ? url('node/' . $node->nid, array(
      'absolute' => 1,
    )) : '[URL]', $teaser ? addslashes($node->title) : '[TITLE]', $teaser ? url('node/' . $node->nid, array(
      'absolute' => 1,
    )) : '[URL]', $teaser ? addslashes($node->title) : '[TITLE]', $image_url, variable_get('addthis_image_width', '125'), variable_get('addthis_image_height', '16'), trim(check_plain(strip_tags(variable_get('addthis_image_attributes_alt', 'share')))), trim(check_plain(strip_tags(variable_get('addthis_image_attributes_class', 'addthisimage')))));
    if ($_addthis_counter == 1) {
      $button .= sprintf('<script type="text/javascript" src="%s/js/%d/addthis_widget.js">', $secure ? 'https://secure.addthis.com' : 'http://s7.addthis.com', variable_get('addthis_widget_version', '152'));
      $button .= '</script>';
    }
  }
  return $button;
}

Functions

Namesort descending Description
addthis_block Implementation of hook_block().
addthis_ds_fields Implementation of hook_ds_fields().
addthis_form_alter Implementation of hook_form_alter().
addthis_link Implementation of hook_link().
addthis_menu Implementation of hook_menu().
addthis_perm Implementation of hook_perm().
addthis_theme Implementation of hook_theme().
addthis_views_api Implementation of hook_views_api().
theme_addthis_button Theme the AddThis button.
_addthis_create_button Internal function to generate code for AddThis button
_addthis_ds_create_button Internal function for Display Suite to return the code for AddThis button.