diff --git a/README.md b/README.md index aed7bf401d58b6169ad4382897793043f33bea58..49abf7d27108680654139029a69be5f1b8316cfc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ WordPress Codebird * _Contributors: @automattic, @batmoo, @danielbachuber, @nickdaugherty_ * _Tested up to: 3.5.1_ -* _Stable tag: 1.0.1_ +* _Stable tag: 1.1.0_ * _License: GPLv2 or later_ * _License URI: http://www.gnu.org/licenses/gpl-2.0.html_ @@ -23,4 +23,10 @@ Include both the Codebird library and `class-wp-codebird.php`, then get a new in $wp_codebird = WP_Codebird::getInstance(); ``` -The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code. \ No newline at end of file +The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code. + +Changes +-------------------- +**1.1.0 (04/09/2013)** + +* Updated to support Codebird 2.3.2 and Bearer authentication \ No newline at end of file diff --git a/class-wp-codebird.php b/class-wp-codebird.php index f9537ac4968334af696f4e0841c3f8a2ed71e71f..a9333711a770d05529d140fbc7f46d15d3b94bc5 100644 --- a/class-wp-codebird.php +++ b/class-wp-codebird.php @@ -4,9 +4,14 @@ * An extension of the Codebird class to use Wordpress' HTTP API instead of * cURL. * - * @version 1.0.1 + * @version 1.1.0 */ class WP_Codebird extends Codebird { + /** + * The current singleton instance + */ + private static $_instance = null; + /** * Returns singleton class instance * Always use this method unless you're working with multiple authenticated @@ -39,8 +44,10 @@ class WP_Codebird extends Codebird { * * @return mixed The API reply, encoded in the set return_format. */ - protected function _callApi($httpmethod, $method, $method_template, $params = array(), $multipart = false) { - $url = $this->_getEndpoint( $method, $method_template ); + protected function _callApi( $httpmethod, $method, $method_template, $params = array(), $multipart = false, $app_only_auth = false ) { + $url = $this->_getEndpoint( $method, $method_template ); + $url_with_params = null; + $remote_params = array( 'method' => 'GET', 'timeout' => 5, @@ -53,15 +60,19 @@ class WP_Codebird extends Codebird { 'sslverify' => false ); - if ($httpmethod == 'GET') { - $signed = $this->_sign( $httpmethod, $url, $params ); - $reply = wp_remote_get( $signed, $remote_params ); + if ( 'GET' == $httpmethod ) { + $authorization = $this->_sign( $httpmethod, $url, $params ); + + if ( count( $params ) > 0 ) { + $url_with_params = $url .= '?' . http_build_query( $params ); + } } else { if ( $multipart ) { $authorization = $this->_sign( 'POST', $url, array(), true ); $post_fields = $params; } else { - $post_fields = $this->_sign( 'POST', $url, $params ); + $authorization = $this->_sign( 'POST', $url, $params ); + $post_fields = $this->_sign( 'POST', $url, $params ); } $headers = array(); @@ -80,7 +91,29 @@ class WP_Codebird extends Codebird { 'cookies' => array(), 'sslverify' => false ); + } + + if ( $app_only_auth ){ + if ( null == self::$_oauth_consumer_key ) + throw new Exception( 'To make an app-only auth API request, the consumer key must be set' ); + + // automatically fetch bearer token, if necessary + if ( null == self::$_oauth_bearer_token ) + $this->oauth2_token(); + + $bearer = 'Bearer ' . self::$_oauth_bearer_token; + $remote_params['headers']['authorization'] = $bearer; + } else { + // If this is a standard OAuth GET request, add on the authorization header + // Must be added here because $app_only_auth affects what the header will be + if ( 'GET' == $httpmethod ) + $remote_params['headers'][] = $authorization; + } + + if ( 'GET' == $httpmethod ) { + $reply = wp_remote_get( $url, $remote_params ); + } else { $reply = wp_remote_post( $url, $remote_params ); } @@ -104,6 +137,63 @@ class WP_Codebird extends Codebird { return $reply; } + /** + * Gets the OAuth bearer token + * + * Overridden to use the WordPress HTTP API + * + * @return string The OAuth bearer token + */ + + public function oauth2_token() { + if ( null == self::$_oauth_consumer_key ) { + throw new Exception('To obtain a bearer token, the consumer key must be set.'); + } + + $post_fields = array( + 'grant_type' => 'client_credentials' + ); + + $url = self::$_endpoint_oauth . 'oauth2/token'; + + $headers = array( + 'Authorization' => 'Basic ' . base64_encode( self::$_oauth_consumer_key . ':' . self::$_oauth_consumer_secret ), + 'Expect:' + ); + + $remote_params = array( + 'method' => 'POST', + 'timeout' => 5, + 'redirection' => 5, + 'httpversion' => '1.0', + 'blocking' => true, + 'headers' => $headers, + 'body' => $post_fields, + 'cookies' => array(), + 'sslverify' => false + ); + + $reply = wp_remote_post( $url, $remote_params ); + + $httpstatus = wp_remote_retrieve_response_code( $reply ); + + $reply = $this->_parseApiReply( 'oauth2/token', $reply ); + + if ( CODEBIRD_RETURNFORMAT_OBJECT == $this->_return_format ) { + $reply->httpstatus = $httpstatus; + + if ( 200 == $httpstatus ) + self::setBearerToken( $reply->access_token ); + } else { + $reply['httpstatus'] = $httpstatus; + + if ( 200 == $httpstatus ) + self::setBearerToken( $reply['access_token'] ); + } + + return $reply; + } + /** * Parses the API reply to encode it in the set return_format. * @@ -153,4 +243,4 @@ class WP_Codebird extends Codebird { } return $parsed; } -} +} \ No newline at end of file