You are here

sharerich.module in Sharerich 7

Same filename and directory in other branches
  1. 8 sharerich.module
  2. 7.3 sharerich.module
  3. 7.2 sharerich.module

File

sharerich.module
View source
<?php

/**
 * @file
 * Module file for Sharerich.
 */
require_once 'includes/sharerich.tokens.inc';

/**
 * Implements hook_libraries_info().
 */
function sharerich_libraries_info() {
  return array(
    'rrssb' => array(
      'title' => 'Ridiculously Responsive Social Share Buttons',
      'vendor url' => 'http://kurtnoble.com/labs/rrssb/',
      'download url' => 'https://github.com/kni-labs/rrssb',
      'path' => '/',
      'files' => array(
        'js' => array(
          'js/rssb.min.js',
        ),
        'css' => array(
          'css/rrssb.css',
        ),
      ),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function sharerich_menu() {
  $items['admin/config/user-interface/sharerich'] = array(
    'title' => 'Sharerich',
    'description' => 'Configure the Sharerich to select which social buttons you would like to enable.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'sharerich_admin_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer sharerich',
    ),
    'file' => 'sharerich.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function sharerich_permission() {
  return array(
    'administer sharerich' => array(
      'title' => t('Administer Sharerich'),
      'description' => t('Permission to allow user to configure Sharerich.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_theme().
 */
function sharerich_theme() {
  return array(
    'sharerich_buttons' => array(
      'variables' => array(
        'title' => '',
        'item_list' => array(),
        'attributes' => array(),
      ),
      'path' => drupal_get_path('module', 'sharerich') . '/theme',
      'template' => 'sharerich-buttons',
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function sharerich_block_info() {
  $blocks['sharerich'] = array(
    'info' => t('Sharerich'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function sharerich_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'sharerich':
      $block['content'] = sharerich_get_buttons();
  }
  return $block;
}

/**
 * Returns markup with list of share buttons.
 */
function sharerich_get_buttons($node = NULL) {
  $key = 0;
  if (!is_null($node)) {
    $key = $node->nid;
  }
  $buttons =& drupal_static(__FUNCTION__ . $key, array());
  if (empty($buttons)) {
    $services = sharerich_get_services();
    foreach ($services as $service_name) {
      $content = sharerich_get_service_content($service_name);
      if (!empty($content)) {
        $buttons[$service_name] = array(
          'data' => $content,
          'class' => array(
            $service_name,
          ),
        );
      }
    }
    $buttons = sharerich_reorder_buttons($buttons);

    // Allow other modules to alter the buttons markup.
    drupal_alter('sharerich_buttons', $buttons, $node);

    // Tokens replacements.
    foreach ($buttons as $key => $button) {
      $buttons[$key]['data'] = token_replace($button['data'], array(
        'node' => $node,
      ));
    }
  }
  $attributes = array(
    'class' => array(
      'sharerich-buttons',
      'rrssb-buttons',
      'clearfix',
    ),
  );
  if (count($buttons) > 0) {

    // Create an item list for the button links.
    $item_list = array(
      '#theme' => 'item_list',
      '#items' => $buttons,
      '#type' => 'ul',
      '#attributes' => $attributes,
    );

    // Output using the sharerich_buttons theme.
    $sharerich_buttons = array(
      '#theme' => 'sharerich_buttons',
      '#item_list' => $item_list,
      '#title' => variable_get('sharerich_title', t('Share This')),
    );
    return $sharerich_buttons;
  }
}

/**
 * Process the sharerich buttons.
 */
function template_process_sharerich_buttons(&$vars) {
}

/**
 * Implements hook_sharerich_buttons_alter().
 */
function hook_sharerich_buttons_alter(&$buttons) {
}

/**
 * Helper to return the services.
 */
function sharerich_get_services() {
  $defaults = array(
    'email',
    'facebook',
    'linkedin',
    'twitter',
    'googleplus',
    'pinterest',
  );
  return variable_get('sharerich_services', $defaults);
}

/**
 * Helper to load services from the disk.
 */
function sharerich_load_service($service_name) {
  $dir = drupal_get_path('module', 'sharerich') . '/services';
  $service_name = $dir . '/' . $service_name . '.inc';
  if (file_exists($service_name)) {
    return file_get_contents($service_name);
  }
  return FALSE;
}

/**
 * Helper to get the service content from file or variables.
 */
function sharerich_get_service_content($service_name) {
  $var_name = 'sharerich_custom_' . $service_name;
  $var_get = variable_get($var_name, '');
  if (!empty($var_get)) {
    $content = variable_get($var_name);
  }
  else {
    $content = sharerich_load_service($service_name);
  }
  return $content;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function sharerich_form_node_type_form_alter(&$form, &$form_state) {
  $form['sharerich'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sharerich'),
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
    '#weight' => 20,
    '#attributes' => array(
      'class' => array(
        'sharerich-node-type-settings-form',
      ),
    ),
    '#access' => user_access('administer nodes'),
  );
  $entity_type = $form['#node_type']->type;
  $form['sharerich']['show'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Sharerich for this content type.'),
    '#default_value' => variable_get('sharerich_node_' . $entity_type, FALSE),
    '#suffix' => t('After enabling Sharerich you need to configure each display on Manage display tag.<br/>Please visit the <a href="/admin/config/user-interface/sharerich">configuration page</a> to customize the share buttons.'),
  );
  $form['#submit'][] = 'sharerich_node_type_callback';
}

/**
 * Custom handler to save sharerich info.
 */
function sharerich_node_type_callback(&$form, &$form_state) {
  $entity_type = $form['#node_type']->type;
  $var_name = 'sharerich_node_' . $form_state['values']['type'];
  $var_get = variable_get($var_name);
  $form_value = $form_state['complete form']['sharerich']['show']['#value'];

  // When Sharerich is enabled for the first time on this content type,
  // redirect to the display settings.
  if (empty($var_get) && $form_value == TRUE) {
    drupal_set_message(t('Please configure the field Sharerich for each Display.'));
    $form_state['redirect'] = 'admin/structure/types/manage/' . $entity_type . '/display';
  }
  variable_set($var_name, $form_value);
  field_info_cache_clear();
}

/**
 * Implements hook_node_prepare().
 */
function sharerich_node_prepare($node) {
  if (!isset($node->sharerich)) {
    $node->sharerich = variable_get('sharerich_node_{$node->type}', FALSE);
  }
}

/**
 * Implements hook_node_view().
 */
function sharerich_node_view($node, $view_mode, $langcode) {
  $extra = sharerich_field_extra_fields();

  // Check that we're supporting the node type being viewed.
  if (empty($extra['node'][$node->type]['display'])) {
    return;
  }
  $config = field_bundle_settings('node', $node->type);
  foreach ($extra['node'][$node->type]['display'] as $field_name => $field_info) {

    // Check to make sure this field is visible in this view mode.
    $displays =& $config['extra_fields']['display'][$field_name];
    if (isset($displays['default']) && !isset($displays['full'])) {

      // Fallback for Default display when viewing full view node.
      $displays['full'] = $displays['default'];
    }
    if (isset($displays[$view_mode]) && $displays[$view_mode]['visible'] == FALSE) {
      continue;
    }
    if ($field_name == 'sharerich') {
      $node->content['sharerich'] = sharerich_get_buttons($node);
    }
  }
}

/**
 * Implements hook_field_extra_fields().
 */
function sharerich_field_extra_fields() {
  $extra = array();
  foreach (node_type_get_types() as $node) {
    if (variable_get('sharerich_node_' . $node->type, FALSE)) {
      $extra['node'][$node->type]['display'] = array(
        'sharerich' => array(
          'label' => t('Sharerich'),
          'description' => t('This is a fake field, you can configure it on each Display.'),
          'weight' => 100,
          'visible' => TRUE,
        ),
      );
    }
  }
  return $extra;
}

/**
 * Implements hook_page_build().
 */
function sharerich_page_build() {
  if (path_is_admin(current_path())) {
    return;
  }

  // Add CSS from the configuration page.
  $css = explode('|', preg_replace('/(\\r\\n?|\\n)/', '|', variable_get('sharerich_add_css')));
  foreach ($css as $key => $url) {
    if (!empty($url)) {
      if (stripos($url, 'http') !== FALSE) {
        drupal_add_css($url, 'external');
      }
      else {
        drupal_add_css($url);
      }
    }
  }

  // Add Js from the configuration page.
  $js = explode('|', preg_replace('/(\\r\\n?|\\n)/', '|', variable_get('sharerich_add_js')));
  foreach ($js as $key => $url) {
    if (!empty($url)) {
      if (stripos($url, 'http') !== FALSE) {
        drupal_add_js($url, 'external');
      }
      else {
        drupal_add_js($url, array(
          'scope' => 'footer',
        ));
      }
    }
  }

  // Backwards compatibility to jQuery.live.
  $js = <<<JS
(function(\$) {
  typeof \$ && typeof \$.fn.on !== "function" && \$.fn.extend({
     on: function(event, callback, fallback) {
       switch (typeof callback) {
         case "function": return this.live(event, callback);
         case "string"  : return \$(callback).live(event, fallback);
       }
     }
   });
}(jQuery));
JS;
  drupal_add_js($js, array(
    'type' => 'inline',
    'scope' => 'header',
  ));
}

/**
 * Helper to reorder buttons.
 *
 * This is temporary until we build drag and drop of services in the admin UI.
 */
function sharerich_reorder_buttons($buttons, $order = array(
  'facebook',
  'twitter',
  'linkedin',
  'email',
)) {
  $tmp = array();
  foreach ($order as $service_name) {
    $tmp[$service_name] = $buttons[$service_name];
    unset($buttons[$service_name]);
  }
  return array_merge($tmp, $buttons);
}

/**
 * Implements hook_google_analytics_et_api().
 */
function sharerich_google_analytics_et_api() {
  $selectors = array(
    array(
      'event' => 'mousedown',
      'selector' => '.sharerich-buttons li',
      'category' => 'Share Button',
      'action' => '!text',
      'label' => '!currentPage',
      'value' => 0,
      'noninteraction' => FALSE,
    ),
  );
  return $selectors;
}

Functions

Namesort descending Description
hook_sharerich_buttons_alter Implements hook_sharerich_buttons_alter().
sharerich_block_info Implements hook_block_info().
sharerich_block_view Implements hook_block_view().
sharerich_field_extra_fields Implements hook_field_extra_fields().
sharerich_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
sharerich_get_buttons Returns markup with list of share buttons.
sharerich_get_services Helper to return the services.
sharerich_get_service_content Helper to get the service content from file or variables.
sharerich_google_analytics_et_api Implements hook_google_analytics_et_api().
sharerich_libraries_info Implements hook_libraries_info().
sharerich_load_service Helper to load services from the disk.
sharerich_menu Implements hook_menu().
sharerich_node_prepare Implements hook_node_prepare().
sharerich_node_type_callback Custom handler to save sharerich info.
sharerich_node_view Implements hook_node_view().
sharerich_page_build Implements hook_page_build().
sharerich_permission Implements hook_permission().
sharerich_reorder_buttons Helper to reorder buttons.
sharerich_theme Implements hook_theme().
template_process_sharerich_buttons Process the sharerich buttons.