From 40418ea8ede87b4d061c3b540a66ba12456453a5 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Wed, 16 Feb 2022 16:17:24 -0600 Subject: [PATCH] Squashed commits from WordPress/gutenberg#38681 commit 9e87c24d3be87dffd7dd396e93b865c871f413c6 Author: Alex Lende Date: Wed Feb 16 14:14:39 2022 -0600 Add example for get_metadata_boolean commit 0481c25c779551657fbb4d12d62ad82c8d73d66d Author: Alex Lende Date: Wed Feb 16 14:04:49 2022 -0600 Fix conditional hook commit 18b6f62213631e89662c2f057df0e0931ee2d836 Author: Alex Lende Date: Thu Feb 10 12:58:44 2022 -0600 Prevent generating other default presets commit 7729c2a8f3b864296049dd76eb18b47bf294f3eb Author: Alex Lende Date: Thu Feb 10 12:28:41 2022 -0600 Try refactor should_override_preset commit 23c5ed34e833957b9d0e56bf82a614d8db9bcb88 Author: Alex Lende Date: Thu Feb 10 11:24:17 2022 -0600 Use multi-origin duotone presets commit e90fe4c6cfd508eed67ba048592e6bf298d55a8a Author: Alex Lende Date: Thu Feb 10 10:54:49 2022 -0600 Try refactoring override path and bool to not be inverse commit b1b9fe39da4b0fc04026514fe79725c4c617823d Author: Alex Lende Date: Thu Feb 10 10:41:19 2022 -0600 Add defaultDuotone to prevent override commit 6713f8725320af0c074727326fe9b7aa219bb76d Author: Alex Lende Date: Wed Feb 9 15:55:18 2022 -0600 Update docs commit fb0d9a6975d316e79acd8cb198032c1739245dba Author: Alex Lende Date: Wed Feb 9 15:46:34 2022 -0600 Fix phpcs commit ab51b12808d728b08ee76923dbdf22b35f572308 Author: Alex Lende Date: Wed Feb 9 15:34:06 2022 -0600 Add defaultDuotone to JSON schema commit 07076eb55a2e7f43c217755e6284214bbabe6254 Author: Alex Lende Date: Wed Feb 9 14:54:04 2022 -0600 Disable duotone css vars too commit 50598cf7f96a8ab9c754c4ccfb65d11f78a0c3f7 Author: Alex Lende Date: Wed Feb 9 13:44:03 2022 -0600 Add defaultDuotone to disable default SVGs commit 5a45d0d447a1ac45d5d472d854b528ac1c0509dc Author: Alex Lende Date: Wed Feb 9 13:43:24 2022 -0600 Fix spacing --- .../class-wp-theme-json-resolver.php | 2 + src/wp-includes/class-wp-theme-json.php | 211 ++++++++++-------- src/wp-includes/theme.json | 17 +- 3 files changed, 127 insertions(+), 103 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index b9a3819d403a7..bf9add2ee9c15 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -208,6 +208,8 @@ public static function get_theme_data( $deprecated = array() ) { $default_gradients = true; } $theme_support_data['settings']['color']['defaultGradients'] = $default_gradients; + + $theme_support_data['settings']['color']['defaultDuotone'] = false; } $with_theme_supports = new WP_Theme_JSON( $theme_support_data ); $with_theme_supports->merge( self::$theme ); diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 9ed88ddf22854..7a1380e7a16f0 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -74,31 +74,38 @@ class WP_Theme_JSON { * * This contains the necessary metadata to process them: * - * - path => where to find the preset within the settings section - * - override => whether a theme preset with the same slug as a default preset - * can override it - * - use_default_names => whether to use the default names - * - value_key => the key that represents the value - * - value_func => optionally, instead of value_key, a function to generate - * the value that takes a preset as an argument - * (either value_key or value_func should be present) - * - css_vars => template string to use in generating the CSS Custom Property. - * Example output: "--wp--preset--duotone--blue: " will generate - * as many CSS Custom Properties as presets defined - * substituting the $slug for the slug's value for each preset value. - * - classes => array containing a structure with the classes to - * generate for the presets, where for each array item - * the key is the class name and the value the property name. - * The "$slug" substring will be replaced by the slug of each preset. - * For example: - * 'classes' => array( - * '.has-$slug-color' => 'color', - * '.has-$slug-background-color' => 'background-color', - * '.has-$slug-border-color' => 'border-color', - * ) - * - properties => array of CSS properties to be used by kses to - * validate the content of each preset - * by means of the remove_insecure_properties method. + * - path => Where to find the preset within the settings section. + * - prevent_override => Whether a theme preset with the same slug as a default preset + * should not override it or the path to a setting for the same + * When defaults. + * The relationship between whether to override the defaults + * and whether the defaults are enabled is inverse: + * - If defaults are enabled => theme presets should not be overriden + * - If defaults are disabled => theme presets should be overriden + * For example, a theme sets defaultPalette to false, + * making the default palette hidden from the user. + * In that case, we want all the theme presets to be present, + * so they should override the defaults by setting this false. + * - value_key => the key that represents the value + * - value_func => optionally, instead of value_key, a function to generate + * the value that takes a preset as an argument + * (either value_key or value_func should be present) + * - css_vars => template string to use in generating the CSS Custom Property. + * Example output: "--wp--preset--duotone--blue: " will generate as many CSS Custom Properties as presets defined + * substituting the $slug for the slug's value for each preset value. + * - classes => array containing a structure with the classes to + * generate for the presets, where for each array item + * the key is the class name and the value the property name. + * The "$slug" substring will be replaced by the slug of each preset. + * For example: + * 'classes' => array( + * '.has-$slug-color' => 'color', + * '.has-$slug-background-color' => 'background-color', + * '.has-$slug-border-color' => 'border-color', + * ) + * - properties => array of CSS properties to be used by kses to + * validate the content of each preset + * by means of the remove_insecure_properties method. * * @since 5.8.0 * @since 5.9.0 Added the `color.duotone` and `typography.fontFamilies` presets, @@ -107,53 +114,58 @@ class WP_Theme_JSON { */ const PRESETS_METADATA = array( array( - 'path' => array( 'color', 'palette' ), - 'override' => array( 'color', 'defaultPalette' ), - 'use_default_names' => false, - 'value_key' => 'color', - 'css_vars' => '--wp--preset--color--$slug', - 'classes' => array( + 'path' => array( 'color', 'palette' ), + 'prevent_override' => array( 'color', 'defaultPalette' ), + 'use_default_presets' => array( 'color', 'defaultPalette' ), + 'use_default_names' => false, + 'value_key' => 'color', + 'css_vars' => '--wp--preset--color--$slug', + 'classes' => array( '.has-$slug-color' => 'color', '.has-$slug-background-color' => 'background-color', '.has-$slug-border-color' => 'border-color', ), - 'properties' => array( 'color', 'background-color', 'border-color' ), + 'properties' => array( 'color', 'background-color', 'border-color' ), ), array( - 'path' => array( 'color', 'gradients' ), - 'override' => array( 'color', 'defaultGradients' ), - 'use_default_names' => false, - 'value_key' => 'gradient', - 'css_vars' => '--wp--preset--gradient--$slug', - 'classes' => array( '.has-$slug-gradient-background' => 'background' ), - 'properties' => array( 'background' ), + 'path' => array( 'color', 'gradients' ), + 'prevent_override' => array( 'color', 'defaultGradients' ), + 'use_default_presets' => array( 'color', 'defaultGradients' ), + 'use_default_names' => false, + 'value_key' => 'gradient', + 'css_vars' => '--wp--preset--gradient--$slug', + 'classes' => array( '.has-$slug-gradient-background' => 'background' ), + 'properties' => array( 'background' ), ), array( - 'path' => array( 'color', 'duotone' ), - 'override' => true, - 'use_default_names' => false, - 'value_func' => 'wp_get_duotone_filter_property', - 'css_vars' => '--wp--preset--duotone--$slug', - 'classes' => array(), - 'properties' => array( 'filter' ), + 'path' => array( 'color', 'duotone' ), + 'prevent_override' => array( 'color', 'defaultDuotone' ), + 'use_default_presets' => array( 'color', 'defaultDuotone' ), + 'use_default_names' => false, + 'value_func' => 'gutenberg_get_duotone_filter_property', + 'css_vars' => '--wp--preset--duotone--$slug', + 'classes' => array(), + 'properties' => array( 'filter' ), ), array( - 'path' => array( 'typography', 'fontSizes' ), - 'override' => true, - 'use_default_names' => true, - 'value_key' => 'size', - 'css_vars' => '--wp--preset--font-size--$slug', - 'classes' => array( '.has-$slug-font-size' => 'font-size' ), - 'properties' => array( 'font-size' ), + 'path' => array( 'typography', 'fontSizes' ), + 'prevent_override' => false, + 'use_default_presets' => true, + 'use_default_names' => true, + 'value_key' => 'size', + 'css_vars' => '--wp--preset--font-size--$slug', + 'classes' => array( '.has-$slug-font-size' => 'font-size' ), + 'properties' => array( 'font-size' ), ), array( - 'path' => array( 'typography', 'fontFamilies' ), - 'override' => true, - 'use_default_names' => false, - 'value_key' => 'fontFamily', - 'css_vars' => '--wp--preset--font-family--$slug', - 'classes' => array( '.has-$slug-font-family' => 'font-family' ), - 'properties' => array( 'font-family' ), + 'path' => array( 'typography', 'fontFamilies' ), + 'prevent_override' => false, + 'use_default_presets' => true, + 'use_default_names' => false, + 'value_key' => 'fontFamily', + 'css_vars' => '--wp--preset--font-family--$slug', + 'classes' => array( '.has-$slug-font-family' => 'font-family' ), + 'properties' => array( 'font-family' ), ), ); @@ -257,6 +269,7 @@ class WP_Theme_JSON { 'custom' => null, 'customDuotone' => null, 'customGradient' => null, + 'defaultDuotone' => null, 'defaultGradients' => null, 'defaultPalette' => null, 'duotone' => null, @@ -1079,9 +1092,14 @@ private static function scope_selector( $scope, $selector ) { private static function get_settings_values_by_slug( $settings, $preset_metadata, $origins ) { $preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() ); + $skip_default_presets = ! self::get_metadata_boolean( $settings, $preset_metadata['use_default_presets'], true ); + $result = array(); foreach ( $origins as $origin ) { - if ( ! isset( $preset_per_origin[ $origin ] ) ) { + if ( + ! isset( $preset_per_origin[ $origin ] ) || + ( 'default' === $origin && $skip_default_presets ) + ) { continue; } foreach ( $preset_per_origin[ $origin ] as $preset ) { @@ -1121,9 +1139,14 @@ private static function get_settings_values_by_slug( $settings, $preset_metadata private static function get_settings_slugs( $settings, $preset_metadata, $origins = self::VALID_ORIGINS ) { $preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() ); + $skip_default_presets = ! self::get_metadata_boolean( $settings, $preset_metadata['use_default_presets'], true ); + $result = array(); foreach ( $origins as $origin ) { - if ( ! isset( $preset_per_origin[ $origin ] ) ) { + if ( + ! isset( $preset_per_origin[ $origin ] ) || + ( 'default' === $origin && $skip_default_presets ) + ) { continue; } foreach ( $preset_per_origin[ $origin ] as $preset ) { @@ -1538,7 +1561,7 @@ public function merge( $incoming ) { // Replace the presets. foreach ( self::PRESETS_METADATA as $preset ) { - $override_preset = self::should_override_preset( $this->theme_json, $node['path'], $preset['override'] ); + $override_preset = ! self::get_metadata_boolean( $this->theme_json['settings'], $preset['prevent_override'], true ); foreach ( self::VALID_ORIGINS as $origin ) { $base_path = array_merge( $node['path'], $preset['path'] ); @@ -1596,7 +1619,10 @@ public function get_svg_filters( $origins ) { $filters = ''; foreach ( $origins as $origin ) { - if ( ! isset( $duotone_presets[ $origin ] ) ) { + if ( + ! isset( $duotone_presets[ $origin ] ) || + ( 'default' === $origin && false === $node['color']['defaultDuotone'] ) + ) { continue; } foreach ( $duotone_presets[ $origin ] as $duotone_preset ) { @@ -1609,46 +1635,41 @@ public function get_svg_filters( $origins ) { } /** - * Returns whether a presets should be overridden or not. + * For metadata values that can either be booleans or paths to booleans, gets the value. * - * @since 5.9.0 + * ```php + * $data = array( + * 'color' => array( + * 'defaultPalette' => true + * ) + * ); + * + * self::get_metadata_boolean( $data, false ); + * // => false * - * @param array $theme_json The theme.json like structure to inspect. - * @param array $path Path to inspect. - * @param bool|array $override Data to compute whether to override the preset. + * self::get_metadata_boolean( $data, array( 'color', 'defaultPalette' ) ); + * // => true + * ``` + * + * @since 5.9.0 + * + * @param array $data The data to inspect. + * @param bool|array $path Boolean or path to a boolean. * @return boolean */ - private static function should_override_preset( $theme_json, $path, $override ) { - if ( is_bool( $override ) ) { - return $override; + private static function get_metadata_boolean( $data, $path, $default = false ) { + if ( is_bool( $path ) ) { + return $path; } - /* - * The relationship between whether to override the defaults - * and whether the defaults are enabled is inverse: - * - * - If defaults are enabled => theme presets should not be overridden - * - If defaults are disabled => theme presets should be overridden - * - * For example, a theme sets defaultPalette to false, - * making the default palette hidden from the user. - * In that case, we want all the theme presets to be present, - * so they should override the defaults. - */ - if ( is_array( $override ) ) { - $value = _wp_array_get( $theme_json, array_merge( $path, $override ) ); - if ( isset( $value ) ) { - return ! $value; - } - - // Search the top-level key if none was found for this node. - $value = _wp_array_get( $theme_json, array_merge( array( 'settings' ), $override ) ); + if ( is_array( $path ) ) { + $value = _wp_array_get( $data, $path ); if ( isset( $value ) ) { - return ! $value; + return $value; } - - return true; } + + return $default; } /** diff --git a/src/wp-includes/theme.json b/src/wp-includes/theme.json index 5c1404ccddb51..5ba62ed33be2e 100644 --- a/src/wp-includes/theme.json +++ b/src/wp-includes/theme.json @@ -13,46 +13,47 @@ "custom": true, "customDuotone": true, "customGradient": true, + "defaultDuotone": true, "defaultGradients": true, "defaultPalette": true, "duotone": [ { - "name": "Dark grayscale" , + "name": "Dark grayscale" , "colors": [ "#000000", "#7f7f7f" ], "slug": "dark-grayscale" }, { - "name": "Grayscale" , + "name": "Grayscale" , "colors": [ "#000000", "#ffffff" ], "slug": "grayscale" }, { - "name": "Purple and yellow" , + "name": "Purple and yellow" , "colors": [ "#8c00b7", "#fcff41" ], "slug": "purple-yellow" }, { - "name": "Blue and red" , + "name": "Blue and red" , "colors": [ "#000097", "#ff4747" ], "slug": "blue-red" }, { - "name": "Midnight" , + "name": "Midnight" , "colors": [ "#000000", "#00a5ff" ], "slug": "midnight" }, { - "name": "Magenta and yellow" , + "name": "Magenta and yellow" , "colors": [ "#c7005a", "#fff278" ], "slug": "magenta-yellow" }, { - "name": "Purple and green" , + "name": "Purple and green" , "colors": [ "#a60072", "#67ff66" ], "slug": "purple-green" }, { - "name": "Blue and orange" , + "name": "Blue and orange" , "colors": [ "#1900d8", "#ffa96b" ], "slug": "blue-orange" }