8000 Ensure `GET /wp/v2/categories` with out of bounds offset doesn't return · WP-API/WP-API@4a0e3e5 · GitHub
[go: up one dir, main page]

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

Commit 4a0e3e5

Browse files
Ensure GET /wp/v2/categories with out of bounds offset doesn't return
results
1 parent cb94a9d commit 4a0e3e5

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ public function get_items( $request ) {
155155
unset( $count_args['offset'] );
156156
$total_terms = wp_count_terms( $this->taxonomy, $count_args );
157157

158+
// Ensure we don't return results when offset is out of bounds
159+
// see https://core.trac.wordpress.org/ticket/35935
160+
if ( $prepared_args['offset'] >= $total_terms ) {
161+
$query_result = array();
162+
}
163+
158164
// wp_count_terms can return a falsy value when the term has no children
159165
if ( ! $total_terms ) {
160166
$total_terms = 0;

tests/test-rest-categories-controller.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public function test_get_terms_pagination_headers() {
440440
$headers = $response->get_headers();
441441
$this->assertEquals( 50, $headers['X-WP-Total'] );
442442
$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
443+
$this->assertCount( 10, $response->get_data() );
443444
$next_link = add_query_arg( array(
444445
'page' => 2,
445446
), rest_url( '/wp/v2/categories' ) );
@@ -455,6 +456,7 @@ public function test_get_terms_pagination_headers() {
455456
$headers = $response->get_headers();
456457
$this->assertEquals( 51, $headers['X-WP-Total'] );
457458
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
459+
$this->assertCount( 10, $response->get_data() );
458460
$prev_link = add_query_arg( array(
459461
'page' => 2,
460462
), rest_url( '/wp/v2/categories' ) );
@@ -470,6 +472,7 @@ public function test_get_terms_pagination_headers() {
470472
$headers = $response->get_headers();
471473
$this->assertEquals( 51, $headers['X-WP-Total'] );
472474
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
475+
$this->assertCount( 1, $response->get_data() );
473476
$prev_link = add_query_arg( array(
474477
'page' => 5,
475478
), rest_url( '/wp/v2/categories' ) );
@@ -482,6 +485,7 @@ public function test_get_terms_pagination_headers() {
482485
$headers = $response->get_headers();
483486
$this->assertEquals( 51, $headers['X-WP-Total'] );
484487
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
488+
$this->assertCount( 0, $response->get_data() );
485489
$prev_link = add_query_arg( array(
486490
'page' => 6,
487491
), rest_url( '/wp/v2/categories' ) );

0 commit comments

Comments
 (0)
0