Make WordPress Core

Changeset 38651


Ignore:
Timestamp:
09/25/2016 05:44:24 PM (8 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r38457 r38651  
    965965 * @since 2.3.0
    966966 * @since 4.5.0 Added the 'display_name_with_login' value for 'show'.
     967 * @since 4.7.0 Added the `$role`, `$role__in`, and `$role__not_in` parameters.
    967968 *
    968969 * @param array|string $args {
     
    10051006 *     @type string       $who                     Which type of users to query. Accepts only an empty string or
    10061007 *                                                 'authors'. Default empty.
     1008 *     @type string|array $role                    An array or a comma-separated list of role names that users must
     1009 *                                                 match to be included in results. Note that this is an inclusive
     1010 *                                                 list: users must match *each* role. Default empty.
     1011 *     @type array        $role__in                An array of role names. Matched users must have at least one of
     1012 *                                                 these roles. Default empty array.
     1013 *     @type array        $role__not_in            An array of role names to exclude. Users matching one or more of
     1014 *                                                 these roles will not be included in results. Default empty array.
    10071015 * }
    10081016 * @return string String of HTML content.
     
    10161024        'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
    10171025        'blog_id' => get_current_blog_id(), 'who' => '', 'include_selected' => false,
    1018         'option_none_value' => -1
     1026        'option_none_value' => -1,
     1027        'role' => '',
     1028        'role__in' => array(),
     1029        'role__not_in' => array(),
    10191030    );
    10201031
     
    10231034    $r = wp_parse_args( $args, $defaults );
    10241035
    1025     $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
     1036    $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who', 'role', 'role__in', 'role__not_in' ) );
    10261037
    10271038    $fields = array( 'ID', 'user_login' );
  • trunk/tests/phpunit/tests/user/wpDropdownUsers.php

    r35790 r38651  
    111111        $this->assertContains( $user1->user_login, $found );
    112112    }
     113
     114    /**
     115     * @ticket 38135
     116     */
     117    public function test_role() {
     118        $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
     119        $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
     120
     121        $found = wp_dropdown_users( array(
     122            'echo' => false,
     123            'role' => 'author',
     124            'show' => 'user_login',
     125        ) );
     126
     127        $this->assertNotContains( $u1->user_login, $found );
     128        $this->assertContains( $u2->user_login, $found );
     129    }
     130
     131    /**
     132     * @ticket 38135
     133     */
     134    public function test_role__in() {
     135        $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
     136        $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
     137
     138        $found = wp_dropdown_users( array(
     139            'echo' => false,
     140            'role__in' => array( 'author', 'editor' ),
     141            'show' => 'user_login',
     142        ) );
     143
     144        $this->assertNotContains( $u1->user_login, $found );
     145        $this->assertContains( $u2->user_login, $found );
     146    }
     147
     148    /**
     149     * @ticket 38135
     150     */
     151    public function test_role__not_in() {
     152        $u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
     153        $u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
     154
     155        $found = wp_dropdown_users( array(
     156            'echo' => false,
     157            'role__not_in' => array( 'subscriber', 'editor' ),
     158            'show' => 'user_login',
     159        ) );
     160
     161        $this->assertNotContains( $u1->user_login, $found );
     162        $this->assertContains( $u2->user_login, $found );
     163    }
    113164}
Note: See TracChangeset for help on using the changeset viewer.