Rotating Tweets (Twitter widget and shortcode) - Version 0.43

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.

=

Download this release

Release Info

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

Code changes from version 0.42 to 0.43

Files changed (2) hide show
  1. readme.txt +6 -3
  2. rotatingtweets.php +81 -66
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.42
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -40,10 +40,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.42 =
44
- Fixed major bug causing crashes when Twitter goes down
45
 
46
  == Changelog ==
 
 
 
47
  = 0.42 =
48
  Fixed major bug causing crashes when Twitter goes down
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.43
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
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.43 =
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.43 =
48
+ Improved error checking if Twitter has returned an empty value
49
+
50
  = 0.42 =
51
  Fixed major bug causing crashes when Twitter goes down
52
 
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.42
6
  Author: Martin Tod
7
  Author URI: http://www.martintod.org.uk
8
  License: GPL2
@@ -190,13 +190,18 @@ function rotatingtweets_get_tweets($tw_screen_name,$tw_include_rts,$tw_exclude_r
190
  $latest_json_date = $option[$stringname][datetime];
191
  $timegap = time()-$latest_json_date;
192
  if($timegap > $cache_delay):
193
- $callstring = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$tw_screen_name."&include_entities=1&count=20&include_rts=".$tw_include_rts."&exclude_replies=".$tw_exclude_replies;
194
  $twitterdata = wp_remote_request($callstring);
195
  if(!is_wp_error($twitterdata)):
196
  $twitterjson = json_decode($twitterdata['body']);
197
  endif;
198
  endif;
199
- if(!empty($twitterjson) && empty($twitterjson->errors)):
 
 
 
 
 
200
  $latest_json = $twitterjson;
201
  $option[$stringname][json]=$latest_json;
202
  $option[$stringname][datetime]=time();
@@ -208,73 +213,83 @@ function rotatingtweets_get_tweets($tw_screen_name,$tw_include_rts,$tw_exclude_r
208
  # Displays the tweets
209
  function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeout=4000,$print=TRUE) {
210
  unset($result);
211
- if(!empty($json->errors) or empty($json)) return;
212
  $tweet_count = max(1,intval($tweet_count));
213
  $timeout = max(intval($timeout),0);
214
- $result = "<div class='rotatingtweets' id='".uniqid('rotatingtweets_'.$timeout.'_')."'>";
215
- $tweet_counter = 0;
216
- foreach($json as $twitter_object):
217
- $tweet_counter++;
218
- if($tweet_counter <= $tweet_count):
219
- if($tweet_counter == 1):
220
- $result .= "\n<div class = 'rotatingtweet'>";
221
- else:
222
- $result .= "\n<div class = 'rotatingtweet' style='display:none'>";
223
- endif;
224
- $main_text = $twitter_object->text;
225
- $user = $twitter_object->user;
226
- # Now the substitutions
227
- $entities = $twitter_object->entities;
228
- # Fix up retweets, links, hashtags and use names
229
- unset($before);
230
- unset($after);
231
- # First clean up the retweets
232
- $rt_data = $twitter_object->retweeted_status;
233
- if(!empty($rt_data)):
234
- $rt_user = $rt_data->user;
235
- $main_text = "RT @".$rt_user->screen_name . " " . $rt_data->text;
236
- $before[] = "*@".$rt_user->screen_name."*i";
237
- $after[] = "<a href='http://twitter.com/".$rt_user->screen_name."' title='".$rt_user->name."'>@".$rt_user->screen_name."</a>";
238
- $entities = $rt_data->entities;
239
- endif;
240
- # First the user mentions
241
- $user_mentions = $entities->user_mentions;
242
- if(!empty($user_mentions)):
243
- foreach($user_mentions as $user_mention):
244
- $before[] = "*@".$user_mention->screen_name."*i";
245
- $after[] = "<a href='http://twitter.com/".$user_mention->screen_name."' title='".$user_mention->name."'>@".$user_mention->screen_name."</a>";
246
- endforeach;
247
- # Clearing up duplicates to avoid strange result (possibly risky?)
248
- $before = array_unique($before);
249
- $after = array_unique($after);
250
- endif;
251
- # Now the URLs
252
- $urls = $entities->urls;
253
- if(!empty($urls)):
254
- foreach($urls as $url):
255
- $before[] = "*".$url->url."*";
256
- $after[] = "<a href='".$url->expanded_url."'>".$url->display_url."</a>";
257
- endforeach;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  endif;
259
- $media = $entities->media;
260
- if(!empty($media)):
261
- foreach($media as $medium):
262
- $before[] = "*".$medium->url."*";
263
- $after[] = "<a href='".$medium->expanded_url."'>".$medium->display_url."</a>";
264
- endforeach;
265
- endif;
266
- $before[]="%#(\w+)%";
267
- $after[]='<a href="http://search.twitter.com/search?q=%23$1" title="#$1">#$1</a>';
268
- $main_text = preg_replace($before,$after,$main_text);
269
- $result .= "<p class='rtw_main'>$main_text</p>\n";
270
- $result .= "<p class='rtw_meta'><a href='http://twitter.com/".$user->screen_name."/status/".$twitter_object->id_str."'>".ucfirst(rotatingtweets_contextualtime(strtotime($twitter_object->created_at)))."</a> from <a target='_BLANK' href='http://twitter.com/".$user->screen_name."' title=\"".$user->name."\">".$user->name."'s Twitter</a> via ".$twitter_object->source;
271
- # $result .= '<br /><a href="http://twitter.com/intent/tweet?in_reply_to='.$twitter_object->id_str.'" title="Reply"><img src="'.plugins_url('images/reply.png', __FILE__).'" width="16" height="16" alt="Reply" /></a> <a href="http://twitter.com/intent/retweet?tweet_id='.$twitter_object->id_str.'" title="Retweet" ><img src="'.plugins_url('images/retweet.png', __FILE__).'" width="16" height="16" alt="Retweet" /></a> <a href="http://twitter.com/intent/favorite?tweet_id='.$twitter_object->id_str.'" title="Favourite"><img src="'.plugins_url('images/favorite.png', __FILE__).'" alt="Favorite" width="16" height="16" /></a></p>';
272
- $result .= "</div>";
273
- endif;
274
- endforeach;
275
- $result .= "</div>\n";
276
  if($show_follow==TRUE):
277
- $result .= "<div class='follow-button'><a href='http://twitter.com/".$user->screen_name."' class='twitter-follow-button' title='Follow @".$user->screen_name."'>@".$user->screen_name."</a></div>";
278
  $script = 'http://platform.twitter.com/widgets.js';
279
  wp_enqueue_script( 'twitter-wjs', $script, FALSE, FALSE, TRUE );
280
  endif;
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.43
6
  Author: Martin Tod
7
  Author URI: http://www.martintod.org.uk
8
  License: GPL2
190
  $latest_json_date = $option[$stringname][datetime];
191
  $timegap = time()-$latest_json_date;
192
  if($timegap > $cache_delay):
193
+ $callstring = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".urlencode($tw_screen_name)."&include_entities=1&count=20&include_rts=".$tw_include_rts."&exclude_replies=".$tw_exclude_replies;
194
  $twitterdata = wp_remote_request($callstring);
195
  if(!is_wp_error($twitterdata)):
196
  $twitterjson = json_decode($twitterdata['body']);
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();
213
  # Displays the tweets
214
  function rotating_tweets_display($json,$tweet_count=5,$show_follow=FALSE,$timeout=4000,$print=TRUE) {
215
  unset($result);
 
216
  $tweet_count = max(1,intval($tweet_count));
217
  $timeout = max(intval($timeout),0);
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):
225
+ $tweet_counter++;
226
+ if($tweet_counter <= $tweet_count):
227
+ if($tweet_counter == 1):
228
+ $result .= "\n\t<div class = 'rotatingtweet'>";
229
+ else:
230
+ $result .= "\n\t<div class = 'rotatingtweet' style='display:none'>";
231
+ endif;
232
+ $main_text = $twitter_object->text;
233
+ if(!empty($main_text)):
234
+ $user = $twitter_object->user;
235
+ # Now the substitutions
236
+ $entities = $twitter_object->entities;
237
+ # Fix up retweets, links, hashtags and use names
238
+ unset($before);
239
+ unset($after);
240
+ # First clean up the retweets
241
+ $rt_data = $twitter_object->retweeted_status;
242
+ if(!empty($rt_data)):
243
+ $rt_user = $rt_data->user;
244
+ $main_text = "RT @".$rt_user->screen_name . " " . $rt_data->text;
245
+ $before[] = "*@".$rt_user->screen_name."*i";
246
+ $after[] = "<a href='http://twitter.com/".$rt_user->screen_name."' title='".$rt_user->name."'>@".$rt_user->screen_name."</a>";
247
+ $entities = $rt_data->entities;
248
+ endif;
249
+ # First the user mentions
250
+ $user_mentions = $entities->user_mentions;
251
+ if(!empty($user_mentions)):
252
+ foreach($user_mentions as $user_mention):
253
+ $before[] = "*@".$user_mention->screen_name."*i";
254
+ $after[] = "<a href='http://twitter.com/".$user_mention->screen_name."' title='".$user_mention->name."'>@".$user_mention->screen_name."</a>";
255
+ endforeach;
256
+ # Clearing up duplicates to avoid strange result (possibly risky?)
257
+ $before = array_unique($before);
258
+ $after = array_unique($after);
259
+ endif;
260
+ # Now the URLs
261
+ $urls = $entities->urls;
262
+ if(!empty($urls)):
263
+ foreach($urls as $url):
264
+ $before[] = "*".$url->url."*";
265
+ $after[] = "<a href='".$url->expanded_url."'>".$url->display_url."</a>";
266
+ endforeach;
267
+ endif;
268
+ $media = $entities->media;
269
+ if(!empty($media)):
270
+ foreach($media as $medium):
271
+ $before[] = "*".$medium->url."*";
272
+ $after[] = "<a href='".$medium->expanded_url."'>".$medium->display_url."</a>";
273
+ endforeach;
274
+ endif;
275
+ $before[]="%#(\w+)%";
276
+ $after[]='<a href="http://search.twitter.com/search?q=%23$1" title="#$1">#$1</a>';
277
+ $main_text = preg_replace($before,$after,$main_text);
278
+ $result .= "\n\t\t<p class='rtw_main'>$main_text</p>";
279
+ $result .= "\n\t\t<p class='rtw_meta'><a href='http://twitter.com/".$user->screen_name."/status/".$twitter_object->id_str."'>".ucfirst(rotatingtweets_contextualtime(strtotime($twitter_object->created_at)))."</a> from <a target='_BLANK' href='http://twitter.com/".$user->screen_name."' title=\"".$user->name."\">".$user->name."'s Twitter</a> via ".$twitter_object->source;
280
+ # $result .= '<br /><a href="http://twitter.com/intent/tweet?in_reply_to='.$twitter_object->id_str.'" title="Reply"><img src="'.plugins_url('images/reply.png', __FILE__).'" width="16" height="16" alt="Reply" /></a> <a href="http://twitter.com/intent/retweet?tweet_id='.$twitter_object->id_str.'" title="Retweet" ><img src="'.plugins_url('images/retweet.png', __FILE__).'" width="16" height="16" alt="Retweet" /></a> <a href="http://twitter.com/intent/favorite?tweet_id='.$twitter_object->id_str.'" title="Favourite"><img src="'.plugins_url('images/favorite.png', __FILE__).'" alt="Favorite" width="16" height="16" /></a></p>';
281
+ else:
282
+ $result .= "\n\t\t<p class='rtw_main'>Problem retrieving data from Twitter.</p></div>";
283
+ $result .= "<!-- rotatingtweets plugin was unable to parse this data: ".print_r($json,TRUE)." -->";
284
+ $result .= "\n\t\t<div class = 'rotatingtweet' style='display:none'><p class='rtw_main'>Please check the comments on this page's HTML to understand more.</p>";
285
+ endif;
286
+ $result .= "\n\t</div>";
287
  endif;
288
+ endforeach;
289
+ endif;
290
+ $result .= "\n</div>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  if($show_follow==TRUE):
292
+ $result .= "\n<div class='follow-button'><a href='http://twitter.com/".$user->screen_name."' class='twitter-follow-button' title='Follow @".$user->screen_name."'>@".$user->screen_name."</a></div>";
293
  $script = 'http://platform.twitter.com/widgets.js';
294
  wp_enqueue_script( 'twitter-wjs', $script, FALSE, FALSE, TRUE );
295
  endif;