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
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
- Scaling
- Scales with input
- Instructions
- 13–36
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 197.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 197 | 13–36 | 38 | |
| 8.5 | 197 | 13–36 | 38 | 37 more instructions than PHP 8.4 |
| 8.4 | 160 | 14–75 | 34 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 169 | 14–84 | 34 | |
| 8.2 | 169 | 14–84 | 34 | |
| 8.1 | 169 | 14–84 | 34 | |
| 7.4 | 169 | 14–84 | 34 |
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
- rest_find_any_matching_schema()Finds the matching schema among the "anyOf" schemas.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- rest_filter_response_by_context()Filters the response to remove any fields not available in the given context.
- rest_find_one_matching_schema()Finds the matching schema among the "oneOf" schemas.
- rest_is_array()Determines if a given value is array-like.
- rest_find_matching_pattern_property_schema()Finds the schema for a property using the patternProperties keyword.
Used by · 2
- WP_REST_Controller::filter_response_by_context()Filters a response based on the context defined in the schema.
- rest_filter_response_by_context()Filters the response to remove any fields not available in the given context.
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.
Signature, return type and hooks compared across 5 parsed releases.
Support the "anyOf" and "oneOf" keywords.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.