WP_REST_Terms_Controller::prepare_item_for_response( WP_Term $item, WP_REST_Request $request ): WP_REST_Response
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php:901
Compatibility
- WordPress
- since 4.7.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_Term- Term object.
$requestWP_REST_Request- Request object.
Return value
WP_REST_Response- Response object.
Performance profile
How much work a call to WP_REST_Terms_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
- Heavy
- Scaling
- Constant
- Instructions
- 18–120
- Plugin surface
- 1 hook
- Called by
- 6
Reaches the database via get_term().
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 133.
Third-party callbacks on 'rest_prepare_{$this->taxonomy}' run inside this call, and their cost is not bounded by anything here.
6 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 - querycontent query
get_term()one call below WP_REST_Terms_Controller::prepare_item_for_response()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Terms_Controller::prepare_item_for_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
->is_method() | 18 | ->is_method(), apply_filters() |
!->is_method() && !$fields && !rest_is_field_included() | 75–100 | ->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() && !$fields && rest_is_field_included() | 76–101 | ->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() && !rest_is_field_included() | 80–105 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
!->is_method() && !$fields | 81–106 | ->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() && rest_is_field_included() | 81–106 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
!->is_method() && !rest_is_field_included() | 84–109 | ->is_method(), ->get_fields_for_response(), ->get_value(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
!->is_method() && rest_is_field_included() | 85–110 | ->is_method(), ->get_fields_for_response(), ->get_value(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
!->is_method() | 86–111 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->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() && $fields && !rest_is_field_included() | 89–114 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->get_value(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), rest_is_field_included(), apply_filters() |
!->is_method() | 90–115 | ->is_method(), ->get_fields_for_response(), ->get_value(), ->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() && $fields && rest_is_field_included() | 90–115 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->get_value(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), apply_filters() |
1 further outcome, up to 120 instructions
!->is_method() && $fields | 95–120 | ->is_method(), ->get_fields_for_response(), get_term_link(), ->get_value(), ->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() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 133 | 18–120 | 13 | |
| 8.5 | 133 | 18–120 | 13 | |
| 8.4 | 133 | 18–120 | 13 | 27 fewer instructions than PHP 8.3 |
| 8.3 | 160 | 18–147 | 13 | |
| 8.2 | 160 | 18–147 | 13 | |
| 8.1 | 160 | 18–147 | 13 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 161 | 18–148 | 13 |
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_Terms_Controller::prepare_item_for_response() runs, in this order:
- apply_filters( rest_prepare_{$this->taxonomy} )filterline 906 (+5 into the body)
Filters the term data for a REST API response.
- apply_filters( rest_prepare_{$this->taxonomy} )filterline 976 (+75 into the body)
Filters the term data for a REST API response.
Uses · 9
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_term_link()Generates a permalink for a taxonomy term archive.
- 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_Terms_Controller::get_fields_for_response()
- WP_REST_Terms_Controller::add_additional_fields_to_object()
- WP_REST_Terms_Controller::filter_response_by_context()
- WP_REST_Terms_Controller::prepare_links()Prepares links for the request.
Used by · 6
- WP_REST_Menus_Controller::prepare_item_for_response()Prepares a single term output for response.
- WP_REST_Terms_Controller::create_item()Creates a single term in a taxonomy.
- WP_REST_Terms_Controller::delete_item()Deletes a single term from a taxonomy.
- WP_REST_Terms_Controller::get_item()Gets a single term from a taxonomy.
- WP_REST_Terms_Controller::get_items()Retrieves terms associated with a taxonomy.
- WP_REST_Terms_Controller::update_item()Updates a single term from a taxonomy.
Source code
public function prepare_item_for_response( $item, $request ) { // 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-terms-controller.php */ return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response( array() ), $item, $request ); } $fields = $this->get_fields_for_response( $request ); $data = array(); if ( in_array( 'id', $fields, true ) ) { $data['id'] = (int) $item->term_id; } if ( in_array( 'count', $fields, true ) ) { $data['count'] = (int) $item->count; } if ( in_array( 'description', $fields, true ) ) { $data['description'] = $item->description; } if ( in_array( 'link', $fields, true ) ) { $data['link'] = get_term_link( $item ); } if ( in_array( 'name', $fields, true ) ) { $data['name'] = $item->name; } if ( in_array( 'slug', $fields, true ) ) { $data['slug'] = $item->slug; } if ( in_array( 'taxonomy', $fields, true ) ) { $data['taxonomy'] = $item->taxonomy; } if ( in_array( 'parent', $fields, true ) ) { $data['parent'] = (int) $item->parent; } if ( in_array( 'meta', $fields, true ) ) { $data['meta'] = $this->meta->get_value( $item->term_id, $request ); } $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 ) ) { $response->add_links( $this->prepare_links( $item ) ); } /** * Filters the term data for a REST API response. * * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. * * Possible hook names include: * * - `rest_prepare_category` * - `rest_prepare_post_tag` * * Allows modification of the term data right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Term $item The original term object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $item, $request ); }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.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-terms-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.