From f196c979403e4459bc6f0fa8d5a69add38666f70 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 27 Nov 2024 11:39:48 -0300 Subject: [PATCH 1/2] Change the way how the media is extracted from the cover block. Use the url attr instead of HTML tags --- includes/create-theme/theme-media.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/includes/create-theme/theme-media.php b/includes/create-theme/theme-media.php index d7a2280a..cce49d52 100644 --- a/includes/create-theme/theme-media.php +++ b/includes/create-theme/theme-media.php @@ -33,7 +33,6 @@ public static function get_media_absolute_urls_from_template( $template ) { // Gets the absolute URLs of img in these blocks if ( 'core/image' === $block['blockName'] || 'core/video' === $block['blockName'] || - 'core/cover' === $block['blockName'] || 'core/media-text' === $block['blockName'] ) { $html = new WP_HTML_Tag_Processor( $block['innerHTML'] ); @@ -58,19 +57,8 @@ public static function get_media_absolute_urls_from_template( $template ) { // Gets the absolute URLs of background images in these blocks if ( 'core/cover' === $block['blockName'] ) { - $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 ); - if ( isset( $matches[1] ) ) { - $url = $matches[1]; - if ( CBT_Theme_Utils::is_absolute_url( $url ) ) { - $media[] = $url; - } - } - } + if ( isset( $block['attrs']['url'] ) && CBT_Theme_Utils::is_absolute_url( $block['attrs']['url'] ) ) { + $media[] = $block['attrs']['url']; } } From ff14e1612e1d759897d3676d318249f3934abcdd Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 27 Nov 2024 11:39:58 -0300 Subject: [PATCH 2/2] Add a test case --- tests/test-theme-media.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test-theme-media.php b/tests/test-theme-media.php index 0671a6d6..066c0db5 100644 --- a/tests/test-theme-media.php +++ b/tests/test-theme-media.php @@ -39,6 +39,21 @@ public function test_make_cover_block_local() { $this->assertStringContainsString( '/assets/images', $new_template->content ); } + public function test_make_cover_block_local_with_background_image() { + $template = new stdClass(); + $template->content = ' + +
+ + '; + $new_template = CBT_Theme_Media::make_template_images_local( $template ); + + // The image should be replaced with a relative URL + $this->assertStringNotContainsString( 'http://example.com/image.jpg', $new_template->content ); + $this->assertStringContainsString( 'get_template_directory_uri', $new_template->content ); + $this->assertStringContainsString( '/assets/images', $new_template->content ); + } + public function test_template_with_media_correctly_prepared() { $template = new stdClass(); $template->slug = 'test-template';