Skip to content

Commit

Permalink
Process group background image when saving theme
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Apr 24, 2024
1 parent 5d8dd7b commit cef5535
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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 );

}
}

0 comments on commit cef5535

Please sign in to comment.