Skip to content

Commit 77965c2

Browse files
committed
Enhance metadata handling in resolve_pattern_blocks function
Refines the logic for merging metadata in single-root patterns, ensuring that existing metadata is preserved and new attributes such as `name`, `description`, and `categories` are correctly integrated. Updates unit tests to validate the new behavior, including a test for preserving existing metadata.
1 parent f79436b commit 77965c2

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/wp-includes/blocks.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,22 +1883,25 @@ function resolve_pattern_blocks( $blocks ) {
18831883

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

1886-
// For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1886+
/*
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+
*/
18871890
if ( count( $blocks_to_insert ) === 1 ) {
1888-
$metadata = array(
1889-
'patternName' => $slug,
1890-
);
1891-
1892-
if ( ! empty( $pattern['title'] ) ) {
1893-
$metadata['name'] = sanitize_text_field( $pattern['title'] );
1894-
}
1895-
1896-
if ( ! empty( $pattern['description'] ) ) {
1897-
$metadata['description'] = sanitize_text_field( $pattern['description'] );
1898-
}
1899-
1900-
if ( ! empty( $pattern['categories'] ) && is_array( $pattern['categories'] ) ) {
1901-
$metadata['categories'] = array_map( 'sanitize_text_field', $pattern['categories'] );
1891+
$block_metadata = $blocks_to_insert[0]['attrs']['metadata'] ?? array();
1892+
$metadata = array( 'patternName' => $slug );
1893+
1894+
foreach ( array(
1895+
'name' => 'title',
1896+
'description' => 'description',
1897+
'categories' => 'categories',
1898+
) as $key => $pattern_key ) {
1899+
$value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null;
1900+
if ( $value ) {
1901+
$metadata[ $key ] = 'categories' === $key && is_array( $value )
1902+
? array_map( 'sanitize_text_field', $value )
1903+
: sanitize_text_field( $value );
1904+
}
19021905
}
19031906

19041907
$blocks_to_insert[0]['attrs']['metadata'] = $metadata;

tests/phpunit/tests/blocks/resolvePatternBlocks.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function set_up() {
7171
'categories' => array( 'featured' ),
7272
)
7373
);
74+
register_block_pattern(
75+
'core/existing-metadata',
76+
array(
77+
'title' => 'Existing Metadata Pattern',
78+
'content' => '<!-- wp:paragraph {"metadata":{"patternName":"core/existing-metadata-should-not-overwrite","description":"A existing metadata pattern.","categories":["cake"]}} -->Existing metadata content<!-- /wp:paragraph -->',
79+
)
80+
);
7481
}
7582

7683
public function tear_down() {
@@ -143,6 +150,11 @@ public function data_should_resolve_pattern_blocks_as_expected() {
143150
'<!-- wp:pattern {"slug":"core/single-root-with-forbidden-chars-in-attrs"} /-->',
144151
'<!-- wp:paragraph {"metadata":{"patternName":"core/single-root-with-forbidden-chars-in-attrs","name":"Single Root Pattern","description":"A single root pattern.","categories":["text","bad\'); DROP TABLE wp_posts;\u002d\u002d","","evil\u0000null byte","category with html tags"]}} -->Single root content<!-- /wp:paragraph -->',
145152
),
153+
// Metadata is merged with existing metadata and existing metadata is preserved.
154+
'existing metadata preserved' => array(
155+
'<!-- wp:pattern {"slug":"core/existing-metadata"} /-->',
156+
'<!-- wp:paragraph {"metadata":{"patternName":"core/existing-metadata","name":"Existing Metadata Pattern","description":"A existing metadata pattern.","categories":["cake"]}} -->Existing metadata content<!-- /wp:paragraph -->',
157+
),
146158
);
147159
}
148160
}

0 commit comments

Comments
 (0)