__( 'A widget for showing latest tweets in sidebar', 'mythemeshop' ), ) // Args
);
}
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!' . $after_widget; return; }
//check if cache needs update
$mts_twitter_plugin_last_cache_time = get_option('mts_twitter_plugin_last_cache_time');
$diff = time() - $mts_twitter_plugin_last_cache_time;
$crt = $instance['cachetime'] * 3600;
// yes, it needs update
//require_once('twitteroauth.php');
if($diff >= $crt || empty($mts_twitter_plugin_last_cache_time)){
if(!require_once('twitteroauth.php')){ echo 'Couldn\'t find twitteroauth.php!' . $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") 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!' . $after_widget;
}else{ echo ''.$tweets->errors[0]->message.'' . $after_widget; }
return;
}
for($i = 0;$i <= count($tweets); $i++){
if(!empty($tweets[$i])){
$tweets_array[$i]['created_at'] = $tweets[$i]->created_at;
$tweets_array[$i]['text'] = $tweets[$i]->text;
$tweets_array[$i]['status_id'] = $tweets[$i]->id_str;
}
}
//save tweets to wp option
update_option('mts_twitter_plugin_tweets',serialize($tweets_array));
update_option('mts_twitter_plugin_last_cache_time',time());
echo '';
}
//convert links to clickable format
function convert_links($status,$targetBlank=true,$linkMaxLen=250){
$target=$targetBlank ? " target=\"_blank\" " : ""; // the target
$status = preg_replace("/((http:\/\/|https:\/\/)[^ )]+)/e", "''. ((strlen('$1')>=$linkMaxLen ? substr('$1',0,$linkMaxLen).'...':'$1')).''", $status); // convert link to url
$status = preg_replace("/(@([_a-z0-9\-]+))/i","$1",$status); // convert @ to follow
$status = preg_replace("/(#([_a-z0-9\-]+))/i","$1",$status); // convert # to search
return $status; // return the status
}
//convert dates to readable format
function relative_time($a) {
$b = strtotime("now"); //get current timestampt
$c = strtotime($a); //get timestamp when tweet created
$d = $b - $c; //get difference
$minute = 60; //calculate different time values
$hour = $minute * 60;
$day = $hour * 24;
$week = $day * 7;
if(is_numeric($d) && $d > 0) {
if($d < 3) return "right now"; //if less then 3 seconds
if($d < $minute) return floor($d) . " seconds ago"; //if less then minute
if($d < $minute * 2) return "about 1 minute ago"; //if less then 2 minutes
if($d < $hour) return floor($d / $minute) . " minutes ago"; //if less then hour
if($d < $hour * 2) return "about 1 hour ago"; //if less then 2 hours
if($d < $day) return floor($d / $hour) . " hours ago"; //if less then day
if($d > $day && $d < $day * 2) return "yesterday"; //if more then day, but less then 2 days
if($d < $day * 365) return floor($d / $day) . " days ago"; //if less then year
return "over a year ago"; //else return more than a year
}
}
$mts_twitter_plugin_tweets = maybe_unserialize(get_option('mts_twitter_plugin_tweets'));
if(!empty($mts_twitter_plugin_tweets)){
print '
hours
Visit this link in a new tab, sign in with your account, click on Create a new application and create your own keys in case you don\'t have already
'; } } // register widget function register_mts_twitter_widget(){ register_widget('mts_widget_recent_tweets'); } add_action('widgets_init', 'register_mts_twitter_widget', 1); ?>