Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add new test for blog token health to support user-less sites
30 changes: 30 additions & 0 deletions projects/packages/connection/src/class-tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) ) {
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-test_blog_token_health
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: compat

Add new test for blog token health to support user-less sites