View source
<?php
function google_plusone_menu() {
$items['admin/config/services/google-plusone'] = array(
'title' => 'Google +1 button',
'description' => 'Configure Google Plus One button settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_plusone_admin_settings',
),
'access arguments' => array(
'administer google plusone',
),
'file' => 'google_plusone.admin.inc',
);
$items['google_plusone_demo_badge_preview'] = array(
'title' => 'Google+ Badge Preview',
'page callback' => 'google_plusone_demo_badge_preview',
'page arguments' => array(
'google_plusone_admin_settings',
),
'access arguments' => array(
'administer google plusone',
),
'file' => 'google_plusone.admin.inc',
);
return $items;
}
function google_plusone_permission() {
return array(
'administer google plusone' => array(
'title' => t('Administer Google +1 Button'),
),
'view google plusone' => array(
'title' => t('View Google +1 Button'),
),
);
}
function google_plusone_theme() {
return array(
'google_plusone_button' => array(
'variables' => array(
'node' => NULL,
'url' => NULL,
'css' => '',
'size' => '',
'syntax' => 'g:plusone',
'annotation' => 'bubble',
'width' => '250',
'alias' => 'aliased',
),
),
'google_plusone_badge' => array(
'variables' => array(
'page_id' => '',
'style' => 'standardbadge',
'width' => '300',
'theme' => 'light',
'custom_name' => '',
),
),
);
}
function google_plusone_node_view($node, $view_mode) {
$node_types = variable_get('google_plusone_node_types', array());
if (!empty($node_types[$node->type]) && user_access('view google plusone')) {
$locations = variable_get('google_plusone_node_location', array(
'full',
));
$default = array(
'annotation' => 'bubble',
'width' => '250',
'size' => '',
'css' => 'margin: 0 1em 1em 1em;float:right',
'syntax' => 'g:plusone',
'alias' => 'aliased',
);
$button_settings = array_merge($default, variable_get('google_plusone_button_settings', array()));
$button_settings['node'] = $node;
if (!empty($locations[$view_mode]) && empty($locations['link'])) {
$node->content['google_plusone'] = array(
'#markup' => theme('google_plusone_button__' . $node->type, $button_settings),
'#weight' => variable_get('google_plusone_weight', -10),
);
}
if (!empty($locations[$view_mode]) && !empty($locations['link'])) {
$node->content['links']['#links']['node_google_plusone_link'] = array(
'title' => theme('google_plusone_button__' . $node->type, $button_settings),
'html' => TRUE,
);
}
}
}
function theme_google_plusone_button($variables) {
$add_js =& drupal_static('google_plusone_js_added', FALSE);
$add_js = TRUE;
$url = '';
if (!empty($variables['node']->nid)) {
if ($variables['alias'] === 'aliased') {
$url = url('node/' . $variables['node']->nid, array(
'absolute' => TRUE,
));
}
else {
$url = url('node/' . $variables['node']->nid, array(
'absolute' => TRUE,
'alias' => TRUE,
));
}
}
elseif (!empty($variables['url'])) {
if ($variables['url'] === '<front>') {
$url = url('', array(
'absolute' => TRUE,
));
}
else {
$url = check_url($variables['url']);
}
}
$syntax = empty($variables['syntax']) ? '' : $variables['syntax'];
$data = $syntax === 'g:plusone' ? '' : 'data-';
$url = empty($url) ? '' : $data . 'href="' . $url . '" ';
$size = empty($variables['size']) ? '' : $data . 'size="' . check_plain($variables['size']) . '" ';
$annotation = empty($variables['annotation']) ? '' : $data . 'annotation="' . check_plain($variables['annotation']) . '" ';
$width = empty($variables['width']) ? '' : $data . 'width="' . check_plain($variables['width']) . '" ';
$tag_start = $syntax === 'g:plusone' ? '<g:plusone ' : '<div class="g-plusone" ';
$tag_end = $syntax === 'g:plusone' ? '></g:plusone>' : '></div>';
$button = $tag_start . $url . $size . $annotation . $width . $tag_end;
if ($variables['css'] !== 'nowrapper') {
$css = empty($variables['css']) ? '' : 'style="' . check_plain($variables['css']) . '"';
$button = '<div class="g-plusone-wrapper" ' . $css . ' >' . $button . '</div>';
}
return $button;
}
function theme_google_plusone_badge($variables) {
$add_js =& drupal_static('google_plusone_js_added', FALSE);
if (empty($variables['page_id'])) {
return '';
}
else {
$page_id = check_url($variables['page_id']);
}
$icon_sizes = array(
'smallicon' => '16',
'mediumicon' => '32',
'largeicon' => '64',
);
if (key_exists($variables['style'], $icon_sizes)) {
$custom_name = check_plain($variables['custom_name']);
$icon_size = $icon_sizes[$variables['style']];
$badge = '<a href="' . $page_id . '?prsrc=3" style="display:inline-block;text-decoration:none;color:#333;text-align:center;font:13px/16px arial,sans-serif;white-space:nowrap;">';
if ($icon_size === '64') {
$badge .= '<img src="https://ssl.gstatic.com/images/icons/gplus-64.png" alt="" style="border:0;width:64px;height:64px;margin-bottom:7px;">';
$badge .= '<br/><span style="font-weight:bold;">' . t($custom_name) . '</span><br/><span> ' . t('on') . ' Google+</span></a>';
}
elseif ($icon_size === '32') {
$badge .= '<span style="display:inline-block;font-weight:bold;vertical-align:top;margin-right:5px;margin-top:8px;">' . t($custom_name) . '</span>';
$badge .= '<span style="display:inline-block;vertical-align:top;margin-right:15px;margin-top:8px;">' . t('on') . '</span>';
$badge .= '<img src="https://ssl.gstatic.com/images/icons/gplus-32.png" alt="" style="border:0;width:32px;height:32px;"></a>';
}
else {
$badge .= '<span style="display:inline-block;font-weight:bold;vertical-align:top;margin-right:5px;margin-top:0px;">' . t($custom_name) . '</span>';
$badge .= '<span style="display:inline-block;vertical-align:top;margin-right:13px;margin-top:0px;">' . t('on') . '</span>';
$badge .= '<img src="https://ssl.gstatic.com/images/icons/gplus-16.png" alt="" style="border:0;width:16px;height:16px;"></a>';
}
}
elseif ($variables['style'] == 'page') {
$add_js = TRUE;
$width = (int) $variables['width'];
$badge = '<div class="g-page" data-rel="publisher" data-href="' . check_url($page_id) . '" data-width="' . check_plain($width) . '"></div>';
}
else {
$add_js = TRUE;
$width = (int) $variables['width'];
$height = 131;
if ($variables['style'] === 'small_badge' && $width >= 170) {
$height = 69;
}
$theme = check_plain($variables['theme']);
$syntax = 'HTML5';
$tag_start = $syntax === 'g:plus' ? '<g:plus ' : '<div class="g-plus"';
$tag_end = $syntax === 'g:plus' ? '></g:plus>' : '></div>';
$badge = '<div class="g-badge-wrapper">' . $tag_start . ' data-href="' . $page_id . '" data-theme="' . $theme . '" data-height="' . $height . '" data-width="' . $width . '"' . $tag_end . '</div>';
}
return $badge;
}
function google_plusone_page_alter(&$page) {
$add_js =& drupal_static('google_plusone_js_added', FALSE);
if ($add_js) {
$default_advanced = array(
'lang' => '',
'scope' => 'page_bottom',
'parsetags' => 'onload',
'async' => 1,
);
$config = array_merge($default_advanced, variable_get('google_plusone_advanced_settings', array()));
$script_options = array();
if (!empty($config['lang'])) {
$lang = google_plusone_button_negotiate_language($config['lang']);
$script_options[] = 'lang:"' . $lang . '"';
}
if ($config['parsetags'] !== 'onload') {
$script_options[] = 'parsetags:"' . $config['parsetags'] . '"';
}
if ($config['async']) {
$script = "<script>";
$script .= empty($script_options) ? '' : "window.___gcfg = {" . implode(',', $script_options) . "};";
$script .= "(function() {";
$script .= "var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;";
$script .= "po.src = 'https://apis.google.com/js/plusone.js';";
$script .= "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);";
$script .= "})();</script>";
}
else {
$script = '<script type="text/javascript" src="https://apis.google.com/js/plusone.js">';
$script .= empty($script_options) ? '' : '{' . implode(',', $script_options) . '}';
$script .= '</script>';
}
$page[$config['scope']]['google_plusone'] = array(
'#markup' => $script,
);
}
}
function google_plusone_button_negotiate_language($glanguages = array()) {
global $language;
$current_code = $language->language;
$glanguages = explode(",", $glanguages);
if (count($glanguages) === 1) {
return $glanguages[0];
}
if (in_array($current_code, $glanguages, TRUE)) {
return $current_code;
}
switch (drupal_substr($current_code, 0, 2)) {
case 'en':
return $current_code === 'en' ? 'en-US' : 'en-GB';
case 'zh':
return $current_code === 'zh-hans' ? 'zh-CN' : 'zh-TW';
case 'he':
return 'iw';
case 'es':
return 'es-419';
case 'nn':
return 'no';
case 'pt':
return 'pt-PT';
default:
return '';
}
}
function google_plusone_block_info() {
$blocks['google_plusone_block'] = array(
'info' => t('Google Plus One +1'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['google_plusone_badge_block'] = array(
'info' => t('Google Plus One +1 Badge'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function google_plusone_block_view($delta = '') {
if (user_access('view google plusone')) {
$block = array();
switch ($delta) {
case 'google_plusone_block':
$default = array(
'url' => '',
'annotation' => 'bubble',
'width' => '250',
'size' => '',
'css' => 'text-align:center;',
'alias' => 'aliased',
);
$settings = array_merge($default, variable_get('google_plusone_button_settings', array()));
$settings = array_merge($settings, variable_get('google_plusone_block_settings', array()));
$block['subject'] = NULL;
$block['content'] = theme('google_plusone_button__block', $settings);
return $block;
case 'google_plusone_badge_block':
$page_id = variable_get('google_plusone_badge_page_id', '');
$link = array(
'#tag' => 'link',
'#attributes' => array(
'href' => check_url($page_id),
'rel' => 'publisher',
),
);
drupal_add_html_head($link, 'google_plusone_badge_page_id');
$settings = array(
'page_id' => $page_id,
'style' => variable_get('google_plusone_badge_style', 'standardbadge'),
'width' => variable_get('google_plusone_badge_width', '300'),
'theme' => variable_get('google_plusone_badge_theme', 'light'),
'custom_name' => variable_get('google_plusone_badge_custom_name', ''),
);
$block['subject'] = NULL;
$block['content'] = theme('google_plusone_badge__block', $settings);
return $block;
}
}
}
function google_plusone_block_configure($delta = '') {
$form = array();
if ($delta == 'google_plusone_block') {
module_load_include('inc', 'google_plusone', 'google_plusone.admin');
drupal_add_js('https://apis.google.com/js/plusone.js', 'external');
drupal_add_js(drupal_get_path('module', 'google_plusone') . '/google_plusone.admin.js');
$default = array(
'url' => '',
'annotation' => 'bubble',
'width' => '250',
'size' => '',
'css' => 'text-align:center',
'alias' => 'aliased',
);
$settings = array_merge($default, variable_get('google_plusone_block_settings', array()));
$available_sizes = array(
'small' => t('Small (15px)'),
'medium' => t('Medium (20px)'),
'' => t('Standard (24px)'),
'tall' => t('Tall (60px)'),
);
$available_annotations = array(
'none' => t('None'),
'bubble' => t('Bubble (by default)'),
'inline' => t('Inline'),
);
$form['g1button'] = array(
'#type' => 'fieldset',
'#title' => t('Button Settings'),
'#description' => t('Notice that these settings are exclusively for this block and they cover the basic options.<br /> The rest of settings will be taken from the <a href="@sett">general settings</a>.', array(
'@sett' => url('admin/config/services/google-plusone'),
)),
);
$form['g1button']['google_plusone_block_url'] = array(
'#title' => t('URL address to use in the Google +1 button'),
'#type' => 'textfield',
'#default_value' => $settings['url'],
'#description' => t('3 different options: <br/><ol><li>Type an fixed, absolute URL. Remember to type the <em>http://</em> part.</li><li>Type <em><front></em></front> to use always your frontpage ($base_url), that is language-aware. </li><li>Leave empty. Then it will be used the current URL address present in that moment in that page where the block is.</li></ol>'),
);
$form['g1button']['google_plusone_block_alias'] = array(
'#type' => 'radios',
'#title' => t('Aliased node path'),
'#default_value' => $settings['alias'],
'#options' => array(
'not_aliased' => t('Not aliased'),
'aliased' => t('Aliased'),
),
'#description' => t('By default aliased. It only will be applied when the previous setting <em>URL</em> has been left empty and it\'s a node page. <br/> If you change this setting, <strong>be aware</strong> that Google+ will see them as different URLs, so the button will not keep the previous counting.'),
);
$form['g1button']['google_plusone_block_annotation'] = array(
'#type' => 'radios',
'#title' => t('Annotation: How to show the counting?'),
'#options' => $available_annotations,
'#default_value' => $settings['annotation'],
'#description' => google_plusone_build_preview_button($available_sizes),
);
$form['g1button']['google_plusone_block_width'] = array(
'#type' => 'textfield',
'#title' => t('Width (only affects to inline annotation)'),
'#default_value' => $settings['width'],
'#size' => 20,
'#description' => t('By default recommended 250 (pixels). Minimum 120'),
);
$form['g1button']['google_plusone_block_size'] = array(
'#type' => 'radios',
'#title' => t('Size'),
'#options' => $available_sizes,
'#default_value' => $settings['size'],
);
$form['g1button']['google_plusone_block_wrapper_css'] = array(
'#type' => 'textfield',
'#title' => t('Optional wrapper with CSS'),
'#maxlength' => 256,
'#default_value' => $settings['css'],
'#description' => t('To help with the layout and placement of the button, it will be rendered by default inside a wrapper: <div class="g-plusone-wrapper"> </div><br/>You can enter CSS rules to style this wrapper. By default <em>text-align:center</em><br />To disable totally the wrapper, input the word <em>nowrapper</em>'),
);
}
elseif ($delta == 'google_plusone_badge_block') {
drupal_add_js(array(
'google_plusone_preview' => url('google_plusone_demo_badge_preview'),
), 'setting');
drupal_add_js(drupal_get_path('module', 'google_plusone') . '/google_plusone_badge.admin.js');
$preview_placeholder = '<div style="float:right;margin-right:5%;border-left:1px dotted #ccc;padding:10px 20px" id="badge_preview"><div id="demo"></div>';
$preview_placeholder .= '<div style="margin:1em;width:280px;font-size:0.9em;"><p>' . t('In this sidebar you should see a real-time preview of the badge settings.') . '</p>';
$preview_placeholder .= '<p>' . t('You can help reporting any bugs in the <a href="@module" target="_blank">issue queue</a> of the module.', array(
'@module' => 'http://drupal.org/project/google_plusone',
)) . '</p>';
$preview_placeholder .= '<p>' . t('For debugging purposes, you may want to compare it with the code generated from the <a href="@preview" target="_blank">Google+ Badge Tool site</a>.', array(
'@preview' => 'https://developers.google.com/+/plugins/badge/preview',
)) . '</div>';
$available_styles = array(
'standardbadge' => t('Standard Badge'),
'smallbadge' => t('Small Badge'),
'smallicon' => t('Small Icon'),
'mediumicon' => t('Medium Icon'),
'largeicon' => t('Large Icon'),
'page' => t('Page'),
);
$form['g1badge'] = array(
'#type' => 'fieldset',
'#title' => t('Badge Settings'),
'#description' => t('Specific settings for the Badge.<br/>The rest of settings are shared with the <a href="@sett">general settings</a> of Google +1 button.', array(
'@sett' => url('admin/config/services/google-plusone'),
)) . $preview_placeholder,
);
$form['g1badge']['preview'] = array(
'#value' => $preview_placeholder,
);
$form['g1badge']['google_plusone_badge_page_id'] = array(
'#title' => t('URL address with Page ID'),
'#type' => 'textfield',
'#default_value' => variable_get('google_plusone_badge_page_id', 'https://'),
'#description' => t('The <strong>whole URL</strong> of your Google+ page, not only the Page ID. <br />For example: https://plus.google.com/101560853443212199687'),
);
$form['g1badge']['google_plusone_badge_style'] = array(
'#title' => t('Google+ Badge Style'),
'#type' => 'radios',
'#options' => $available_styles,
'#default_value' => variable_get('google_plusone_badge_style', 'standardbadge'),
'#description' => t('Icons of 16, 32 and 64 px.'),
);
$form['g1badge']['google_plusone_badge_width'] = array(
'#title' => t('Width (for Badge)'),
'#type' => 'textfield',
'#default_value' => variable_get('google_plusone_badge_width', '300'),
'#description' => t('Width of the badge in <em>px</em>. By default 300. Minimum of 100 (for Small Badge) and 170 (for Standard Badge).'),
);
$form['g1badge']['google_plusone_badge_theme'] = array(
'#title' => t('Theme color (for Badge)'),
'#type' => 'radios',
'#options' => array(
'light' => 'Light',
'dark' => 'Dark',
),
'#default_value' => variable_get('google_plusone_badge_theme', 'light'),
);
$form['g1badge']['google_plusone_badge_custom_name'] = array(
'#title' => t('Custom name (only for Icon style)'),
'#type' => 'textfield',
'#default_value' => variable_get('google_plusone_badge_custom_name', ''),
'#description' => t('Custom name of the Google+ page. It will appear only for the icon style.'),
);
}
return $form;
}
function google_plusone_block_save($delta = '', $edit = array()) {
module_load_include('inc', 'google_plusone', 'google_plusone.admin');
if ($delta == 'google_plusone_block') {
$settings = array(
'url' => $edit['google_plusone_block_url'],
'annotation' => $edit['google_plusone_block_annotation'],
'width' => $edit['google_plusone_block_width'],
'size' => $edit['google_plusone_block_size'],
'css' => google_plusone_trim($edit['google_plusone_block_wrapper_css'], ';'),
'alias' => $edit['google_plusone_block_alias'],
);
variable_set('google_plusone_block_settings', $settings);
}
elseif ($delta == 'google_plusone_badge_block') {
variable_set('google_plusone_badge_page_id', check_url(google_plusone_trim($edit['google_plusone_badge_page_id'], '/')));
variable_set('google_plusone_badge_style', $edit['google_plusone_badge_style']);
variable_set('google_plusone_badge_width', $edit['google_plusone_badge_width']);
variable_set('google_plusone_badge_theme', $edit['google_plusone_badge_theme']);
variable_set('google_plusone_badge_custom_name', $edit['google_plusone_badge_custom_name']);
}
}
function google_plusone_ds_fields_info($entity_type) {
$fields = array();
if ($entity_type == 'node') {
$ui_limit = array();
$bundles = variable_get('google_plusone_node_types', array());
foreach ($bundles as $bundle) {
$ui_limit[] = $bundle . '|*';
}
$fields['node'] = array();
$fields['node']['google_plusone_ds_field'] = array(
'title' => t('Google Plus One +1 field'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => '_google_plusone_ds_field',
'ui_limit' => $ui_limit,
);
}
return $fields;
}
function _google_plusone_ds_field($field) {
if (!user_access('view google plusone')) {
return '';
}
$node = $field['entity'];
$default = array(
'annotation' => 'bubble',
'width' => '250',
'size' => '',
'css' => 'margin: 0 1em 1em 1em;float:right',
'syntax' => 'g:plusone',
'alias' => 'aliased',
);
$button_settings = array_merge($default, variable_get('google_plusone_button_settings', array()));
$button_settings['node'] = $node;
return theme('google_plusone_button__' . $node->type, $button_settings);
}