| 1 |
<?php |
| 2 |
/** |
| 3 |
* Twitter API |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Init module |
| 11 |
*/ |
| 12 |
class Powerkit_Twitter_API extends Powerkit_Module { |
| 13 |
|
| 14 |
/** |
| 15 |
* Register module |
| 16 |
*/ |
| 17 |
public function register() { |
| 18 |
$this->name = 'Twitter API'; |
| 19 |
$this->slug = 'twitter-api'; |
| 20 |
$this->type = 'extension'; |
| 21 |
$this->category = 'basic'; |
| 22 |
$this->public = false; |
| 23 |
$this->enabled = false; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* The base_url. |
| 28 |
* |
| 29 |
* @var string $base_url The base_url. |
| 30 |
*/ |
| 31 |
protected $base_url; |
| 32 |
|
| 33 |
/** |
| 34 |
* The get_fields. |
| 35 |
* |
| 36 |
* @var string $get_fields The get_fields. |
| 37 |
*/ |
| 38 |
private $get_fields; |
| 39 |
|
| 40 |
/** |
| 41 |
* The oauth. |
| 42 |
* |
| 43 |
* @var string $oauth The oauth. |
| 44 |
*/ |
| 45 |
private $oauth; |
| 46 |
|
| 47 |
/** |
| 48 |
* The header. |
| 49 |
* |
| 50 |
* @var string $header The header. |
| 51 |
*/ |
| 52 |
private $header; |
| 53 |
|
| 54 |
/** |
| 55 |
* The json. |
| 56 |
* |
| 57 |
* @var string $json The json. |
| 58 |
*/ |
| 59 |
public $json; |
| 60 |
|
| 61 |
/** |
| 62 |
* Init API |
| 63 |
* |
| 64 |
* @param array $request_settings All necessary tokens for OAuth connection. |
| 65 |
* @param string $feed_type Type of Twitter feed. |
| 66 |
*/ |
| 67 |
public function init( $request_settings, $feed_type ) { |
| 68 |
$this->consumer_key = $request_settings['consumer_key']; |
| 69 |
$this->consumer_secret = $request_settings['consumer_secret']; |
| 70 |
$this->access_token = $request_settings['access_token']; |
| 71 |
$this->access_token_secret = $request_settings['access_token_secret']; |
| 72 |
$this->feed_type = $feed_type; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Sets the complete url for our API endpoint. GET fields will be added later |
| 77 |
*/ |
| 78 |
public function set_url_base() { |
| 79 |
switch ( $this->feed_type ) { |
| 80 |
case 'hometimeline': |
| 81 |
$this->base_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'; |
| 82 |
break; |
| 83 |
case 'search': |
| 84 |
$this->base_url = 'https://api.twitter.com/1.1/search/tweets.json'; |
| 85 |
break; |
| 86 |
default: |
| 87 |
$this->base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Encodes an array of GET field data into html characters for including in a URL |
| 93 |
* |
| 94 |
* @param array $get_fields Array of GET fields that are compatible with the Twitter API. |
| 95 |
*/ |
| 96 |
public function set_get_fields( array $get_fields ) { |
| 97 |
$url_string = '?'; |
| 98 |
$length = count( $get_fields ); |
| 99 |
$j = 1; |
| 100 |
foreach ( $get_fields as $key => $value ) { |
| 101 |
$url_string .= rawurlencode( $key ) . '=' . rawurlencode( $value ); |
| 102 |
if ( $j != $length ) { |
| 103 |
$url_string .= '&'; |
| 104 |
} |
| 105 |
$j++; |
| 106 |
} |
| 107 |
|
| 108 |
$this->get_fields = $url_string; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Uses the OAuth data to build the base string needed to create the |
| 113 |
* OAuth signature to be used in the header of the request |
| 114 |
* |
| 115 |
* @param array $oauth Oauth data without the signature. |
| 116 |
*/ |
| 117 |
private function build_base_string( $oauth ) { |
| 118 |
$base_string = array(); |
| 119 |
ksort( $oauth ); |
| 120 |
|
| 121 |
// Start forming the header string by creating a numeric index array with each part of the header string it's own element in the array. |
| 122 |
foreach ( $oauth as $key => $value ) { |
| 123 |
$base_string[] = rawurlencode( $key ) . '=' . rawurlencode( $value ); |
| 124 |
} |
| 125 |
|
| 126 |
// Convert the array of values into a single encoded string and return. |
| 127 |
return 'GET&' . rawurlencode( $this->base_url ) . '&' . rawurlencode( implode( '&', $base_string ) ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Builds the OAuth data array that is used to authenticate the connection |
| 132 |
* to the Twitter API |
| 133 |
*/ |
| 134 |
public function build_oauth() { |
| 135 |
$oauth = array( |
| 136 |
'oauth_consumer_key' => $this->consumer_key, |
| 137 |
'oauth_nonce' => time(), |
| 138 |
'oauth_signature_method' => 'HMAC-SHA1', |
| 139 |
'oauth_token' => $this->access_token, |
| 140 |
'oauth_timestamp' => time(), |
| 141 |
'oauth_version' => '1.0', |
| 142 |
); |
| 143 |
|
| 144 |
$getfields = str_replace( '?', '', explode( '&', $this->get_fields ) ); |
| 145 |
|
| 146 |
// Add the get fields to the oauth associative array to be formed into the header string eventually. |
| 147 |
foreach ( $getfields as $getfield ) { |
| 148 |
$split = explode( '=', $getfield ); |
| 149 |
|
| 150 |
if ( isset( $split[1] ) ) { |
| 151 |
$oauth[ $split[0] ] = urldecode( $split[1] ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// The OAuth signature for Twitter is a hashed, encoded version of the base url, 4 different keys. |
| 156 |
$base_string = $this->build_base_string( $oauth ); |
| 157 |
$composite_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->access_token_secret ); |
| 158 |
$oauth_signature = base64_encode( hash_hmac( 'sha1', $base_string, $composite_key, true ) ); |
| 159 |
$oauth['oauth_signature'] = $oauth_signature; |
| 160 |
|
| 161 |
$this->oauth = $oauth; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Since the OAuth data is passed in a url, special characters need to be encoded |
| 166 |
*/ |
| 167 |
private function encode_header() { |
| 168 |
$header = 'Authorization: OAuth '; |
| 169 |
$values = array(); |
| 170 |
|
| 171 |
// Each element of the header needs to have it's special characters encoded for passing through a url. |
| 172 |
foreach ( $this->oauth as $key => $value ) { |
| 173 |
if ( in_array( |
| 174 |
$key, |
| 175 |
array( |
| 176 |
'oauth_consumer_key', |
| 177 |
'oauth_nonce', |
| 178 |
'oauth_signature', |
| 179 |
'oauth_signature_method', |
| 180 |
'oauth_timestamp', |
| 181 |
'oauth_token', |
| 182 |
'oauth_version', |
| 183 |
) |
| 184 |
) ) { |
| 185 |
$values[] = "$key=\"" . rawurlencode( $value ) . '"'; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$header .= implode( ', ', $values ); |
| 190 |
$this->header = $header; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Attempts to connect to the Twitter api using WP_HTTP class |
| 196 |
* |
| 197 |
* @param string $url The complete api endpoint url. |
| 198 |
*/ |
| 199 |
private function wp_http_request( $url ) { |
| 200 |
$args = array( |
| 201 |
'headers' => $this->header, |
| 202 |
'timeout' => 60, |
| 203 |
'sslverify' => false, |
| 204 |
); |
| 205 |
|
| 206 |
return wp_remote_get( $url, $args ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Uses the data created and gathered up to this point to make the actual connection |
| 211 |
* to the Twitter API. It first tests whether or not a curl connection is possible, |
| 212 |
* followed by file_get_contents connection, then defaults to the WordPress WP_HTTP object |
| 213 |
* |
| 214 |
* @return mixed|string raw json data retrieved from the API request |
| 215 |
*/ |
| 216 |
public function perform_request() { |
| 217 |
$url = $this->base_url . $this->get_fields; |
| 218 |
|
| 219 |
$this->build_oauth(); |
| 220 |
$this->encode_header(); |
| 221 |
|
| 222 |
$this->json = $this->wp_http_request( $url ); |
| 223 |
|
| 224 |
return $this; |
| 225 |
} |
| 226 |
} |
| 227 |
|