wppaste
WordPress

WP_REST_Font_Collections_Controller::prepare_item_for_response( WP_Font_Collection $item, WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
6.5.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php:174
Prepare a single collection output for response.

Compatibility

WordPress
since 6.5.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_Font_Collection
Font collection object.
$requestWP_REST_Request
Request object.

Return value

WP_REST_Response|WP_Error
Response object on success, or WP_Error object on failure.

Performance profile

How much work a call to WP_REST_Font_Collections_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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
28–82

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

Plugin surface
1 hook

Third-party callbacks on 'rest_prepare_font_collection' 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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
array_intersect() && is_wp_error()28–31->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->get_data(), is_wp_error(), ->add_data()
!array_intersect() && ->is_method()31–34->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->is_method(), apply_filters()
array_intersect() && !is_wp_error() && ->is_method()38–41->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->get_data(), is_wp_error(), ->is_method(), apply_filters()
array_intersect() && !is_wp_error()44–48->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->get_data(), is_wp_error(), ->is_method(), ->is_method(), apply_filters()
!rest_is_field_included() && !array_intersect() && !->is_method()57–61->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->is_method(), rest_ensure_response(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters()
!array_intersect() && !->is_method() && rest_is_field_included()64–68->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->is_method(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters()
!rest_is_field_included() && array_intersect() && !is_wp_error() && !->is_method()70–75->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->get_data(), is_wp_error(), ->is_method(), ->is_method(), rest_ensure_response(), rest_is_field_included(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters()
array_intersect() && !is_wp_error() && !->is_method() && rest_is_field_included()77–82->get_fields_for_response(), rest_is_field_included(), array_intersect(), ->get_data(), is_wp_error(), ->is_method(), ->is_method(), rest_ensure_response(), rest_is_field_included(), ->prepare_links(), ->add_links(), ->add_additional_fields_to_object(), ->filter_response_by_context(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11628–8210
8.511628–8210
8.411628–8210
8.311628–8210
8.211628–8210
8.111628–82101 fewer instruction than PHP 7.4
7.411728–8310

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

3 hooks fire while WP_REST_Font_Collections_Controller::prepare_item_for_response() runs, in this order:

  1. apply_filters( rest_prepare_font_collection )filterline 197 (+23 into the body)

    Filters the font collection data for a REST API response.

  2. apply_filters( rest_prepare_font_collection )filterline 213 (+39 into the body)

    Filters the font collection data for a REST API response.

  3. apply_filters( rest_prepare_font_collection )filterline 236 (+62 into the body)

    Filters the font collection data for a REST API response.

Uses · 9

  • 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.
  • is_wp_error()Checks whether the given variable is a WordPress Error.
  • 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).
  • WP_REST_Font_Collections_Controller::get_fields_for_response()
  • WP_REST_Response::__construct()
  • WP_REST_Font_Collections_Controller::prepare_links()Prepares links for the request.
  • WP_REST_Font_Collections_Controller::add_additional_fields_to_object()
  • WP_REST_Font_Collections_Controller::filter_response_by_context()

Used by · 2

Source code

	public function prepare_item_for_response( $item, $request ) {		$fields = $this->get_fields_for_response( $request );		$data   = array(); 		if ( rest_is_field_included( 'slug', $fields ) ) {			$data['slug'] = $item->slug;		} 		// If any data fields are requested, get the collection data.		$data_fields = array( 'name', 'description', 'font_families', 'categories' );		if ( ! empty( array_intersect( $fields, $data_fields ) ) ) {			$collection_data = $item->get_data();			if ( is_wp_error( $collection_data ) ) {				$collection_data->add_data( array( 'status' => 500 ) );				return $collection_data;			} 			/**			 * Don't prepare the response body for HEAD requests.			 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.			 */			if ( $request->is_method( 'HEAD' ) ) {				/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */				return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );			} 			foreach ( $data_fields as $field ) {				if ( rest_is_field_included( $field, $fields ) ) {					$data[ $field ] = $collection_data[ $field ];				}			}		} 		/**		 * Don't prepare the response body for HEAD requests.		 * Can't exit at the beginning of the method due to the potential need to return a WP_Error object.		 */		if ( $request->is_method( 'HEAD' ) ) {			/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */			return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );		} 		$response = rest_ensure_response( $data ); 		if ( rest_is_field_included( '_links', $fields ) ) {			$links = $this->prepare_links( $item );			$response->add_links( $links );		} 		$context        = ! empty( $request['context'] ) ? $request['context'] : 'view';		$response->data = $this->add_additional_fields_to_object( $response->data, $request );		$response->data = $this->filter_response_by_context( $response->data, $context ); 		/**		 * Filters the font collection data for a REST API response.		 *		 * @since 6.5.0		 *		 * @param WP_REST_Response   $response The response object.		 * @param WP_Font_Collection $item     The font collection object.		 * @param WP_REST_Request    $request  Request used to generate the response.		 */		return apply_filters( 'rest_prepare_font_collection', $response, $item, $request );	}

Changelog

Introduced in 6.5.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-font-collections-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.