From cf6f354fa8867f976b841bf0af608f6f35a4a899 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 25 Sep 2016 17:44:24 +0000 Subject: [PATCH] Allow 'role' parameters to be passed to `wp_dropdown_users()`. `wp_dropdown_users()` contains a whitelist of function params that are passed through to `get_users()`. `role`, `role__in`, and `role__not_in` have now been added to this whitelist. Props sillybean. Fixes #38135. git-svn-id: https://develop.svn.wordpress.org/trunk@38651 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 15 +++++- tests/phpunit/tests/user/wpDropdownUsers.php | 51 ++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 9ff6455f38..5d1264d161 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -964,6 +964,7 @@ function setup_userdata($for_user_id = '') { * * @since 2.3.0 * @since 4.5.0 Added the 'display_name_with_login' value for 'show'. + * @since 4.7.0 Added the `$role`, `$role__in`, and `$role__not_in` parameters. * * @param array|string $args { * Optional. Array or string of arguments to generate a drop-down of users. @@ -1004,6 +1005,13 @@ function setup_userdata($for_user_id = '') { * @type int $blog_id ID of blog (Multisite only). Default is ID of the current blog. * @type string $who Which type of users to query. Accepts only an empty string or * 'authors'. Default empty. + * @type string|array $role An array or a comma-separated list of role names that users must + * match to be included in results. Note that this is an inclusive + * list: users must match *each* role. Default empty. + * @type array $role__in An array of role names. Matched users must have at least one of + * these roles. Default empty array. + * @type array $role__not_in An array of role names to exclude. Users matching one or more of + * these roles will not be included in results. Default empty array. * } * @return string String of HTML content. */ @@ -1015,14 +1023,17 @@ function wp_dropdown_users( $args = '' ) { 'show' => 'display_name', 'echo' => 1, 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', 'blog_id' => get_current_blog_id(), 'who' => '', 'include_selected' => false, - 'option_none_value' => -1 + 'option_none_value' => -1, + 'role' => '', + 'role__in' => array(), + 'role__not_in' => array(), ); $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0; $r = wp_parse_args( $args, $defaults ); - $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) ); + $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who', 'role', 'role__in', 'role__not_in' ) ); $fields = array( 'ID', 'user_login' ); diff --git a/tests/phpunit/tests/user/wpDropdownUsers.php b/tests/phpunit/tests/user/wpDropdownUsers.php index 844c9532ed..2f07a9a903 100644 --- a/tests/phpunit/tests/user/wpDropdownUsers.php +++ b/tests/phpunit/tests/user/wpDropdownUsers.php @@ -110,4 +110,55 @@ class Tests_User_WpDropdownUsers extends WP_UnitTestCase { $user1 = get_userdata( $users[1] ); $this->assertContains( $user1->user_login, $found ); } + + /** + * @ticket 38135 + */ + public function test_role() { + $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ); + $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) ); + + $found = wp_dropdown_users( array( + 'echo' => false, + 'role' => 'author', + 'show' => 'user_login', + ) ); + + $this->assertNotContains( $u1->user_login, $found ); + $this->assertContains( $u2->user_login, $found ); + } + + /** + * @ticket 38135 + */ + public function test_role__in() { + $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ); + $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) ); + + $found = wp_dropdown_users( array( + 'echo' => false, + 'role__in' => array( 'author', 'editor' ), + 'show' => 'user_login', + ) ); + + $this->assertNotContains( $u1->user_login, $found ); + $this->assertContains( $u2->user_login, $found ); + } + + /** + * @ticket 38135 + */ + public function test_role__not_in() { + $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ); + $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) ); + + $found = wp_dropdown_users( array( + 'echo' => false, + 'role__not_in' => array( 'subscriber', 'editor' ), + 'show' => 'user_login', + ) ); + + $this->assertNotContains( $u1->user_login, $found ); + $this->assertContains( $u2->user_login, $found ); + } }