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
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
- Scaling
- Scales with input
- Instructions
- 6–61
- Plugin surface
- None
- Called by
- 0
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 91.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($request) | 6 | none |
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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 91 | 6–61 | 12 | |
| 8.5 | 91 | 6–61 | 12 | |
| 8.4 | 91 | 6–61 | 12 | |
| 8.3 | 91 | 6–61 | 12 | |
| 8.2 | 91 | 6–61 | 12 | |
| 8.1 | 91 | 6–61 | 12 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 92 | 6–61 | 12 |
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
- wp_parse_list()Converts a comma- or space-separated list of scalar values to an array.
- wp_is_numeric_array()Determines if the variable is a numeric-indexed array.
- _rest_array_intersect_key_recursive()Recursively computes the intersection of arrays using keys for comparison.
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.
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.