From 85cfb06b94155dcb2b158283c2d0744f8df2bd16 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Mon, 30 Nov 2026 07:54:04 +0530 Subject: [PATCH 1/2] feat: add backport for GH PR gutenberg/pull/74322 --- src/wp-includes/class-wp-duotone.php | 305 ++++++++++++++++++++++++++- 1 file changed, 304 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-duotone.php b/src/wp-includes/class-wp-duotone.php index 49cf221c11af6..fb764687645f1 100644 --- a/src/wp-includes/class-wp-duotone.php +++ b/src/wp-includes/class-wp-duotone.php @@ -148,6 +148,19 @@ class WP_Duotone { */ private static $block_css_declarations = array(); + /** + * Cache of the flattened color palette, keyed by color slug. + * + * Used to resolve color preset references in duotone colors. + * + * @internal + * + * @since 7.3.0 + * + * @var ?array + */ + private static $color_palette_cache = null; + /** * Clamps a value between an upper and lower bound. * @@ -518,6 +531,16 @@ 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 ) { + // Treat an empty value as transparent, so a missing color gives consistent output. + if ( empty( $input ) ) { + return array( + 'r' => 0, + 'g' => 0, + 'b' => 0, + 'a' => 0, + ); + } + $result = self::colord_parse_hex( $input ); if ( ! $result ) { @@ -531,6 +554,220 @@ private static function colord_parse( $input ) { return $result; } + /** + * Gets the flattened color palette from the global settings. + * + * Collects the color presets from the default, theme, and custom origins + * into a single array keyed by slug. + * + * @internal + * + * @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'] ) ) { + // The 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; + } + + /** + * Clears the color palette cache. + * + * Call this when the global settings might have changed, such as when + * switching style variations. + * + * @internal + * + * @since 7.3.0 + */ + public static function clear_color_palette_cache() { + self::$color_palette_cache = null; + } + + /** + * Checks if a color value is a CSS custom property. + * + * Supports these formats: + * - var(--wp--preset--color--slug) + * - var:preset|color|slug + * - var(--wp--custom--name) + * + * @internal + * + * @since 7.3.0 + * + * @param mixed $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; + } + + if ( preg_match( '/^var\(--wp--preset--color--[a-zA-Z0-9_-]+\)$/', $color ) ) { + return true; + } + + if ( preg_match( '/^var:preset\|color\|[a-zA-Z0-9_-]+$/', $color ) ) { + return true; + } + + if ( preg_match( '/^var\(--wp--custom--[a-zA-Z0-9_-]+\)$/', $color ) ) { + return true; + } + + return false; + } + + /** + * Extracts the color slug from a color preset CSS custom property. + * + * @internal + * + * @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 the var(--wp--preset--color--slug) format. + if ( preg_match( '/^var\(--wp--preset--color--([a-zA-Z0-9_-]+)\)$/', $color, $matches ) ) { + return $matches[1]; + } + + // Try the var:preset|color|slug format. + if ( preg_match( '/^var:preset\|color\|([a-zA-Z0-9_-]+)$/', $color, $matches ) ) { + return $matches[1]; + } + + return null; + } + + /** + * Resolves a CSS custom property to its color value. + * + * @internal + * + * @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 could not be resolved. + */ + private static function resolve_css_custom_property( $color ) { + // Return values that are not a CSS custom property as they are. + 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 ] ) ) { + // Resolve again in case the palette color is also a custom property. + return self::resolve_css_custom_property( $palette[ $slug ] ); + } + + return null; + } + + /** + * Resolves the CSS custom properties in an array of colors. + * + * @internal + * + * @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; + continue; + } + + 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' ); + } + + // Keep the original value. + $resolved_colors[] = $color; + } + + return $resolved_colors; + } + + /** + * Checks if any of the colors in an array is a CSS custom property. + * + * @internal + * + * @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 +899,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. + $resolved_colors = self::resolve_duotone_colors( $colors ); + $duotone_values = array( 'r' => array(), 'g' => array(), @@ -669,7 +909,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 +1253,20 @@ 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 the CSS custom properties in the preset can be resolved. + if ( isset( $preset['colors'] ) && is_array( $preset['colors'] ) && self::has_css_custom_properties( $preset['colors'] ) ) { + $resolved = self::resolve_duotone_colors( $preset['colors'] ); + + 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 +1515,40 @@ public static function output_footer_assets() { } } + /** + * Resolves the CSS custom properties in duotone presets for the editor. + * + * Call this before sending duotone presets to the editor, so the SVG + * filters and previews are built from actual color values. + * + * @internal + * + * @since 7.3.0 + * + * @param array $presets The duotone presets, which may contain CSS custom properties. + * @return array The presets with CSS custom properties resolved to 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 the resolved colors if every color resolved. + 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 +1556,7 @@ public static function output_footer_assets() { * in JS and rendered in the post editor. * * @since 6.3.0 + * @since 7.3.0 Added the resolved duotone presets to the editor settings. * * @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. @@ -1294,6 +1583,20 @@ 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, ); + + // The editor needs actual color values to build the previews for the picker. + $resolved_presets = self::resolve_presets_for_editor( $global_styles_presets ); + $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'], + ); + } + + $settings['duotone'] ??= array(); + $settings['duotone']['resolved'] = $resolved_duotone_for_picker; } return $settings; From 849212ff9acb021f9601b380731cb3c7e6355be6 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Mon, 30 Nov 2026 09:16:24 +0530 Subject: [PATCH 2/2] feat: remove extra function from backport pr --- src/wp-includes/class-wp-duotone.php | 157 +++++++++++++-------------- 1 file changed, 73 insertions(+), 84 deletions(-) diff --git a/src/wp-includes/class-wp-duotone.php b/src/wp-includes/class-wp-duotone.php index fb764687645f1..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. * @@ -148,19 +155,6 @@ class WP_Duotone { */ private static $block_css_declarations = array(); - /** - * Cache of the flattened color palette, keyed by color slug. - * - * Used to resolve color preset references in duotone colors. - * - * @internal - * - * @since 7.3.0 - * - * @var ?array - */ - private static $color_palette_cache = null; - /** * Clamps a value between an upper and lower bound. * @@ -531,7 +525,8 @@ 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 ) { - // Treat an empty value as transparent, so a missing color gives consistent output. + + // Handle empty/falsy as transparent otherwise it leads to an inconsistent behaviour. if ( empty( $input ) ) { return array( 'r' => 0, @@ -555,12 +550,10 @@ private static function colord_parse( $input ) { } /** - * Gets the flattened color palette from the global settings. + * Get the flattened color palette from global settings. * - * Collects the color presets from the default, theme, and custom origins - * into a single array keyed by slug. - * - * @internal + * 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 * @@ -577,7 +570,7 @@ private static function get_color_palette() { $palette = $settings['color']['palette'] ?? array(); if ( isset( $palette['default'] ) || isset( $palette['theme'] ) || isset( $palette['custom'] ) ) { - // The palette is organized by origin. + // 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 ) { @@ -599,12 +592,10 @@ private static function get_color_palette() { } /** - * Clears the color palette cache. + * Clear the color palette cache. * - * Call this when the global settings might have changed, such as when - * switching style variations. - * - * @internal + * This should be called when global settings might have changed, + * such as when switching style variations. * * @since 7.3.0 */ @@ -613,18 +604,15 @@ public static function clear_color_palette_cache() { } /** - * Checks if a color value is a CSS custom property. + * Check if a color value is a CSS custom property. * - * Supports these formats: + * Supports multiple formats: * - var(--wp--preset--color--slug) * - var:preset|color|slug - * - var(--wp--custom--name) - * - * @internal * * @since 7.3.0 * - * @param mixed $color The color value to check. + * @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 ) { @@ -632,14 +620,17 @@ private static function is_css_custom_property( $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; } @@ -648,9 +639,7 @@ private static function is_css_custom_property( $color ) { } /** - * Extracts the color slug from a color preset CSS custom property. - * - * @internal + * Extract the color slug from a CSS custom property. * * @since 7.3.0 * @@ -658,12 +647,12 @@ private static function is_css_custom_property( $color ) { * @return string|null The color slug, or null if not found. */ private static function get_slug_from_css_custom_property( $color ) { - // Try the var(--wp--preset--color--slug) format. + // Try var(--wp--preset--color--slug) format. if ( preg_match( '/^var\(--wp--preset--color--([a-zA-Z0-9_-]+)\)$/', $color, $matches ) ) { return $matches[1]; } - // Try the var:preset|color|slug format. + // Try var:preset|color|slug format. if ( preg_match( '/^var:preset\|color\|([a-zA-Z0-9_-]+)$/', $color, $matches ) ) { return $matches[1]; } @@ -672,17 +661,15 @@ private static function get_slug_from_css_custom_property( $color ) { } /** - * Resolves a CSS custom property to its color value. - * - * @internal + * 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 could not be resolved. + * @return string|null The resolved color value, or null if it couldn't be resolved. */ private static function resolve_css_custom_property( $color ) { - // Return values that are not a CSS custom property as they are. + // If it's not a CSS custom property, return as-is. if ( ! self::is_css_custom_property( $color ) ) { return $color; } @@ -696,7 +683,7 @@ private static function resolve_css_custom_property( $color ) { $palette = self::get_color_palette(); if ( isset( $palette[ $slug ] ) ) { - // Resolve again in case the palette color is also a custom property. + // Recursively resolve in case the palette color is also a variable. return self::resolve_css_custom_property( $palette[ $slug ] ); } @@ -704,9 +691,7 @@ private static function resolve_css_custom_property( $color ) { } /** - * Resolves the CSS custom properties in an array of colors. - * - * @internal + * Resolve all CSS custom properties in an array of colors. * * @since 7.3.0 * @@ -725,29 +710,27 @@ private static function resolve_duotone_colors( $colors ) { if ( null !== $resolved ) { $resolved_colors[] = $resolved; - continue; - } + } 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' ); + } - 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; } - - // Keep the original value. - $resolved_colors[] = $color; } return $resolved_colors; } /** - * Checks if any of the colors in an array is a CSS custom property. - * - * @internal + * Check if any colors in the array are CSS custom properties. * * @since 7.3.0 * @@ -899,7 +882,7 @@ 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. + // Resolve any CSS custom properties in the colors array. $resolved_colors = self::resolve_duotone_colors( $colors ); $duotone_values = array( @@ -1254,17 +1237,20 @@ private static function get_all_global_styles_presets() { self::$global_styles_presets[ $filter_id ] = $preset; - // Validate that the CSS custom properties in the preset can be resolved. - if ( isset( $preset['colors'] ) && is_array( $preset['colors'] ) && self::has_css_custom_properties( $preset['colors'] ) ) { - $resolved = self::resolve_duotone_colors( $preset['colors'] ); - - 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' ); + // 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' ); + } } } } @@ -1516,17 +1502,15 @@ public static function output_footer_assets() { } /** - * Resolves the CSS custom properties in duotone presets for the editor. - * - * Call this before sending duotone presets to the editor, so the SVG - * filters and previews are built from actual color values. + * Resolves CSS custom properties in duotone presets for editor use. * - * @internal + * 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, which may contain CSS custom properties. - * @return array The presets with CSS custom properties resolved to colors. + * @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(); @@ -1537,7 +1521,7 @@ private static function resolve_presets_for_editor( $presets ) { if ( isset( $preset['colors'] ) && is_array( $preset['colors'] ) ) { $resolved_colors = self::resolve_duotone_colors( $preset['colors'] ); - // Only use the resolved colors if every color resolved. + // Only use resolved colors if we get valid results. if ( ! empty( $resolved_colors ) && count( $resolved_colors ) === count( $preset['colors'] ) ) { $resolved_preset['colors'] = $resolved_colors; } @@ -1556,7 +1540,7 @@ private static function resolve_presets_for_editor( $presets ) { * in JS and rendered in the post editor. * * @since 6.3.0 - * @since 7.3.0 Added the resolved duotone presets to the editor settings. + * @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. @@ -1566,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 ), @@ -1584,8 +1571,7 @@ public static function add_editor_settings( $settings ) { 'isGlobalStyles' => false, ); - // The editor needs actual color values to build the previews for the picker. - $resolved_presets = self::resolve_presets_for_editor( $global_styles_presets ); + // This enables color swatches and previews to display correctly. $resolved_duotone_for_picker = array(); foreach ( $resolved_presets as $preset ) { $resolved_duotone_for_picker[] = array( @@ -1595,7 +1581,10 @@ public static function add_editor_settings( $settings ) { ); } - $settings['duotone'] ??= array(); + // Add resolved duotone to editor settings for JavaScript access. + if ( ! isset( $settings['duotone'] ) ) { + $settings['duotone'] = array(); + } $settings['duotone']['resolved'] = $resolved_duotone_for_picker; }