Rotating Tweets (Twitter widget and shortcode) - Version 0.46

Version Description

Includes fix for the major bug causing crashes when Twitter goes down. Also improved error checking if Twitter has returned an empty value or is rate-limited.

=

Download this release

Release Info

Developer mpntod
Plugin Icon wp plugin Rotating Tweets (Twitter widget and shortcode)
Version 0.46
Comparing to
See all releases

Code changes from version 0.44 to 0.46

Files changed (2) hide show
  1. readme.txt +7 -3
  2. rotatingtweets.php +46 -7
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: shortcode,widget,twitter,rotating,rotate,rotator,tweet,tweets
5
  Requires at least: 2.6
6
  Tested up to: 3.4
7
- Stable tag: 0.44
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -13,6 +13,7 @@ Replaces a shortcode such as [rotatingtweets screen_name='your_twitter_name'], o
13
  == Description ==
14
  * Replaces a [shortcode](http://codex.wordpress.org/Shortcode) such as `[rotatingtweets screen_name='your_twitter_name']`, or a [widget](http://codex.wordpress.org/WordPress_Widgets), with a rotating display of your most recent tweets
15
  * Space efficient - instead of showing all your tweets at once, shows one at a time and then smoothly replaces it with the next one. After showing all your tweets, loops back to the beginning again.
 
16
  * Customisable - you decide whose tweets to show, how many to show, whether to include retweets and replies, and whether to show a follow button. You can also decide how quickly the tweets rotate.
17
  * Replaces [t.co](http://t.co) links with the original link
18
  * Caches the most recent data from Twitter to avoid problems with rate limiting
@@ -40,10 +41,13 @@ But you may just decide to use the 'Rotating Tweets' widget!
40
  In most cases, each use (or "instance") of this plug-in gets data from Twitter every 2 minutes. The exception is when two or more instances share the same settings (screen name etc.), in which case they share the same data rather than each calling it separately.
41
 
42
  == Upgrade notice ==
43
- = 0.44 =
44
- Includes fix for the major bug causing crashes when Twitter goes down. Also improved error checking if Twitter has returned an empty value.
45
 
46
  == Changelog ==
 
 
 
47
  = 0.44 =
48
  Removes follow button if Twitter has returned an empty value
49
 
4
  Tags: shortcode,widget,twitter,rotating,rotate,rotator,tweet,tweets
5
  Requires at least: 2.6
6
  Tested up to: 3.4
7
+ Stable tag: 0.46
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
13
  == Description ==
14
  * Replaces a [shortcode](http://codex.wordpress.org/Shortcode) such as `[rotatingtweets screen_name='your_twitter_name']`, or a [widget](http://codex.wordpress.org/WordPress_Widgets), with a rotating display of your most recent tweets
15
  * Space efficient - instead of showing all your tweets at once, shows one at a time and then smoothly replaces it with the next one. After showing all your tweets, loops back to the beginning again.
16
+ * Reliable - keeps showing your latest Tweets even if the Twitter website is down.
17
  * Customisable - you decide whose tweets to show, how many to show, whether to include retweets and replies, and whether to show a follow button. You can also decide how quickly the tweets rotate.
18
  * Replaces [t.co](http://t.co) links with the original link
19
  * Caches the most recent data from Twitter to avoid problems with rate limiting
41
  In most cases, each use (or "instance") of this plug-in gets data from Twitter every 2 minutes. The exception is when two or more instances share the same settings (screen name etc.), in which case they share the same data rather than each calling it separately.
42
 
43
  == Upgrade notice ==
44
+ = 0.46 =
45
+ Includes fix for the major bug causing crashes when Twitter goes down. Also improved error checking if Twitter has returned an empty value or is rate-limited.
46
 
47
  == Changelog ==
48
+ = 0.46 =
49
+ Properly handles rate-limiting by Twitter
50
+
51
  = 0.44 =
52
  Removes follow button if Twitter has returned an empty value
53
 
rotatingtweets.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  Plugin Name: Rotating Tweets widget & shortcode
4
  Description: Replaces a shortcode such as [rotatingtweets userid='your_twitter_name'], or a widget, with a rotating tweets display
5
- Version: 0.44
6
  Author: Martin Tod
7
  Author URI: http://www.martintod.org.uk
8
  License: GPL2
@@ -197,19 +197,47 @@ function rotatingtweets_get_tweets($tw_screen_name,$tw_include_rts,$tw_exclude_r
197
  endif;
198
  endif;
199
  if(!empty($twitterjson->errors)):
200
- # If there's an error, reset the cache timer to make sure we don't hit Twitter too hard and get rate limited
201
- $option[$stringname][datetime]=time()-60;
202
  update_option($optionname,$option);
 
 
 
 
 
 
 
 
203
  elseif(!empty($twitterjson)):
204
  # If there's regular data, then update the cache and return the data
205
  $latest_json = $twitterjson;
206
- $option[$stringname][json]=$latest_json;
207
- $option[$stringname][datetime]=time();
208
  update_option($optionname,$option);
209
  endif;
210
  return($latest_json);
211
  }
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  # Displays the tweets
214
  function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeout=4000,$print=TRUE) {
215
  unset($result);
@@ -218,7 +246,18 @@ function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeou
218
  $result = "\n<div class='rotatingtweets' id='".uniqid('rotatingtweets_'.$timeout.'_')."'>";
219
  if(empty($json)):
220
  $result .= "\n\t<div class = 'rotatingtweet'><p class='rtw_main'>Problem retrieving data from Twitter.</p></div>";
221
- $result .= "\n\t<div class = 'rotatingtweet' style='display:none'><p class='rtw_main'>Please check the Twitter name used in the settings.</p></div>";
 
 
 
 
 
 
 
 
 
 
 
222
  else:
223
  $tweet_counter = 0;
224
  foreach($json as $twitter_object):
@@ -298,6 +337,6 @@ function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeou
298
  wp_enqueue_script( 'rotating_tweet', plugins_url('js/rotating_tweet.js', __FILE__),array('jquery','jquery-cycle'),FALSE,FALSE );
299
  wp_enqueue_style( 'rotating_tweet', plugins_url('css/style.css', __FILE__));
300
  if($print) echo $result;
301
- return $result;
302
  }
303
  ?>
2
  /*
3
  Plugin Name: Rotating Tweets widget & shortcode
4
  Description: Replaces a shortcode such as [rotatingtweets userid='your_twitter_name'], or a widget, with a rotating tweets display
5
+ Version: 0.46
6
  Author: Martin Tod
7
  Author URI: http://www.martintod.org.uk
8
  License: GPL2
197
  endif;
198
  endif;
199
  if(!empty($twitterjson->errors)):
200
+ # If there's an error, reset the cache timer to make sure we don't hit Twitter too hard and get rate limited.
201
+ $option[$stringname][datetime]=time();
202
  update_option($optionname,$option);
203
+ elseif(!empty($twitterjson->error)):
204
+ # If Twitter is being rate limited, delays the next load until the reset time
205
+ # For some reason the rate limiting error has a different error variable!
206
+ $rate = rotatingtweets_get_rate_data();
207
+ if($rate && $rate->remaining_hits == 0):
208
+ $option[$stringname]['datetime']= $rate->reset_time_in_seconds - $cache_delay + 1;
209
+ update_option($optionname,$option);
210
+ endif;
211
  elseif(!empty($twitterjson)):
212
  # If there's regular data, then update the cache and return the data
213
  $latest_json = $twitterjson;
214
+ $option[$stringname]['json']=$latest_json;
215
+ $option[$stringname]['datetime']=time();
216
  update_option($optionname,$option);
217
  endif;
218
  return($latest_json);
219
  }
220
 
221
+ # Gets the rate limiting data to see how long it will be before we can tweet again
222
+ function rotatingtweets_get_rate_data() {
223
+ $callstring = "http://api.twitter.com/1/account/rate_limit_status.json";
224
+ $ratedata = wp_remote_request($callstring);
225
+ if(!is_wp_error($ratedata)):
226
+ $rate = json_decode($ratedata['body']);
227
+ return($rate);
228
+ else:
229
+ return(FALSE);
230
+ endif;
231
+ }
232
+
233
+ # This function is used for debugging what happens when the site is rate-limited - best not used otherwise!
234
+ function rotatingtweets_trigger_rate_limiting() {
235
+ $callstring = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter";
236
+ for ($i=1; $i<150; $i++) {
237
+ $ratedata = wp_remote_request($callstring);
238
+ }
239
+ }
240
+
241
  # Displays the tweets
242
  function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeout=4000,$print=TRUE) {
243
  unset($result);
246
  $result = "\n<div class='rotatingtweets' id='".uniqid('rotatingtweets_'.$timeout.'_')."'>";
247
  if(empty($json)):
248
  $result .= "\n\t<div class = 'rotatingtweet'><p class='rtw_main'>Problem retrieving data from Twitter.</p></div>";
249
+ $rate = rotatingtweets_get_rate_data();
250
+ # Check if the problem is rate limiting
251
+ if($rate && $rate->remaining_hits == 0):
252
+ $result .= "\n\t<div class = 'rotatingtweet' style='display:none'><p class='rtw_main'>This website is currently <a href='https://dev.twitter.com/docs/rate-limiting/faq'>rate-limited by Twitter</a>.</p></div>";
253
+ $waittimevalue = intval(($rate->reset_time_in_seconds-time - time())/60);
254
+ $waittime = $waittimevalue." minutes";
255
+ if($waittimevalue == 1) $waittime = "1 minute";
256
+ if($waittimevalue == 0) $waittime = "less than a minute";
257
+ $result .= "\n\t<div class = 'rotatingtweet' style='display:none'><p class='rtw_main'>Next attempt to get data will be in {$waittime}.</p></div>";
258
+ else:
259
+ $result .= "\n\t<div class = 'rotatingtweet' style='display:none'><p class='rtw_main'>Please check the Twitter name used in the settings.</p></div>";
260
+ endif;
261
  else:
262
  $tweet_counter = 0;
263
  foreach($json as $twitter_object):
337
  wp_enqueue_script( 'rotating_tweet', plugins_url('js/rotating_tweet.js', __FILE__),array('jquery','jquery-cycle'),FALSE,FALSE );
338
  wp_enqueue_style( 'rotating_tweet', plugins_url('css/style.css', __FILE__));
339
  if($print) echo $result;
340
+ return($result);
341
  }
342
  ?>