Skip to content

Commit

Permalink
Remove theme attribute from exported templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyip committed Jan 12, 2021
1 parent 4d4cc57 commit 81abc9b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@
* @package gutenberg
*/

/**
* Parses wp_template content and injects the current theme's
* stylesheet as a theme attribute into each wp_template_part
*
* @param string $template_content serialized wp_template content.
*
* @return string Updated wp_template content.
*/
function _remove_theme_attribute_from_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'] ) ) {
// phpcs:ignore
unset( $template_blocks[$key]['attrs']['theme'] );
$has_updated_content = true;
}
}

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

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


/**
* Output a ZIP file with an export of the current templates
* and template parts from the site editor, and close the connection.
Expand All @@ -24,6 +57,9 @@ 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;

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

0 comments on commit 81abc9b

Please sign in to comment.