Skip to content

Commit 44491ba

Browse files
committed
Users: Ensure that users with no role on a site are taken into consideration when listing users on Multisite.
This ensures that users who are a member of a site but have no role are correctly listed on the Users screen and can be filtered from the 'None' role filter. Props tobi823, flixos90, scottlee Fixes #36196 git-svn-id: https://develop.svn.wordpress.org/trunk@41138 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 06160eb commit 44491ba

4 files changed

Lines changed: 33 additions & 27 deletions

File tree

src/wp-admin/includes/class-wp-users-list-table.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function prepare_items() {
9898
$args = array(
9999
'number' => $users_per_page,
100100
'offset' => ( $paged-1 ) * $users_per_page,
101-
'include' => wp_get_users_with_no_role(),
101+
'include' => wp_get_users_with_no_role( $this->site_id ),
102102
'search' => $usersearch,
103103
'fields' => 'all_with_meta'
104104
);
@@ -177,7 +177,7 @@ protected function get_views() {
177177
if ( $this->is_site_users ) {
178178
$url = 'site-users.php?id=' . $this->site_id;
179179
switch_to_blog( $this->site_id );
180-
$users_of_blog = count_users();
180+
$users_of_blog = count_users( 'time', $this->site_id );
181181
restore_current_blog();
182182
} else {
183183
$url = 'users.php';
@@ -369,9 +369,6 @@ public function display_rows() {
369369
$post_counts = count_many_users_posts( array_keys( $this->items ) );
370370

371371
foreach ( $this->items as $userid => $user_object ) {
372-
if ( is_multisite() && empty( $user_object->allcaps ) )
373-
continue;
374-
375372
echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
376373
}
377374
}

src/wp-includes/user.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -838,18 +838,23 @@ function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
838838
*
839839
* @since 3.0.0
840840
* @since 4.4.0 The number of users with no role is now included in the `none` element.
841+
* @since 4.9.0 The `$site_id` parameter was added to support multisite.
841842
*
842843
* @global wpdb $wpdb WordPress database abstraction object.
843844
*
844-
* @param string $strategy 'time' or 'memory'
845+
* @param string $strategy Optional. The computational strategy to use when counting the users.
846+
* Accepts either 'time' or 'memory'. Default 'time'.
847+
* @param int|null $site_id Optional. The site ID to count users for. Defaults to the current site.
845848
* @return array Includes a grand total and an array of counts indexed by role strings.
846849
*/
847-
function count_users($strategy = 'time') {
850+
function count_users( $strategy = 'time', $site_id = null ) {
848851
global $wpdb;
849852

850853
// Initialize
851-
$id = get_current_blog_id();
852-
$blog_prefix = $wpdb->get_blog_prefix($id);
854+
if ( ! $site_id ) {
855+
$site_id = get_current_blog_id();
856+
}
857+
$blog_prefix = $wpdb->get_blog_prefix( $site_id );
853858
$result = array();
854859

855860
if ( 'time' == $strategy ) {
@@ -920,10 +925,6 @@ function count_users($strategy = 'time') {
920925
$result['avail_roles'] =& $avail_roles;
921926
}
922927

923-
if ( is_multisite() ) {
924-
$result['avail_roles']['none'] = 0;
925-
}
926-
927928
return $result;
928929
}
929930

@@ -2483,20 +2484,20 @@ function wp_destroy_all_sessions() {
24832484
/**
24842485
* Get the user IDs of all users with no role on this site.
24852486
*
2486-
* This function returns an empty array when used on Multisite.
2487-
*
24882487
* @since 4.4.0
2488+
* @since 4.9.0 The `$site_id` parameter was added to support multisite.
24892489
*
2490+
* @param int|null $site_id Optional. The site ID to get users with no role for. Defaults to the current site.
24902491
* @return array Array of user IDs.
24912492
*/
2492-
function wp_get_users_with_no_role() {
2493+
function wp_get_users_with_no_role( $site_id = null ) {
24932494
global $wpdb;
24942495

2495-
if ( is_multisite() ) {
2496-
return array();
2496+
if ( ! $site_id ) {
2497+
$site_id = get_current_blog_id();
24972498
}
24982499

2499-
$prefix = $wpdb->get_blog_prefix();
2500+
$prefix = $wpdb->get_blog_prefix( $site_id );
25002501
$regex = implode( '|', array_keys( wp_roles()->get_names() ) );
25012502
$regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
25022503
$users = $wpdb->get_col( $wpdb->prepare( "

tests/phpunit/tests/user/countUsers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function test_count_users_is_accurate( $strategy ) {
5252

5353
/**
5454
* @ticket 22993
55+
* @ticket 36196
5556
* @group multisite
5657
* @group ms-required
5758
*
@@ -103,7 +104,7 @@ public function test_count_users_multisite_is_accurate( $strategy ) {
103104
'author' => 1,
104105
'contributor' => 1,
105106
'subscriber' => 1,
106-
'none' => 0,
107+
'none' => 2,
107108
), $count['avail_roles'] );
108109

109110
// Test users counts on blog 1

tests/phpunit/tests/user/wpGetUsersWithNoRole.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function test_get_users_with_no_role_is_accurate() {
3636

3737
/**
3838
* @ticket 22993
39+
* @ticket 36196
3940
* @group multisite
4041
* @group ms-required
4142
*/
@@ -56,21 +57,27 @@ public function test_get_users_with_no_role_multisite_is_accurate() {
5657
'user_id' => $editor,
5758
) );
5859

59-
// Add users to blogs
60+
// Add editor to blog 1
6061
add_user_to_blog( $blog_1, $editor, 'editor' );
6162

6263
// Test users on root site
6364
$users = wp_get_users_with_no_role();
64-
$this->assertSame( array(), $users );
65+
$this->assertSame( array(
66+
"{$nobody}",
67+
), $users );
6568

6669
// Test users counts on blog 1
67-
switch_to_blog( $blog_1 );
68-
$users = wp_get_users_with_no_role();
69-
restore_current_blog();
70-
71-
// Test users on root site
70+
$users = wp_get_users_with_no_role( $blog_1 );
7271
$this->assertSame( array(), $users );
7372

73+
// Add admin to blog 1 with no role
74+
add_user_to_blog( $blog_1, $admin, '' );
75+
76+
// Re-test users counts on blog 1
77+
$users = wp_get_users_with_no_role( $blog_1 );
78+
$this->assertSame( array(
79+
"{$admin}",
80+
), $users );
7481
}
7582

7683
/**

0 commit comments

Comments
 (0)