Skip to content

Commit

Permalink
Squashed commits from WordPress/gutenberg#38681
Browse files Browse the repository at this point in the history
commit 9e87c24d3be87dffd7dd396e93b865c871f413c6
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 16 14:14:39 2022 -0600

    Add example for get_metadata_boolean

commit 0481c25c779551657fbb4d12d62ad82c8d73d66d
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 16 14:04:49 2022 -0600

    Fix conditional hook

commit 18b6f62213631e89662c2f057df0e0931ee2d836
Author: Alex Lende <alex@lende.xyz>
Date:   Thu Feb 10 12:58:44 2022 -0600

    Prevent generating other default presets

commit 7729c2a8f3b864296049dd76eb18b47bf294f3eb
Author: Alex Lende <alex@lende.xyz>
Date:   Thu Feb 10 12:28:41 2022 -0600

    Try refactor should_override_preset

commit 23c5ed34e833957b9d0e56bf82a614d8db9bcb88
Author: Alex Lende <alex@lende.xyz>
Date:   Thu Feb 10 11:24:17 2022 -0600

    Use multi-origin duotone presets

commit e90fe4c6cfd508eed67ba048592e6bf298d55a8a
Author: Alex Lende <alex@lende.xyz>
Date:   Thu Feb 10 10:54:49 2022 -0600

    Try refactoring override path and bool to not be inverse

commit b1b9fe39da4b0fc04026514fe79725c4c617823d
Author: Alex Lende <alex@lende.xyz>
Date:   Thu Feb 10 10:41:19 2022 -0600

    Add defaultDuotone to prevent override

commit 6713f8725320af0c074727326fe9b7aa219bb76d
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 15:55:18 2022 -0600

    Update docs

commit fb0d9a6975d316e79acd8cb198032c1739245dba
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 15:46:34 2022 -0600

    Fix phpcs

commit ab51b12808d728b08ee76923dbdf22b35f572308
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 15:34:06 2022 -0600

    Add defaultDuotone to JSON schema

commit 07076eb55a2e7f43c217755e6284214bbabe6254
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 14:54:04 2022 -0600

    Disable duotone css vars too

commit 50598cf7f96a8ab9c754c4ccfb65d11f78a0c3f7
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 13:44:03 2022 -0600

    Add defaultDuotone to disable default SVGs

commit 5a45d0d447a1ac45d5d472d854b528ac1c0509dc
Author: Alex Lende <alex@lende.xyz>
Date:   Wed Feb 9 13:43:24 2022 -0600

    Fix spacing
  • Loading branch information
Alex Lende committed Feb 16, 2022
1 parent b60d6f5 commit 40418ea
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 103 deletions.
2 changes: 2 additions & 0 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
211 changes: 116 additions & 95 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: <value>" 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: <value>" 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,
Expand All @@ -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' ),
),
);

Expand Down Expand Up @@ -257,6 +269,7 @@ class WP_Theme_JSON {
'custom' => null,
'customDuotone' => null,
'customGradient' => null,
'defaultDuotone' => null,
'defaultGradients' => null,
'defaultPalette' => null,
'duotone' => null,
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/wp-includes/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 40418ea

Please sign in to comment.