Changeset 58303
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r58292 r58303 1520 1520 1521 1521 /** 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 */ 1530 function 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 /** 1522 1599 * Given an array of parsed block trees, applies callbacks before and after serializing them and 1523 1600 * returns their concatenated output. -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php
r56093 r58303 163 163 */ 164 164 public function prepare_item_for_response( $item, $request ) { 165 // Resolve pattern blocks so they don't need to be resolved client-side 166 // in the editor, improving performance. 167 $blocks = parse_blocks( $item['content'] ); 168 $blocks = resolve_pattern_blocks( $blocks ); 169 $item['content'] = serialize_blocks( $blocks ); 170 165 171 $fields = $this->get_fields_for_response( $request ); 166 172 $keys = array( -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r58229 r58303 669 669 */ 670 670 public function prepare_item_for_response( $item, $request ) { 671 // Resolve pattern blocks so they don't need to be resolved client-side 672 // in the editor, improving performance. 673 $blocks = parse_blocks( $item->content ); 674 $blocks = resolve_pattern_blocks( $blocks ); 675 $item->content = serialize_blocks( $blocks ); 676 671 677 // Restores the more descriptive, specific name for use within this method. 672 678 $template = $item;
Note: See TracChangeset
for help on using the changeset viewer.