diff --git a/includes/create-theme/theme-templates.php b/includes/create-theme/theme-templates.php
index 04fd1ffa..88f30572 100644
--- a/includes/create-theme/theme-templates.php
+++ b/includes/create-theme/theme-templates.php
@@ -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();
}
}
}
diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php
index 8bd64c45..0a81da24 100644
--- a/tests/test-theme-templates.php
+++ b/tests/test-theme-templates.php
@@ -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 = '
+
+