__('A widget that show Twitter feeds.', 'gdl_back_office')));
}
// Output of the widget
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$twitter_username = $instance['twitter_username'];
$show_num = $instance['show_num'];
$consumer_key = $instance['consumer_key'];
$consumer_secret = $instance['consumer_secret'];
$access_token = $instance['access_token'];
$access_token_secret = $instance['access_token_secret'];
$cache_time = $instance['cache_time'];
// Opening of widget
echo $before_widget;
// Open of title tag
if ( $title ){
echo $before_title . $title . $after_title;
}
$last_cache_time = get_option('gdl_twitter_widget_last_cache_time', 0);
$diff = time() - $last_cache_time;
$crt = $cache_time * 3600;
if(empty($last_cache_time) || $diff >= $crt){
$connection = getConnectionWithAccessToken($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitter_username."&count=" . $show_num) 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;
}
$tweets_data = array();
for($i = 0;$i <= count($tweets); $i++){
if(!empty($tweets[$i])){
$tweets_data[$i]['created_at'] = $tweets[$i]->created_at;
$tweets_data[$i]['text'] = $tweets[$i]->text;
$tweets_data[$i]['status_id'] = $tweets[$i]->id_str;
}
}
update_option('gdl_twitter_widget_tweets',serialize($tweets_data));
update_option('gdl_twitter_widget_last_cache_time',time());
}else{
$tweets_data = maybe_unserialize(get_option('gdl_twitter_widget_tweets'));
}
echo '