View source
<?php
require 'rrssb.config.inc';
define('RRSSB_LIBRARY_MIN_VERSION', '0.5.x');
define('RRSSB_LIBRARY_URI', 'https://github.com/AdamPS/rrssb-plus/releases');
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">';
if ($prefix = variable_get('rrssb_prefix')) {
$output .= '<div class="rrssb-prefix">' . t($prefix) . '</div>';
}
$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><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('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']) && strpos($button['follow_url'], '[rrssb:username]') !== FALSE) {
$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',
),
),
);
}
$appearance = rrssb_appearance();
$form['rrssb_appearance'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Appearance'),
);
$form['rrssb_appearance']['size'] = array(
'#type' => 'textfield',
'#title' => t('Size'),
'#size' => 5,
'#default_value' => $appearance['size'],
'#description' => t('Size, as a proportion of default size set in CSS.'),
);
$form['rrssb_appearance']['shrink'] = array(
'#type' => 'textfield',
'#title' => t('Minimum size'),
'#size' => 5,
'#default_value' => $appearance['shrink'],
'#description' => t('Minimum size to shrink buttons to, as a proportion of original size.'),
);
$form['rrssb_appearance']['regrow'] = array(
'#type' => 'textfield',
'#title' => t('Extra row size'),
'#size' => 5,
'#default_value' => $appearance['regrow'],
'#description' => t('Maximum size of buttons after they have been forced to split onto extra rows of buttons, as a proportion of original size.'),
);
$form['rrssb_appearance']['minRows'] = array(
'#type' => 'textfield',
'#title' => t('Minimum rows'),
'#size' => 5,
'#default_value' => $appearance['minRows'],
'#description' => t('Minimum number of rows of buttons. Set to a large value to create vertical layout.'),
);
$form['rrssb_appearance']['maxRows'] = array(
'#type' => 'textfield',
'#title' => t('Maximum rows'),
'#size' => 5,
'#default_value' => $appearance['maxRows'],
'#description' => t('Maximum number of rows of buttons. If more rows would be needed, instead the labels are hidden. Set to a large value to keep labels if at all possible.'),
);
$form['rrssb_appearance']['prefixReserve'] = array(
'#type' => 'textfield',
'#title' => t('Prefix reserved width'),
'#size' => 5,
'#default_value' => $appearance['prefixReserve'],
'#description' => t('Proportion of total width reserved for prefix to be inline.'),
);
$form['rrssb_appearance']['prefixHide'] = array(
'#type' => 'textfield',
'#title' => t('Prefix maximum width'),
'#size' => 5,
'#default_value' => $appearance['prefixHide'],
'#description' => t('Maximum prefix width as a proportion of total width before hiding prefix.'),
);
$form['rrssb_appearance']['alignRight'] = array(
'#type' => 'checkbox',
'#title' => t('Right-align buttons'),
'#size' => 5,
'#default_value' => $appearance['alignRight'],
'#description' => t('By default, buttons are left-aligned, with any padding added on the right. Enable this to right-align, and instead pad on the left.'),
);
$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');
cache_clear_all('rrssb:rrssb:', 'cache_block', TRUE);
rrssb_flush_caches();
}
function rrssb_flush_caches() {
if ($old = variable_get('rrssb_css_file')) {
file_unmanaged_delete($old);
}
variable_del('rrssb_css_file');
}
function rrssb_libraries_info() {
$test = variable_get('rrssb_test');
$libraries['rrssb-plus'] = array(
'name' => 'Ridiculously Responsive Social Share Buttons Plus',
'vendor url' => 'http://www.albanyweb.co.uk/rrssb-plus',
'download url' => RRSSB_LIBRARY_URI,
'version arguments' => array(
'file' => 'VERSION.txt',
'pattern' => '/([\\d\\.x]+)/',
),
'files' => array(
'js' => array(
$test ? 'js/rrssb.js' : 'js/rrssb.min.js',
),
'css' => array(
'css/rrssb.css',
),
),
'integration files' => array(
'rrssb' => array(
'js' => array(
'rrssb.init.js',
),
),
),
);
return $libraries;
}
function rrssb_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
$requirements['rrssb']['title'] = t('RRSSB+ library');
$library = libraries_detect('rrssb-plus');
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_MIN_VERSION);
if ($compare < 0) {
$requirements['rrssb']['severity'] = REQUIREMENT_ERROR;
$requirements['rrssb']['description'] = t('Library version is too old. ');
}
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,
);
}
$css = variable_get('rrssb_css_file');
if (!$css) {
$css = rrssb_gen_css();
}
$items['#attached']['libraries_load'][] = array(
'rrssb-plus',
);
$items['#attached']['css'] = array(
$css,
);
$items['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'rrssb' => rrssb_appearance(),
),
);
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-plus') . '/icons';
$follow = variable_get('rrssb_follow');
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 ($follow && isset($button['title_follow'])) {
$button['text'] = $button['title_follow'];
}
if (!isset($button['text'])) {
$button['text'] = $name;
}
}
uasort($buttons, 'drupal_sort_weight');
if (!$all) {
$key = $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_appearance() {
$appearance = variable_get('rrssb_appearance');
return $appearance;
}
function rrssb_gen_css() {
$css = "/* Auto-generated RRSSB CSS file. */\n";
$settings = rrssb_settings();
foreach ($settings as $name => $button) {
$svg = str_replace('<path ', '<path fill="#FFF" ', $button['svg']);
$svg = strtr($svg, array(
'<' => '%3C',
'>' => '%3E',
'#' => '%23',
'"' => '\'',
));
$css .= <<<EOM
.rrssb-buttons li.rrssb-{<span class="php-variable">$name</span>} a { background-color: {<span class="php-variable">$button</span>[<span class="php-string">'color'</span>]}; }
.rrssb-buttons li.rrssb-{<span class="php-variable">$name</span>} a:hover { background-color: {<span class="php-variable">$button</span>[<span class="php-string">'color_hover'</span>]}; }
.rrssb-{<span class="php-variable">$name</span>} .rrssb-icon { background: url("data:image/svg+xml,{<span class="php-variable">$svg</span>}"); }
EOM;
}
$id = substr(hash('sha256', serialize($settings) . microtime()), 0, 8);
$dir = 'public://rrssb';
$file = "{$dir}/rrssb.{$id}.css";
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
file_unmanaged_save_data($css, $file, FILE_EXISTS_REPLACE);
if ($old = variable_get('rrssb_css_file')) {
file_unmanaged_delete($old);
}
variable_set('rrssb_css_file', $file);
return $file;
}
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;
}