From 04c217343b79864135509a82a5fa8e2875f8153d Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Fri, 12 Dec 2025 22:04:45 +0530 Subject: [PATCH 1/4] Fix: Localize repeated background images in Cover block (#794) --- includes/create-theme/theme-media.php | 19 +++++++++++++++---- tests/test-theme-templates.php | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/includes/create-theme/theme-media.php b/includes/create-theme/theme-media.php index aae3e019..d53b794a 100644 --- a/includes/create-theme/theme-media.php +++ b/includes/create-theme/theme-media.php @@ -56,25 +56,36 @@ public static function get_media_absolute_urls_from_template( $template ) { } } - // Gets the absolute URLs of background images in these blocks + // Gets the absolute URLs of background images in Cover blocks if ( 'core/cover' === $block['blockName'] ) { + // 1) Parse inline styles for background-image $html = new WP_HTML_Tag_Processor( $block['innerHTML'] ); while ( $html->next_tag( 'div' ) ) { $style = $html->get_attribute( 'style' ); if ( $style ) { $matches = array(); - preg_match( '/background-image: url\((.*)\)/', $style, $matches ); + // Match url(...) with or without quotes + preg_match( '/background-image:\s*url\(("|\')?(.*?)(\1)\)/i', $style, $matches ); if ( isset( $matches[1] ) ) { - $url = $matches[1]; + // In quoted match, the URL is in group 2; otherwise group 2 also holds the URL + $url = isset( $matches[2] ) ? $matches[2] : $matches[1]; if ( CBT_Theme_Utils::is_absolute_url( $url ) ) { $media[] = $url; } } } } + + // 2) Handle repeated background set via block attributes + if ( isset( $block['attrs']['style']['background']['backgroundImage']['url'] ) ) { + $cover_bg_url = $block['attrs']['style']['background']['backgroundImage']['url']; + if ( CBT_Theme_Utils::is_absolute_url( $cover_bg_url ) ) { + $media[] = $cover_bg_url; + } + } } - // Gets the absolute URLs of background images in these blocks + // Gets the absolute URLs of background images in Group blocks if ( 'core/group' === $block['blockName'] ) { if ( isset( $block['attrs']['style']['background']['backgroundImage']['url'] ) && CBT_Theme_Utils::is_absolute_url( $block['attrs']['style']['background']['backgroundImage']['url'] ) ) { $media[] = $block['attrs']['style']['background']['backgroundImage']['url']; diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 15bdc8f0..680d1d52 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -372,4 +372,20 @@ public function test_localize_nested_cover_block_children() { $this->assertStringContainsString( 'This is text to localize', $new_template->content ); } + public function test_localize_cover_repeated_background_via_attrs() { + $template = new stdClass(); + $template->content = '\n' + . '
'; + $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeMedia' => true ) ); + $this->assertStringContainsString( '/assets/images/bg.png', $new_template->content ); + } + + public function test_localize_cover_repeated_background_inline_style() { + $template = new stdClass(); + $template->content = '\n' + . '
'; + $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeMedia' => true ) ); + $this->assertStringContainsString( '/assets/images/pattern.webp', $new_template->content ); + } + } From a4c8b6c8d287d51ee08d243424be6ef5dea21497 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Fri, 12 Dec 2025 22:18:27 +0530 Subject: [PATCH 2/4] Fix: Use correct localizeImages option key in tests --- tests/test-theme-templates.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 680d1d52..225dd934 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -376,7 +376,7 @@ public function test_localize_cover_repeated_background_via_attrs() { $template = new stdClass(); $template->content = '\n' . '
'; - $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeMedia' => true ) ); + $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); $this->assertStringContainsString( '/assets/images/bg.png', $new_template->content ); } @@ -384,7 +384,7 @@ public function test_localize_cover_repeated_background_inline_style() { $template = new stdClass(); $template->content = '\n' . '
'; - $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeMedia' => true ) ); + $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); $this->assertStringContainsString( '/assets/images/pattern.webp', $new_template->content ); } From e7bd0008f48b9d4304a88098a50464bc4839f48a Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Fri, 12 Dec 2025 22:22:08 +0530 Subject: [PATCH 3/4] Fix: Add slug property to test templates --- tests/test-theme-templates.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 225dd934..1e91cf56 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -374,6 +374,7 @@ public function test_localize_nested_cover_block_children() { public function test_localize_cover_repeated_background_via_attrs() { $template = new stdClass(); + $template->slug = 'test-template'; $template->content = '\n' . '
'; $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); @@ -382,6 +383,7 @@ public function test_localize_cover_repeated_background_via_attrs() { public function test_localize_cover_repeated_background_inline_style() { $template = new stdClass(); + $template->slug = 'test-template'; $template->content = '\n' . '
'; $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); From 50bddb21b5608d2bb8ea4e29c7095a8689f3e957 Mon Sep 17 00:00:00 2001 From: Om vataliya Date: Fri, 12 Dec 2025 22:28:35 +0530 Subject: [PATCH 4/4] Fix: Call CBT_Theme_Media directly to avoid paternization --- tests/test-theme-templates.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test-theme-templates.php b/tests/test-theme-templates.php index 1e91cf56..1ff0b55f 100644 --- a/tests/test-theme-templates.php +++ b/tests/test-theme-templates.php @@ -374,19 +374,17 @@ public function test_localize_nested_cover_block_children() { public function test_localize_cover_repeated_background_via_attrs() { $template = new stdClass(); - $template->slug = 'test-template'; $template->content = '\n' . '
'; - $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); + $new_template = CBT_Theme_Media::make_template_images_local( $template ); $this->assertStringContainsString( '/assets/images/bg.png', $new_template->content ); } public function test_localize_cover_repeated_background_inline_style() { $template = new stdClass(); - $template->slug = 'test-template'; $template->content = '\n' . '
'; - $new_template = CBT_Theme_Templates::prepare_template_for_export( $template, null, array( 'localizeImages' => true ) ); + $new_template = CBT_Theme_Media::make_template_images_local( $template ); $this->assertStringContainsString( '/assets/images/pattern.webp', $new_template->content ); }