Latest Tweets Widget - Version 1.0.4

Version Description

  • Library update fixes dates for old PHP versions
Download this release

Release Info

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

Code changes from version 1.0.3 to 1.0.4

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.3
8
  Author URI: http://timwhitlock.info/
9
  */
10
 
@@ -184,4 +184,14 @@ add_action( 'widgets_init', 'latest_tweets_register_widget' );
184
 
185
  if( is_admin() ){
186
  require_once dirname(__FILE__).'/lib/twitter-api.php';
 
 
 
 
 
 
 
 
 
187
  }
 
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
 
184
 
185
  if( is_admin() ){
186
  require_once dirname(__FILE__).'/lib/twitter-api.php';
187
+
188
+ // extra visibility of API settings link
189
+ function latest_tweets_plugin_row_meta( $links, $file ){
190
+ if( false !== strpos($file,'/latest-tweets.php') ){
191
+ $links[] = '<a href="options-general.php?page=twitter-api-admin"><strong>Connect</strong></a>';
192
+ }
193
+ return $links;
194
+ }
195
+ add_action('plugin_row_meta', 'latest_tweets_plugin_row_meta', 10, 2 );
196
  }
197
+
lib/twitter-api-admin.php CHANGED
@@ -86,7 +86,7 @@ function twitter_api_admin_render_login( $consumer_key, $consumer_secret ){
86
  // Remember request token and render link to authorize
87
  // we're storing permanently - not using session here, because WP provides no session API.
88
  _twitter_api_config( array( 'request_secret' => $Token->secret ) );
89
- $href = TWITTER_OAUTH_AUTHORIZE_URL.'?oauth_token='.rawurlencode($Token->key);
90
  echo '<p><a class="button-primary" href="',esc_html($href),'">Connect to Twitter</a></p>';
91
  echo '<p>&nbsp;</p>';
92
  }
86
  // Remember request token and render link to authorize
87
  // we're storing permanently - not using session here, because WP provides no session API.
88
  _twitter_api_config( array( 'request_secret' => $Token->secret ) );
89
+ $href = $Token->get_authorization_url();
90
  echo '<p><a class="button-primary" href="',esc_html($href),'">Connect to Twitter</a></p>';
91
  echo '<p>&nbsp;</p>';
92
  }
lib/twitter-api-core.php CHANGED
@@ -116,7 +116,7 @@ class TwitterApiClient {
116
  * @internal
117
  */
118
  public function __sleep(){
119
- return array('User','Consumer','AccessToken');
120
  }
121
 
122
  /**
@@ -288,7 +288,7 @@ class TwitterApiClient {
288
  * Perform an OAuth request - these differ somewhat from regular API calls
289
  * @internal
290
  */
291
- function oauth_exchange( $endpoint, array $args ){
292
  // build a post request and authenticate via OAuth header
293
  $params = new TwitterOAuthParams( $args );
294
  $params->set_consumer( $this->Consumer );
@@ -301,7 +301,7 @@ class TwitterApiClient {
301
  'redirection' => 0,
302
  'headers' => array( 'Authorization' => $params->oauth_header() ),
303
  );
304
- $http = self::http_request( $endpoint, $conf, 200 );
305
  $body = trim( $http['body'] );
306
  $stat = $http['response']['code'];
307
  if( 200 !== $stat ){
@@ -361,13 +361,17 @@ class TwitterOAuthToken {
361
  public $key;
362
  public $secret;
363
 
364
- function __construct( $key, $secret = '' ){
365
  if( ! $key ){
366
  throw new Exception( __('Invalid OAuth token').' - '.__('Key required even if secret is empty') );
367
  }
368
  $this->key = $key;
369
  $this->secret = $secret;
370
  }
 
 
 
 
371
 
372
  }
373
 
116
  * @internal
117
  */
118
  public function __sleep(){
119
+ return array('Consumer','AccessToken');
120
  }
121
 
122
  /**
288
  * Perform an OAuth request - these differ somewhat from regular API calls
289
  * @internal
290
  */
291
+ public function oauth_exchange( $endpoint, array $args ){
292
  // build a post request and authenticate via OAuth header
293
  $params = new TwitterOAuthParams( $args );
294
  $params->set_consumer( $this->Consumer );
301
  'redirection' => 0,
302
  'headers' => array( 'Authorization' => $params->oauth_header() ),
303
  );
304
+ $http = self::http_request( $endpoint, $conf );
305
  $body = trim( $http['body'] );
306
  $stat = $http['response']['code'];
307
  if( 200 !== $stat ){
361
  public $key;
362
  public $secret;
363
 
364
+ public function __construct( $key, $secret = '' ){
365
  if( ! $key ){
366
  throw new Exception( __('Invalid OAuth token').' - '.__('Key required even if secret is empty') );
367
  }
368
  $this->key = $key;
369
  $this->secret = $secret;
370
  }
371
+
372
+ public function get_authorization_url(){
373
+ return TWITTER_OAUTH_AUTHORIZE_URL.'?oauth_token='.rawurlencode($this->key);
374
+ }
375
 
376
  }
377
 
lib/twitter-api-utils.php CHANGED
@@ -70,24 +70,39 @@ function twitter_api_relative_date( $strdate ){
70
  $hdiff = (int) floor( $tdiff / 3600 );
71
  return sprintf( _n( 'About %u hour ago', 'About %u hours ago', $hdiff ), $hdiff );
72
  }
73
- //
74
- static $dt;
75
- if( ! isset($dt) ){
76
- $tz = ini_get('date.timezone') or $tz = 'Europe/London';
77
- $tz = new DateTimeZone( $tz );
78
- $dt = new DateTime;
79
- $dt->setTimezone( $tz );
80
- }
81
- $dt->setTimestamp( $tt );
82
  // within 24 hours?
83
  if( $tdiff < 86400 ){
84
- return __('Yesterday at ').$dt->format('g:i A');
85
  }
86
  // else return formatted date, e.g. "Oct 20th 2008 9:27 PM GMT" */
87
- return $dt->format('M jS Y g:i A');
88
  }
89
 
90
 
91
 
92
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
70
  $hdiff = (int) floor( $tdiff / 3600 );
71
  return sprintf( _n( 'About %u hour ago', 'About %u hours ago', $hdiff ), $hdiff );
72
  }
 
 
 
 
 
 
 
 
 
73
  // within 24 hours?
74
  if( $tdiff < 86400 ){
75
+ return __('Yesterday at ').twitter_api_date_format('g:i A', $tt );
76
  }
77
  // else return formatted date, e.g. "Oct 20th 2008 9:27 PM GMT" */
78
+ return twitter_api_date_format('M jS Y g:i A', $tt );
79
  }
