#55213 closed enhancement (fixed)
Respect _fields query arg in preloaded requests
Reported by: | jsnajdr | Owned by: | gziolo |
---|---|---|---|
Milestone: | 6.0 | Priority: | normal |
Severity: | normal | Version: | 5.9 |
Component: | REST API | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
Suppose a preloaded request includes a _fields
query param that asks that only selected response fields are returned. You can add such a request with a filter:
function add_filtered_index_request( $preload_paths ) {
array_push( $preload_paths, '/?_fields=home,title' );
return $preload_paths;
}
add_filter( 'block_editor_rest_api_preload_paths', 'add_filtered_index_request' );
However, you will discover that the _fields
param is not respected and all fields are returned.
This is because the rest_preload_api_request
doesn't call rest_filter_response_fields
on the response object. For other requests this call happens inside the rest_post_dispatch filter
, but this filter is not called when preloading responses.
Change History (7)
This ticket was mentioned in PR #2340 on WordPress/wordpress-develop by jsnajdr.
3 years ago
#1
This ticket was mentioned in Slack in #core-editor by jsnajdr. View the logs.
3 years ago
#4
@
3 years ago
- Milestone changed from 6.0 to 6.1
With 6.0 Beta 1 released yesterday, I'm moving this ticket to the 6.1 milestone.
#6
@
3 years ago
- Owner set to gziolo
- Resolution set to fixed
- Status changed from new to closed
In 53217:
3 years ago
#7
Committed with https://core.trac.wordpress.org/changeset/53217.
Fixes https://core.trac.wordpress.org/ticket/55213
Suppose a preloaded request includes a
_fields
query param that asks that only selected response fields are returned. You can add such a request with a filter:{{{php
function add_filtered_index_request( $preload_paths ) {
}
add_filter( 'block_editor_rest_api_preload_paths', 'add_filtered_index_request' );
}}}
However, you will discover that the
_fields
param is not respected and all fields are returned.This is because the
rest_preload_api_request
doesn't callrest_filter_response_fields
on theresponse
object. For other requests this call happens inside therest_post_dispatch
filter, but this filter is not called when preloading responses.This patch adds the
rest_post_dispatch
filter call after every preloaded response. This filter also calls therest_send_allow_header
function to add theAllow
header, so we can also remove an explicit call when handling anOPTIONS
request.Calling the
rest_post_dispatch
filter should be perfectly fine because it's already called in all similar circumstances:WP_REST_Server::serve_request
WP_REST_Server::serve_batch_request_v1
WP_REST_Server::embed_links
One extra effect of this patch is that preloaded responses for
GET
requests will also includeheaders
, while previously they would only includebody
, andheaders
would be present only forOPTIONS
responses. Before:After:
Including these headers is a good and desirable thing because it allows us to get rid of redundant OPTIONS request where we already have GET response. The GET response would now already contain the desired info about permissions. See also this discussion on a Gutenberg PR: https://github.com/WordPress/gutenberg/pull/38901#issuecomment-1044340843
In principle this patch is similar to what @TimothyBJacobs did some time ago for embeds: https://core.trac.wordpress.org/ticket/51722