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
2 changes: 2 additions & 0 deletions 2 lib/endpoints/class-wp-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ public function get_collection_params() {
'description' => __( 'Maximum number of items to be returned in result set.' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
Expand Down
6 changes: 6 additions & 0 deletions 6 plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ function rest_validate_request_arg( $value, $request, $param ) {
}
}

if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && isset( $args['minimum'] ) && isset( $args['maximum'] ) ) {
if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d and %d' ), $param, $args['minimum'], $args['maximum'] ) );
}
}

return true;
}
}
Expand Down
17 changes: 17 additions & 0 deletions 17 tests/test-rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ public function test_get_items_menu_order_query() {
$this->assertEquals( $id3, $data[3]['id'] );
}

public function test_get_items_min_max_pages_query() {
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
$request->set_param( 'per_page', 0 );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
$data = $response->get_data();
// Safe format for 4.4 and 4.5 https://core.trac.wordpress.org/ticket/35028
$first_error = array_shift( $data['data']['params'] );
$this->assertContains( 'per_page must be between 1 and 100', $first_error );
$request->set_param( 'per_page', 101 );
$response = $this->server->dispatch( $request );
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
$data = $response->get_data();
$first_error = array_shift( $data['data']['params'] );
$this->assertContains( 'per_page must be between 1 and 100', $first_error );
}

public function test_get_items_private_filter_query_var() {
// Private query vars inaccessible to unauthorized users
wp_set_current_user( 0 );
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.