Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Site Editing: Update theme attribute injection and removal #28368

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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