Skip to content

Commit

Permalink
Only serialize updated content
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyip committed Jan 12, 2021
1 parent 3b8e71b commit 4d4cc57
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,27 @@ function _gutenberg_get_template_files( $template_type ) {
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_content( $template_content, $theme ) {
$new_content = '';
$blocks = parse_blocks( $template_content );

$updated_blocks = array_map(
function( $block ) use ( $theme ) {
if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['theme'] ) ) {
$block['attrs']['theme'] = $theme;
}
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

foreach ( $template_blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['theme'] ) ) {
// phpcs:ignore
$template_blocks[$key]['attrs']['theme'] = $theme;
$has_updated_content = true;
}
}

return $block;
},
$blocks
);
if ( $has_updated_content ) {
foreach ( $template_blocks as $block ) {
$new_content = $new_content . serialize_block( $block );
}

foreach ( $updated_blocks as $block ) {
$new_content = $new_content . serialize_block( $block );
return $new_content;
} else {
return $template_content;
}

return $new_content;
}

/**
Expand Down

0 comments on commit 4d4cc57

Please sign in to comment.