__( 'Display recent tweets', TP_RECENT_TEXT_DOMAIN ), ) // Args
);
}
//widget output
public function widget($args, $instance) {
extract($args);
if(!empty($instance['title'])){ $title = apply_filters( 'widget_title', $instance['title'] ); }
echo $before_widget;
if ( ! empty( $title ) ){ echo $before_title . $title . $after_title; }
//check settings and die if not set
if(empty($instance['consumerkey']) || empty($instance['consumersecret']) || empty($instance['accesstoken']) || empty($instance['accesstokensecret']) || empty($instance['cachetime']) || empty($instance['username'])){
echo ''.__('Please fill all widget settings!',TP_RECENT_TEXT_DOMAIN).'' . $after_widget;
return;
}
//check if cache needs update
$tp_twitter_plugin_last_cache_time = get_option('tp_twitter_plugin_last_cache_time');
$diff = time() - $tp_twitter_plugin_last_cache_time;
$crt = $instance['cachetime'] * 3600;
// yes, it needs update
if($diff >= $crt || empty($tp_twitter_plugin_last_cache_time)){
if(!require_once('twitteroauth.php')){
echo ''.__('Couldn\'t find twitteroauth.php!',TP_RECENT_TEXT_DOMAIN).'' . $after_widget;
return;
}
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($instance['consumerkey'], $instance['consumersecret'], $instance['accesstoken'], $instance['accesstokensecret']);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$instance['username']."&count=10&exclude_replies=".$instance['excludereplies']) or die('Couldn\'t retrieve tweets! Wrong username?');
if(!empty($tweets->errors)){
if($tweets->errors[0]->message == 'Invalid or expired token'){
echo ''.$tweets->errors[0]->message.'!
' . __('You\'ll need to regenerate it here!',TP_RECENT_TEXT_DOMAIN) . $after_widget;
}else{
echo ''.$tweets->errors[0]->message.'' . $after_widget;
}
return;
}
$tweets_array = array();
for($i = 0;$i <= count($tweets); $i++){
if(!empty($tweets[$i])){
$tweets_array[$i]['created_at'] = $tweets[$i]->created_at;
//clean tweet text
$tweets_array[$i]['text'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $tweets[$i]->text);
if(!empty($tweets[$i]->id_str)){
$tweets_array[$i]['status_id'] = $tweets[$i]->id_str;
}
}
}
//save tweets to wp option
update_option('tp_twitter_plugin_tweets',serialize($tweets_array));
update_option('tp_twitter_plugin_last_cache_time',time());
echo '';
}
$tp_twitter_plugin_tweets = maybe_unserialize(get_option('tp_twitter_plugin_tweets'));
if(!empty($tp_twitter_plugin_tweets) && is_array($tp_twitter_plugin_tweets)){
print '
Check out the SumoMe plugin
'; } print 'Get your API keys & tokens at:
https://apps.twitter.com/
Check out our SumoMe plugin
hours
'; } } //convert links to clickable format if (!function_exists('tp_convert_links')) { function tp_convert_links($status,$targetBlank=true,$linkMaxLen=250){ // the target $target=$targetBlank ? " target=\"_blank\" " : ""; // convert link to url $status = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[A-Z0-9+&@#\/%=~_|]/i', '\0', $status); // convert @ to follow $status = preg_replace("/(@([_a-z0-9\-]+))/i","$1",$status); // convert # to search $status = preg_replace("/(#([_a-z0-9\-]+))/i","$1",$status); // return the status return $status; } } //convert dates to readable format if (!function_exists('tp_relative_time')) { function tp_relative_time($a) { //get current timestampt $b = strtotime('now'); //get timestamp when tweet created $c = strtotime($a); //get difference $d = $b - $c; //calculate different time values $minute = 60; $hour = $minute * 60; $day = $hour * 24; $week = $day * 7; if(is_numeric($d) && $d > 0) { //if less then 3 seconds if($d < 3) return __('right now',TP_RECENT_TEXT_DOMAIN); //if less then minute if($d < $minute) return floor($d) . __(' seconds ago',TP_RECENT_TEXT_DOMAIN); //if less then 2 minutes if($d < $minute * 2) return __('about 1 minute ago',TP_RECENT_TEXT_DOMAIN); //if less then hour if($d < $hour) return floor($d / $minute) . __(' minutes ago',TP_RECENT_TEXT_DOMAIN); //if less then 2 hours if($d < $hour * 2) return __('about 1 hour ago',TP_RECENT_TEXT_DOMAIN); //if less then day if($d < $day) return floor($d / $hour) . __(' hours ago',TP_RECENT_TEXT_DOMAIN); //if more then day, but less then 2 days if($d > $day && $d < $day * 2) return __('yesterday',TP_RECENT_TEXT_DOMAIN); //if less then year if($d < $day * 365) return floor($d / $day) . __(' days ago',TP_RECENT_TEXT_DOMAIN); //else return more than a year return __('over a year ago',TP_RECENT_TEXT_DOMAIN); } } } // register widget function register_tp_twitter_widget(){ register_widget('tp_widget_recent_tweets'); } add_action('widgets_init', 'register_tp_twitter_widget', 1) ?>