Block Editor: Fix for theme.json: color.duotone and spacing.units should allow empty sets.

This commit fixes an issue with the color.duotone & spacing.units in which empty values didn't override previous origins, resulting in that a theme couldn't provide an empty set for this via its theme.json.

Props nosolosw, youknowriad, aristath.
See #53175.

git-svn-id: https://develop.svn.wordpress.org/trunk@51383 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jorge Costa
2021-07-08 19:30:18 +00:00
parent 7f20da50e7
commit 6bd5962c7d
2 changed files with 205 additions and 2 deletions

View File

@@ -1106,8 +1106,8 @@ class WP_Theme_JSON {
foreach ( $nodes as $metadata ) {
foreach ( $to_replace as $property_path ) {
$path = array_merge( $metadata['path'], $property_path );
$node = _wp_array_get( $incoming_data, $path, array() );
if ( ! empty( $node ) ) {
$node = _wp_array_get( $incoming_data, $path, null );
if ( isset( $node ) ) {
_wp_array_set( $this->theme_json, $path, $node );
}
}

View File

@@ -667,6 +667,209 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
$this->assertEqualSetsWithIndex( $expected, $actual );
}
/**
* @ticket 53175
*/
public function test_merge_incoming_data_empty_presets() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'duotone' => array(
array(
'slug' => 'value',
'colors' => array( 'red', 'green' ),
),
),
'gradients' => array(
array(
'slug' => 'gradient',
'gradient' => 'gradient',
),
),
'palette' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
'spacing' => array(
'units' => array( 'px', 'em' ),
),
'typography' => array(
'fontSizes' => array(
array(
'slug' => 'size',
'value' => 'size',
),
),
),
),
)
);
$theme_json->merge(
new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'duotone' => array(),
'gradients' => array(),
'palette' => array(),
),
'spacing' => array(
'units' => array(),
),
'typography' => array(
'fontSizes' => array(),
),
),
)
)
);
$actual = $theme_json->get_raw_data();
$expected = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'duotone' => array(),
'gradients' => array(
'theme' => array(),
),
'palette' => array(
'theme' => array(),
),
),
'spacing' => array(
'units' => array(),
),
'typography' => array(
'fontSizes' => array(
'theme' => array(),
),
),
),
);
$this->assertEqualSetsWithIndex( $expected, $actual );
}
/**
* @ticket 53175
*/
public function test_merge_incoming_data_null_presets() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'duotone' => array(
array(
'slug' => 'value',
'colors' => array( 'red', 'green' ),
),
),
'gradients' => array(
array(
'slug' => 'gradient',
'gradient' => 'gradient',
),
),
'palette' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
'spacing' => array(
'units' => array( 'px', 'em' ),
),
'typography' => array(
'fontSizes' => array(
array(
'slug' => 'size',
'value' => 'size',
),
),
),
),
)
);
$theme_json->merge(
new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'custom' => false,
),
'spacing' => array(
'customMargin' => false,
),
'typography' => array(
'customLineHeight' => false,
),
),
)
)
);
$actual = $theme_json->get_raw_data();
$expected = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array(
'color' => array(
'custom' => false,
'duotone' => array(
array(
'slug' => 'value',
'colors' => array( 'red', 'green' ),
),
),
'gradients' => array(
'theme' => array(
array(
'slug' => 'gradient',
'gradient' => 'gradient',
),
),
),
'palette' => array(
'theme' => array(
array(
'slug' => 'red',
'color' => 'red',
),
),
),
),
'spacing' => array(
'customMargin' => false,
'units' => array( 'px', 'em' ),
),
'typography' => array(
'customLineHeight' => false,
'fontSizes' => array(
'theme' => array(
array(
'slug' => 'size',
'value' => 'size',
),
),
),
),
),
);
$this->assertEqualSetsWithIndex( $expected, $actual );
}
/**
* @ticket 52991
*/