From 0df1e996b5a2f97c693180539cc5234177bef462 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 7 Jan 2016 06:48:04 -0800 Subject: [PATCH 1/2] Add `include` parameter to `GET /wp/v2/users` --- .../class-wp-rest-users-controller.php | 15 +++++++++++++- tests/test-rest-users-controller.php | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/endpoints/class-wp-rest-users-controller.php b/lib/endpoints/class-wp-rest-users-controller.php index 308d167368..1b5e946daa 100755 --- a/lib/endpoints/class-wp-rest-users-controller.php +++ b/lib/endpoints/class-wp-rest-users-controller.php @@ -80,11 +80,13 @@ public function register_routes() { public function get_items( $request ) { $prepared_args = array(); + $prepared_args['include'] = $request['include']; $prepared_args['order'] = $request['order']; $prepared_args['number'] = $request['per_page']; $prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number']; $orderby_possibles = array( 'id' => 'ID', + 'include' => 'include', 'name' => 'display_name', 'registered_date' => 'registered', ); @@ -781,6 +783,12 @@ public function get_collection_params() { $query_params['context']['default'] = 'view'; + $query_params['include'] = array( + 'description' => __( 'Limit result set to specific ids.' ), + 'type' => 'array', + 'default' => array(), + 'sanitize_callback' => 'wp_parse_id_list', + ); $query_params['order'] = array( 'default' => 'asc', 'description' => __( 'Order sort attribute ascending or descending.' ), @@ -791,7 +799,12 @@ public function get_collection_params() { $query_params['orderby'] = array( 'default' => 'name', 'description' => __( 'Sort collection by object attribute.' ), - 'enum' => array( 'id', 'name', 'registered_date' ), + 'enum' => array( + 'id', + 'include', + 'name', + 'registered_date' + ), 'sanitize_callback' => 'sanitize_key', 'type' => 'string', ); diff --git a/tests/test-rest-users-controller.php b/tests/test-rest-users-controller.php index 0cc25edfba..9a660d1ea0 100644 --- a/tests/test-rest-users-controller.php +++ b/tests/test-rest-users-controller.php @@ -206,6 +206,26 @@ public function test_get_items_orderby() { $this->assertEquals( $low_id, $data[0]['id'] ); } + public function test_get_items_include_query() { + wp_set_current_user( $this->user ); + $id1 = $this->factory->user->create(); + $id2 = $this->factory->user->create(); + $id3 = $this->factory->user->create(); + $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); + // Orderby=>asc + $request->set_param( 'include', array( $id3, $id1 ) ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 2, count( $data ) ); + $this->assertEquals( $id1, $data[0]['id'] ); + // Orderby=>include + $request->set_param( 'orderby', 'include' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + $this->assertEquals( 2, count( $data ) ); + $this->assertEquals( $id3, $data[0]['id'] ); + } + public function test_get_items_search() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', '/wp/v2/users' ); From cf976a061fdd00380aaa5d552f6a0c2da123a93c Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 7 Jan 2016 07:39:34 -0800 Subject: [PATCH 2/2] Fix PHPCS issue --- lib/endpoints/class-wp-rest-users-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/endpoints/class-wp-rest-users-controller.php b/lib/endpoints/class-wp-rest-users-controller.php index 1b5e946daa..108ff0bc09 100755 --- a/lib/endpoints/class-wp-rest-users-controller.php +++ b/lib/endpoints/class-wp-rest-users-controller.php @@ -803,7 +803,7 @@ public function get_collection_params() { 'id', 'include', 'name', - 'registered_date' + 'registered_date', ), 'sanitize_callback' => 'sanitize_key', 'type' => 'string',