wppaste
WordPress

rest_filter_response_by_context( array|object $response_data, array $schema, string $context ): array|object

Since
5.5.0, 5.6.0
Source
wp-includes/rest-api.php:3087
Filters the response to remove any fields not available in the given context.

Compatibility

WordPress
since 5.6.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

$response_dataarray|object
The response data to modify.
$schemaarray
The schema for the endpoint used to filter the response.
$contextstring
The requested context.

Return value

array|object
The filtered response data.

Performance profile

How much work a call to rest_filter_response_by_context() 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
13–36

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below rest_filter_response_by_context()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

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

WhenInstructionsCalls it makes
!->next_token()13->set_bookmark(), ->next_token(), ->get_tag()
!->next_token() && !->is_tag_closer()16->set_bookmark(), ->next_token(), ->get_tag(), ->is_tag_closer()
->next_token()17->set_bookmark(), ->next_token(), ->get_token_name(), ->get_tag()
->next_token() && !->is_tag_closer()20->set_bookmark(), ->next_token(), ->get_token_name(), ->get_tag(), ->is_tag_closer()
!->next_token() && ->is_tag_closer()32->set_bookmark(), ->next_token(), ->get_tag(), ->is_tag_closer(), ->set_bookmark()
->next_token() && ->is_tag_closer()36->set_bookmark(), ->next_token(), ->get_token_name(), ->get_tag(), ->is_tag_closer(), ->set_bookmark()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev19713–3638
8.519713–363837 more instructions than PHP 8.4
8.416014–75349 fewer instructions than PHP 8.3
8.316914–8434
8.216914–8434
8.116914–8434
7.416914–8434

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 · 6

Used by · 2

Source code

function rest_filter_response_by_context( $response_data, $schema, $context ) {	if ( isset( $schema['anyOf'] ) ) {		$matching_schema = rest_find_any_matching_schema( $response_data, $schema, '' );		if ( ! is_wp_error( $matching_schema ) ) {			if ( ! isset( $schema['type'] ) ) {				$schema['type'] = $matching_schema['type'];			} 			$response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context );		}	} 	if ( isset( $schema['oneOf'] ) ) {		$matching_schema = rest_find_one_matching_schema( $response_data, $schema, '', true );		if ( ! is_wp_error( $matching_schema ) ) {			if ( ! isset( $schema['type'] ) ) {				$schema['type'] = $matching_schema['type'];			} 			$response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context );		}	} 	if ( ! is_array( $response_data ) && ! is_object( $response_data ) ) {		return $response_data;	} 	if ( isset( $schema['type'] ) ) {		$type = $schema['type'];	} elseif ( isset( $schema['properties'] ) ) {		$type = 'object'; // Back compat if a developer accidentally omitted the type.	} else {		return $response_data;	} 	$is_array_type  = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );	$is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ); 	if ( $is_array_type && $is_object_type ) {		if ( rest_is_array( $response_data ) ) {			$is_object_type = false;		} else {			$is_array_type = false;		}	} 	$has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ); 	foreach ( $response_data as $key => $value ) {		$check = array(); 		if ( $is_array_type ) {			$check = $schema['items'] ?? array();		} elseif ( $is_object_type ) {			if ( isset( $schema['properties'][ $key ] ) ) {				$check = $schema['properties'][ $key ];			} else {				$pattern_property_schema = rest_find_matching_pattern_property_schema( $key, $schema );				if ( null !== $pattern_property_schema ) {					$check = $pattern_property_schema;				} elseif ( $has_additional_properties ) {					$check = $schema['additionalProperties'];				}			}		} 		if ( ! isset( $check['context'] ) ) {			continue;		} 		if ( ! in_array( $context, $check['context'], true ) ) {			if ( $is_array_type ) {				// All array items share schema, so there's no need to check each one.				$response_data = array();				break;			} 			if ( is_object( $response_data ) ) {				unset( $response_data->$key );			} else {

Changelog

Introduced in 5.5.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.

5.6.0
Support the "patternProperties" keyword for objects.
Support the "anyOf" and "oneOf" keywords.from the docblock
5.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.