Make WordPress Core

Changeset 41653


Ignore:
Timestamp:
09/30/2017 03:53:27 AM (9 years ago)
Author:
flixos90
Message:

Multisite: Fix count_users() possibly querying incorrect roles when passed a different site ID.

The time strategy in count_users() queries users by role. However, the roles queried for were not affected by passing another site than the current one through the $site_id parameter, causing users having roles that were not queried for to appear as users without a role. This changeset fixes the issue by switching the site before retrieving the roles to query for.

Fixes #42014.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r41564 r41653  
    856856
    857857        if ( 'time' == $strategy ) {
    858                 $avail_roles = wp_roles()->get_names();
     858                if ( is_multisite() && $site_id != get_current_blog_id() ) {
     859                        switch_to_blog( $site_id );
     860                        $avail_roles = wp_roles()->get_names();
     861                        restore_current_blog();
     862                } else {
     863                        $avail_roles = wp_roles()->get_names();
     864                }
    859865
    860866                // Build a CPU-intensive query that will return concise information.
  • trunk/tests/phpunit/tests/user/countUsers.php

    r41138 r41653  
    132132                ), $count['avail_roles'] );
    133133
     134        }
     135
     136        /**
     137         * @ticket 42014
     138         * @group multisite
     139         * @group ms-required
     140         *
     141         * @dataProvider data_count_users_strategies
     142         */
     143        public function test_count_users_multisite_queries_correct_roles( $strategy ) {
     144                $site_id = (int) self::factory()->blog->create();
     145
     146                switch_to_blog( $site_id );
     147                wp_roles()->add_role( 'tester', 'Tester', array( 'test' => true ) );
     148                $user_id = self::factory()->user->create( array(
     149                        'role' => 'tester',
     150                ) );
     151                restore_current_blog();
     152
     153                $count = count_users( $strategy, $site_id );
     154                $this->assertEqualSetsWithIndex( array(
     155                        'tester' => 1,
     156                        'none'   => 0,
     157                ), $count['avail_roles'] );
    134158        }
    135159
Note: See TracChangeset for help on using the changeset viewer.