← All changes
|
jetpack_vendor/automattic/jetpack-connection/src/class-tokens.php
+28
-3
16.2
→
16.3
View file →
| @@ -99,8 +99,10 @@ | ||
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * Perform the API request to validate only the blog. |
| 102 | 102 | * |
| 103 | + * @since 9.8.0 Returns a WP_Error, not false, when the request fails. | |
| 104 | + * | |
| 103 | 105 | * @return bool|WP_Error Boolean with the test result. WP_Error if test cannot be performed. |
| 104 | 106 | */ |
| 105 | 107 | public function validate_blog_token() { |
| 106 | 108 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| @@ -106,8 +108,14 @@ | ||
| 106 | 108 | $blog_id = Jetpack_Options::get_option( 'id' ); |
| 107 | 109 | if ( ! $blog_id ) { |
| 108 | 110 | return new WP_Error( 'site_not_registered', 'Site not registered.' ); |
| 109 | 111 | } |
| 112 | + | |
| 113 | + // A missing blog token is broken, not unverifiable: the signed request would fail before it is sent. | |
| 114 | + if ( ! $this->get_access_token() ) { | |
| 115 | + return false; | |
| 116 | + } | |
| 117 | + | |
| 110 | 118 | $url = sprintf( |
| 111 | 119 | '%s/%s/v%s/%s', |
| 112 | 120 | Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ), |
| 113 | 121 | 'wpcom', |
| @@ -117,12 +125,16 @@ | ||
| 117 | 125 | |
| 118 | 126 | $method = 'GET'; |
| 119 | 127 | $response = Client::remote_request( compact( 'url', 'method' ) ); |
| 120 | 128 | |
| 121 | - if ( is_wp_error( $response ) || ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { | |
| 122 | - return false; | |
| 129 | + if ( is_wp_error( $response ) ) { | |
| 130 | + return $response; | |
| 123 | 131 | } |
| 124 | 132 | |
| 133 | + if ( ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { | |
| 134 | + return new WP_Error( 'blog_token_check_failed', 'The blog token health check could not be performed.' ); | |
| 135 | + } | |
| 136 | + | |
| 125 | 137 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 126 | 138 | |
| 127 | 139 | return is_array( $body ) && isset( $body['is_healthy'] ) && true === $body['is_healthy']; |
| 128 | 140 | } |
| @@ -293,9 +305,22 @@ | ||
| 293 | 305 | $options = compact( 'user_tokens', 'master_user' ); |
| 294 | 306 | } else { |
| 295 | 307 | $options = compact( 'user_tokens' ); |
| 296 | 308 | } |
| 297 | - return Jetpack_Options::update_options( $options ); | |
| 309 | + $updated = Jetpack_Options::update_options( $options ); | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Fires when the user token gets replaced. | |
| 313 | + * | |
| 314 | + * @since 1.29.0 | |
| 315 | + * @since 9.8.1 Fired from Tokens::update_user_token() so every write path (authorize, provisioning, CLI) clears stale connection errors, not just the REST endpoint. | |
| 316 | + * | |
| 317 | + * @param int $user_id User ID. | |
| 318 | + * @param string $token New user token. | |
| 319 | + */ | |
| 320 | + do_action( 'jetpack_updated_user_token', $user_id, $token ); | |
| 321 | + | |
| 322 | + return $updated; | |
| 298 | 323 | } |
| 299 | 324 | |
| 300 | 325 | /** |
| 301 | 326 | * Sign a user role with the master access token. |