PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 All 128 releases
wordpress-seo / src / values / oauth / oauth-token.php

oauth-token.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/values/oauth/oauth-token.php

140 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Values\OAuth;
4
5 use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception;
6 use YoastSEO_Vendor\League\OAuth2\Client\Token\AccessTokenInterface;
7
8 /**
9 * Class OAuth_Token
10 */
11 class OAuth_Token {
12
13 /**
14 * The access token.
15 *
16 * @var string
17 */
18 public $access_token;
19
20 /**
21 * The refresh token.
22 *
23 * @var string
24 */
25 public $refresh_token;
26
27 /**
28 * The expiration date.
29 *
30 * @var int
31 */
32 public $expires;
33
34 /**
35 * Whether or not the token has expired.
36 *
37 * @var bool
38 */
39 public $has_expired;
40
41 /**
42 * The timestamp at which the token was created.
43 *
44 * @var int
45 */
46 public $created_at;
47
48 /**
49 * The number of times we've gotten an error trying to refresh this token.
50 *
51 * @var int
52 */
53 public $error_count;
54
55 /**
56 * OAuth_Token constructor.
57 *
58 * @param string $access_token The access token.
59 * @param string $refresh_token The refresh token.
60 * @param int $expires The date and time at which the token will expire.
61 * @param bool $has_expired Whether or not the token has expired.
62 * @param int $created_at The timestamp of when the token was created.
63 * @param int $error_count The number of times we've gotten an error trying to refresh this token.
64 *
65 * @throws Empty_Property_Exception Exception thrown if a token property is empty.
66 */
67 public function __construct( $access_token, $refresh_token, $expires, $has_expired, $created_at, $error_count = 0 ) {
68
69 if ( empty( $access_token ) ) {
70 throw new Empty_Property_Exception( 'access_token' );
71 }
72
73 $this->access_token = $access_token;
74
75 if ( empty( $refresh_token ) ) {
76 throw new Empty_Property_Exception( 'refresh_token' );
77 }
78
79 $this->refresh_token = $refresh_token;
80
81 if ( empty( $expires ) ) {
82 throw new Empty_Property_Exception( 'expires' );
83 }
84
85 $this->expires = $expires;
86
87 if ( $has_expired === null ) {
88 throw new Empty_Property_Exception( 'has_expired' );
89 }
90
91 $this->has_expired = $has_expired;
92 $this->created_at = $created_at;
93 $this->error_count = $error_count;
94 }
95
96 /**
97 * Creates a new instance based on the passed response.
98 *
99 * @param AccessTokenInterface $response The response object to create a new instance from.
100 *
101 * @return OAuth_Token The token object.
102 *
103 * @throws Empty_Property_Exception Exception thrown if a token property is empty.
104 */
105 public static function from_response( AccessTokenInterface $response ) {
106 return new self(
107 $response->getToken(),
108 $response->getRefreshToken(),
109 $response->getExpires(),
110 $response->hasExpired(),
111 \time(),
112 );
113 }
114
115 /**
116 * Determines whether or not the token has expired.
117 *
118 * @return bool Whether or not the token has expired.
119 */
120 public function has_expired() {
121 return ( \time() >= $this->expires ) || $this->has_expired === true;
122 }
123
124 /**
125 * Converts the object to an array.
126 *
127 * @return array The converted object.
128 */
129 public function to_array() {
130 return [
131 'access_token' => $this->access_token,
132 'refresh_token' => $this->refresh_token,
133 'expires' => $this->expires,
134 'has_expired' => $this->has_expired(),
135 'created_at' => $this->created_at,
136 'error_count' => $this->error_count,
137 ];
138 }
139 }
140