View source
<?php
require_once dirname(__FILE__) . '/twitter_pull.class.inc';
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_flush_caches() {
return array(
TWITTER_PULL_CACHE_TABLE,
);
}
function twitter_pull_init() {
$css_path = drupal_get_path('module', 'twitter_pull') . '/twitter-pull-listing.css';
drupal_add_css($css_path);
}
function twitter_pull_render($twitkey, $title = NULL, $num_items = NULL, $themekey = NULL, $rts = NULL, $exclude_replies = NULL) {
$title = empty($title) && $title != FALSE ? t('Related Tweets') : check_plain($title);
$themekey = empty($themekey) ? 'twitter_pull_listing' : $themekey;
$num_items = empty($num_items) ? twitter_pull_num_items() : $num_items;
$tweets = twitter_pull_retrieve($twitkey, $num_items, $rts, $exclude_replies);
$ret = theme($themekey, $tweets, $twitkey, $title);
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, $rts = NULL, $exclude_replies = NULL) {
$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->rts = $rts;
$puller->exclude_replies = $exclude_replies;
$puller
->get_items();
$tweets = $puller->tweets;
} catch (Exception $e) {
watchdog('Twitter Pull', $e
->getMessage(), array(), WATCHDOG_WARNING);
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, time() + $cache_length);
}
}
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_theme() {
return array(
'twitter_pull_listing' => array(
'arguments' => array(
'tweets' => NULL,
'twitkey' => NULL,
'title' => NULL,
),
'template' => 'twitter-pull-listing',
),
);
}
function twitter_pull_block($op = 'list', $delta = 0, $edit = array()) {
$info = twitter_pull_block_data();
switch ($op) {
case 'list':
return array_map(create_function('$a', 'return array("info"=>$a->name);'), $info);
break;
case 'view':
$b_info = $info["{$delta}"];
$content = twitter_pull_render($b_info->tweetkey, $b_info->title, $b_info->number_of_items, $b_info->theme_key);
return array(
"subject" => "",
"content" => $content,
);
break;
}
}
function twitter_pull_ctools_plugin_api($module, $api) {
if ($module == 'boxes' && $api == 'plugins') {
return array(
'version' => 1,
);
}
}
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_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->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;
}
}
else {
$data = array();
}
return $data;
}