Skip to content

Commit face42f

Browse files
committed
Starting to backport WordPress/wordpress-develop#10248 to GB
1 parent 5a123f7 commit face42f

File tree

2 files changed

+925
-0
lines changed

2 files changed

+925
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Compatibility shims for block APIs for WordPress 7.0.
4+
*
5+
* @package gutenberg
6+
*/
7+
8+
if ( ! function_exists( 'gutenberg_resolve_pattern_blocks' ) ) {
9+
/**
10+
* Replaces patterns in a block tree with their content.
11+
*
12+
* @since 6.6.0
13+
* @since 7.0.0 Adds metadata to attributes of single-pattern container blocks.
14+
*
15+
* @param array $blocks An array blocks.
16+
*
17+
* @return array An array of blocks with patterns replaced by their content.
18+
*/
19+
function gutenberg_resolve_pattern_blocks( $blocks ) {
20+
static $inner_content;
21+
// Keep track of seen references to avoid infinite loops.
22+
static $seen_refs = array();
23+
$i = 0;
24+
while ( $i < count( $blocks ) ) {
25+
error_log( '!!gutenberg_resolve_pattern_blocks>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' );
26+
error_log( print_r( $blocks[ $i ]['blockName'], true ) );
27+
28+
if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) {
29+
$attrs = $blocks[ $i ]['attrs'];
30+
31+
if ( empty( $attrs['slug'] ) ) {
32+
++$i;
33+
continue;
34+
}
35+
36+
$slug = $attrs['slug'];
37+
38+
if ( isset( $seen_refs[ $slug ] ) ) {
39+
// Skip recursive patterns.
40+
array_splice( $blocks, $i, 1 );
41+
continue;
42+
}
43+
44+
$registry = WP_Block_Patterns_Registry::get_instance();
45+
$pattern = $registry->get_registered( $slug );
46+
47+
// Skip unknown patterns.
48+
if ( ! $pattern ) {
49+
++$i;
50+
continue;
51+
}
52+
$blocks_to_insert = parse_blocks( trim( $pattern['content'] ) );
53+
54+
// For single-root patterns, add the pattern name to make this a pattern instance in the editor.
55+
if ( count( $blocks_to_insert ) === 1 ) {
56+
$metadata = array(
57+
'patternName' => $slug,
58+
);
59+
60+
if ( ! empty( $pattern['title'] ) ) {
61+
$metadata['name'] = sanitize_text_field( $pattern['title'] );
62+
}
63+
64+
if ( ! empty( $pattern['description'] ) ) {
65+
$metadata['description'] = sanitize_text_field( $pattern['description'] );
66+
}
67+
68+
if ( ! empty( $pattern['categories'] ) && is_array( $pattern['categories'] ) ) {
69+
$metadata['categories'] = array_map( 'sanitize_text_field', $pattern['categories'] );
70+
}
71+
72+
$blocks_to_insert[0]['attrs']['metadata'] = $metadata;
73+
}
74+
75+
$seen_refs[ $slug ] = true;
76+
$prev_inner_content = $inner_content;
77+
$inner_content = null;
78+
$blocks_to_insert = gutenberg_resolve_pattern_blocks( $blocks_to_insert );
79+
$inner_content = $prev_inner_content;
80+
unset( $seen_refs[ $slug ] );
81+
array_splice( $blocks, $i, 1, $blocks_to_insert );
82+
83+
// If we have inner content, we need to insert nulls in the
84+
// inner content array, otherwise serialize_blocks will skip
85+
// blocks.
86+
if ( $inner_content ) {
87+
$null_indices = array_keys( $inner_content, null, true );
88+
$content_index = $null_indices[ $i ];
89+
$nulls = array_fill( 0, count( $blocks_to_insert ), null );
90+
array_splice( $inner_content, $content_index, 1, $nulls );
91+
}
92+
93+
// Skip inserted blocks.
94+
$i += count( $blocks_to_insert );
95+
} else {
96+
if ( ! empty( $blocks[ $i ]['innerBlocks'] ) ) {
97+
$prev_inner_content = $inner_content;
98+
$inner_content = $blocks[ $i ]['innerContent'];
99+
$blocks[ $i ]['innerBlocks'] = gutenberg_resolve_pattern_blocks(
100+
$blocks[ $i ]['innerBlocks']
101+
);
102+
$blocks[ $i ]['innerContent'] = $inner_content;
103+
$inner_content = $prev_inner_content;
104+
}
105+
++$i;
106+
}
107+
}
108+
return $blocks;
109+
}
110+
}

0 commit comments

Comments
 (0)