REST API: Allow fetching multiple users at once via the slug
parameter.
This matches similar changes previously made for posts (#38579) and terms (#40027). Props curdin, MatheusGimenez. Fixes #40213. Merges [40378] to the 4.7 branch. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40426 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
59e00d43a5
commit
0b17a58481
@ -220,6 +220,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
||||
'per_page' => 'number',
|
||||
'search' => 'search',
|
||||
'roles' => 'role__in',
|
||||
'slug' => 'nicename__in',
|
||||
);
|
||||
|
||||
$prepared_args = array();
|
||||
@ -260,12 +261,6 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
||||
if ( ! empty( $prepared_args['search'] ) ) {
|
||||
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
|
||||
}
|
||||
|
||||
if ( isset( $registered['slug'] ) && ! empty( $request['slug'] ) ) {
|
||||
$prepared_args['search'] = $request['slug'];
|
||||
$prepared_args['search_columns'] = array( 'user_nicename' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters WP_User_Query arguments when querying users via the REST API.
|
||||
*
|
||||
@ -1361,7 +1356,10 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
|
||||
|
||||
$query_params['slug'] = array(
|
||||
'description' => __( 'Limit result set to users with a specific slug.' ),
|
||||
'type' => 'string',
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
);
|
||||
|
||||
$query_params['roles'] = array(
|
||||
|
@ -584,6 +584,68 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
|
||||
$this->assertEquals( $id2, $data[0]['id'] );
|
||||
}
|
||||
|
||||
public function test_get_items_slug_array_query() {
|
||||
wp_set_current_user( self::$user );
|
||||
$id1 = $this->factory->user->create( array(
|
||||
'display_name' => 'Taco',
|
||||
'user_login' => 'taco'
|
||||
) );
|
||||
$id2 = $this->factory->user->create( array(
|
||||
'display_name' => 'Enchilada',
|
||||
'user_login' => 'enchilada'
|
||||
) );
|
||||
$id3 = $this->factory->user->create( array(
|
||||
'display_name' => 'Burrito',
|
||||
'user_login' => 'burrito'
|
||||
) );
|
||||
$this->factory->user->create( array(
|
||||
'display_name' => 'Hon Pizza',
|
||||
'user_login' => 'pizza'
|
||||
) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
|
||||
$request->set_param( 'slug', array(
|
||||
'taco',
|
||||
'burrito',
|
||||
'enchilada',
|
||||
) );
|
||||
$request->set_param( 'orderby', 'slug' );
|
||||
$request->set_param( 'order', 'asc' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$slugs = wp_list_pluck( $data, 'slug' );
|
||||
$this->assertEquals( array( 'burrito', 'enchilada', 'taco' ), $slugs );
|
||||
}
|
||||
|
||||
public function test_get_items_slug_csv_query() {
|
||||
wp_set_current_user( self::$user );
|
||||
$id1 = $this->factory->user->create( array(
|
||||
'display_name' => 'Taco',
|
||||
'user_login' => 'taco'
|
||||
) );
|
||||
$id2 = $this->factory->user->create( array(
|
||||
'display_name' => 'Enchilada',
|
||||
'user_login' => 'enchilada'
|
||||
) );
|
||||
$id3 = $this->factory->user->create( array(
|
||||
'display_name' => 'Burrito',
|
||||
'user_login' => 'burrito'
|
||||
) );
|
||||
$this->factory->user->create( array(
|
||||
'display_name' => 'Hon Pizza',
|
||||
'user_login' => 'pizza'
|
||||
) );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
|
||||
$request->set_param( 'slug', 'taco,burrito , enchilada');
|
||||
$request->set_param( 'orderby', 'slug' );
|
||||
$request->set_param( 'order', 'desc' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$slugs = wp_list_pluck( $data, 'slug' );
|
||||
$this->assertEquals( array( 'taco', 'enchilada', 'burrito' ), $slugs );
|
||||
}
|
||||
|
||||
// Note: Do not test using editor role as there is an editor role created in testing and it makes it hard to test this functionality.
|
||||
public function test_get_items_roles() {
|
||||
wp_set_current_user( self::$user );
|
||||
|
@ -2439,7 +2439,10 @@ mockedApiResponse.Schema = {
|
||||
"slug": {
|
||||
"required": false,
|
||||
"description": "Limit result set to users with a specific slug.",
|
||||
"type": "string"
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"roles": {
|
||||
"required": false,
|
||||
|
Loading…
Reference in New Issue
Block a user