Skip to content

Commit

Permalink
Syncing source code changes from WordPress/gutenberg#60100
Browse files Browse the repository at this point in the history
Tests to come
  • Loading branch information
ramonjd committed Jul 8, 2024
1 parent e3e7fdb commit 49d4b8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
38 changes: 32 additions & 6 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ public static function get_style_variations( $scope = 'theme' ) {
* as the value of `_link` object in REST API responses.
*
* @since 6.6.0
* @since 6.7.0 Resolve relative paths in block styles.
*
* @param WP_Theme_JSON $theme_json A theme json instance.
* @return array An array of resolved paths.
Expand All @@ -860,21 +861,19 @@ public static function get_resolved_theme_uris( $theme_json ) {
}

$theme_json_data = $theme_json->get_raw_data();

// Top level styles.
$background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null;

/*
* The same file convention when registering web fonts.
* See: WP_Font_Face_Resolver::to_theme_file_uri.
*/
$placeholder = 'file:./';

// Top level styles.
$background_image_url = $theme_json_data['styles']['background']['backgroundImage']['url'] ?? null;
if (
isset( $background_image_url ) &&
is_string( $background_image_url ) &&
// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
str_starts_with( $background_image_url, $placeholder )
) {
str_starts_with( $background_image_url, $placeholder ) ) {
$file_type = wp_check_filetype( $background_image_url );
$src_url = str_replace( $placeholder, '', $background_image_url );
$resolved_theme_uri = array(
Expand All @@ -888,6 +887,33 @@ public static function get_resolved_theme_uris( $theme_json ) {
$resolved_theme_uris[] = $resolved_theme_uri;
}

// Block styles.
if ( ! empty( $theme_json_data['styles']['blocks'] ) ) {
foreach ( $theme_json_data['styles']['blocks'] as $block_name => $block_styles ) {
if ( ! isset( $block_styles['background']['backgroundImage']['url'] ) ) {
continue;
}
$background_image_url = $block_styles['background']['backgroundImage']['url'] ?? null;
if (
isset( $background_image_url ) &&
is_string( $background_image_url ) &&
// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
str_starts_with( $background_image_url, $placeholder ) ) {
$file_type = wp_check_filetype( $background_image_url );
$src_url = str_replace( $placeholder, '', $background_image_url );
$resolved_theme_uri = array(
'name' => $background_image_url,
'href' => sanitize_url( get_theme_file_uri( $src_url ) ),
'target' => "styles.blocks.{$block_name}.background.backgroundImage.url",
);
if ( isset( $file_type['type'] ) ) {
$resolved_theme_uri['type'] = $file_type['type'];
}
$resolved_theme_uris[] = $resolved_theme_uri;
}
}
}

return $resolved_theme_uris;
}

Expand Down
8 changes: 4 additions & 4 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ class WP_Theme_JSON {
*/
const VALID_STYLES = array(
'background' => array(
'backgroundImage' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
'backgroundImage' => null,
'backgroundPosition' => null,
'backgroundRepeat' => null,
'backgroundSize' => null,
),
'border' => array(
'color' => null,
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,15 @@ function wp_get_global_styles_custom_css() {
* Adds global style rules to the inline style for each block.
*
* @since 6.1.0
* @since 6.7.0 Resolve relative paths in block styles.
*
* @global WP_Styles $wp_styles
*/
function wp_add_global_styles_for_blocks() {
global $wp_styles;

$tree = WP_Theme_JSON_Resolver::get_merged_data();
$tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree );
$block_nodes = $tree->get_styles_block_nodes();

$can_use_cached = ! wp_is_development_mode( 'theme' );
Expand Down

0 comments on commit 49d4b8d

Please sign in to comment.