tweet.module in Tweet 6
Same filename and directory in other branches
Builds links to post pages to twitter.
File
tweet.moduleView source
<?php
/**
* @file
* Builds links to post pages to twitter.
*/
/**
* Implementation of hook_help().
*/
function tweet_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#tweet":
$output = '<p>' . t("This module builds links to post pages to twitter. See the function tweet_to_twitter() in the module file for an explanation of how to use it.") . '</p>';
break;
}
return $output;
}
/**
* Implementation of hook_link().
*/
function tweet_link($type, $node = NULL, $teaser = FALSE) {
if ($type == 'node' && in_array($node->type, variable_get('tweet_types', _tweet_node_types())) && !_tweet_exclude($node->nid)) {
$title = variable_get('tweet_title', 1);
if (!$teaser) {
$link_type = variable_get('tweet_node', 'icon');
if ($link_type != 'none') {
$links['tweet'] = _tweet_to_twitter($link_type, $title, $node->nid);
return $links;
}
}
else {
$link_type = variable_get('tweet_teaser', 'none');
if ($link_type != 'none') {
$links['tweet'] = _tweet_to_twitter($link_type, TRUE, $node->nid);
return $links;
}
}
}
}
/**
* Implementation of hook_menu().
*/
function tweet_menu() {
$items = array();
$items['admin/settings/tweet'] = array(
'title' => 'Tweet settings',
'description' => 'Adjust certain display settings for Tweet.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_admin',
),
'access arguments' => array(
'access administration pages',
),
'file' => 'tweet.admin.inc',
);
return $items;
}
/**
* Returns a link from _tweet_to_twitter().
* @see _tweet_to_twitter()
*/
function tweet_to_twitter($type = 'icon', $title = TRUE, $nid = '', $q = '') {
$array = _tweet_to_twitter($type, $title, $nid, $q);
return l($array['title'], $array['href'], array(
'attributes' => $array['attributes'],
'query' => $array['query'],
'absolute' => TRUE,
'html' => $array['html'],
));
}
/**
* Creates a link to post a URL and optionally title to twitter. Uses the current page by default.
*
* @param $type
* Specifies what will show up in the link: the twitter icon, the twitter icon and text, or just text.
* Pass 'icon' to show just the icon, 'icon_text' to show the icon and text, and 'text' to show just the text.
* Required if display options for nodes are set to 'none' on the settings page.
* @param $title
* If FALSE, no page title will be included in the twitter message; if TRUE, the pagetitle will be included and will be determined from the current page or the NID passed to the function.
* If $title is a string, the string will be used as the title. @title is replaced with the current page's title or the title as determined from the NID passed to the function.
* If 1, the default from the settings page will be used.
* @param $nid
* The NID of the node for which the twitter link should be constructed.
* @param $q
* The absolute URL of the page for which the twitter link should be constructed.
* If this is not the current page, the title must be set manually, or it will be incorrect.
* @see _tweet_make_url()
* @see _tweet_get_title()
* @see tweet_to_twitter()
* @see tweet_link()
* @return
* A themed link to post the specified or current page to twitter, optionally including the page title.
*/
function _tweet_to_twitter($type = '', $title = 1, $nid = '', $q = '') {
$hashtag = '';
if (variable_get('tweet_hashtag', '')) {
//It is necessary to manually escape the # character because if unescaped it will not be included in the message and if drupal_urlencoded() it gets encoded twice.
$hashtag = ' ' . str_replace('#', '%23', variable_get('tweet_hashtag', ''));
}
if ($nid && !$q) {
global $base_url, $base_root, $base_path;
$alias = drupal_get_path_alias('node/' . $nid);
$q = $base_root . $base_path . $alias;
}
$tweet = _tweet_make_url($q);
if ($title === 1 || $title === '1') {
$title = variable_get('tweet_title', 1);
}
if ($title) {
if (is_string($title) && $title !== '1') {
$title = str_replace('@title', _tweet_get_title($nid), $title);
$tweet .= ' ' . $title . $hashtag;
}
else {
$tweet .= ' ' . _tweet_get_title($nid) . $hashtag;
}
}
else {
//Add a space even though titles are not included so the user can begin typing a description right away.
$tweet .= ' ';
}
$path = 'http://twitter.com/home';
$text = t('Post to Twitter');
$image_location = drupal_get_path('module', 'tweet') . '/icon.png';
$image = '<img src="' . $base_url . '/' . variable_get('tweet_image', $image_location) . '" alt="' . $text . '" title="' . $text . '" />';
if (!$type) {
//Note that $type can be 'none', in which case nothing shows up.
$type = variable_get('tweet_node', 'icon');
}
if ($type == 'icon') {
$show = $image;
}
else {
if ($type == 'icon_text') {
$show = $image . ' ' . $text;
}
else {
if ($type == 'text') {
$show = $text;
}
}
}
$attributes = array(
'class' => 'tweet',
'rel' => 'nofollow',
);
if (variable_get('tweet_new_window', 'target') == 'target') {
$attributes['target'] = '_blank';
}
else {
if (variable_get('tweet_new_window', 'target') == 'js') {
$attributes['onClick'] = "window.open('{$path}?status={$tweet}','twitter','')";
$path = $_GET['q'];
$tweet = 'sent';
}
}
return array(
'title' => $show,
'href' => $path,
'attributes' => $attributes,
'query' => 'status=' . $tweet,
'html' => TRUE,
);
}
/**
* Returns the title of the node for which the NID was passed or the current page.
* Note that there is no good way to get the page title for a page that is not the current page.
* We assume the title is the same as the title of the node if a node is being viewed, but this is often not the case when certain modules are being used.
* In this case, it is recommended that you manually pass the title to tweet_to_twitter().
*
* @param $nid
* The NID of the node for which to return the title. If not passed, uses the current page.
* @see _tweet_to_twitter()
* @return
* The title of the node for the NID passed or the title of the current page.
*/
function _tweet_get_title($nid = '') {
if ($nid) {
$node = node_load(array(
'nid' => $nid,
));
$title = $node->title;
}
else {
$title = drupal_get_title();
}
if (drupal_strlen($title) > 120) {
$title = drupal_substr($title, 0, 119) . '';
}
return $title;
}
/**
* Retrieves and beautifies the abbreviated URL.
*
* @param $q
* The URL of the page for which to create the abbreviated URL. If not passed uses the current page.
* @see _tweet_to_twitter()
* @see _tweet_get_url()
* @return
* An abbreviated URL.
*/
function _tweet_make_url($q = '') {
if (!$q) {
global $base_url;
$q = $base_url . base_path() . $_GET['q'];
}
$cached = cache_get($q);
if ($cached->data) {
return $cached->data;
}
$url = _tweet_get_url($q);
//If the primary service fails, try the secondary service.
if (!$url) {
$url = _tweet_get_url($q, variable_get('tweet_service_backup', 'TinyURL'));
//If the secondary service fails, use the original URL.
if (!$url) {
$url = $q;
}
}
//Replace "http://" with "www." if the URL is abbreviated because it's shorter.
if ($url != $q) {
$url = drupal_substr($url, 7);
$url = 'www.' . $url;
}
$expire = time() + 60 * 60 * 24 * 7 * 3;
cache_set($q, $url, 'cache', $expire);
return $url;
}
/**
* Gets an abbreviated URL using either CURL or PHP from the appropriate service.
* Times out after three (3) seconds.
*
* @param $original
* The URL of the page for which to retrieve the abbreviated URL
* @param $service
* The service to use to abbreviate the URL.
* Available services include hex.io, idek.net, is.gd, lin.cr, ri.ms, th8.us, and TinyURL.
* @see _tweet_make_url()
* @return
* An abbreviated URL.
*/
function _tweet_get_url($original, $service = '') {
if (!$service) {
$service = variable_get('tweet_service', 'is.gd');
}
if ($service == 'hex.io') {
$url = 'http://hex.io/api-create.php?url=' . $original;
}
else {
if ($service == 'idek.net') {
$url = 'http://idek.net/c.php?idek-api=true&idek-ref=drupal_tweet_module&idek-url=' . $original;
}
else {
if ($service == 'is.gd') {
$url = 'http://is.gd/api.php?longurl=' . $original;
}
else {
if ($service == 'lin.cr') {
$url = 'http://lin.cr/?mode=api&full=1&l=' . $original;
}
else {
if ($service == 'ri.ms') {
$url = 'http://ri.ms/api-create.php?url=' . $original;
}
else {
if ($service == 'th8.us') {
$url = 'http://th8.us/api.php?url=' . $original;
}
else {
if ($service == 'TinyURL') {
$url = 'http://tinyurl.com/api-create.php?url=' . $original;
}
else {
return $original;
}
}
}
}
}
}
}
if (variable_get('tweet_method', 'curl') == 'php') {
$context = stream_context_create(array(
'http' => array(
'timeout' => 3,
),
));
$contents = file_get_contents($url, 0, $context);
}
else {
if (variable_get('tweet_method', 'curl') == 'curl') {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($c, CURLOPT_URL, $url);
$contents = curl_exec($c);
curl_close($c);
}
else {
$contents = $url;
}
}
if ($contents && drupal_substr($contents, 0, 7) == 'http://') {
return $contents;
}
watchdog('tweet', '%method failed to return an abbreviated URL from %service.', array(
'%method' => drupal_strtoupper(variable_get('tweet_method', 'curl')),
'%service' => variable_get('tweet_service', 'is.gd'),
), WATCHDOG_NOTICE, $url);
return FALSE;
}
/**
* Excludes certain Node IDs from displaying links.
*
* @param $nid
* The NID to check for exclusion.
* @return
* TRUE if the node should be excluded, or FALSE if it should not.
*/
function _tweet_exclude($nid) {
$exclude = explode(',', variable_get('tweet_exclude', ''));
$excludes = array();
foreach ($exclude as $check) {
$excludes[] = trim($check);
}
if (!empty($excludes)) {
if (in_array($nid, $excludes)) {
return TRUE;
}
}
return FALSE;
}
/**
* Helper function to provide node types in the format array(TYPE => TYPE).
*/
function _tweet_node_types() {
$a = array_keys(node_get_types());
$return = drupal_map_assoc($a);
return $return;
}
Functions
Name![]() |
Description |
---|---|
tweet_help | Implementation of hook_help(). |
tweet_link | Implementation of hook_link(). |
tweet_menu | Implementation of hook_menu(). |
tweet_to_twitter | Returns a link from _tweet_to_twitter(). |
_tweet_exclude | Excludes certain Node IDs from displaying links. |
_tweet_get_title | Returns the title of the node for which the NID was passed or the current page. Note that there is no good way to get the page title for a page that is not the current page. We assume the title is the same as the title of the node if a node is being… |
_tweet_get_url | Gets an abbreviated URL using either CURL or PHP from the appropriate service. Times out after three (3) seconds. |
_tweet_make_url | Retrieves and beautifies the abbreviated URL. |
_tweet_node_types | Helper function to provide node types in the format array(TYPE => TYPE). |
_tweet_to_twitter | Creates a link to post a URL and optionally title to twitter. Uses the current page by default. |