Posts, Post Types: Avoid unnecessarily parsing blocks twice in wp_trim_excerpt().

All blocks relevant for the excerpt are already being parsed in `excerpt_remove_blocks()`. Therefore running `do_blocks()` on the post content only to create the excerpt is unnecessary and wasteful from a performance perspective.

Props thekt12, spacedmonkey, mukesh27, joemcgill.
Fixes #58682.

Built from https://develop.svn.wordpress.org/trunk@56560


git-svn-id: http://core.svn.wordpress.org/trunk@56072 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-09-12 19:20:21 +00:00
parent e5490118af
commit 4bfba35169
3 changed files with 19 additions and 4 deletions

View File

@ -956,6 +956,10 @@ function filter_block_kses_value( $value, $allowed_html, $allowed_protocols = ar
* @return string The parsed and filtered content.
*/
function excerpt_remove_blocks( $content ) {
if ( ! has_blocks( $content ) ) {
return $content;
}
$allowed_inner_blocks = array(
// Classic blocks have their blockName set to null.
null,

View File

@ -3980,18 +3980,29 @@ function wp_trim_excerpt( $text = '', $post = null ) {
* within the excerpt are stripped out. Modifying the tags here
* is wasteful and can lead to bugs in the image counting logic.
*/
$filter_removed = remove_filter( 'the_content', 'wp_filter_content_tags' );
$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags' );
/*
* Temporarily unhook do_blocks() since excerpt_remove_blocks( $text )
* handels block rendering needed for excerpt.
*/
$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
/**
// Restore the original filter if removed.
if ( $filter_block_removed ) {
add_filter( 'the_content', 'do_blocks', 9 );
}
/*
* Only restore the filter callback if it was removed above. The logic
* to unhook and restore only applies on the default priority of 10,
* which is generally used for the filter callback in WordPress core.
*/
if ( $filter_removed ) {
if ( $filter_image_removed ) {
add_filter( 'the_content', 'wp_filter_content_tags' );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56559';
$wp_version = '6.4-alpha-56560';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.