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:213
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
- Scaling
- Constant
- Instructions
- 84–137
- Plugin surface
- 1 hook
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 156.
Third-party callbacks on 'rest_prepare_taxonomy' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!->is_method() && empty($taxonomy) && !$fields && !rest_is_field_included() | 84–125 | ->is_method(), ->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() |
!->is_method() && empty($taxonomy) && !$fields && rest_is_field_included() | 85–126 | ->is_method(), ->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() |
!->is_method() && empty($taxonomy) && !rest_is_field_included() | 90–131 | ->is_method(), ->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() |
!->is_method() && empty($taxonomy) && !$fields | 90–131 | ->is_method(), ->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() |
!->is_method() && empty($taxonomy) && rest_is_field_included() | 91–132 | ->is_method(), ->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() |
!->is_method() && empty($taxonomy) | 96–137 | ->is_method(), ->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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 156 | 84–137 | 16 | |
| 8.5 | 156 | 84–137 | 16 | |
| 8.4 | 156 | 84–137 | 16 | 33 fewer instructions than PHP 8.3 |
| 8.3 | 189 | 117–170 | 16 | |
| 8.2 | 189 | 117–170 | 16 | |
| 8.1 | 189 | 117–170 | 16 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 192 | 118–172 | 16 |
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 · 2
2 hooks fire while WP_REST_Taxonomies_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_taxonomy )filterline 220 (+7 into the body)
Filters a taxonomy returned from the REST API.
- apply_filters( rest_prepare_taxonomy )filterline 301 (+88 into the body)
Filters a taxonomy returned from the REST API.
Uses · 8
- apply_filters()Calls the callback functions that have been added to a filter hook.
- 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.
- WP_REST_Response::__construct()
- 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
- WP_REST_Taxonomies_Controller::get_item()Retrieves a specific taxonomy.
- WP_REST_Taxonomies_Controller::get_items()Retrieves all public taxonomies.
Source code
public function prepare_item_for_response( $item, $request ) { // Restores the more descriptive, specific name for use within this method. $taxonomy = $item; // Don't prepare the response body for HEAD requests. if ( $request->is_method( 'HEAD' ) ) { /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php */ return apply_filters( 'rest_prepare_taxonomy', new WP_REST_Response( array() ), $taxonomy, $request ); } $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. *Changelog
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$taxonomy to $item to match parent class for PHP 8 named parameter support.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.