diff --git a/src/wp-admin/includes/class-wp-users-list-table.php b/src/wp-admin/includes/class-wp-users-list-table.php index f557c2b459..7637896880 100644 --- a/src/wp-admin/includes/class-wp-users-list-table.php +++ b/src/wp-admin/includes/class-wp-users-list-table.php @@ -98,7 +98,7 @@ class WP_Users_List_Table extends WP_List_Table { $args = array( 'number' => $users_per_page, 'offset' => ( $paged-1 ) * $users_per_page, - 'include' => wp_get_users_with_no_role(), + 'include' => wp_get_users_with_no_role( $this->site_id ), 'search' => $usersearch, 'fields' => 'all_with_meta' ); @@ -177,7 +177,7 @@ class WP_Users_List_Table extends WP_List_Table { if ( $this->is_site_users ) { $url = 'site-users.php?id=' . $this->site_id; switch_to_blog( $this->site_id ); - $users_of_blog = count_users(); + $users_of_blog = count_users( 'time', $this->site_id ); restore_current_blog(); } else { $url = 'users.php'; @@ -369,9 +369,6 @@ class WP_Users_List_Table extends WP_List_Table { $post_counts = count_many_users_posts( array_keys( $this->items ) ); foreach ( $this->items as $userid => $user_object ) { - if ( is_multisite() && empty( $user_object->allcaps ) ) - continue; - echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 ); } } diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 5615650692..a0e699c2c1 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -838,18 +838,23 @@ function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') { * * @since 3.0.0 * @since 4.4.0 The number of users with no role is now included in the `none` element. + * @since 4.9.0 The `$site_id` parameter was added to support multisite. * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $strategy 'time' or 'memory' + * @param string $strategy Optional. The computational strategy to use when counting the users. + * Accepts either 'time' or 'memory'. Default 'time'. + * @param int|null $site_id Optional. The site ID to count users for. Defaults to the current site. * @return array Includes a grand total and an array of counts indexed by role strings. */ -function count_users($strategy = 'time') { +function count_users( $strategy = 'time', $site_id = null ) { global $wpdb; // Initialize - $id = get_current_blog_id(); - $blog_prefix = $wpdb->get_blog_prefix($id); + if ( ! $site_id ) { + $site_id = get_current_blog_id(); + } + $blog_prefix = $wpdb->get_blog_prefix( $site_id ); $result = array(); if ( 'time' == $strategy ) { @@ -920,10 +925,6 @@ function count_users($strategy = 'time') { $result['avail_roles'] =& $avail_roles; } - if ( is_multisite() ) { - $result['avail_roles']['none'] = 0; - } - return $result; } @@ -2483,20 +2484,20 @@ function wp_destroy_all_sessions() { /** * Get the user IDs of all users with no role on this site. * - * This function returns an empty array when used on Multisite. - * * @since 4.4.0 + * @since 4.9.0 The `$site_id` parameter was added to support multisite. * + * @param int|null $site_id Optional. The site ID to get users with no role for. Defaults to the current site. * @return array Array of user IDs. */ -function wp_get_users_with_no_role() { +function wp_get_users_with_no_role( $site_id = null ) { global $wpdb; - if ( is_multisite() ) { - return array(); + if ( ! $site_id ) { + $site_id = get_current_blog_id(); } - $prefix = $wpdb->get_blog_prefix(); + $prefix = $wpdb->get_blog_prefix( $site_id ); $regex = implode( '|', array_keys( wp_roles()->get_names() ) ); $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex ); $users = $wpdb->get_col( $wpdb->prepare( " diff --git a/tests/phpunit/tests/user/countUsers.php b/tests/phpunit/tests/user/countUsers.php index f5fa6b9356..115b4baa73 100644 --- a/tests/phpunit/tests/user/countUsers.php +++ b/tests/phpunit/tests/user/countUsers.php @@ -52,6 +52,7 @@ class Tests_User_CountUsers extends WP_UnitTestCase { /** * @ticket 22993 + * @ticket 36196 * @group multisite * @group ms-required * @@ -103,7 +104,7 @@ class Tests_User_CountUsers extends WP_UnitTestCase { 'author' => 1, 'contributor' => 1, 'subscriber' => 1, - 'none' => 0, + 'none' => 2, ), $count['avail_roles'] ); // Test users counts on blog 1 diff --git a/tests/phpunit/tests/user/wpGetUsersWithNoRole.php b/tests/phpunit/tests/user/wpGetUsersWithNoRole.php index 82c0da73bf..10ecf2a565 100644 --- a/tests/phpunit/tests/user/wpGetUsersWithNoRole.php +++ b/tests/phpunit/tests/user/wpGetUsersWithNoRole.php @@ -36,6 +36,7 @@ class Tests_User_GetUsersWithNoRole extends WP_UnitTestCase { /** * @ticket 22993 + * @ticket 36196 * @group multisite * @group ms-required */ @@ -56,21 +57,27 @@ class Tests_User_GetUsersWithNoRole extends WP_UnitTestCase { 'user_id' => $editor, ) ); - // Add users to blogs + // Add editor to blog 1 add_user_to_blog( $blog_1, $editor, 'editor' ); // Test users on root site $users = wp_get_users_with_no_role(); - $this->assertSame( array(), $users ); + $this->assertSame( array( + "{$nobody}", + ), $users ); // Test users counts on blog 1 - switch_to_blog( $blog_1 ); - $users = wp_get_users_with_no_role(); - restore_current_blog(); - - // Test users on root site + $users = wp_get_users_with_no_role( $blog_1 ); $this->assertSame( array(), $users ); + // Add admin to blog 1 with no role + add_user_to_blog( $blog_1, $admin, '' ); + + // Re-test users counts on blog 1 + $users = wp_get_users_with_no_role( $blog_1 ); + $this->assertSame( array( + "{$admin}", + ), $users ); } /**