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

Template Parts: Fix loading issue #28088

Merged
merged 14 commits into from
Jan 14, 2021
Prev Previous commit
Next Next commit
Remove theme attribute from exported templates
jeyip committed Jan 12, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit b3b4af61db120c23ced23212e71ba3c6af0c7a2b
36 changes: 36 additions & 0 deletions lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,39 @@
* @package gutenberg
*/

/**
* Parses wp_template content and injects the current theme's
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a more accurate description here be that it removes the theme attribute, instead of stating that it injects a new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I agree.

* 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Sounds good 👍

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.
@@ -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'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for declaring $updated_content variable here since it's not reused below.

$template->content = _remove_theme_attribute_from_content( $template['content'] ); 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

$template->content = $updated_content;

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