Skip to content

Commit

Permalink
Patterns: Don't inject theme attribute on frontend.
Browse files Browse the repository at this point in the history
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 WordPress/gutenberg#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
  • Loading branch information
Bernhard Reiter authored and Bernhard Reiter committed Oct 12, 2023
1 parent 5ac7625 commit 8a7d30c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
9 changes: 6 additions & 3 deletions wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
8 changes: 5 additions & 3 deletions wp-includes/class-wp-block-patterns-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8a7d30c

Please sign in to comment.