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 / V2 / TwitterHelperApi.php
custom-twitter-feeds / inc / V2 Last commit date
Adapter.php 1 month ago CtfOauthConnect.php 1 month ago MediaAdapter.php 1 month ago TweetAdapter.php 1 month ago TwitterHelperApi.php 1 month ago UserAdapter.php 1 month ago
TwitterHelperApi.php
152 lines
1 <?php
2
3 /**
4 * Class for requesting oauth2 access token.
5 *
6 * @package twitter-api-v2
7 */
8
9 namespace TwitterFeed\V2;
10
11 // Don't load directly.
12 if (! defined('ABSPATH')) {
13 die('-1');
14 }
15
16 /**
17 * TwitterHelperApi class.
18 */
19 class TwitterHelperApi {
20
21 /**
22 * Access token transient key.
23 *
24 * @const string
25 */
26 const ACCESS_TOKEN_TRANSIENT_KEY = 'sb_twitter_oauth_bearer_access_token_cache';
27
28 /**
29 * Consumer key.
30 *
31 * @var string
32 */
33 private $consumer_key;
34
35 /**
36 * Consumer secret.
37 * @var string
38 */
39 private $consumer_secret;
40
41 /**
42 * Constructor.
43 *
44 * @param string $consumer_key Consumer key.
45 * @param string $consumer_secret Consumer secret.
46 */
47 public function __construct($consumer_key, $consumer_secret) {
48 $this->consumer_key = $consumer_key;
49 $this->consumer_secret = $consumer_secret;
50 }
51
52 /**
53 * Get access token using consumer key and consumer secret.
54 *
55 * @param string $consumer_key Consumer Key.
56 * @param string $consumer_secret Consumer Secret.
57 *
58 * @return false|string
59 */
60 public function getAccessToken($consumer_key, $consumer_secret)
61 {
62 $url = 'https://api.twitter.com/oauth2/token';
63
64 $args = [
65 'headers' => [
66 'Authorization' => 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret),
67 ],
68 'body' => [
69 'grant_type' => 'client_credentials',
70 ],
71 ];
72
73 $response = wp_remote_post($url, $args);
74
75 if (is_wp_error($response)) {
76 return false;
77 }
78
79 $remote_post_response_body = wp_remote_retrieve_body($response);
80 $array_response = json_decode($remote_post_response_body, ARRAY_A);
81
82 if (empty($array_response['access_token'])) {
83 return false;
84 }
85
86 return $array_response['access_token'];
87 }
88
89 /**
90 * Check if cached twitter access token is valid .
91 *
92 * @param string $twitter_oauth_access_token_cache Access token cache.
93 * @param string $current_hash Current hash.
94 *
95 * @return bool
96 */
97 public function isCachedTwitterAccessTokenValid($twitter_oauth_access_token_cache, $current_hash)
98 {
99 // If nothing is saved into the cache, invalidate cache.
100 if (false === $twitter_oauth_access_token_cache) {
101 return false;
102 }
103
104 // If the reference hash is missing, invalidate cache.
105 if (empty($twitter_oauth_access_token_cache['reference_hash'])) {
106 return false;
107 }
108
109 // If the consumer key and consumer secret got changed from the settings, invalidate cache.
110 if ($twitter_oauth_access_token_cache['reference_hash'] !== $current_hash) {
111 return false;
112 }
113
114 // If the access token is empty, invalidate cache.
115 if (empty($twitter_oauth_access_token_cache['access_token'])) {
116 return false;
117 }
118
119 return true;
120 }
121
122 /**
123 * Get cached twitter access token.
124 *
125 * @return null|string
126 */
127 public function getCachedTwitterAccessToken()
128 {
129 $twitter_oauth_access_token_cache = get_transient(static::ACCESS_TOKEN_TRANSIENT_KEY);
130 $current_hash = md5($this->consumer_key . ':' . $this->consumer_secret);
131
132 if (!$this->isCachedTwitterAccessTokenValid($twitter_oauth_access_token_cache, $current_hash)) {
133 $twitter_oauth_access_token = $this->getAccessToken($this->consumer_key, $this->consumer_secret);
134
135 if ($twitter_oauth_access_token) {
136 set_transient(
137 'sb_twitter_oauth_bearer_access_token_cache',
138 [
139 'access_token' => $twitter_oauth_access_token,
140 'reference_hash' => $current_hash,
141 ],
142 MONTH_IN_SECONDS
143 );
144 }
145
146 return $twitter_oauth_access_token;
147 }
148
149 return $twitter_oauth_access_token_cache['access_token'];
150 }
151 }
152