8000 Merge pull request #2291 from BE-Webdesign/Add-date-query-for-comments · WP-API/WP-API@7063707 · 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 7063707

Browse files
Merge pull request #2291 from BE-Webdesign/ 8000 Add-date-query-for-comments
Add date query for comments
2 parents 4787ce2 + 0a40b32 commit 7063707

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public function get_items( $request ) {
135135
'author__not_in' => $request['author_exclude'],
136136
);
137137

138+
$prepared_args['date_query'] = array();
139+
// Set before into date query. Date query must be specified as an array of an array.
140+
if ( isset( $request['before'] ) ) {
141+
$prepared_args['date_query'][0]['before'] = $request['before'];
142+
}
143+
144+
// Set after into date query. Date query must be specified as an array of an array.
145+
if ( isset( $request['after'] ) ) {
146+
$prepared_args['date_query'][0]['after'] = $request['after'];
147+
}
148+
138149
if ( empty( $request['offset'] ) ) {
139150
$prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
140151
}
@@ -924,6 +935,12 @@ public function get_collection_params() {
924935

925936
$query_params['context']['default'] = 'view';
926937

938+
$query_params['after'] = array(
939+
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.' ),
940+
'type' => 'string',
941+
'format' => 'date-time',
942+
'validate_callback' => 'rest_validate_request_arg',
943+
);
927944
$query_params['author'] = array(
928945
'description' => __( 'Limit result set to comments assigned to specific user ids. Requires authorization.' ),
929946
'sanitize_callback' => 'wp_parse_id_list',
@@ -944,6 +961,12 @@ public function get_collection_params() {
944961
'validate_callback' => 'rest_validate_request_arg',
945962
'type' => 'string',
946963
);
964+
$query_params['before'] = array(
965+
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.' ),
966+
'type' => 'string',
967+
'format' => 'date-time',
968+
'validate_callback' => 'rest_validate_request_arg',
969+
);
947970
$query_params['exclude'] = array(
948971
'description' => __( 'Ensure result set excludes specific ids.' ),
949972
'type' => 'array',

tests/test-rest-comments-controller.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ public function test_registered_query_params() {
8282
$keys = array_keys( $data['endpoints'][0]['args'] );
8383
sort( $keys );
8484
$this->assertEquals( array(
85+
'after',
8586
'author',
8687
'author_email',
8788
'author_exclude',
89+
'before',
8890
'context',
8991
'exclude',
9092
'include',
@@ -454,6 +456,37 @@ public function test_get_comments_pagination_headers() {
454456
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
455457
}
456458

459+
public function test_get_comments_invalid_date() {
460+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
461+
$request->set_param( 'after', rand_str() );
462+
$request->set_param( 'before', rand_str() );
463+
$response = $this->server->dispatch( $request );
464+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
465+
}
466+
467+
public function test_get_comments_valid_date() {
468+
$comment1 = $this->factory->comment->create( array(
469+
'comment_date' => '2016-01-15T00:00:00Z',
470+
'comment_post_ID' => $this->post_id,
471+
) );
472+
$comment2 = $this->factory->comment->create( array(
473+
'comment_date' => '2016-01-16T00:00:00Z',
474+
'comment_post_ID' => $this->post_id,
475+
) );
476+
$comment3 = $this->factory->comment->create( array(
477+
'comment_date' => '2016-01-17T00:00:00Z',
478+
'comment_post_ID' => $this->post_id,
479+
) );
480+
481+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
482+
$request->set_param( 'after', '2016-01-15T00:00:00Z' );
483+
$request->set_param( 'before', '2016-01-17T00:00:00Z' );
484+
$response = $this->server->dispatch( $request );
485+
$data = $response->get_data();
486+
$this->assertCount( 1, $data );
487+
$this->assertEquals( $comment2, $data[0]['id'] );
488+
}
489+
457490
public function test_get_item() {
458491
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
459492

0 commit comments

Comments
 (0)
0