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
This commit is contained in:
Bernhard Reiter 2023-10-12 18:44:45 +00:00
parent 5ac76255a9
commit 8a7d30c7f8
4 changed files with 15 additions and 8 deletions

View File

@ -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;
}

View File

@ -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 = '';

View File

@ -165,14 +165,16 @@ final class WP_Block_Patterns_Registry {
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;
}

View File

@ -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.