Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit unbounded per_page=-1 requests for Pages and Shared Blocks #6657

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion editor/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const applyWithAPIDataItems = withAPIData( ( props, { type } ) => {
const isHierarchical = get( props, [ 'postType', 'hierarchical' ], false );
const queryString = stringify( {
context: 'edit',
per_page: 100,
per_page: -1,
exclude: postId,
parent_exclude: postId,
_fields: [ 'id', 'parent', 'title' ],
Expand Down
2 changes: 1 addition & 1 deletion editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export default {
if ( id ) {
result = wp.apiRequest( { path: `/wp/v2/${ basePath }/${ id }` } );
} else {
result = wp.apiRequest( { path: `/wp/v2/${ basePath }` } );
result = wp.apiRequest( { path: `/wp/v2/${ basePath }?per_page=-1` } );
}

result.then(
Expand Down
54 changes: 52 additions & 2 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ function gutenberg_register_post_prepare_functions( $post_type ) {
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_permalink_template_to_posts', 10, 3 );
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_block_format_to_post_content', 10, 3 );
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_target_schema_to_links', 10, 3 );
add_filter( "rest_{$post_type}_collection_params", 'gutenberg_filter_post_collection_parameters', 10, 2 );
add_filter( "rest_{$post_type}_query", 'gutenberg_filter_post_query_arguments', 10, 2 );
return $post_type;
}
add_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' );
Expand Down Expand Up @@ -423,7 +425,12 @@ function gutenberg_ensure_wp_json_has_theme_supports( $response ) {
* @param WP_REST_Request $request Request used to generate the response.
*/
function gutenberg_handle_early_callback_checks( $response, $handler, $request ) {
if ( '/wp/v2/users' === $request->get_route() ) {
$routes = array(
'/wp/v2/blocks',
'/wp/v2/pages',
'/wp/v2/users',
);
if ( in_array( $request->get_route(), $routes ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should perform a strict check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$can_view_authors = false;
$can_unbounded_query = false;
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
Expand All @@ -440,7 +447,8 @@ function gutenberg_handle_early_callback_checks( $response, $handler, $request )
return new WP_Error( 'rest_forbidden_per_page', __( 'Sorry, you are not allowed make unbounded queries.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) );
}
}
if ( ! empty( $request['who'] ) && 'authors' === $request['who'] ) {
if ( '/wp/v2/users' === $request->get_route()
&& ! empty( $request['who'] ) && 'authors' === $request['who'] ) {
if ( ! $can_view_authors ) {
return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) );
}
Expand All @@ -450,6 +458,48 @@ function gutenberg_handle_early_callback_checks( $response, $handler, $request )
}
add_filter( 'rest_request_before_callbacks', 'gutenberg_handle_early_callback_checks', 10, 3 );

/**
* Include additional query parameters on the posts query endpoint.
*
* @see https://core.trac.wordpress.org/ticket/43998
*
* @param array $query_params JSON Schema-formatted collection parameters.
* @param string $post_type Post type being accessed.
* @return array
*/
function gutenberg_filter_post_collection_parameters( $query_params, $post_type ) {
$post_types = array( 'page', 'wp_block' );
if ( in_array( $post_type->name, $post_types, true )
&& isset( $query_params['per_page'] ) ) {
// Change from '1' to '-1', which means unlimited.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand this comment; where is '1' being checked for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any checks at all for minimum though.

Maybe I just don't get the query params variable here but it looks like it's a dictionary and here you're just checking for $query_params['per_page'], not $query_params['per_page']['minimum'].

It would be at least handy to say // Change from default value '1' to '-1', which means unlimited.

That said this code strikes me as assuming minimum is set to the default if set at all which I can't tell from here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow. Can you create a PR against this PR with your suggested changes?

Copy link
Member

@tofumatt tofumatt May 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, I understand the code now but it was not obvious at first. I'd suggest changing this function to add a few more comments for context:

function gutenberg_filter_post_collection_parameters( $query_params, $post_type ) {
	$post_types = array( 'page', 'wp_block' );
	// HACK: If 'per_page' is set, ignore it and change the items returned
	// per-page to be '-1'.
	// See: https://github.com/WordPress/gutenberg/issues/6180#issuecomment-384511059
	if ( in_array( $post_type->name, $post_types, true )
		&& isset( $query_params['per_page'] ) ) {
		// Change from '1' (default) to '-1', which means unlimited.
		$query_params['per_page']['minimum'] = -1;
		// Default sanitize callback is 'absint', which won't work in our case.
		$query_params['per_page']['sanitize_callback'] = 'rest_sanitize_request_arg';
	}
	return $query_params;
}

That seems to be the intent of the code, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that comment is right. This is adjusting the collection params schema, not the actual request arguments. The schema ( per_page) is validated by WordPress itself in WP_REST_Request using rest_validate_value_from_schema.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha; the structure of these filters is unclear to me as I'm new to the project. Thanks for the explanation 👍

$query_params['per_page']['minimum'] = -1;
// Default sanitize callback is 'absint', which won't work in our case.
$query_params['per_page']['sanitize_callback'] = 'rest_sanitize_request_arg';
}
return $query_params;
}

/**
* Filter post collection query parameters to include specific behavior.
*
* @see https://core.trac.wordpress.org/ticket/43998
*
* @param array $prepared_args Array of arguments for WP_Query.
* @param WP_REST_Request $request The current request.
* @return array
*/
function gutenberg_filter_post_query_arguments( $prepared_args, $request ) {
$post_types = array( 'page', 'wp_block' );
if ( in_array( $prepared_args['post_type'], $post_types, true ) ) {
// Avoid triggering 'rest_post_invalid_page_number' error
// which will need to be addressed in https://core.trac.wordpress.org/ticket/43998.
if ( -1 === $prepared_args['posts_per_page'] ) {
$prepared_args['posts_per_page'] = 100000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a hack that will be hard to debug as there isn't any logging around it.

Also: it seems unbounded requests are only allowed for authorised users... seems like they need edit_posts to be able to use this feature but just wanted to make sure that's also a requirement to use Gutenberg (sorry, I'm new here!) or this will have unpredictable results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a hack that will be hard to debug as there isn't any logging around it.

Yes. It's a hack that will only exist until the patch I've yet to prepare for https://core.trac.wordpress.org/ticket/43998 lands in WP 4.9.7

Also: it seems unbounded requests are only allowed for authorised users... seems like they need edit_posts to be able to use this feature but just wanted to make sure that's also a requirement to use Gutenberg (sorry, I'm new here!) or this will have unpredictable results.

Correct. edit_posts is a reasonable minimum capability assumption for Gutenberg.

}
}
return $prepared_args;
}

/**
* Include additional query parameters on the user query endpoint.
*
Expand Down
20 changes: 20 additions & 0 deletions phpunit/class-gutenberg-rest-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,24 @@ public function test_get_items_unbounded_per_page_unauthorized() {
$data = $response->get_data();
$this->assertEquals( 'rest_forbidden_per_page', $data['code'] );
}

public function test_get_pages_unbounded_per_page() {
wp_set_current_user( $this->author );
$this->factory->post->create( array( 'post_type' => 'page' ) );
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
$request->set_param( 'per_page', '-1' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}

public function test_get_pages_unbounded_per_page_unauthorized() {
wp_set_current_user( $this->subscriber );
$this->factory->post->create( array( 'post_type' => 'page' ) );
$request = new WP_REST_Request( 'GET', '/wp/v2/pages' );
$request->set_param( 'per_page', '-1' );
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 403, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( 'rest_forbidden_per_page', $data['code'] );
}
}