80
 
81
 
82
 
83
+ /**
84
+ * Abstraction of date formatting for older PHP versions
85
+ * - DateTime class requires PHP >= 5.2
86
+ * - DateTime::setTimestamp requires PHP >= 5.3
87
+ * @internal
88
+ */
89
+ function twitter_api_date_format( $f, $t ){
90
+ static $oldphp;
91
+ if( ! isset($oldphp) ){
92
+ $oldphp = 0 > version_compare(PHP_VERSION, '5.3');
93
+ }
94
+ if( $oldphp ){
95
+ return date( $f, $t );
96
+ }
97
+ // using DateTime instance to avoid empty timezone warnings and such
98
+ static $dt;
99
+ if( ! isset($dt) ){
100
+ $tz = ini_get('date.timezone') or $tz = 'Europe/London';
101
+ $tz = new DateTimeZone( $tz );
102
+ $dt = new DateTime;
103
+ $dt->setTimezone( $tz );
104
+ }
105
+ $dt->setTimestamp( $t );
106
+ return $dt->format( $f );
107
+ }
108
 
lib/twitter-api.php CHANGED
@@ -1,6 +1,7 @@
1
  <?php
2
  /**
3
- * Twitter API, built from: https://github.com/timwhitlock/wp-twitter-api
 
4
  */
5
 
6
 
1
  <?php
2
  /**
3
+ * Twitter API Wordpress library.
4
+ * @author Tim Whitlock <@timwhitlock>
5
  */
6
 
7
 
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.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -55,10 +55,18 @@ These details are available in the [Twitter dashboard](https://dev.twitter.com/a
55
 
56
  Once you've populated the first two fields, just click the *Connect* button and follow the prompts.
57
 
 
 
 
 
 
58
 
59
 
60
  == Changelog ==
61
 
 
 
 
62
  = 1.0.3 =
63
  * Added theme filters
64
  * Added configs for showing replies and RTs
@@ -115,7 +123,7 @@ If you want to override the default markup of the tweets, the following filters
115
  * Override the unordered list for tweets with `latest_tweets_render_list`
116
  * Add a footer before the end of the widget with `latest_tweets_render_after`
117
 
118
- Here's an **example** of using some of the above in tour theme's functions.php file:
119
 
120
  add_filter('latest_tweets_render_date', function( $created_at ){
121
  $date = DateTime::createFromFormat('D M d H:i:s O Y', $created_at );
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
 
55
 
56
  Once you've populated the first two fields, just click the *Connect* button and follow the prompts.
57
 
58
+ = I get SSL certificate errors =
59
+
60
+ You can disable SSL verification of twitter.com by adding this to your theme functions.php:
61
+ `add_filter('https_ssl_verify', '__return_false');`
62
+ Do so at your own risk.
63
 
64
 
65
  == Changelog ==
66
 
67
+ = 1.0.4 =
68
+ * Library update fixes dates for old PHP versions
69
+
70
  = 1.0.3 =
71
  * Added theme filters
72
  * Added configs for showing replies and RTs
123
  * Override the unordered list for tweets with `latest_tweets_render_list`
124
  * Add a footer before the end of the widget with `latest_tweets_render_after`
125
 
126
+ Here's an **example** of using some of the above in your theme's functions.php file:
127
 
128
  add_filter('latest_tweets_render_date', function( $created_at ){
129
  $date = DateTime::createFromFormat('D M d H:i:s O Y', $created_at );