Skip to content

Commit

Permalink
Fix how appearanceTools works (#37254)
Browse files Browse the repository at this point in the history
* Only opt-in into tools if it is actually true

* Fix lint issues

* Allow null values in the props

* Fix lint issue
  • Loading branch information
oandregal committed Dec 10, 2021
1 parent ef384b3 commit 47f6ee8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/compat/wordpress-5.9/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,16 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) {
private static function maybe_opt_in_into_settings( $theme_json ) {
$new_theme_json = $theme_json;

if ( isset( $new_theme_json['settings']['appearanceTools'] ) ) {
if (
isset( $new_theme_json['settings']['appearanceTools'] ) &&
true === $new_theme_json['settings']['appearanceTools']
) {
self::do_opt_in_into_settings( $new_theme_json['settings'] );
}

if ( isset( $new_theme_json['settings']['blocks'] ) && is_array( $new_theme_json['settings']['blocks'] ) ) {
foreach ( $new_theme_json['settings']['blocks'] as &$block ) {
if ( isset( $block['appearanceTools'] ) ) {
if ( isset( $block['appearanceTools'] ) && ( true === $block['appearanceTools'] ) ) {
self::do_opt_in_into_settings( $block );
}
}
Expand Down Expand Up @@ -393,7 +396,9 @@ private static function do_opt_in_into_settings( &$context ) {
);

foreach ( $to_opt_in as $path ) {
if ( null === _wp_array_get( $context, $path, null ) ) {
// Use "unset prop" as a marker instead of "null" because
// "null" can be a valid value for some props (e.g. blockGap).
if ( 'unset prop' === _wp_array_get( $context, $path, 'unset prop' ) ) {
_wp_array_set( $context, $path, true );
}
}
Expand Down
60 changes: 57 additions & 3 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,15 @@ function test_get_settings_presets_are_keyed_by_origin() {
$this->assertEqualSetsWithIndex( $expected_no_origin, $actual_no_origin );
}

function test_get_settings_using_opt_in_key() {
function test_get_settings_appearance_true_opts_in() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'appearanceTools' => true,
'spacing' => array(
'blockGap' => false, // This should override appearanceTools.
),
'blocks' => array(
'core/paragraph' => array(
'typography' => array(
Expand All @@ -205,6 +208,9 @@ function test_get_settings_using_opt_in_key() {
'typography' => array(
'lineHeight' => false, // This should override appearanceTools.
),
'spacing' => array(
'blockGap' => null,
),
),
),
),
Expand All @@ -223,7 +229,7 @@ function test_get_settings_using_opt_in_key() {
'link' => true,
),
'spacing' => array(
'blockGap' => true,
'blockGap' => false,
'margin' => true,
'padding' => true,
),
Expand All @@ -247,7 +253,7 @@ function test_get_settings_using_opt_in_key() {
'link' => true,
),
'spacing' => array(
'blockGap' => true,
'blockGap' => false,
'margin' => true,
'padding' => true,
),
Expand All @@ -261,6 +267,54 @@ function test_get_settings_using_opt_in_key() {
$this->assertEqualSetsWithIndex( $expected, $actual );
}

function test_get_settings_appearance_false_does_not_opt_in() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'appearanceTools' => false,
'border' => array(
'width' => true,
),
'blocks' => array(
'core/paragraph' => array(
'typography' => array(
'lineHeight' => false,
),
),
'core/group' => array(
'typography' => array(
'lineHeight' => false,
),
),
),
),
)
);

$actual = $theme_json->get_settings();
$expected = array(
'appearanceTools' => false,
'border' => array(
'width' => true,
),
'blocks' => array(
'core/paragraph' => array(
'typography' => array(
'lineHeight' => false,
),
),
'core/group' => array(
'typography' => array(
'lineHeight' => false,
),
),
),
);

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

function test_get_stylesheet_support_for_shorthand_and_longhand_values() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
Expand Down

0 comments on commit 47f6ee8

Please sign in to comment.