wppaste
WordPress

WP_REST_Block_Directory_Controller::prepare_item_for_response( array $item, WP_REST_Request $request ): WP_REST_Response|WP_Error

Since
5.5.0, 5.9.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php:118
Parse block metadata for a block, and prepare it for an API response.

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

$itemarray
The plugin metadata.
$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_Block_Directory_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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
97–105

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()one call below WP_REST_Block_Directory_Controller::prepare_item_for_response()
  • hookthird-party callbacksapply_filters()one call below WP_REST_Block_Directory_Controller::prepare_item_for_response()

Further down the call graph this can also reach cache, serialize, query 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 · 3 distinct outcomes

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

WhenInstructionsCalls it makes
!rest_is_field_included()97–99->get_fields_for_response(), reset(), wp_trim_words(), wp_strip_all_tags(), strtotime(), gmdate(), __(), strtotime(), human_time_diff(), sprintf(), ->add_additional_fields_to_object(), name(), rest_is_field_included(), rest_is_field_included()
rest_is_field_included()98–100->get_fields_for_response(), reset(), wp_trim_words(), wp_strip_all_tags(), strtotime(), gmdate(), __(), strtotime(), human_time_diff(), sprintf(), ->add_additional_fields_to_object(), name(), rest_is_field_included(), ->prepare_links(), ->add_links()
always103–105->get_fields_for_response(), reset(), wp_trim_words(), wp_strip_all_tags(), strtotime(), gmdate(), __(), strtotime(), human_time_diff(), sprintf(), ->add_additional_fields_to_object(), name(), rest_is_field_included(), rest_is_field_included(), ->prepare_links(), ->add_links()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10697–1054
8.510697–1054
8.410697–1054
8.310697–1054
8.210697–1054
8.110697–10542 fewer instructions than PHP 7.4
7.410898–1064

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.

Uses · 9

  • wp_trim_words()Trims text to a certain number of words.
  • wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
  • __()Retrieves the translation of $text.
  • human_time_diff()Determines the difference between two timestamps.
  • 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_Block_Directory_Controller::get_fields_for_response()
  • WP_REST_Block_Directory_Controller::add_additional_fields_to_object()
  • WP_REST_Response::__construct()
  • WP_REST_Block_Directory_Controller::prepare_links()Generates a list of links to include in the response for the plugin.

Used by · 1

Source code

	public function prepare_item_for_response( $item, $request ) {		// Restores the more descriptive, specific name for use within this method.		$plugin = $item; 		$fields = $this->get_fields_for_response( $request ); 		// There might be multiple blocks in a plugin. Only the first block is mapped.		$block_data = reset( $plugin['blocks'] ); 		// A data array containing the properties we'll return.		$block = array(			'name'                => $block_data['name'],			'title'               => ( $block_data['title'] ? $block_data['title'] : $plugin['name'] ),			'description'         => wp_trim_words( $plugin['short_description'], 30, '...' ),			'id'                  => $plugin['slug'],			'rating'              => $plugin['rating'] / 20,			'rating_count'        => (int) $plugin['num_ratings'],			'active_installs'     => (int) $plugin['active_installs'],			'author_block_rating' => $plugin['author_block_rating'] / 20,			'author_block_count'  => (int) $plugin['author_block_count'],			'author'              => wp_strip_all_tags( $plugin['author'] ),			'icon'                => $plugin['icons']['1x'] ?? 'block-default',			'last_updated'        => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),			'humanized_updated'   => sprintf(				/* translators: %s: Human-readable time difference. */				__( '%s ago' ),				human_time_diff( strtotime( $plugin['last_updated'] ) )			),		); 		$this->add_additional_fields_to_object( $block, $request ); 		$response = new WP_REST_Response( $block ); 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {			$response->add_links( $this->prepare_links( $plugin ) );		} 		return $response;	}

Changelog

Introduced in 5.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.

5.9.0
Renamed $plugin to $item to match parent class for PHP 8 named parameter support.from the docblock
5.5.0
Introduced.from the docblock

About this page

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