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 fb1d99e

Browse filesBrowse files
author
Daniel Bachhuber
committed
Merge pull request #1197 from WP-API/1186-comments-pagination
Pagination headers for comment collection queries
2 parents 0fd1624 + 08399b0 commit fb1d99e
Copy full SHA for fb1d99e

2 files changed

+91-4Lines changed: 91 additions & 4 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-comments-controller.php‎

Copy file name to clipboardExpand all lines: lib/endpoints/class-wp-rest-comments-controller.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ public function get_items( $request ) {
182182
}
183183

184184
$response = rest_ensure_response( $comments );
185+
unset( $prepared_args['number'] );
186+
unset( $prepared_args['offset'] );
187+
$query = new WP_Comment_Query;
188+
$prepared_args['count'] = true;
189+
$total_comments = $query->query( $prepared_args );
190+
$response->header( 'X-WP-Total', (int) $total_comments );
191+
$max_pages = ceil( $total_comments / $request['per_page'] );
192+
$response->header( 'X-WP-TotalPages', (int) $max_pages );
193+
194+
$base = add_query_arg( $request->get_query_params(), rest_url( '/wp/v2/comments' ) );
195+
if ( $request['page'] > 1 ) {
196+
$prev_page = $request['page'] - 1;
197+
if ( $prev_page > $max_pages ) {
198+
$prev_page = $max_pages;
199+
}
200+
$prev_link = add_query_arg( 'page', $prev_page, $base );
201+
$response->link_header( 'prev', $prev_link );
202+
}
203+
if ( $max_pages > $request['page'] ) {
204+
$next_page = $request['page'] + 1;
205+
$next_link = add_query_arg( 'page', $next_page, $base );
206+
$response->link_header( 'next', $next_link );
207+
}
185208

186209
return $response;
187210
}
Collapse file

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

Copy file name to clipboardExpand all lines: tests/test-rest-comments-controller.php
+68-4Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,70 @@ public function test_get_items_for_post() {
9494
$this->assertCount( 2, $comments );
9595
}
9696

97+
public function test_get_comments_pagination_headers() {
98+
wp_set_current_user( $this->admin_id );
99+
// Start of the index
100+
for ( $i = 0; $i < 49; $i++ ) {
101+
$this->factory->comment->create( array(
102+
'comment_content' => "Comment {$i}",
103+
'comment_post_ID' => $this->post_id,
104+
) );
105+
}
106+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
107+
$response = $this->server->dispatch( $request );
108+
$headers = $response->get_headers();
109+
$this->assertEquals( 50, $headers['X-WP-Total'] );
110+
$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
111+
$next_link = add_query_arg( array(
112+
'page' => 2,
113+
), rest_url( '/wp/v2/comments' ) );
114+
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
115+
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
116+
// 3rd page
117+
$this->factory->comment->create( array(
118+
'comment_content' => 'Comment 51',
119+
'comment_post_ID' => $this->post_id,
120+
) );
121+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
122+
$request->set_param( 'page', 3 );
123+
$response = $this->server->dispatch( $request );
124+
$headers = $response->get_headers();
125+
$this->assertEquals( 51, $headers['X-WP-Total'] );
126+
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
127+
$prev_link = add_query_arg( array(
128+
'page' => 2,
129+
), rest_url( '/wp/v2/comments' ) );
130+
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
131+
$next_link = add_query_arg( array(
132+
'page' => 4,
133+
), rest_url( '/wp/v2/comments' ) );
134+
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
135+
// Last page
136+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
137+
$request->set_param( 'page', 6 );
138+
$response = $this->server->dispatch( $request );
139+
$headers = $response->get_headers();
140+
$this->assertEquals( 51, $headers['X-WP-Total'] );
141+
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
142+
$prev_link = add_query_arg( array(
143+
'page' => 5,
144+
), rest_url( '/wp/v2/comments' ) );
145+
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
146+
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
147+
// Out of bounds
148+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
149+
$request->set_param( 'page', 8 );
150+
$response = $this->server->dispatch( $request );
151+
$headers = $response->get_headers();
152+
$this->assertEquals( 51, $headers['X-WP-Total'] );
153+
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
154+
$prev_link = add_query_arg( array(
155+
'page' => 6,
156+
), rest_url( '/wp/v2/comments' ) );
157+
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
158+
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
159+
}
160+
97161
public function test_get_item() {
98162
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $this->approved_id ) );
99163

@@ -119,7 +183,7 @@ public function test_prepare_item() {
119183
}
120184

121185
public function test_get_comment_invalid_id() {
122-
$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . 100 );
186+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . 99999 );
123187

124188
$response = $this->server->dispatch( $request );
125189
$this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
@@ -135,7 +199,7 @@ public function test_get_comment_invalid_context() {
135199
public function test_get_comment_invalid_post_id() {
136200
$comment_id = $this->factory->comment->create( array(
137201
'comment_approved' => 1,
138-
'comment_post_ID' => 100,
202+
'comment_post_ID' => 99999,
139203
));
140204
$request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
141205

@@ -338,7 +402,7 @@ public function test_update_comment_invalid_id() {
338402
$params = array(
339403
'content' => 'Oh, they have the internet on computers now!',
340404
);
341-
$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . 100 );
405+
$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . 99999 );
342406
$request->add_header( 'content-type', 'application/json' );
343407
$request->set_body( json_encode( $params ) );
344408

@@ -378,7 +442,7 @@ public function test_delete_item() {
378442
public function test_delete_comment_invalid_id() {
379443
wp_set_current_user( $this->admin_id );
380444

381-
$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', 100 ) );
445+
$request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', 99999 ) );
382446

383447
$response = $this->server->dispatch( $request );
384448
$response = rest_ensure_response( $response );

0 commit comments

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