You are here

widget_services.module in Service links 6.2

Same filename and directory in other branches
  1. 7.2 services/widget_services.module

Extends Service Links with dynamic buttons.

@author Fabio Mucciante (TheCrow)

File

services/widget_services.module
View source
<?php

/**
 * @file
 * Extends Service Links with dynamic buttons.
 *
 * @author Fabio Mucciante (TheCrow)
 */

/**
 * Implementation of hook_service_links().
 */
function widget_services_service_links() {
  $links = array();
  $links['facebook_share'] = array(
    'name' => t('Share on Facebook'),
    'link' => 'http://www.facebook.com/sharer.php',
    'description' => t('Share this post on Facebook'),
    'javascript' => 'facebook_share.js',
    'icon' => 'facebook.png',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_facebook_share_data',
  );
  $links['twitter_widget'] = array(
    'name' => 'Tweet Widget',
    'link' => 'http://twitter.com/share?url=<raw-encoded-short-url>&count=<data-count>&via=<data-via>&text=<raw-encoded-title>&counturl=<raw-encoded-url>',
    'icon' => 'twitter.png',
    'description' => t('Tweet This'),
    'attributes' => array(
      'class' => 'twitter-share-button',
    ),
    'javascript' => 'twitter_button.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_twitter_widget_data',
  );
  $links['facebook_like'] = array(
    'name' => 'Facebook Like',
    'link' => 'http://www.facebook.com/plugins/like.php?href=<encoded-url>&layout=<layout>&show_faces=<show_faces>&action=<action>&colorscheme=<colorscheme>&width=<width>&height=<height>&font=<font>&locale=<locale>',
    'icon' => 'facebook.png',
    'description' => t('I Like it'),
    'javascript' => 'facebook_like.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_facebook_like_data',
  );
  $links['google_plus_one'] = array(
    'name' => 'Google Plus One',
    'link' => '<url>',
    'description' => t('Plus it'),
    'javascript' => 'google_plus_one.js',
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_google_plus_one_data',
  );
  $links['linkedin_share_button'] = array(
    'name' => 'Linkedin Share Button',
    'link' => '<url>',
    'icon' => 'linkedin.png',
    'description' => t('Share on Linkedin'),
    'javascript' => array(
      'linkedin_share_button.js',
    ),
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'callback' => 'ws_linkedin_share_button_data',
  );
  $links['pinterest_button'] = array(
    'name' => 'Pinterest',
    //'link' => 'http://pinterest.com/pin/create/button/?url=<raw-encoded-url>&description=<pinterest-description>&media=<pinterest-media>',
    'link' => 'http://pinterest.com/pin/create/button/?url=<raw-encoded-long-url>&description=<pinterest-description>&media=<pinterest-media>',
    'icon' => 'pinterest.png',
    'description' => t('Pin It'),
    'javascript' => array(
      'pinterest_button.js',
    ),
    'style' => SERVICE_LINKS_STYLE_EMPTY,
    'attributes' => array(
      'class' => 'pin-it-button',
    ),
    'callback' => 'ws_pinterest_button_data',
    'preset' => 'ws_pinterest_button_tags',
  );
  return $links;
}

/**
 * Callback function for Twitter Widget.
 */
function ws_twitter_widget_data(&$service, $subst) {
  static $vars;
  if (!isset($vars)) {
    $vars['tags'] = array(
      'count' => '<data-count>',
      'via' => '<data-via>',
    );
    $vars['subst'] = array(
      'count' => check_plain(variable_get('service_links_tw_data_count', 'horizontal')),
      'via' => check_plain(variable_get('service_links_tw_data_via', '')),
    );
  }
  $service['url'] = str_replace($vars['tags'], $vars['subst'], $service['url']);
}

/**
 * Callback function for Facebook Share.
 */
function ws_facebook_share_data(&$service, $subst) {
  static $fs_settings;
  if (!isset($fs_settings)) {
    $fs_settings = array(
      'type' => check_plain(variable_get('service_links_fs_type', 'button_count')),
      'app_id' => check_plain(variable_get('service_links_fs_app_id', '150123828484431')),
      'css' => check_plain(preg_replace('/[^0-9a-z\\-:;]/', '', variable_get('service_links_fs_css', ''))),
    );
    drupal_add_js(array(
      'ws_fs' => $fs_settings,
    ), 'setting');
  }
  $service['attributes']['rel'] = $subst['url'];
}

/**
 * Callback function for Facebook Like.
 */
