diff --git a/includes/Core/Authentication/Has_Multiple_Admins.php b/includes/Core/Authentication/Has_Multiple_Admins.php index dd70c8b0085..62f390a1ce5 100644 --- a/includes/Core/Authentication/Has_Multiple_Admins.php +++ b/includes/Core/Authentication/Has_Multiple_Admins.php @@ -29,7 +29,7 @@ class Has_Multiple_Admins { /** * The option_name for this transient. */ - const OPTION = 'googlesitekit_has_multiple_admins'; + const OPTION = 'googlesitekit_has_multiple_administrators'; /** * Transients instance. @@ -70,8 +70,42 @@ public function register() { * @return boolean TRUE if the site kit has multiple admins, otherwise FALSE. */ public function get() { - $admins_count = $this->transients->get( self::OPTION ); - if ( false === $admins_count ) { + $has_multiple_admins = $this->transients->get( self::OPTION ); + + if ( false !== $has_multiple_admins ) { + return 1 === $has_multiple_admins; + } + + if ( is_multisite() ) { + $super_admins = get_super_admins(); + // If there are multiple super admins, we definitely have multiple admins. + if ( count( $super_admins ) > 1 ) { + $admins_count = count( $super_admins ); + // There's no need to check local admins in this case. + } else { + // If there is 0 or 1 super admin, we need to check local admins. + // We exclude the super admin from the local check to avoid double counting + // if they are also added as a local administrator. + $exclude_users = array(); + if ( ! empty( $super_admins ) ) { + $super_admin = get_user_by( 'login', $super_admins[0] ); + if ( $super_admin ) { + $exclude_users[] = $super_admin->ID; + } + } + + $user_query_args = array( + 'number' => 1, + 'role__in' => array( 'Administrator' ), + 'count_total' => true, + 'exclude' => $exclude_users, + ); + + $user_query = new WP_User_Query( $user_query_args ); + // Add 1 if there is a super admin, plus any other local admins found. + $admins_count = ( ! empty( $super_admins ) ? 1 : 0 ) + $user_query->get_total(); + } + } else { $user_query_args = array( 'number' => 1, 'role__in' => array( 'Administrator' ), @@ -80,11 +114,13 @@ public function get() { $user_query = new WP_User_Query( $user_query_args ); $admins_count = $user_query->get_total(); - - $this->transients->set( self::OPTION, $admins_count, WEEK_IN_SECONDS ); } - return $admins_count > 1; + $has_multiple_admins = $admins_count > 1 ? 1 : 0; + + $this->transients->set( self::OPTION, $has_multiple_admins, WEEK_IN_SECONDS ); + + return $has_multiple_admins; } /**