Permalink
Browse files

Replace legacy Twitter library with tmhOAuth.

tmhOAuth provides several benefits over the previous library, including
the ability to upload media with tweets. Added the new Upload Media
option for tweets and updated contextual help to reflect the new
option. Also expanded the recognized Twitter tokens to include image
sizes.
  • Loading branch information...
1 parent 03285c4 commit dce9e0cff368ed4bc666f2771543d7734c51c1b1 Michael Sisk committed Mar 6, 2013
Showing with 4,156 additions and 1,148 deletions.
  1. +3,376 −0 -/lib/cacert.pem
  2. +0 −917 -/lib/oauth.php
  3. +648 −192 -/lib/twitter.php
  4. +1 −0 -/php/admin.php
  5. +40 −15 -/php/config.php
  6. +3 −1 -/php/help.php
  7. +3 −0 -/php/legacy.php
  8. +14 −0 -/php/upgrade.php
  9. +71 −23 webcomic.php
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -132,6 +132,7 @@ public function activate() {
'roles' => array( '!' )
),
'twitter' => array(
+ 'media' => false,
'format' => __( '%collection-name has updated! %site-url', 'webcomic' ),
'oauth_token' => '',
'oauth_secret' => '',
View
@@ -126,6 +126,7 @@ public function admin_init() {
add_settings_field( "{$k}_twitter_consumer_key", __( 'Consumer Key', 'webcomic' ), array( $this, 'collection_twitter_consumer_key' ), "{$k}-options", "{$k}-twitter", array( 'label_for' => 'webcomic_twitter_consumer_key' ) );
add_settings_field( "{$k}_twitter_consumer_secret", __( 'Consumer Secret', 'webcomic' ), array( $this, 'collection_twitter_consumer_secret' ), "{$k}-options", "{$k}-twitter", array( 'label_for' => 'webcomic_twitter_consumer_secret' ) );
add_settings_field( "{$k}_twitter_format", __( 'Tweet Format', 'webcomic' ), array( $this, 'collection_twitter_format' ), "{$k}-options", "{$k}-twitter", array( 'label_for' => 'webcomic_twitter_format' ) );
+ add_settings_field( "{$k}_twitter_media", __( 'Upload Media', 'webcomic' ), array( $this, 'collection_twitter_media' ), "{$k}-options", "{$k}-twitter", array( 'label_for' => 'webcomic_twitter_media' ) );
}
if ( ( isset( $_GET[ 'page' ], $_GET[ 'settings-updated' ] ) and 'webcomic-options' === $_GET[ 'page' ] and 'true' === $_GET[ 'settings-updated' ] ) or ( isset( $_GET[ 'page' ], $_GET[ 'post_type' ], $_GET[ 'settings-updated' ] ) and preg_match( '/^webcomic\d+-options$/', $_GET[ 'page' ] ) and isset( self::$config[ 'collections' ][ $_GET[ 'post_type' ] ] ) and 'true' === $_GET[ 'settings-updated' ] ) ) {
@@ -870,6 +871,16 @@ public function collection_twitter_format() {
<?php
}
+ /** Render Twitter > Media
+ *
+ * @uses Webcomic::$config
+ */
+ public function collection_twitter_media() {
+ ?>
+ <label><input type="checkbox" name="webcomic_twitter_media" id="webcomic_twitter_media"<?php checked( self::$config[ 'collections' ][ $_GET[ 'post_type' ] ][ 'twitter' ][ 'media' ] ); ?>> <?php _e( 'Upload media with tweet', 'webcomic' ); ?></label>
+ <?php
+ }
+
/** Save callback for the webcomic option.
*
* If 'webcomic_general' is set we're working on the general
@@ -995,6 +1006,7 @@ public function save( $options ) {
'roles' => array( '!' )
),
'twitter' => array(
+ 'media' => isset( $_POST[ 'webcomic_twitter_media' ] ),
'format' => $_POST[ 'webcomic_twitter_format' ],
'oauth_token' => self::$config[ 'collections' ][ $id ][ 'twitter' ][ 'oauth_token' ],
'oauth_secret' => self::$config[ 'collections' ][ $id ][ 'twitter' ][ 'oauth_secret' ],
@@ -1234,43 +1246,56 @@ public static function ajax_commerce_defaults( $email ) {
* @param string $collection Collection the Twitter credentials belong to.
*/
public static function ajax_twitter_account( $consumer_key, $consumer_secret, $collection ) {
- if ( !class_exists( 'TwitterOAuth' ) ) {
+ if ( !class_exists( 'tmhOAuth' ) ) {
require_once self::$dir . '-/lib/twitter.php';
}
+ $oauth = new tmhOAuth( array_merge( array(
+ 'consumer_key' => $consumer_key,
+ 'consumer_secret' => $consumer_secret
+ ), ( $consumer_key and $consumer_secret ) ? array(
+ 'user_token' => self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_token' ],
+ 'user_secret' => self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_secret' ]
+ ) : array()
+ ) );
+
if ( $consumer_key and $consumer_secret ) {
- $oauth = new TwitterOAuth( $consumer_key, $consumer_secret, self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_token' ], self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_secret' ] );
- $credentials = $oauth->get( 'account/verify_credentials' );
-
self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'consumer_key' ] = $consumer_key;
self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'consumer_secret' ] = $consumer_secret;
update_option( 'webcomic_options', self::$config );
- if ( isset( $credentials->screen_name ) ) {
+ $code = $oauth->request( 'GET', $oauth->url( '1/account/verify_credentials' ) );
+
+ if ( 200 === intval( $code ) ) {
+ $response = json_decode( $oauth->response[ 'response' ] );
+
printf( '<a href="http://twitter.com/%s" target="_blank"><b>@%s</b></a> <a href="https://twitter.com/settings/applications" target="_blank" class="button">%s</a>',
- $credentials->screen_name,
- $credentials->screen_name,
+ $response->screen_name,
+ $response->screen_name,
__( 'Revoke Access', 'webcomic' )
);
} else {
- $oauth = new TwitterOAuth( $consumer_key, $consumer_secret );
- $request = $oauth->getRequestToken( add_query_arg( array( 'webcomic_twitter_oauth' => true, 'webcomic_collection' => $collection ), get_site_url() ) );
+ $code = $oauth->request( 'POST', $oauth->url( 'oauth/request_token', '' ), array(
+ 'oauth_callback' => add_query_arg( array( 'webcomic_twitter_oauth' => true, 'webcomic_collection' => $collection ), get_site_url() )
+ ) );
- if ( isset( $request[ 'Failed to validate oauth signature and token' ] ) ) {
- _e( 'Validation error. Please ensure your <a href="https://dev.twitter.com/apps/new" target="_blank">Twitter Application</a> <b>consumer key</b> and <b>consumer secret</b> are entered correctly.', 'webcomic' );
- } else {
- self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'request_token' ] = isset( $request[ 'oauth_token' ] ) ? $request[ 'oauth_token' ] : '';
- self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'request_secret' ] = isset( $request[ 'oauth_token_secret' ] ) ? $request[ 'oauth_token_secret' ] : '';
+ if ( 200 === intval( $code ) ) {
+ $response = $oauth->extract_params( $oauth->response[ 'response' ] );
+
+ self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'request_token' ] = isset( $response[ 'oauth_token' ] ) ? $response[ 'oauth_token' ] : '';
+ self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'request_secret' ] = isset( $response[ 'oauth_token_secret' ] ) ? $response[ 'oauth_token_secret' ] : '';
update_option( 'webcomic_options', self::$config );
printf( '%s<a href="%s"><img src="%s-/img/twitter.png" alt="%s"></a>',
( self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_token' ] and self::$config[ 'collections' ][ $collection ][ 'twitter' ][ 'oauth_secret' ] ) ? __( '<p class="description">Your credentials could not be verified.</p>', 'webcomic' ) : '',
- $oauth->getAuthorizeURL( $request ),
+ add_query_arg( array( 'oauth_token' => $response[ 'oauth_token' ] ), $oauth->url( 'oauth/authorize', '' ) ),
self::$url,
__( 'Sign in with Twitter', 'webcomic' )
);
+ } else {
+ _e( 'Validation error. Please ensure your <a href="https://dev.twitter.com/apps/new" target="_blank">Twitter Application</a> <b>consumer key</b> and <b>consumer secret</b> are entered correctly.', 'webcomic' );
}
}
} else {
View
@@ -924,11 +924,13 @@ private function collection_settings_twitter( $screen ) {
<p>%s</p>
<p><strong>%s</strong></p>
<p>%s</p>
+ <p>%s</p>
<p><strong>%s</strong></p>
<p>%s</p>%s',
sprintf( __( 'These settings allow you to connect a Twitter account with the collection via a Twitter Application. Anytime a new webcomic is published in this collection a status update will be made to the account you authorize in the format you specify using your Twitter Application. Visit <a href="%s" target="_blank">Twitter Developers</a> to create a new Twitter Application. The optional <strong>Callback URL</strong> setting of your Twitter Application must be set to your site URL:', 'webcomic' ), '//dev.twitter.com/apps/new' ),
home_url(),
- sprintf( __( "Once created, go to the <strong>Settings</strong> tab for your Twitter Application and ensure that <strong>Read and Write</strong> is selected for <strong>Application Type</strong>. Then return to the <strong>Details</strong> tab and copy the <strong>Consumer Key</strong> and <strong>Consumer Secret</strong> values into their respective fields on this page. If the keys are entered correctly a <strong>Sign in with Twitter</strong> option will appear in the <strong>Authorized Account</strong> area. Please refer to the <a href='%s' target='_blank'>Beginner's Guide</a> for a complete list of tags accepted by <strong>Tweet Format</strong>.", 'webcomic' ), '//github.com/mgsisk/webcomic/wiki/Twitter-Settings' ),
+ sprintf( __( "Once created, go to the <strong>Settings</strong> tab for your Twitter Application and ensure that <strong>Read and Write</strong> is selected for <strong>Application Type</strong>. Then return to the <strong>Details</strong> tab and copy the <strong>Consumer Key</strong> and <strong>Consumer Secret</strong> values into their respective fields on this page. If the keys are entered correctly a <strong>Sign in with Twitter</strong> option will appear in the <strong>Authorized Account</strong> area.", 'webcomic' ), '//github.com/mgsisk/webcomic/wiki/Twitter-Settings' ),
+ __( "Please refer to the <a href='%s' target='_blank'>Beginner's Guide</a> for a complete list of tags accepted by <strong>Tweet Format</strong>. When <strong>Upload Media</strong> is selected Webcomic will send the first full-size media attachment along with the tweet it publishes. The media attachment will be uploaded to pic.twitter.com and displayed with the tweet.", 'webcomic' ),
__( 'Twitter Cards', 'webcomic' ),
sprintf( __( 'Twitter Cards do not require a Twitter Application. Webcomic automatially generates the necessary information for Twitter Cards on all Webcomic-related pages, but you must opt-in to have these cards displayed and Twitter must approve the integration. This process only needs to be done once for your entire site. Visit <a href="%s" target="_blank">Participate in Twitter Cards</a> to opt-in.', 'webcomic' ), '//dev.twitter.com/docs/cards', '//dev.twitter.com/form/participate-twitter-cards' ),
$cards
View
@@ -315,6 +315,7 @@ private function upgrade1( $stage = 0 ) {
'roles' => array( '!' )
),
'twitter' => array(
+ 'media' => false,
'format' => __( '%collection-name has updated! %site-url', 'webcomic' ),
'oauth_token' => '',
'oauth_secret' => '',
@@ -599,6 +600,7 @@ private function upgrade2( $stage = 0 ) {
'roles' => array( '!' )
),
'twitter' => array(
+ 'media' => false,
'format' => __( '%collection-name has updated! %site-url', 'webcomic' ),
'oauth_token' => '',
'oauth_secret' => '',
@@ -896,6 +898,7 @@ private function upgrade3( $stage = 0 ) {
'roles' => array( '!' )
),
'twitter' => array(
+ 'media' => false,
'format' => __( '%collection-name has updated! %site-url', 'webcomic' ),
'oauth_token' => '',
'oauth_secret' => '',
View
@@ -25,6 +25,10 @@ public function __construct() {
$this->_406();
}
+ if ( version_compare( self::$config[ 'version' ], '4.0.9', '<' ) ) {
+ $this->_409();
+ }
+
self::$config[ 'version' ] = self::$version;
update_option( 'webcomic_options', self::$config );
@@ -60,4 +64,14 @@ private function _406() {
self::$config[ 'gestures' ] = false;
}
}
+
+ /** Upgrade to 4.0.9
+ *
+ * @uses Webcomic::$config
+ */
+ private function _409() {
+ foreach ( self::$config[ 'collections' ] as $k => $v ) {
+ self::$config[ 'collections' ][ $k ][ 'twitter' ][ 'media' ] = false;
+ }
+ }
}
Oops, something went wrong.

0 comments on commit dce9e0c

Please sign in to comment.