Skip to content
Open
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
19 changes: 15 additions & 4 deletions includes/create-theme/theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
16 changes: 16 additions & 0 deletions tests/test-theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- wp:cover {"style":{"background":{"backgroundImage":{"url":"http://example.com/bg.png","repeat":"repeat"}}}} -->\n'
. '<div class="wp-block-cover"><div class="wp-block-cover__inner-container"></div></div><!-- /wp:cover -->';
$new_template = CBT_Theme_Media::make_template_images_local( $template );
$this->assertStringContainsString( '<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/bg.png', $new_template->content );
}

public function test_localize_cover_repeated_background_inline_style() {
$template = new stdClass();
$template->content = '<!-- wp:cover -->\n'
. '<div class="wp-block-cover" style="background-image:url(\'http://example.com/pattern.webp\');background-repeat:repeat"></div><!-- /wp:cover -->';
$new_template = CBT_Theme_Media::make_template_images_local( $template );
$this->assertStringContainsString( '<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/pattern.webp', $new_template->content );
}

}