Skip to content

Commit 328ab83

Browse files
committed
Editor: Fix handling of default presets at block level in theme.json
Fix an issue where block-level presets do not work correctly when default presets are disabled and the slug matches a default preset. Props aaronrobertshaw, bph, ramonopoly, wildworks. Fixes #64195. git-svn-id: https://develop.svn.wordpress.org/trunk@61190 602fd350-edb4-49c9-b593-d223f7449a82
1 parent cb003b0 commit 328ab83

File tree

2 files changed

+217
-1
lines changed

2 files changed

+217
-1
lines changed

src/wp-includes/class-wp-theme-json.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,11 @@ public function merge( $incoming ) {
32503250
foreach ( static::PRESETS_METADATA as $preset_metadata ) {
32513251
$prevent_override = $preset_metadata['prevent_override'];
32523252
if ( is_array( $prevent_override ) ) {
3253-
$prevent_override = _wp_array_get( $this->theme_json['settings'], $preset_metadata['prevent_override'] );
3253+
$global_path = array_merge( array( 'settings' ), $prevent_override );
3254+
$global_value = _wp_array_get( $this->theme_json, $global_path, null );
3255+
3256+
$node_level_path = array_merge( $node['path'], $prevent_override );
3257+
$prevent_override = _wp_array_get( $this->theme_json, $node_level_path, $global_value );
32543258
}
32553259

32563260
foreach ( static::VALID_ORIGINS as $origin ) {

tests/phpunit/tests/theme/wpThemeJson.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6411,4 +6411,216 @@ public function test_opt_in_to_block_style_variations() {
64116411

64126412
$this->assertSame( $expected, $button_variations );
64136413
}
6414+
6415+
/**
6416+
* Tests that block-level settings inherit global default settings when not explicitly set.
6417+
*
6418+
* When a block doesn't have its own default presets setting, it should inherit
6419+
* the global setting from the theme. This affects whether default presets
6420+
* are filtered out during merging.
6421+
*
6422+
* @ticket 64195
6423+
*/
6424+
public function test_merge_incoming_data_block_level_inherits_global_default_setting() {
6425+
$defaults = new WP_Theme_JSON(
6426+
array(
6427+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6428+
'settings' => array(
6429+
'color' => array(
6430+
'defaultDuotone' => true,
6431+
'duotone' => array(
6432+
array(
6433+
'slug' => 'dark-grayscale',
6434+
'colors' => array( '#000000', '#7f7f7f' ),
6435+
'name' => 'Default Dark grayscale',
6436+
),
6437+
),
6438+
),
6439+
),
6440+
),
6441+
'default'
6442+
);
6443+
$theme = new WP_Theme_JSON(
6444+
array(
6445+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6446+
'settings' => array(
6447+
'color' => array(
6448+
'defaultDuotone' => false,
6449+
),
6450+
'blocks' => array(
6451+
'core/image' => array(
6452+
'color' => array(
6453+
// No defaultDuotone setting - should inherit global (false) set by theme.
6454+
'duotone' => array(
6455+
array(
6456+
'slug' => 'dark-grayscale',
6457+
'colors' => array( '#000000', '#7f7f7f' ),
6458+
'name' => 'Theme Dark grayscale',
6459+
),
6460+
),
6461+
),
6462+
),
6463+
'core/cover' => array(
6464+
'color' => array(
6465+
'defaultDuotone' => true, // Explicitly enabled at block level
6466+
'duotone' => array(
6467+
array(
6468+
'slug' => 'dark-grayscale',
6469+
'colors' => array( '#000000', '#7f7f7f' ),
6470+
'name' => 'Cover Dark grayscale',
6471+
),
6472+
),
6473+
),
6474+
),
6475+
),
6476+
),
6477+
)
6478+
);
6479+
6480+
$expected = array(
6481+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6482+
'settings' => array(
6483+
'color' => array(
6484+
'defaultDuotone' => false,
6485+
'duotone' => array(
6486+
'default' => array(
6487+
array(
6488+
'slug' => 'dark-grayscale',
6489+
'colors' => array( '#000000', '#7f7f7f' ),
6490+
'name' => 'Default Dark grayscale',
6491+
),
6492+
),
6493+
),
6494+
),
6495+
'blocks' => array(
6496+
'core/image' => array(
6497+
'color' => array(
6498+
'duotone' => array(
6499+
'theme' => array(
6500+
array(
6501+
'slug' => 'dark-grayscale',
6502+
'colors' => array( '#000000', '#7f7f7f' ),
6503+
'name' => 'Theme Dark grayscale',
6504+
),
6505+
),
6506+
),
6507+
),
6508+
),
6509+
'core/cover' => array(
6510+
'color' => array(
6511+
'defaultDuotone' => true,
6512+
'duotone' => array(
6513+
'theme' => array(
6514+
// Should be filtered out because block-level defaults are enabled
6515+
// and slug matches default
6516+
),
6517+
),
6518+
),
6519+
),
6520+
),
6521+
),
6522+
);
6523+
6524+
$defaults->merge( $theme );
6525+
$actual = $defaults->get_raw_data();
6526+
6527+
$this->assertEqualSetsWithIndex( $expected, $actual );
6528+
}
6529+
6530+
/**
6531+
* Tests that presets with unique slugs are preserved during merging.
6532+
*
6533+
* When merging theme presets, any preset with a slug that doesn't match
6534+
* a default preset should always be preserved, regardless of default
6535+
* preset settings. Only presets with matching slugs should be filtered out
6536+
* when defaults are enabled.
6537+
*
6538+
* @ticket 64195
6539+
*/
6540+
public function test_merge_incoming_data_unique_slugs_always_preserved() {
6541+
$defaults = new WP_Theme_JSON(
6542+
array(
6543+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6544+
'settings' => array(
6545+
'color' => array(
6546+
'defaultDuotone' => true, // Defaults enabled
6547+
'duotone' => array(
6548+
array(
6549+
'slug' => 'dark-grayscale',
6550+
'colors' => array( '#000000', '#7f7f7f' ),
6551+
'name' => 'Default Dark grayscale',
6552+
),
6553+
),
6554+
),
6555+
),
6556+
),
6557+
'default'
6558+
);
6559+
$theme = new WP_Theme_JSON(
6560+
array(
6561+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6562+
'settings' => array(
6563+
'blocks' => array(
6564+
'core/image' => array(
6565+
'color' => array(
6566+
'defaultDuotone' => true, // Block-level defaults enabled
6567+
'duotone' => array(
6568+
array(
6569+
'slug' => 'custom-unique',
6570+
'colors' => array( '#ff0000', '#00ff00' ),
6571+
'name' => 'Custom Unique',
6572+
),
6573+
array(
6574+
'slug' => 'dark-grayscale', // Matches default slug
6575+
'colors' => array( '#111111', '#888888' ),
6576+
'name' => 'Theme Dark grayscale',
6577+
),
6578+
),
6579+
),
6580+
),
6581+
),
6582+
),
6583+
)
6584+
);
6585+
6586+
$expected = array(
6587+
'version' => WP_Theme_JSON::LATEST_SCHEMA,
6588+
'settings' => array(
6589+
'color' => array(
6590+
'defaultDuotone' => true,
6591+
'duotone' => array(
6592+
'default' => array(
6593+
array(
6594+
'slug' => 'dark-grayscale',
6595+
'colors' => array( '#000000', '#7f7f7f' ),
6596+
'name' => 'Default Dark grayscale',
6597+
),
6598+
),
6599+
),
6600+
),
6601+
'blocks' => array(
6602+
'core/image' => array(
6603+
'color' => array(
6604+
'defaultDuotone' => true,
6605+
'duotone' => array(
6606+
'theme' => array(
6607+
array(
6608+
'slug' => 'custom-unique', // Should always be preserved
6609+
'colors' => array( '#ff0000', '#00ff00' ),
6610+
'name' => 'Custom Unique',
6611+
),
6612+
// 'dark-grayscale' should be filtered out due to slug match
6613+
),
6614+
),
6615+
),
6616+
),
6617+
),
6618+
),
6619+
);
6620+
6621+
$defaults->merge( $theme );
6622+
$actual = $defaults->get_raw_data();
6623+
6624+
$this->assertEqualSetsWithIndex( $expected, $actual );
6625+
}
64146626
}

0 commit comments

Comments
 (0)