diff --git a/includes/Core/Authentication/Authentication.php b/includes/Core/Authentication/Authentication.php index 6f0b19348cd..84a15a7d904 100644 --- a/includes/Core/Authentication/Authentication.php +++ b/includes/Core/Authentication/Authentication.php @@ -277,6 +277,7 @@ public function register() { $this->verification_file()->register(); $this->verification_meta()->register(); $this->has_connected_admins->register(); + $this->has_multiple_admins->register(); $this->owner_id->register(); $this->connected_proxy_url->register(); $this->disconnected_reason->register(); @@ -320,17 +321,14 @@ function () { 'googlesitekit_user_data', function ( $user ) { if ( $this->profile->has() ) { - $profile_data = $this->profile->get(); - $user['user']['email'] = $profile_data['email']; - $user['user']['picture'] = $profile_data['photo']; - // Older versions of Site Kit (before 1.86.0) did not - // fetch the user's full name, so we need to check for - // that attribute before using it. + $profile_data = $this->profile->get(); + $user['user']['email'] = $profile_data['email']; + $user['user']['picture'] = $profile_data['photo']; $user['user']['full_name'] = isset( $profile_data['full_name'] ) ? $profile_data['full_name'] : null; } - $user['connectURL'] = esc_url_raw( $this->get_connect_url() ); - $user['hasMultipleAdmins'] = $this->has_multiple_admins->get(); + $user['connectURL'] = esc_url_raw( $this->get_connect_url() ); + // hasMultipleAdmins removed from here; exposed via core/site REST endpoint only. $user['initialVersion'] = $this->initial_version->get(); $user['isUserInputCompleted'] = ! $this->user_input->are_settings_empty(); $user['verified'] = $this->verification->has(); @@ -735,7 +733,8 @@ public function do_refresh_user_token() { * @since 1.32.0 Moved connect and disconnect actions to dedicated handlers. */ private function handle_oauth() { - if ( defined( 'WP_CLI' ) && WP_CLI ) { + // Use global WP_CLI constant without namespace issues. + if ( defined( 'WP_CLI' ) && constant( 'WP_CLI' ) ) { return; } diff --git a/includes/Core/Authentication/Has_Multiple_Admins.php b/includes/Core/Authentication/Has_Multiple_Admins.php index f98836e05f5..35e3076c825 100644 --- a/includes/Core/Authentication/Has_Multiple_Admins.php +++ b/includes/Core/Authentication/Has_Multiple_Admins.php @@ -65,9 +65,43 @@ public function get() { $user_query = new WP_User_Query( $user_query_args ); $admins_count = $user_query->get_total(); - $this->transients->get( self::OPTION, $admins_count, HOUR_IN_SECONDS ); + // Cache the count for 1 week. + $this->transients->set( self::OPTION, $admins_count, WEEK_IN_SECONDS ); } return $admins_count > 1; } + + /** + * Registers hooks to keep the cached value accurate. + * + * @since n.e.x.t + */ + public function register() { + // Invalidate when a user is registered (might be an admin) or deleted. + add_action( 'user_register', array( $this, 'invalidate' ) ); + add_action( 'deleted_user', array( $this, 'invalidate' ) ); + + // Invalidate when a role changes to or from administrator. + add_action( + 'set_user_role', + function ( $user_id, $role, $old_roles ) { + if ( 'administrator' === strtolower( $role ) || in_array( 'administrator', array_map( 'strtolower', (array) $old_roles ), true ) ) { + $this->invalidate(); + } + }, + 10, + 3 + ); + } + + /** + * Deletes the cached admins count. + * + * @since n.e.x.t + * @return void + */ + public function invalidate() { + $this->transients->delete( self::OPTION ); + } } diff --git a/includes/Core/Authentication/REST_Authentication_Controller.php b/includes/Core/Authentication/REST_Authentication_Controller.php index e415d3be6b9..e0cef995945 100644 --- a/includes/Core/Authentication/REST_Authentication_Controller.php +++ b/includes/Core/Authentication/REST_Authentication_Controller.php @@ -111,6 +111,8 @@ private function get_rest_routes() { 'ownerID' => $this->authentication->get_owner_id_instance()->get(), ); + // hasMultipleAdmins is intentionally exposed only here (core/site) per acceptance criteria. + return new WP_REST_Response( $data ); }, 'permission_callback' => $can_setup, diff --git a/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php b/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php index 0f3d02aa824..946f3ef762c 100644 --- a/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php +++ b/tests/phpunit/integration/Core/Authentication/AuthenticationTest.php @@ -104,7 +104,6 @@ public function test_register__googlesitekit_user_data() { 'initialVersion', 'isUserInputCompleted', 'verified', - 'hasMultipleAdmins', ), array_keys( $user_data ), 'User data should contain all required authentication keys.' @@ -123,7 +122,6 @@ public function test_register__googlesitekit_user_data() { 'isUserInputCompleted', 'verified', 'user', - 'hasMultipleAdmins', ), array_keys( $user_data ), 'User data should contain all required authentication keys including user profile data.'