Twitter for Wordpress 1.9.7 by Ricardo González.
Author: Alexander Hofbauer
Author URI: http://derhofbauer.at/blog
*/
/* Copyright 2007 Ricardo González Castro (rick[in]jinlabs.com)
* Copyright 2010 Alexander Hofbauer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!defined('AJAXED_TWITTER_FRAMEWORK')) define('AJAXED_TWITTER_FRAMEWORK', 'jquery');
if (!class_exists("AJAXedTwitter")) { class AJAXedTwitter
{
private static $options = false;
public static $default_options = array(
'id' => 'ajaxed-twitter-for-wordpress',
'title' => 'Tweets',
'username' => '',
'num' => 5,
'list' => true,
'update' => true,
'linked' => '#',
'hyperlinks' => true,
'twitter-users' => true,
'encode-utf8' => false,
'retries' => 2,
'animate' => true,
'cache-age' => 1800,
'timeout' => 2
);
public static $option_fields = array(
'title' => array('label' => 'Title', 'type' => 'text'),
'username' => array('label' => 'Username', 'type' => 'text'),
'num' => array('label' => 'Number of links', 'type' => 'text'),
'list' => array('label' => 'Ouput as unordered list', 'type' => 'checkbox'),
'update' => array('label' => 'Show timestamps', 'type' => 'checkbox'),
'linked' => array('label' => 'Linked', 'type' => 'text'),
'hyperlinks' => array('label' => 'Discover Hyperlinks', 'type' => 'checkbox'),
'twitter-users' => array('label' => 'Discover @replies', 'type' => 'checkbox'),
'encode-utf8' => array('label' => 'UTF8 Encode', 'type' => 'checkbox'),
'retries' => array('label' => 'Retries', 'type' => 'text'),
'animate' => array('label' => 'Animate', 'type' => 'checkbox'),
'cache-age' => array('label' => 'Expiry of cached tweets (seconds)', 'type' => 'text')
//'timeout' => array('label' => 'Timeout for RSS download', 'type' => 'text')
);
public static function init()
{
if (is_admin()) return;
if (AJAXED_TWITTER_FRAMEWORK == 'mootools' || AJAXED_TWITTER_FRAMEWORK == 'both') {
wp_enqueue_script('ajaxed-twitter-mootools', self::plugin_path().'/js/mootools.twitter.js');
}
if (AJAXED_TWITTER_FRAMEWORK == 'jquery' || AJAXED_TWITTER_FRAMEWORK == 'both') {
wp_enqueue_script('jquery.twitter', self::plugin_path().'/js/jquery.twitter.js', array('jquery'));
}
}
/**
* Get Twitter messages
*
* If $options is passed empty, stored options are taken instead.
*/
public static function messages(array $options)
{
if (empty($options)) return;
self::$options = array_merge(self::$default_options, $options);
$options = &self::$options;
$output = '';
$messages = self::fetch_rss('http://twitter.com/statuses/user_timeline/'.$options['username'].'.rss');
if ($options['list']) $output = '
';
return $output;
}
/**
* Copy of fetch_rss() in WP's rss.php (version 2.9.1).
*
* We want our personal cache settings without disturbing
* any other plugin or altering common cache settings.
*/
private static function fetch_rss($url)
{
if (!defined('MAGPIE_INPUT_ENCODING')) define('MAGPIE_INPUT_ENCODING', 'UTF-8');
if (!defined('MAGPIE_OUTPUT_ENCODING')) define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
if (!defined('MAGPIE_FETCH_TIME_OUT')) define('MAGPIE_FETCH_TIME_OUT', self::$options['timeout']);
// don't know whether the next two are needed
if (!defined('MAGPIE_CACHE_FRESH_ONLY')) define('MAGPIE_CACHE_FRESH_ONLY', 0);
if (!defined('MAGPIE_USE_GZIP')) define('MAGPIE_USE_GZIP', true);
include_once(ABSPATH.WPINC.'/rss.php');
if (self::$options['cache-age'] === -1) {
$resp = _fetch_remote_file($url);
if (is_success($resp->status)) {
return _response_to_rss($resp);
} else {
return false;
}
}
$cache = new RSSCache(
!defined('MAGPIE_CACHE_DIR') ? './cache' : MAGPIE_CACHE_DIR,
self::$options['cache-age']
);
$request_headers = array(); // HTTP headers to send with fetch
$rss = 0; // parsed RSS object
$errormsg = 0; // errors, if any
$cache_status = (!$cache->ERROR) ? $cache->check_cache($url) : 0;
if ($cache_status == 'HIT') {
$rss = $cache->get($url);
if (isset($rss) and $rss) {
$rss->from_cache = 1;
return $rss;
}
}
if ($cache_status == 'STALE') {
$rss = $cache->get($url);
if (isset($rss->etag) and $rss->last_modified) {
$request_headers['If-None-Match'] = $rss->etag;
$request_headers['If-Last-Modified'] = $rss->last_modified;
}
}
$resp = _fetch_remote_file($url, $request_headers);
if (isset($resp) and $resp) {
if ($resp->status == '304') {
$cache->set($url, $rss);
return $rss;
} elseif (is_success($resp->status)) {
$rss = _response_to_rss($resp);
if ( $rss ) {
$cache->set($url, $rss);
return $rss;
}
} else {
$errormsg = "Failed to fetch $url. ";
if ($resp->error) {
$http_error = substr($resp->error, 0, -2);
$errormsg .= "(HTTP Error: $http_error)";
} else {
$errormsg .= "(HTTP Response: " . $resp->response_code .')';
}
}
} else {
$errormsg = "Unable to retrieve RSS file for unknown reasons.";
}
if ($rss) return $rss;
return false;
}
private static function hyperlink($text)
{
// Props to Allen Shaw & webmancers.com
// match protocol://address/path/file.extension?some=variable&another=asf%
//$text = preg_replace("/\b([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","", $text);
$text = preg_replace(
'/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',
'',
$text
);
// match www.something.domain/path/file.extension?some=variable&another=asf%
//$text = preg_replace("/\b(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","", $text);
$text = preg_replace(
'/\b(?$1',
$text
);
// match name@address
$text = preg_replace(
'/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i',
'',
$text
);
//mach #trendingtopics. Props to Michael Voigt
$text = preg_replace(
'/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i',
'$1$3 ',
$text
);
return $text;
}
private static function userlink($text)
{
$text = preg_replace(
'/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i',
'$1$3 ',
$text
);
return $text;
}
public static function plugin_path()
{
return '/'.path_join(PLUGINDIR, basename(dirname(__FILE__)));
}
}}
if (!class_exists("AJAXedTwitterWidget")) { class AJAXedTwitterWidget extends WP_Widget
{
public function __construct()
{
parent::__construct('AJAXedTwitterWidget', 'AJAXed Twitter', array(), array('height' => 500, 'width' => 450));
}
public function form($instance)
{
$options = array_merge(AJAXedTwitter::$default_options, $instance);
echo '';
}
public function update($new_instance, $old_instance)
{
foreach (AJAXedTwitter::$default_options as $option => $value) {
if ($option == 'linked') {
// linked is a hybrid and can be false or a string
$new_instance['linked'] = ($new_instance['linked'] === '') ? false : $new_instance['linked'];
continue;
}
if (AJAXedTwitter::$option_fields[$option]['type'] == 'checkbox') {
$new_instance[$option] = (isset($new_instance[$option])) ? true : false;
} else {
$new_instance[$option] = $new_instance[$option];
}
}
$new_instance['id'] = $this->id;
return $new_instance;
}
public function widget($args, $instance)
{
$instance = array_merge(AJAXedTwitter::$default_options, $instance);
echo $args['before_widget'];
echo $args['before_title'].$instance['title'].$args['after_title'];
echo $args['after_widget'];
if ($instance['username'] == '') return;
if (AJAXED_TWITTER_FRAMEWORK == 'mootools') {
echo "Please set AJAXED_TWITTER_FRAMEWORK in wp_config.php to 'both'
( define('AJAXED_TWITTER_FRAMEWORK', 'both'); )
";
return;
}
echo '';
}
}}
if (class_exists("AJAXedTwitter")) {
add_action('init', array('AJAXedTwitter', 'init')); // initialize plugin (enqueues scripts)
add_action('widgets_init', create_function('', 'return register_widget("AJAXedTwitterWidget");'));
}