From 4bfba3516953ab9a27ce4007302613d68ef66d59 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 12 Sep 2023 19:20:21 +0000 Subject: [PATCH] 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 --- wp-includes/blocks.php | 4 ++++ wp-includes/formatting.php | 17 ++++++++++++++--- wp-includes/version.php | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 3c88f7b4c3..7795a6969b 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -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, diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 05084329d3..0e5797fa7e 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -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' ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 8d2df4d68f..ccbb89a59a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.