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 = ' + +
wp-image-635?template data
+ + '; + $new_template = CBT_Theme_Templates::eliminate_environment_specific_content( $template ); + + $this->assertStringContainsString( 'class="custom-wp-image-635"', $new_template->content ); + $this->assertStringContainsString( '
wp-image-635?template data
', $new_template->content ); + $this->assertStringNotContainsString( ' wp-image-635"', $new_template->content ); + } + + public function test_eliminate_image_id_from_cover_with_fixed_background() { + $template = new stdClass(); + $template->content = ' + +
+ + '; + $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 = '