PluginProbe ʕ •ᴥ•ʔ
Custom Twitter Feeds – A Tweets Widget or X Feed Widget / 2.7.0
Custom Twitter Feeds – A Tweets Widget or X Feed Widget v2.7.0
2.8.0 2.7.0 2.6.1 2.6.0 1.0 1.0.1 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.4 1.4.1 1.5 1.5.1 1.6 1.6.1 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.1.2 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.5.4 2.5.5 trunk
custom-twitter-feeds / inc / CtfOauthConnect.php
custom-twitter-feeds / inc Last commit date
Admin 1 month ago Builder 1 month ago Integrations 1 month ago SmashTwitter 1 month ago UsageTracking 1 month ago V2 1 month ago blocks 1 month ago CTF_Display_Elements.php 1 month ago CTF_Feed.php 1 month ago CTF_Feed_Locator.php 1 month ago CTF_GDPR_Integrations.php 1 month ago CTF_Parse.php 1 month ago CTF_Settings.php 1 month ago CtfAdmin.php 1 month ago CtfCache.php 1 month ago CtfFeed.php 1 month ago CtfOauthConnect.php 1 month ago SB_Twitter_Cron_Updater.php 1 month ago admin-hooks.php 1 month ago ctf-functions.php 1 month ago notices.php 1 month ago widget.php 1 month ago
CtfOauthConnect.php
269 lines
1 <?php
2 /**
3 * Class OauthConnect
4 *
5 * Simple, lightweight class to make a connection to the Twitter API
6 * Supports home timeline, user timeline, and search endpoints
7 */
8 namespace TwitterFeed;
9
10 // Don't load directly
11 if ( ! defined( 'ABSPATH' ) ) {
12 die( '-1' );
13 }
14
15 class CtfOauthConnect
16 {
17 /**
18 * @var string
19 */
20 protected $base_url;
21
22 /**
23 * @var string
24 */
25 protected $get_fields;
26
27 /**
28 * @var string
29 */
30 protected $request_method;
31
32 /**
33 * @var array
34 */
35 protected $oauth;
36
37 /**
38 * @var string
39 */
40 protected $header;
41
42 /**
43 * @var bool
44 */
45 public $api_error_no = false;
46
47 /**
48 * @var bool
49 */
50 public $api_error_message = false;
51
52 /**
53 * @var string
54 */
55 public $json;
56
57 /**
58 * @var string
59 */
60 protected $consumer_key;
61
62 /**
63 * @var string
64 */
65 protected $consumer_secret;
66
67 /**
68 * @var string
69 */
70 protected $access_token;
71
72 /**
73 * @var string
74 */
75 protected $access_token_secret;
76
77 /**
78 * @var string
79 */
80 protected $feed_type;
81
82 /**
83 * @param array $request_settings all necessary tokens for OAuth connection
84 * @param $feed_type string type of Twitter feed
85 */
86 public function __construct( array $request_settings, $feed_type )
87 {
88 $this->consumer_key = $request_settings['consumer_key'];
89 $this->consumer_secret = $request_settings['consumer_secret'];
90 $this->access_token = $request_settings['access_token'];
91 $this->access_token_secret = $request_settings['access_token_secret'];
92 $this->feed_type = $feed_type;
93 }
94
95 /**
96 * Sets the complete url for our API endpoint. GET fields will be added later
97 */
98 public function setUrlBase()
99 {
100 switch ( $this->feed_type ) {
101 case "hometimeline":
102 $this->base_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
103 break;
104 case "search":
105 $this->base_url = 'https://api.twitter.com/1.1/search/tweets.json';
106 break;
107 case "accountlookup":
108 $this->base_url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
109 break;
110 case "userslookup":
111 $this->base_url = 'https://api.twitter.com/1.1/users/lookup.json';
112 break;
113 default:
114 $this->base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
115 }
116 }
117
118 /**
119 * Encodes an array of GET field data into html characters for including in a URL
120 *
121 * @param array $get_fields array of GET fields that are compatible with the Twitter API
122 */
123 public function setGetFields( array $get_fields )
124 {
125 $url_string = '?';
126 $length = count( $get_fields );
127 $j = 1;
128 foreach ( $get_fields as $key => $value ) {
129 $url_string .= rawurlencode( $key ) . '=' . rawurlencode( $value );
130 if ( $j != $length ) {
131 $url_string .= '&';
132 }
133 $j++;
134 }
135
136 $this->get_fields = $url_string;
137 }
138
139 /**
140 * Users can manually set the request method if there is an uncatchable error in
141 * the other methods
142 *
143 * @param string $request_method
144 */
145 public function setRequestMethod( $request_method = 'auto' )
146 {
147 $this->request_method = $request_method;
148 }
149
150 /**
151 * Uses the OAuth data to build the base string needed to create the
152 * OAuth signature to be used in the header of the request
153 *
154 * @param $oauth array oauth data without the signature
155 * @return string the base string for needed to construct the oauth signature
156 */
157 private function buildBaseString( $oauth )
158 {
159 $base_string = array();
160 ksort( $oauth );
161
162 // start forming the header string by creating a numeric index array with
163 // each part of the header string it's own element in the array
164 foreach ( $oauth as $key => $value ) {
165 $base_string[] = rawurlencode( $key ) . '=' . rawurlencode( $value );
166 }
167
168 // convert the array of values into a single encoded string and return
169 return 'GET&' . rawurlencode( $this->base_url ) . '&' . rawurlencode( implode( '&', $base_string ) );
170 }
171
172 /**
173 * Builds the OAuth data array that is used to authenticate the connection
174 * to the Twitter API
175 */
176 public function buildOauth()
177 {
178 $oauth = array(
179 'oauth_consumer_key' => $this->consumer_key,
180 'oauth_nonce' => time(),
181 'oauth_signature_method' => 'HMAC-SHA1',
182 'oauth_token' => $this->access_token,
183 'oauth_timestamp' => time(),
184 'oauth_version' => '1.0'
185 );
186
187 $getfields = str_replace( '?', '', explode( '&', $this->get_fields ? $this->get_fields : '' ) );
188
189 // add the get fields to the oauth associative array to be
190 // formed into the header string eventually
191 foreach ( $getfields as $getfield ) {
192 $split = explode( '=', $getfield );
193
194 if ( isset( $split[1] ) ) {
195 $oauth[$split[0]] = urldecode( $split[1] );
196 }
197 }
198
199 // the OAuth signature for Twitter is a hashed, encoded version of the base url, 4 different keys
200 $base_string = $this->buildBaseString( $oauth );
201 $composite_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->access_token_secret );
202 $oauth_signature = base64_encode( hash_hmac( 'sha1', $base_string, $composite_key, true ) );
203 $oauth['oauth_signature'] = $oauth_signature;
204
205 $this->oauth = $oauth;
206 }
207
208 /**
209 * Since the OAuth data is passed in a url, special characters need to be encoded
210 */
211 private function encodeHeader()
212 {
213 $header = 'Authorization: OAuth ';
214 $values = array();
215
216 // each element of the header needs to have it's special characters encoded for
217 // passing through a url
218 foreach ( $this->oauth as $key => $value ) {
219 if ( in_array( $key, array( 'oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
220 'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version' ) ) ){
221 $values[] = "$key=\"" . rawurlencode( $value ) . "\"";
222 }
223 }
224
225 $header .= implode( ', ', $values );
226 $this->header = $header;
227 }
228
229 /**
230 * Attempts to connect to the Twitter api using WP_HTTP class
231 *
232 * @param $url string the complete api endpoint url
233 * @return mixed json string retrieved in the request
234 */
235 protected function wpHttpRequest( $url )
236 {
237 $args = array(
238 'headers' => $this->header,
239 'timeout' => 60,
240 );
241 $result = wp_remote_get( $url, $args );
242
243 if ( ! is_wp_error( $result ) ) {
244 return $result['body']; // just need the body to keep everything simple
245 } else {
246 return '{}';
247 }
248
249 }
250
251 /**
252 * Uses the data created and gathered up to this point to make the actual connection
253 * to the Twitter API. It first tests whether or not a curl connection is possible,
254 * followed by file_get_contents connection, then defaults to the WordPress WP_HTTP object
255 *
256 * @return mixed|string raw json data retrieved from the API request
257 */
258 public function performRequest()
259 {
260 $url = $this->base_url . $this->get_fields;
261 $this->buildOauth();
262 $this->encodeHeader();
263
264 $this->json = $this->wpHttpRequest( $url );
265
266 return $this;
267 }
268 }
269