diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 44a422dbf41f8c..8963e11095700c 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -646,6 +646,34 @@ private static function append_to_selector( $selector, $to_append ) { return implode( ',', $new_selectors ); } + /** + * Function that given an array of presets keyed by origin + * and the value key of the preset returns an array where each key is + * the a preset slug and each value is the preset value. + * + * @param array $preset_per_origin Array of presets keyed by origin. + * @param string $value_key The property of the preset that contains its value. + * + * @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 ) { + if ( ! isset( $preset_per_origin[ $origin ] ) ) { + continue; + } + foreach ( $preset_per_origin[ $origin ] as $preset ) { + // We don't want to use kebabCase here, + // see https://github.com/WordPress/gutenberg/issues/32347 + // However, we need to make sure the generated class or css variable + // doesn't contain spaces. + $result[ preg_replace( '/\s+/', '-', $preset['slug'] ) ] = $preset[ $value_key ]; + } + } + return $result; + } + /** * Given a settings array, it returns the generated rulesets * for the preset classes. @@ -664,19 +692,16 @@ private static function compute_preset_classes( $settings, $selector ) { $stylesheet = ''; foreach ( self::PRESETS_METADATA as $preset ) { - $values = _wp_array_get( $settings, $preset['path'], array() ); - foreach ( $values as $value ) { - foreach ( $preset['classes'] as $class ) { + $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); + $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); + foreach ( $preset['classes'] as $class ) { + foreach ( $preset_by_slug as $slug => $value ) { $stylesheet .= self::to_ruleset( - // We don't want to use kebabCase here, - // see https://github.com/WordPress/gutenberg/issues/32347 - // However, we need to make sure the generated class - // doesn't contain spaces. - self::append_to_selector( $selector, '.has-' . preg_replace( '/\s+/', '-', $value['slug'] ) . '-' . $class['class_suffix'] ), + self::append_to_selector( $selector, '.has-' . $slug . '-' . $class['class_suffix'] ), array( array( 'name' => $class['property_name'], - 'value' => $value[ $preset['value_key'] ] . ' !important', + 'value' => $value . ' !important', ), ) ); @@ -706,11 +731,12 @@ private static function compute_preset_classes( $settings, $selector ) { private static function compute_preset_vars( $settings ) { $declarations = array(); foreach ( self::PRESETS_METADATA as $preset ) { - $values = _wp_array_get( $settings, $preset['path'], array() ); - foreach ( $values as $value ) { + $preset_per_origin = _wp_array_get( $settings, $preset['path'], array() ); + $preset_by_slug = self::get_merged_preset_by_slug( $preset_per_origin, $preset['value_key'] ); + foreach ( $preset_by_slug as $slug => $value ) { $declarations[] = array( - 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $value['slug'], - 'value' => $value[ $preset['value_key'] ], + 'name' => '--wp--preset--' . $preset['css_var_infix'] . '--' . $slug, + 'value' => $value, ); } } @@ -1106,85 +1132,59 @@ public function get_stylesheet( $type = 'all' ) { /** * Merge new incoming data. * - * @param WP_Theme_JSON_Gutenberg $incoming Data to merge. - * @param string $update_or_remove Whether update or remove existing colors - * for which the incoming data has a duplicated slug. + * @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, $update_or_remove = 'remove' ) { - $incoming_data = $incoming->get_raw_data(); - $existing_data = $this->theme_json; + public function merge( $incoming, $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. - $this->theme_json = array_replace_recursive( $this->theme_json, $incoming_data ); + // 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' ); - // There are a few cases in which we want to merge things differently - // from what array_replace_recursive does. - - // Some incoming properties should replace the existing. - $to_replace = array(); - $to_replace[] = array( 'custom' ); - $to_replace[] = array( 'spacing', 'units' ); - $to_replace[] = array( 'typography', 'fontSizes' ); - $to_replace[] = array( 'typography', 'fontFamilies' ); - - // Some others should be appended to the existing. - // If the slug is the same than an existing element, - // the $update_or_remove param is used to decide - // what to do with the existing element: - // either remove it and append the incoming, - // or update it with the incoming. $to_append = array(); - $to_append[] = array( 'color', 'duotone' ); - $to_append[] = array( 'color', 'gradients' ); $to_append[] = array( 'color', 'palette' ); + $to_append[] = array( 'color', 'gradients' ); + $to_append[] = array( 'typography', 'fontSizes' ); + $to_append[] = array( 'typography', 'fontFamilies' ); $nodes = self::get_setting_nodes( $this->theme_json ); foreach ( $nodes as $metadata ) { - foreach ( $to_replace as $path_to_replace ) { - $path = array_merge( $metadata['path'], $path_to_replace ); + foreach ( $properties 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 $path_to_append ) { - $path = array_merge( $metadata['path'], $path_to_append ); - $incoming_node = _wp_array_get( $incoming_data, $path, array() ); - $existing_node = _wp_array_get( $existing_data, $path, array() ); - - if ( empty( $incoming_node ) && empty( $existing_node ) ) { - continue; - } - $index_table = array(); - $existing_slugs = array(); - $merged = array(); - foreach ( $existing_node as $key => $value ) { - $index_table[ $value['slug'] ] = $key; - $existing_slugs[] = $value['slug']; - $merged[ $key ] = $value; - } - - $to_remove = array(); - foreach ( $incoming_node as $value ) { - if ( ! in_array( $value['slug'], $existing_slugs, true ) ) { - $merged[] = $value; - } elseif ( 'update' === $update_or_remove ) { - $merged[ $index_table[ $value['slug'] ] ] = $value; + 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 { - $merged[] = $value; - $to_remove[] = $index_table[ $value['slug'] ]; + $new_node[ $origin ] = $node; } + gutenberg_experimental_set( $this->theme_json, $path, $new_node ); } - - // Remove the duplicated values and pack the sparsed array. - foreach ( $to_remove as $index ) { - unset( $merged[ $index ] ); - } - $merged = array_values( $merged ); - - gutenberg_experimental_set( $this->theme_json, $path, $merged ); } } diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index bcdedda86f30b7..e37264d25319b2 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -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 ); + $with_theme_supports->merge( self::$theme, 'theme' ); return $with_theme_supports; } @@ -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() ); - $result->merge( self::get_theme_data( $theme_support_data ) ); + $result->merge( self::get_core_data(), 'core' ); + $result->merge( self::get_theme_data( $theme_support_data ), 'theme' ); if ( 'user' === $origin ) { - $result->merge( self::get_user_data(), 'update' ); + $result->merge( self::get_user_data(), 'user' ); } return $result; diff --git a/lib/experimental-default-theme.json b/lib/experimental-default-theme.json index 7297493491c99a..e12e4231018a36 100644 --- a/lib/experimental-default-theme.json +++ b/lib/experimental-default-theme.json @@ -6,148 +6,124 @@ { "name": "Black", "slug": "black", - "color": "#000000", - "origin": "core" + "color": "#000000" }, { "name": "Cyan bluish gray", "slug": "cyan-bluish-gray", - "color": "#abb8c3", - "origin": "core" + "color": "#abb8c3" }, { "name": "White", "slug": "white", - "color": "#ffffff", - "origin": "core" + "color": "#ffffff" }, { "name": "Pale pink", "slug": "pale-pink", - "color": "#f78da7", - "origin": "core" + "color": "#f78da7" }, { "name": "Vivid red", "slug": "vivid-red", - "color": "#cf2e2e", - "origin": "core" + "color": "#cf2e2e" }, { "name": "Luminous vivid orange", "slug": "luminous-vivid-orange", - "color": "#ff6900", - "origin": "core" + "color": "#ff6900" }, { "name": "Luminous vivid amber", "slug": "luminous-vivid-amber", - "color": "#fcb900", - "origin": "core" + "color": "#fcb900" }, { "name": "Light green cyan", "slug": "light-green-cyan", - "color": "#7bdcb5", - "origin": "core" + "color": "#7bdcb5" }, { "name": "Vivid green cyan", "slug": "vivid-green-cyan", - "color": "#00d084", - "origin": "core" + "color": "#00d084" }, { "name": "Pale cyan blue", "slug": "pale-cyan-blue", - "color": "#8ed1fc", - "origin": "core" + "color": "#8ed1fc" }, { "name": "Vivid cyan blue", "slug": "vivid-cyan-blue", - "color": "#0693e3", - "origin": "core" + "color": "#0693e3" }, { "name": "Vivid purple", "slug": "vivid-purple", - "color": "#9b51e0", - "origin": "core" + "color": "#9b51e0" } ], "gradients": [ { "name": "Vivid cyan blue to vivid purple", "gradient": "linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)", - "slug": "vivid-cyan-blue-to-vivid-purple", - "origin": "core" + "slug": "vivid-cyan-blue-to-vivid-purple" }, { "name": "Light green cyan to vivid green cyan", "gradient": "linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)", - "slug": "light-green-cyan-to-vivid-green-cyan", - "origin": "core" + "slug": "light-green-cyan-to-vivid-green-cyan" }, { "name": "Luminous vivid amber to luminous vivid orange", "gradient": "linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%)", - "slug": "luminous-vivid-amber-to-luminous-vivid-orange", - "origin": "core" + "slug": "luminous-vivid-amber-to-luminous-vivid-orange" }, { "name": "Luminous vivid orange to vivid red", "gradient": "linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%)", - "slug": "luminous-vivid-orange-to-vivid-red", - "origin": "core" + "slug": "luminous-vivid-orange-to-vivid-red" }, { "name": "Very light gray to cyan bluish gray", "gradient": "linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%)", - "slug": "very-light-gray-to-cyan-bluish-gray", - "origin": "core" + "slug": "very-light-gray-to-cyan-bluish-gray" }, { "name": "Cool to warm spectrum", "gradient": "linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%)", - "slug": "cool-to-warm-spectrum", - "origin": "core" + "slug": "cool-to-warm-spectrum" }, { "name": "Blush light purple", "gradient": "linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%)", - "slug": "blush-light-purple", - "origin": "core" + "slug": "blush-light-purple" }, { "name": "Blush bordeaux", "gradient": "linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)", - "slug": "blush-bordeaux", - "origin": "core" + "slug": "blush-bordeaux" }, { "name": "Luminous dusk", "gradient": "linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)", - "slug": "luminous-dusk", - "origin": "core" + "slug": "luminous-dusk" }, { "name": "Pale ocean", "gradient": "linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%)", - "slug": "pale-ocean", - "origin": "core" + "slug": "pale-ocean" }, { "name": "Electric grass", "gradient": "linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%)", - "slug": "electric-grass", - "origin": "core" + "slug": "electric-grass" }, { "name": "Midnight", "gradient": "linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%)", - "slug": "midnight", - "origin": "core" + "slug": "midnight" } ], "duotone": [ diff --git a/lib/global-styles.php b/lib/global-styles.php index 08f6802a42659d..5b3fe46f6533fc 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -148,14 +148,37 @@ function_exists( 'gutenberg_is_edit_site_page' ) && // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php. $settings['__experimentalFeatures'] = $consolidated->get_settings(); + if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) { - $settings['colors'] = $settings['__experimentalFeatures']['color']['palette']; - unset( $settings['__experimentalFeatures']['color']['palette'] ); + $colors_by_origin = $settings['__experimentalFeatures']['color']['palette']; + $settings['colors'] = isset( $colors_by_origin['user'] ) ? + $colors_by_origin['user'] : ( + isset( $colors_by_origin['theme'] ) ? + $colors_by_origin['theme'] : + $colors_by_origin['core'] + ); } + if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) { - $settings['gradients'] = $settings['__experimentalFeatures']['color']['gradients']; - unset( $settings['__experimentalFeatures']['color']['gradients'] ); + $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients']; + $settings['gradients'] = isset( $gradients_by_origin['user'] ) ? + $gradients_by_origin['user'] : ( + isset( $gradients_by_origin['theme'] ) ? + $gradients_by_origin['theme'] : + $gradients_by_origin['core'] + ); + } + + if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { + $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes']; + $settings['fontSizes'] = isset( $font_sizes_by_origin['user'] ) ? + $font_sizes_by_origin['user'] : ( + isset( $font_sizes_by_origin['theme'] ) ? + $font_sizes_by_origin['theme'] : + $font_sizes_by_origin['core'] + ); } + if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) { $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom']; unset( $settings['__experimentalFeatures']['color']['custom'] ); @@ -164,10 +187,6 @@ function_exists( 'gutenberg_is_edit_site_page' ) && $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient']; unset( $settings['__experimentalFeatures']['color']['customGradient'] ); } - if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) { - $settings['fontSizes'] = $settings['__experimentalFeatures']['typography']['fontSizes']; - unset( $settings['__experimentalFeatures']['typography']['fontSizes'] ); - } if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) { $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize']; unset( $settings['__experimentalFeatures']['typography']['customFontSize'] ); diff --git a/packages/block-editor/src/components/use-setting/index.js b/packages/block-editor/src/components/use-setting/index.js index 43ee185c396617..d302ceb277425f 100644 --- a/packages/block-editor/src/components/use-setting/index.js +++ b/packages/block-editor/src/components/use-setting/index.js @@ -49,18 +49,11 @@ const deprecatedFlags = { 'spacing.customPadding': ( settings ) => settings.enableCustomSpacing, }; -const filterColorsFromCoreOrigin = ( path, setting ) => { - if ( path !== 'color.palette' && path !== 'color.gradients' ) { - return setting; - } - - if ( ! Array.isArray( setting ) ) { - return setting; - } - - const colors = setting.filter( ( color ) => color?.origin !== 'core' ); - - return colors.length > 0 ? colors : setting; +const PATHS_WITH_MERGE = { + 'color.gradients': true, + 'color.palette': true, + 'typography.fontFamilies': true, + 'typography.fontSizes': true, }; /** @@ -90,10 +83,14 @@ export default function useSetting( path ) { const experimentalFeaturesResult = get( settings, blockPath ) ?? get( settings, defaultsPath ); if ( experimentalFeaturesResult !== undefined ) { - return filterColorsFromCoreOrigin( - path, - experimentalFeaturesResult - ); + if ( PATHS_WITH_MERGE[ path ] ) { + return ( + experimentalFeaturesResult.user ?? + experimentalFeaturesResult.theme ?? + experimentalFeaturesResult.core + ); + } + return experimentalFeaturesResult; } // 2 - Use deprecated settings, otherwise. @@ -101,10 +98,7 @@ export default function useSetting( path ) { ? deprecatedFlags[ path ]( settings ) : undefined; if ( deprecatedSettingsValue !== undefined ) { - return filterColorsFromCoreOrigin( - path, - deprecatedSettingsValue - ); + return deprecatedSettingsValue; } // 3 - Fall back for typography.dropCap: diff --git a/packages/edit-site/src/components/editor/global-styles-provider.js b/packages/edit-site/src/components/editor/global-styles-provider.js index 6d173773a18499..05e672dfa05f17 100644 --- a/packages/edit-site/src/components/editor/global-styles-provider.js +++ b/packages/edit-site/src/components/editor/global-styles-provider.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { set, get, mergeWith } from 'lodash'; +import { set, get, mergeWith, mapValues, setWith, clone } from 'lodash'; /** * WordPress dependencies @@ -30,6 +30,7 @@ import { ROOT_BLOCK_SUPPORTS, getValueFromVariable, getPresetVariable, + PRESET_METADATA, } from './utils'; import { toCustomProperties, toStyles } from './global-styles-renderer'; import { store as editSiteStore } from '../../store'; @@ -47,31 +48,7 @@ const GlobalStylesContext = createContext( { /* eslint-enable no-unused-vars */ } ); -const mergeTreesCustomizer = ( objValue, srcValue, key ) => { - // Users can add their own colors. - // We want to append them when they don't - // have the same slug as an existing color, - // otherwise we want to update the existing color instead. - if ( 'palette' === key ) { - const indexTable = {}; - const existingSlugs = []; - const result = [ ...( objValue ?? [] ) ]; - result.forEach( ( { slug }, index ) => { - indexTable[ slug ] = index; - existingSlugs.push( slug ); - } ); - - ( srcValue ?? [] ).forEach( ( element ) => { - if ( existingSlugs.includes( element?.slug ) ) { - result[ indexTable[ element?.slug ] ] = element; - } else { - result.push( element ); - } - } ); - - return result; - } - +const mergeTreesCustomizer = ( objValue, srcValue ) => { // We only pass as arrays the presets, // in which case we want the new array of values // to override the old array (no merging). @@ -135,6 +112,10 @@ const getBlockMetadata = ( blockTypes ) => { return result; }; +function immutableSet( object, path, value ) { + return setWith( object ? clone( object ) : {}, path, value, clone ); +} + export default function GlobalStylesProvider( { children, baseStyles } ) { const [ content, setContent ] = useGlobalStylesEntityContent(); const { blockTypes, settings } = useSelect( ( select ) => { @@ -174,12 +155,41 @@ export default function GlobalStylesProvider( { children, baseStyles } ) { newUserStyles = EMPTY_CONTENT; } + const addUserToSettings = ( settingsToAdd ) => { + PRESET_METADATA.forEach( ( { path } ) => { + const presetData = get( settingsToAdd, path ); + if ( presetData ) { + settingsToAdd = immutableSet( settingsToAdd, path, { + user: presetData, + } ); + } + } ); + return settingsToAdd; + }; + + let userStylesWithOrigin = newUserStyles; + if ( userStylesWithOrigin.settings ) { + userStylesWithOrigin = { + ...userStylesWithOrigin, + settings: addUserToSettings( userStylesWithOrigin.settings ), + }; + if ( userStylesWithOrigin.settings.blocks ) { + userStylesWithOrigin.settings = { + ...userStylesWithOrigin.settings, + blocks: mapValues( + userStylesWithOrigin.settings.blocks, + addUserToSettings + ), + }; + } + } + // At this point, the version schema of the theme & user // is the same, so we can merge them. const newMergedStyles = mergeWith( {}, baseStyles, - newUserStyles, + userStylesWithOrigin, mergeTreesCustomizer ); diff --git a/packages/edit-site/src/components/editor/global-styles-renderer.js b/packages/edit-site/src/components/editor/global-styles-renderer.js index 45241512c0e7dc..5d3f9da5cc079b 100644 --- a/packages/edit-site/src/components/editor/global-styles-renderer.js +++ b/packages/edit-site/src/components/editor/global-styles-renderer.js @@ -52,12 +52,17 @@ function getPresetsDeclarations( blockPresets = {} ) { return reduce( PRESET_METADATA, ( declarations, { path, valueKey, cssVarInfix } ) => { - const preset = get( blockPresets, path, [] ); - preset.forEach( ( value ) => { - declarations.push( - `--wp--preset--${ cssVarInfix }--${ value.slug }: ${ value[ valueKey ] }` - ); + const presetByOrigin = get( blockPresets, path, [] ); + [ 'core', 'theme', 'user' ].forEach( ( origin ) => { + if ( presetByOrigin[ origin ] ) { + presetByOrigin[ origin ].forEach( ( value ) => { + declarations.push( + `--wp--preset--${ cssVarInfix }--${ value.slug }: ${ value[ valueKey ] }` + ); + } ); + } } ); + return declarations; }, [] @@ -78,22 +83,26 @@ function getPresetsClasses( blockSelector, blockPresets = {} ) { if ( ! classes ) { return declarations; } - const presets = get( blockPresets, path, [] ); - presets.forEach( ( preset ) => { - classes.forEach( ( { classSuffix, propertyName } ) => { - const slug = preset.slug; - const value = preset[ valueKey ]; - // We don't want to use kebabCase from lodash here - // see https://github.com/WordPress/gutenberg/issues/32347 - // However, we need to make sure the generated class - // doesn't contain spaces. - const classSelectorToUse = `.has-${ slug.replace( - /\s+/g, - '-' - ) }-${ classSuffix }`; - const selectorToUse = `${ blockSelector }${ classSelectorToUse }`; - declarations += `${ selectorToUse }{${ propertyName }: ${ value } !important;}`; - } ); + const presetByOrigin = get( blockPresets, path, [] ); + [ 'core', 'theme', 'user' ].forEach( ( origin ) => { + if ( presetByOrigin[ origin ] ) { + presetByOrigin[ origin ].forEach( ( preset ) => { + classes.forEach( ( { classSuffix, propertyName } ) => { + const slug = preset.slug; + const value = preset[ valueKey ]; + // We don't want to use kebabCase from lodash here + // see https://github.com/WordPress/gutenberg/issues/32347 + // However, we need to make sure the generated class + // doesn't contain spaces. + const classSelectorToUse = `.has-${ slug.replace( + /\s+/g, + '-' + ) }-${ classSuffix }`; + const selectorToUse = `${ blockSelector }${ classSelectorToUse }`; + declarations += `${ selectorToUse }{${ propertyName }: ${ value } !important;}`; + } ); + } ); + } } ); return declarations; }, diff --git a/packages/edit-site/src/components/editor/test/global-styles-renderer.js b/packages/edit-site/src/components/editor/test/global-styles-renderer.js index 9d0f510c4be592..37798630dcd8e1 100644 --- a/packages/edit-site/src/components/editor/test/global-styles-renderer.js +++ b/packages/edit-site/src/components/editor/test/global-styles-renderer.js @@ -214,10 +214,20 @@ describe( 'global styles renderer', () => { const tree = { settings: { color: { - palette: [ - { name: 'White', slug: 'white', color: 'white' }, - { name: 'Black', slug: 'black', color: 'black' }, - ], + palette: { + user: [ + { + name: 'White', + slug: 'white', + color: 'white', + }, + { + name: 'Black', + slug: 'black', + color: 'black', + }, + ], + }, }, custom: { 'font-primary': 'value', @@ -229,18 +239,20 @@ describe( 'global styles renderer', () => { blocks: { 'core/heading': { typography: { - fontSizes: [ - { - name: 'small', - slug: 'small', - size: '12px', - }, - { - name: 'medium', - slug: 'medium', - size: '23px', - }, - ], + fontSizes: { + theme: [ + { + name: 'small', + slug: 'small', + size: '12px', + }, + { + name: 'medium', + slug: 'medium', + size: '23px', + }, + ], + }, }, }, }, @@ -264,10 +276,20 @@ describe( 'global styles renderer', () => { const tree = { settings: { color: { - palette: [ - { name: 'White', slug: 'white', color: 'white' }, - { name: 'Black', slug: 'black', color: 'black' }, - ], + palette: { + core: [ + { + name: 'White', + slug: 'white', + color: 'white', + }, + { + name: 'Black', + slug: 'black', + color: 'black', + }, + ], + }, }, }, styles: { diff --git a/packages/edit-site/src/components/editor/utils.js b/packages/edit-site/src/components/editor/utils.js index 7d5d55884feeeb..05218ea42ad108 100644 --- a/packages/edit-site/src/components/editor/utils.js +++ b/packages/edit-site/src/components/editor/utils.js @@ -90,18 +90,11 @@ function getPresetMetadataFromStyleProperty( styleProperty ) { return getPresetMetadataFromStyleProperty.MAP[ styleProperty ]; } -const filterColorsFromCoreOrigin = ( path, setting ) => { - if ( path !== 'color.palette' && path !== 'color.gradients' ) { - return setting; - } - - if ( ! Array.isArray( setting ) ) { - return setting; - } - - const colors = setting.filter( ( color ) => color?.origin !== 'core' ); - - return colors.length > 0 ? colors : setting; +const PATHS_WITH_MERGE = { + 'color.gradients': true, + 'color.palette': true, + 'typography.fontFamilies': true, + 'typography.fontSizes': true, }; export function useSetting( path, blockName = '' ) { @@ -110,8 +103,11 @@ export function useSetting( path, blockName = '' ) { } ); const topLevelPath = `__experimentalFeatures.${ path }`; const blockPath = `__experimentalFeatures.blocks.${ blockName }.${ path }`; - const setting = get( settings, blockPath ) ?? get( settings, topLevelPath ); - return filterColorsFromCoreOrigin( path, setting ); + const result = get( settings, blockPath ) ?? get( settings, topLevelPath ); + if ( PATHS_WITH_MERGE[ path ] ) { + return result.user ?? result.theme ?? result.core; + } + return result; } export function getPresetVariable( styles, context, propertyName, value ) { diff --git a/packages/edit-site/src/components/sidebar/color-palette-panel.js b/packages/edit-site/src/components/sidebar/color-palette-panel.js index 49e7c964fc29c8..fbae0697b9c811 100644 --- a/packages/edit-site/src/components/sidebar/color-palette-panel.js +++ b/packages/edit-site/src/components/sidebar/color-palette-panel.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { difference, get } from 'lodash'; +import { get } from 'lodash'; /** * WordPress dependencies @@ -38,14 +38,23 @@ export default function ColorPalettePanel( { ( select ) => { const baseStyles = select( editSiteStore ).getSettings() .__experimentalGlobalStylesBaseStyles; + const contextualBasePalette = get( baseStyles, [ + 'settings', + 'blocks', + contextName, + 'color', + 'palette', + ] ); + const globalPalette = get( baseStyles, [ + 'settings', + 'color', + 'palette', + ] ); const basePalette = - get( baseStyles, [ - 'settings', - 'blocks', - contextName, - 'color', - 'palette', - ] ) ?? get( baseStyles, [ 'settings', 'color', 'palette' ] ); + contextualBasePalette?.theme ?? + contextualBasePalette?.core ?? + globalPalette?.theme ?? + globalPalette?.core; if ( ! basePalette ) { return EMPTY_ARRAY; } @@ -58,21 +67,7 @@ export default function ColorPalettePanel( { immutableColorSlugs={ immutableColorSlugs } colors={ colors } onChange={ ( newColors ) => { - const existingUserColors = ( newColors ?? [] ).filter( - ( color ) => color.origin === 'user' - ); - const differentUserColors = difference( newColors, colors ); - if ( differentUserColors.length === 1 ) { - differentUserColors[ 0 ] = { - ...differentUserColors[ 0 ], - origin: 'user', - }; - } - - setSetting( contextName, 'color.palette', [ - ...existingUserColors, - ...differentUserColors, - ] ); + setSetting( contextName, 'color.palette', newColors ); } } emptyUI={ __( 'Colors are empty! Add some colors to create your own color palette.' diff --git a/phpunit/class-wp-theme-json-schema-v0-test.php b/phpunit/class-wp-theme-json-schema-v0-test.php index aad503dc7c1616..d2fd23e04edf49 100644 --- a/phpunit/class-wp-theme-json-schema-v0-test.php +++ b/phpunit/class-wp-theme-json-schema-v0-test.php @@ -335,129 +335,132 @@ function test_get_settings() { function test_get_stylesheet() { $root_name = WP_Theme_JSON_Schema_V0::ROOT_BLOCK_NAME; $all_blocks_name = WP_Theme_JSON_Schema_V0::ALL_BLOCKS_NAME; - - $theme_json = new WP_Theme_JSON_Gutenberg( - array( - 'settings' => array( - $all_blocks_name => array( - 'color' => array( - 'text' => 'value', - 'palette' => array( - array( - 'slug' => 'white', - 'color' => 'white', - ), - array( - 'slug' => 'black', - 'color' => 'black', + $theme_json = new WP_Theme_JSON_Gutenberg( array() ); + $theme_json->merge( + new WP_Theme_JSON_Gutenberg( + array( + 'settings' => array( + $all_blocks_name => array( + 'color' => array( + 'text' => 'value', + 'palette' => array( + array( + 'slug' => 'white', + 'color' => 'white', + ), + array( + 'slug' => 'black', + 'color' => 'black', + ), ), ), - ), - 'typography' => array( - 'fontFamilies' => array( - array( - 'slug' => 'small', - 'fontFamily' => '14px', + 'typography' => array( + 'fontFamilies' => array( + array( + 'slug' => 'small', + 'fontFamily' => '14px', + ), + array( + 'slug' => 'big', + 'fontFamily' => '41px', + ), ), - array( - 'slug' => 'big', - 'fontFamily' => '41px', + ), + 'misc' => 'value', + ), + $root_name => array( + 'color' => array( + 'palette' => array( + array( + 'slug' => 'grey', + 'color' => 'grey', + ), ), ), ), - 'misc' => 'value', - ), - $root_name => array( - 'color' => array( - 'palette' => array( - array( - 'slug' => 'grey', - 'color' => 'grey', + 'core/group' => array( + 'custom' => array( + 'base-font' => 16, + 'line-height' => array( + 'small' => 1.2, + 'medium' => 1.4, + 'large' => 1.8, ), ), ), ), - 'core/group' => array( - 'custom' => array( - 'base-font' => 16, - 'line-height' => array( - 'small' => 1.2, - 'medium' => 1.4, - 'large' => 1.8, + 'styles' => array( + $root_name => array( + 'color' => array( + 'link' => '#111', + 'text' => 'var:preset|color|grey', ), + 'misc' => 'value', ), - ), - ), - 'styles' => array( - $root_name => array( - 'color' => array( - 'link' => '#111', - 'text' => 'var:preset|color|grey', - ), - 'misc' => 'value', - ), - 'core/group' => array( - 'color' => array( - 'link' => '#333', - ), - 'spacing' => array( - 'padding' => array( - 'top' => '12px', - 'bottom' => '24px', + 'core/group' => array( + 'color' => array( + 'link' => '#333', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '12px', + 'bottom' => '24px', + ), ), ), - ), - 'core/heading/h1' => array( - 'color' => array( - 'link' => '#111', - ), - 'typography' => array( - 'fontSize' => '1em', - ), - ), - 'core/heading/h2' => array( - 'color' => array( - 'link' => '#222', - ), - 'typography' => array( - 'fontSize' => '2em', - ), - ), - 'core/post-title/h2' => array( - 'color' => array( - 'link' => '#222', - ), - 'typography' => array( - 'fontSize' => '2em', - ), - ), - 'core/post-title/h5' => array( - 'color' => array( - 'link' => '#555', + 'core/heading/h1' => array( + 'color' => array( + 'link' => '#111', + ), + 'typography' => array( + 'fontSize' => '1em', + ), ), - 'typography' => array( - 'fontSize' => '5em', + 'core/heading/h2' => array( + 'color' => array( + 'link' => '#222', + ), + 'typography' => array( + 'fontSize' => '2em', + ), ), - ), - 'core/query-title/h4' => array( - 'color' => array( - 'link' => '#444', + 'core/post-title/h2' => array( + 'color' => array( + 'link' => '#222', + ), + 'typography' => array( + 'fontSize' => '2em', + ), ), - 'typography' => array( - 'fontSize' => '4em', + 'core/post-title/h5' => array( + 'color' => array( + 'link' => '#555', + ), + 'typography' => array( + 'fontSize' => '5em', + ), ), - ), - 'core/query-title/h5' => array( - 'color' => array( - 'link' => '#555', + 'core/query-title/h4' => array( + 'color' => array( + 'link' => '#444', + ), + 'typography' => array( + 'fontSize' => '4em', + ), ), - 'typography' => array( - 'fontSize' => '5em', + 'core/query-title/h5' => array( + 'color' => array( + 'link' => '#555', + ), + 'typography' => array( + 'fontSize' => '5em', + ), ), ), - ), - 'misc' => 'value', - ) + 'misc' => 'value', + ) + ), + 'core' ); $this->assertEquals( diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index fa3a4931a561b2..c38aa1d9ae768c 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -57,107 +57,111 @@ function test_get_settings() { } 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', + $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', + ), ), ), - ), - 'typography' => array( - 'fontFamilies' => array( - array( - 'slug' => 'small', - 'fontFamily' => '14px', - ), - array( - 'slug' => 'big', - 'fontFamily' => '41px', + '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, + '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', - ), + 'styles' => array( + 'color' => array( + 'text' => 'var:preset|color|grey', ), - ), - 'blocks' => array( - 'core/group' => array( - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => '#111', - ), - ), - ), - 'spacing' => array( - 'padding' => array( - 'top' => '12px', - 'bottom' => '24px', + 'misc' => 'value', + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', + 'background' => '#333', ), ), ), - 'core/heading' => array( - 'color' => array( - 'text' => '#123456', - ), - 'elements' => array( - 'link' => array( - 'color' => array( - 'text' => '#111', - 'background' => '#333', + 'blocks' => array( + 'core/group' => array( + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', + ), ), - 'typography' => array( - 'fontSize' => '60px', + ), + 'spacing' => array( + 'padding' => array( + 'top' => '12px', + 'bottom' => '24px', ), ), ), - ), - 'core/post-date' => array( - 'color' => array( - 'text' => '#123456', + 'core/heading' => array( + 'color' => array( + 'text' => '#123456', + ), + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#111', + 'background' => '#333', + ), + 'typography' => array( + 'fontSize' => '60px', + ), + ), + ), ), - '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', - ) + 'misc' => 'value', + ) + ), + 'core' ); $this->assertEquals( @@ -175,24 +179,28 @@ function test_get_stylesheet() { } function test_get_stylesheet_preset_classes_work_with_compounded_selectors() { - $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_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' ); $this->assertEquals( @@ -202,33 +210,37 @@ 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( - '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() ); + $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', + ), ), ), ), ), ), - ), - '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( @@ -242,34 +254,38 @@ 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( - '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() ); + $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', + ), ), ), ), - ), - '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' ); $this->assertEquals( @@ -279,35 +295,42 @@ public function test_get_stylesheet_preset_values_are_marked_as_important() { } public function test_merge_incoming_data() { - $initial = array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'color' => array( - 'custom' => false, - 'palette' => array( - array( - 'slug' => 'red', - 'color' => 'red', + $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', + ), + ), ), - array( - 'slug' => 'green', - 'color' => 'green', + 'blocks' => array( + 'core/paragraph' => array( + 'color' => array( + 'custom' => false, + ), + ), ), ), - ), - 'blocks' => array( - 'core/paragraph' => array( - 'color' => array( - 'custom' => false, + 'styles' => array( + 'typography' => array( + 'fontSize' => '12', ), ), - ), - ), - 'styles' => array( - 'typography' => array( - 'fontSize' => '12', - ), + + ) ), + 'core' ); $add_new_block = array( @@ -437,37 +460,37 @@ public function test_merge_incoming_data() { 'custom' => true, 'customGradient' => true, 'palette' => array( - array( - 'slug' => 'red', - 'color' => 'red', - ), - array( - 'slug' => 'green', - 'color' => 'green', - ), - array( - 'slug' => 'blue', - 'color' => 'blue', + 'core' => array( + array( + 'slug' => 'blue', + 'color' => 'blue', + ), ), ), 'gradients' => array( - array( - 'slug' => 'gradient', - 'gradient' => 'gradient', + 'core' => array( + array( + 'slug' => 'gradient', + 'gradient' => 'gradient', + ), ), ), ), 'typography' => array( 'fontSizes' => array( - array( - 'slug' => 'fontSize', - 'size' => 'fontSize', + 'core' => array( + array( + 'slug' => 'fontSize', + 'size' => 'fontSize', + ), ), ), 'fontFamilies' => array( - array( - 'slug' => 'fontFamily', - 'fontFamily' => 'fontFamily', + 'core' => array( + array( + 'slug' => 'fontFamily', + 'fontFamily' => 'fontFamily', + ), ), ), ), @@ -509,14 +532,13 @@ public function test_merge_incoming_data() { ), ); - $theme_json = new WP_Theme_JSON_Gutenberg( $initial ); - $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 ) ); + $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' ); $actual = $theme_json->get_raw_data(); $this->assertEqualSetsWithIndex( $expected, $actual );