diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 8963e11095700c..51f8e7213c85ae 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -34,6 +34,12 @@ class WP_Theme_JSON_Gutenberg { */ const ROOT_BLOCK_SELECTOR = 'body'; + const VALID_ORIGINS = array( + 'core', + 'theme', + 'user', + ); + const VALID_TOP_LEVEL_KEYS = array( 'customTemplates', 'templateParts', @@ -277,9 +283,14 @@ class WP_Theme_JSON_Gutenberg { /** * Constructor. * - * @param array $theme_json A structure that follows the theme.json schema. + * @param array $theme_json A structure that follows the theme.json schema. + * @param string $origin What source of data this object represents. One of core, theme, or user. Default: theme. */ - public function __construct( $theme_json = array() ) { + public function __construct( $theme_json = array(), $origin = 'theme' ) { + if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) { + $origin = 'theme'; + } + // The old format is not meant to be ported to core. // We can remove it at that point. if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { @@ -289,6 +300,18 @@ public function __construct( $theme_json = array() ) { $valid_block_names = array_keys( self::get_blocks_metadata() ); $valid_element_names = array_keys( self::ELEMENTS ); $this->theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + + // Internally, presets are keyed by origin. + $nodes = self::get_setting_nodes( $this->theme_json ); + foreach ( $nodes as $node ) { + foreach ( self::PRESETS_METADATA as $preset ) { + $path = array_merge( $node['path'], $preset['path'] ); + $preset = _wp_array_get( $this->theme_json, $path, array() ); + if ( ! empty( $preset ) ) { + gutenberg_experimental_set( $this->theme_json, $path, array( $origin => $preset ) ); + } + } + } } /** @@ -657,9 +680,8 @@ private static function append_to_selector( $selector, $to_append ) { * @return array Array of presets where each key is a slug and each value is the preset value. */ private static function get_merged_preset_by_slug( $preset_per_origin, $value_key ) { - $origins = array( 'core', 'theme', 'user' ); - $result = array(); - foreach ( $origins as $origin ) { + $result = array(); + foreach ( self::VALID_ORIGINS as $origin ) { if ( ! isset( $preset_per_origin[ $origin ] ) ) { continue; } @@ -1133,59 +1155,34 @@ public function get_stylesheet( $type = 'all' ) { * Merge new incoming data. * * @param WP_Theme_JSON $incoming Data to merge. - * @param string $origin origin of the incoming data (e.g: core, theme, or user). */ - public function merge( $incoming, $origin ) { - + public function merge( $incoming ) { $incoming_data = $incoming->get_raw_data(); $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); // The array_replace_recursive algorithm merges at the leaf level. // For leaf values that are arrays it will use the numeric indexes for replacement. - // In those cases, what we want is to use the incoming value, if it exists. - // - // These are the cases that have array values at the leaf levels. - $properties = array(); - $properties[] = array( 'custom' ); - $properties[] = array( 'spacing', 'units' ); - $properties[] = array( 'color', 'duotone' ); - - $to_append = array(); - $to_append[] = array( 'color', 'palette' ); - $to_append[] = array( 'color', 'gradients' ); - $to_append[] = array( 'typography', 'fontSizes' ); - $to_append[] = array( 'typography', 'fontFamilies' ); + // In those cases, we want to replace the existing with the incoming value, if it exists. + $to_replace = array(); + $to_replace[] = array( 'custom' ); + $to_replace[] = array( 'spacing', 'units' ); + $to_replace[] = array( 'color', 'duotone' ); + foreach ( self::VALID_ORIGINS as $origin ) { + $to_replace[] = array( 'color', 'palette', $origin ); + $to_replace[] = array( 'color', 'gradients', $origin ); + $to_replace[] = array( 'typography', 'fontSizes', $origin ); + $to_replace[] = array( 'typography', 'fontFamilies', $origin ); + } $nodes = self::get_setting_nodes( $this->theme_json ); foreach ( $nodes as $metadata ) { - foreach ( $properties as $property_path ) { + foreach ( $to_replace as $property_path ) { $path = array_merge( $metadata['path'], $property_path ); $node = _wp_array_get( $incoming_data, $path, array() ); if ( ! empty( $node ) ) { gutenberg_experimental_set( $this->theme_json, $path, $node ); } } - - foreach ( $to_append as $property_path ) { - $path = array_merge( $metadata['path'], $property_path ); - $node = _wp_array_get( $incoming_data, $path, null ); - if ( null !== $node ) { - $existing_node = _wp_array_get( $this->theme_json, $path, null ); - $new_node = array_filter( - $existing_node, - function ( $key ) { - return in_array( $key, array( 'core', 'theme', 'user ' ), true ); - }, - ARRAY_FILTER_USE_KEY - ); - if ( isset( $node[ $origin ] ) ) { - $new_node[ $origin ] = $node[ $origin ]; - } else { - $new_node[ $origin ] = $node; - } - gutenberg_experimental_set( $this->theme_json, $path, $new_node ); - } - } } } @@ -1282,14 +1279,26 @@ private static function is_safe_css_declaration( $property_name, $property_value /** * Removes insecure data from theme.json. + * + * @param array $theme_json Structure to sanitize. + * + * @return array Sanitized structure. */ - public function remove_insecure_properties() { + public static function remove_insecure_properties( $theme_json ) { $sanitized = array(); + if ( ! isset( $theme_json['version'] ) || 0 === $theme_json['version'] ) { + $theme_json = WP_Theme_JSON_Schema_V0::parse( $theme_json ); + } + + $valid_block_names = array_keys( self::get_blocks_metadata() ); + $valid_element_names = array_keys( self::ELEMENTS ); + $theme_json = self::sanitize( $theme_json, $valid_block_names, $valid_element_names ); + $blocks_metadata = self::get_blocks_metadata(); - $style_nodes = self::get_style_nodes( $this->theme_json, $blocks_metadata ); + $style_nodes = self::get_style_nodes( $theme_json, $blocks_metadata ); foreach ( $style_nodes as $metadata ) { - $input = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); if ( empty( $input ) ) { continue; } @@ -1300,9 +1309,9 @@ public function remove_insecure_properties() { } } - $setting_nodes = self::get_setting_nodes( $this->theme_json ); + $setting_nodes = self::get_setting_nodes( $theme_json ); foreach ( $setting_nodes as $metadata ) { - $input = _wp_array_get( $this->theme_json, $metadata['path'], array() ); + $input = _wp_array_get( $theme_json, $metadata['path'], array() ); if ( empty( $input ) ) { continue; } @@ -1314,17 +1323,18 @@ public function remove_insecure_properties() { } if ( empty( $sanitized['styles'] ) ) { - unset( $this->theme_json['styles'] ); + unset( $theme_json['styles'] ); } else { - $this->theme_json['styles'] = $sanitized['styles']; + $theme_json['styles'] = $sanitized['styles']; } if ( empty( $sanitized['settings'] ) ) { - unset( $this->theme_json['settings'] ); + unset( $theme_json['settings'] ); } else { - $this->theme_json['settings'] = $sanitized['settings']; + $theme_json['settings'] = $sanitized['settings']; } + return $theme_json; } /** diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index e37264d25319b2..1f6cfe6d30ce85 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -250,7 +250,7 @@ public static function get_core_data() { $config = self::read_json_file( __DIR__ . '/experimental-default-theme.json' ); $config = self::translate( $config ); - self::$core = new WP_Theme_JSON_Gutenberg( $config ); + self::$core = new WP_Theme_JSON_Gutenberg( $config, 'core' ); return self::$core; } @@ -290,7 +290,7 @@ public static function get_theme_data( $theme_support_data = array() ) { * to override the ones declared via add_theme_support. */ $with_theme_supports = new WP_Theme_JSON_Gutenberg( $theme_support_data ); - $with_theme_supports->merge( self::$theme, 'theme' ); + $with_theme_supports->merge( self::$theme ); return $with_theme_supports; } @@ -368,7 +368,7 @@ public static function get_user_data() { $json_decoding_error = json_last_error(); if ( JSON_ERROR_NONE !== $json_decoding_error ) { trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() ); - return new WP_Theme_JSON_Gutenberg( $config ); + return new WP_Theme_JSON_Gutenberg( $config, 'user' ); } // Very important to verify if the flag isGlobalStylesUserThemeJSON is true. @@ -382,7 +382,7 @@ public static function get_user_data() { $config = $decoded_data; } } - self::$user = new WP_Theme_JSON_Gutenberg( $config ); + self::$user = new WP_Theme_JSON_Gutenberg( $config, 'user' ); return self::$user; } @@ -415,11 +415,11 @@ public static function get_merged_data( $settings = array(), $origin = 'user' ) $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $settings ); $result = new WP_Theme_JSON_Gutenberg(); - $result->merge( self::get_core_data(), 'core' ); - $result->merge( self::get_theme_data( $theme_support_data ), 'theme' ); + $result->merge( self::get_core_data() ); + $result->merge( self::get_theme_data( $theme_support_data ) ); if ( 'user' === $origin ) { - $result->merge( self::get_user_data(), 'user' ); + $result->merge( self::get_user_data() ); } return $result; diff --git a/lib/client-assets.php b/lib/client-assets.php index ca067c947db705..1562679c951c37 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -236,7 +236,7 @@ function gutenberg_register_packages_scripts( $scripts ) { // When in production, use the plugin's version as the default asset version; // else (for development or test) default to use the current time. $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); - $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.js' : '.min.js'; + $suffix = '.min.js'; foreach ( glob( gutenberg_dir_path() . "build/*/index$suffix" ) as $path ) { // Prefix `wp-` to package directory to get script handle. diff --git a/lib/global-styles.php b/lib/global-styles.php index 5b3fe46f6533fc..7fb4d7ced95af9 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -240,9 +240,9 @@ function gutenberg_global_styles_filter_post( $content ) { $decoded_data['isGlobalStylesUserThemeJSON'] ) { unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); - $theme_json = new WP_Theme_JSON_Gutenberg( $decoded_data ); - $theme_json->remove_insecure_properties(); - $data_to_encode = $theme_json->get_raw_data(); + + $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data ); + $data_to_encode['isGlobalStylesUserThemeJSON'] = true; return wp_json_encode( $data_to_encode ); } diff --git a/packages/data/src/components/use-select/index.js b/packages/data/src/components/use-select/index.js index 54476fc229bbbd..1941305b764745 100644 --- a/packages/data/src/components/use-select/index.js +++ b/packages/data/src/components/use-select/index.js @@ -148,12 +148,11 @@ export default function useSelect( _mapSelect, deps ) { errorMessage += `\nThe error may be correlated with this previous error:\n`; errorMessage += `${ latestMapOutputError.current.stack }\n\n`; errorMessage += 'Original stack trace:'; - - throw new Error( errorMessage ); - } else { - // eslint-disable-next-line no-console - console.error( errorMessage ); } + + // eslint-disable-next-line no-console + console.error( errorMessage ); + mapOutput = latestMapOutput.current; } } diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 9b1c6792a299bf..ce003aeb6087b7 100644 --- a/phpunit/class-wp-theme-json-resolver-test.php +++ b/phpunit/class-wp-theme-json-resolver-test.php @@ -155,15 +155,17 @@ function test_translations_are_applied() { array( 'color' => array( 'palette' => array( - array( - 'slug' => 'light', - 'name' => 'Jasny', - 'color' => '#f5f7f9', - ), - array( - 'slug' => 'dark', - 'name' => 'Ciemny', - 'color' => '#000', + 'theme' => array( + array( + 'slug' => 'light', + 'name' => 'Jasny', + 'color' => '#f5f7f9', + ), + array( + 'slug' => 'dark', + 'name' => 'Ciemny', + 'color' => '#000', + ), ), ), 'custom' => false, @@ -172,10 +174,12 @@ function test_translations_are_applied() { 'core/paragraph' => array( 'color' => array( 'palette' => array( - array( - 'slug' => 'light', - 'name' => 'Jasny', - 'color' => '#f5f7f9', + 'theme' => array( + array( + 'slug' => 'light', + 'name' => 'Jasny', + 'color' => '#f5f7f9', + ), ), ), ), diff --git a/phpunit/class-wp-theme-json-schema-v0-test.php b/phpunit/class-wp-theme-json-schema-v0-test.php index d2fd23e04edf49..bb7828d0e2fa0a 100644 --- a/phpunit/class-wp-theme-json-schema-v0-test.php +++ b/phpunit/class-wp-theme-json-schema-v0-test.php @@ -283,9 +283,11 @@ function test_get_settings() { 'custom' => false, 'customGradient' => false, 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', + 'theme' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), ), ), ), @@ -295,9 +297,11 @@ function test_get_settings() { 'customGradient' => false, 'custom' => false, 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', + 'theme' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), ), ), ), @@ -307,9 +311,11 @@ function test_get_settings() { 'customGradient' => false, 'custom' => false, 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', + 'theme' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), ), ), ), @@ -319,9 +325,11 @@ function test_get_settings() { 'customGradient' => false, 'custom' => false, 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', + 'theme' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), ), ), ), @@ -459,8 +467,7 @@ function test_get_stylesheet() { ), 'misc' => 'value', ) - ), - 'core' + ) ); $this->assertEquals( diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index c38aa1d9ae768c..4cba5bc5c7864b 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -56,112 +56,233 @@ function test_get_settings() { $this->assertEqualSetsWithIndex( $expected, $actual ); } - function test_get_stylesheet() { - $theme_json = new WP_Theme_JSON_Gutenberg( array() ); - $theme_json->merge( - new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'color' => array( - 'text' => 'value', - 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', - ), + function test_get_settings_presets_are_keyed_by_origin() { + $core_origin = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'white', + 'color' => 'white', ), ), - 'typography' => array( - 'fontFamilies' => array( - array( - 'slug' => 'small', - 'fontFamily' => '14px', - ), - array( - 'slug' => 'big', - 'fontFamily' => '41px', + ), + 'invalid/key' => 'value', + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'white', + 'color' => 'white', + ), ), ), ), - 'misc' => 'value', - 'blocks' => array( - 'core/group' => array( - 'custom' => array( - 'base-font' => 16, - 'line-height' => array( - 'small' => 1.2, - 'medium' => 1.4, - 'large' => 1.8, + ), + ), + ), + 'core' + ); + $no_origin = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'black', + 'color' => 'black', + ), + ), + ), + 'invalid/key' => 'value', + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'black', + 'color' => 'black', ), ), ), ), ), - 'styles' => array( - 'color' => array( - 'text' => 'var:preset|color|grey', + ), + ) + ); + + $actual_core = $core_origin->get_raw_data(); + $actual_no_origin = $no_origin->get_raw_data(); + + $expected_core = array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'palette' => array( + 'core' => array( + array( + 'slug' => 'white', + 'color' => 'white', + ), ), - 'misc' => 'value', - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => '#111', - 'background' => '#333', + ), + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'palette' => array( + 'core' => array( + array( + 'slug' => 'white', + 'color' => 'white', + ), ), ), ), - 'blocks' => array( - 'core/group' => array( - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => '#111', - ), + ), + ), + ), + ); + $expected_no_origin = array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'palette' => array( + 'theme' => array( + array( + 'slug' => 'black', + 'color' => 'black', + ), + ), + ), + ), + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'palette' => array( + 'theme' => array( + array( + 'slug' => 'black', + 'color' => 'black', ), ), - 'spacing' => array( - 'padding' => array( - 'top' => '12px', - 'bottom' => '24px', + ), + ), + ), + ), + ), + ); + + $this->assertEqualSetsWithIndex( $expected_core, $actual_core ); + $this->assertEqualSetsWithIndex( $expected_no_origin, $actual_no_origin ); + } + + function test_get_stylesheet() { + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'text' => 'value', + 'palette' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), + ), + ), + 'typography' => array( + 'fontFamilies' => array( + array( + 'slug' => 'small', + 'fontFamily' => '14px', + ), + array( + 'slug' => 'big', + 'fontFamily' => '41px', + ), + ), + ), + 'misc' => 'value', + 'blocks' => array( + 'core/group' => array( + 'custom' => array( + 'base-font' => 16, + 'line-height' => array( + 'small' => 1.2, + 'medium' => 1.4, + 'large' => 1.8, + ), + ), + ), + ), + ), + 'styles' => array( + 'color' => array( + 'text' => 'var:preset|color|grey', + ), + 'misc' => 'value', + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', + 'background' => '#333', + ), + ), + ), + 'blocks' => array( + 'core/group' => array( + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', ), ), ), - 'core/heading' => array( - 'color' => array( - 'text' => '#123456', + 'spacing' => array( + 'padding' => array( + 'top' => '12px', + 'bottom' => '24px', ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => '#111', - 'background' => '#333', - ), - 'typography' => array( - 'fontSize' => '60px', - ), + ), + ), + 'core/heading' => array( + 'color' => array( + 'text' => '#123456', + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', + 'background' => '#333', + ), + 'typography' => array( + 'fontSize' => '60px', ), ), ), - 'core/post-date' => array( - 'color' => array( - 'text' => '#123456', - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'background' => '#777', - 'text' => '#555', - ), + ), + 'core/post-date' => array( + 'color' => array( + 'text' => '#123456', + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'background' => '#777', + 'text' => '#555', ), ), ), ), ), - 'misc' => 'value', - ) - ), - 'core' + ), + 'misc' => 'value', + ) ); $this->assertEquals( @@ -179,28 +300,24 @@ function test_get_stylesheet() { } function test_get_stylesheet_preset_classes_work_with_compounded_selectors() { - $theme_json = new WP_Theme_JSON_Gutenberg( array() ); - $theme_json->merge( - new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'blocks' => array( - 'core/heading' => array( - 'color' => array( - 'palette' => array( - array( - 'slug' => 'white', - 'color' => '#fff', - ), + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'blocks' => array( + 'core/heading' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'white', + 'color' => '#fff', ), ), ), ), ), - ) - ), - 'theme' + ), + ) ); $this->assertEquals( @@ -210,37 +327,33 @@ function test_get_stylesheet_preset_classes_work_with_compounded_selectors() { } function test_get_stylesheet_preset_rules_come_after_block_rules() { - $theme_json = new WP_Theme_JSON_Gutenberg( array() ); - $theme_json->merge( - new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'blocks' => array( - 'core/group' => array( - 'color' => array( - 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', - ), + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', ), ), ), ), ), - 'styles' => array( - 'blocks' => array( - 'core/group' => array( - 'color' => array( - 'text' => 'red', - ), + ), + 'styles' => array( + 'blocks' => array( + 'core/group' => array( + 'color' => array( + 'text' => 'red', ), ), ), - ) - ), - 'theme' + ), + ) ); $this->assertEquals( @@ -254,36 +367,33 @@ function test_get_stylesheet_preset_rules_come_after_block_rules() { } public function test_get_stylesheet_preset_values_are_marked_as_important() { - $theme_json = new WP_Theme_JSON_Gutenberg( array() ); - $theme_json->merge( - new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'color' => array( - 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', - ), + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', ), ), ), - 'styles' => array( - 'blocks' => array( - 'core/paragraph' => array( - 'color' => array( - 'text' => 'red', - 'background' => 'blue', - ), - 'typography' => array( - 'fontSize' => '12px', - 'lineHeight' => '1.3', - ), + ), + 'styles' => array( + 'blocks' => array( + 'core/paragraph' => array( + 'color' => array( + 'text' => 'red', + 'background' => 'blue', + ), + 'typography' => array( + 'fontSize' => '12px', + 'lineHeight' => '1.3', ), ), ), - ) + ), ), 'core' ); @@ -295,42 +405,37 @@ public function test_get_stylesheet_preset_values_are_marked_as_important() { } public function test_merge_incoming_data() { - $theme_json = new WP_Theme_JSON_Gutenberg( array() ); - $theme_json->merge( - new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'color' => array( - 'custom' => false, - 'palette' => array( - array( - 'slug' => 'red', - 'color' => 'red', - ), - array( - 'slug' => 'green', - 'color' => 'green', - ), + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( + 'custom' => false, + 'palette' => array( + array( + 'slug' => 'red', + 'color' => 'red', ), - ), - 'blocks' => array( - 'core/paragraph' => array( - 'color' => array( - 'custom' => false, - ), + array( + 'slug' => 'green', + 'color' => 'green', ), ), ), - 'styles' => array( - 'typography' => array( - 'fontSize' => '12', + 'blocks' => array( + 'core/paragraph' => array( + 'color' => array( + 'custom' => false, + ), ), ), - - ) - ), - 'core' + ), + 'styles' => array( + 'typography' => array( + 'fontSize' => '12', + ), + ), + ) ); $add_new_block = array( @@ -460,7 +565,7 @@ public function test_merge_incoming_data() { 'custom' => true, 'customGradient' => true, 'palette' => array( - 'core' => array( + 'theme' => array( array( 'slug' => 'blue', 'color' => 'blue', @@ -468,7 +573,7 @@ public function test_merge_incoming_data() { ), ), 'gradients' => array( - 'core' => array( + 'theme' => array( array( 'slug' => 'gradient', 'gradient' => 'gradient', @@ -478,7 +583,7 @@ public function test_merge_incoming_data() { ), 'typography' => array( 'fontSizes' => array( - 'core' => array( + 'theme' => array( array( 'slug' => 'fontSize', 'size' => 'fontSize', @@ -486,7 +591,7 @@ public function test_merge_incoming_data() { ), ), 'fontFamilies' => array( - 'core' => array( + 'theme' => array( array( 'slug' => 'fontFamily', 'fontFamily' => 'fontFamily', @@ -532,20 +637,20 @@ public function test_merge_incoming_data() { ), ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_new_block ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_key_in_settings ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $update_key_in_settings ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_styles ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_key_in_styles ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_invalid_context ), 'core' ); - $theme_json->merge( new WP_Theme_JSON_Gutenberg( $update_presets ), 'core' ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_new_block ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_key_in_settings ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $update_key_in_settings ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_styles ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_key_in_styles ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $add_invalid_context ) ); + $theme_json->merge( new WP_Theme_JSON_Gutenberg( $update_presets ) ); $actual = $theme_json->get_raw_data(); $this->assertEqualSetsWithIndex( $expected, $actual ); } function test_remove_insecure_properties_removes_unsafe_styles() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -582,11 +687,9 @@ function test_remove_insecure_properties_removes_unsafe_styles() { ), ), ), - ), - true + ) ); - $theme_json->remove_insecure_properties(); - $actual = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -621,7 +724,7 @@ function test_remove_insecure_properties_removes_unsafe_styles() { } function test_remove_insecure_properties_removes_unsafe_styles_sub_properties() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -673,8 +776,7 @@ function test_remove_insecure_properties_removes_unsafe_styles_sub_properties() ), true ); - $theme_json->remove_insecure_properties(); - $actual = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -724,7 +826,7 @@ function test_remove_insecure_properties_removes_unsafe_styles_sub_properties() } function test_remove_insecure_properties_removes_non_preset_settings() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( @@ -779,11 +881,9 @@ function test_remove_insecure_properties_removes_non_preset_settings() { ), ), ), - ), - true + ) ); - $theme_json->remove_insecure_properties(); - $result = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( @@ -831,11 +931,11 @@ function test_remove_insecure_properties_removes_non_preset_settings() { ), ), ); - $this->assertEqualSetsWithIndex( $expected, $result ); + $this->assertEqualSetsWithIndex( $expected, $actual ); } function test_remove_insecure_properties_removes_unsafe_preset_settings() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( @@ -916,11 +1016,9 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { ), ), ), - ), - true + ) ); - $theme_json->remove_insecure_properties(); - $result = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( @@ -957,11 +1055,11 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { ), ), ); - $this->assertEqualSetsWithIndex( $expected, $result ); + $this->assertEqualSetsWithIndex( $expected, $actual ); } function test_remove_insecure_properties_applies_safe_styles() { - $theme_json = new WP_Theme_JSON_Gutenberg( + $actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -972,8 +1070,7 @@ function test_remove_insecure_properties_applies_safe_styles() { ), true ); - $theme_json->remove_insecure_properties(); - $result = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'styles' => array( @@ -982,7 +1079,7 @@ function test_remove_insecure_properties_applies_safe_styles() { ), ), ); - $this->assertEqualSetsWithIndex( $expected, $result ); + $this->assertEqualSetsWithIndex( $expected, $actual ); } function test_get_custom_templates() { diff --git a/webpack.config.js b/webpack.config.js index ec981656d18bb2..967f88733a0b7f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -319,7 +319,7 @@ module.exports = { ] ) ), new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ), - new ReadableJsAssetsWebpackPlugin(), + mode === 'production' && new ReadableJsAssetsWebpackPlugin(), ].filter( Boolean ), watchOptions: { ignored: [ '**/node_modules', '**/packages/*/src' ],