Make WordPress Core


Ignore:
Timestamp:
06/03/2024 06:30:02 PM (2 years ago)
Author:
ellatrix
Message:

Editor: resolve patterns server side.

See ​https://github.com/WordPress/gutenberg/pull/60349.
See ​https://github.com/WordPress/gutenberg/pull/61757.
See ​https://github.com/WordPress/wordpress-develop/pull/6673.

Fixes #61228.

Props ellatrix, antonvlasenko.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r58292 r58303  
    15201520
    15211521/**
     1522 * Replaces patterns in a block tree with their content.
     1523 *
     1524 * @since 6.6.0
     1525 *
     1526 * @param array $blocks An array blocks.
     1527 *
     1528 * @return array An array of blocks with patterns replaced by their content.
     1529 */
     1530function resolve_pattern_blocks( $blocks ) {
     1531        static $inner_content;
     1532        // Keep track of seen references to avoid infinite loops.
     1533        static $seen_refs = array();
     1534        $i                = 0;
     1535        while ( $i < count( $blocks ) ) {
     1536                if ( 'core/pattern' === $blocks[ $i ]['blockName'] ) {
     1537                        $attrs = $blocks[ $i ]['attrs'];
     1538
     1539                        if ( empty( $attrs['slug'] ) ) {
     1540                                ++$i;
     1541                                continue;
     1542                        }
     1543
     1544                        $slug = $attrs['slug'];
     1545
     1546                        if ( isset( $seen_refs[ $slug ] ) ) {
     1547                                // Skip recursive patterns.
     1548                                array_splice( $blocks, $i, 1 );
     1549                                continue;
     1550                        }
     1551
     1552                        $registry = WP_Block_Patterns_Registry::get_instance();
     1553                        $pattern  = $registry->get_registered( $slug );
     1554
     1555                        // Skip unknown patterns.
     1556                        if ( ! $pattern ) {
     1557                                ++$i;
     1558                                continue;
     1559                        }
     1560
     1561                        $blocks_to_insert   = parse_blocks( $pattern['content'] );
     1562                        $seen_refs[ $slug ] = true;
     1563                        $prev_inner_content = $inner_content;
     1564                        $inner_content      = null;
     1565                        $blocks_to_insert   = resolve_pattern_blocks( $blocks_to_insert );
     1566                        $inner_content      = $prev_inner_content;
     1567                        unset( $seen_refs[ $slug ] );
     1568                        array_splice( $blocks, $i, 1, $blocks_to_insert );
     1569
     1570                        // If we have inner content, we need to insert nulls in the
     1571                        // inner content array, otherwise serialize_blocks will skip
     1572                        // blocks.
     1573                        if ( $inner_content ) {
     1574                                $null_indices  = array_keys( $inner_content, null, true );
     1575                                $content_index = $null_indices[ $i ];
     1576                                $nulls         = array_fill( 0, count( $blocks_to_insert ), null );
     1577                                array_splice( $inner_content, $content_index, 1, $nulls );
     1578                        }
     1579
     1580                        // Skip inserted blocks.
     1581                        $i += count( $blocks_to_insert );
     1582                } else {
     1583                        if ( ! empty( $blocks[ $i ]['innerBlocks'] ) ) {
     1584                                $prev_inner_content          = $inner_content;
     1585                                $inner_content               = $blocks[ $i ]['innerContent'];
     1586                                $blocks[ $i ]['innerBlocks'] = resolve_pattern_blocks(
     1587                                        $blocks[ $i ]['innerBlocks']
     1588                                );
     1589                                $blocks[ $i ]['innerContent'] = $inner_content;
     1590                                $inner_content               = $prev_inner_content;
     1591                        }
     1592                        ++$i;
     1593                }
     1594        }
     1595        return $blocks;
     1596}
     1597
     1598/**
    15221599 * Given an array of parsed block trees, applies callbacks before and after serializing them and
    15231600 * returns their concatenated output.
Note: See TracChangeset for help on using the changeset viewer.