You are here

function addtoany_create_buttons in AddToAny Share Buttons 7.4

Generate code for AddToAny buttons.

Parameters

array $url: If present this will be used as the URL. It can be a relative internal path or an absolute URL. Use an empty string or <front> for the home page or NULL to use the current page URL. The URL will be run through check_url().

string $title: If present this will be used as the title. Use an empty string for no title or NULL to use the current page title. The title will be run through check_plain().

Return value

string The HTML code for the buttons.

1 call to addtoany_create_buttons()
addtoany_create_node_buttons in ./addtoany.module
Generate code for AddToAny buttons for a node.

File

./addtoany.module, line 305
Stand alone module file to handle AddToAny buttons integration

Code

function addtoany_create_buttons($url = NULL, $title = NULL) {
  global $_addtoany_num, $base_path;
  $_addtoany_num++;
  $additional_html = variable_get('addtoany_additional_html', '');
  $additional_html_placement = variable_get('addtoany_additional_html_placement', 'before');

  // Default to <front> or the current path.
  if (!isset($url)) {

    // Use <front> for the front page to avoid '/node' as the final URL,
    // otherwise use current path.
    $url = drupal_is_front_page() ? '<front>' : current_path();
  }
  else {

    // Sanitize and encode URL for HTML output.
    $url = check_url($url);
  }

  // Default to the current title, using the site name for the home page.
  if (!isset($title)) {
    if (drupal_is_front_page()) {
      $title = module_exists('page_title') ? page_title_page_get_title() : check_plain(variable_get('site_name', 'Drupal'));
    }
    else {
      $title = module_exists('page_title') ? page_title_page_get_title() : drupal_get_title();
    }
  }

  // Set the link name.
  $link_name = $title;

  // Make an absolute link URL out of whatever URL/path we have been given.
  $link_url = url($url, array(
    'absolute' => TRUE,
  ));
  $buttons_size = variable_get('addtoany_buttons_size', '32');

  // Must be a 3 digit positive integer.
  $buttons_size = $buttons_size !== '32' && strlen($buttons_size) <= 3 && $buttons_size !== '' && is_numeric($buttons_size) && intval($buttons_size) == $buttons_size && $buttons_size > 0 ? $buttons_size : '32';
  $button_setting = variable_get('addtoany_image', 'share_save_171_16.png|171|16');
  if ($button_setting == 'a2a_svg_32') {
    $button_setting = 'text';
    $button_text = '';
  }
  elseif ($button_setting == 'custom') {
    $button_image = check_url(variable_get('addtoany_custom_image', ''));
    $button_width = '';
    $button_height = '';
  }
  elseif ($button_setting == 'none') {

    // Universal share button disabled.
  }
  elseif ($button_setting != 'text') {
    $button = explode('|', $button_setting);
    $button_filename = $button[0];
    $button_width = ' width="' . $button[1] . '"';
    $button_height = ' height="' . $button[2] . '"';
    $button_image = $base_path . drupal_get_path('module', 'addtoany') . '/images/' . $button_filename;
  }
  if ($button_setting != 'none') {

    // Permit blank button_text (so AddToAny Kit can set the icon) if set above,
    // or use addtoany_link_text if set.
    $button_text = isset($button_text) ? $button_text : check_plain(variable_get('addtoany_link_text', ''));
    if (strlen($button_text) > 0) {
      $button_text = ' ' . $button_text;
    }
    $button_innerHTML = $button_setting == "text" ? $button_text : sprintf('<img src="%s"%s%s %s/>%s', $button_image, $button_width, $button_height, filter_xss(variable_get('addtoany_image_attributes', 'alt="Share this"'), array()), $button_text);
    $universal_button = sprintf('<a%s href="https://www.addtoany.com/share#url=%s&amp;title=%s">%s</a>', ' class="a2a_dd addtoany_share_save"', rawurlencode($link_url), rawurlencode($link_name), $button_innerHTML);
  }
  else {
    $universal_button = '';
  }

  // If doing AJAX
  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $javascript = '';
    $kit_data_attrs = drupal_attributes(array(
      'data-a2a-url' => $link_url,
      'data-a2a-title' => $link_name,
    ));
    $kit_id_attr = '';
    $kit_target_classname = '';
  }
  else {
    $javascript = _addtoany_create_script($_addtoany_num, $link_name, $link_url);
    $kit_data_attrs = '';
    $kit_id_attr = ' id="da2a_' . $_addtoany_num . '"';

    // The ID is later removed by JS (to support AJAX, previously).
    $kit_target_classname = ' a2a_target';
  }
  return sprintf('<span class="a2a_kit%s%s addtoany_list"%s%s>
      %s
      %s
      %s
    </span>
    %s', $buttons_size != '16' ? ' a2a_kit_size_' . $buttons_size : '', $kit_target_classname, $kit_id_attr, $kit_data_attrs, $additional_html_placement == 'before' ? $additional_html : '', $universal_button, $additional_html_placement == 'after' ? $additional_html : '', $javascript);
}