function ws_facebook_like_data(&$service, $subst) {
  static $vars;
  if (!isset($vars)) {
    $vars['tags'] = array(
      'layout' => '<layout>',
      'show_faces' => '<show_faces>',
      'action' => '<action>',
      'colorscheme' => '<colorscheme>',
      'font' => '<font>',
      'width' => '<width>',
      'height' => '<height>',
      'locale' => '<locale>',
    );
    $vars['subst'] = array(
      'layout' => check_plain(variable_get('service_links_fl_layout', 'button_count')),
      'show_faces' => check_plain(variable_get('service_links_fl_show_faces', 'false')),
      'action' => check_plain(variable_get('service_links_fl_action', 'like')),
      'colorscheme' => check_plain(variable_get('service_links_fl_colorscheme', 'light')),
      'font' => check_plain(variable_get('service_links_fl_font', '')),
      'width' => (int) variable_get('service_links_fl_width', 100),
      'height' => (int) variable_get('service_links_fl_height', 21),
      'locale' => check_plain(variable_get('service_links_fl_locale', '')),
    );
    $fl_settings = array(
      'width' => $vars['subst']['width'],
      'height' => $vars['subst']['height'],
    );
    drupal_add_js(array(
      'ws_fl' => $fl_settings,
    ), 'setting');
  }
  $service['url'] = str_replace($vars['tags'], $vars['subst'], $service['url']);
}

/**
 * Callback function for Google Plus One.
 */
function ws_google_plus_one_data(&$service, $subst) {
  static $gpo_settings;
  if (!isset($gpo_settings)) {
    $gpo_settings = array(
      'size' => check_plain(variable_get('service_links_gpo_size', '')),
      'annotation' => check_plain(variable_get('service_links_gpo_annotation', '')),
      'lang' => check_plain(variable_get('service_links_gpo_lang', '')),
      'callback' => check_plain(variable_get('service_links_gpo_callback', '')),
      'width' => (int) variable_get('service_links_gpo_width', 300),
    );
    drupal_add_js(array(
      'ws_gpo' => $gpo_settings,
    ), 'setting');
  }
}

/**
 * Callback function for Linkedin Share Button.
 */
function ws_linkedin_share_button_data(&$service, $subst) {
  static $lsb_settings;
  if (!isset($lsb_settings)) {
    if (!module_exists('jquery_update')) {
      drupal_set_message(t('Linkedin Share Button requires <a href="@url">Jquery Update 2.x</a> to work correctly.', array(
        '@url' => 'http://drupal.org/project/jquery_update',
      )), 'warning', FALSE);
    }
    $lsb_settings = array(
      'countmode' => check_plain(variable_get('service_links_lsb_countmode', 'right')),
    );
    drupal_add_js(array(
      'ws_lsb' => $lsb_settings,
    ), 'setting');
  }
}

/**
 * Preset function for Pinterest Button, which fill the media tag.
 */
function ws_pinterest_button_tags(&$service, &$settings, $node = NULL) {
  static $tokens;
  if (!isset($tokens)) {
    $tokens['media'] = variable_get('service_links_pb_mediatoken', '');
    $tokens['description'] = variable_get('service_links_pb_descriptiontoken', '');
  }
  $settings['tag']['pinterest-media'] = '<pinterest-media>';
  if (module_exists('token')) {
    $settings['subst']['pinterest-media'] = rawurlencode(@token_replace($tokens['media'], 'node', $node));
    if (empty($tokens['description'])) {
      $service['link'] = str_replace('pinterest-description', 'raw-encoded-teaser', $service['link']);
    }
    else {
      $settings['tag']['pinterest-description'] = '<pinterest-description>';
      $settings['subst']['pinterest-description'] = rawurlencode(@token_replace($tokens['description'], 'node', $node));
    }
  }
  else {
    $settings['subst']['pinterest-media'] = '';
    $service['link'] = str_replace('pinterest-description', 'raw-encoded-teaser', $service['link']);
  }
}

/**
 * Callback function for Pinterest Button.
 */
function ws_pinterest_button_data(&$service, $subst) {
  static $pb_settings;
  if (!isset($pb_settings)) {
    $pb_settings = array(
      'countlayout' => check_plain(variable_get('service_links_pb_countlayout', 'horizontal')),
    );
    drupal_add_js(array(
      'ws_pb' => $pb_settings,
    ), 'setting');
  }
}

/**
 * Implementation of hook_menu().
 */
function widget_services_menu() {
  $items = array();
  $items['admin/settings/service_links/widgets'] = array(
    'title' => 'Widgets',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'widget_services_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'parent' => 'admin/settings/service_links',
    'file' => 'widget_services.admin.inc',
  );
  return $items;
}

Functions

Namesort descending Description
widget_services_menu Implementation of hook_menu().
widget_services_service_links Implementation of hook_service_links().
ws_facebook_like_data Callback function for Facebook Like.
ws_facebook_share_data Callback function for Facebook Share.
ws_google_plus_one_data Callback function for Google Plus One.
ws_linkedin_share_button_data Callback function for Linkedin Share Button.
ws_pinterest_button_data Callback function for Pinterest Button.
ws_pinterest_button_tags Preset function for Pinterest Button, which fill the media tag.
ws_twitter_widget_data Callback function for Twitter Widget.