wppaste
WordPress

WP_REST_Navigation_Fallback_Controller::prepare_item_for_response( WP_Post $item, WP_REST_Request $request ): WP_REST_Response

Since
6.3.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php:152
Matches the post data to the schema we want.

Compatibility

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

$itemWP_Post
The wp_navigation Post object whose response is being prepared.
$requestWP_REST_Request
Request object.

Return value

WP_REST_Response
$response The response data.

Performance profile

How much work a call to WP_REST_Navigation_Fallback_Controller::prepare_item_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
41–53

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

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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
!rest_is_field_included()41–46->get_fields_for_response(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included()
rest_is_field_included()43–48->get_fields_for_response(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links()
always48–53->get_fields_for_response(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5441–534
8.55441–534
8.45441–534
8.35441–534
8.25441–534
8.15441–5341 fewer instruction than PHP 7.4
7.45541–544

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_is_field_included()Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.
  • rest_ensure_response()Ensures a REST response is a response object (for consistency).
  • WP_REST_Navigation_Fallback_Controller::get_fields_for_response()
  • WP_REST_Navigation_Fallback_Controller::add_additional_fields_to_object()
  • WP_REST_Navigation_Fallback_Controller::filter_response_by_context()
  • WP_REST_Navigation_Fallback_Controller::prepare_links()Prepares the links for the request.

Used by · 1

Source code

	public function prepare_item_for_response( $item, $request ) {		$data = array(); 		$fields = $this->get_fields_for_response( $request ); 		if ( rest_is_field_included( 'id', $fields ) ) {			$data['id'] = (int) $item->ID;		} 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';		$data    = $this->add_additional_fields_to_object( $data, $request );		$data    = $this->filter_response_by_context( $data, $context ); 		$response = rest_ensure_response( $data ); 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {			$links = $this->prepare_links( $item );			$response->add_links( $links );		} 		return $response;	}

Changelog

Introduced in 6.3.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 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-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.