From 3de83926d69ed5fcf8c49fa9339492785bf9133e Mon Sep 17 00:00:00 2001 From: isabel_brison Date: Thu, 29 Jun 2023 06:21:28 +0000 Subject: [PATCH] Editor: update duotone support. Updates duotone support after stabilisation of selectors API and adds a few small code quality and UI improvements. Props onemaggie, peterwilsoncc, ajlende, audrasjb, mikeschroder, ramonopoly. Fixes #58555. Built from https://develop.svn.wordpress.org/trunk@56101 git-svn-id: http://core.svn.wordpress.org/trunk@55613 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-supports/duotone.php | 580 +-------- wp-includes/class-wp-duotone.php | 1274 ++++++++++++++++++++ wp-includes/class-wp-theme-json.php | 5 +- wp-includes/default-filters.php | 4 - wp-includes/deprecated.php | 498 ++++++++ wp-includes/global-styles-and-settings.php | 39 - wp-includes/script-loader.php | 28 - wp-includes/version.php | 2 +- wp-settings.php | 1 + 9 files changed, 1800 insertions(+), 631 deletions(-) create mode 100644 wp-includes/class-wp-duotone.php diff --git a/wp-includes/block-supports/duotone.php b/wp-includes/block-supports/duotone.php index 429eeee051..b7964aa478 100644 --- a/wp-includes/block-supports/duotone.php +++ b/wp-includes/block-supports/duotone.php @@ -32,566 +32,32 @@ * @since 5.8.0 */ -/** - * Takes input from [0, n] and returns it as [0, 1]. - * - * Direct port of TinyColor's function, lightly simplified to maintain - * consistency with TinyColor. - * - * @see https://github.com/bgrins/TinyColor - * - * @since 5.8.0 - * @access private - * - * @param mixed $n Number of unknown type. - * @param int $max Upper value of the range to bound to. - * @return float Value in the range [0, 1]. - */ -function wp_tinycolor_bound01( $n, $max ) { - if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) { - $n = '100%'; - } - - $n = min( $max, max( 0, (float) $n ) ); - - // Automatically convert percentage into number. - if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) { - $n = (int) ( $n * $max ) / 100; - } - - // Handle floating point rounding errors. - if ( ( abs( $n - $max ) < 0.000001 ) ) { - return 1.0; - } - - // Convert into [0, 1] range if it isn't already. - return ( $n % $max ) / (float) $max; -} - -/** - * Direct port of tinycolor's boundAlpha function to maintain consistency with - * how tinycolor works. - * - * @see https://github.com/bgrins/TinyColor - * - * @since 5.9.0 - * @access private - * - * @param mixed $n Number of unknown type. - * @return float Value in the range [0,1]. - */ -function _wp_tinycolor_bound_alpha( $n ) { - if ( is_numeric( $n ) ) { - $n = (float) $n; - if ( $n >= 0 && $n <= 1 ) { - return $n; - } - } - return 1; -} - -/** - * Rounds and converts values of an RGB object. - * - * Direct port of TinyColor's function, lightly simplified to maintain - * consistency with TinyColor. - * - * @see https://github.com/bgrins/TinyColor - * - * @since 5.8.0 - * @access private - * - * @param array $rgb_color RGB object. - * @return array Rounded and converted RGB object. - */ -function wp_tinycolor_rgb_to_rgb( $rgb_color ) { - return array( - 'r' => wp_tinycolor_bound01( $rgb_color['r'], 255 ) * 255, - 'g' => wp_tinycolor_bound01( $rgb_color['g'], 255 ) * 255, - 'b' => wp_tinycolor_bound01( $rgb_color['b'], 255 ) * 255, - ); -} - -/** - * Helper function for hsl to rgb conversion. - * - * Direct port of TinyColor's function, lightly simplified to maintain - * consistency with TinyColor. - * - * @see https://github.com/bgrins/TinyColor - * - * @since 5.8.0 - * @access private - * - * @param float $p first component. - * @param float $q second component. - * @param float $t third component. - * @return float R, G, or B component. - */ -function wp_tinycolor_hue_to_rgb( $p, $q, $t ) { - if ( $t < 0 ) { - ++$t; - } - if ( $t > 1 ) { - --$t; - } - if ( $t < 1 / 6 ) { - return $p + ( $q - $p ) * 6 * $t; - } - if ( $t < 1 / 2 ) { - return $q; - } - if ( $t < 2 / 3 ) { - return $p + ( $q - $p ) * ( 2 / 3 - $t ) * 6; - } - return $p; -} - -/** - * Converts an HSL object to an RGB object with converted and rounded values. - * - * Direct port of TinyColor's function, lightly simplified to maintain - * consistency with TinyColor. - * - * @see https://github.com/bgrins/TinyColor - * - * @since 5.8.0 - * @access private - * - * @param array $hsl_color HSL object. - * @return array Rounded and converted RGB object. - */ -function wp_tinycolor_hsl_to_rgb( $hsl_color ) { - $h = wp_tinycolor_bound01( $hsl_color['h'], 360 ); - $s = wp_tinycolor_bound01( $hsl_color['s'], 100 ); - $l = wp_tinycolor_bound01( $hsl_color['l'], 100 ); - - if ( 0 === $s ) { - // Achromatic. - $r = $l; - $g = $l; - $b = $l; - } else { - $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s; - $p = 2 * $l - $q; - $r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 ); - $g = wp_tinycolor_hue_to_rgb( $p, $q, $h ); - $b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 ); - } - - return array( - 'r' => $r * 255, - 'g' => $g * 255, - 'b' => $b * 255, - ); -} - -/** - * Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2 - * used in the JavaScript. Only colors output from react-color are implemented. - * - * Direct port of TinyColor's function, lightly simplified to maintain - * consistency with TinyColor. - * - * @see https://github.com/bgrins/TinyColor - * @see https://github.com/casesandberg/react-color/ - * - * @since 5.8.0 - * @since 5.9.0 Added alpha processing. - * @access private - * - * @param string $color_str CSS color string. - * @return array RGB object. - */ -function wp_tinycolor_string_to_rgb( $color_str ) { - $color_str = strtolower( trim( $color_str ) ); - - $css_integer = '[-\\+]?\\d+%?'; - $css_number = '[-\\+]?\\d*\\.\\d+%?'; - - $css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')'; - - $permissive_match3 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?'; - $permissive_match4 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?'; - - $rgb_regexp = '/^rgb' . $permissive_match3 . '$/'; - if ( preg_match( $rgb_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => $match[1], - 'g' => $match[2], - 'b' => $match[3], - ) - ); - - $rgb['a'] = 1; - - return $rgb; - } - - $rgba_regexp = '/^rgba' . $permissive_match4 . '$/'; - if ( preg_match( $rgba_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => $match[1], - 'g' => $match[2], - 'b' => $match[3], - ) - ); - - $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] ); - - return $rgb; - } - - $hsl_regexp = '/^hsl' . $permissive_match3 . '$/'; - if ( preg_match( $hsl_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_hsl_to_rgb( - array( - 'h' => $match[1], - 's' => $match[2], - 'l' => $match[3], - ) - ); - - $rgb['a'] = 1; - - return $rgb; - } - - $hsla_regexp = '/^hsla' . $permissive_match4 . '$/'; - if ( preg_match( $hsla_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_hsl_to_rgb( - array( - 'h' => $match[1], - 's' => $match[2], - 'l' => $match[3], - ) - ); - - $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] ); - - return $rgb; - } - - $hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/'; - if ( preg_match( $hex8_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => base_convert( $match[1], 16, 10 ), - 'g' => base_convert( $match[2], 16, 10 ), - 'b' => base_convert( $match[3], 16, 10 ), - ) - ); - - $rgb['a'] = _wp_tinycolor_bound_alpha( - base_convert( $match[4], 16, 10 ) / 255 - ); - - return $rgb; - } - - $hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/'; - if ( preg_match( $hex6_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => base_convert( $match[1], 16, 10 ), - 'g' => base_convert( $match[2], 16, 10 ), - 'b' => base_convert( $match[3], 16, 10 ), - ) - ); - - $rgb['a'] = 1; - - return $rgb; - } - - $hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/'; - if ( preg_match( $hex4_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => base_convert( $match[1] . $match[1], 16, 10 ), - 'g' => base_convert( $match[2] . $match[2], 16, 10 ), - 'b' => base_convert( $match[3] . $match[3], 16, 10 ), - ) - ); - - $rgb['a'] = _wp_tinycolor_bound_alpha( - base_convert( $match[4] . $match[4], 16, 10 ) / 255 - ); - - return $rgb; - } - - $hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/'; - if ( preg_match( $hex3_regexp, $color_str, $match ) ) { - $rgb = wp_tinycolor_rgb_to_rgb( - array( - 'r' => base_convert( $match[1] . $match[1], 16, 10 ), - 'g' => base_convert( $match[2] . $match[2], 16, 10 ), - 'b' => base_convert( $match[3] . $match[3], 16, 10 ), - ) - ); - - $rgb['a'] = 1; - - return $rgb; - } - - /* - * The JS color picker considers the string "transparent" to be a hex value, - * so we need to handle it here as a special case. - */ - if ( 'transparent' === $color_str ) { - return array( - 'r' => 0, - 'g' => 0, - 'b' => 0, - 'a' => 0, - ); - } -} - -/** - * Returns the prefixed id for the duotone filter for use as a CSS id. - * - * @since 5.9.1 - * @access private - * - * @param array $preset Duotone preset value as seen in theme.json. - * @return string Duotone filter CSS id. - */ -function wp_get_duotone_filter_id( $preset ) { - if ( ! isset( $preset['slug'] ) ) { - return ''; - } - - return 'wp-duotone-' . $preset['slug']; -} - -/** - * Returns the CSS filter property url to reference the rendered SVG. - * - * @since 5.9.0 - * @since 6.1.0 Allow unset for preset colors. - * @access private - * - * @param array $preset Duotone preset value as seen in theme.json. - * @return string Duotone CSS filter property url value. - */ -function wp_get_duotone_filter_property( $preset ) { - if ( isset( $preset['colors'] ) && 'unset' === $preset['colors'] ) { - return 'none'; - } - $filter_id = wp_get_duotone_filter_id( $preset ); - return "url('#" . $filter_id . "')"; -} - -/** - * Returns the duotone filter SVG string for the preset. - * - * @since 5.9.1 - * @access private - * - * @param array $preset Duotone preset value as seen in theme.json. - * @return string Duotone SVG filter. - */ -function wp_get_duotone_filter_svg( $preset ) { - $filter_id = wp_get_duotone_filter_id( $preset ); - - $duotone_values = array( - 'r' => array(), - 'g' => array(), - 'b' => array(), - 'a' => array(), - ); - - if ( ! isset( $preset['colors'] ) || ! is_array( $preset['colors'] ) ) { - $preset['colors'] = array(); - } - - foreach ( $preset['colors'] as $color_str ) { - $color = wp_tinycolor_string_to_rgb( $color_str ); - - $duotone_values['r'][] = $color['r'] / 255; - $duotone_values['g'][] = $color['g'] / 255; - $duotone_values['b'][] = $color['b'] / 255; - $duotone_values['a'][] = $color['a']; - } - - ob_start(); - - ?> - - - - - - - - - - - - - - - - - <', '><', $svg ); - $svg = trim( $svg ); - } - - return $svg; -} - -/** - * Registers the style and colors block attributes for block types that support it. - * - * @since 5.8.0 - * @access private - * - * @param WP_Block_Type $block_type Block Type. - */ -function wp_register_duotone_support( $block_type ) { - $has_duotone_support = false; - if ( property_exists( $block_type, 'supports' ) ) { - $has_duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); - } - - if ( $has_duotone_support ) { - if ( ! $block_type->attributes ) { - $block_type->attributes = array(); - } - - if ( ! array_key_exists( 'style', $block_type->attributes ) ) { - $block_type->attributes['style'] = array( - 'type' => 'object', - ); - } - } -} - -/** - * Renders out the duotone stylesheet and SVG. - * - * @since 5.8.0 - * @since 6.1.0 Allow unset for preset colors. - * @access private - * - * @param string $block_content Rendered block content. - * @param array $block Block object. - * @return string Filtered block content. - */ -function wp_render_duotone_support( $block_content, $block ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - - $duotone_support = false; - if ( $block_type && property_exists( $block_type, 'supports' ) ) { - $duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); - } - - $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] ); - - if ( - ! $duotone_support || - ! $has_duotone_attribute - ) { - return $block_content; - } - - $colors = $block['attrs']['style']['color']['duotone']; - $filter_key = is_array( $colors ) ? implode( '-', $colors ) : $colors; - $filter_preset = array( - 'slug' => wp_unique_id( sanitize_key( $filter_key . '-' ) ), - 'colors' => $colors, - ); - $filter_property = wp_get_duotone_filter_property( $filter_preset ); - $filter_id = wp_get_duotone_filter_id( $filter_preset ); - - $scope = '.' . $filter_id; - $selectors = explode( ',', $duotone_support ); - $scoped = array(); - foreach ( $selectors as $sel ) { - $scoped[] = $scope . ' ' . trim( $sel ); - } - $selector = implode( ', ', $scoped ); - - // !important is needed because these styles render before global styles, - // and they should be overriding the duotone filters set by global styles. - $filter_style = SCRIPT_DEBUG - ? $selector . " {\n\tfilter: " . $filter_property . " !important;\n}\n" - : $selector . '{filter:' . $filter_property . ' !important;}'; - - wp_register_style( $filter_id, false ); - wp_add_inline_style( $filter_id, $filter_style ); - wp_enqueue_style( $filter_id ); - - if ( 'unset' !== $colors ) { - $filter_svg = wp_get_duotone_filter_svg( $filter_preset ); - add_action( - 'wp_footer', - static function () use ( $filter_svg, $selector ) { - echo $filter_svg; - - /* - * Safari renders elements incorrectly on first paint when the - * SVG filter comes after the content that it is filtering, so - * we force a repaint with a WebKit hack which solves the issue. - */ - global $is_safari; - if ( $is_safari ) { - /* - * Simply accessing el.offsetHeight flushes layout and style - * changes in WebKit without having to wait for setTimeout. - */ - printf( - '', - wp_json_encode( $selector ) - ); - } - } - ); - } - - // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper. - return preg_replace( - '/' . preg_quote( 'class="', '/' ) . '/', - 'class="' . $filter_id . ' ', - $block_content, - 1 - ); -} - // Register the block support. WP_Block_Supports::get_instance()->register( 'duotone', array( - 'register_attribute' => 'wp_register_duotone_support', + 'register_attribute' => array( 'WP_Duotone', 'register_duotone_support' ), ) ); -add_filter( 'render_block', 'wp_render_duotone_support', 10, 2 ); + +// Set up metadata prior to rendering any blocks. +add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_styles_presets' ), 10 ); +add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_style_block_names' ), 10 ); + +// Add classnames to blocks using duotone support. +add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 2 ); + +// Enqueue styles. +// Block styles (core-block-supports-inline-css) before the style engine (wp_enqueue_stored_styles). +// Global styles (global-styles-inline-css) after the other global styles (wp_enqueue_global_styles). +add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_block_styles' ), 9 ); +add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_global_styles' ), 11 ); + +// Add SVG filters to the footer. Also, for classic themes, output block styles (core-block-supports-inline-css). +add_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ), 10 ); + +// Add styles and SVGs for use in the editor via the EditorStyles component. +add_filter( 'block_editor_settings_all', array( 'WP_Duotone', 'add_editor_settings' ), 10 ); + +// Migrate the old experimental duotone support flag. +add_filter( 'block_type_metadata_settings', array( 'WP_Duotone', 'migrate_experimental_duotone_support_flag' ), 10, 2 ); diff --git a/wp-includes/class-wp-duotone.php b/wp-includes/class-wp-duotone.php new file mode 100644 index 0000000000..99da521f06 --- /dev/null +++ b/wp-includes/class-wp-duotone.php @@ -0,0 +1,1274 @@ + 'blue-orange', + * … + * ] + * + * @internal + * + * @since 6.3.0 + * + * @var array + */ + private static $global_styles_block_names = array(); + + /** + * An array of duotone filter data from global, theme, and custom presets. + * + * Example: + * [ + * 'wp-duotone-blue-orange' => [ + * 'slug' => 'blue-orange', + * 'colors' => [ '#0000ff', '#ffcc00' ], + * ], + * 'wp-duotone-red-yellow' => [ + * 'slug' => 'red-yellow', + * 'colors' => [ '#cc0000', '#ffff33' ], + * ], + * … + * ] + * + * @internal + * + * @since 6.3.0 + * + * @var array + */ + private static $global_styles_presets = array(); + + /** + * All of the duotone filter data from presets for CSS custom properties on + * the page. + * + * Example: + * [ + * 'wp-duotone-blue-orange' => [ + * 'slug' => 'blue-orange', + * 'colors' => [ '#0000ff', '#ffcc00' ], + * ], + * … + * ] + * + * @internal + * + * @since 6.3.0 + * + * @var array + */ + private static $used_global_styles_presets = array(); + + /** + * All of the duotone filter data for SVGs on the page. Includes both + * presets and custom filters. + * + * Example: + * [ + * 'wp-duotone-blue-orange' => [ + * 'slug' => 'blue-orange', + * 'colors' => [ '#0000ff', '#ffcc00' ], + * ], + * 'wp-duotone-000000-ffffff-2' => [ + * 'slug' => '000000-ffffff-2', + * 'colors' => [ '#000000', '#ffffff' ], + * ], + * … + * ] + * + * @internal + * + * @since 6.3.0 + * + * @var array + */ + private static $used_svg_filter_data = array(); + + /** + * All of the block CSS declarations for styles on the page. + * + * Example: + * [ + * [ + * 'selector' => '.wp-duotone-000000-ffffff-2.wp-block-image img', + * 'declarations' => [ + * 'filter' => 'url(#wp-duotone-000000-ffffff-2)', + * ], + * ], + * … + * ] + * + * @internal + * + * @since 6.3.0 + * + * @var array + */ + private static $block_css_declarations = array(); + + /** + * Clamps a value between an upper and lower bound. + * + * Direct port of colord's clamp function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L23 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param float $number The number to clamp. + * @param float $min The minimum value. + * @param float $max The maximum value. + * @return float The clamped value. + */ + private static function colord_clamp( $number, $min = 0, $max = 1 ) { + return $number > $max ? $max : ( $number > $min ? $number : $min ); + } + + /** + * Processes and clamps a degree (angle) value properly. + * + * Direct port of colord's clampHue function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L32 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param float $degrees The hue to clamp. + * @return float The clamped hue. + */ + private static function colord_clamp_hue( $degrees ) { + $degrees = is_finite( $degrees ) ? $degrees % 360 : 0; + return $degrees > 0 ? $degrees : $degrees + 360; + } + + /** + * Converts a hue value to degrees from 0 to 360 inclusive. + * + * Direct port of colord's parseHue function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L40 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param float $value The hue value to parse. + * @param string $unit The unit of the hue value. + * @return float The parsed hue value. + */ + private static function colord_parse_hue( $value, $unit = 'deg' ) { + $angle_units = array( + 'grad' => 360 / 400, + 'turn' => 360, + 'rad' => 360 / ( M_PI * 2 ), + ); + + $factor = $angle_units[ $unit ]; + if ( ! $factor ) { + $factor = 1; + } + + return (float) $value * $factor; + } + + /** + * Parses any valid Hex3, Hex4, Hex6 or Hex8 string and converts it to an RGBA object + * + * Direct port of colord's parseHex function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hex.ts#L8 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param string $hex The hex string to parse. + * @return array|null An array of RGBA values or null if the hex string is invalid. + */ + private static function colord_parse_hex( $hex ) { + $is_match = preg_match( + '/^#([0-9a-f]{3,8})$/i', + $hex, + $hex_match + ); + + if ( ! $is_match ) { + return null; + } + + $hex = $hex_match[1]; + + if ( 4 >= strlen( $hex ) ) { + return array( + 'r' => (int) base_convert( $hex[0] . $hex[0], 16, 10 ), + 'g' => (int) base_convert( $hex[1] . $hex[1], 16, 10 ), + 'b' => (int) base_convert( $hex[2] . $hex[2], 16, 10 ), + 'a' => 4 === strlen( $hex ) ? round( base_convert( $hex[3] . $hex[3], 16, 10 ) / 255, 2 ) : 1, + ); + } + + if ( 6 === strlen( $hex ) || 8 === strlen( $hex ) ) { + return array( + 'r' => (int) base_convert( substr( $hex, 0, 2 ), 16, 10 ), + 'g' => (int) base_convert( substr( $hex, 2, 2 ), 16, 10 ), + 'b' => (int) base_convert( substr( $hex, 4, 2 ), 16, 10 ), + 'a' => 8 === strlen( $hex ) ? round( (int) base_convert( substr( $hex, 6, 2 ), 16, 10 ) / 255, 2 ) : 1, + ); + } + + return null; + } + + /** + * Clamps an array of RGBA values. + * + * Direct port of colord's clampRgba function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgb.ts#L5 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param array $rgba The RGBA array to clamp. + * @return array The clamped RGBA array. + */ + private static function colord_clamp_rgba( $rgba ) { + $rgba['r'] = self::colord_clamp( $rgba['r'], 0, 255 ); + $rgba['g'] = self::colord_clamp( $rgba['g'], 0, 255 ); + $rgba['b'] = self::colord_clamp( $rgba['b'], 0, 255 ); + $rgba['a'] = self::colord_clamp( $rgba['a'] ); + + return $rgba; + } + + /** + * Parses a valid RGB[A] CSS color function/string + * + * Direct port of colord's parseRgbaString function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgbString.ts#L18 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param string $input The RGBA string to parse. + * @return array|null An array of RGBA values or null if the RGB string is invalid. + */ + private static function colord_parse_rgba_string( $input ) { + // Functional syntax. + $is_match = preg_match( + '/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i', + $input, + $match + ); + + if ( ! $is_match ) { + // Whitespace syntax. + $is_match = preg_match( + '/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i', + $input, + $match + ); + } + + if ( ! $is_match ) { + return null; + } + + /* + * For some reason, preg_match doesn't include empty matches at the end + * of the array, so we add them manually to make things easier later. + */ + for ( $i = 1; $i <= 8; $i++ ) { + if ( ! isset( $match[ $i ] ) ) { + $match[ $i ] = ''; + } + } + + if ( $match[2] !== $match[4] || $match[4] !== $match[6] ) { + return null; + } + + return self::colord_clamp_rgba( + array( + 'r' => (float) $match[1] / ( $match[2] ? 100 / 255 : 1 ), + 'g' => (float) $match[3] / ( $match[4] ? 100 / 255 : 1 ), + 'b' => (float) $match[5] / ( $match[6] ? 100 / 255 : 1 ), + 'a' => '' === $match[7] ? 1 : (float) $match[7] / ( $match[8] ? 100 : 1 ), + ) + ); + } + + /** + * Clamps an array of HSLA values. + * + * Direct port of colord's clampHsla function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L6 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param array $hsla The HSLA array to clamp. + * @return array The clamped HSLA array. + */ + private static function colord_clamp_hsla( $hsla ) { + $hsla['h'] = self::colord_clamp_hue( $hsla['h'] ); + $hsla['s'] = self::colord_clamp( $hsla['s'], 0, 100 ); + $hsla['l'] = self::colord_clamp( $hsla['l'], 0, 100 ); + $hsla['a'] = self::colord_clamp( $hsla['a'] ); + + return $hsla; + } + + /** + * Converts an HSVA array to RGBA. + * + * Direct port of colord's hsvaToRgba function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsv.ts#L52 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param array $hsva The HSVA array to convert. + * @return array The RGBA array. + */ + private static function colord_hsva_to_rgba( $hsva ) { + $h = ( $hsva['h'] / 360 ) * 6; + $s = $hsva['s'] / 100; + $v = $hsva['v'] / 100; + $a = $hsva['a']; + + $hh = floor( $h ); + $b = $v * ( 1 - $s ); + $c = $v * ( 1 - ( $h - $hh ) * $s ); + $d = $v * ( 1 - ( 1 - $h + $hh ) * $s ); + $module = $hh % 6; + + return array( + 'r' => array( $v, $c, $b, $b, $d, $v )[ $module ] * 255, + 'g' => array( $d, $v, $v, $c, $b, $b )[ $module ] * 255, + 'b' => array( $b, $b, $d, $v, $v, $c )[ $module ] * 255, + 'a' => $a, + ); + } + + /** + * Converts an HSLA array to HSVA. + * + * Direct port of colord's hslaToHsva function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L33 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param array $hsla The HSLA array to convert. + * @return array The HSVA array. + */ + private static function colord_hsla_to_hsva( $hsla ) { + $h = $hsla['h']; + $s = $hsla['s']; + $l = $hsla['l']; + $a = $hsla['a']; + + $s *= ( $l < 50 ? $l : 100 - $l ) / 100; + + return array( + 'h' => $h, + 's' => $s > 0 ? ( ( 2 * $s ) / ( $l + $s ) ) * 100 : 0, + 'v' => $l + $s, + 'a' => $a, + ); + } + + /** + * Converts an HSLA array to RGBA. + * + * Direct port of colord's hslaToRgba function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L55 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param array $hsla The HSLA array to convert. + * @return array The RGBA array. + */ + private static function colord_hsla_to_rgba( $hsla ) { + return self::colord_hsva_to_rgba( self::colord_hsla_to_hsva( $hsla ) ); + } + + /** + * Parses a valid HSL[A] CSS color function/string. + * + * Direct port of colord's parseHslaString function. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hslString.ts#L17 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param string $input The HSLA string to parse. + * @return array|null An array of RGBA values or null if the RGB string is invalid. + */ + private static function colord_parse_hsla_string( $input ) { + // Functional syntax. + $is_match = preg_match( + '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i', + $input, + $match + ); + + if ( ! $is_match ) { + // Whitespace syntax. + $is_match = preg_match( + '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i', + $input, + $match + ); + } + + if ( ! $is_match ) { + return null; + } + + /* + * For some reason, preg_match doesn't include empty matches at the end + * of the array, so we add them manually to make things easier later. + */ + for ( $i = 1; $i <= 6; $i++ ) { + if ( ! isset( $match[ $i ] ) ) { + $match[ $i ] = ''; + } + } + + $hsla = self::colord_clamp_hsla( + array( + 'h' => self::colord_parse_hue( $match[1], $match[2] ), + 's' => (float) $match[3], + 'l' => (float) $match[4], + 'a' => '' === $match[5] ? 1 : (float) $match[5] / ( $match[6] ? 100 : 1 ), + ) + ); + + return self::colord_hsla_to_rgba( $hsla ); + } + + /** + * Tries to convert an incoming string into RGBA values. + * + * Direct port of colord's parse function simplified for our use case. This + * version only supports string parsing and only returns RGBA values. + * + * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/parse.ts#L37 Sourced from colord. + * + * @internal + * + * @since 6.3.0 + * + * @param string $input The string to parse. + * @return array|null An array of RGBA values or null if the string is invalid. + */ + private static function colord_parse( $input ) { + $result = self::colord_parse_hex( $input ); + + if ( ! $result ) { + $result = self::colord_parse_rgba_string( $input ); + } + + if ( ! $result ) { + $result = self::colord_parse_hsla_string( $input ); + } + + return $result; + } + + /** + * Takes the inline CSS duotone variable from a block and return the slug. + * + * Handles styles slugs like: + * var:preset|duotone|blue-orange + * var(--wp--preset--duotone--blue-orange) + * + * @internal + * + * @since 6.3.0 + * + * @param string $duotone_attr The duotone attribute from a block. + * @return string The slug of the duotone preset or an empty string if no slug is found. + */ + private static function get_slug_from_attribute( $duotone_attr ) { + // Uses Branch Reset Groups `(?|…)` to return one capture group. + preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches ); + + return ! empty( $matches[1] ) ? $matches[1] : ''; + } + + /** + * Checks if we have a valid duotone preset. + * + * Valid presets are defined in the $global_styles_presets array. + * + * @internal + * + * @since 6.3.0 + * + * @param string $duotone_attr The duotone attribute from a block. + * @return bool True if the duotone preset present and valid. + */ + private static function is_preset( $duotone_attr ) { + $slug = self::get_slug_from_attribute( $duotone_attr ); + $filter_id = self::get_filter_id( $slug ); + + return array_key_exists( $filter_id, self::$global_styles_presets ); + } + + /** + * Gets the CSS variable name for a duotone preset. + * + * Example output: + * --wp--preset--duotone--blue-orange + * + * @internal + * + * @since 6.3.0 + * + * @param string $slug The slug of the duotone preset. + * @return string The CSS variable name. + */ + private static function get_css_custom_property_name( $slug ) { + return "--wp--preset--duotone--$slug"; + } + + /** + * Get the ID of the duotone filter. + * + * Example output: + * wp-duotone-blue-orange + * + * @internal + * + * @since 6.3.0 + * + * @param string $slug The slug of the duotone preset. + * @return string The ID of the duotone filter. + */ + private static function get_filter_id( $slug ) { + return "wp-duotone-$slug"; + } + + /** + * Get the CSS variable for a duotone preset. + * + * Example output: + * var(--wp--preset--duotone--blue-orange) + * + * @internal + * + * @since 6.3.0 + * + * @param string $slug The slug of the duotone preset. + * @return string The CSS variable. + */ + private static function get_css_var( $slug ) { + $name = self::get_css_custom_property_name( $slug ); + return "var($name)"; + } + + /** + * Get the URL for a duotone filter. + * + * Example output: + * url(#wp-duotone-blue-orange) + * + * @internal + * + * @since 6.3.0 + * + * @param string $filter_id The ID of the filter. + * @return string The URL for the duotone filter. + */ + private static function get_filter_url( $filter_id ) { + return "url(#$filter_id)"; + } + + /** + * Gets the SVG for the duotone filter definition. + * + * Whitespace is removed when SCRIPT_DEBUG is not enabled. + * + * @internal + * + * @since 6.3.0 + * + * @param string $filter_id The ID of the filter. + * @param array $colors An array of color strings. + * @return string An SVG with a duotone filter definition. + */ + private static function get_filter_svg( $filter_id, $colors ) { + $duotone_values = array( + 'r' => array(), + 'g' => array(), + 'b' => array(), + 'a' => array(), + ); + + foreach ( $colors as $color_str ) { + $color = self::colord_parse( $color_str ); + + if ( null === $color ) { + $error_message = sprintf( + /* translators: %s: duotone colors */ + __( '"%s" in theme.json settings.color.duotone is not a hex or rgb string.' ), + $color_str + ); + _doing_it_wrong( __METHOD__, $error_message, '6.3.0' ); + } else { + $duotone_values['r'][] = $color['r'] / 255; + $duotone_values['g'][] = $color['g'] / 255; + $duotone_values['b'][] = $color['b'] / 255; + $duotone_values['a'][] = $color['a']; + } + } + + ob_start(); + + ?> + + + + + + + + + + + + + + + + + <', '><', $svg ); + $svg = trim( $svg ); + } + + return $svg; + } + + /** + * Returns the prefixed id for the duotone filter for use as a CSS id. + * + * Exported for the deprecated function wp_get_duotone_filter_id(). + * + * @internal + * + * @since 6.3.0 + * @deprecated 6.3.0 + * + * @param array $preset Duotone preset value as seen in theme.json. + * @return string Duotone filter CSS id. + */ + public static function get_filter_id_from_preset( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + $filter_id = ''; + if ( isset( $preset['slug'] ) ) { + $filter_id = self::get_filter_id( $preset['slug'] ); + } + return $filter_id; + } + + /** + * Gets the SVG for the duotone filter definition from a preset. + * + * Exported for the deprecated function wp_get_duotone_filter_property(). + * + * @internal + * + * @since 6.3.0 + * @deprecated 6.3.0 + * + * @param array $preset The duotone preset. + * @return string The SVG for the filter definition. + */ + public static function get_filter_svg_from_preset( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + $filter_id = self::get_filter_id_from_preset( $preset ); + return self::get_filter_svg( $filter_id, $preset['colors'] ); + } + + /** + * Get the SVGs for the duotone filters. + * + * Example output: + * + * + * @internal + * + * @since 6.3.0 + * + * @param array $sources The duotone presets. + * @return string The SVGs for the duotone filters. + */ + private static function get_svg_definitions( $sources ) { + $svgs = ''; + foreach ( $sources as $filter_id => $filter_data ) { + $colors = $filter_data['colors']; + $svgs .= self::get_filter_svg( $filter_id, $colors ); + } + return $svgs; + } + + /** + * Get the CSS for global styles. + * + * Example output: + * body{--wp--preset--duotone--blue-orange:url('#wp-duotone-blue-orange');} + * + * @internal + * + * @since 6.3.0 + * + * @param array $sources The duotone presets. + * @return string The CSS for global styles. + */ + private static function get_global_styles_presets( $sources ) { + $css = 'body{'; + foreach ( $sources as $filter_id => $filter_data ) { + $slug = $filter_data['slug']; + $colors = $filter_data['colors']; + $css_property_name = self::get_css_custom_property_name( $slug ); + $declaration_value = is_string( $colors ) ? $colors : self::get_filter_url( $filter_id ); + $css .= "$css_property_name:$declaration_value;"; + } + $css .= '}'; + return $css; + } + + /** + * Enqueue a block CSS declaration for the page. + * + * This does not include any SVGs. + * + * @internal + * + * @since 6.3.0 + * + * @param string $filter_id The filter ID. e.g. 'wp-duotone-000000-ffffff-2'. + * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. + * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'. + */ + private static function enqueue_block_css( $filter_id, $duotone_selector, $filter_value ) { + // Build the CSS selectors to which the filter will be applied. + $selectors = explode( ',', $duotone_selector ); + + $selectors_scoped = array(); + foreach ( $selectors as $selector_part ) { + /* + * Assuming the selector part is a subclass selector (not a tag name) + * so we can prepend the filter id class. If we want to support elements + * such as `img` or namespaces, we'll need to add a case for that here. + */ + $selectors_scoped[] = '.' . $filter_id . trim( $selector_part ); + } + + $selector = implode( ', ', $selectors_scoped ); + + self::$block_css_declarations[] = array( + 'selector' => $selector, + 'declarations' => array( + 'filter' => $filter_value, + ), + ); + } + + /** + * Enqueue custom filter assets for the page. + * + * Includes an SVG filter and block CSS declaration. + * + * @internal + * + * @since 6.3.0 + * + * @param string $filter_id The filter ID. e.g. 'wp-duotone-000000-ffffff-2'. + * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. + * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'. + * @param array $filter_data Duotone filter data with 'slug' and 'colors' keys. + */ + private static function enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ) { + self::$used_svg_filter_data[ $filter_id ] = $filter_data; + self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value ); + } + + /** + * Enqueue preset assets for the page. + * + * Includes a CSS custom property, SVG filter, and block CSS declaration. + * + * @internal + * + * @since 6.3.0 + * + * @param string $filter_id The filter ID. e.g. 'wp-duotone-blue-orange'. + * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. + * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'. + */ + private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) { + if ( ! array_key_exists( $filter_id, self::$global_styles_presets ) ) { + $error_message = sprintf( + /* translators: %s: duotone filter ID */ + __( 'The duotone id "%s" is not registered in theme.json settings' ), + $filter_id + ); + _doing_it_wrong( __METHOD__, $error_message, '6.3.0' ); + return; + } + self::$used_global_styles_presets[ $filter_id ] = self::$global_styles_presets[ $filter_id ]; + self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, self::$global_styles_presets[ $filter_id ] ); + } + + /** + * Registers the style and colors block attributes for block types that support it. + * + * Block support is added with `supports.filter.duotone` in block.json. + * + * @since 6.3.0 + * + * @param WP_Block_Type $block_type Block Type. + */ + public static function register_duotone_support( $block_type ) { + $has_duotone_support = false; + if ( property_exists( $block_type, 'supports' ) ) { + /* + * Previous `color.__experimentalDuotone` support flag is migrated + * to `filter.duotone` via `block_type_metadata_settings` filter. + */ + $has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), null ); + } + + if ( $has_duotone_support ) { + if ( ! $block_type->attributes ) { + $block_type->attributes = array(); + } + + if ( ! array_key_exists( 'style', $block_type->attributes ) ) { + $block_type->attributes['style'] = array( + 'type' => 'object', + ); + } + } + } + + /** + * Get the CSS selector for a block type. + * + * This handles selectors defined in `color.__experimentalDuotone` support + * if `filter.duotone` support is not defined. + * + * @param string $block_name The block name. + * + * @internal + * + * @since 6.3.0 + * + * @return string The CSS selector or null if there is no support. + */ + private static function get_selector( $block_name ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + + if ( $block_type && property_exists( $block_type, 'supports' ) ) { + /* + * Backwards compatibility with `supports.color.__experimentalDuotone` + * is provided via the `block_type_metadata_settings` filter. If + * `supports.filter.duotone` has not been set and the experimental + * property has been, the experimental property value is copied into + * `supports.filter.duotone`. + */ + $duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false ); + if ( ! $duotone_support ) { + return null; + } + + /* + * If the experimental duotone support was set, that value is to be + * treated as a selector and requires scoping. + */ + $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); + if ( $experimental_duotone ) { + $root_selector = wp_get_block_css_selector( $block_type ); + return is_string( $experimental_duotone ) + ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone ) + : $root_selector; + } + + // Regular filter.duotone support uses filter.duotone selectors with fallbacks. + return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); + } + } + + /** + * Scrape all possible duotone presets from global and theme styles and + * store them in self::$global_styles_presets. + * + * Used in conjunction with self::render_duotone_support for blocks that + * use duotone preset filters. + * + * @since 6.3.0 + */ + public static function set_global_styles_presets() { + // Get the per block settings from the theme.json. + $tree = wp_get_global_settings(); + $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() ); + + foreach ( $presets_by_origin as $presets ) { + foreach ( $presets as $preset ) { + $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); + + self::$global_styles_presets[ $filter_id ] = $preset; + } + } + } + + /** + * Scrape all block names from global styles and store in self::$global_styles_block_names. + * + * Used in conjunction with self::render_duotone_support to output the + * duotone filters defined in the theme.json global styles. + * + * @since 6.3.0 + */ + public static function set_global_style_block_names() { + // Get the per block settings from the theme.json. + $tree = WP_Theme_JSON_Resolver::get_merged_data(); + $block_nodes = $tree->get_styles_block_nodes(); + $theme_json = $tree->get_raw_data(); + + foreach ( $block_nodes as $block_node ) { + // This block definition doesn't include any duotone settings. Skip it. + if ( empty( $block_node['duotone'] ) ) { + continue; + } + + // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'. + $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) ); + $duotone_attr = _wp_array_get( $theme_json, $duotone_attr_path, array() ); + + if ( empty( $duotone_attr ) ) { + continue; + } + // If it has a duotone filter preset, save the block name and the preset slug. + $slug = self::get_slug_from_attribute( $duotone_attr ); + + if ( $slug && $slug !== $duotone_attr ) { + self::$global_styles_block_names[ $block_node['name'] ] = $slug; + } + } + } + + /** + * Render out the duotone CSS styles and SVG. + * + * The hooks self::set_global_style_block_names and self::set_global_styles_presets + * must be called before this function. + * + * @since 6.3.0 + * + * @param string $block_content Rendered block content. + * @param array $block Block object. + * @return string Filtered block content. + */ + public static function render_duotone_support( $block_content, $block ) { + $duotone_selector = self::get_selector( $block['blockName'] ); + + // The block should have a duotone attribute or have duotone defined in its theme.json to be processed. + $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] ); + $has_global_styles_duotone = array_key_exists( $block['blockName'], self::$global_styles_block_names ); + + if ( + empty( $block_content ) || + ! $duotone_selector || + ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) + ) { + return $block_content; + } + + // Generate the pieces needed for rendering a duotone to the page. + if ( $has_duotone_attribute ) { + + /* + * Possible values for duotone attribute: + * 1. Array of colors - e.g. array('#000000', '#ffffff'). + * 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|blue-orange' or 'var(--wp--preset--duotone--blue-orange)'' + * 3. A CSS string - e.g. 'unset' to remove globally applied duotone. + */ + + $duotone_attr = $block['attrs']['style']['color']['duotone']; + $is_preset = is_string( $duotone_attr ) && self::is_preset( $duotone_attr ); + $is_css = is_string( $duotone_attr ) && ! $is_preset; + $is_custom = is_array( $duotone_attr ); + + if ( $is_preset ) { + + $slug = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'blue-orange'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. + $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. + + // CSS custom property, SVG filter, and block CSS. + self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); + + } elseif ( $is_css ) { + $slug = wp_unique_id( sanitize_key( $duotone_attr . '-' ) ); // e.g. 'unset-1'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-unset-1'. + $filter_value = $duotone_attr; // e.g. 'unset'. + + // Just block CSS. + self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value ); + } elseif ( $is_custom ) { + $slug = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); // e.g. '000000-ffffff-2'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-000000-ffffff-2'. + $filter_value = self::get_filter_url( $filter_id ); // e.g. 'url(#wp-duotone-filter-000000-ffffff-2)'. + $filter_data = array( + 'slug' => $slug, + 'colors' => $duotone_attr, + ); + + // SVG filter and block CSS. + self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ); + } + } elseif ( $has_global_styles_duotone ) { + $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. + $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. + + // CSS custom property, SVG filter, and block CSS. + self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); + } + + // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper. + $tags = new WP_HTML_Tag_Processor( $block_content ); + if ( $tags->next_tag() ) { + $tags->add_class( $filter_id ); + } + return $tags->get_updated_html(); + } + + /** + * Appends the used block duotone filter declarations to the inline block supports CSS. + * + * Uses the declarations saved in earlier calls to self::enqueue_block_css. + * + * @since 6.3.0 + */ + public static function output_block_styles() { + if ( ! empty( self::$block_css_declarations ) ) { + wp_style_engine_get_stylesheet_from_css_rules( + self::$block_css_declarations, + array( + 'context' => 'block-supports', + ) + ); + } + } + + /** + * Appends the used global style duotone filter presets (CSS custom + * properties) to the inline global styles CSS. + * + * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset. + * + * @since 6.3.0 + */ + public static function output_global_styles() { + if ( ! empty( self::$used_global_styles_presets ) ) { + wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) ); + } + } + + /** + * Outputs all necessary SVG for duotone filters, CSS for classic themes. + * + * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset + * and self::enqueue_custom_filter. + * + * @since 6.3.0 + */ + public static function output_footer_assets() { + if ( ! empty( self::$used_svg_filter_data ) ) { + echo self::get_svg_definitions( self::$used_svg_filter_data ); + } + + // This is for classic themes - in block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. + if ( ! wp_is_block_theme() && ! empty( self::$used_global_styles_presets ) ) { + wp_add_inline_style( 'core-block-supports', self::get_global_styles_presets( self::$used_global_styles_presets ) ); + } + } + + /** + * Adds the duotone SVGs and CSS custom properties to the editor settings. + * + * This allows the properties to be pulled in by the EditorStyles component + * in JS and rendered in the post editor. + * + * @since 6.3.0 + * + * @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. + */ + public static function add_editor_settings( $settings ) { + if ( ! empty( self::$global_styles_presets ) ) { + if ( ! isset( $settings['styles'] ) ) { + $settings['styles'] = array(); + } + + $settings['styles'][] = array( + // For the editor we can add all of the presets by default. + 'assets' => self::get_svg_definitions( self::$global_styles_presets ), + // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. + '__unstableType' => 'svgs', + // These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings. + 'isGlobalStyles' => false, + ); + + $settings['styles'][] = array( + // For the editor we can add all of the presets by default. + 'css' => self::get_global_styles_presets( self::$global_styles_presets ), + // This must be set and must be something other than 'theme' or they will be stripped out in the post editor component. + '__unstableType' => 'presets', + // 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, + ); + } + + return $settings; + } + + /** + * Migrates the experimental duotone support flag to the stabilized location. + * + * This moves `supports.color.__experimentalDuotone` to `supports.filter.duotone`. + * + * @since 6.3.0 + * + * @param array $settings Current block type settings. + * @param array $metadata Block metadata as read in via block.json. + * + * @return array Filtered block type settings. + */ + public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) { + $duotone_support = _wp_array_get( $metadata, array( 'supports', 'color', '__experimentalDuotone' ), null ); + + if ( ! isset( $settings['supports']['filter']['duotone'] ) && null !== $duotone_support ) { + _wp_array_set( $settings, array( 'supports', 'filter', 'duotone' ), (bool) $duotone_support ); + } + + return $settings; + } + + /** + * Gets the CSS filter property value from a preset. + * + * Exported for the deprecated function wp_get_duotone_filter_id(). + * + * @internal + * + * @since 6.3.0 + * @deprecated 6.3.0 + * + * @param array $preset The duotone preset. + * @return string The CSS filter property value. + */ + public static function get_filter_css_property_value_from_preset( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + if ( isset( $preset['colors'] ) && is_string( $preset['colors'] ) ) { + return $preset['colors']; + } + + $filter_id = self::get_filter_id_from_preset( $preset ); + + return 'url(#' . $filter_id . ')'; + } +} diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index c9517576a3..75665d271c 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -114,6 +114,7 @@ class WP_Theme_JSON { * @since 6.0.0 Replaced `override` with `prevent_override` and updated the * `prevent_override` value for `color.duotone` to use `color.defaultDuotone`. * @since 6.2.0 Added 'shadow' presets. + * @since 6.3.0 Replaced value_func for duotone with `null`. Custom properties are handled by class-wp-duotone.php. * @var array */ const PRESETS_METADATA = array( @@ -143,8 +144,8 @@ class WP_Theme_JSON { 'path' => array( 'color', 'duotone' ), 'prevent_override' => array( 'color', 'defaultDuotone' ), 'use_default_names' => false, - 'value_func' => 'wp_get_duotone_filter_property', - 'css_vars' => '--wp--preset--duotone--$slug', + 'value_func' => null, // CSS Custom Properties for duotone are handled by block supports in class-wp-duotone.php. + 'css_vars' => null, 'classes' => array(), 'properties' => array( 'filter' ), ), diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index 4ddaf291bb..44cf5bd705 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -606,10 +606,6 @@ add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' ); add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' ); add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 ); -// SVG filters like duotone have to be loaded at the beginning of the body in both admin and the front-end. -add_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' ); -add_action( 'in_admin_header', 'wp_global_styles_render_svg_filters' ); - add_action( 'wp_default_styles', 'wp_default_styles' ); add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 ); diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 1626995290..05630d6f2f 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -4804,3 +4804,501 @@ function wp_img_tag_add_loading_attr( $image, $context ) { return $image; } + +/** + * Takes input from [0, n] and returns it as [0, 1]. + * + * Direct port of TinyColor's function, lightly simplified to maintain + * consistency with TinyColor. + * + * @link https://github.com/bgrins/TinyColor + * + * @since 5.8.0 + * @deprecated 6.3.0 + * + * @access private + * + * @param mixed $n Number of unknown type. + * @param int $max Upper value of the range to bound to. + * @return float Value in the range [0, 1]. + */ +function wp_tinycolor_bound01( $n, $max ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) { + $n = '100%'; + } + + $n = min( $max, max( 0, (float) $n ) ); + + // Automatically convert percentage into number. + if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) { + $n = (int) ( $n * $max ) / 100; + } + + // Handle floating point rounding errors. + if ( ( abs( $n - $max ) < 0.000001 ) ) { + return 1.0; + } + + // Convert into [0, 1] range if it isn't already. + return ( $n % $max ) / (float) $max; +} + +/** + * Direct port of tinycolor's boundAlpha function to maintain consistency with + * how tinycolor works. + * + * @link https://github.com/bgrins/TinyColor + * + * @since 5.9.0 + * @deprecated 6.3.0 + * + * @access private + * + * @param mixed $n Number of unknown type. + * @return float Value in the range [0,1]. + */ +function _wp_tinycolor_bound_alpha( $n ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + if ( is_numeric( $n ) ) { + $n = (float) $n; + if ( $n >= 0 && $n <= 1 ) { + return $n; + } + } + return 1; +} + +/** + * Rounds and converts values of an RGB object. + * + * Direct port of TinyColor's function, lightly simplified to maintain + * consistency with TinyColor. + * + * @link https://github.com/bgrins/TinyColor + * + * @since 5.8.0 + * @deprecated 6.3.0 + * + * @access private + * + * @param array $rgb_color RGB object. + * @return array Rounded and converted RGB object. + */ +function wp_tinycolor_rgb_to_rgb( $rgb_color ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + return array( + 'r' => wp_tinycolor_bound01( $rgb_color['r'], 255 ) * 255, + 'g' => wp_tinycolor_bound01( $rgb_color['g'], 255 ) * 255, + 'b' => wp_tinycolor_bound01( $rgb_color['b'], 255 ) * 255, + ); +} + +/** + * Helper function for hsl to rgb conversion. + * + * Direct port of TinyColor's function, lightly simplified to maintain + * consistency with TinyColor. + * + * @link https://github.com/bgrins/TinyColor + * + * @since 5.8.0 + * @deprecated 6.3.0 + * + * @access private + * + * @param float $p first component. + * @param float $q second component. + * @param float $t third component. + * @return float R, G, or B component. + */ +function wp_tinycolor_hue_to_rgb( $p, $q, $t ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + if ( $t < 0 ) { + ++$t; + } + if ( $t > 1 ) { + --$t; + } + if ( $t < 1 / 6 ) { + return $p + ( $q - $p ) * 6 * $t; + } + if ( $t < 1 / 2 ) { + return $q; + } + if ( $t < 2 / 3 ) { + return $p + ( $q - $p ) * ( 2 / 3 - $t ) * 6; + } + return $p; +} + +/** + * Converts an HSL object to an RGB object with converted and rounded values. + * + * Direct port of TinyColor's function, lightly simplified to maintain + * consistency with TinyColor. + * + * @link https://github.com/bgrins/TinyColor + * + * @since 5.8.0 + * @deprecated 6.3.0 + * + * @access private + * + * @param array $hsl_color HSL object. + * @return array Rounded and converted RGB object. + */ +function wp_tinycolor_hsl_to_rgb( $hsl_color ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + $h = wp_tinycolor_bound01( $hsl_color['h'], 360 ); + $s = wp_tinycolor_bound01( $hsl_color['s'], 100 ); + $l = wp_tinycolor_bound01( $hsl_color['l'], 100 ); + + if ( 0 === $s ) { + // Achromatic. + $r = $l; + $g = $l; + $b = $l; + } else { + $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s; + $p = 2 * $l - $q; + $r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 ); + $g = wp_tinycolor_hue_to_rgb( $p, $q, $h ); + $b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 ); + } + + return array( + 'r' => $r * 255, + 'g' => $g * 255, + 'b' => $b * 255, + ); +} + +/** + * Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2 + * used in the JavaScript. Only colors output from react-color are implemented. + * + * Direct port of TinyColor's function, lightly simplified to maintain + * consistency with TinyColor. + * + * @link https://github.com/bgrins/TinyColor + * @link https://github.com/casesandberg/react-color/ + * + * @since 5.8.0 + * @since 5.9.0 Added alpha processing. + * @deprecated 6.3.0 + * + * @access private + * + * @param string $color_str CSS color string. + * @return array RGB object. + */ +function wp_tinycolor_string_to_rgb( $color_str ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + $color_str = strtolower( trim( $color_str ) ); + + $css_integer = '[-\\+]?\\d+%?'; + $css_number = '[-\\+]?\\d*\\.\\d+%?'; + + $css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')'; + + $permissive_match3 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?'; + $permissive_match4 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?'; + + $rgb_regexp = '/^rgb' . $permissive_match3 . '$/'; + if ( preg_match( $rgb_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => $match[1], + 'g' => $match[2], + 'b' => $match[3], + ) + ); + + $rgb['a'] = 1; + + return $rgb; + } + + $rgba_regexp = '/^rgba' . $permissive_match4 . '$/'; + if ( preg_match( $rgba_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => $match[1], + 'g' => $match[2], + 'b' => $match[3], + ) + ); + + $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] ); + + return $rgb; + } + + $hsl_regexp = '/^hsl' . $permissive_match3 . '$/'; + if ( preg_match( $hsl_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_hsl_to_rgb( + array( + 'h' => $match[1], + 's' => $match[2], + 'l' => $match[3], + ) + ); + + $rgb['a'] = 1; + + return $rgb; + } + + $hsla_regexp = '/^hsla' . $permissive_match4 . '$/'; + if ( preg_match( $hsla_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_hsl_to_rgb( + array( + 'h' => $match[1], + 's' => $match[2], + 'l' => $match[3], + ) + ); + + $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] ); + + return $rgb; + } + + $hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/'; + if ( preg_match( $hex8_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => base_convert( $match[1], 16, 10 ), + 'g' => base_convert( $match[2], 16, 10 ), + 'b' => base_convert( $match[3], 16, 10 ), + ) + ); + + $rgb['a'] = _wp_tinycolor_bound_alpha( + base_convert( $match[4], 16, 10 ) / 255 + ); + + return $rgb; + } + + $hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/'; + if ( preg_match( $hex6_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => base_convert( $match[1], 16, 10 ), + 'g' => base_convert( $match[2], 16, 10 ), + 'b' => base_convert( $match[3], 16, 10 ), + ) + ); + + $rgb['a'] = 1; + + return $rgb; + } + + $hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/'; + if ( preg_match( $hex4_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => base_convert( $match[1] . $match[1], 16, 10 ), + 'g' => base_convert( $match[2] . $match[2], 16, 10 ), + 'b' => base_convert( $match[3] . $match[3], 16, 10 ), + ) + ); + + $rgb['a'] = _wp_tinycolor_bound_alpha( + base_convert( $match[4] . $match[4], 16, 10 ) / 255 + ); + + return $rgb; + } + + $hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/'; + if ( preg_match( $hex3_regexp, $color_str, $match ) ) { + $rgb = wp_tinycolor_rgb_to_rgb( + array( + 'r' => base_convert( $match[1] . $match[1], 16, 10 ), + 'g' => base_convert( $match[2] . $match[2], 16, 10 ), + 'b' => base_convert( $match[3] . $match[3], 16, 10 ), + ) + ); + + $rgb['a'] = 1; + + return $rgb; + } + + /* + * The JS color picker considers the string "transparent" to be a hex value, + * so we need to handle it here as a special case. + */ + if ( 'transparent' === $color_str ) { + return array( + 'r' => 0, + 'g' => 0, + 'b' => 0, + 'a' => 0, + ); + } +} + +/** + * Returns the prefixed id for the duotone filter for use as a CSS id. + * + * @since 5.9.1 + * @deprecated 6.3.0 + * + * @access private + * + * @param array $preset Duotone preset value as seen in theme.json. + * @return string Duotone filter CSS id. + */ +function wp_get_duotone_filter_id( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + return WP_Duotone::get_filter_id_from_preset( $preset ); +} + +/** + * Returns the CSS filter property url to reference the rendered SVG. + * + * @since 5.9.0 + * @since 6.1.0 Allow unset for preset colors. + * @deprecated 6.3.0 + * + * @access private + * + * @param array $preset Duotone preset value as seen in theme.json. + * @return string Duotone CSS filter property url value. + */ +function wp_get_duotone_filter_property( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + return WP_Duotone::get_filter_css_property_value_from_preset( $preset ); +} + +/** + * Returns the duotone filter SVG string for the preset. + * + * @since 5.9.1 + * @deprecated 6.3.0 + * + * @access private + * + * @param array $preset Duotone preset value as seen in theme.json. + * @return string Duotone SVG filter. + */ +function wp_get_duotone_filter_svg( $preset ) { + _deprecated_function( __FUNCTION__, '6.3.0' ); + return WP_Duotone::get_filter_svg_from_preset( $preset ); +} + +/** + * Registers the style and colors block attributes for block types that support it. + * + * @since 5.8.0 + * @deprecated 6.3.0 Use WP_Duotone::register_duotone_support() instead. + * + * @access private + * + * @param WP_Block_Type $block_type Block Type. + */ +function wp_register_duotone_support( $block_type ) { + _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::register_duotone_support' ); + return WP_Duotone::register_duotone_support( $block_type ); +} + +/** + * Renders out the duotone stylesheet and SVG. + * + * @since 5.8.0 + * @since 6.1.0 Allow unset for preset colors. + * @deprecated 6.3.0 Use WP_Duotone::render_duotone_support() instead. + * + * @access private + * + * @param string $block_content Rendered block content. + * @param array $block Block object. + * @return string Filtered block content. + */ +function wp_render_duotone_support( $block_content, $block ) { + _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::render_duotone_support' ); + return WP_Duotone::render_duotone_support( $block_content, $block ); +} + +/** + * Returns a string containing the SVGs to be referenced as filters (duotone). + * + * @since 5.9.1 + * @deprecated 6.3.0 SVG generation is handled on a per-block basis in block supports. + * + * @return string + */ +function wp_get_global_styles_svg_filters() { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + /* + * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme + * developer's workflow. + */ + $can_use_cached = wp_get_development_mode() !== 'theme'; + $cache_group = 'theme_json'; + $cache_key = 'wp_get_global_styles_svg_filters'; + if ( $can_use_cached ) { + $cached = wp_cache_get( $cache_key, $cache_group ); + if ( $cached ) { + return $cached; + } + } + + $supports_theme_json = wp_theme_has_theme_json(); + + $origins = array( 'default', 'theme', 'custom' ); + if ( ! $supports_theme_json ) { + $origins = array( 'default' ); + } + + $tree = WP_Theme_JSON_Resolver::get_merged_data(); + $svgs = $tree->get_svg_filters( $origins ); + + if ( $can_use_cached ) { + wp_cache_set( $cache_key, $svgs, $cache_group ); + } + + return $svgs; +} + +/** + * Renders the SVG filters supplied by theme.json. + * + * Note that this doesn't render the per-block user-defined + * filters which are handled by wp_render_duotone_support, + * but it should be rendered before the filtered content + * in the body to satisfy Safari's rendering quirks. + * + * @since 5.9.1 + * @deprecated 6.3.0 SVG generation is handled on a per-block basis in block supports. + */ +function wp_global_styles_render_svg_filters() { + _deprecated_function( __FUNCTION__, '6.3.0' ); + + /* + * When calling via the in_admin_header action, we only want to render the + * SVGs on block editor pages. + */ + if ( + is_admin() && + ! get_current_screen()->is_block_editor() + ) { + return; + } + + $filters = wp_get_global_styles_svg_filters(); + if ( ! empty( $filters ) ) { + echo $filters; + } +} diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index a921feee07..c352f0d7c6 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/wp-includes/global-styles-and-settings.php @@ -288,45 +288,6 @@ function wp_get_global_styles_custom_css() { return $stylesheet; } -/** - * Returns a string containing the SVGs to be referenced as filters (duotone). - * - * @since 5.9.1 - * - * @return string - */ -function wp_get_global_styles_svg_filters() { - /* - * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme - * developer's workflow. - */ - $can_use_cached = wp_get_development_mode() !== 'theme'; - $cache_group = 'theme_json'; - $cache_key = 'wp_get_global_styles_svg_filters'; - if ( $can_use_cached ) { - $cached = wp_cache_get( $cache_key, $cache_group ); - if ( $cached ) { - return $cached; - } - } - - $supports_theme_json = wp_theme_has_theme_json(); - - $origins = array( 'default', 'theme', 'custom' ); - if ( ! $supports_theme_json ) { - $origins = array( 'default' ); - } - - $tree = WP_Theme_JSON_Resolver::get_merged_data(); - $svgs = $tree->get_svg_filters( $origins ); - - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $svgs, $cache_group ); - } - - return $svgs; -} - /** * Adds global style rules to the inline style for each block. * diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index b6414350b7..3de06605f1 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2479,34 +2479,6 @@ function wp_enqueue_global_styles_custom_css() { } } -/** - * Renders the SVG filters supplied by theme.json. - * - * Note that this doesn't render the per-block user-defined - * filters which are handled by wp_render_duotone_support, - * but it should be rendered before the filtered content - * in the body to satisfy Safari's rendering quirks. - * - * @since 5.9.1 - */ -function wp_global_styles_render_svg_filters() { - /* - * When calling via the in_admin_header action, we only want to render the - * SVGs on block editor pages. - */ - if ( - is_admin() && - ! get_current_screen()->is_block_editor() - ) { - return; - } - - $filters = wp_get_global_styles_svg_filters(); - if ( ! empty( $filters ) ) { - echo $filters; - } -} - /** * Checks if the editor scripts and styles for all registered block types * should be enqueued on the current screen. diff --git a/wp-includes/version.php b/wp-includes/version.php index b870fb2642..3589aa4737 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-beta2-56100'; +$wp_version = '6.3-beta2-56101'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-settings.php b/wp-settings.php index ac3365ab3f..08651606bc 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -175,6 +175,7 @@ require ABSPATH . WPINC . '/class-wp-theme-json-schema.php'; require ABSPATH . WPINC . '/class-wp-theme-json-data.php'; require ABSPATH . WPINC . '/class-wp-theme-json.php'; require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php'; +require ABSPATH . WPINC . '/class-wp-duotone.php'; require ABSPATH . WPINC . '/global-styles-and-settings.php'; require ABSPATH . WPINC . '/class-wp-block-template.php'; require ABSPATH . WPINC . '/block-template-utils.php';