diff --git a/lib/endpoints/class-wp-rest-comments-controller.php b/lib/endpoints/class-wp-rest-comments-controller.php index da83ca15d6..1401e7e9f2 100755 --- a/lib/endpoints/class-wp-rest-comments-controller.php +++ b/lib/endpoints/class-wp-rest-comments-controller.php @@ -135,6 +135,17 @@ public function get_items( $request ) { 'author__not_in' => $request['author_exclude'], ); + $prepared_args['date_query'] = array(); + // Set before into date query. Date query must be specified as an array of an array. + if ( isset( $request['before'] ) ) { + $prepared_args['date_query'][0]['before'] = $request['before']; + } + + // Set after into date query. Date query must be specified as an array of an array. + if ( isset( $request['after'] ) ) { + $prepared_args['date_query'][0]['after'] = $request['after']; + } + if ( empty( $request['offset'] ) ) { $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 ); } @@ -924,6 +935,12 @@ public function get_collection_params() { $query_params['context']['default'] = 'view'; + $query_params['after'] = array( + 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.' ), + 'type' => 'string', + 'format' => 'date-time', + 'validate_callback' => 'rest_validate_request_arg', + ); $query_params['author'] = array( 'description' => __( 'Limit result set to comments assigned to specific user ids. Requires authorization.' ), 'sanitize_callback' => 'wp_parse_id_list', @@ -944,6 +961,12 @@ public function get_collection_params() { 'validate_callback' => 'rest_validate_request_arg', 'type' => 'string', ); + $query_params['before'] = array( + 'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.' ), + 'type' => 'string', + 'format' => 'date-time', + 'validate_callback' => 'rest_validate_request_arg', + ); $query_params['exclude'] = array( 'description' => __( 'Ensure result set excludes specific ids.' ), 'type' => 'array', diff --git a/tests/test-rest-comments-controller.php b/tests/test-rest-comments-controller.php index b8bd27fb8e..a4466f3698 100644 --- a/tests/test-rest-comments-controller.php +++ b/tests/test-rest-comments-controller.php @@ -82,9 +82,11 @@ public function test_registered_query_params() { $keys = array_keys( $data['endpoints'][0]['args'] ); sort( $keys ); $this->assertEquals( array( + 'after', 'author', 'author_email', 'author_exclude', + 'before', 'context', 'exclude', 'include', @@ -454,6 +456,37 @@ public function test_get_comments_pagination_headers() { $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) ); } + public function test_get_comments_invalid_date() { + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'after', rand_str() ); + $request->set_param( 'before', rand_str() ); + $response = $this->server->dispatch( $request ); + $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + } + + public function test_get_comments_valid_date() { + $comment1 = $this->factory->comment->create( array( + 'comment_date' => '2016-01-15T00:00:00Z', + 'comment_post_ID' => $this->post_id, + ) ); + $comment2 = $this->factory->comment->create( array( + 'comment_date' => '2016-01-16T00:00:00Z', + 'comment_post_ID' => $this->post_id, + ) ); + $comment3 = $this->factory->comment->create( array( + 'comment_date' => '2016-01-17T00:00:00Z', + 'comment_post_ID' => $this->post_id, + ) ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments' ); + $request->set_param( 'after', '2016-01-15T00:00:00Z' ); + $request->set_param( 'before', '2016-01-17T00:00:00Z' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + $this->assertCount( 1, $data ); + $this->assertEquals( $comment2, $data[0]['id'] ); + } + public function test_get_item() { $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );