Skip to content

Commit

Permalink
Template Parts: Fix loading issue (#28088)
Browse files Browse the repository at this point in the history
* Inject theme attribute in theme provided template content
* Remove theme attribute from exported templates
  • Loading branch information
jeyip authored Jan 14, 2021
1 parent a009a43 commit d944866
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
45 changes: 43 additions & 2 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@ function _gutenberg_get_template_files( $template_type ) {
return $template_files;
}

/**
* 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.
* @param string $theme the active theme's stylesheet.
*
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_content( $template_content, $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'] ) &&
wp_get_theme()->get_stylesheet() === $theme
) {
$template_blocks[ $key ]['attrs']['theme'] = $theme;
$has_updated_content = true;
}
}

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

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

/**
* Build a unified template object based on a theme file.
*
Expand All @@ -115,12 +151,17 @@ function _gutenberg_get_template_files( $template_type ) {
*/
function _gutenberg_build_template_result_from_file( $template_file, $template_type ) {
$default_template_types = gutenberg_get_default_template_types();
$template_content = file_get_contents( $template_file['path'] );
$theme = wp_get_theme()->get_stylesheet();

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

$theme = wp_get_theme()->get_stylesheet();
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = file_get_contents( $template_file['path'] );
$template->content = $template_content;
$template->slug = $template_file['slug'];
$template->is_custom = false;
$template->type = $template_type;
Expand Down
35 changes: 35 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,38 @@
* @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'] ) ) {
unset( $template_blocks[ $key ]['attrs']['theme'] );
$has_updated_content = true;
}
}

if ( $has_updated_content ) {
foreach ( $template_blocks as $block ) {
$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 +56,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
30 changes: 30 additions & 0 deletions phpunit/class-block-templates-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ function test_gutenberg_build_template_result_from_post() {
$this->assertEquals( 'wp_template', $template->type );
}

function test_inject_theme_attribute_in_content() {
$theme = get_stylesheet();
$content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full", "tagName":"header","className":"site-header"} /-->';
$template_content = _inject_theme_attribute_in_content(
$content_without_theme_attribute,
$theme
);
$expected = sprintf(
'<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header","theme":"%s"} /-->',
get_stylesheet()
);
$this->assertEquals( $expected, $template_content );

// Does not inject theme when there is an existing theme attribute.
$content_with_existing_theme_attribute = '<!-- wp:template-part {"slug":"header","theme":"fake-theme","align":"full", "tagName":"header","className":"site-header"} /-->';
$template_content = _inject_theme_attribute_in_content(
$content_with_existing_theme_attribute,
$theme
);
$this->assertEquals( $content_with_existing_theme_attribute, $template_content );

// Does not inject theme when there is no template part.
$content_with_no_template_part = '<!-- wp:post-content /-->';
$template_content = _inject_theme_attribute_in_content(
$content_with_no_template_part,
$theme
);
$this->assertEquals( $content_with_no_template_part, $template_content );
}

/**
* Should retrieve the template from the theme files.
*/
Expand Down
26 changes: 26 additions & 0 deletions phpunit/class-edit-site-export-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Test `gutenberg_edit_site_export` and its helper functions.
*
* @package Gutenberg
*/

class Edit_Site_Export_Test extends WP_UnitTestCase {
function test_remove_theme_attribute_from_content() {
$content_without_theme_attribute = '<!-- wp:template-part {"slug":"header","theme":"tt1-blocks","align":"full","tagName":"header","className":"site-header"} /-->';
$template_content = _remove_theme_attribute_from_content( $content_without_theme_attribute );
$expected = '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->';
$this->assertEquals( $expected, $template_content );

// Does not modify content when there is no existing theme attribute.
$content_with_existing_theme_attribute = '<!-- wp:template-part {"slug":"header","align":"full","tagName":"header","className":"site-header"} /-->';
$template_content = _remove_theme_attribute_from_content( $content_with_existing_theme_attribute );
$this->assertEquals( $content_with_existing_theme_attribute, $template_content );

// Does not remove theme when there is no template part.
$content_with_no_template_part = '<!-- wp:post-content /-->';
$template_content = _remove_theme_attribute_from_content( $content_with_no_template_part );
$this->assertEquals( $content_with_no_template_part, $template_content );
}
}

0 comments on commit d944866

Please sign in to comment.