From 8a7d30c7f8c5b4f215893ab2f2276cb6c081831a Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Thu, 12 Oct 2023 18:44:45 +0000 Subject: [PATCH] Patterns: Don't inject `theme` attribute on frontend. Having the patterns registry inject the `theme` attribute into all Template Part blocks inside every pattern was found to negatively impact performance. It turns out that it's only required for the editor (i.e. in the REST API) but not on the frontend; there, it's instead possible to fall back to the currently active theme. The latter change was made to the Pattern and Template Part blocks in https://github.com/WordPress/gutenberg/pull/55217, which was sync'ed to Core in [56849]. Consequently, this changeset removes `theme` attribute insertion from the frontend. Props flixos90, gziolo, dmsnell, hellofromtonya. Fixes #59583. Built from https://develop.svn.wordpress.org/trunk@56896 git-svn-id: http://core.svn.wordpress.org/trunk@56407 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-template-utils.php | 9 ++++++--- wp-includes/blocks.php | 4 +++- wp-includes/class-wp-block-patterns-registry.php | 8 +++++--- wp-includes/version.php | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index c5953e1d4ce..94934599a87 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -549,15 +549,18 @@ function _build_block_template_result_from_file( $template_file, $template_type $template->area = $template_file['area']; } - $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; + $before_block_visitor = ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ? '_inject_theme_attribute_in_template_part_block' : null; $after_block_visitor = null; $hooked_blocks = get_hooked_blocks(); if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); } - $blocks = parse_blocks( $template_content ); - $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + if ( null !== $before_block_visitor || null !== $after_block_visitor ) { + $blocks = parse_blocks( $template_content ); + $template_content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + } + $template->content = $template_content; return $template; } diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index ecb4fa5b3df..3a2e943ad5c 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -779,7 +779,9 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. */ return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) { - _inject_theme_attribute_in_template_part_block( $block ); + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + _inject_theme_attribute_in_template_part_block( $block ); + } $markup = ''; diff --git a/wp-includes/class-wp-block-patterns-registry.php b/wp-includes/class-wp-block-patterns-registry.php index a11bac06bef..1c528abb3f9 100644 --- a/wp-includes/class-wp-block-patterns-registry.php +++ b/wp-includes/class-wp-block-patterns-registry.php @@ -165,14 +165,16 @@ public function unregister( $pattern_name ) { private function prepare_content( $pattern, $hooked_blocks ) { $content = $pattern['content']; - $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; + $before_block_visitor = ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ? '_inject_theme_attribute_in_template_part_block' : null; $after_block_visitor = null; if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern ); } - $blocks = parse_blocks( $content ); - $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + if ( null !== $before_block_visitor || null !== $after_block_visitor ) { + $blocks = parse_blocks( $content ); + $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + } return $content; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 52d81d0510c..cc43927789e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta3-56849'; +$wp_version = '6.4-beta3-56896'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.