From d6cbacad1a17dd5e9d052ba4cd7c54ae8831bb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Thu, 8 Jul 2021 15:17:34 +0200 Subject: [PATCH] Allow themes to provide empty values for `color.duotone` and `spacing.units` and remove `layout.units` (#33280) --- lib/class-wp-theme-json-gutenberg.php | 4 +- packages/block-editor/src/hooks/layout.js | 2 +- packages/block-library/src/column/edit.js | 2 +- .../block-library/src/column/edit.native.js | 2 +- .../block-library/src/columns/edit.native.js | 2 +- phpunit/class-wp-theme-json-test.php | 197 ++++++++++++++++++ 6 files changed, 203 insertions(+), 6 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index c7a38362c964f9..19785910251b28 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -1172,8 +1172,8 @@ public function merge( $incoming ) { 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 ) ) { gutenberg_experimental_set( $this->theme_json, $path, $node ); } } diff --git a/packages/block-editor/src/hooks/layout.js b/packages/block-editor/src/hooks/layout.js index 14a540cf3284fd..95f9bdd77dede3 100644 --- a/packages/block-editor/src/hooks/layout.js +++ b/packages/block-editor/src/hooks/layout.js @@ -39,7 +39,7 @@ function LayoutPanel( { setAttributes, attributes } ) { }, [] ); const units = useCustomUnits( { - availableUnits: useSetting( 'layout.units' ) || [ + availableUnits: useSetting( 'spacing.units' ) || [ '%', 'px', 'em', diff --git a/packages/block-library/src/column/edit.js b/packages/block-library/src/column/edit.js index ae8d3083e2f01d..ebd91ee0b4925c 100644 --- a/packages/block-library/src/column/edit.js +++ b/packages/block-library/src/column/edit.js @@ -34,7 +34,7 @@ function ColumnEdit( { } ); const units = useCustomUnits( { - availableUnits: useSetting( 'layout.units' ) || [ + availableUnits: useSetting( 'spacing.units' ) || [ '%', 'px', 'em', diff --git a/packages/block-library/src/column/edit.native.js b/packages/block-library/src/column/edit.native.js index e30db2f4b0fd87..71bab15b4f9245 100644 --- a/packages/block-library/src/column/edit.native.js +++ b/packages/block-library/src/column/edit.native.js @@ -65,7 +65,7 @@ function ColumnEdit( { const [ widthUnit, setWidthUnit ] = useState( valueUnit || '%' ); const units = useCustomUnits( { - availableUnits: useSetting( 'layout.units' ) || [ + availableUnits: useSetting( 'spacing.units' ) || [ '%', 'px', 'em', diff --git a/packages/block-library/src/columns/edit.native.js b/packages/block-library/src/columns/edit.native.js index e72fdabd88b572..0905167504e904 100644 --- a/packages/block-library/src/columns/edit.native.js +++ b/packages/block-library/src/columns/edit.native.js @@ -107,7 +107,7 @@ function ColumnsEditContainer( { const { width } = sizes || {}; const units = useCustomUnits( { - availableUnits: useSetting( 'layout.units' ) || [ + availableUnits: useSetting( 'spacing.units' ) || [ '%', 'px', 'em', diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 5b9cc20c3233d2..3df33165d62111 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -649,6 +649,203 @@ public function test_merge_incoming_data() { $this->assertEqualSetsWithIndex( $expected, $actual ); } + public function test_merge_incoming_data_empty_presets() { + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::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_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::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_Gutenberg::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 ); + } + + public function test_merge_incoming_data_null_presets() { + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::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_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::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_Gutenberg::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 ); + } + function test_remove_insecure_properties_removes_unsafe_styles() { $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array(