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.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 23 lib/endpoints/class-wp-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
33 changes: 33 additions & 0 deletions 33 tests/test-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 ) );

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.