8000 Merge pull request #2266 from BE-Webdesign/Potential-enhancement-#2144 · WP-API/WP-API@be1ee54 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit be1ee54

Browse files
Merge pull request #2266 from BE-Webdesign/Potential-enhancement-#2144
Add date query to posts endpoints
2 parents 7063707 + d9b9763 commit be1ee54

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ public function get_items( $request ) {
102102
$args['post_status'] = $request['status'];
103103
$args['s'] = $request['search'];
104104

105+
$args['date_query'] = array();
106+
// Set before into date query. Date query must be specified as an array of an array.
107+
if ( isset( $request['before'] ) ) {
108+
$args['date_query'][0]['before'] = $request['before'];
109+
}
110+
111+
// Set after into date query. Date query must be specified as an array of an array.
112+
if ( isset( $request['after'] ) ) {
113+
$args['date_query'][0]['after'] = $request['after'];
114+
}
115+
105116
if ( is_array( $request['filter'] ) ) {
106117
$args = array_merge( $args, $request['filter'] );
107118
unset( $args['filter'] );
@@ -599,6 +610,7 @@ protected function get_allowed_query_vars() {
599610
'post_parent__in',
600611
'post_parent__not_in',
601612
'posts_per_page',
613+
'date_query',
602614
);
603615
$valid_vars = array_merge( $valid_vars, $rest_valid );
604616

@@ -1581,6 +1593,12 @@ public function get_collection_params() {
15811593

15821594
$params['context']['default'] = 'view';
15831595

1596+
$params['after'] = array(
1597+
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.' ),
1598+
'type' => 'string',
1599+
'format' => 'date-time',
1600+
'validate_callback' => 'rest_validate_request_arg',
1601+
);
15841602
if ( post_type_supports( $this->post_type, 'author' ) ) {
15851603
$params['author'] = array(
15861604
'description' => __( 'Limit result set to posts assigned to specific authors.' ),
@@ -1597,6 +1615,12 @@ public function get_collection_params() {
15971615
'validate_callback' => 'rest_validate_request_arg',
15981616
);
15991617
}
1618+
$params['before'] = array(
1619+
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.' ),
1620+
'type' => 'string',
1621+
'format' => 'date-time',
1622+
'validate_callback' => 'rest_validate_request_arg',
1623+
);
16001624
$params['exclude'] = array(
16011625
'description' => __( 'Ensure result set excludes specific ids.' ),
16021626
'type' => 'array',

tests/test-rest-attachments-controller.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ public function test_registered_query_params() {
105105
$keys = array_keys( $data['endpoints'][0]['args'] );
106106
sort( $keys );
107107
$this->assertEquals( array(
108+
'after',
108109
'author',
109110
'author_exclude',
111+
'before',
110112
'context',
111113
'exclude',
112114
'filter',
@@ -300,6 +302,39 @@ public function test_get_items_private_status() {
300302
$this->assertEquals( $attachment_id1, $data[0]['id'] );
301303
}
302304

305+
public function test_get_items_invalid_date() {
306+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
307+
$request->set_param( 'after', rand_str() );
308+
$request->set_param( 'before', rand_str() );
309+
$response = $this->server->dispatch( $request );
310+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
311+
}
312+
313+
public function test_get_items_valid_date() {
314+
$id1 = $this->factory->attachment->create_object( $this->test_file, 0, array(
315+
'post_date' => '2016-01-15T00:00:00Z',
316+
'post_mime_type' => 'image/jpeg',
317+
'post_excerpt' => 'A sample caption',
318+
) );
319+
$id2 = $this->factory->attachment->create_object( $this->test_file, 0, array(
320+
'post_date' => '2016-01-16T00:00:00Z',
321+
'post_mime_type' => 'image/jpeg',
322+
'post_excerpt' => 'A sample caption',
323+
) );
324+
$id3 = $this->factory->attachment->create_object( $this->test_file, 0, array(
325+
'post_date' => '2016-01-17T00:00:00Z',
326+
'post_mime_type' => 'image/jpeg',
327+
'post_excerpt' => 'A sample caption',
328+
) );
329+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
330+
$request->set_param( 'after', '2016-01-15T00:00:00Z' );
331+
$request->set_param( 'before', '2016-01-17T00:00:00Z' );
332+
$response = $this->server->dispatch( $request );
333+
$data = $response->get_data();
334+
$this->assertCount( 1, $data );
335+
$this->assertEquals( $id2, $data[0]['id'] );
336+
}
337+
303338
public function test_get_item() {
304339
$attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
305340
'post_mime_type' => 'image/jpeg',

tests/test-rest-pages-controller.php

Lines changed: 23 additions & 0 deletions
< 341A /div>
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public function test_registered_query_params() {
5050
$keys = array_keys( $data['endpoints'][0]['args'] );
5151
sort( $keys );
5252
$this->assertEquals( array(
53+
'after',
5354
'author',
5455
'author_exclude',
56+
'before',
5557
'context',
5658
'exclude',
5759
'filter',
@@ -187,6 +189,27 @@ public function test_get_items_private_filter_query_var() {
187189
$this->assertEquals( $draft_id, $data[0]['id'] );
188190
}
189191

192+
public function test_get_items_invalid_date() {
193+
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
194+
$request->set_param( 'after', rand_str() );
195+
$request->set_param( 'before', rand_str() );
196+
$response = $this->server->dispatch( $request );
197+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
198+
}
199+
200+
public function test_get_items_valid_date() {
201+
$post1 = $this->factory->post->create( array( 'post_date' => '2016-01-15T00:00:00Z', 'post_type' => 'page' ) );
202+
$post2 = $this->factory->post->create( array( 'post_date' => '2016-01-16T00:00:00Z', 'post_type' => 'page' ) );
203+
$post3 = $this->factory->post->create( array( 'post_date' => '2016-01-17T00:00:00Z', 'post_type' => 'page' ) );
204+
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
205+
$request->set_param( 'after', '2016-01-15T00:00:00Z' );
206+
$request->set_param( 'before', '2016-01-17T00:00:00Z' );
207+
$response = $this->server->dispatch( $request );
208+
$data = $response->get_data();
209+
$this->assertCount( 1, $data );
210+
$this->assertEquals( $post2, $data[0]['id'] );
211+
}
212+
190213
public function test_get_item() {
191214

192215
}

tests/test-rest-posts-controller.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public function test_registered_query_params() {
5757
$keys = array_keys( $data['endpoints'][0]['args'] );
5858
sort( $keys );
5959
$this->assertEquals( array(
60+
'after',
6061
'author',
6162
'author_exclude',
63+
'before',
6264
'context',
6365
'exclude',
6466
'filter',
@@ -413,6 +415,28 @@ public function test_get_items_invalid_context() {
413415
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
414416
}
415417

418+
public function test_get_items_invalid_date() {
419+
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
420+
$request->set_param( 'after', rand_str() );
421+
$request->set_param( 'before', rand_str() );
422+
$response = $this->server->dispatch( $request );
423+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
424+
}
425+
426+
public function test_get_items_valid_date() {
427+
$post1 = $this->factory->post->create( array( 'post_date' => '2016-01-15T00:00:00Z' ) );
428+
$post2 = $this->factory->post->create( array( 'post_date' => '2016-01-16T00:00:00Z' ) );
429+
$post3 = $this->factory->post->create( array( 'post_date' => '2016-01-17T00:00:00Z' ) );
430+
431+
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
432+
$request->set_param( 'after', '2016-01-15T00:00:00Z' );
433+
$request->set_param( 'before', '2016-01-17T00:00:00Z' );
434+
$response = $this->server->dispatch( $request );
435+
$data = $response->get_data();
436+
$this->assertCount( 1, $data );
437+
$this->assertEquals( $post2, $data[0]['id'] );
438+
}
439+
416440
public function test_get_item() {
417441
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $this->post_id ) );
418442
$response = $this->server->dispatch( $request );

0 commit comments

Comments
 (0)
0