|
| 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 | + if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) { |
| 26 | + $attrs = $blocks[ $i ]['attrs']; |
| 27 | + |
| 28 | + if ( empty( $attrs['slug'] ) ) { |
| 29 | + ++$i; |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + $slug = $attrs['slug']; |
| 34 | + |
| 35 | + if ( isset( $seen_refs[ $slug ] ) ) { |
| 36 | + // Skip recursive patterns. |
| 37 | + array_splice( $blocks, $i, 1 ); |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + $registry = WP_Block_Patterns_Registry::get_instance(); |
| 42 | + $pattern = $registry->get_registered( $slug ); |
| 43 | + |
| 44 | + // Skip unknown patterns. |
| 45 | + if ( ! $pattern ) { |
| 46 | + ++$i; |
| 47 | + continue; |
| 48 | + } |
| 49 | + ////////////////////////////// |
| 50 | + // START CORE MODIFICATIONS // |
| 51 | + ////////////////////////////// |
| 52 | + $blocks_to_insert = parse_blocks( trim( $pattern['content'] ) ); |
| 53 | + |
| 54 | + /* |
| 55 | + * For single-root patterns, add the pattern name to make this a pattern instance in the editor. |
| 56 | + * If the pattern has metadata, merge it with the existing metadata. |
| 57 | + */ |
| 58 | + if ( count( $blocks_to_insert ) === 1 ) { |
| 59 | + $block_metadata = $blocks_to_insert[0]['attrs']['metadata'] ?? array(); |
| 60 | + $block_metadata['patternName'] = $slug; |
| 61 | + |
| 62 | + /* |
| 63 | + * Merge pattern metadata with existing block metadata. |
| 64 | + * Pattern metadata takes precedence, but existing block metadata |
| 65 | + * is preserved as a fallback when the pattern doesn't define that field. |
| 66 | + * Only the defined fields (name, description, categories) are updated; |
| 67 | + * other metadata keys are preserved. |
| 68 | + */ |
| 69 | + foreach ( array( |
| 70 | + 'name' => 'title', // 'title' is the field in the pattern object 'name' is the field in the block metadata. |
| 71 | + 'description' => 'description', |
| 72 | + 'categories' => 'categories', |
| 73 | + ) as $key => $pattern_key ) { |
| 74 | + $value = $pattern[ $pattern_key ] ?? $block_metadata[ $key ] ?? null; |
| 75 | + if ( $value ) { |
| 76 | + $block_metadata[ $key ] = 'categories' === $key && is_array( $value ) |
| 77 | + ? array_map( 'sanitize_text_field', $value ) |
| 78 | + : sanitize_text_field( $value ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $blocks_to_insert[0]['attrs']['metadata'] = $block_metadata; |
| 83 | + } |
| 84 | + ////////////////////////////// |
| 85 | + // END CORE MODIFICATIONS // |
| 86 | + ////////////////////////////// |
| 87 | + |
| 88 | + $seen_refs[ $slug ] = true; |
| 89 | + $prev_inner_content = $inner_content; |
| 90 | + $inner_content = null; |
| 91 | + $blocks_to_insert = gutenberg_resolve_pattern_blocks( $blocks_to_insert ); |
| 92 | + $inner_content = $prev_inner_content; |
| 93 | + unset( $seen_refs[ $slug ] ); |
| 94 | + array_splice( $blocks, $i, 1, $blocks_to_insert ); |
| 95 | + |
| 96 | + // If we have inner content, we need to insert nulls in the |
| 97 | + // inner content array, otherwise serialize_blocks will skip |
| 98 | + // blocks. |
| 99 | + if ( $inner_content ) { |
| 100 | + $null_indices = array_keys( $inner_content, null, true ); |
| 101 | + $content_index = $null_indices[ $i ]; |
| 102 | + $nulls = array_fill( 0, count( $blocks_to_insert ), null ); |
| 103 | + array_splice( $inner_content, $content_index, 1, $nulls ); |
| 104 | + } |
| 105 | + |
| 106 | + // Skip inserted blocks. |
| 107 | + $i += count( $blocks_to_insert ); |
| 108 | + } else { |
| 109 | + if ( ! empty( $blocks[ $i ]['innerBlocks'] ) ) { |
| 110 | + $prev_inner_content = $inner_content; |
| 111 | + $inner_content = $blocks[ $i ]['innerContent']; |
| 112 | + $blocks[ $i ]['innerBlocks'] = gutenberg_resolve_pattern_blocks( |
| 113 | + $blocks[ $i ]['innerBlocks'] |
| 114 | + ); |
| 115 | + $blocks[ $i ]['innerContent'] = $inner_content; |
| 116 | + $inner_content = $prev_inner_content; |
| 117 | + } |
| 118 | + ++$i; |
| 119 | + } |
| 120 | + } |
| 121 | + return $blocks; |
| 122 | + } |
| 123 | +} |
0 commit comments