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.


git-svn-id: https://develop.svn.wordpress.org/trunk@40378 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
James Nylen 2017-04-05 22:24:24 +00:00
parent 66b4bb9809
commit a7b3612f2a
3 changed files with 75 additions and 12 deletions

View File

@ -221,6 +221,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
'per_page' => 'number',
'search' => 'search',
'roles' => 'role__in',
'slug' => 'nicename__in',
);
$prepared_args = array();
@ -261,12 +262,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.
*
@ -1363,8 +1358,11 @@ 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',
'description' => __( 'Limit result set to users with one or more specific slugs.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
);
$query_params['roles'] = array(

View File

@ -574,6 +574,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 );

View File

@ -1989,7 +1989,7 @@ mockedApiResponse.Schema = {
},
"slug": {
"required": false,
"description": "Limit result set to terms with a specific slug.",
"description": "Limit result set to terms with one or more specific slugs.",
"type": "array",
"items": {
"type": "string"
@ -2227,7 +2227,7 @@ mockedApiResponse.Schema = {
},
"slug": {
"required": false,
"description": "Limit result set to terms with a specific slug.",
"description": "Limit result set to terms with one or more specific slugs.",
"type": "array",
"items": {
"type": "string"
@ -2444,8 +2444,11 @@ mockedApiResponse.Schema = {
},
"slug": {
"required": false,
"description": "Limit result set to users with a specific slug.",
"type": "string"
"description": "Limit result set to users with one or more specific slugs.",
"type": "array",
"items": {
"type": "string"
}
},
"roles": {
"required": false,