From 819a504381235247413f77194963c1076b206050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 11 Jun 2021 12:57:03 +0200 Subject: [PATCH 1/4] Make origin part of the object. We need to store the presets by keys upon object construction to make sure the in-memory shape of the structure is always correct. --- lib/class-wp-theme-json-gutenberg.php | 159 ++--- ...class-wp-theme-json-resolver-gutenberg.php | 14 +- lib/global-styles.php | 2 +- phpunit/class-wp-theme-json-resolver-test.php | 30 +- .../class-wp-theme-json-schema-v0-test.php | 35 +- phpunit/class-wp-theme-json-test.php | 567 +++++++++++------- 6 files changed, 477 insertions(+), 330 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 8963e11095700..0950aa86a9d49 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -18,6 +18,14 @@ class WP_Theme_JSON_Gutenberg { */ private $theme_json = null; + /** + * What source of data this object represents. + * One of VALID_ORIGINS. + * + * @var string + */ + private $origin = null; + /** * Holds block metadata extracted from block.json * to be shared among all instances so we don't @@ -34,6 +42,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', @@ -278,8 +292,15 @@ class WP_Theme_JSON_Gutenberg { * Constructor. * * @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 ) ) { + $this->origin = $origin; + } else { + $this->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 +310,28 @@ 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 ) ); + } + } + } + } + + /** + * Returns the origin of data. + * One of the valid origins: core, theme, user. + * + * @return string + */ + private function get_origin() { + return $this->origin; } /** @@ -657,9 +700,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 ) { + foreach ( self::VALID_ORIGINS as $origin ) { if ( ! isset( $preset_per_origin[ $origin ] ) ) { continue; } @@ -1133,59 +1175,35 @@ 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 ) { + $origin = $incoming->get_origin(); $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 ); - } - } } } @@ -1201,40 +1219,45 @@ function ( $key ) { private static function remove_insecure_settings( $input ) { $output = array(); foreach ( self::PRESETS_METADATA as $preset_metadata ) { - $current_preset = _wp_array_get( $input, $preset_metadata['path'], null ); - if ( null === $current_preset ) { + $preset_by_origin = _wp_array_get( $input, $preset_metadata['path'], null ); + if ( null === $preset_by_origin ) { continue; } - $escaped_preset = array(); - foreach ( $current_preset as $single_preset ) { - if ( - esc_attr( esc_html( $single_preset['name'] ) ) === $single_preset['name'] && - sanitize_html_class( $single_preset['slug'] ) === $single_preset['slug'] - ) { - $value = $single_preset[ $preset_metadata['value_key'] ]; - $single_preset_is_valid = null; - if ( isset( $preset_metadata['classes'] ) && count( $preset_metadata['classes'] ) > 0 ) { - $single_preset_is_valid = true; - foreach ( $preset_metadata['classes'] as $class_meta_data ) { - $property = $class_meta_data['property_name']; - if ( ! self::is_safe_css_declaration( $property, $value ) ) { - $single_preset_is_valid = false; - break; + foreach ( self::VALID_ORIGINS as $origin ) { + if ( ! isset( $preset_by_origin[ $origin ] ) ) { + continue; + } + + $escaped_preset = array(); + foreach ( $preset_by_origin[ $origin ] as $single_preset ) { + if ( + esc_attr( esc_html( $single_preset['name'] ) ) === $single_preset['name'] && + sanitize_html_class( $single_preset['slug'] ) === $single_preset['slug'] + ) { + $value = $single_preset[ $preset_metadata['value_key'] ]; + $single_preset_is_valid = null; + if ( isset( $preset_metadata['classes'] ) && count( $preset_metadata['classes'] ) > 0 ) { + $single_preset_is_valid = true; + foreach ( $preset_metadata['classes'] as $class_meta_data ) { + $property = $class_meta_data['property_name']; + if ( ! self::is_safe_css_declaration( $property, $value ) ) { + $single_preset_is_valid = false; + break; + } } + } else { + $property = $preset_metadata['css_var_infix']; + $single_preset_is_valid = self::is_safe_css_declaration( $property, $value ); + } + if ( $single_preset_is_valid ) { + $escaped_preset[] = $single_preset; } - } else { - $property = $preset_metadata['css_var_infix']; - $single_preset_is_valid = self::is_safe_css_declaration( $property, $value ); - } - if ( $single_preset_is_valid ) { - $escaped_preset[] = $single_preset; } } - } - - if ( ! empty( $escaped_preset ) ) { - gutenberg_experimental_set( $output, $preset_metadata['path'], $escaped_preset ); + if ( ! empty( $escaped_preset ) ) { + gutenberg_experimental_set( $output, array_merge( $preset_metadata['path'], array( $origin ) ), $escaped_preset ); + } } } diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index e37264d25319b..1f6cfe6d30ce8 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/global-styles.php b/lib/global-styles.php index 5b3fe46f6533f..18390eb05da7f 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -240,7 +240,7 @@ 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 = new WP_Theme_JSON_Gutenberg( $decoded_data, 'user' ); $theme_json->remove_insecure_properties(); $data_to_encode = $theme_json->get_raw_data(); $data_to_encode['isGlobalStylesUserThemeJSON'] = true; diff --git a/phpunit/class-wp-theme-json-resolver-test.php b/phpunit/class-wp-theme-json-resolver-test.php index 9b1c6792a299b..ce003aeb6087b 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 d2fd23e04edf4..bb7828d0e2fa0 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 c38aa1d9ae768..edd5f95ed5ac0 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,13 +637,13 @@ 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 ); @@ -779,8 +884,7 @@ function test_remove_insecure_properties_removes_non_preset_settings() { ), ), ), - ), - true + ) ); $theme_json->remove_insecure_properties(); $result = $theme_json->get_raw_data(); @@ -789,20 +893,22 @@ function test_remove_insecure_properties_removes_non_preset_settings() { 'settings' => array( 'color' => array( 'palette' => array( - array( - 'name' => 'Red', - 'slug' => 'red', - 'color' => '#ff0000', - ), - array( - 'name' => 'Green', - 'slug' => 'green', - 'color' => '#00ff00', - ), - array( - 'name' => 'Blue', - 'slug' => 'blue', - 'color' => '#0000ff', + 'theme' => array( + array( + 'name' => 'Red', + 'slug' => 'red', + 'color' => '#ff0000', + ), + array( + 'name' => 'Green', + 'slug' => 'green', + 'color' => '#00ff00', + ), + array( + 'name' => 'Blue', + 'slug' => 'blue', + 'color' => '#0000ff', + ), ), ), ), @@ -810,20 +916,22 @@ function test_remove_insecure_properties_removes_non_preset_settings() { 'core/group' => array( 'color' => array( 'palette' => array( - array( - 'name' => 'Yellow', - 'slug' => 'yellow', - 'color' => '#ff0000', - ), - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#00ff00', - ), - array( - 'name' => 'Orange', - 'slug' => 'orange', - 'color' => '#0000ff', + 'theme' => array( + array( + 'name' => 'Yellow', + 'slug' => 'yellow', + 'color' => '#ff0000', + ), + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#00ff00', + ), + array( + 'name' => 'Orange', + 'slug' => 'orange', + 'color' => '#0000ff', + ), ), ), ), @@ -916,8 +1024,7 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { ), ), ), - ), - true + ) ); $theme_json->remove_insecure_properties(); $result = $theme_json->get_raw_data(); @@ -926,19 +1033,23 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { 'settings' => array( 'color' => array( 'palette' => array( - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#FFC0CB', + 'theme' => array( + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#FFC0CB', + ), ), ), ), 'typography' => array( 'fontFamilies' => array( - array( - 'name' => 'Cambria', - 'slug' => 'cambria', - 'fontFamily' => 'Cambria, Georgia, serif', + 'theme' => array( + array( + 'name' => 'Cambria', + 'slug' => 'cambria', + 'fontFamily' => 'Cambria, Georgia, serif', + ), ), ), ), @@ -946,10 +1057,12 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { 'core/group' => array( 'color' => array( 'palette' => array( - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#FFC0CB', + 'theme' => array( + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#FFC0CB', + ), ), ), ), From 815d8d90f8e4e42bfd3b2a694f5b929056e42c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 11 Jun 2021 14:04:17 +0200 Subject: [PATCH 2/4] Fix lint issues --- lib/class-wp-theme-json-gutenberg.php | 8 ++++---- phpunit/class-wp-theme-json-test.php | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 0950aa86a9d49..4875a8cd33afa 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -45,7 +45,7 @@ class WP_Theme_JSON_Gutenberg { const VALID_ORIGINS = array( 'core', 'theme', - 'user ' + 'user', ); const VALID_TOP_LEVEL_KEYS = array( @@ -291,11 +291,11 @@ 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(), $origin = 'theme' ) { - if ( in_array( $origin, self::VALID_ORIGINS ) ) { + if ( in_array( $origin, self::VALID_ORIGINS, true ) ) { $this->origin = $origin; } else { $this->origin = 'theme'; @@ -700,7 +700,7 @@ 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 ) { - $result = array(); + $result = array(); foreach ( self::VALID_ORIGINS as $origin ) { if ( ! isset( $preset_per_origin[ $origin ] ) ) { continue; diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index edd5f95ed5ac0..60e0010446e05 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -86,7 +86,7 @@ function test_get_settings_presets_are_keyed_by_origin() { ), 'core' ); - $no_origin = new WP_Theme_JSON_Gutenberg( + $no_origin = new WP_Theme_JSON_Gutenberg( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( @@ -112,16 +112,16 @@ function test_get_settings_presets_are_keyed_by_origin() { ), ), ), - ), + ) ); $actual_core = $core_origin->get_raw_data(); $actual_no_origin = $no_origin->get_raw_data(); - $expected_core = array( + $expected_core = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( - 'color' => array( + 'color' => array( 'palette' => array( 'core' => array( array( @@ -131,7 +131,7 @@ function test_get_settings_presets_are_keyed_by_origin() { ), ), ), - 'blocks' => array( + 'blocks' => array( 'core/group' => array( 'color' => array( 'palette' => array( @@ -150,7 +150,7 @@ function test_get_settings_presets_are_keyed_by_origin() { $expected_no_origin = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( - 'color' => array( + 'color' => array( 'palette' => array( 'theme' => array( array( @@ -160,7 +160,7 @@ function test_get_settings_presets_are_keyed_by_origin() { ), ), ), - 'blocks' => array( + 'blocks' => array( 'core/group' => array( 'color' => array( 'palette' => array( From f84c4cb539e098147f656c69a1d30981cdb3923a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 11 Jun 2021 14:39:57 +0200 Subject: [PATCH 3/4] Refactor remove_insecure_data so it is origin agnostic --- lib/class-wp-theme-json-gutenberg.php | 90 ++++++++++--------- lib/global-styles.php | 6 +- phpunit/class-wp-theme-json-test.php | 124 +++++++++++--------------- 3 files changed, 106 insertions(+), 114 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 4875a8cd33afa..65b4a9e791f1e 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -1219,46 +1219,41 @@ public function merge( $incoming ) { private static function remove_insecure_settings( $input ) { $output = array(); foreach ( self::PRESETS_METADATA as $preset_metadata ) { - $preset_by_origin = _wp_array_get( $input, $preset_metadata['path'], null ); - if ( null === $preset_by_origin ) { + $current_preset = _wp_array_get( $input, $preset_metadata['path'], null ); + if ( null === $current_preset ) { continue; } - foreach ( self::VALID_ORIGINS as $origin ) { - if ( ! isset( $preset_by_origin[ $origin ] ) ) { - continue; - } - - $escaped_preset = array(); - foreach ( $preset_by_origin[ $origin ] as $single_preset ) { - if ( - esc_attr( esc_html( $single_preset['name'] ) ) === $single_preset['name'] && - sanitize_html_class( $single_preset['slug'] ) === $single_preset['slug'] - ) { - $value = $single_preset[ $preset_metadata['value_key'] ]; - $single_preset_is_valid = null; - if ( isset( $preset_metadata['classes'] ) && count( $preset_metadata['classes'] ) > 0 ) { - $single_preset_is_valid = true; - foreach ( $preset_metadata['classes'] as $class_meta_data ) { - $property = $class_meta_data['property_name']; - if ( ! self::is_safe_css_declaration( $property, $value ) ) { - $single_preset_is_valid = false; - break; - } + $escaped_preset = array(); + foreach ( $current_preset as $single_preset ) { + if ( + esc_attr( esc_html( $single_preset['name'] ) ) === $single_preset['name'] && + sanitize_html_class( $single_preset['slug'] ) === $single_preset['slug'] + ) { + $value = $single_preset[ $preset_metadata['value_key'] ]; + $single_preset_is_valid = null; + if ( isset( $preset_metadata['classes'] ) && count( $preset_metadata['classes'] ) > 0 ) { + $single_preset_is_valid = true; + foreach ( $preset_metadata['classes'] as $class_meta_data ) { + $property = $class_meta_data['property_name']; + if ( ! self::is_safe_css_declaration( $property, $value ) ) { + $single_preset_is_valid = false; + break; } - } else { - $property = $preset_metadata['css_var_infix']; - $single_preset_is_valid = self::is_safe_css_declaration( $property, $value ); - } - if ( $single_preset_is_valid ) { - $escaped_preset[] = $single_preset; } + } else { + $property = $preset_metadata['css_var_infix']; + $single_preset_is_valid = self::is_safe_css_declaration( $property, $value ); + } + if ( $single_preset_is_valid ) { + $escaped_preset[] = $single_preset; } - } - if ( ! empty( $escaped_preset ) ) { - gutenberg_experimental_set( $output, array_merge( $preset_metadata['path'], array( $origin ) ), $escaped_preset ); } } + + if ( ! empty( $escaped_preset ) ) { + gutenberg_experimental_set( $output, $preset_metadata['path'], $escaped_preset ); + } } return $output; @@ -1305,14 +1300,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; } @@ -1323,9 +1330,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; } @@ -1337,17 +1344,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/global-styles.php b/lib/global-styles.php index 18390eb05da7f..7fb4d7ced95af 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, 'user' ); - $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/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 60e0010446e05..4cba5bc5c7864 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -650,7 +650,7 @@ public function test_merge_incoming_data() { } 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( @@ -687,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( @@ -726,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( @@ -778,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( @@ -829,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( @@ -886,29 +883,26 @@ function test_remove_insecure_properties_removes_non_preset_settings() { ), ) ); - $theme_json->remove_insecure_properties(); - $result = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( 'color' => array( 'palette' => array( - 'theme' => array( - array( - 'name' => 'Red', - 'slug' => 'red', - 'color' => '#ff0000', - ), - array( - 'name' => 'Green', - 'slug' => 'green', - 'color' => '#00ff00', - ), - array( - 'name' => 'Blue', - 'slug' => 'blue', - 'color' => '#0000ff', - ), + array( + 'name' => 'Red', + 'slug' => 'red', + 'color' => '#ff0000', + ), + array( + 'name' => 'Green', + 'slug' => 'green', + 'color' => '#00ff00', + ), + array( + 'name' => 'Blue', + 'slug' => 'blue', + 'color' => '#0000ff', ), ), ), @@ -916,22 +910,20 @@ function test_remove_insecure_properties_removes_non_preset_settings() { 'core/group' => array( 'color' => array( 'palette' => array( - 'theme' => array( - array( - 'name' => 'Yellow', - 'slug' => 'yellow', - 'color' => '#ff0000', - ), - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#00ff00', - ), - array( - 'name' => 'Orange', - 'slug' => 'orange', - 'color' => '#0000ff', - ), + array( + 'name' => 'Yellow', + 'slug' => 'yellow', + 'color' => '#ff0000', + ), + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#00ff00', + ), + array( + 'name' => 'Orange', + 'slug' => 'orange', + 'color' => '#0000ff', ), ), ), @@ -939,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( @@ -1026,30 +1018,25 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { ), ) ); - $theme_json->remove_insecure_properties(); - $result = $theme_json->get_raw_data(); + $expected = array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, 'settings' => array( 'color' => array( 'palette' => array( - 'theme' => array( - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#FFC0CB', - ), + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#FFC0CB', ), ), ), 'typography' => array( 'fontFamilies' => array( - 'theme' => array( - array( - 'name' => 'Cambria', - 'slug' => 'cambria', - 'fontFamily' => 'Cambria, Georgia, serif', - ), + array( + 'name' => 'Cambria', + 'slug' => 'cambria', + 'fontFamily' => 'Cambria, Georgia, serif', ), ), ), @@ -1057,12 +1044,10 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { 'core/group' => array( 'color' => array( 'palette' => array( - 'theme' => array( - array( - 'name' => 'Pink', - 'slug' => 'pink', - 'color' => '#FFC0CB', - ), + array( + 'name' => 'Pink', + 'slug' => 'pink', + 'color' => '#FFC0CB', ), ), ), @@ -1070,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( @@ -1085,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( @@ -1095,7 +1079,7 @@ function test_remove_insecure_properties_applies_safe_styles() { ), ), ); - $this->assertEqualSetsWithIndex( $expected, $result ); + $this->assertEqualSetsWithIndex( $expected, $actual ); } function test_get_custom_templates() { From 20b8d044fafb738a85e801a5919274e549fcc542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Fri, 11 Jun 2021 17:48:42 +0200 Subject: [PATCH 4/4] We no longer need to store the $origin --- lib/class-wp-theme-json-gutenberg.php | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 65b4a9e791f1e..51f8e7213c85a 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -18,14 +18,6 @@ class WP_Theme_JSON_Gutenberg { */ private $theme_json = null; - /** - * What source of data this object represents. - * One of VALID_ORIGINS. - * - * @var string - */ - private $origin = null; - /** * Holds block metadata extracted from block.json * to be shared among all instances so we don't @@ -295,10 +287,8 @@ class WP_Theme_JSON_Gutenberg { * @param string $origin What source of data this object represents. One of core, theme, or user. Default: theme. */ public function __construct( $theme_json = array(), $origin = 'theme' ) { - if ( in_array( $origin, self::VALID_ORIGINS, true ) ) { - $this->origin = $origin; - } else { - $this->origin = 'theme'; + if ( ! in_array( $origin, self::VALID_ORIGINS, true ) ) { + $origin = 'theme'; } // The old format is not meant to be ported to core. @@ -324,16 +314,6 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { } } - /** - * Returns the origin of data. - * One of the valid origins: core, theme, user. - * - * @return string - */ - private function get_origin() { - return $this->origin; - } - /** * Sanitizes the input according to the schemas. * @@ -1177,7 +1157,6 @@ public function get_stylesheet( $type = 'all' ) { * @param WP_Theme_JSON $incoming Data to merge. */ public function merge( $incoming ) { - $origin = $incoming->get_origin(); $incoming_data = $incoming->get_raw_data(); $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data );