Wordpress Plugins by Grobekelle.
Version: 0.1
Author: grobekelle
Author URI: http://www.grobekelle.de
*/
/**
* v0.1 07.07.2009 initial release
*/
class TwitterWidget {
var $id;
var $title;
var $plugin_url;
var $version;
var $name;
var $url;
var $options;
var $locale;
var $cache_file;
function TwitterWidget() {
$this->id = 'twitterwidget';
$this->title = 'TwitterWidget';
$this->version = '0.1';
$this->plugin_url = 'http://www.grobekelle.de/wordpress-plugins';
$this->name = 'TwitterWidget v'. $this->version;
$this->url = get_bloginfo('wpurl'). '/wp-content/plugins/' . $this->id;
$this->locale = get_locale();
$this->path = dirname(__FILE__);
$this->cache_file = $this->path . '/cache/cache.html';
if(empty($this->locale)) {
$this->locale = 'en_US';
}
load_textdomain($this->id, sprintf('%s/%s.mo', $this->path, $this->locale));
$this->loadOptions();
if(!is_admin()) {
add_filter('wp_head', array(&$this, 'blogHeader'));
}
else {
add_action('admin_menu', array( &$this, 'optionMenu'));
}
add_action('widgets_init', array( &$this, 'initWidget'));
}
function optionMenu() {
add_options_page($this->title, $this->title, 8, __FILE__, array(&$this, 'optionMenuPage'));
}
function optionMenuPage() {
?>
=$this->title?>
id])) {
/**
* nasty checkbox handling
*/
foreach(array('link_links', 'link_hashtags', 'link_names', 'link_tweets', 'nofollow', 'show_twitter_link', 'target_blank') as $field ) {
if(!isset($_POST[$this->id][$field])) {
$_POST[$this->id][$field] = '0';
}
}
@unlink($this->cache_file);
$this->updateOptions( $_POST[ $this->id ] );
echo '
' . __( 'Settings saved!', $this->id) . '
';
}
?>
options as $k => $v) {
if(array_key_exists( $k, $options)) {
$this->options[ $k ] = trim($options[ $k ]);
}
}
update_option($this->id, $this->options);
}
function loadOptions() {
# delete_option($this->id);
$this->options = get_option($this->id);
if(!$this->options) {
$this->options = array(
'installed' => time(),
'username' => '',
'nofollow' => 1,
'target_blank' => 1,
'limit' => 5,
'width' => 160,
# 'height' => 400,
'time_format' => 'm/d/Y',
'link_tweets' => 1,
'link_links' => 1,
'link_names' => 1,
'link_hashtags' => 1,
'show_twitter_link' => 1,
'title' => 'TwitterWidget'
);
add_option($this->id, $this->options, $this->name, 'yes');
if(is_admin()) {
add_filter('admin_footer', array(&$this, 'addAdminFooter'));
}
}
}
function httpGet($url) {
if(!class_exists('Snoopy')) {
include_once(ABSPATH. WPINC. '/class-snoopy.php');
}
$Snoopy = new Snoopy();
if(@$Snoopy->fetch($url)) {
if(!empty( $Snoopy->results)) {
return $Snoopy->results;
}
}
return false;
}
function initWidget() {
if(function_exists('register_sidebar_widget')) {
register_sidebar_widget($this->title . ' Widget', array($this, 'showWidget'), null, 'widget_twitterwidget');
}
}
function showWidget( $args ) {
extract($args);
printf( '%s%s%s%s%s%s', $before_widget, $before_title, $this->options['title'], $after_title, $this->getCode(), $after_widget );
}
function blogHeader() {
printf('' . "\n", $this->id, $this->id, $this->version);
printf(''. "\n", $this->url, $this->id);
printf('', $this->options['width']);
}
function getToken($data, $pattern) {
if(preg_match('|<' . $pattern . '>(.*?)' . $pattern . '>|s', $data, $matches)) {
return $matches[1];
}
return '';
}
function getTweets($user) {
if(empty($user)) {
return false;
}
/**
* not the best way, but we can't assume that every webhost simplexml installed
*/
$data = $this->httpGet('http://twitter.com/statuses/user_timeline/'. $user. '.rss');
if($data !== false) {
if(preg_match_all('/- (.*?)<\/item>/s', $data, $matches)) {
$result = array();
foreach($matches[0] as $match) {
$result[] = array(
'message' => $this->formatMessage(substr($this->getToken($match, 'title'), strlen($user)+2).' '),
'link' => $this->getToken($match, 'link'),
'date' => $this->formatTime($this->getToken($match, 'pubDate'))
);
}
return array_slice($result, 0, $this->options['limit']);
}
}
return false;
}
function formatMessage($s) {
$rel = intval($this->options['nofollow']) == 1 ? ' rel="nofollow"' : '';
$target = intval($this->options['target_blank']) == 1 ? ' target="_blank"' : '';
// links
if(intval($this->options['link_links']) == 1) {
$s = preg_replace("/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i",sprintf(" $2", $rel, $target), $s);
$s = preg_replace("/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/i",sprintf(" $2", $rel, $target), $s);
}
/*
// email
$s = preg_replace("/\s([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})([\s|\.|\,])/i"," $2", $s);*/
// #hashtags - Props to Michael Voigt
if(intval($this->options['link_hashtags']) == 1) {
$s = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', sprintf("$1$3 ", $rel, $target), $s);
}
// @twitter-user
if(intval($this->options['link_names']) == 1) {
$s = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', sprintf("$1$3 ", $rel, $target), $s);
}
return trim($s);
}
function formatTime($t) {
$time = strtotime($t);
# $time = date($this->options['time_format'], $time);
# return $time;
if(abs(time() - $time) < 86400) {
$time = sprintf(__('%s ago', $this->id), human_time_diff($time));
}
else {
$time = sprintf(__('at %s', $this->id), date($this->options['time_format'], $time));
}
return $time;
return $t;
}
function getCode() {
if(empty($this->options['username'])) {
return __('Twitter-Username missing! Please configure the plugin first!', $this->id);
}
$create = false;
if(!file_exists($this->cache_file)) {
$create = true;
}
elseif(time() - filemtime($this->cache_file) > 120) {
$create = true;
}
if(!$create) {
return file_get_contents($this->cache_file);
}
$tweets = $this->getTweets($this->options['username']);
if(is_array($tweets)) {
$data = '';
foreach($tweets as $tweet) {
if(intval($this->options['link_tweets']) == 1) {
$link = sprintf('', $tweet['link'], $this->options['target_blank'] == 1 ? ' target="_blank"' : '', $this->options['nofollow'] == 1 ? ' rel="nofollow"' : '', __('view tweet', $this->id));
}
$data .= sprintf('', $tweet['date'], $tweet['message'], $link);
}
$data = '';
if(is_writeable($this->path. '/cache')) {
file_put_contents($this->cache_file, $data);
}
return $data;
}
return '';
}
}
function twitterwidget_display() {
global $TwitterWidget;
if($TwitterWidget) {
echo $TwitterWidget->getcode();
}
}
add_action( 'plugins_loaded', create_function( '$TwitterWidget_5kks2', 'global $TwitterWidget; $TwitterWidget = new TwitterWidget();' ) );
?>