From a7b3612f2ac265c09dd910d3fe7824b2faa35ab2 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Wed, 5 Apr 2017 22:24:24 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-users-controller.php | 14 ++--- .../tests/rest-api/rest-users-controller.php | 62 +++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 11 ++-- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index c762ed41d7..63fb4e9e99 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -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( diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 0a85336155..1a81e422d1 100644 --- a/tests/phpunit/tests/rest-api/rest-users-controller.php +++ b/tests/phpunit/tests/rest-api/rest-users-controller.php @@ -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 ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 2e47935796..0df5b9e5b2 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -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,