View source
<?php
require_once 'includes/sharerich.tokens.inc';
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',
),
),
),
);
}
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;
}
function sharerich_permission() {
return array(
'administer sharerich' => array(
'title' => t('Administer Sharerich'),
'description' => t('Permission to allow user to configure Sharerich.'),
'restrict access' => TRUE,
),
);
}
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',
),
);
}
function sharerich_block_info() {
$blocks['sharerich'] = array(
'info' => t('Sharerich'),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
function sharerich_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'sharerich':
$block['content'] = sharerich_get_buttons();
}
return $block;
}
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);
drupal_alter('sharerich_buttons', $buttons, $node);
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) {
$item_list = array(
'#theme' => 'item_list',
'#items' => $buttons,
'#type' => 'ul',
'#attributes' => $attributes,
);
$sharerich_buttons = array(
'#theme' => 'sharerich_buttons',
'#item_list' => $item_list,
'#title' => variable_get('sharerich_title', t('Share This')),
);
return $sharerich_buttons;
}
}
function template_process_sharerich_buttons(&$vars) {
}
function hook_sharerich_buttons_alter(&$buttons) {
}
function sharerich_get_services() {
$defaults = array(
'email',
'facebook',
'linkedin',
'twitter',
'googleplus',
'pinterest',
);
return variable_get('sharerich_services', $defaults);
}
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;
}
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;
}
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';
}
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'];
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();
}
function sharerich_node_prepare($node) {
if (!isset($node->sharerich)) {
$node->sharerich = variable_get('sharerich_node_{$node->type}', FALSE);
}
}
function sharerich_node_view($node, $view_mode, $langcode) {
$extra = sharerich_field_extra_fields();
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) {
$displays =& $config['extra_fields']['display'][$field_name];
if (isset($displays['default']) && !isset($displays['full'])) {
$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);
}
}
}
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;
}
function sharerich_page_build() {
if (path_is_admin(current_path())) {
return;
}
$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);
}
}
}
$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',
));
}
}
}
$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',
));
}
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);
}
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;
}