Skip to content

Commit

Permalink
add code from #524 and add some unit tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Mar 22, 2024
1 parent 65db63e commit 3229006
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 5 deletions.
47 changes: 43 additions & 4 deletions admin/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,51 @@ public static function escape_text( $text ) {

public static function eliminate_environment_specific_content( $template ) {

// Templates that reference template parts are exported with the 'theme' attribute.
// This is undesirable and should be removed.
$template->content = str_replace( ',"theme":"' . get_stylesheet() . '"', '', $template->content );
$template_blocks = parse_blocks( $template->content );
$blocks = _flatten_blocks( $template_blocks );

foreach ( $blocks as $key => $block ) {

// remove theme attribute from template parts
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
}

// TODO: Remove things like id's and refs and other things that are environment specific
// remove ref attribute from blocks
// TODO: are there any other blocks that have refs?
if ( 'core/navigation' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) {
unset( $blocks[ $key ]['attrs']['ref'] );
}

if ( in_array( $block['blockName'], array( 'core/image', 'core/cover' ), true ) ) {
// remove id attribute from image and cover blocks
// TODO: are there any other blocks that have ids?
if ( isset( $block['attrs']['id'] ) ) {
unset( $blocks[ $key ]['attrs']['id'] );
}

// remove wp-image-[id] class from image and cover blocks
if ( isset( $block['attrs']['className'] ) ) {
$blocks[ $key ]['attrs']['className'] = preg_replace( '/wp-image-\d+/', '', $block['attrs']['className'] );
}
}

// set taxQuery to null for query blocks
if ( 'core/query' === $block['blockName'] ) {
if ( isset( $block['attrs']['taxQuery'] ) ) {
unset( $blocks[ $key ]['attrs']['taxQuery'] );
}
if ( isset( $block['attrs']['queryId'] ) ) {
unset( $blocks[ $key ]['attrs']['queryId'] );
}
}
}

$new_content = '';
foreach ( $template_blocks as $block ) {
$new_content .= serialize_block( $block );
}
$template->content = $new_content;
return $template;
}
}
78 changes: 77 additions & 1 deletion tests/test-theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function test_paragraphs_in_groups_are_localized() {
$new_template = Theme_Templates::escape_text_in_template( $template );
$this->assertStringContainsString( 'This is text to localize', $new_template->content );
$this->assertStringNotContainsString( '<p>This is text to localize</p>', $new_template->content );

}

// public function test_buttons_are_localized() {
Expand All @@ -44,4 +43,81 @@ public function test_paragraphs_in_groups_are_localized() {
// $this->assertStringNotContainsString( '<a class="wp-block-button__link wp-element-button">This is text to localize </a>', $new_template->content );

// }

public function test_eliminate_theme_ref_from_template_part() {
$template = new stdClass();
$template->content = '<!-- wp:template-part {"slug":"header","theme":"testtheme"} /-->';
$new_template = Theme_Templates::eliminate_environment_specific_content( $template );
$this->assertStringContainsString( '<!-- wp:template-part {"slug":"header"} /-->', $new_template->content );
}

public function test_eliminate_nav_block_ref() {
$template = new stdClass();
$template->content = '<!-- wp:navigation {"ref":4} /-->';
$new_template = Theme_Templates::eliminate_environment_specific_content( $template );
$this->assertStringContainsString( '<!-- wp:navigation /-->', $new_template->content );
}

public function test_eliminate_nav_block_ref_in_nested_block() {
$template = new stdClass();
$template->content = '
<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:navigation {"ref":4} /--></div>
<!-- /wp:group -->
';
$new_template = Theme_Templates::eliminate_environment_specific_content( $template );
$this->assertStringContainsString( '<!-- wp:navigation /-->', $new_template->content );
}

// TODO: This one is failing. The class on the <figure> element is not being removed.
// public function test_eliminate_id_from_image() {
// $template = new stdClass();
// $template->content = '
// <!-- wp:image {"id":635} -->
// <figure class="wp-block-image size-large"><img src="http://example.com/file.jpg" alt="" class="wp-image-635"/></figure>
// <!-- /wp:image -->
// ';
// $new_template = Theme_Templates::eliminate_environment_specific_content( $template );
// $this->assertStringContainsString( '<!-- wp:image -->', $new_template->content );
// $this->assertStringNotContainsString( '<!-- wp:image {"id":635} -->', $new_template->content );
// $this->assertStringNotContainsString( 'wp-image-635', $new_template->content );
// }

//TODO: I recall it being discussed that the queryId might need to stay for some reason (that I don't recall).
// Should we NOT be removing that here?
public function test_eliminate_queryId_from_query_loop() {
$template = new stdClass();
$template->content = '
<!-- wp:query {"queryId":43,"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"metadata":{"categories":["posts"]}} -->
<div class="wp-block-query">
<!-- wp:post-template -->
<!-- wp:post-title {"isLink":true} /-->
<!-- wp:post-excerpt /-->
<!-- /wp:post-template -->
</div>
<!-- /wp:query -->
';
$new_template = Theme_Templates::eliminate_environment_specific_content( $template );
$this->assertStringContainsString( '<!-- wp:query', $new_template->content );
$this->assertStringNotContainsString( '"queryId":43', $new_template->content );
}

// TODO: I'm not sure of the proper way to format this property for testing or now to cause it to be
// added via the Global Styles Panel.
// public function test_eliminate_taxQuery_from_query_loop() {
// $template = new stdClass();
// $template->content = '
// <!-- wp:query {"queryId":43,"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"metadata":{"categories":["posts"]}} -->
// <div class="wp-block-query">
// <!-- wp:post-template -->
// <!-- wp:post-title {"isLink":true} /-->
// <!-- wp:post-excerpt /-->
// <!-- /wp:post-template -->
// </div>
// <!-- /wp:query -->
// ';
// $new_template = Theme_Templates::eliminate_environment_specific_content( $template );
// $this->assertStringContainsString( '<!-- wp:query', $new_template->content );
// $this->assertStringNotContainsString( '"queryId":43', $new_template->content );
// }
}

0 comments on commit 3229006

Please sign in to comment.