wppaste
WordPress

WP_REST_Controller::get_fields_for_response( WP_REST_Request $request ): string[]

Since
4.9.6
Source
wp-includes/rest-api/endpoints/class-wp-rest-controller.php:572
Gets an array of fields to be included on the response.

Description

Included fields are based on item schema and _fields= request argument.

Compatibility

WordPress
since 4.9.6
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

$requestWP_REST_Request
Full details about the request.

Return value

string[]
Fields to be included in the response.

Performance profile

How much work a call to WP_REST_Controller::get_fields_for_response() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–18

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
$field9none
!$field16–18explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1059–1815
8.51059–1815
8.41059–181512 fewer instructions than PHP 8.3
8.311712–2415
8.211712–2415
8.111712–241526 more instructions than PHP 7.4
7.49132–6913

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

Used by · 1

Source code

	public function get_fields_for_response( $request ) {		$schema     = $this->get_item_schema();		$properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); 		$additional_fields = $this->get_additional_fields(); 		foreach ( $additional_fields as $field_name => $field_options ) {			/*			 * For back-compat, include any field with an empty schema			 * because it won't be present in $this->get_item_schema().			 */			if ( is_null( $field_options['schema'] ) ) {				$properties[ $field_name ] = $field_options;			}		} 		// Exclude fields that specify a different context than the request context.		$context = $request['context'];		if ( $context ) {			foreach ( $properties as $name => $options ) {				if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) {					unset( $properties[ $name ] );				}			}		} 		$fields = array_keys( $properties ); 		/*		 * '_links' and '_embedded' are not typically part of the item schema,		 * but they can be specified in '_fields', so they are added here as a		 * convenience for checking with rest_is_field_included().		 */		$fields[] = '_links';		if ( $request->has_param( '_embed' ) ) {			$fields[] = '_embedded';		} 		$fields = array_unique( $fields ); 		if ( ! isset( $request['_fields'] ) ) {			return $fields;		}		$requested_fields = wp_parse_list( $request['_fields'] );		if ( 0 === count( $requested_fields ) ) {			return $fields;		}		// Trim off outside whitespace from the comma delimited list.		$requested_fields = array_map( 'trim', $requested_fields );		// Always persist 'id', because it can be needed for add_additional_fields_to_object().		if ( in_array( 'id', $fields, true ) ) {			$requested_fields[] = 'id';		}		// Return the list of all requested fields which appear in the schema.		return array_reduce(			$requested_fields,			static function ( $response_fields, $field ) use ( $fields ) {				if ( in_array( $field, $fields, true ) ) {					$response_fields[] = $field;					return $response_fields;				}				// Check for nested fields if $field is not a direct match.				$nested_fields = explode( '.', $field );				/*				 * A nested field is included so long as its top-level property				 * is present in the schema.				 */				if ( in_array( $nested_fields[0], $fields, true ) ) {					$response_fields[] = $field;				}				return $response_fields;			},			array()		);	}

Changelog

Introduced in 4.9.6. 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.9.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-controller.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.