wppaste
WordPress

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

Since
6.0.0, 6.3.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php:164
Prepare a raw block pattern before it gets output in a REST API response.

Compatibility

WordPress
since 6.3.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
Raw pattern as registered, before any changes.
$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_Patterns_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
42–44

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

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

  • hookthird-party callbacksapply_filters()one call below WP_REST_Block_Patterns_Controller::prepare_item_for_response()

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always42–44parse_blocks(), resolve_pattern_blocks(), serialize_blocks(), ->get_fields_for_response(), ->add_additional_fields_to_object(), ->filter_response_by_context(), rest_ensure_response()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5742–445
8.55742–445
8.45742–445
8.35742–445
8.25742–445
8.15742–4451 fewer instruction than PHP 7.4
7.45842–455

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

  • parse_blocks()Parses blocks out of a content string.
  • resolve_pattern_blocks()Replaces patterns in a block tree with their content.
  • serialize_blocks()Returns a joined string of the aggregate serialization of the given parsed blocks.
  • 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.
  • rest_ensure_response()Ensures a REST response is a response object (for consistency).
  • WP_REST_Block_Patterns_Controller::get_fields_for_response()
  • WP_REST_Block_Patterns_Controller::add_additional_fields_to_object()
  • WP_REST_Block_Patterns_Controller::filter_response_by_context()

Used by · 1

Source code

	public function prepare_item_for_response( $item, $request ) {		// Resolve pattern blocks so they don't need to be resolved client-side		// in the editor, improving performance.		$blocks          = parse_blocks( $item['content'] );		$blocks          = resolve_pattern_blocks( $blocks );		$item['content'] = serialize_blocks( $blocks ); 		$fields = $this->get_fields_for_response( $request );		$keys   = array(			'name'          => 'name',			'title'         => 'title',			'content'       => 'content',			'description'   => 'description',			'viewportWidth' => 'viewport_width',			'inserter'      => 'inserter',			'categories'    => 'categories',			'keywords'      => 'keywords',			'blockTypes'    => 'block_types',			'postTypes'     => 'post_types',			'templateTypes' => 'template_types',			'source'        => 'source',		);		$data   = array();		foreach ( $keys as $item_key => $rest_key ) {			if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) {				$data[ $rest_key ] = $item[ $item_key ];			}		} 		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';		$data    = $this->add_additional_fields_to_object( $data, $request );		$data    = $this->filter_response_by_context( $data, $context );		return rest_ensure_response( $data );	}

Changelog

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

6.3.0
Added source property.from the docblock
6.0.0
Introduced.from the docblock

About this page

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