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
This commit is contained in:
desrosj 2021-07-06 15:32:57 +00:00
parent 5ab81a5f38
commit 98579e4495
4 changed files with 53 additions and 18 deletions

View File

@ -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 );
}
}

View File

@ -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();
}

View File

@ -4207,3 +4207,20 @@ function wp_sensitive_page_meta() {
<?php
wp_strict_cross_origin_referrer();
}
/**
* Render inner blocks from the `core/columns` block for generating an excerpt.
*
* @since 5.2.0
* @deprecated 5.8.0
*
* @access private
*
* @param array $columns The parsed columns 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 ) {
_deprecated_function( __FUNCTION__, '5.8.0', '_excerpt_render_inner_blocks()' );
return _excerpt_render_inner_blocks( $columns, $allowed_blocks );
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-51344';
$wp_version = '5.9-alpha-51348';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.