diff --git a/projects/packages/connection/changelog/add-test_blog_token_health b/projects/packages/connection/changelog/add-test_blog_token_health new file mode 100644 index 000000000000..17ebed61f344 --- /dev/null +++ b/projects/packages/connection/changelog/add-test_blog_token_health @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add new test for blog token health to support user-less sites diff --git a/projects/packages/connection/src/class-tokens.php b/projects/packages/connection/src/class-tokens.php index 730318ecd971..41a8f06364e4 100644 --- a/projects/packages/connection/src/class-tokens.php +++ b/projects/packages/connection/src/class-tokens.php @@ -70,6 +70,36 @@ public function validate( $user_id = null ) { return $body ? $body : false; } + /** + * Perform the API request to validate only the blog. + * + * @return bool|WP_Error Boolean with the test result. WP_Error if test cannot be performed. + */ + public function validate_blog_token() { + $blog_id = Jetpack_Options::get_option( 'id' ); + if ( ! $blog_id ) { + return new WP_Error( 'site_not_registered', 'Site not registered.' ); + } + $url = sprintf( + '%s/%s/v%s/%s', + Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ), + 'wpcom', + '2', + 'sites/' . $blog_id . '/jetpack-token-health/blog' + ); + + $method = 'GET'; + $response = Client::remote_request( compact( 'url', 'method' ) ); + + if ( is_wp_error( $response ) || ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { + return false; + } + + $body = json_decode( wp_remote_retrieve_body( $response ), true ); + + return is_array( $body ) && isset( $body['is_healthy'] ) && true === $body['is_healthy']; + } + /** * Obtains the auth token. * diff --git a/projects/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php b/projects/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php index 26b1cc151a34..30ecc893fcaa 100644 --- a/projects/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php +++ b/projects/plugins/jetpack/_inc/lib/debugger/class-jetpack-cxn-tests.php @@ -471,17 +471,70 @@ protected function test__identity_crisis() { } /** - * Tests blog and current user's token against wp.com's check-token-health endpoint. + * Tests the health of the Connection tokens. + * + * This will always check the blog token health. It will also check the user token health if + * a user is logged in and connected, or if there's a connected owner. * * @since 9.0.0 + * @since 9.6.0 Checks only blog token if current user not connected or site does not have a connected owner. * * @return array Test results. */ protected function test__connection_token_health() { - $name = __FUNCTION__; + $name = __FUNCTION__; + $m = new Connection_Manager(); + $user_id = get_current_user_id(); + + // Check if there's a connected logged in user. + if ( $user_id && ! $m->is_user_connected( $user_id ) ) { + $user_id = false; + } + + // If no logged in user to check, let's see if there's a master_user set. + if ( ! $user_id ) { + $user_id = Jetpack_Options::get_option( 'master_user' ); + if ( $user_id && ! $m->is_user_connected( $user_id ) ) { + return self::connection_failing_test( $name, __( 'Missing token for the connection owner.', 'jetpack' ) ); + } + } - $m = new Connection_Manager(); - $user_id = get_current_user_id() ? get_current_user_id() : $m->get_connection_owner_id(); + if ( $user_id ) { + return $this->check_tokens_health( $user_id ); + } else { + return $this->check_blog_token_health(); + } + } + + /** + * Tests blog and user's token against wp.com's check-token-health endpoint. + * + * @since 9.6.0 + * + * @return array Test results. + */ + protected function check_blog_token_health() { + $name = 'test__connection_token_health'; + $valid = ( new Tokens() )->validate_blog_token(); + + if ( ! $valid ) { + return self::connection_failing_test( $name, __( 'Blog token validation failed.', 'jetpack' ) ); + } else { + return self::passing_test( array( 'name' => $name ) ); + } + } + + /** + * Tests blog token against wp.com's check-token-health endpoint. + * + * @since 9.6.0 + * + * @param int $user_id The user ID to check the tokens for. + * + * @return array Test results. + */ + protected function check_tokens_health( $user_id ) { + $name = 'test__connection_token_health'; $validated_tokens = ( new Tokens() )->validate( $user_id ); if ( ! is_array( $validated_tokens ) || count( array_diff_key( array_flip( array( 'blog_token', 'user_token' ) ), $validated_tokens ) ) ) { diff --git a/projects/plugins/jetpack/changelog/add-test_blog_token_health b/projects/plugins/jetpack/changelog/add-test_blog_token_health new file mode 100644 index 000000000000..4260edd04317 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-test_blog_token_health @@ -0,0 +1,4 @@ +Significance: minor +Type: compat + +Add new test for blog token health to support user-less sites