-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
|
@@ -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 ) ) { | ||
$can_view_authors = false; | ||
$can_unbounded_query = false; | ||
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' ); | ||
|
@@ -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() ) ); | ||
} | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand this comment; where is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It doesn't need to be checked for. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any checks at all for 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 It would be at least handy to say 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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
Correct. |
||
} | ||
} | ||
return $prepared_args; | ||
} | ||
|
||
/** | ||
* Include additional query parameters on the user query endpoint. | ||
* | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3cf6f5e