Latest Tweets Widget - Version 1.0.5

Version Description

  • Moved widget title outside latest-tweets wrapper
  • Using WordPress 'transient' cache when APC not available
Download this release

Release Info

Developer timwhitlock
Plugin Icon 128x128 Latest Tweets Widget
Version 1.0.5
Comparing to
See all releases

Code changes from version 1.0.4 to 1.0.5

Files changed (3) hide show
  1. latest-tweets.php +5 -7
  2. lib/twitter-api-core.php +58 -54
  3. readme.txt +7 -1
latest-tweets.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Latest Tweets
4
  Plugin URI: http://wordpress.org/extend/plugins/latest-tweets-widget/
5
  Description: Provides a sidebar widget showing latest tweets - compatible with the new Twitter API 1.1
6
  Author: Tim Whitlock
7
- Version: 1.0.4
8
  Author URI: http://timwhitlock.info/
9
  */
10
 
@@ -23,10 +23,8 @@ function latest_tweets_render( $screen_name, $count, $rts, $ats ){
23
  if( ! function_exists('twitter_api_get') ){
24
  require_once dirname(__FILE__).'/lib/twitter-api.php';
25
  }
26
- if( function_exists('apc_fetch') ){
27
- // We could cache the rendered HTML right here, but this keeps caching abstracted in library
28
- twitter_api_enable_cache( 300 );
29
- }
30
  // Build API params for "statuses/user_timeline" // https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
31
  $trim_user = true;
32
  $include_rts = ! empty($rts);
@@ -78,7 +76,7 @@ function latest_tweets_render( $screen_name, $count, $rts, $ats ){
78
 
79
 
80
  /**
81
- * Example latest tweets widget class
82
  */
83
  class Latest_Tweets_Widget extends WP_Widget {
84
 
@@ -162,8 +160,8 @@ class Latest_Tweets_Widget extends WP_Widget {
162
  // output widget applying filters to each element
163
  echo
164
  $args['before_widget'],
 
165
  '<div class="latest-tweets">',
166
- $title,
167
  apply_filters( 'latest_tweets_render_before', '' ),
168
  $list,
169
  apply_filters( 'latest_tweets_render_after', '' ),
4
  Plugin URI: http://wordpress.org/extend/plugins/latest-tweets-widget/
5
  Description: Provides a sidebar widget showing latest tweets - compatible with the new Twitter API 1.1
6
  Author: Tim Whitlock
7
+ Version: 1.0.5
8
  Author URI: http://timwhitlock.info/
9
  */
10
 
23
  if( ! function_exists('twitter_api_get') ){
24
  require_once dirname(__FILE__).'/lib/twitter-api.php';
25
  }
26
+ // We could cache the rendered HTML right here, but this keeps caching abstracted in library
27
+ twitter_api_enable_cache( 300 );
 
 
28
  // Build API params for "statuses/user_timeline" // https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
29
  $trim_user = true;
30
  $include_rts = ! empty($rts);
76
 
77
 
78
  /**
79
+ * latest tweets widget class
80
  */
81
  class Latest_Tweets_Widget extends WP_Widget {
82
 
160
  // output widget applying filters to each element
161
  echo
162
  $args['before_widget'],
163
+ $title,
164
  '<div class="latest-tweets">',
 
165
  apply_filters( 'latest_tweets_render_before', '' ),
166
  $list,
167
  apply_filters( 'latest_tweets_render_after', '' ),
lib/twitter-api-core.php CHANGED
@@ -15,6 +15,8 @@ define('TWITTER_OAUTH_AUTHORIZE_URL', 'https://twitter.com/oauth/authorize');
15
  define('TWITTER_OAUTH_AUTHENTICATE_URL', 'https://twitter.com/oauth/authenticate');
16
 
17
  define('TWITTER_OAUTH_ACCESS_TOKEN_URL', 'https://twitter.com/oauth/access_token');
 
 
18
 
19
 
20
 
@@ -49,6 +51,45 @@ function _twitter_api_config( array $update = array() ){
49
 
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  /**
53
  * Client for the Twitter REST API 1.1
54
  */
@@ -124,13 +165,9 @@ class TwitterApiClient {
124
  * @return TwitterApiClient
125
  */
126
  public function enable_cache( $ttl = 0, $namespace = 'wp_twitter_api_' ){
127
- if( function_exists('apc_store') ){
128
- $this->cache_ttl = (int) $ttl;
129
- $this->cache_ns = $namespace;
130
- return $this;
131
- }
132
- trigger_error( __('Cannot enable Twitter API cache without APC extension') );
133
- return $this->disable_cache();
134
  }
135
 
136
  /**
@@ -218,7 +255,7 @@ class TwitterApiClient {
218
  if( preg_match('/^(\d+)-/', $this->AccessToken->key, $reg ) ){
219
  $cachekey .= '_'.$reg[1];
220
  }
221
- $data = apc_fetch( $cachekey );
222
  if( is_array($data) ){
223
  return $data;
224
  }
@@ -249,7 +286,7 @@ class TwitterApiClient {
249
  // unserializable array assumed to be serious error
250
  if( ! is_array($data) ){
251
  $err = array(
252
- 'message' => __($http['response']['message']),
253
  'code' => -1
254
  );
255
  TwitterApiException::chuck( $err, $status );
@@ -268,7 +305,7 @@ class TwitterApiClient {
268
  }
269
  }
270
  if( isset($cachekey) ){
271
- apc_store( $cachekey, $data, $this->cache_ttl );
272
  }
273
  // remember current rate limits for this endpoint
274
  $this->last_call = $path;
@@ -455,61 +492,28 @@ class TwitterOAuthParams {
455
 
456
 
457
  /**
458
- * HTTP status codes with some overridden for Twitter-related messages.
459
  * Note these do not replace error text from Twitter, they're for complete API failures.
460
  * @param int HTTP status code
461
  * @return string HTTP status text
462
  */
463
  function _twitter_api_http_status_text( $s ){
464
  static $codes = array (
465
- 100 => 'Continue',
466
- 101 => 'Switching Protocols',
467
-
468
- 200 => 'OK',
469
- 201 => 'Created',
470
- 202 => 'Accepted',
471
- 203 => 'Non-Authoritative Information',
472
- 204 => 'No Content',
473
- 205 => 'Reset Content',
474
- 206 => 'Partial Content',
475
-
476
- 300 => 'Multiple Choices',
477
- 301 => 'Moved Permanently',
478
- 302 => 'Found',
479
- 303 => 'See Other',
480
- 304 => 'Not Modified',
481
- 305 => 'Use Proxy',
482
- 307 => 'Temporary Redirect',
483
-
484
- 400 => 'Bad Request',
485
- 401 => 'Authorization Required',
486
- 402 => 'Payment Required',
487
- 403 => 'Forbidden',
488
- 404 => 'Not Found',
489
- 405 => 'Method Not Allowed',
490
- 406 => 'Not Acceptable',
491
- 407 => 'Proxy Authentication Required',
492
- 408 => 'Request Time-out',
493
- 409 => 'Conflict',
494
- 410 => 'Gone',
495
- 411 => 'Length Required',
496
- 412 => 'Precondition Failed',
497
- 413 => 'Request Entity Too Large',
498
- 414 => 'Request-URI Too Large',
499
- 415 => 'Unsupported Media Type',
500
- 416 => 'Requested range not satisfiable',
501
- 417 => 'Expectation Failed',
502
- // ..
503
  429 => 'Twitter API rate limit exceeded',
504
-
505
  500 => 'Twitter server error',
506
- 501 => 'Not Implemented',
507
  502 => 'Twitter is not responding',
508
  503 => 'Twitter is too busy to respond',
509
- 504 => 'Gateway Time-out',
510
- 505 => 'HTTP Version not supported',
511
  );
512
- return __( isset($codes[$s]) ? $codes[$s] : sprintf('Status %u from Twitter', $s) );
 
 
 
 
 
 
 
 
 
513
  }
514
 
515
 
15
  define('TWITTER_OAUTH_AUTHENTICATE_URL', 'https://twitter.com/oauth/authenticate');
16
 
17
  define('TWITTER_OAUTH_ACCESS_TOKEN_URL', 'https://twitter.com/oauth/access_token');
18
+
19
+ define('TWITTER_CACHE_APC', function_exists('apc_fetch') );
20
 
21
 
22
 
51
 
52
 
53
 
54
+ /**
55
+ * abstraction of cache fetching, using apc where possible
56
+ * @return mixed
57
+ */
58
+ function _twitter_api_cache_get( $key ){
59
+ if( TWITTER_CACHE_APC ){
60
+ return apc_fetch( $key );
61
+ }
62
+ if( isset($key{45}) ){
63
+ $key = 'twcache_'.md5($key);
64
+ }
65
+ return get_transient( $key );
66
+ }
67
+
68
+
69
+
70
+ /**
71
+ * abstraction of cache setting, using apc where possible
72
+ * @internal
73
+ * @return void
74
+ */
75
+ function _twitter_api_cache_set( $key, $value, $ttl ){
76
+ if( TWITTER_CACHE_APC ){
77
+ apc_store( $key, $value, $ttl );
78
+ return;
79
+ }
80
+ if( isset($key{45}) ){
81
+ $key = 'twcache_'.md5($key);
82
+ }
83
+ if( ! $ttl ){
84
+ // WP will expire immediately as opposed to never, setting to ten days.
85
+ $ttl = 864000;
86
+ }
87
+ set_transient( $key, $value, $ttl );
88
+ }
89
+
90
+
91
+
92
+
93
  /**
94
  * Client for the Twitter REST API 1.1
95
  */
165
  * @return TwitterApiClient
166
  */
167
  public function enable_cache( $ttl = 0, $namespace = 'wp_twitter_api_' ){
168
+ $this->cache_ttl = (int) $ttl;
169
+ $this->cache_ns = $namespace;
170
+ return $this;
 
 
 
 
171
  }
172
 
173
  /**
255
  if( preg_match('/^(\d+)-/', $this->AccessToken->key, $reg ) ){
256
  $cachekey .= '_'.$reg[1];
257
  }
258
+ $data = _twitter_api_cache_get( $cachekey );
259
  if( is_array($data) ){
260
  return $data;
261
  }
286
  // unserializable array assumed to be serious error
287
  if( ! is_array($data) ){
288
  $err = array(
289
+ 'message' => '', // <- blank so we use twitter-specific message
290
  'code' => -1
291
  );
292
  TwitterApiException::chuck( $err, $status );
305
  }
306
  }
307
  if( isset($cachekey) ){
308
+ _twitter_api_cache_set( $cachekey, $data, $this->cache_ttl );
309
  }
310
  // remember current rate limits for this endpoint
311
  $this->last_call = $path;
492
 
493
 
494
  /**
495
+ * Overridden HTTP status codes for common Twitter-related problems.
496
  * Note these do not replace error text from Twitter, they're for complete API failures.
497
  * @param int HTTP status code
498
  * @return string HTTP status text
499
  */
500
  function _twitter_api_http_status_text( $s ){
501
  static $codes = array (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  429 => 'Twitter API rate limit exceeded',
 
503
  500 => 'Twitter server error',
 
504
  502 => 'Twitter is not responding',
505
  503 => 'Twitter is too busy to respond',
 
 
506
  );
507
+ if( isset($codes[$s]) ){
508
+ return __( $codes[$s] );
509
+ }
510
+ // fall back to Wordpress registry to save bloat
511
+ $text = get_status_header_desc( $s );
512
+ if( $text ){
513
+ return __( $text );
514
+ }
515
+ // unknown status
516
+ return sprintf( __('Status %u from Twitter'), $s );
517
  }
518
 
519
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://timwhitlock.info/donate-to-a-project/
4
  Tags: twitter, tweets, oauth, api, rest, api, widget, sidebar
5
  Requires at least: 3.5.1
6
  Tested up to: 3.5.1
7
- Stable tag: 1.0.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -21,6 +21,8 @@ Built by [timwhitlock](https://twitter.com/timwhitlock)
21
 
22
  The underlying Twitter API library is [available on Github](https://github.com/timwhitlock/wp-twitter-api)
23
 
 
 
24
 
25
  == Installation ==
26
 
@@ -64,6 +66,10 @@ Do so at your own risk.
64
 
65
  == Changelog ==
66
 
 
 
 
 
67
  = 1.0.4 =
68
  * Library update fixes dates for old PHP versions
69
 
4
  Tags: twitter, tweets, oauth, api, rest, api, widget, sidebar
5
  Requires at least: 3.5.1
6
  Tested up to: 3.5.1
7
+ Stable tag: 1.0.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
21
 
22
  The underlying Twitter API library is [available on Github](https://github.com/timwhitlock/wp-twitter-api)
23
 
24
+ See also [Latest Vines](http://wordpress.org/extend/plugins/latest-vines-widget/)
25
+
26
 
27
  == Installation ==
28
 
66
 
67
  == Changelog ==
68
 
69
+ = 1.0.5 =
70
+ * Moved widget title outside latest-tweets wrapper
71
+ * Using WordPress 'transient' cache when APC not available
72
+
73
  = 1.0.4 =
74
  * Library update fixes dates for old PHP versions
75