From 0690170556266a52a1ceb45085d4926e7cd3974f Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Tue, 6 Jul 2021 15:31:48 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@51348 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 35 ++++++++----- src/wp-includes/deprecated.php | 17 +++++++ tests/phpunit/tests/post/getTheExcerpt.php | 59 ++++++++++++++++++++++ 3 files changed, 99 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index df5de7c021..547bbe8147 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -710,7 +710,13 @@ function excerpt_remove_blocks( $content ) { 'core/verse', ); - $allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) ); + $allowed_wrapper_blocks = array( + 'core/columns', + 'core/column', + 'core/group', + ); + + $allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks ); /** * Filters the list of blocks that can contribute to the excerpt. @@ -729,8 +735,8 @@ function excerpt_remove_blocks( $content ) { foreach ( $blocks as $block ) { if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { if ( ! empty( $block['innerBlocks'] ) ) { - if ( 'core/columns' === $block['blockName'] ) { - $output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks ); + if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) { + $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks ); continue; } @@ -753,23 +759,28 @@ function excerpt_remove_blocks( $content ) { } /** - * Render inner blocks from the `core/columns` block for generating an excerpt. + * Render inner blocks from the allowed wrapper blocks + * for generating an excerpt. * - * @since 5.2.0 + * @since 5.8 * @access private * - * @param array $columns The parsed columns block. + * @param array $parsed_block The parsed block. * @param array $allowed_blocks The list of allowed inner blocks. * @return string The rendered inner blocks. */ -function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) { +function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { $output = ''; - foreach ( $columns['innerBlocks'] as $column ) { - foreach ( $column['innerBlocks'] as $inner_block ) { - if ( in_array( $inner_block['blockName'], $allowed_blocks, true ) && empty( $inner_block['innerBlocks'] ) ) { - $output .= render_block( $inner_block ); - } + foreach ( $parsed_block['innerBlocks'] as $inner_block ) { + if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) { + continue; + } + + if ( empty( $inner_block['innerBlocks'] ) ) { + $output .= render_block( $inner_block ); + } else { + $output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks ); } } diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 2c5f24bde0..43ced1429e 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -4207,3 +4207,20 @@ function wp_sensitive_page_meta() { assertSame( 'Bar', $found ); } + + /** + * @ticket 53604 + */ + public function test_inner_blocks_excerpt() { + $content_1 = ' +
+
+
+

Column 1

+
+ + + +
+

Column 2

+
+
+
+ + + +

+'; + + $content_2 = ' +
+

Paragraph inside group block

+
+ + + +

+'; + + $post_1 = self::factory()->post->create_and_get( + array( + 'post_content' => $content_1, + 'post_excerpt' => '', + ) + ); + + $post_2 = self::factory()->post->create_and_get( + array( + 'post_content' => $content_2, + 'post_excerpt' => '', + ) + ); + + $this->assertSame( + 'Column 1 Column 2', + get_the_excerpt( ( new WP_Query( array( 'p' => $post_1->ID ) ) )->posts[0] ) + ); + + $this->assertSame( + 'Paragraph inside group block', + get_the_excerpt( ( new WP_Query( array( 'p' => $post_2->ID ) ) )->posts[0] ) + ); + } }