Skip to content

Commit 71075a0

Browse files
committed
Modifies the resolve_pattern_blocks function to include metadata for single-root patterns, allowing the pattern name and title to be stored in the block attributes.
Adds unit tests.
1 parent b77fbe6 commit 71075a0

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/wp-includes/blocks.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,16 @@ function resolve_pattern_blocks( $blocks ) {
18801880
continue;
18811881
}
18821882

1883-
$blocks_to_insert = parse_blocks( $pattern['content'] );
1883+
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
1884+
1885+
// For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1886+
if ( count( $blocks_to_insert ) === 1 ) {
1887+
$blocks_to_insert[0]['attrs']['metadata'] = array(
1888+
'patternName' => $slug,
1889+
'name' => $pattern['title'],
1890+
);
1891+
}
1892+
18841893
$seen_refs[ $slug ] = true;
18851894
$prev_inner_content = $inner_content;
18861895
$inner_content = null;

tests/phpunit/tests/blocks/resolvePatternBlocks.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,37 @@ public function set_up() {
3030
'description' => 'Recursive pattern.',
3131
)
3232
);
33+
register_block_pattern(
34+
'core/single-root',
35+
array(
36+
'title' => 'Single Root Pattern',
37+
'content' => '<!-- wp:paragraph -->Single root content<!-- /wp:paragraph -->',
38+
'description' => 'A single root pattern.',
39+
)
40+
);
41+
register_block_pattern(
42+
'core/with-attrs',
43+
array(
44+
'title' => 'Pattern With Attrs',
45+
'content' => '<!-- wp:paragraph {"className":"custom-class"} -->Content<!-- /wp:paragraph -->',
46+
'description' => 'A pattern with existing attributes.',
47+
)
48+
);
49+
register_block_pattern(
50+
'core/nested-single',
51+
array(
52+
'title' => 'Nested Pattern',
53+
'content' => '<!-- wp:group --><!-- wp:paragraph -->Nested content<!-- /wp:paragraph --><!-- wp:pattern {"slug":"core/single-root"} /--><!-- /wp:group -->',
54+
'description' => 'A nested single root pattern.',
55+
)
56+
);
3357
}
3458

3559
public function tear_down() {
3660
unregister_block_pattern( 'core/test' );
3761
unregister_block_pattern( 'core/recursive' );
38-
62+
unregister_block_pattern( 'core/single-root' );
63+
unregister_block_pattern( 'core/with-attrs' );
3964
parent::tear_down();
4065
}
4166

@@ -67,6 +92,12 @@ public function data_should_resolve_pattern_blocks_as_expected() {
6792
'recursive pattern' => array( '<!-- wp:pattern {"slug":"core/recursive"} /-->', '<!-- wp:paragraph -->Recursive<!-- /wp:paragraph -->' ),
6893
// Resolves the pattern within a block.
6994
'pattern within a block' => array( '<!-- wp:group --><!-- wp:paragraph -->Before<!-- /wp:paragraph --><!-- wp:pattern {"slug":"core/test"} /--><!-- wp:paragraph -->After<!-- /wp:paragraph --><!-- /wp:group -->', '<!-- wp:group --><!-- wp:paragraph -->Before<!-- /wp:paragraph --><!-- wp:paragraph -->Hello<!-- /wp:paragraph --><!-- wp:paragraph -->World<!-- /wp:paragraph --><!-- wp:paragraph -->After<!-- /wp:paragraph --><!-- /wp:group -->' ),
95+
// Resolves the single-root pattern and adds metadata.
96+
'single-root pattern' => array( '<!-- wp:pattern {"slug":"core/single-root"} /-->', '<!-- wp:paragraph {"metadata":{"patternName":"core/single-root","name":"Single Root Pattern"}} -->Single root content<!-- /wp:paragraph -->' ),
97+
// Existing attributes are preserved when adding metadata.
98+
'existing attributes preserved' => array( '<!-- wp:pattern {"slug":"core/with-attrs"} /-->', '<!-- wp:paragraph {"className":"custom-class","metadata":{"patternName":"core/with-attrs","name":"Pattern With Attrs"}} -->Content<!-- /wp:paragraph -->' ),
99+
// Resolves the nested single-root pattern and adds metadata.
100+
'nested single-root pattern' => array( '<!-- wp:pattern {"slug":"core/nested-single"} /-->', '<!-- wp:group {"metadata":{"patternName":"core/nested-single","name":"Nested Pattern"}} --><!-- wp:paragraph -->Nested content<!-- /wp:paragraph --><!-- wp:paragraph {"metadata":{"patternName":"core/single-root","name":"Single Root Pattern"}} -->Single root content<!-- /wp:paragraph --><!-- /wp:group -->' ),
70101
);
71102
}
72103
}

0 commit comments

Comments
 (0)