Skip to content

Commit 3ec266f

Browse files
committed
Refactor metadata merging in resolve_pattern_blocks function
Improves the handling of metadata for single-root patterns by ensuring that existing metadata is preserved while integrating new attributes such as `name`, `description`, and `categories`. Updates unit tests to reflect the changes in metadata structure, ensuring accurate representation of merged metadata.
1 parent 330dc2b commit 3ec266f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/wp-includes/blocks.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,28 +1883,37 @@ function resolve_pattern_blocks( $blocks ) {
18831883

18841884
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
18851885

1886+
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
1887+
18861888
/*
1887-
* For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1888-
* If the pattern has metadata, merge it with the existing metadata.
1889-
*/
1889+
* For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1890+
* If the pattern has metadata, merge it with the existing metadata.
1891+
*/
18901892
if ( count( $blocks_to_insert ) === 1 ) {
1891-
$block_metadata = $blocks_to_insert[0]['attrs']['metadata'] ?? array();
1892-
$metadata = array( 'patternName' => $slug );
1893+
$block_metadata = $blocks_to_insert[0]['attrs']['metadata'] ?? array();
1894+
$block_metadata['patternName'] = $slug;
18931895

1896+
/*
1897+
* Merge pattern metadata with existing block metadata.
1898+
* Pattern metadata takes precedence, but existing block metadata
1899+
* is preserved as a fallback when the pattern doesn't define that field.
1900+
* Only the defined fields (name, description, categories) are updated;
1901+
* other metadata keys are preserved.
1902+
*/
18941903
foreach ( array(
1895-
'name' => 'title',
1904+
'name' => 'title', // 'title' is the field in the pattern object 'name' is the field in the block metadata.
18961905
'description' => 'description',
18971906
'categories' => 'categories',
18981907
) as $key => $pattern_key ) {
18991908
$value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null;
19001909
if ( $value ) {
1901-
$metadata[ $key ] = 'categories' === $key && is_array( $value )
1910+
$block_metadata[ $key ] = 'categories' === $key && is_array( $value )
19021911
? array_map( 'sanitize_text_field', $value )
19031912
: sanitize_text_field( $value );
19041913
}
19051914
}
19061915

1907-
$blocks_to_insert[0]['attrs']['metadata'] = $metadata;
1916+
$blocks_to_insert[0]['attrs']['metadata'] = $block_metadata;
19081917
}
19091918

19101919
$seen_refs[ $slug ] = true;

tests/phpunit/tests/blocks/resolvePatternBlocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function data_should_resolve_pattern_blocks_as_expected() {
154154
// Metadata is merged with existing metadata and existing metadata is preserved.
155155
'existing metadata preserved' => array(
156156
'<!-- wp:pattern {"slug":"core/existing-metadata"} /-->',
157-
'<!-- wp:paragraph {"metadata":{"patternName":"core/existing-metadata","name":"Existing Metadata Pattern","description":"A existing metadata pattern.","categories":["cake"]}} -->Existing metadata content<!-- /wp:paragraph -->',
157+
'<!-- wp:paragraph {"metadata":{"patternName":"core/existing-metadata","description":"A existing metadata pattern.","categories":["cake"],"name":"Existing Metadata Pattern"}} -->Existing metadata content<!-- /wp:paragraph -->',
158158
),
159159
);
160160
}

0 commit comments

Comments
 (0)