Skip to content

Commit

Permalink
Ensures that background image theme.json objects are replaced, and no…
Browse files Browse the repository at this point in the history
…t merged. The image object represents a single unit. We want to prevent "cross contamination"
  • Loading branch information
ramonjd committed Aug 15, 2024
1 parent 039969c commit a4caf72
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -3110,6 +3110,7 @@ protected static function get_metadata_boolean( $data, $path, $default_value = f
*
* @since 5.8.0
* @since 5.9.0 Duotone preset also has origins.
* @since 6.7.0 Replace background image objects during merge.
*
* @param WP_Theme_JSON $incoming Data to merge.
*/
Expand Down Expand Up @@ -3233,6 +3234,25 @@ public function merge( $incoming ) {
}
}
}

/*
* Style values are merged at the leaf level, however
* some values provide exceptions, namely style values that are
* objects and represent unique definitions for the style.
*/
$style_nodes = static::get_styles_block_nodes();
foreach ( $style_nodes as $style_node ) {
$path = $style_node['path'];
/*
* Background image styles should be replaced, not merged,
* as they themselves are specific object definitions for the style.
*/
$background_image_path = array_merge( $path, static::PROPERTIES_METADATA['background-image'] );
$content = _wp_array_get( $incoming_data, $background_image_path, null );
if ( isset( $content ) ) {
_wp_array_set( $this->theme_json, $background_image_path, $content );
}
}
}

/**
Expand Down
109 changes: 109 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,115 @@ public function test_merge_incoming_data_presets_use_default_names() {
$this->assertSameSetsWithIndex( $expected, $actual );
}

/**
* @ticket 61858
*/
public function test_merge_incoming_background_styles() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/quote.png',
),
'backgroundSize' => 'cover',
),
'blocks' => array(
'core/group' => array(
'background' => array(
'backgroundImage' => array(
'ref' => 'styles.blocks.core/verse.background.backgroundImage',
),
'backgroundAttachment' => 'fixed',
),
),
'core/quote' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/quote.png',
),
'backgroundAttachment' => array(
'ref' => 'styles.blocks.core/group.background.backgroundAttachment',
),
),
),
),
),
)
);

$update_background_image_styles = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundSize' => 'contain',
),
'blocks' => array(
'core/group' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/group.png',
),
),
),
'core/quote' => array(
'background' => array(
'backgroundAttachment' => 'fixed',
),
),
'core/verse' => array(
'background' => array(
'backgroundImage' => array(
'ref' => 'styles.blocks.core/group.background.backgroundImage',
),
),
),
),
),
);
$expected = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/quote.png',
),
'backgroundSize' => 'contain',
),
'blocks' => array(
'core/group' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/group.png',
),
'backgroundAttachment' => 'fixed',
),
),
'core/quote' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'http://example.org/quote.png',
),
'backgroundAttachment' => 'fixed',
),
),
'core/verse' => array(
'background' => array(
'backgroundImage' => array(
'ref' => 'styles.blocks.core/group.background.backgroundImage',
),
),
),
),
),
);
$theme_json->merge( new WP_Theme_JSON( $update_background_image_styles ) );
$actual = $theme_json->get_raw_data();

$this->assertEqualSetsWithIndex( $expected, $actual );
}

/**
* @ticket 54336
*/
Expand Down

0 comments on commit a4caf72

Please sign in to comment.