wppaste
WordPress

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
Prepares a single term output for response.

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

Reaches the database via get_term().

Scaling
Constant

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

Instructions
18–120

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

Plugin surface
1 hook

Third-party callbacks on 'rest_prepare_{$this->taxonomy}' run inside this call, and their cost is not bounded by anything here.

Called by
6

6 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
  • querycontent queryget_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.

WhenInstructionsCalls 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() && !$fields81–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() && $fields95–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

PHPCompiledExecutedBranchesNotes
8.6-dev13318–12013
8.513318–12013
8.413318–1201327 fewer instructions than PHP 8.3
8.316018–14713
8.216018–14713
8.116018–147131 fewer instruction than PHP 7.4
7.416118–14813

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:

  1. apply_filters( rest_prepare_{$this->taxonomy} )filterline 906 (+5 into the body)

    Filters the term data for a REST API response.

  2. 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

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.

  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.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.