Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use url attr to get the url of the media used by cover block #750

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
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
16 changes: 2 additions & 14 deletions includes/create-theme/theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand All @@ -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'];
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/test-theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '
<!-- wp:cover {"url":"http://example.com/image.jpg"} -->
<div class="wp-block-cover"><div class="wp-block-cover__image-background" style="background-image:url(http://example.com/image.jpg)"></div></div>
<!-- /wp:cover -->
';
$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';
Expand Down