Skip to content
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
16 changes: 12 additions & 4 deletions includes/create-theme/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,22 @@ private static function eliminate_environment_specific_content_from_block( $bloc
if ( in_array( $block['blockName'], array( 'core/image', 'core/cover' ), true ) ) {
// remove id attribute from image and cover blocks
if ( isset( $block['attrs']['id'] ) ) {
$image_id = $block['attrs']['id'];
$image_class = 'wp-image-' . $block['attrs']['id'];
unset( $block['attrs']['id'] );
// remove wp-image-[id] class from inner content
// Remove the matching class token without changing similar text elsewhere.
foreach ( $block['innerContent'] as $inner_key => $inner_content ) {
if ( is_null( $inner_content ) ) {
if ( is_null( $inner_content ) || false === strpos( $inner_content, $image_class ) ) {
continue;
}
$block['innerContent'][ $inner_key ] = str_replace( 'wp-image-' . $image_id, '', $inner_content );

// Covers with fixed or repeated backgrounds put the class on a div, not an img.
$processor = new WP_HTML_Tag_Processor( $inner_content );
while ( $processor->next_tag() ) {
if ( $processor->has_class( $image_class ) ) {
$processor->remove_class( $image_class );
}
}
$block['innerContent'][ $inner_key ] = $processor->__toString();
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/test-theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ public function test_eliminate_id_from_image() {
$this->assertStringNotContainsString( 'wp-image-635', $new_template->content );
}

public function test_eliminate_image_id_only_removes_matching_class_token() {
$template = new stdClass();
$template->content = '
<!-- wp:image {"id":635} -->
<figure class="wp-block-image"><img class="custom-wp-image-635 wp-image-635" alt=""/><figcaption>wp-image-635?template data</figcaption></figure>
<!-- /wp:image -->
';
$new_template = CBT_Theme_Templates::eliminate_environment_specific_content( $template );

$this->assertStringContainsString( 'class="custom-wp-image-635"', $new_template->content );
$this->assertStringContainsString( '<figcaption>wp-image-635?template data</figcaption>', $new_template->content );
$this->assertStringNotContainsString( ' wp-image-635"', $new_template->content );
Comment thread
Copilot marked this conversation as resolved.
}

public function test_eliminate_image_id_from_cover_with_fixed_background() {
$template = new stdClass();
$template->content = '
<!-- wp:cover {"url":"http://example.com/file.jpg","id":635,"hasParallax":true} -->
<div class="wp-block-cover has-parallax"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><div role="img" class="wp-block-cover__image-background wp-image-635 has-parallax" style="background-position:50% 50%;background-image:url(http://example.com/file.jpg)"></div><div class="wp-block-cover__inner-container"></div></div>
<!-- /wp:cover -->
';
$new_template = CBT_Theme_Templates::eliminate_environment_specific_content( $template );

$this->assertStringNotContainsString( '"id":635', $new_template->content );
$this->assertStringNotContainsString( 'wp-image-635', $new_template->content );
$this->assertStringContainsString( 'class="wp-block-cover__image-background has-parallax"', $new_template->content );
}

public function test_eliminate_taxQuery_from_query_loop() {
$template = new stdClass();
$template->content = '
Expand Down
Loading