diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index adb134a148..a98ca8fa05 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -857,7 +857,7 @@ class WP_User_Query { $this->results = $r; } elseif ( 'all' == $qv['fields'] ) { foreach ( $this->results as $key => $user ) { - $this->results[ $key ] = new WP_User( $user ); + $this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] ); } } } diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 196b3d4fd4..f208f5609a 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -599,4 +599,30 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertSame( array( 'author' ), $user->roles ); $this->assertSame( array( 'author' => true ), $user->caps ); } + + /** + * @ticket 31878 + */ + public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all() { + if ( ! is_multisite() ) { + $this->markTestSkipped( __METHOD__ . ' is a multisite-only test.' ); + } + + $b = $this->factory->blog->create(); + $u = $this->factory->user->create(); + add_user_to_blog( $b, $u, 'author' ); + + $query = new WP_User_Query( array( + 'fields' => 'all', + 'include' => $u, + 'blog_id' => $b, + ) ); + + $found = $query->get_results(); + + $this->assertNotEmpty( $found ); + $user = reset( $found ); + $this->assertSame( array( 'author' ), $user->roles ); + $this->assertSame( array( 'author' => true ), $user->caps ); + } }