Skip to content

Commit d8f1d20

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 25420f0 commit d8f1d20

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
@@ -1871,7 +1871,16 @@ function resolve_pattern_blocks( $blocks ) {
18711871
continue;
18721872
}
18731873

1874-
$blocks_to_insert = parse_blocks( $pattern['content'] );
1874+
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
1875+
1876+
// For single-root patterns, add the pattern name to make this a pattern instance in the editor.
1877+
if ( count( $blocks_to_insert ) === 1 ) {
1878+
$blocks_to_insert[0]['attrs']['metadata'] = array(
1879+
'patternName' => $slug,
1880+
'name' => $pattern['title'],
1881+
);
1882+
}
1883+
18751884
$seen_refs[ $slug ] = true;
18761885
$prev_inner_content = $inner_content;
18771886
$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)