';
// If "hide rss" hasn't been checked, show the linked icon
if ( $args['hiderss'] != 'true' ) {
if ( file_exists(dirname(__FILE__) . '/rss.png') ) {
$icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png';
} else {
$icon = get_option('siteurl').'/wp-includes/images/rss.png';
}
$feedUrl = $this->_getFeedUrl($args, 'rss', false);
$linkAttrs = array(
'class' => 'twitterwidget twitterwidget-rss',
'title' => __('Syndicate this content', 'twitter-widget-pro'),
'href' => $feedUrl
);
$args['before_title'] .= $this->_buildLink("

", $linkAttrs, true);
}
$twitterLink = 'http://twitter.com/' . $args['username'];
$args['after_title'] = '' . $args['after_title'];
if (empty($args['title'])) {
$args['title'] = "Twitter: {$args['username']}";
}
$linkAttrs = array(
'class' => 'twitterwidget twitterwidget-title',
'title' => "Twitter: {$args['username']}",
'href' => $twitterLink
);
$args['title'] = $this->_buildLink($args['title'], $linkAttrs, current_user_can('unfiltered_html'));
$widgetContent .= $args['before_title'] . $args['title'] . $args['after_title'];
if (!is_a($tweets, 'wpTwitterWidgetException') && !empty($tweets[0]) && $args['avatar'] == 'true') {
$widgetContent .= '';
}
$widgetContent .= '
';
if (is_a($tweets, 'wpTwitterWidgetException')) {
$widgetContent .= '';
} else if (count($tweets) == 0) {
$widgetContent .= '';
} else {
$count = 0;
foreach ($tweets as $tweet) {
if ( $args['hidereplies'] != 'true' || empty($tweet->in_reply_to_user_id)) {
// Set our "ago" string which converts the date to "# ___(s) ago"
$tweet->ago = $this->_timeSince(strtotime($tweet->created_at), $args['showts']);
$entryContent = apply_filters( 'widget_twitter_content', $tweet->text );
$from = sprintf(__('from %s', 'twitter-widget-pro'), str_replace('&', '&', $tweet->source));
$widgetContent .= '- ';
$widgetContent .= "{$entryContent}";
$widgetContent .= "";
$widgetContent .= "";
$linkAttrs = array(
'href' => "http://twitter.com/{$tweet->user->screen_name}/statuses/{$tweet->id}"
);
$widgetContent .= $this->_buildLink($tweet->ago, $linkAttrs);
$widgetContent .= '';
$widgetContent .= "{$from}";
if ( !empty($tweet->in_reply_to_screen_name) ) {
$rtLinkText = sprintf( __('in reply to %s', 'twitter-widget-pro'), $tweet->in_reply_to_screen_name );
$widgetContent .= '';
$linkAttrs = array(
'href' => "http://twitter.com/{$tweet->in_reply_to_screen_name}/statuses/{$tweet->in_reply_to_status_id}",
'class' => 'reply-to'
);
$widgetContent .= $this->_buildLink($rtLinkText, $linkAttrs);
$widgetContent .= '';
}
$widgetContent .= '
';
if (++$count >= $args['items']) {
break;
}
}
}
}
if ( $args['showXavisysLink'] == 'true' ) {
$widgetContent .= '- ';
$linkAttrs = array(
'href' => 'http://xavisys.com/wordpress-plugins/wordpress-twitter-widget/',
'title' => __('Get Twitter Widget for your WordPress site', 'twitter-widget-pro')
);
$widgetContent .= __('Powered by', 'twitter-widget-pro');
$widgetContent .= $this->_buildLink('WordPress Twitter Widget Pro', $linkAttrs);
$widgetContent .= '
';
}
$widgetContent .= '
' . $args['after_widget'];
return $widgetContent;
}
/**
* Gets tweets, from cache if possible
*
* @param array $widgetOptions - options needed to get feeds
* @return array - Array of objects
*/
private function _getTweets($widgetOptions) {
$feedHash = sha1($this->_getFeedUrl($widgetOptions));
$tweets = get_option("wptw-{$feedHash}");
$cacheAge = get_option("wptw-{$feedHash}-time");
//If we don't have cache or it's more than 5 minutes old
if ( empty($tweets) || (time() - $cacheAge) > 300 ) {
try {
$tweets = $this->_parseFeed($widgetOptions);
update_option("wptw-{$feedHash}", $tweets);
update_option("wptw-{$feedHash}-time", time());
} catch (wpTwitterWidgetException $e) {
throw $e;
}
}
return $tweets;
}
/**
* Pulls the JSON feed from Twitter and returns an array of objects
*
* @param array $widgetOptions - settings needed to get feed url, etc
* @return array
*/
private function _parseFeed($widgetOptions) {
$feedUrl = $this->_getFeedUrl($widgetOptions);
$resp = wp_remote_request($feedUrl, array('timeout' => $widgetOptions['fetchTimeOut']));
if ( !is_wp_error($resp) && $resp['response']['code'] >= 200 && $resp['response']['code'] < 300 ) {
if (function_exists('json_decode')) {
$decodedResponse = json_decode( $resp['body'] );
} else {
global $wp_json;
if ( !is_a($wp_json, 'Services_JSON') ) {
require_once( 'class-json.php' );
$wp_json = new Services_JSON();
}
$decodedResponse = $wp_json->decode( $resp['body'] );
}
if ( empty($decodedResponse) ) {
if (empty($widgetOptions['errmsg'])) {
$widgetOptions['errmsg'] = __('Invalid Twitter Response.', 'twitter-widget-pro');
}
throw new wpTwitterWidgetException($widgetOptions['errmsg']);
} elseif( !empty($decodedResponse->error) ) {
if (empty($widgetOptions['errmsg'])) {
$widgetOptions['errmsg'] = $decodedResponse->error;
}
throw new wpTwitterWidgetException($widgetOptions['errmsg']);
} else {
return $decodedResponse;
}
} else {
// Failed to fetch url;
if (empty($widgetOptions['errmsg'])) {
$widgetOptions['errmsg'] = __('Could not connect to Twitter', 'twitter-widget-pro');
}
throw new wpTwitterWidgetException($widgetOptions['errmsg']);
}
}
/**
* Gets the URL for the desired feed.
*
* @param array $widgetOptions - settings needed such as username, feet type, etc
* @param string[optional] $type - 'rss' or 'json'
* @param bool[optional] $count - If true, it adds the count parameter to the URL
* @return string - Twitter feed URL
*/
private function _getFeedUrl($widgetOptions, $type = 'json', $count = true) {
if (!in_array($type, array('rss', 'json'))) {
$type = 'json';
}
if ( $count ) {
$num = ($widgetOptions['hidereplies'])? 100:$widgetOptions['items'];
$count = sprintf('?count=%u', $num);
} else {
$count = '';
}
return sprintf('http://twitter.com/statuses/user_timeline/%1$s.%2$s%3$s', $widgetOptions['username'], $type, $count);
}
/**
* Twitter displays all tweets that are less than 24 with something like
* "about 4 hours ago" and ones older than 24 hours with a time and date.
* This function allows us to simulate that functionality, but lets us
* choose where the dividing line is.
*
* @param int $startTimestamp - The timestamp used to calculate time passed
* @param int $max - Max number of seconds to conver to "ago" messages. 0 for all, -1 for none
* @return string
*/
private function _timeSince($startTimestamp, $max) {
// array of time period chunks
$chunks = array(
'year' => 60 * 60 * 24 * 365, // 31,536,000 seconds
'month' => 60 * 60 * 24 * 30, // 2,592,000 seconds
'week' => 60 * 60 * 24 * 7, // 604,800 seconds
'day' => 60 * 60 * 24, // 86,400 seconds
'hour' => 60 * 60, // 3600 seconds
'minute' => 60, // 60 seconds
'second' => 1 // 1 second
);
$since = time() - $startTimestamp;
if ($max != '-1' && $since >= $max) {
return date_i18n(__('h:i:s A F d, Y', 'twitter-widget-pro'), $startTimestamp);
}
foreach ( $chunks as $key => $seconds ) {
// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$messages = array(
'year' => _n('about %s year ago', 'about %s years ago', $count, 'twitter-widget-pro'),
'month' => _n('about %s month ago', 'about %s months ago', $count, 'twitter-widget-pro'),
'week' => _n('about %s week ago', 'about %s weeks ago', $count, 'twitter-widget-pro'),
'day' => _n('about %s day ago', 'about %s days ago', $count, 'twitter-widget-pro'),
'hour' => _n('about %s hour ago', 'about %s hours ago', $count, 'twitter-widget-pro'),
'minute' => _n('about %s minute ago', 'about %s minutes ago', $count, 'twitter-widget-pro'),
'second' => _n('about %s second ago', 'about %s seconds ago', $count, 'twitter-widget-pro'),
);
return sprintf($messages[$key], $count);
}
/**
* Returns the Twitter user's profile image, linked to that user's profile
*
* @param object $user - Twitter User
* @return string - Linked image (XHTML)
*/
private function _getProfileImage($user) {
$linkAttrs = array(
'href' => "http://twitter.com/{$user->screen_name}",
'title' => $user->name
);
return $this->_buildLink("