View source
<?php
function tweetbutton_help($path, $arg) {
$output = NULL;
switch ($path) {
case 'admin/help#tweetbutton':
return t('This button allows your website to let people share content on Twitter without having to leave the page. Promote strategic Twitter accounts at the same time while driving traffic to your website.');
}
}
function tweetbutton_menu() {
$items = array();
$items['admin/settings/tweetbutton/list'] = array(
'title' => 'Tweetbutton Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/settings/tweetbutton'] = array(
'title' => 'Tweetbutton Settings',
'description' => 'Configure tweetbutton settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweetbutton_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'tweetbutton.admin.inc',
'weight' => 0,
);
$items['admin/settings/tweetbutton/node'] = array(
'title' => 'Tweetbutton Node Settings',
'description' => 'Configure tweetbutton for nodes',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweetbutton_node_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'tweetbutton.admin.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function tweetbutton_theme() {
return array(
'tweetbutton_display' => array(
'variables' => array(
'entity' => NULL,
'options' => NULL,
),
),
);
}
function tweetbutton_perm() {
return array(
'access tweetbutton',
);
}
function tweetbutton_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($a3) {
$node_location = 'teaser';
}
else {
$node_location = 'full';
}
$locations = variable_get('tweetbutton_node_location', array(
'full',
));
if (in_array($node->type, variable_get('tweetbutton_node_types', array(
'story',
)), TRUE) && !empty($locations[$node_location]) && user_access('access tweetbutton') && tweetbutton_allowed_path() && $op == 'view') {
$weight = module_exists('content') ? content_extra_field_weight($node->type, 'tweetbutton_widget') : variable_get('tweetbutton_node_weight', -5);
$node->content['tweetbutton'] = array(
'#value' => theme('tweetbutton_display', $node),
'#weight' => $weight,
);
}
}
function tweetbutton_link($entity_type, $entity, $teaser = FALSE) {
$location = variable_get('tweetbutton_node_location', array(
'full',
));
if ($entity_type != 'node' || !user_access('access tweetbutton') || empty($location['links']) || !tweetbutton_allowed_path() || !in_array($entity->type, variable_get('tweetbutton_node_types', array(
'story',
)), TRUE)) {
return;
}
$links = array();
$links['tweetbutton'] = array(
'title' => theme('tweetbutton_display', $entity, array(
'type' => 'horizontal',
)),
'html' => TRUE,
);
return $links;
}
function tweetbutton_allowed_path() {
static $page_match;
if (isset($page_match)) {
return $page_match;
}
$pages = variable_get('tweetbutton_page_pages', '');
$visibility = variable_get('tweetbutton_page_visibility', '');
if ($pages) {
if ($visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
$page_match = !($visibility xor $page_match);
}
else {
$page_match = drupal_eval($pages);
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
function tweetbutton_get_attributes($entity = NULL, $options = array()) {
$default_option = array(
'type' => variable_get('tweetbutton_button', 'vertical'),
'text' => variable_get('tweetbutton_tweet_text', ''),
'language' => variable_get('tweetbutton_language', ''),
'account' => variable_get('tweetbutton_account', ''),
'rel_account' => variable_get('tweetbutton_rel_account_name', ''),
'rel_desc' => variable_get('tweetbutton_rel_account_description', ''),
'entity_type' => 'node',
'url' => url('node/' . $entity->nid, array(
'absolute' => TRUE,
)),
);
$options += $default_option;
global $language;
$attributes = array(
'data-count' => $options['type'],
'data-via' => $options['account'],
'data-related' => $options['rel_account'] . ':' . $options['rel_desc'],
'data-text' => $options['text'] ? token_replace($options['text'], $options['entity_type'], $entity) : '',
'data-counturl' => $options['url'],
'data-url' => $options['url'],
'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
);
return $attributes;
}
function theme_tweetbutton_display($entity_id, $options = array()) {
global $_tweetbutton_init;
$_tweetbutton_init = TRUE;
$attributes = tweetbutton_get_attributes($entity_id, $options);
$link = '<div class="tweetbutton"><a href="http://twitter.com/share" class="twitter-share-button" ' . drupal_attributes($attributes) . '>Tweet</a></div>';
return $link;
}
function tweetbutton_views_api() {
return array(
'api' => 2.0,
);
}
function tweetbutton_views_handlers() {
return array(
'handlers' => array(
'views_handler_field_tweetbutton_node' => array(
'parent' => 'views_handler_field_tweetbutton',
'file' => 'tweetbutton_views_field_handlers.inc',
),
'views_handler_field_tweetbutton_user' => array(
'parent' => 'views_handler_field_tweetbutton',
'file' => 'tweetbutton_views_field_handlers.inc',
),
'views_handler_field_tweetbutton_taxonomy' => array(
'parent' => 'views_handler_field_tweetbutton',
'file' => 'tweetbutton_views_field_handlers.inc',
),
'views_handler_field_tweetbutton' => array(
'parent' => 'views_handler_field',
'file' => 'tweetbutton_views_field_handlers.inc',
),
),
);
}
function tweetbutton_content_extra_fields($type) {
$types = variable_get('tweetbutton_node_types', array(
'story',
));
$location = variable_get('tweetbutton_node_location', array(
'full',
));
if (!empty($types[$type]) && empty($location['links'])) {
$extras['tweetbutton_widget'] = array(
'label' => t('Tweetbutton'),
'description' => t('The Tweetbutton widget'),
'weight' => variable_get('tweetbutton_node_weight', -5),
);
return $extras;
}
}
function tweetbutton_footer($main = 0) {
global $_tweetbutton_init;
if (!$_tweetbutton_init) {
return;
}
$output = '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
return $output;
}