resolve_pattern_blocks( array $blocks ): array
- Since
- 6.6.0, 7.0.0
- Source
wp-includes/blocks.php:1935
Compatibility
- WordPress
- since 7.0.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
$blocksarray- An array blocks.
Return value
array- An array of blocks with patterns replaced by their content.
Performance profile
How much work a call to resolve_pattern_blocks() 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
- Scaling
- Scales with input
- Instructions
- 9
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5. The body compiles to 150.
Nothing here hands control to plugin code.
3 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()one call below resolve_pattern_blocks()
Further down the call graph this can also reach option, 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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost resolve_pattern_blocks() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$i | 9 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 148 | 9 | 15 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 150 | 9 | 15 | |
| 8.4 | 150 | 9 | 15 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 152 | 9 | 15 | |
| 8.2 | 152 | 9 | 15 | |
| 8.1 | 152 | 9 | 15 | |
| 7.4 | 152 | 9 | 15 |
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 · 4
- parse_blocks()Parses blocks out of a content string.
- sanitize_text_field()Sanitizes a string from user input or from the database.
- resolve_pattern_blocks()Replaces patterns in a block tree with their content.
- WP_Block_Patterns_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 3
- WP_REST_Block_Patterns_Controller::prepare_item_for_response()Prepare a raw block pattern before it gets output in a REST API response.
- WP_REST_Templates_Controller::prepare_item_for_response()Prepare a single template output for response
- resolve_pattern_blocks()Replaces patterns in a block tree with their content.
Source code
function resolve_pattern_blocks( $blocks ) { static $inner_content; // Keep track of seen references to avoid infinite loops. static $seen_refs = array(); $i = 0; while ( $i < count( $blocks ) ) { if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) { $attrs = $blocks[ $i ]['attrs']; if ( empty( $attrs['slug'] ) ) { ++$i; continue; } $slug = $attrs['slug']; if ( isset( $seen_refs[ $slug ] ) ) { // Skip recursive patterns. array_splice( $blocks, $i, 1 ); continue; } $registry = WP_Block_Patterns_Registry::get_instance(); $pattern = $registry->get_registered( $slug ); // Skip unknown patterns. if ( ! $pattern ) { ++$i; continue; } $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); /* * For single-root patterns, add the pattern name to make this a pattern instance in the editor. * If the pattern has metadata, merge it with the existing metadata. */ if ( count( $blocks_to_insert ) === 1 ) { $block_metadata = $blocks_to_insert[0]['attrs']['metadata'] ?? array(); $block_metadata['patternName'] = $slug; /* * Merge pattern metadata with existing block metadata. * Pattern metadata takes precedence, but existing block metadata * is preserved as a fallback when the pattern doesn't define that field. * Only the defined fields (name, description, categories) are updated; * other metadata keys are preserved. */ foreach ( array( 'name' => 'title', // 'title' is the field in the pattern object 'name' is the field in the block metadata. 'description' => 'description', 'categories' => 'categories', ) as $key => $pattern_key ) { $value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null; if ( $value ) { $block_metadata[ $key ] = is_array( $value ) ? array_map( 'sanitize_text_field', $value ) : sanitize_text_field( $value ); } } $blocks_to_insert[0]['attrs']['metadata'] = $block_metadata; } $seen_refs[ $slug ] = true; $prev_inner_content = $inner_content; $inner_content = null; $blocks_to_insert = resolve_pattern_blocks( $blocks_to_insert ); $inner_content = $prev_inner_content; unset( $seen_refs[ $slug ] ); array_splice( $blocks, $i, 1, $blocks_to_insert ); // If we have inner content, we need to insert nulls in the // inner content array, otherwise serialize_blocks will skip // blocks. if ( $inner_content ) { $null_indices = array_keys( $inner_content, null, true ); $content_index = $null_indices[ $i ]; $nulls = array_fill( 0, count( $blocks_to_insert ), null ); array_splice( $inner_content, $content_index, 1, $nulls );Changelog
Introduced in 6.6.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.1.0 tag, from
src/wp-includes/blocks.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.