Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse blocks to generate the excerpt #11704

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function get_dynamic_block_names() {
* Retrieve the dynamic blocks regular expression for searching.
*
* @since 3.6.0
* @deprecated 4.4.0 Use gutenberg_parse_blocks()
*
* @return string
*/
Expand Down Expand Up @@ -192,6 +193,7 @@ function do_blocks( $content ) {
* Remove all dynamic blocks from the given content.
*
* @since 3.6.0
* @deprecated 4.4.0 Use excerpt_remove_blocks()
*
* @param string $content Content of the current post.
* @return string
Expand All @@ -214,7 +216,7 @@ function strip_dynamic_blocks( $content ) {
* @return string
*/
function strip_dynamic_blocks_add_filter( $text ) {
add_filter( 'the_content', 'strip_dynamic_blocks', 6 );
add_filter( 'the_content', 'excerpt_remove_blocks', 6 );

return $text;
}
Expand All @@ -234,9 +236,63 @@ function strip_dynamic_blocks_add_filter( $text ) {
* @return string
*/
function strip_dynamic_blocks_remove_filter( $text ) {
remove_filter( 'the_content', 'strip_dynamic_blocks', 6 );
remove_filter( 'the_content', 'excerpt_remove_blocks', 6 );

return $text;
}
add_filter( 'wp_trim_excerpt', 'strip_dynamic_blocks_remove_filter', 0 ); // Before all other.
}

if ( ! function_exists( 'excerpt_remove_blocks' ) ) {
/**
* Parses blocks out of a content string, and renders those appropriate for the excerpt.
*
* As the excerpt should be a small string of text relevant to the full post content,
* this function renders the blocks that are most likely to contain such text.
*
* @since 4.4.0
*
* @param string $content The content to parse.
* @return string The parsed and filtered content.
*/
function excerpt_remove_blocks( $content ) {
$allowed_blocks = array(
// Classic blocks have their blockName set to null.
null,
'core/columns',
'core/freeform',
'core/heading',
'core/html',
'core/list',
'core/media-text',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
);
/**
* 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 );

$blocks = gutenberg_parse_blocks( $content );
$output = '';

foreach ( $blocks as $block ) {
if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
$output .= gutenberg_render_block( $block );
}
}

return $output;
}
}
18 changes: 8 additions & 10 deletions phpunit/class-blocks-api-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class Blocks_API extends WP_UnitTestCase {
<!-- /wp:spacer -->';

public $filtered_content = '
<!-- wp:paragraph -->

<p>paragraph</p>
<!-- /wp:paragraph -->



<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->';

';

/**
* Dummy block rendering function.
Expand Down Expand Up @@ -77,17 +75,17 @@ function tearDown() {
}

/**
* Tests strip_dynamic_blocks().
* Tests excerpt_remove_blocks().
*
* @covers ::strip_dynamic_blocks
* @covers ::excerpt_remove_blocks
*/
function test_strip_dynamic_blocks() {
function test_excerpt_remove_blocks() {
// Simple dynamic block..
$content = '<!-- wp:core/block /-->';
$this->assertEmpty( strip_dynamic_blocks( $content ) );
$this->assertEmpty( excerpt_remove_blocks( $content ) );

// Dynamic block with options, embedded in other content.
$this->assertEquals( $this->filtered_content, strip_dynamic_blocks( $this->content ) );
$this->assertEquals( $this->filtered_content, excerpt_remove_blocks( $this->content ) );
}

/**
Expand Down