[wp-trac] [WordPress Trac] #47637: Enhance excerpt_remove_blocks: add group Blocks and give Block Developers the ability to define excerpts for their Blocks
WordPress Trac
noreply at wordpress.org
Tue Jul 2 11:29:16 UTC 2019
#47637: Enhance excerpt_remove_blocks: add group Blocks and give Block Developers
the ability to define excerpts for their Blocks
-------------------------------+-----------------------------
Reporter: kuchenundkakao | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Posts, Post Types | Version: 5.2.2
Severity: normal | Keywords: 2nd-opinion
Focuses: |
-------------------------------+-----------------------------
The function excerpt_remove_blocks only considers top-level Blocks in an
allowed list for the autogeneration of excerpts. However, since there is
now a Group Block in Core (and a lot of self-developed Grouping Blocks out
there), this can lead to autogenerated Excerpts being empty although there
is plenty of content within the Post.
I propose to add a new filterable "group blocks" array, which adds another
level to be included in the autogeneration. Additionally, there should be
a possibility to register a callback which can be used to generate a
custom excerpt dynamically if needed for custom blocks.
Change function excerpt_remove_blocks to this:
{{{
function excerpt_remove_blocks($content){
$allowed_inner_blocks = array(
// Classic blocks have their blockName set to null.
null,
'core/freeform',
'core/heading',
'core/html',
'core/list',
'core/media-text',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
);
$group_block_excerpt_functions = array(
'core/group' => 'parse_group_block_excerpt',
);
$allowed_blocks = array_merge( $allowed_inner_blocks, array(
'core/columns' ) );
/**
* Filters the list of blocks that can contribute to the excerpt.
*
* If a dynamic block is added to this list, it must not generate
another
* excerpt, as this will cause an infinite loop to occur.
*
* @since 4.4.0
*
* @param array $allowed_blocks The list of allowed blocks.
*/
$allowed_blocks = apply_filters( 'excerpt_allowed_blocks',
$allowed_blocks );
$group_blocks =
apply_filters('excerpt_allowed_group_blocks',$group_block_excerpt_functions);
$blocks = parse_blocks( $content );
$output = '';
foreach ( $blocks as $block ) {
if(in_array($block['blockName'],$group_blocks,true)){
//We have a group Block with no extra excerpt
function
$output.=
parse_group_block_excerpt($block,$allowed_inner_blocks);
}
elseif(in_array($block['blockName'],array_keys($group_blocks),true)){
//The Block registered a custom callback for
autogenerating an Excerpt
$output.=call_user_func($group_blocks[$block['blockName']],$block,$allowed_inner_blocks);
} elseif( 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 );
continue;
}
// Skip the block if it has disallowed or nested inner
blocks.
foreach ( $block['innerBlocks'] as $inner_block ) {
if (
! in_array( $inner_block['blockName'],
$allowed_inner_blocks, true ) ||
! empty( $inner_block['innerBlocks'] )
) {
continue 2;
}
}
}
$output .= render_block( $block );
}
}
return $output;
}
}}}
Add a function parse_group_block_excerpt
{{{
function parse_group_block_excerpt($block,$allowed_blocks){
$output = "";
if(!empty($block['innerBlocks'])) {
foreach($block['innerBlocks'] as $inner_block){
if('core/columns' === $inner_block['blockName']){
$output .=
_excerpt_render_inner_columns_blocks( $inner_block, $allowed_inner_blocks
);
continue;
}
// Skip the block if it has disallowed or nested
inner blocks.
foreach($inner_block['innerBlocks'] as
$inner_inner_block){
if (
! in_array(
$inner_inner_block['blockName'], $allowed_inner_blocks, true ) ||
! empty(
$inner_inner_block['innerBlocks'] )
){
continue 2;
}
}
}
}
return $output;
}
}}}
After that, a custom block can register itself as an group block just by
using
{{{
add_filter('excerpt_allowed_group_blocks','add_my_awesome_group_block_to_excerpt');
function add_my_awesome_group_block_to_excerpt($allowed_blocks=array()){
$allowed_blocks[] = 'my-awesome/groupblock';
return $allowed_blocks;
}
}}}
or even by using a custom excerpt function for dynamic blocks by using
{{{
add_filter('excerpt_allowed_group_blocks','add_my_awesome_group_block_to_excerpt');
function add_my_awesome_group_block_to_excerpt($allowed_blocks=array()){
$allowed_blocks['my-awesome/groupblock'] =
'my_awesome_group_block_custom_excerpt';
return $allowed_blocks;
}
}}}
(I hope i did this right as this is my first ticket)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47637>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list