wppaste
WordPress

rest_filter_response_fields( WP_REST_Response $response, WP_REST_Server $server, WP_REST_Request $request ): WP_REST_Response

Since
4.8.0
Source
wp-includes/rest-api.php:929
Filters the REST API response to include only an allow-listed set of response object fields.

Compatibility

WordPress
since 4.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$responseWP_REST_Response
Current response being served.
$serverWP_REST_Server
ResponseHandler instance (usually WP_REST_Server).
$requestWP_REST_Request
The request that was used to make current response.

Return value

WP_REST_Response
Response to be served, trimmed down to contain a subset of fields.

Performance profile

How much work a call to rest_filter_response_fields() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–61

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 91.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_filter_response_fields() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($request)6none
isset($request) && ->is_error()9->is_error()
isset($request) && !->is_error()21->is_error(), ->get_data(), wp_parse_list()
isset($request) && !->is_error() && wp_is_numeric_array()40–42->is_error(), ->get_data(), wp_parse_list(), array_map(), wp_is_numeric_array(), ->set_data()
isset($request) && !->is_error() && !wp_is_numeric_array()41–42->is_error(), ->get_data(), wp_parse_list(), array_map(), wp_is_numeric_array(), _rest_array_intersect_key_recursive(), ->set_data()
isset($request) && !->is_error() && !$fields && isset($ref[$next]) && wp_is_numeric_array()60–61->is_error(), ->get_data(), wp_parse_list(), array_map(), explode(), array_shift(), wp_is_numeric_array(), ->set_data()
isset($request) && !->is_error() && !$fields && isset($ref[$next]) && !wp_is_numeric_array()61->is_error(), ->get_data(), wp_parse_list(), array_map(), explode(), array_shift(), wp_is_numeric_array(), _rest_array_intersect_key_recursive(), ->set_data()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev916–6112
8.5916–6112
8.4916–6112
8.3916–6112
8.2916–6112
8.1916–61121 fewer instruction than PHP 7.4
7.4926–6112

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 3

Source code

function rest_filter_response_fields( $response, $server, $request ) {	if ( ! isset( $request['_fields'] ) || $response->is_error() ) {		return $response;	} 	$data = $response->get_data(); 	$fields = wp_parse_list( $request['_fields'] ); 	if ( 0 === count( $fields ) ) {		return $response;	} 	// Trim off outside whitespace from the comma delimited list.	$fields = array_map( 'trim', $fields ); 	// Create nested array of accepted field hierarchy.	$fields_as_keyed = array();	foreach ( $fields as $field ) {		$parts = explode( '.', $field );		$ref   = &$fields_as_keyed;		while ( count( $parts ) > 1 ) {			$next = array_shift( $parts );			if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) {				// Skip any sub-properties if their parent prop is already marked for inclusion.				break 2;			}			$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();			$ref          = &$ref[ $next ];		}		$last         = array_shift( $parts );		$ref[ $last ] = true;	} 	if ( wp_is_numeric_array( $data ) ) {		$new_data = array();		foreach ( $data as $item ) {			$new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed );		}	} else {		$new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed );	} 	$response->set_data( $new_data ); 	return $response;}

Changelog

Introduced in 4.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.