Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit 60629ff

Browse filesBrowse files
committed
Merge pull request #1217 from WP-API/924-declare-user-query
Declare supported arguments for user collection queries
2 parents ac9a324 + ab92d95 commit 60629ff
Copy full SHA for 60629ff

2 files changed

+106-22Lines changed: 106 additions & 22 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/endpoints/class-wp-rest-users-controller.php‎

Copy file name to clipboardExpand all lines: lib/endpoints/class-wp-rest-users-controller.php
+40-22Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,13 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
1010
*/
1111
public function register_routes() {
1212

13+
$query_params = $this->get_collection_params();
1314
register_rest_route( 'wp/v2', '/users', array(
1415
array(
1516
'methods' => WP_REST_Server::READABLE,
1617
'callback' => array( $this, 'get_items' ),
1718
'permission_callback' => array( $this, 'get_items_permissions_check' ),
18-
'args' => array(
19-
'context' => array(
20-
'default' => 'view',
21-
),
22-
'order' => array(
23-
'default' => 'asc',
24-
'sanitize_callback' => 'sanitize_key',
25-
),
26-
'orderby' => array(
27-
'default' => 'user_login',
28-
'sanitize_callback' => 'sanitize_key',
29-
),
30-
'per_page' => array(
31-
'default' => 10,
32-
'sanitize_callback' => 'absint',
33-
),
34-
'page' => array(
35-
'default' => 1,
36-
'sanitize_callback' => 'absint',
37-
),
38-
),
19+
'args' => $query_params,
3920
),
4021
array(
4122
'methods' => WP_REST_Server::CREATABLE,
@@ -101,9 +82,15 @@ public function get_items( $request ) {
10182

10283
$prepared_args = array();
10384
$prepared_args['order'] = $request['order'];
104-
$prepared_args['orderby'] = $request['orderby'];
10585
$prepared_args['number'] = $request['per_page'];
10686
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
87+
$orderby_possibles = array(
88+
'id' => 'ID',
89+
'name' => 'display_name',
90+
'registered_date' => 'registered',
91+
);
92+
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
93+
$prepared_args['search'] = $request['search'];
10794

10895
$prepared_args = apply_filters( 'rest_user_query', $prepared_args, $request );
10996

@@ -646,4 +633,35 @@ public function get_item_schema() {
646633
);
647634
return $this->add_additional_fields_schema( $schema );
648635
}
636+
637+
/**
638+
* Get the query params for collections
639+
*
640+
* @return array
641+
*/
642+
public function get_collection_params() {
643+
$query_params = parent::get_collection_params();
644+
$query_params['context'] = array(
645+
'default' => 'view',
646+
'description' => 'Change the response format based on request context.',
647+
'enum' => array( 'view', 'edit' ),
648+
'sanitize_callback' => 'sanitize_key',
649+
'type' => 'string',
650+
);
651+
$query_params['order'] = array(
652+
'default' => 'asc',
653+
'description' => 'Order sort attribute ascending or descending.',
654+
'enum' => array( 'asc', 'desc' ),
655+
'sanitize_callback' => 'sanitize_key',
656+
'type' => 'string',
657+
);
658+
$query_params['orderby'] = array(
659+
'default' => 'name',
660+
'description' => 'Sort collection by object attribute.',
661+
'enum' => array( 'id', 'name', 'registered_date' ),
662+
'sanitize_callback' => 'sanitize_key',
663+
'type' => 'string',
664+
);
665+
return $query_params;
666+
}
649667
}
Collapse file

‎tests/test-rest-users-controller.php‎

Copy file name to clipboardExpand all lines: tests/test-rest-users-controller.php
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,72 @@ public function test_get_items_pagination_headers() {
116116
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
117117
}
118118

119+
public function test_get_items_per_page() {
120+
wp_set_current_user( $this->user );
121+
for ( $i = 0; $i < 20; $i++ ) {
122+
$this->factory->user->create( array( 'display_name' => "User {$i}" ) );
123+
}
124+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
125+
$response = $this->server->dispatch( $request );
126+
$this->assertEquals( 10, count( $response->get_data() ) );
127+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
128+
$request->set_param( 'per_page', 5 );
129+
$response = $this->server->dispatch( $request );
130+
$this->assertEquals( 5, count( $response->get_data() ) );
131+
}
132+
133+
public function test_get_items_page() {
134+
wp_set_current_user( $this->user );
135+
for ( $i = 0; $i < 20; $i++ ) {
136+
$this->factory->user->create( array( 'display_name' => "User {$i}" ) );
137+
}
138+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
139+
$request->set_param( 'per_page', 5 );
140+
$request->set_param( 'page', 2 );
141+
$response = $this->server->dispatch( $request );
142+
$this->assertEquals( 5, count( $response->get_data() ) );
143+
$prev_link = add_query_arg( array(
144+
'per_page' => 5,
145+
'page' => 1,
146+
), rest_url( '/wp/v2/users' ) );
147+
$headers = $response->get_headers();
148+
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
149+
}
150+
151+
public function test_get_items_orderby() {
152+
wp_set_current_user( $this->user );
153+
$low_id = $this->factory->user->create( array( 'display_name' => 'AAAAA' ) );
154+
$mid_id = $this->factory->user->create( array( 'display_name' => 'NNNNN' ) );
155+
$high_id = $this->factory->user->create( array( 'display_name' => 'ZZZZ' ) );
156+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
157+
$request->set_param( 'orderby', 'name' );
158+
$request->set_param( 'order', 'desc' );
159+
$request->set_param( 'per_page', 1 );
160+
$response = $this->server->dispatch( $request );
161+
$data = $response->get_data();
162+
$this->assertEquals( $high_id, $data[0]['id'] );
163+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
164+
$request->set_param( 'orderby', 'name' );
165+
$request->set_param( 'order', 'asc' );
166+
$request->set_param( 'per_page', 1 );
167+
$response = $this->server->dispatch( $request );
168+
$data = $response->get_data();
169+
$this->assertEquals( $low_id, $data[0]['id'] );
170+
}
171+
172+
public function test_get_items_search() {
173+
wp_set_current_user( $this->user );
174+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
175+
$request->set_param( 'search', 'yololololo' );
176+
$response = $this->server->dispatch( $request );
177+
$this->assertEquals( 0, count( $response->get_data() ) );
178+
$yolo_id = $this->factory->user->create( array( 'display_name' => 'yololololo' ) );
179+
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
180+
$request->set_param( 'search', $yolo_id ); // searching doesn't support display name
181+
$response = $this->server->dispatch( $request );
182+
$this->assertEquals( 1, count( $response->get_data() ) );
183+
}
184+
119185
public function test_get_item() {
120186
$user_id = $this->factory->user->create();
121187
wp_set_current_user( $this->user );

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.