Make WordPress Core


Ignore:
Timestamp:
07/06/2021 03:31:48 PM (3 years ago)
Author:
desrosj
Message:

Posts: Prevent an empty excerpt when groups and nested column blocks are present.

This improves the logic within excerpt_remove_blocks() to better handle innerBlocks. This prevents an empty excerpt from being returned when core/columns, core/column, and core/group blocks are present.

This issue has been surfaced in the Query Loop block, where excerpts can be set to display.

Props aristath.
Fixes #53604.

File:
1 edited

Legend:

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

    r51298 r51348  
    711711    );
    712712
    713     $allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) );
     713    $allowed_wrapper_blocks = array(
     714        'core/columns',
     715        'core/column',
     716        'core/group',
     717    );
     718
     719    $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
    714720
    715721    /**
     
    730736        if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
    731737            if ( ! empty( $block['innerBlocks'] ) ) {
    732                 if ( 'core/columns' === $block['blockName'] ) {
    733                     $output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks );
     738                if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
     739                    $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
    734740                    continue;
    735741                }
     
    754760
    755761/**
    756  * Render inner blocks from the `core/columns` block for generating an excerpt.
    757  *
    758  * @since 5.2.0
     762 * Render inner blocks from the allowed wrapper blocks
     763 * for generating an excerpt.
     764 *
     765 * @since 5.8
    759766 * @access private
    760767 *
    761  * @param array $columns        The parsed columns block.
     768 * @param array $parsed_block   The parsed block.
    762769 * @param array $allowed_blocks The list of allowed inner blocks.
    763770 * @return string The rendered inner blocks.
    764771 */
    765 function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
     772function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
    766773    $output = '';
    767774
    768     foreach ( $columns['innerBlocks'] as $column ) {
    769         foreach ( $column['innerBlocks'] as $inner_block ) {
    770             if ( in_array( $inner_block['blockName'], $allowed_blocks, true ) && empty( $inner_block['innerBlocks'] ) ) {
    771                 $output .= render_block( $inner_block );
    772             }
     775    foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
     776        if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
     777            continue;
     778        }
     779
     780        if ( empty( $inner_block['innerBlocks'] ) ) {
     781            $output .= render_block( $inner_block );
     782        } else {
     783            $output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
    773784        }
    774785    }
Note: See TracChangeset for help on using the changeset viewer.