Skip to content

Commit 5b2ee49

Browse files
REST API: Let routes turn off _fields, _embed and response links.
Replace the `allow_fields`, `allow_embed` and `allow_links` options with one `response_processing` route and handler option. It is `false` to serve the response data as returned, or an array with `fields`, `embed` and `links` keys to turn off single features: the `_fields` parameter filtering the data, the `_embed` parameter embedding linked resources, and the response links being added to the data as `_links`. Each feature resolves on its own, the handler setting over the route setting, and `embed` is off whenever `links` is off. The settings are resolved once, when the request is matched, and are read from the request attributes and the matched handler by the server, by `rest_filter_response_fields()` and by `WP_REST_Controller::get_fields_for_response()`. A response returned by the `rest_pre_dispatch` filter follows the settings of the route it was returned for. The index lists the settings of an endpoint that turns off a feature, and `rest_preload_api_request()` no longer casts the data to an array. See #66267.
1 parent 7a5e128 commit 5b2ee49

5 files changed

Lines changed: 614 additions & 133 deletions

File tree

‎src/wp-includes/rest-api.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ function _rest_array_intersect_key_recursive( $array1, $array2 ) {
950950
* Filters the REST API response to include only an allow-listed set of response object fields.
951951
*
952952
* @since 4.8.0
953-
* @since 7.2.0 The response is left unchanged when the route disables `allow_fields`.
953+
* @since 7.2.0 The response is left unchanged when the route turns off the `fields` response processing.
954954
*
955955
* @param WP_REST_Response $response Current response being served.
956956
* @param WP_REST_Server $server ResponseHandler instance (usually WP_REST_Server).
@@ -962,8 +962,12 @@ function rest_filter_response_fields( $response, $server, $request ) {
962962
return $response;
963963
}
964964

965-
if ( $server instanceof WP_REST_Server && ! $server->is_response_option_enabled( $response, 'allow_fields' ) ) {
966-
return $response;
965+
if ( $server instanceof WP_REST_Server ) {
966+
$processing = $server->get_response_processing( $request, $response );
967+
968+
if ( ! $processing['fields'] ) {
969+
return $response;
970+
}
967971
}
968972

969973
$data = $response->get_data();
@@ -3053,7 +3057,7 @@ function rest_preload_api_request( $memo, $path ) {
30533057
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
30543058
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request );
30553059
$embed = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false;
3056-
$data = (array) $server->response_to_data( $response, $embed );
3060+
$data = $server->response_to_data( $response, $embed, $request );
30573061

30583062
if ( 'OPTIONS' === $method ) {
30593063
$memo[ $method ][ $path ] = array(

0 commit comments

Comments
 (0)