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

Process group background image when saving theme #586

Merged
merged 1 commit into from
Apr 25, 2024
Merged
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
7 changes: 7 additions & 0 deletions admin/create-theme/theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public static function get_media_absolute_urls_from_template( $template ) {
}
}
}

// Gets the absolute URLs of background images in these blocks
if ( 'core/group' === $block['blockName'] ) {
if ( isset( $block['attrs']['style']['background']['backgroundImage']['url'] ) && Theme_Utils::is_absolute_url( $block['attrs']['style']['background']['backgroundImage']['url'] ) ) {
$media[] = $block['attrs']['style']['background']['backgroundImage']['url'];
}
}
}

return $media;
Expand Down
20 changes: 20 additions & 0 deletions tests/test-theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ public function test_template_with_media_correctly_prepared() {

}

public function test_make_group_block_local() {
$template = new stdClass();
$template->slug = 'test-template';
$template->content = '
<!-- wp:group {"style":{"background":{"backgroundImage":{"url":"http://example.com/image.jpg","id":31,"source":"file","title":"Screenshot 2024-04-18 at 14-08-49 Blog Home ‹ Template ‹ a8c-wp-env ‹ Editor — WordPress"}}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group"></div>
<!-- /wp:group -->
';
$new_template = Theme_Templates::prepare_template_for_export( $template );

// Content should be replaced with a pattern block
$this->assertStringContainsString( '<!-- wp:pattern', $new_template->content );

// The media to install should be in the collection
$this->assertContains( 'http://example.com/image.jpg', $new_template->media );

// The pattern is correctly encoded
$this->assertStringContainsString( '{"backgroundImage":{"url":"<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/assets/images/image.jpg"', $new_template->pattern );

}
}
Loading