View source
<?php
define('TWITTER_PULL_NUM_ITEMS', 5);
define('TWITTER_PULL_CACHE_LENGTH', 20);
define('TWITTER_PULL_EMPTY_MESSAGE', 'No Tweets');
define('TWITTER_PULL_CACHE_TABLE', 'cache_pulled_tweets');
function twitter_pull_num_items() {
return variable_get('twitter_pull_num_items', TWITTER_PULL_NUM_ITEMS);
}
function twitter_pull_cache_length() {
return variable_get('twitter_pull_cache_length', TWITTER_PULL_CACHE_LENGTH);
}
function twitter_pull_empty_message() {
return variable_get('twitter_pull_empty_message', TWITTER_PULL_EMPTY_MESSAGE);
}
function twitter_pull_menu() {
$items = array();
$items['twitter_pull_lazy/%'] = array(
'page callback' => 'twitter_pull_lazy',
'page arguments' => array(
1,
),
'access arguments' => array(
'access content',
),
);
return $items;
}
function twitter_pull_flush_caches() {
return array(
TWITTER_PULL_CACHE_TABLE,
);
}
function twitter_pull_theme() {
return array(
'twitter_pull_listing' => array(
'variables' => array(
'tweets' => NULL,
'twitkey' => NULL,
'title' => NULL,
'lazy_load' => NULL,
),
'template' => 'twitter-pull-listing',
),
);
}
function twitter_pull_preprocess(&$variables, $hook) {
switch ($hook) {
case 'twitter_pull_listing':
if (!empty($variables['tweets']) && is_array($variables['tweets'])) {
foreach ($variables['tweets'] as $key => $tweet) {
$tweet->time_ago = t('!time ago.', array(
'!time' => format_interval(time() - $tweet->timestamp),
));
$variables['tweets'][$key] = $tweet;
}
}
break;
}
}
function twitter_pull_render($twitkey, $title = NULL, $num_items = NULL, $themekey = NULL, $lazy_load = FALSE) {
drupal_add_css(drupal_get_path('module', 'twitter_pull') . '/twitter-pull-listing.css');
$lazy_id = rtrim(base64_encode($twitkey) . '-' . base64_encode($title) . '-' . (int) $num_items . '-' . $themekey, '-');
$title = empty($title) && $title != FALSE ? t('Related Tweets') : filter_xss($title);
$themekey = empty($themekey) ? 'twitter_pull_listing' : $themekey;
$num_items = empty($num_items) ? twitter_pull_num_items() : $num_items;
if (!$lazy_load) {
$tweets = twitter_pull_retrieve($twitkey, $num_items);
}
else {
$tweets = NULL;
$uri = url('twitter_pull_lazy/' . $lazy_id);
$id = uniqid('twitter-pull-lazy-');
$lazy_load = '<div class="throbber twitter-pull-lazy" id="' . $id . '">' . t('Loading...') . '</div>';
drupal_add_js('jQuery(document).ready(function () { jQuery.get("' . $uri . '", function(data) { jQuery("#' . $id . '").html(data).removeClass("throbber"); }); });', 'inline');
}
module_invoke_all('twitter_pull_modify', $tweets);
$ret = theme($themekey, array(
'tweets' => $tweets,
'twitkey' => $twitkey,
'title' => $title,
'lazy_load' => $lazy_load,
));
if (empty($ret) && !empty($tweets)) {
$errmsg = t("Non-empty list of tweets returned blank space after applying theme function. Most probably you are passing invalid/unregistered theme key or tpl file corresponding to the theme key does not yet exist. Please fix the problem.");
watchdog('Twitter Pull', $errmsg, array(), WATCHDOG_WARNING);
$ret = t('Errors occured while trying to retrieve tweets. Please check Watchdog log messages.');
}
return $ret;
}
function twitter_pull_retrieve($twitkey, $num_items = NULL) {
global $is_https;
$num_items = intval($num_items) > 0 ? intval($num_items) : twitter_pull_num_items();
$cache_key = $twitkey . '::' . $num_items;
$cache = cache_get($cache_key, TWITTER_PULL_CACHE_TABLE);
$tweets = array();
if (!empty($cache) && !empty($cache->data) && time() < $cache->expire) {
$tweets = $cache->data;
}
else {
try {
$puller = new twitter_puller($twitkey, $num_items);
$puller
->get_items();
$tweets = $puller->tweets;
} catch (Exception $e) {
watchdog('Twitter Pull', $e
->getMessage(), array(), WATCHDOG_WARNING);
if (!empty($cache) && !empty($cache->data)) {
return $cache->data;
}
else {
return twitter_pull_empty_message();
}
}
if (!empty($tweets) && is_array($tweets)) {
$cache_length = twitter_pull_cache_length() * 60;
cache_set($cache_key, $tweets, TWITTER_PULL_CACHE_TABLE, REQUEST_TIME + $cache_length);
}
}
if (!empty($tweets) && is_array($tweets) && $is_https) {
foreach ($tweets as $i => $tweet) {
$tweets[$i]->userphoto = $tweet->userphoto_https;
}
}
return $tweets;
}
function twitter_pull_add_links($text) {
$pattern = '#(https?)://([^\\s\\(\\)\\,]+)#ims';
$repl = '<a href="$1://$2" rel="nofollow" title="$1://$2">$2</a>';
$text = preg_replace($pattern, $repl, $text);
$pattern = '#@(\\w+)#ims';
$repl = '<a href="http://twitter.com/$1" rel="nofollow" title="@$1">@$1</a>';
$text = preg_replace($pattern, $repl, $text);
$pattern = '/[#]+([A-Za-z0-9-_]+)/';
$repl = '<a href="http://twitter.com/#!/search?q=%23$1" title="#$1" rel="nofollow">#$1</a>';
$text = preg_replace($pattern, $repl, $text);
return filter_xss($text);
}
function twitter_pull_block_info() {
$info = twitter_pull_block_data();
if (!empty($info) && is_array($info)) {
return array_map(create_function('$a', 'return array("info"=>$a->name);'), $info);
}
else {
return array();
}
}
function twitter_pull_block_view($delta = '') {
$info = twitter_pull_block_data();
$b_info = $info["{$delta}"];
$content = twitter_pull_render($b_info->tweetkey, $b_info->title, $b_info->number_of_items, $b_info->theme_key, $b_info->lazy_load);
return array(
"subject" => "",
"content" => $content,
);
}
function twitter_pull_block_data() {
static $data;
if (!empty($data)) {
return $data;
}
$data = module_invoke_all('twitter_pull_blocks');
drupal_alter('twitter_pull_data', $data);
if (!empty($data) && is_array($data)) {
foreach ($data as &$block) {
$block->title = empty($block->title) && $block->title !== FALSE ? $block->name : $block->title;
$block->number_of_items = empty($block->number_of_items) ? 5 : $block->number_of_items;
$block->theme_key = empty($block->theme_key) ? 'twitter_pull_listing' : $block->theme_key;
$block->lazy_load = empty($block->lazy_load) ? FALSE : $block->lazy_load;
}
}
else {
$data = array();
}
return $data;
}
function twitter_pull_ctools_plugin_api($module, $api) {
if ($module == 'boxes' && $api == 'plugins') {
return array(
'version' => 1,
);
}
}
function twitter_pull_lazy($lazy_id) {
$parameters = explode('-', $lazy_id, 4);
$twitkey = base64_decode($parameters[0]);
$title = base64_decode($parameters[1]);
$num_items = isset($parameters[2]) ? (int) $parameters[2] : NULL;
$themekey = isset($parameters[3]) ? check_plain($parameters[3]) : NULL;
print twitter_pull_render($twitkey, $title, $num_items, $themekey, FALSE);
}
function twitter_pull_boxes_plugins() {
$info = array();
$path = drupal_get_path('module', 'twitter_pull') . '/plugins';
$info['twitter'] = array(
'title' => 'Twitter Box',
'handler' => array(
'parent' => 'box',
'class' => 'twitter_pull_box',
'file' => 'twitter_pull_box.inc',
'path' => $path,
),
);
return $info;
}
function twitter_pull_admin_menu_cache_info() {
$caches['twitter_pull'] = array(
'title' => t('Twitter pull tweets'),
'callback' => '_twitter_pull_cache_clear',
);
return $caches;
}
function _twitter_pull_cache_clear() {
cache_clear_all('*', TWITTER_PULL_CACHE_TABLE, TRUE);
}