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

FSE: Strip post ids from template part blocks on export. #26268

Merged
merged 2 commits into from
Nov 2, 2020
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
31 changes: 30 additions & 1 deletion lib/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ function gutenberg_edit_site_export() {

$current_template = gutenberg_find_template_post_and_parts( $template_type );
if ( isset( $current_template ) ) {
$zip->addFromString( 'theme/block-templates/' . $current_template['template_post']->post_name . '.html', $current_template['template_post']->post_content );
$zip->addFromString(
'theme/block-templates/' . $current_template['template_post']->post_name . '.html',
gutenberg_strip_post_ids_from_template_part_blocks( $current_template['template_post']->post_content )
);

foreach ( $current_template['template_part_ids'] as $template_part_id ) {
$template_part = get_post( $template_part_id );
Expand Down Expand Up @@ -62,3 +65,29 @@ function () {
);
}
);

/**
* Remove post id attributes from template part blocks.
*
* This is needed so that Gutenberg loads the HTML file of the template, instead of looking for a template part post.
*
* @param string $template_content Template content to modify.
*
* @return string Potentially modified template content.
*/
function gutenberg_strip_post_ids_from_template_part_blocks( $template_content ) {
$blocks = parse_blocks( $template_content );

array_walk(
$blocks,
function( &$block ) {
if ( 'core/template-part' !== $block['blockName'] ) {
return;
}

unset( $block['attrs']['postId'] );
}
);

return serialize_blocks( $blocks );
}
1 change: 1 addition & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function fail_if_died( $message ) {
$GLOBALS['wp_tests_options'] = array(
'gutenberg-experiments' => array(
'gutenberg-widget-experiments' => '1',
'gutenberg-full-site-editing' => 1,
),
);

Expand Down
29 changes: 29 additions & 0 deletions phpunit/class-edit-site-export-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Test `gutenberg_edit_site_export` and its helper functions.
*
* @package Gutenberg
*/

class Edit_Site_Export_Test extends WP_UnitTestCase {
/**
* Verify that post ids are stripped from template part blocks during the export.
*
* This is needed so that the exported template is loaded from the theme.
*/
public function test_post_ids_are_stripped_from_template_parts() {
$post = self::factory()->post->create_and_get(
array(
'post_content' => '<!-- wp:template-part {"postId":123,"slug":"header","theme":"gutenberg-tests"} /-->',
'post_title' => 'Archive',
'post_type' => 'wp_template',
)
);

$this->assertSame(
'<!-- wp:template-part {"slug":"header","theme":"gutenberg-tests"} /-->',
gutenberg_strip_post_ids_from_template_part_blocks( $post->post_content )
);
}
}