Skip to content

Commit

Permalink
Full Site Editing: Update theme attribute injection and removal (#28368)
Browse files Browse the repository at this point in the history
* Refactor theme attribute injection and removal
* Update jsdocs
  • Loading branch information
jeyip authored Jan 22, 2021
1 parent 90697bb commit 1608a02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
14 changes: 6 additions & 8 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,20 @@ function _gutenberg_get_template_files( $template_type ) {
* stylesheet as a theme attribute into each wp_template_part
*
* @param string $template_content serialized wp_template content.
* @param string $theme the active theme's stylesheet.
*
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_content( $template_content, $theme ) {
function _inject_theme_attribute_in_content( $template_content ) {
$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'] ) &&
wp_get_theme()->get_stylesheet() === $theme
! isset( $block['attrs']['theme'] )
) {
$template_blocks[ $key ]['attrs']['theme'] = $theme;
$template_blocks[ $key ]['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
}
}
Expand All @@ -136,9 +134,9 @@ function _inject_theme_attribute_in_content( $template_content, $theme ) {
}

return $new_content;
} else {
return $template_content;
}

return $template_content;
}

/**
Expand All @@ -155,7 +153,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
$theme = wp_get_theme()->get_stylesheet();

if ( 'wp_template' === $template_type ) {
$template_content = _inject_theme_attribute_in_content( $template_content, $theme );
$template_content = _inject_theme_attribute_in_content( $template_content );
}

$template = new WP_Block_Template();
Expand Down
11 changes: 5 additions & 6 deletions lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

/**
* Parses wp_template content and injects the current theme's
* stylesheet as a theme attribute into each wp_template_part
* Parses wp_template content and removes the theme attribute from
* each wp_template_part
*
* @param string $template_content serialized wp_template content.
*
Expand All @@ -31,9 +31,9 @@ function _remove_theme_attribute_from_content( $template_content ) {
}

return $new_content;
} else {
return $template_content;
}

return $template_content;
}


Expand All @@ -56,8 +56,7 @@ function gutenberg_edit_site_export() {
// Load templates into the zip file.
$templates = gutenberg_get_block_templates();
foreach ( $templates as $template ) {
$updated_content = _remove_theme_attribute_from_content( $template->content );
$template->content = $updated_content;
$template->content = _remove_theme_attribute_from_content( $template->content );

$zip->addFromString(
'theme/block-templates/' . $template->slug . '.html',
Expand Down

0 comments on commit 1608a02

Please sign in to comment.