wppaste
WordPress

WP_REST_Taxonomies_Controller::prepare_item_for_response( WP_Taxonomy $item, WP_REST_Request $request ): WP_REST_Response

Since
4.7.0, 5.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php:209
Prepares a taxonomy object for serialization.

Compatibility

WordPress
since 5.9.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_Taxonomy
Taxonomy data.
$requestWP_REST_Request
Full details about the request.

Return value

WP_REST_Response
Response object.

Performance profile

How much work a call to WP_REST_Taxonomies_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
80–133

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

Plugin surface
1 hook

Third-party callbacks on 'rest_prepare_taxonomy' run inside this call, and their cost is not bounded by anything here.

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 callbacksapply_filters()called directly

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 WP_REST_Taxonomies_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($taxonomy) && !$fields && !rest_is_field_included()80–121->get_fields_for_response(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters()
empty($taxonomy) && !$fields && rest_is_field_included()81–122->get_fields_for_response(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters()
empty($taxonomy) && !rest_is_field_included()86–127->get_fields_for_response(), array_values(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters()
empty($taxonomy) && !$fields86–127->get_fields_for_response(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters()
empty($taxonomy) && rest_is_field_included()87–128->get_fields_for_response(), array_values(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters()
empty($taxonomy)92–133->get_fields_for_response(), array_values(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14280–13315
8.514280–13315
8.414280–1331533 fewer instructions than PHP 8.3
8.3175113–16615
8.2175113–16615
8.1175113–166153 fewer instructions than PHP 7.4
7.4178114–16815

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.

Hooks and filters fired · 1

One hook fires while WP_REST_Taxonomies_Controller::prepare_item_for_response() runs, in this order:

  1. apply_filters( rest_prepare_taxonomy )filterline 291 (+82 into the body)

    Filters a taxonomy returned from the REST API.

Uses · 7

  • rest_ensure_response()Ensures a REST response is a response object (for consistency).
  • 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.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • WP_REST_Taxonomies_Controller::get_fields_for_response()
  • WP_REST_Taxonomies_Controller::add_additional_fields_to_object()
  • WP_REST_Taxonomies_Controller::filter_response_by_context()
  • WP_REST_Taxonomies_Controller::prepare_links()Prepares links for the request.

Used by · 2

Source code

	public function prepare_item_for_response( $item, $request ) {		// Restores the more descriptive, specific name for use within this method.		$taxonomy = $item; 		$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 		$fields = $this->get_fields_for_response( $request );		$data   = array(); 		if ( in_array( 'name', $fields, true ) ) {			$data['name'] = $taxonomy->label;		} 		if ( in_array( 'slug', $fields, true ) ) {			$data['slug'] = $taxonomy->name;		} 		if ( in_array( 'capabilities', $fields, true ) ) {			$data['capabilities'] = $taxonomy->cap;		} 		if ( in_array( 'description', $fields, true ) ) {			$data['description'] = $taxonomy->description;		} 		if ( in_array( 'labels', $fields, true ) ) {			$data['labels'] = $taxonomy->labels;		} 		if ( in_array( 'types', $fields, true ) ) {			$data['types'] = array_values( $taxonomy->object_type );		} 		if ( in_array( 'show_cloud', $fields, true ) ) {			$data['show_cloud'] = $taxonomy->show_tagcloud;		} 		if ( in_array( 'hierarchical', $fields, true ) ) {			$data['hierarchical'] = $taxonomy->hierarchical;		} 		if ( in_array( 'rest_base', $fields, true ) ) {			$data['rest_base'] = $base;		} 		if ( in_array( 'rest_namespace', $fields, true ) ) {			$data['rest_namespace'] = $taxonomy->rest_namespace;		} 		if ( in_array( 'visibility', $fields, true ) ) {			$data['visibility'] = array(				'public'             => (bool) $taxonomy->public,				'publicly_queryable' => (bool) $taxonomy->publicly_queryable,				'show_admin_column'  => (bool) $taxonomy->show_admin_column,				'show_in_nav_menus'  => (bool) $taxonomy->show_in_nav_menus,				'show_in_quick_edit' => (bool) $taxonomy->show_in_quick_edit,				'show_ui'            => (bool) $taxonomy->show_ui,			);		} 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';		$data    = $this->add_additional_fields_to_object( $data, $request );		$data    = $this->filter_response_by_context( $data, $context ); 		// Wrap the data in a response object.		$response = rest_ensure_response( $data ); 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {			$response->add_links( $this->prepare_links( $taxonomy ) );		} 		/**		 * Filters a taxonomy returned from the REST API.		 *		 * Allows modification of the taxonomy data right before it is returned.		 *		 * @since 4.7.0		 *		 * @param WP_REST_Response $response The response object.		 * @param WP_Taxonomy      $item     The original taxonomy object.

Changelog

Introduced in 4.7.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.9.0
Renamed $taxonomy to $item to match parent class for PHP 8 named parameter support.from the docblock
4.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-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.