Make WordPress Core


Ignore:
Timestamp:
06/29/2023 06:19:41 AM (17 months ago)
Author:
isabel_brison
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r56037 r56101  
    48054805    return $image;
    48064806}
     4807
     4808/**
     4809 * Takes input from [0, n] and returns it as [0, 1].
     4810 *
     4811 * Direct port of TinyColor's function, lightly simplified to maintain
     4812 * consistency with TinyColor.
     4813 *
     4814 * @link https://github.com/bgrins/TinyColor
     4815 *
     4816 * @since 5.8.0
     4817 * @deprecated 6.3.0
     4818 *
     4819 * @access private
     4820 *
     4821 * @param mixed $n   Number of unknown type.
     4822 * @param int   $max Upper value of the range to bound to.
     4823 * @return float Value in the range [0, 1].
     4824 */
     4825function wp_tinycolor_bound01( $n, $max ) {
     4826    _deprecated_function( __FUNCTION__, '6.3.0' );
     4827    if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) {
     4828        $n = '100%';
     4829    }
     4830
     4831    $n = min( $max, max( 0, (float) $n ) );
     4832
     4833    // Automatically convert percentage into number.
     4834    if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) {
     4835        $n = (int) ( $n * $max ) / 100;
     4836    }
     4837
     4838    // Handle floating point rounding errors.
     4839    if ( ( abs( $n - $max ) < 0.000001 ) ) {
     4840        return 1.0;
     4841    }
     4842
     4843    // Convert into [0, 1] range if it isn't already.
     4844    return ( $n % $max ) / (float) $max;
     4845}
     4846
     4847/**
     4848 * Direct port of tinycolor's boundAlpha function to maintain consistency with
     4849 * how tinycolor works.
     4850 *
     4851 * @link https://github.com/bgrins/TinyColor
     4852 *
     4853 * @since 5.9.0
     4854 * @deprecated 6.3.0
     4855 *
     4856 * @access private
     4857 *
     4858 * @param mixed $n Number of unknown type.
     4859 * @return float Value in the range [0,1].
     4860 */
     4861function _wp_tinycolor_bound_alpha( $n ) {
     4862    _deprecated_function( __FUNCTION__, '6.3.0' );
     4863
     4864    if ( is_numeric( $n ) ) {
     4865        $n = (float) $n;
     4866        if ( $n >= 0 && $n <= 1 ) {
     4867            return $n;
     4868        }
     4869    }
     4870    return 1;
     4871}
     4872
     4873/**
     4874 * Rounds and converts values of an RGB object.
     4875 *
     4876 * Direct port of TinyColor's function, lightly simplified to maintain
     4877 * consistency with TinyColor.
     4878 *
     4879 * @link https://github.com/bgrins/TinyColor
     4880 *
     4881 * @since 5.8.0
     4882 * @deprecated 6.3.0
     4883 *
     4884 * @access private
     4885 *
     4886 * @param array $rgb_color RGB object.
     4887 * @return array Rounded and converted RGB object.
     4888 */
     4889function wp_tinycolor_rgb_to_rgb( $rgb_color ) {
     4890    _deprecated_function( __FUNCTION__, '6.3.0' );
     4891
     4892    return array(
     4893        'r' => wp_tinycolor_bound01( $rgb_color['r'], 255 ) * 255,
     4894        'g' => wp_tinycolor_bound01( $rgb_color['g'], 255 ) * 255,
     4895        'b' => wp_tinycolor_bound01( $rgb_color['b'], 255 ) * 255,
     4896    );
     4897}
     4898
     4899/**
     4900 * Helper function for hsl to rgb conversion.
     4901 *
     4902 * Direct port of TinyColor's function, lightly simplified to maintain
     4903 * consistency with TinyColor.
     4904 *
     4905 * @link https://github.com/bgrins/TinyColor
     4906 *
     4907 * @since 5.8.0
     4908 * @deprecated 6.3.0
     4909 *
     4910 * @access private
     4911 *
     4912 * @param float $p first component.
     4913 * @param float $q second component.
     4914 * @param float $t third component.
     4915 * @return float R, G, or B component.
     4916 */
     4917function wp_tinycolor_hue_to_rgb( $p, $q, $t ) {
     4918    _deprecated_function( __FUNCTION__, '6.3.0' );
     4919
     4920    if ( $t < 0 ) {
     4921        ++$t;
     4922    }
     4923    if ( $t > 1 ) {
     4924        --$t;
     4925    }
     4926    if ( $t < 1 / 6 ) {
     4927        return $p + ( $q - $p ) * 6 * $t;
     4928    }
     4929    if ( $t < 1 / 2 ) {
     4930        return $q;
     4931    }
     4932    if ( $t < 2 / 3 ) {
     4933        return $p + ( $q - $p ) * ( 2 / 3 - $t ) * 6;
     4934    }
     4935    return $p;
     4936}
     4937
     4938/**
     4939 * Converts an HSL object to an RGB object with converted and rounded values.
     4940 *
     4941 * Direct port of TinyColor's function, lightly simplified to maintain
     4942 * consistency with TinyColor.
     4943 *
     4944 * @link https://github.com/bgrins/TinyColor
     4945 *
     4946 * @since 5.8.0
     4947 * @deprecated 6.3.0
     4948 *
     4949 * @access private
     4950 *
     4951 * @param array $hsl_color HSL object.
     4952 * @return array Rounded and converted RGB object.
     4953 */
     4954function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
     4955    _deprecated_function( __FUNCTION__, '6.3.0' );
     4956
     4957    $h = wp_tinycolor_bound01( $hsl_color['h'], 360 );
     4958    $s = wp_tinycolor_bound01( $hsl_color['s'], 100 );
     4959    $l = wp_tinycolor_bound01( $hsl_color['l'], 100 );
     4960
     4961    if ( 0 === $s ) {
     4962        // Achromatic.
     4963        $r = $l;
     4964        $g = $l;
     4965        $b = $l;
     4966    } else {
     4967        $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
     4968        $p = 2 * $l - $q;
     4969        $r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 );
     4970        $g = wp_tinycolor_hue_to_rgb( $p, $q, $h );
     4971        $b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 );
     4972    }
     4973
     4974    return array(
     4975        'r' => $r * 255,
     4976        'g' => $g * 255,
     4977        'b' => $b * 255,
     4978    );
     4979}
     4980
     4981/**
     4982 * Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2
     4983 * used in the JavaScript. Only colors output from react-color are implemented.
     4984 *
     4985 * Direct port of TinyColor's function, lightly simplified to maintain
     4986 * consistency with TinyColor.
     4987 *
     4988 * @link https://github.com/bgrins/TinyColor
     4989 * @link https://github.com/casesandberg/react-color/
     4990 *
     4991 * @since 5.8.0
     4992 * @since 5.9.0 Added alpha processing.
     4993 * @deprecated 6.3.0
     4994 *
     4995 * @access private
     4996 *
     4997 * @param string $color_str CSS color string.
     4998 * @return array RGB object.
     4999 */
     5000function wp_tinycolor_string_to_rgb( $color_str ) {
     5001    _deprecated_function( __FUNCTION__, '6.3.0' );
     5002
     5003    $color_str = strtolower( trim( $color_str ) );
     5004
     5005    $css_integer = '[-\\+]?\\d+%?';
     5006    $css_number  = '[-\\+]?\\d*\\.\\d+%?';
     5007
     5008    $css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')';
     5009
     5010    $permissive_match3 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
     5011    $permissive_match4 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
     5012
     5013    $rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
     5014    if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
     5015        $rgb = wp_tinycolor_rgb_to_rgb(
     5016            array(
     5017                'r' => $match[1],
     5018                'g' => $match[2],
     5019                'b' => $match[3],
     5020            )
     5021        );
     5022
     5023        $rgb['a'] = 1;
     5024
     5025        return $rgb;
     5026    }
     5027
     5028    $rgba_regexp = '/^rgba' . $permissive_match4 . '$/';
     5029    if ( preg_match( $rgba_regexp, $color_str, $match ) ) {
     5030        $rgb = wp_tinycolor_rgb_to_rgb(
     5031            array(
     5032                'r' => $match[1],
     5033                'g' => $match[2],
     5034                'b' => $match[3],
     5035            )
     5036        );
     5037
     5038        $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
     5039
     5040        return $rgb;
     5041    }
     5042
     5043    $hsl_regexp = '/^hsl' . $permissive_match3 . '$/';
     5044    if ( preg_match( $hsl_regexp, $color_str, $match ) ) {
     5045        $rgb = wp_tinycolor_hsl_to_rgb(
     5046            array(
     5047                'h' => $match[1],
     5048                's' => $match[2],
     5049                'l' => $match[3],
     5050            )
     5051        );
     5052
     5053        $rgb['a'] = 1;
     5054
     5055        return $rgb;
     5056    }
     5057
     5058    $hsla_regexp = '/^hsla' . $permissive_match4 . '$/';
     5059    if ( preg_match( $hsla_regexp, $color_str, $match ) ) {
     5060        $rgb = wp_tinycolor_hsl_to_rgb(
     5061            array(
     5062                'h' => $match[1],
     5063                's' => $match[2],
     5064                'l' => $match[3],
     5065            )
     5066        );
     5067
     5068        $rgb['a'] = _wp_tinycolor_bound_alpha( $match[4] );
     5069
     5070        return $rgb;
     5071    }
     5072
     5073    $hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
     5074    if ( preg_match( $hex8_regexp, $color_str, $match ) ) {
     5075        $rgb = wp_tinycolor_rgb_to_rgb(
     5076            array(
     5077                'r' => base_convert( $match[1], 16, 10 ),
     5078                'g' => base_convert( $match[2], 16, 10 ),
     5079                'b' => base_convert( $match[3], 16, 10 ),
     5080            )
     5081        );
     5082
     5083        $rgb['a'] = _wp_tinycolor_bound_alpha(
     5084            base_convert( $match[4], 16, 10 ) / 255
     5085        );
     5086
     5087        return $rgb;
     5088    }
     5089
     5090    $hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
     5091    if ( preg_match( $hex6_regexp, $color_str, $match ) ) {
     5092        $rgb = wp_tinycolor_rgb_to_rgb(
     5093            array(
     5094                'r' => base_convert( $match[1], 16, 10 ),
     5095                'g' => base_convert( $match[2], 16, 10 ),
     5096                'b' => base_convert( $match[3], 16, 10 ),
     5097            )
     5098        );
     5099
     5100        $rgb['a'] = 1;
     5101
     5102        return $rgb;
     5103    }
     5104
     5105    $hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
     5106    if ( preg_match( $hex4_regexp, $color_str, $match ) ) {
     5107        $rgb = wp_tinycolor_rgb_to_rgb(
     5108            array(
     5109                'r' => base_convert( $match[1] . $match[1], 16, 10 ),
     5110                'g' => base_convert( $match[2] . $match[2], 16, 10 ),
     5111                'b' => base_convert( $match[3] . $match[3], 16, 10 ),
     5112            )
     5113        );
     5114
     5115        $rgb['a'] = _wp_tinycolor_bound_alpha(
     5116            base_convert( $match[4] . $match[4], 16, 10 ) / 255
     5117        );
     5118
     5119        return $rgb;
     5120    }
     5121
     5122    $hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
     5123    if ( preg_match( $hex3_regexp, $color_str, $match ) ) {
     5124        $rgb = wp_tinycolor_rgb_to_rgb(
     5125            array(
     5126                'r' => base_convert( $match[1] . $match[1], 16, 10 ),
     5127                'g' => base_convert( $match[2] . $match[2], 16, 10 ),
     5128                'b' => base_convert( $match[3] . $match[3], 16, 10 ),
     5129            )
     5130        );
     5131
     5132        $rgb['a'] = 1;
     5133
     5134        return $rgb;
     5135    }
     5136
     5137    /*
     5138     * The JS color picker considers the string "transparent" to be a hex value,
     5139     * so we need to handle it here as a special case.
     5140     */
     5141    if ( 'transparent' === $color_str ) {
     5142        return array(
     5143            'r' => 0,
     5144            'g' => 0,
     5145            'b' => 0,
     5146            'a' => 0,
     5147        );
     5148    }
     5149}
     5150
     5151/**
     5152 * Returns the prefixed id for the duotone filter for use as a CSS id.
     5153 *
     5154 * @since 5.9.1
     5155 * @deprecated 6.3.0
     5156 *
     5157 * @access private
     5158 *
     5159 * @param array $preset Duotone preset value as seen in theme.json.
     5160 * @return string Duotone filter CSS id.
     5161 */
     5162function wp_get_duotone_filter_id( $preset ) {
     5163    _deprecated_function( __FUNCTION__, '6.3.0' );
     5164    return WP_Duotone::get_filter_id_from_preset( $preset );
     5165}
     5166
     5167/**
     5168 * Returns the CSS filter property url to reference the rendered SVG.
     5169 *
     5170 * @since 5.9.0
     5171 * @since 6.1.0 Allow unset for preset colors.
     5172 * @deprecated 6.3.0
     5173 *
     5174 * @access private
     5175 *
     5176 * @param array $preset Duotone preset value as seen in theme.json.
     5177 * @return string Duotone CSS filter property url value.
     5178 */
     5179function wp_get_duotone_filter_property( $preset ) {
     5180    _deprecated_function( __FUNCTION__, '6.3.0' );
     5181    return WP_Duotone::get_filter_css_property_value_from_preset( $preset );
     5182}
     5183
     5184/**
     5185 * Returns the duotone filter SVG string for the preset.
     5186 *
     5187 * @since 5.9.1
     5188 * @deprecated 6.3.0
     5189 *
     5190 * @access private
     5191 *
     5192 * @param array $preset Duotone preset value as seen in theme.json.
     5193 * @return string Duotone SVG filter.
     5194 */
     5195function wp_get_duotone_filter_svg( $preset ) {
     5196    _deprecated_function( __FUNCTION__, '6.3.0' );
     5197    return WP_Duotone::get_filter_svg_from_preset( $preset );
     5198}
     5199
     5200/**
     5201 * Registers the style and colors block attributes for block types that support it.
     5202 *
     5203 * @since 5.8.0
     5204 * @deprecated 6.3.0 Use WP_Duotone::register_duotone_support() instead.
     5205 *
     5206 * @access private
     5207 *
     5208 * @param WP_Block_Type $block_type Block Type.
     5209 */
     5210function wp_register_duotone_support( $block_type ) {
     5211    _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::register_duotone_support' );
     5212    return WP_Duotone::register_duotone_support( $block_type );
     5213}
     5214
     5215/**
     5216 * Renders out the duotone stylesheet and SVG.
     5217 *
     5218 * @since 5.8.0
     5219 * @since 6.1.0 Allow unset for preset colors.
     5220 * @deprecated 6.3.0 Use WP_Duotone::render_duotone_support() instead.
     5221 *
     5222 * @access private
     5223 *
     5224 * @param string $block_content Rendered block content.
     5225 * @param array  $block         Block object.
     5226 * @return string Filtered block content.
     5227 */
     5228function wp_render_duotone_support( $block_content, $block ) {
     5229    _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::render_duotone_support' );
     5230    return WP_Duotone::render_duotone_support( $block_content, $block );
     5231}
     5232
     5233/**
     5234 * Returns a string containing the SVGs to be referenced as filters (duotone).
     5235 *
     5236 * @since 5.9.1
     5237 * @deprecated 6.3.0 SVG generation is handled on a per-block basis in block supports.
     5238 *
     5239 * @return string
     5240 */
     5241function wp_get_global_styles_svg_filters() {
     5242    _deprecated_function( __FUNCTION__, '6.3.0' );
     5243
     5244    /*
     5245     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
     5246     * developer's workflow.
     5247     */
     5248    $can_use_cached = wp_get_development_mode() !== 'theme';
     5249    $cache_group    = 'theme_json';
     5250    $cache_key      = 'wp_get_global_styles_svg_filters';
     5251    if ( $can_use_cached ) {
     5252        $cached = wp_cache_get( $cache_key, $cache_group );
     5253        if ( $cached ) {
     5254            return $cached;
     5255        }
     5256    }
     5257
     5258    $supports_theme_json = wp_theme_has_theme_json();
     5259
     5260    $origins = array( 'default', 'theme', 'custom' );
     5261    if ( ! $supports_theme_json ) {
     5262        $origins = array( 'default' );
     5263    }
     5264
     5265    $tree = WP_Theme_JSON_Resolver::get_merged_data();
     5266    $svgs = $tree->get_svg_filters( $origins );
     5267
     5268    if ( $can_use_cached ) {
     5269        wp_cache_set( $cache_key, $svgs, $cache_group );
     5270    }
     5271
     5272    return $svgs;
     5273}
     5274
     5275/**
     5276 * Renders the SVG filters supplied by theme.json.
     5277 *
     5278 * Note that this doesn't render the per-block user-defined
     5279 * filters which are handled by wp_render_duotone_support,
     5280 * but it should be rendered before the filtered content
     5281 * in the body to satisfy Safari's rendering quirks.
     5282 *
     5283 * @since 5.9.1
     5284 * @deprecated 6.3.0 SVG generation is handled on a per-block basis in block supports.
     5285 */
     5286function wp_global_styles_render_svg_filters() {
     5287    _deprecated_function( __FUNCTION__, '6.3.0' );
     5288
     5289    /*
     5290     * When calling via the in_admin_header action, we only want to render the
     5291     * SVGs on block editor pages.
     5292     */
     5293    if (
     5294        is_admin() &&
     5295        ! get_current_screen()->is_block_editor()
     5296    ) {
     5297        return;
     5298    }
     5299
     5300    $filters = wp_get_global_styles_svg_filters();
     5301    if ( ! empty( $filters ) ) {
     5302        echo $filters;
     5303    }
     5304}
Note: See TracChangeset for help on using the changeset viewer.