#55213 closed enhancement (fixed)
Respect _fields query arg in preloaded requests
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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.
4 years ago
#1
This ticket was mentioned in Slack in #core-editor by jsnajdr. View the logs.
4 years ago
#4
@
4 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
@
4 years ago
- Owner set to gziolo
- Resolution set to fixed
- Status changed from new to closed
In 53217:
4 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
_fieldsquery 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
_fieldsparam is not respected and all fields are returned.This is because the
rest_preload_api_requestdoesn't callrest_filter_response_fieldson theresponseobject. For other requests this call happens inside therest_post_dispatchfilter, but this filter is not called when preloading responses.This patch adds the
rest_post_dispatchfilter call after every preloaded response. This filter also calls therest_send_allow_headerfunction to add theAllowheader, so we can also remove an explicit call when handling anOPTIONSrequest.Calling the
rest_post_dispatchfilter should be perfectly fine because it's already called in all similar circumstances:WP_REST_Server::serve_requestWP_REST_Server::serve_batch_request_v1WP_REST_Server::embed_linksOne extra effect of this patch is that preloaded responses for
GETrequests will also includeheaders, while previously they would only includebody, andheaderswould be present only forOPTIONSresponses. Before:{ '/': { body: { home, title } }, 'OPTIONS': { '/': { body: { home, title }, headers: { Allow } } } }After:
{ '/': { body: { home, title }, headers: { Allow } }, 'OPTIONS': { '/': { body: { home, title }, headers: { Allow } } } }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