Skip to content

Commit f7465b9

Browse files
committed
Inject theme attribute during serialization
1 parent b926b86 commit f7465b9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/wp-includes/block-template-utils.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,26 @@ function _inject_theme_attribute_in_block_template_content( $template_content )
513513
return $template_content;
514514
}
515515

516+
/**
517+
* Injects the active theme's stylesheet as a `theme` attribute
518+
* into a given template part block.
519+
*
520+
* @since 6.4.0
521+
* @access private
522+
*
523+
* @param array $block a parsed block.
524+
* @return array Updated block.
525+
*/
526+
function _inject_theme_attribute_in_template_part_block( $block ) {
527+
if (
528+
'core/template-part' === $block['blockName'] &&
529+
! isset( $block['attrs']['theme'] )
530+
) {
531+
$block['attrs']['theme'] = get_stylesheet();
532+
}
533+
return $block;
534+
}
535+
516536
/**
517537
* Parses a block template and removes the theme attribute from each template part.
518538
*
@@ -565,7 +585,6 @@ function _build_block_template_result_from_file( $template_file, $template_type
565585
$template = new WP_Block_Template();
566586
$template->id = $theme . '//' . $template_file['slug'];
567587
$template->theme = $theme;
568-
$template->content = _inject_theme_attribute_in_block_template_content( $template_content );
569588
$template->slug = $template_file['slug'];
570589
$template->source = 'theme';
571590
$template->type = $template_type;
@@ -589,6 +608,9 @@ function _build_block_template_result_from_file( $template_file, $template_type
589608
$template->area = $template_file['area'];
590609
}
591610

611+
$blocks = parse_blocks( $template_content );
612+
$template->content = serialize_blocks( $blocks, '_inject_theme_attribute_in_template_part_block' );
613+
592614
return $template;
593615
}
594616

0 commit comments

Comments
 (0)