diff --git a/src/wp-includes/class-wp-duotone.php b/src/wp-includes/class-wp-duotone.php index 49cf221c11af6..865290c8bf9e8 100644 --- a/src/wp-includes/class-wp-duotone.php +++ b/src/wp-includes/class-wp-duotone.php @@ -126,6 +126,13 @@ class WP_Duotone { */ private static $used_svg_filter_data = array(); + /** + * Cache the resolved color palette to avoid repeated lookups. + * + * @var array|null + */ + private static $color_palette_cache = null; + /** * All of the block CSS declarations for styles on the page. * @@ -518,6 +525,17 @@ private static function colord_parse_hsla_string( $input ) { * @return array|null An array of RGBA values or null if the string is invalid. */ private static function colord_parse( $input ) { + + // Handle empty/falsy as transparent otherwise it leads to an inconsistent behaviour. + if ( empty( $input ) ) { + return array( + 'r' => 0, + 'g' => 0, + 'b' => 0, + 'a' => 0, + ); + } + $result = self::colord_parse_hex( $input ); if ( ! $result ) { @@ -531,6 +549,208 @@ private static function colord_parse( $input ) { return $result; } + /** + * Get the flattened color palette from global settings. + * + * This retrieves all color presets from default, theme, and custom origins + * and flattens them into a single array indexed by slug. + * + * @since 7.3.0 + * + * @return array Associative array of color slugs to color values. + */ + private static function get_color_palette() { + if ( null !== self::$color_palette_cache ) { + return self::$color_palette_cache; + } + + self::$color_palette_cache = array(); + + $settings = wp_get_global_settings(); + $palette = $settings['color']['palette'] ?? array(); + + if ( isset( $palette['default'] ) || isset( $palette['theme'] ) || isset( $palette['custom'] ) ) { + // If palette is organized by origin. + foreach ( array( 'default', 'theme', 'custom' ) as $origin ) { + if ( isset( $palette[ $origin ] ) && is_array( $palette[ $origin ] ) ) { + foreach ( $palette[ $origin ] as $color_preset ) { + if ( isset( $color_preset['slug'], $color_preset['color'] ) ) { + self::$color_palette_cache[ $color_preset['slug'] ] = $color_preset['color']; + } + } + } + } + } else { + foreach ( $palette as $color_preset ) { + if ( isset( $color_preset['slug'], $color_preset['color'] ) ) { + self::$color_palette_cache[ $color_preset['slug'] ] = $color_preset['color']; + } + } + } + + return self::$color_palette_cache; + } + + /** + * Clear the color palette cache. + * + * This should be called when global settings might have changed, + * such as when switching style variations. + * + * @since 7.3.0 + */ + public static function clear_color_palette_cache() { + self::$color_palette_cache = null; + } + + /** + * Check if a color value is a CSS custom property. + * + * Supports multiple formats: + * - var(--wp--preset--color--slug) + * - var:preset|color|slug + * + * @since 7.3.0 + * + * @param string $color The color value to check. + * @return bool True if the color is a CSS custom property. + */ + private static function is_css_custom_property( $color ) { + if ( ! is_string( $color ) ) { + return false; + } + + // Check for var(--wp--preset--color--slug) format. + if ( preg_match( '/^var\(--wp--preset--color--[a-zA-Z0-9_-]+\)$/', $color ) ) { + return true; + } + + // Check for var:preset|color|slug format. + if ( preg_match( '/^var:preset\|color\|[a-zA-Z0-9_-]+$/', $color ) ) { + return true; + } + + // Check for var(--wp--custom--*) format (custom CSS properties). + if ( preg_match( '/^var\(--wp--custom--[a-zA-Z0-9_-]+\)$/', $color ) ) { + return true; + } + + return false; + } + + /** + * Extract the color slug from a CSS custom property. + * + * @since 7.3.0 + * + * @param string $color The CSS custom property string. + * @return string|null The color slug, or null if not found. + */ + private static function get_slug_from_css_custom_property( $color ) { + // Try var(--wp--preset--color--slug) format. + if ( preg_match( '/^var\(--wp--preset--color--([a-zA-Z0-9_-]+)\)$/', $color, $matches ) ) { + return $matches[1]; + } + + // Try var:preset|color|slug format. + if ( preg_match( '/^var:preset\|color\|([a-zA-Z0-9_-]+)$/', $color, $matches ) ) { + return $matches[1]; + } + + return null; + } + + /** + * Resolve a CSS custom property to its actual color value. + * + * @since 7.3.0 + * + * @param string $color The color value, possibly a CSS custom property. + * @return string|null The resolved color value, or null if it couldn't be resolved. + */ + private static function resolve_css_custom_property( $color ) { + // If it's not a CSS custom property, return as-is. + if ( ! self::is_css_custom_property( $color ) ) { + return $color; + } + + $slug = self::get_slug_from_css_custom_property( $color ); + + if ( null === $slug ) { + return null; + } + + $palette = self::get_color_palette(); + + if ( isset( $palette[ $slug ] ) ) { + // Recursively resolve in case the palette color is also a variable. + return self::resolve_css_custom_property( $palette[ $slug ] ); + } + + return null; + } + + /** + * Resolve all CSS custom properties in an array of colors. + * + * @since 7.3.0 + * + * @param array $colors Array of color values. + * @return array Array of resolved color values. + */ + private static function resolve_duotone_colors( $colors ) { + if ( ! is_array( $colors ) ) { + return $colors; + } + + $resolved_colors = array(); + + foreach ( $colors as $color ) { + $resolved = self::resolve_css_custom_property( $color ); + + if ( null !== $resolved ) { + $resolved_colors[] = $resolved; + } else { + // Log a warning if we cannot resolve a CSS variable. + if ( self::is_css_custom_property( $color ) ) { + $error_message = sprintf( + /* translators: %s: CSS custom property */ + __( 'Could not resolve CSS custom property "%s" in duotone colors. Make sure the color preset exists in your theme.json.' ), + $color + ); + _doing_it_wrong( __METHOD__, $error_message, '7.3.0' ); + } + + // Include the original value if it's not a CSS variable. + $resolved_colors[] = $color; + } + } + + return $resolved_colors; + } + + /** + * Check if any colors in the array are CSS custom properties. + * + * @since 7.3.0 + * + * @param array $colors Array of color values. + * @return bool True if any color is a CSS custom property. + */ + private static function has_css_custom_properties( $colors ) { + if ( ! is_array( $colors ) ) { + return false; + } + + foreach ( $colors as $color ) { + if ( self::is_css_custom_property( $color ) ) { + return true; + } + } + + return false; + } + /** * Takes the inline CSS duotone variable from a block and return the slug. * @@ -662,6 +882,9 @@ private static function get_filter_url( $filter_id ) { * @return string An SVG with a duotone filter definition. */ private static function get_filter_svg( $filter_id, $colors ) { + // Resolve any CSS custom properties in the colors array. + $resolved_colors = self::resolve_duotone_colors( $colors ); + $duotone_values = array( 'r' => array(), 'g' => array(), @@ -669,7 +892,7 @@ private static function get_filter_svg( $filter_id, $colors ) { 'a' => array(), ); - foreach ( $colors as $color_str ) { + foreach ( $resolved_colors as $color_str ) { $color = self::colord_parse( $color_str ); if ( null === $color ) { @@ -1013,6 +1236,23 @@ private static function get_all_global_styles_presets() { $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); self::$global_styles_presets[ $filter_id ] = $preset; + + // Validate that CSS custom properties can be resolved. + if ( isset( $preset['colors'] ) && is_array( $preset['colors'] ) ) { + if ( self::has_css_custom_properties( $preset['colors'] ) ) { + $resolved = self::resolve_duotone_colors( $preset['colors'] ); + + // Check if all colors can be resolved. + if ( count( $resolved ) !== count( $preset['colors'] ) ) { + $error_message = sprintf( + /* translators: %s: duotone preset slug */ + __( 'Duotone preset "%s" contains CSS custom properties that could not be resolved. Make sure the referenced color presets exist in your theme.json.' ), + $preset['slug'] + ); + _doing_it_wrong( __METHOD__, $error_message, '7.3.0' ); + } + } + } } } @@ -1261,6 +1501,38 @@ public static function output_footer_assets() { } } + /** + * Resolves CSS custom properties in duotone presets for editor use. + * + * This should be called before sending duotone presets to the editor + * so that SVG filters can be generated with actual color values. + * + * @since 7.3.0 + * + * @param array $presets The duotone presets with possible CSS variables. + * @return array The presets with CSS variables resolved to actual colors. + */ + private static function resolve_presets_for_editor( $presets ) { + $resolved_presets = array(); + + foreach ( $presets as $filter_id => $preset ) { + $resolved_preset = $preset; + + if ( isset( $preset['colors'] ) && is_array( $preset['colors'] ) ) { + $resolved_colors = self::resolve_duotone_colors( $preset['colors'] ); + + // Only use resolved colors if we get valid results. + if ( ! empty( $resolved_colors ) && count( $resolved_colors ) === count( $preset['colors'] ) ) { + $resolved_preset['colors'] = $resolved_colors; + } + } + + $resolved_presets[ $filter_id ] = $resolved_preset; + } + + return $resolved_presets; + } + /** * Adds the duotone SVGs and CSS custom properties to the editor settings. * @@ -1268,6 +1540,7 @@ public static function output_footer_assets() { * in JS and rendered in the post editor. * * @since 6.3.0 + * @since 7.3.0 Added CSS custom property resolution for editor previews. * * @param array $settings The block editor settings from the `block_editor_settings_all` filter. * @return array The editor settings with duotone SVGs and CSS custom properties. @@ -1277,6 +1550,9 @@ public static function add_editor_settings( $settings ) { if ( ! empty( $global_styles_presets ) ) { $settings['styles'] ??= array(); + // The editor needs actual color values to generate SVG filter previews. + $resolved_presets = self::resolve_presets_for_editor( self::$global_styles_presets ); + $settings['styles'][] = array( // For the editor we can add all of the presets by default. 'assets' => self::get_svg_definitions( $global_styles_presets ), @@ -1294,6 +1570,22 @@ public static function add_editor_settings( $settings ) { // These styles are no longer generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings. 'isGlobalStyles' => false, ); + + // This enables color swatches and previews to display correctly. + $resolved_duotone_for_picker = array(); + foreach ( $resolved_presets as $preset ) { + $resolved_duotone_for_picker[] = array( + 'name' => $preset['name'] ?? $preset['slug'], + 'slug' => $preset['slug'], + 'colors' => $preset['colors'], + ); + } + + // Add resolved duotone to editor settings for JavaScript access. + if ( ! isset( $settings['duotone'] ) ) { + $settings['duotone'] = array(); + } + $settings['duotone']['resolved'] = $resolved_duotone_for_picker; } return $settings;