WordPress.org

Make WordPress Core

Changeset 39967


Ignore:
Timestamp:
01/26/17 13:52:06 (4 weeks ago)
Author:
joehoyle
Message:

REST API: Return an error if the page number is out of bounds.

Return an error from the REST API if a page number larger than the total pages count is requested.

Props morganestes.
Fixes #39061.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r39954 r39967  
    328328 
    329329        $max_pages = ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] ); 
     330 
     331        if ( $page > $max_pages && $total_posts > 0 ) { 
     332            return new WP_Error( 'rest_post_invalid_page_number', __( 'The page number requested is larger than the number of pages available.' ), array( 'status' => 400 ) ); 
     333        } 
     334 
    330335        $response  = rest_ensure_response( $posts ); 
    331336 
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r39954 r39967  
    826826        $response = $this->server->dispatch( $request ); 
    827827        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 
     828    } 
     829 
     830    /** 
     831     * @ticket 39061 
     832     */ 
     833    public function test_get_items_invalid_max_pages() { 
     834        // Out of bounds 
     835        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 
     836        $request->set_param( 'page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 
     837        $response = $this->server->dispatch( $request ); 
     838        $this->assertErrorResponse( 'rest_post_invalid_page_number', $response, 400 ); 
    828839    } 
    829840 
Note: See TracChangeset for help on using the changeset viewer.