Make WordPress Core

Changeset 51382


Ignore:
Timestamp:
07/08/2021 05:27:27 PM (3 years ago)
Author:
jorbin
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.

This introduces the excerpt_allowed_wrapper_blocks filter for controlling which blocks should be considered wrapper blocks.

Wrapper blocks and their nested contents are not stripped by excerpt_remove_blocks(), allowing their contents to appear in generated excerpts.

Backports [51348], [51375], [51378], and [51379] to the 5.8 branch.

Fixes #53604.
Props aristath, jorbin, SergeyBiryukov, desrosj.
Unprops jorbin.

Location:
branches/5.8
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.8

  • branches/5.8/src/wp-includes/blocks.php

    r51363 r51382  
    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    /**
     720     * Filters the list of blocks that can be used as wrapper blocks, allowing
     721     * excerpts to be generated from the `innerBlocks` of these wrappers.
     722     *
     723     * @since 5.8.0
     724     *
     725     * @param array $allowed_wrapper_blocks The list of allowed wrapper blocks.
     726     */
     727    $allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );
     728
     729    $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
    714730
    715731    /**
     
    730746        if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
    731747            if ( ! empty( $block['innerBlocks'] ) ) {
    732                 if ( 'core/columns' === $block['blockName'] ) {
    733                     $output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks );
     748                if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
     749                    $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
    734750                    continue;
    735751                }
     
    754770
    755771/**
    756  * Render inner blocks from the `core/columns` block for generating an excerpt.
    757  *
    758  * @since 5.2.0
     772 * Render inner blocks from the allowed wrapper blocks
     773 * for generating an excerpt.
     774 *
     775 * @since 5.8
    759776 * @access private
    760777 *
    761  * @param array $columns        The parsed columns block.
     778 * @param array $parsed_block   The parsed block.
    762779 * @param array $allowed_blocks The list of allowed inner blocks.
    763780 * @return string The rendered inner blocks.
    764781 */
    765 function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
     782function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
    766783    $output = '';
    767784
    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             }
     785    foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
     786        if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
     787            continue;
     788        }
     789
     790        if ( empty( $inner_block['innerBlocks'] ) ) {
     791            $output .= render_block( $inner_block );
     792        } else {
     793            $output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
    773794        }
    774795    }
  • branches/5.8/src/wp-includes/deprecated.php

    r50810 r51382  
    42084208    wp_strict_cross_origin_referrer();
    42094209}
     4210
     4211/**
     4212 * Render inner blocks from the `core/columns` block for generating an excerpt.
     4213 *
     4214 * @since 5.2.0
     4215 * @deprecated 5.8.0
     4216 *
     4217 * @access private
     4218 *
     4219 * @param array $columns        The parsed columns block.
     4220 * @param array $allowed_blocks The list of allowed inner blocks.
     4221 * @return string The rendered inner blocks.
     4222 */
     4223function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
     4224    _deprecated_function( __FUNCTION__, '5.8.0', '_excerpt_render_inner_blocks()' );
     4225    return _excerpt_render_inner_blocks( $columns, $allowed_blocks );
     4226}
  • branches/5.8/tests/phpunit/tests/post/getTheExcerpt.php

    r46586 r51382  
    146146        $this->assertSame( 'Bar', $found );
    147147    }
     148
     149    /**
     150     * @ticket 53604
     151     */
     152    public function test_inner_blocks_excerpt() {
     153        $content_1 = '<!-- wp:group -->
     154<div class="wp-block-group"><!-- wp:columns -->
     155<div class="wp-block-columns"><!-- wp:column -->
     156<div class="wp-block-column"><!-- wp:paragraph -->
     157<p>Column 1</p>
     158<!-- /wp:paragraph --></div>
     159<!-- /wp:column -->
     160
     161<!-- wp:column -->
     162<div class="wp-block-column"><!-- wp:paragraph -->
     163<p>Column 2</p>
     164<!-- /wp:paragraph --></div>
     165<!-- /wp:column --></div>
     166<!-- /wp:columns --></div>
     167<!-- /wp:group -->
     168
     169<!-- wp:paragraph -->
     170<p></p>
     171<!-- /wp:paragraph -->';
     172
     173        $content_2 = '<!-- wp:group -->
     174<div class="wp-block-group"><!-- wp:paragraph -->
     175<p>Paragraph inside group block</p>
     176<!-- /wp:paragraph --></div>
     177<!-- /wp:group -->
     178
     179<!-- wp:paragraph -->
     180<p></p>
     181<!-- /wp:paragraph -->';
     182
     183        $post_1 = self::factory()->post->create_and_get(
     184            array(
     185                'post_content' => $content_1,
     186                'post_excerpt' => '',
     187            )
     188        );
     189
     190        $post_2 = self::factory()->post->create_and_get(
     191            array(
     192                'post_content' => $content_2,
     193                'post_excerpt' => '',
     194            )
     195        );
     196
     197        $this->assertSame(
     198            'Column 1 Column 2',
     199            get_the_excerpt( ( new WP_Query( array( 'p' => $post_1->ID ) ) )->posts[0] )
     200        );
     201
     202        $this->assertSame(
     203            'Paragraph inside group block',
     204            get_the_excerpt( ( new WP_Query( array( 'p' => $post_2->ID ) ) )->posts[0] )
     205        );
     206    }
    148207}
Note: See TracChangeset for help on using the changeset viewer.