From 98579e44954a82e1a424144b2cf05b5d1a08d71c Mon Sep 17 00:00:00 2001 From: desrosj Date: Tue, 6 Jul 2021 15:32:57 +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. Built from https://develop.svn.wordpress.org/trunk@51348 git-svn-id: http://core.svn.wordpress.org/trunk@50957 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 35 ++++++++++++++++++---------- wp-includes/blocks/legacy-widget.php | 17 ++++++++++---- wp-includes/deprecated.php | 17 ++++++++++++++ wp-includes/version.php | 2 +- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index df5de7c021..547bbe8147 100644 --- a/wp-includes/blocks.php +++ b/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/wp-includes/blocks/legacy-widget.php b/wp-includes/blocks/legacy-widget.php index 030aa88f56..8a32605681 100644 --- a/wp-includes/blocks/legacy-widget.php +++ b/wp-includes/blocks/legacy-widget.php @@ -25,13 +25,15 @@ function render_block_core_legacy_widget( $attributes ) { } $id_base = $attributes['idBase']; - if ( method_exists( $wp_widget_factory, 'get_widget_key' ) ) { - $widget_key = $wp_widget_factory->get_widget_key( $id_base ); + if ( method_exists( $wp_widget_factory, 'get_widget_key' ) && method_exists( $wp_widget_factory, 'get_widget_object' ) ) { + $widget_key = $wp_widget_factory->get_widget_key( $id_base ); + $widget_object = $wp_widget_factory->get_widget_object( $id_base ); } else { - $widget_key = gutenberg_get_widget_key( $id_base ); + $widget_key = gutenberg_get_widget_key( $id_base ); + $widget_object = gutenberg_get_widget_object( $id_base ); } - if ( ! $widget_key ) { + if ( ! $widget_key || ! $widget_object ) { return ''; } @@ -45,8 +47,13 @@ function render_block_core_legacy_widget( $attributes ) { $instance = array(); } + $args = array( + 'widget_id' => $widget_object->id, + 'widget_name' => $widget_object->name, + ); + ob_start(); - the_widget( $widget_key, $instance ); + the_widget( $widget_key, $instance, $args ); return ob_get_clean(); } diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 2c5f24bde0..43ced1429e 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -4207,3 +4207,20 @@ function wp_sensitive_page_meta() {