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 (!class_exists("AJAXedTwitter")) { class AJAXedTwitter { private static $options = false; private static $default_options = array( 'title' => 'Tweets', 'username' => '', 'num' => 5, 'list' => true, 'update' => true, 'linked' => '#', 'hyperlinks' => true, 'twitter-users' => true, 'encode-utf8' => false, 'error-id' => 'twitter-error', 'retries' => 2, 'animate' => true, 'cache-age' => 1800, 'timeout' => 2 ); private 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'), 'error-id' => array('label' => 'Error ID', 'type' => 'text'), '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') ); /** * Widget function */ public static function widget($args) { $path = get_bloginfo('wpurl').self::plugin_path(); if (!self::$options) self::get_options(); ?> '; echo $args['after_widget']; } /** * Merges default and saved options * @return array of current options */ private static function get_options() { self::$options = get_option('AJAXedTwitter'); if (is_array(self::$options)) { self::$options = array_merge(self::$default_options, self::$options); } else { self::$options = self::$default_options; } } public static function widget_control() { if (!self::$options) self::get_options(); if (is_admin() && $_POST['AJAXedTwitter-title']) { foreach (self::$options as $option => $value) { if ($option == 'linked') { // linked is a hybrid and can be false or a string self::$options['linked'] = ($_POST['AJAXedTwitter-linked'] === '') ? false : $_POST['AJAXedTwitter-linked']; continue; } if (self::$option_fields[$option]['type'] == 'checkbox') { self::$options[$option] = (isset($_POST['AJAXedTwitter-'.$option])) ? true : false; } else { self::$options[$option] = $_POST['AJAXedTwitter-'.$option]; } } update_option('AJAXedTwitter', self::$options); } echo ''; foreach (self::$option_fields as $option => $field) { if ($field['type'] == 'checkbox') { if (self::$options[$option] === true) { $value = 'value="true" checked="checked"'; } else { $value = 'value="false"'; } } else { $value = 'value="'.self::$options[$option].'"'; } echo ''; echo ''; echo ''; } echo '
'; } public static function init() { // this will automatically add the MooTools Class "Twitter" to wp_head(); if (!is_admin()) wp_enqueue_script('ajaxed-twitter', self::plugin_path().'/js/Twitter.js'); } /** * Get Twitter messages * * If $options is passed empty, stored options are taken instead. */ public static function messages(array $options = array()) { if (empty($options) && !self::$options) { self::get_options(); } if (!empty($options)) { self::$options = array_merge(self::$default_options, $options); } $options = &self::$options; $options['error-id'] = ($options['error-id']) ? 'id="'.$options['error-id'].'"' : ''; $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","$1", $text); $text = preg_replace( '/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i', '$1', $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","$1", $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', '$1', $text ); //mach #trendingtopics. Props to Michael Voigt $text = preg_replace( '/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', '$1#$2$3 ', $text ); return $text; } private static function userlink($text) { $text = preg_replace( '/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', '$1@$2$3 ', $text ); return $text; } public static function plugin_path() { return '/'.path_join(PLUGINDIR, basename(dirname(__FILE__))); } }} if (class_exists("AJAXedTwitter")) { // initialize plugin add_action('init', array('AJAXedTwitter', 'init')); register_sidebar_widget('AJAXed Twitter for Wordpress', array('AJAXedTwitter', 'widget')); register_widget_control('AJAXed Twitter for Wordpress', array('AJAXedTwitter', 'widget_control'), 450, 500); }