View source
<?php
require 'rrssb.config.inc';
define('RRSSB_LIBRARY_VERSION', '1.10.0');
define('RRSSB_LIBRARY_URI', 'https://github.com/kni-labs/rrssb/archive/' . RRSSB_LIBRARY_VERSION . '.zip');
define('RRSSB_IMAGE_TOKENS_DEFAULT', '[node:field_image]|[rrssbsite:logo-url]');
function rrssb_menu() {
$items['admin/config/content/rrssb'] = array(
'title' => 'Ridiculously Responsive Social Sharing Buttons',
'description' => 'Configure the Ridiculously Responsive Social Sharing Buttons to select which social buttons you would like to enable.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'rrssb_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer rrssb',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function rrssb_permission() {
return array(
'administer rrssb' => array(
'title' => t('Administer RRSSB'),
'description' => t('Permission to allow user to configure the Ridiculously Responsive Social Share Buttons.'),
'restrict access' => TRUE,
),
);
}
function rrssb_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'rrssb' && !empty($data['rrssb'])) {
foreach ($tokens as $name => $original) {
if (isset($data['rrssb'][$name])) {
$replacements[$original] = $data['rrssb'][$name];
}
}
}
if ($type == 'rrssbsite') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'logo-url':
if ($uri = theme_get_setting('logo')) {
$replacements[$original] = $uri;
}
break;
}
}
}
return $replacements;
}
function rrssb_theme($existing, $type, $theme, $path) {
return array(
'rrssb_config_buttons' => array(
'render element' => 'element',
),
'rrssb_button_list' => array(
'render element' => 'element',
),
'rrssb_button' => array(
'variables' => array(
'name' => NULL,
'button' => NULL,
'rrssb' => NULL,
),
),
);
}
function theme_rrssb_config_buttons($variables) {
$elements = $variables['element'];
drupal_add_tabledrag('rrssb_config_buttons', 'order', 'sibling', 'item-row-weight');
$header = array(
'label' => t('Button'),
'enabled' => t('Enabled'),
'username' => t('Username'),
'weight' => t('Weight'),
);
$rows = array();
foreach (element_children($elements) as $name) {
$row = array(
'class' => array(
'draggable',
),
);
foreach ($header as $fieldname => $title) {
$row['data'][] = drupal_render($elements[$name][$fieldname]);
}
$rows[] = $row;
}
return theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'rrssb_config_buttons',
),
));
}
function theme_rrssb_button_list($variables) {
$output = '<div class="rrssb-item-list">';
if ($prefix = variable_get('rrssb_prefix')) {
$output .= '<span class="rrssb-prefix">' . t($prefix) . '</span>';
}
$output .= '<ul class="rrssb-buttons">';
$output .= drupal_render_children($variables['element']);
$output .= '</ul></div>';
return $output;
}
function template_preprocess_rrssb_button(&$variables) {
extract($variables);
$key = variable_get('rrssb_follow') ? 'follow_url' : 'share_url';
$rrssb['username'] = $button['username'];
$variables['link'] = token_replace($button[$key], array(
'rrssb' => $rrssb,
), array(
'callback' => '_rrssb_urlencode',
));
}
function _rrssb_urlencode(&$replacements, $data, $options) {
$replacements = array_map('rawurlencode', $replacements);
}
function theme_rrssb_button($variables) {
extract($variables);
$class = $button['popup'] ? 'class="popup"' : '';
$output = <<<EOM
<li class="rrssb-{<span class="php-variable">$name</span>}"><a href="{<span class="php-variable">$link</span>}" {<span class="php-variable">$class</span>}><span class="rrssb-icon">{<span class="php-variable">$button</span>[<span class="php-string">'svg'</span>]}</span><span class="rrssb-text">{<span class="php-variable">$button</span>[<span class="php-string">'text'</span>]}</span></a></li>
EOM;
return $output;
}
function rrssb_block_info() {
$blocks['rrssb'] = array(
'info' => t('Ridiculously Responsive Social Share Buttons'),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
function rrssb_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'rrssb':
$block['subject'] = t('Share this content.');
$block['content'] = rrssb_get_buttons();
break;
}
return $block;
}
function rrssb_form() {
$all_buttons = rrssb_settings(TRUE);
$chosen = rrssb_get_chosen();
$form['rrssb_follow'] = array(
'#type' => 'select',
'#title' => t('Select type of buttons'),
'#options' => array(
0 => t('Share'),
1 => t('Follow'),
),
'#default_value' => variable_get('rrssb_follow'),
'#description' => t('"Share" buttons invite the visitor to share the page from your site onto their page/channel/profile. "Follow" buttons direct the visitor to your page/channel/profile.'),
);
$form['rrssb_chosen'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Configure buttons'),
'#theme' => 'rrssb_config_buttons',
);
foreach ($all_buttons as $name => $button) {
unset($require_follow);
if (!isset($button['follow_url'])) {
$require_follow = 0;
}
else {
if (!isset($button['share_url'])) {
$require_follow = 1;
}
}
$form['rrssb_chosen'][$name]['label'] = array(
'#type' => 'item',
'#markup' => $button['text'],
);
$form['rrssb_chosen'][$name]['enabled'] = array(
'#type' => 'checkbox',
'#default_value' => isset($chosen[$name]['enabled']) ? $chosen[$name]['enabled'] : FALSE,
);
if (isset($require_follow)) {
$form['rrssb_chosen'][$name]['enabled']['#states'] = array(
'visible' => array(
":input[name='rrssb_follow']" => array(
'value' => $require_follow,
),
),
);
}
if (isset($button['follow_url'])) {
$form['rrssb_chosen'][$name]['username'] = array(
'#type' => 'textfield',
'#default_value' => isset($chosen[$name]['username']) ? $chosen[$name]['username'] : '',
'#states' => array(
'visible' => array(
":input[name='rrssb_follow']" => array(
'value' => 1,
),
),
'required' => array(
":input[name='rrssb_chosen[{$name}][enabled]']" => array(
'checked' => TRUE,
),
),
),
);
}
$form['rrssb_chosen'][$name]['weight'] = array(
'#type' => 'weight',
'#default_value' => isset($chosen[$name]['weight']) ? $chosen[$name]['weight'] : 0,
'#delta' => 20,
'#attributes' => array(
'class' => array(
'item-row-weight',
),
),
);
}
$form['rrssb_prefix'] = array(
'#type' => 'textfield',
'#title' => t('Prefix text before the buttons'),
'#default_value' => variable_get('rrssb_prefix'),
'#description' => t('Put this text before the buttons. For example "Follow us" or "Share this page".'),
);
$form['rrssb_image_tokens'] = array(
'#type' => 'textfield',
'#title' => t('Tokens to use to find images'),
'#default_value' => variable_get('rrssb_image_tokens', RRSSB_IMAGE_TOKENS_DEFAULT),
'#description' => t('Enter one or more tokens, separated by |. These tokens will be tried in turn to determine the image to use in buttons.
The default value is !default which you can adapt to pick other fields or as desired.', array(
'!default' => RRSSB_IMAGE_TOKENS_DEFAULT,
)),
);
$form['#submit'][] = 'rrssb_form_submit';
$form['#validate'][] = 'rrssb_form_validate';
return system_settings_form($form);
}
function rrssb_form_validate($form, &$form_state) {
$follow = $form_state['values']['rrssb_follow'];
$follow_text = $follow ? t('Follow') : t('Share');
foreach ($form_state['values']['rrssb_chosen'] as $name => $settings) {
if ($settings['enabled']) {
if ($follow && isset($settings['username']) && !$settings['username']) {
form_set_error("rrssb_chosen[{$name}][username]", t('You must set the username to use "Follow" button for !button', array(
'!button' => $name,
)));
}
}
}
}
function rrssb_form_submit() {
cache_clear_all('rrssb_buttons', 'cache');
}
function rrssb_libraries_info() {
$libraries['rrssb'] = array(
'name' => 'Ridiculously Responsive Social Share Buttons',
'vendor url' => 'http://kurtnoble.com/labs/rrssb/',
'download url' => RRSSB_LIBRARY_URI,
'version arguments' => array(
'file' => 'package.json',
'pattern' => '/"version": "([\\d\\.]+)"/',
),
'files' => array(
'js' => array(
'js/rrssb.min.js',
),
'css' => array(
'css/rrssb.css',
),
),
);
return $libraries;
}
function rrssb_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
$requirements['rrssb']['title'] = t('RRSSB library');
$library = libraries_detect('rrssb');
if (!$library['installed']) {
$requirements['rrssb']['value'] = t('Not installed');
$requirements['rrssb']['severity'] = REQUIREMENT_ERROR;
$requirements['rrssb']['description'] = '';
}
else {
$requirements['rrssb']['value'] = $library['version'];
$compare = version_compare($library['version'], RRSSB_LIBRARY_VERSION);
if ($compare < 0) {
$requirements['rrssb']['severity'] = REQUIREMENT_ERROR;
$requirements['rrssb']['description'] = t('Library version is too old. ');
}
else {
if ($compare > 0) {
$requirements['rrssb']['severity'] = REQUIREMENT_WARNING;
$requirements['rrssb']['description'] = t('Library version is newer than has been tested. ');
}
else {
$requirements['rrssb']['severity'] = REQUIREMENT_OK;
}
}
}
if ($requirements['rrssb']['severity'] != REQUIREMENT_OK) {
$requirements['rrssb']['description'] .= t('Please download the RRSSB library using "drush rrssb-plugin" or from !link.', array(
'!link' => l(RRSSB_LIBRARY_URI, RRSSB_LIBRARY_URI),
));
}
}
return $requirements;
}
function rrssb_get_chosen() {
$defaults = array(
'email' => array(
'enabled' => TRUE,
'weight' => -20,
),
'facebook' => array(
'enabled' => TRUE,
'weight' => -19,
),
'linkedin' => array(
'enabled' => TRUE,
'weight' => -18,
),
'twitter' => array(
'enabled' => TRUE,
'weight' => -17,
),
'googleplus' => array(
'enabled' => TRUE,
'weight' => -16,
),
'pinterest' => array(
'enabled' => TRUE,
'weight' => -15,
),
);
$chosen = variable_get('rrssb_chosen', $defaults);
if (!is_array(current($chosen))) {
$weight = -20;
foreach ($chosen as $name => &$enabled) {
$enabled = array(
'enabled' => $enabled ? TRUE : FALSE,
'weight' => $weight++,
);
}
}
return $chosen;
}
function rrssb_get_buttons($node = NULL) {
if (is_null($node)) {
$node = menu_get_object();
}
$image_tokens = explode('|', variable_get('rrssb_image_tokens', RRSSB_IMAGE_TOKENS_DEFAULT));
$config = array(
'url' => array(
'[node:url]',
'[current-page:url]',
),
'title' => array(
'[node:title]',
'[current-page:title]',
),
'image' => $image_tokens,
);
foreach ($config as $param => $tokens) {
foreach ($tokens as $token) {
$rrssb[$param] = token_replace($token, array(
'node' => $node,
), array(
'clear' => TRUE,
));
if ($rrssb[$param]) {
break;
}
}
}
list($rrssb['image']) = explode(',', $rrssb['image']);
$items = array(
'#theme' => 'rrssb_button_list',
);
foreach (rrssb_settings() as $name => $button) {
$items[] = array(
'#theme' => 'rrssb_button',
'#name' => $name,
'#button' => $button,
'#rrssb' => $rrssb,
);
}
$items['#attached']['libraries_load'][] = array(
'rrssb',
);
return $items;
}
function rrssb_settings($all = FALSE) {
$buttons =& drupal_static($all ? __FUNCTION__ : __FUNCTION__ . '__all');
if (isset($buttons)) {
return $buttons;
}
if (!$all && ($cache = cache_get('rrssb_buttons'))) {
$buttons = $cache->data;
return $buttons;
}
$chosen = rrssb_get_chosen();
$defaults = array(
'enabled' => FALSE,
'weight' => 0,
'username' => '',
'popup' => TRUE,
);
$buttons = module_invoke_all('rrssb_buttons');
drupal_alter('rrssb_buttons', $buttons);
$iconsDir = libraries_get_path('rrssb') . '/icons';
foreach ($buttons as $name => &$button) {
if (isset($chosen[$name])) {
$button += $chosen[$name];
}
$button += $defaults;
$button['username'] = check_plain($button['username']);
if (!isset($button['svg'])) {
$svgfile = isset($button['svgfile']) ? $button['svgfile'] : "<icons>/{$name}.min.svg";
$svgfile = str_replace('<icons>', $iconsDir, $svgfile);
$button['svg'] = file_get_contents($svgfile);
}
if (!isset($button['text'])) {
$button['text'] = $name;
}
}
uasort($buttons, 'drupal_sort_weight');
if (!$all) {
$key = variable_get('rrssb_follow') ? 'follow_url' : 'share_url';
$buttons = array_filter($buttons, function ($button) use ($key) {
return $button['enabled'] && isset($button[$key]);
});
cache_set('rrssb_buttons', $buttons);
}
return $buttons;
}
function rrssb_form_node_type_form_alter(&$form, &$form_state) {
$form['rrssb'] = array(
'#type' => 'fieldset',
'#title' => t('Ridiculously Responsive Social Share Buttons'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#weight' => 20,
'#attributes' => array(
'class' => array(
'rrssb-node-type-settings-form',
),
),
'#access' => user_access('administer nodes'),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'rrssb') . '/rrssb.nodetype.js',
),
),
);
$form['rrssb']['show'] = array(
'#type' => 'checkbox',
'#title' => t('Show the social buttons?'),
'#default_value' => variable_get('rrssb_' . $form['#node_type']->type, 0),
);
$form['#submit'][] = 'rrssb_node_type_callback';
}
function rrssb_node_type_callback($form, &$form_state) {
variable_set('rrssb_' . $form_state['values']['type'], $form_state['complete form']['rrssb']['show']['#value']);
field_info_cache_clear();
}
function rrssb_node_prepare($node) {
if (!isset($node->rrssb)) {
$node->rrssb = variable_get("rrssb_{$node->type}", 0);
}
}
function rrssb_node_view($node, $view_mode, $langcode) {
if (variable_get('rrssb_' . $node->type) == 1) {
$node->content['rrssb'] = rrssb_get_buttons($node);
}
}
function rrssb_field_extra_fields() {
$extra = array();
foreach (node_type_get_types() as $node_type) {
if (variable_get('rrssb_' . $node_type->type) == 1) {
$extra['node'][$node_type->type]['display']['rrssb'] = array(
'label' => t('Ridiculously Responsive Social Share Buttons'),
'description' => t('A fake field to display Social buttons'),
'weight' => 10,
);
}
}
return $extra;
}