[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-duotone.php (source)

   1  <?php
   2  /**
   3   * WP_Duotone class
   4   *
   5   * Parts of this source were derived and modified from colord,
   6   * released under the MIT license.
   7   *
   8   * https://github.com/omgovich/colord
   9   *
  10   * Copyright (c) 2020 Vlad Shilov omgovich@ya.ru
  11   *
  12   * Permission is hereby granted, free of charge, to any person obtaining
  13   * a copy of this software and associated documentation files (the
  14   * "Software"), to deal in the Software without restriction, including
  15   * without limitation the rights to use, copy, modify, merge, publish,
  16   * distribute, sublicense, and/or sell copies of the Software, and to
  17   * permit persons to whom the Software is furnished to do so, subject to
  18   * the following conditions:
  19   *
  20   * The above copyright notice and this permission notice shall be
  21   * included in all copies or substantial portions of the Software.
  22   *
  23   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28   * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30   *
  31   * @package WordPress
  32   * @since 6.3.0
  33   */
  34  
  35  /**
  36   * Manages duotone block supports and global styles.
  37   *
  38   * @access private
  39   */
  40  class WP_Duotone {
  41      /**
  42       * Block names from global, theme, and custom styles that use duotone presets and the slug of
  43       * the preset they are using.
  44       *
  45       * Example:
  46       *  [
  47       *      'core/featured-image' => 'blue-orange',
  48       *       …
  49       *  ]
  50       *
  51       * @internal
  52       *
  53       * @since 6.3.0
  54       *
  55       * @var ?array
  56       */
  57      private static $global_styles_block_names;
  58  
  59      /**
  60       * An array of duotone filter data from global, theme, and custom presets.
  61       *
  62       * Example:
  63       *  [
  64       *      'wp-duotone-blue-orange' => [
  65       *          'slug'  => 'blue-orange',
  66       *          'colors' => [ '#0000ff', '#ffcc00' ],
  67       *      ],
  68       *      'wp-duotone-red-yellow' => [
  69       *          'slug'   => 'red-yellow',
  70       *          'colors' => [ '#cc0000', '#ffff33' ],
  71       *      ],
  72       *      …
  73       *  ]
  74       *
  75       * @internal
  76       *
  77       * @since 6.3.0
  78       *
  79       * @var ?array
  80       */
  81      private static $global_styles_presets;
  82  
  83      /**
  84       * All of the duotone filter data from presets for CSS custom properties on
  85       * the page.
  86       *
  87       * Example:
  88       *  [
  89       *      'wp-duotone-blue-orange' => [
  90       *          'slug'   => 'blue-orange',
  91       *          'colors' => [ '#0000ff', '#ffcc00' ],
  92       *      ],
  93       *      …
  94       *  ]
  95       *
  96       * @internal
  97       *
  98       * @since 6.3.0
  99       *
 100       * @var array
 101       */
 102      private static $used_global_styles_presets = array();
 103  
 104      /**
 105       * All of the duotone filter data for SVGs on the page. Includes both
 106       * presets and custom filters.
 107       *
 108       * Example:
 109       *  [
 110       *      'wp-duotone-blue-orange' => [
 111       *          'slug'   => 'blue-orange',
 112       *          'colors' => [ '#0000ff', '#ffcc00' ],
 113       *      ],
 114       *      'wp-duotone-000000-ffffff-2' => [
 115       *          'slug'   => '000000-ffffff-2',
 116       *          'colors' => [ '#000000', '#ffffff' ],
 117       *      ],
 118       *      …
 119       *  ]
 120       *
 121       * @internal
 122       *
 123       * @since 6.3.0
 124       *
 125       * @var array
 126       */
 127      private static $used_svg_filter_data = array();
 128  
 129      /**
 130       * All of the block CSS declarations for styles on the page.
 131       *
 132       * Example:
 133       *  [
 134       *      [
 135       *          'selector'     => '.wp-duotone-000000-ffffff-2.wp-block-image img',
 136       *          'declarations' => [
 137       *              'filter' => 'url(#wp-duotone-000000-ffffff-2)',
 138       *          ],
 139       *      ],
 140       *      …
 141       *  ]
 142       *
 143       * @internal
 144       *
 145       * @since 6.3.0
 146       *
 147       * @var array
 148       */
 149      private static $block_css_declarations = array();
 150  
 151      /**
 152       * Clamps a value between an upper and lower bound.
 153       *
 154       * Direct port of colord's clamp function.
 155       *
 156       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L23 Sourced from colord.
 157       *
 158       * @internal
 159       *
 160       * @since 6.3.0
 161       *
 162       * @param float $number The number to clamp.
 163       * @param float $min    The minimum value.
 164       * @param float $max    The maximum value.
 165       * @return float The clamped value.
 166       */
 167  	private static function colord_clamp( $number, $min = 0, $max = 1 ) {
 168          return $number > $max ? $max : ( $number > $min ? $number : $min );
 169      }
 170  
 171      /**
 172       * Processes and clamps a degree (angle) value properly.
 173       *
 174       * Direct port of colord's clampHue function.
 175       *
 176       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L32 Sourced from colord.
 177       *
 178       * @internal
 179       *
 180       * @since 6.3.0
 181       *
 182       * @param float $degrees The hue to clamp.
 183       * @return float The clamped hue.
 184       */
 185  	private static function colord_clamp_hue( $degrees ) {
 186          $degrees = is_finite( $degrees ) ? $degrees % 360 : 0;
 187          return $degrees > 0 ? $degrees : $degrees + 360;
 188      }
 189  
 190      /**
 191       * Converts a hue value to degrees from 0 to 360 inclusive.
 192       *
 193       * Direct port of colord's parseHue function.
 194       *
 195       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L40 Sourced from colord.
 196       *
 197       * @internal
 198       *
 199       * @since 6.3.0
 200       *
 201       * @param float  $value The hue value to parse.
 202       * @param string $unit  The unit of the hue value.
 203       * @return float The parsed hue value.
 204       */
 205  	private static function colord_parse_hue( $value, $unit = 'deg' ) {
 206          $angle_units = array(
 207              'grad' => 360 / 400,
 208              'turn' => 360,
 209              'rad'  => 360 / ( M_PI * 2 ),
 210          );
 211  
 212          $factor = $angle_units[ $unit ] ?? 1;
 213  
 214          return (float) $value * $factor;
 215      }
 216  
 217      /**
 218       * Parses any valid Hex3, Hex4, Hex6 or Hex8 string and converts it to an RGBA object.
 219       *
 220       * Direct port of colord's parseHex function.
 221       *
 222       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hex.ts#L8 Sourced from colord.
 223       *
 224       * @internal
 225       *
 226       * @since 6.3.0
 227       *
 228       * @param string $hex The hex string to parse.
 229       * @return array|null An array of RGBA values or null if the hex string is invalid.
 230       */
 231  	private static function colord_parse_hex( $hex ) {
 232          $is_match = preg_match(
 233              '/^#([0-9a-f]{3,8})$/i',
 234              $hex,
 235              $hex_match
 236          );
 237  
 238          if ( ! $is_match ) {
 239              return null;
 240          }
 241  
 242          $hex = $hex_match[1];
 243  
 244          if ( 4 >= strlen( $hex ) ) {
 245              return array(
 246                  'r' => (int) base_convert( $hex[0] . $hex[0], 16, 10 ),
 247                  'g' => (int) base_convert( $hex[1] . $hex[1], 16, 10 ),
 248                  'b' => (int) base_convert( $hex[2] . $hex[2], 16, 10 ),
 249                  'a' => 4 === strlen( $hex ) ? round( (int) base_convert( $hex[3] . $hex[3], 16, 10 ) / 255, 2 ) : 1,
 250              );
 251          }
 252  
 253          if ( 6 === strlen( $hex ) || 8 === strlen( $hex ) ) {
 254              return array(
 255                  'r' => (int) base_convert( substr( $hex, 0, 2 ), 16, 10 ),
 256                  'g' => (int) base_convert( substr( $hex, 2, 2 ), 16, 10 ),
 257                  'b' => (int) base_convert( substr( $hex, 4, 2 ), 16, 10 ),
 258                  'a' => 8 === strlen( $hex ) ? round( (int) base_convert( substr( $hex, 6, 2 ), 16, 10 ) / 255, 2 ) : 1,
 259              );
 260          }
 261  
 262          return null;
 263      }
 264  
 265      /**
 266       * Clamps an array of RGBA values.
 267       *
 268       * Direct port of colord's clampRgba function.
 269       *
 270       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgb.ts#L5 Sourced from colord.
 271       *
 272       * @internal
 273       *
 274       * @since 6.3.0
 275       *
 276       * @param array $rgba The RGBA array to clamp.
 277       * @return array The clamped RGBA array.
 278       */
 279  	private static function colord_clamp_rgba( $rgba ) {
 280          $rgba['r'] = self::colord_clamp( $rgba['r'], 0, 255 );
 281          $rgba['g'] = self::colord_clamp( $rgba['g'], 0, 255 );
 282          $rgba['b'] = self::colord_clamp( $rgba['b'], 0, 255 );
 283          $rgba['a'] = self::colord_clamp( $rgba['a'] );
 284  
 285          return $rgba;
 286      }
 287  
 288      /**
 289       * Parses a valid RGB[A] CSS color function/string.
 290       *
 291       * Direct port of colord's parseRgbaString function.
 292       *
 293       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgbString.ts#L18 Sourced from colord.
 294       *
 295       * @internal
 296       *
 297       * @since 6.3.0
 298       *
 299       * @param string $input The RGBA string to parse.
 300       * @return array|null An array of RGBA values or null if the RGB string is invalid.
 301       */
 302  	private static function colord_parse_rgba_string( $input ) {
 303          // Functional syntax.
 304          $is_match = preg_match(
 305              '/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 306              $input,
 307              $match
 308          );
 309  
 310          if ( ! $is_match ) {
 311              // Whitespace syntax.
 312              $is_match = preg_match(
 313                  '/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 314                  $input,
 315                  $match
 316              );
 317          }
 318  
 319          if ( ! $is_match ) {
 320              return null;
 321          }
 322  
 323          /*
 324           * For some reason, preg_match doesn't include empty matches at the end
 325           * of the array, so we add them manually to make things easier later.
 326           */
 327          for ( $i = 1; $i <= 8; $i++ ) {
 328              if ( ! isset( $match[ $i ] ) ) {
 329                  $match[ $i ] = '';
 330              }
 331          }
 332  
 333          if ( $match[2] !== $match[4] || $match[4] !== $match[6] ) {
 334              return null;
 335          }
 336  
 337          return self::colord_clamp_rgba(
 338              array(
 339                  'r' => (float) $match[1] / ( $match[2] ? 100 / 255 : 1 ),
 340                  'g' => (float) $match[3] / ( $match[4] ? 100 / 255 : 1 ),
 341                  'b' => (float) $match[5] / ( $match[6] ? 100 / 255 : 1 ),
 342                  'a' => '' === $match[7] ? 1 : (float) $match[7] / ( $match[8] ? 100 : 1 ),
 343              )
 344          );
 345      }
 346  
 347      /**
 348       * Clamps an array of HSLA values.
 349       *
 350       * Direct port of colord's clampHsla function.
 351       *
 352       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L6 Sourced from colord.
 353       *
 354       * @internal
 355       *
 356       * @since 6.3.0
 357       *
 358       * @param array $hsla The HSLA array to clamp.
 359       * @return array The clamped HSLA array.
 360       */
 361  	private static function colord_clamp_hsla( $hsla ) {
 362          $hsla['h'] = self::colord_clamp_hue( $hsla['h'] );
 363          $hsla['s'] = self::colord_clamp( $hsla['s'], 0, 100 );
 364          $hsla['l'] = self::colord_clamp( $hsla['l'], 0, 100 );
 365          $hsla['a'] = self::colord_clamp( $hsla['a'] );
 366  
 367          return $hsla;
 368      }
 369  
 370      /**
 371       * Converts an HSVA array to RGBA.
 372       *
 373       * Direct port of colord's hsvaToRgba function.
 374       *
 375       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsv.ts#L52 Sourced from colord.
 376       *
 377       * @internal
 378       *
 379       * @since 6.3.0
 380       *
 381       * @param array $hsva The HSVA array to convert.
 382       * @return array The RGBA array.
 383       */
 384  	private static function colord_hsva_to_rgba( $hsva ) {
 385          $h = ( $hsva['h'] / 360 ) * 6;
 386          $s = $hsva['s'] / 100;
 387          $v = $hsva['v'] / 100;
 388          $a = $hsva['a'];
 389  
 390          $hh     = floor( $h );
 391          $b      = $v * ( 1 - $s );
 392          $c      = $v * ( 1 - ( $h - $hh ) * $s );
 393          $d      = $v * ( 1 - ( 1 - $h + $hh ) * $s );
 394          $module = $hh % 6;
 395  
 396          return array(
 397              'r' => array( $v, $c, $b, $b, $d, $v )[ $module ] * 255,
 398              'g' => array( $d, $v, $v, $c, $b, $b )[ $module ] * 255,
 399              'b' => array( $b, $b, $d, $v, $v, $c )[ $module ] * 255,
 400              'a' => $a,
 401          );
 402      }
 403  
 404      /**
 405       * Converts an HSLA array to HSVA.
 406       *
 407       * Direct port of colord's hslaToHsva function.
 408       *
 409       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L33 Sourced from colord.
 410       *
 411       * @internal
 412       *
 413       * @since 6.3.0
 414       *
 415       * @param array $hsla The HSLA array to convert.
 416       * @return array The HSVA array.
 417       */
 418  	private static function colord_hsla_to_hsva( $hsla ) {
 419          $h = $hsla['h'];
 420          $s = $hsla['s'];
 421          $l = $hsla['l'];
 422          $a = $hsla['a'];
 423  
 424          $s *= ( $l < 50 ? $l : 100 - $l ) / 100;
 425  
 426          return array(
 427              'h' => $h,
 428              's' => $s > 0 ? ( ( 2 * $s ) / ( $l + $s ) ) * 100 : 0,
 429              'v' => $l + $s,
 430              'a' => $a,
 431          );
 432      }
 433  
 434      /**
 435       * Converts an HSLA array to RGBA.
 436       *
 437       * Direct port of colord's hslaToRgba function.
 438       *
 439       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L55 Sourced from colord.
 440       *
 441       * @internal
 442       *
 443       * @since 6.3.0
 444       *
 445       * @param array $hsla The HSLA array to convert.
 446       * @return array The RGBA array.
 447       */
 448  	private static function colord_hsla_to_rgba( $hsla ) {
 449          return self::colord_hsva_to_rgba( self::colord_hsla_to_hsva( $hsla ) );
 450      }
 451  
 452      /**
 453       * Parses a valid HSL[A] CSS color function/string.
 454       *
 455       * Direct port of colord's parseHslaString function.
 456       *
 457       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hslString.ts#L17 Sourced from colord.
 458       *
 459       * @internal
 460       *
 461       * @since 6.3.0
 462       *
 463       * @param string $input The HSLA string to parse.
 464       * @return array|null An array of RGBA values or null if the RGB string is invalid.
 465       */
 466  	private static function colord_parse_hsla_string( $input ) {
 467          // Functional syntax.
 468          $is_match = preg_match(
 469              '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 470              $input,
 471              $match
 472          );
 473  
 474          if ( ! $is_match ) {
 475              // Whitespace syntax.
 476              $is_match = preg_match(
 477                  '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 478                  $input,
 479                  $match
 480              );
 481          }
 482  
 483          if ( ! $is_match ) {
 484              return null;
 485          }
 486  
 487          /*
 488           * For some reason, preg_match doesn't include empty matches at the end
 489           * of the array, so we add them manually to make things easier later.
 490           */
 491          for ( $i = 1; $i <= 6; $i++ ) {
 492              if ( ! isset( $match[ $i ] ) ) {
 493                  $match[ $i ] = '';
 494              }
 495          }
 496  
 497          $hsla = self::colord_clamp_hsla(
 498              array(
 499                  'h' => self::colord_parse_hue( $match[1], $match[2] ),
 500                  's' => (float) $match[3],
 501                  'l' => (float) $match[4],
 502                  'a' => '' === $match[5] ? 1 : (float) $match[5] / ( $match[6] ? 100 : 1 ),
 503              )
 504          );
 505  
 506          return self::colord_hsla_to_rgba( $hsla );
 507      }
 508  
 509      /**
 510       * Tries to convert an incoming string into RGBA values.
 511       *
 512       * Direct port of colord's parse function simplified for our use case. This
 513       * version only supports string parsing and only returns RGBA values.
 514       *
 515       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/parse.ts#L37 Sourced from colord.
 516       *
 517       * @internal
 518       *
 519       * @since 6.3.0
 520       *
 521       * @param string $input The string to parse.
 522       * @return array|null An array of RGBA values or null if the string is invalid.
 523       */
 524  	private static function colord_parse( $input ) {
 525          $result = self::colord_parse_hex( $input );
 526  
 527          if ( ! $result ) {
 528              $result = self::colord_parse_rgba_string( $input );
 529          }
 530  
 531          if ( ! $result ) {
 532              $result = self::colord_parse_hsla_string( $input );
 533          }
 534  
 535          return $result;
 536      }
 537  
 538      /**
 539       * Takes the inline CSS duotone variable from a block and return the slug.
 540       *
 541       * Handles styles slugs like:
 542       * var:preset|duotone|blue-orange
 543       * var(--wp--preset--duotone--blue-orange)
 544       *
 545       * @internal
 546       *
 547       * @since 6.3.0
 548       *
 549       * @param string|string[] $duotone_attr The duotone attribute from a block.
 550       * @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed).
 551       */
 552  	private static function get_slug_from_attribute( $duotone_attr ) {
 553          if ( ! is_string( $duotone_attr ) ) {
 554              return '';
 555          }
 556  
 557          // Uses Branch Reset Groups `(?|…)` to return one capture group.
 558          preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches );
 559  
 560          return ! empty( $matches[1] ) ? $matches[1] : '';
 561      }
 562  
 563      /**
 564       * Checks if we have a valid duotone preset.
 565       *
 566       * Valid presets are defined in the $global_styles_presets array.
 567       *
 568       * @internal
 569       *
 570       * @since 6.3.0
 571       *
 572       * @param string $duotone_attr The duotone attribute from a block.
 573       * @param string|string[] $duotone_attr The duotone attribute from a block.
 574       */
 575  	private static function is_preset( $duotone_attr ) {
 576          if ( ! is_string( $duotone_attr ) ) {
 577              return false;
 578          }
 579  
 580          $slug      = self::get_slug_from_attribute( $duotone_attr );
 581          $filter_id = self::get_filter_id( $slug );
 582  
 583          return array_key_exists( $filter_id, self::get_all_global_styles_presets() );
 584      }
 585  
 586      /**
 587       * Gets the CSS variable name for a duotone preset.
 588       *
 589       * Example output:
 590       *  --wp--preset--duotone--blue-orange
 591       *
 592       * @internal
 593       *
 594       * @since 6.3.0
 595       *
 596       * @param string $slug The slug of the duotone preset.
 597       * @return string The CSS variable name.
 598       */
 599  	private static function get_css_custom_property_name( $slug ) {
 600          return "--wp--preset--duotone--$slug";
 601      }
 602  
 603      /**
 604       * Get the ID of the duotone filter.
 605       *
 606       * Example output:
 607       *  wp-duotone-blue-orange
 608       *
 609       * @internal
 610       *
 611       * @since 6.3.0
 612       *
 613       * @param string $slug The slug of the duotone preset.
 614       * @return string The ID of the duotone filter.
 615       */
 616  	private static function get_filter_id( $slug ) {
 617          return "wp-duotone-$slug";
 618      }
 619  
 620      /**
 621       * Get the CSS variable for a duotone preset.
 622       *
 623       * Example output:
 624       *  var(--wp--preset--duotone--blue-orange)
 625       *
 626       * @internal
 627       *
 628       * @since 6.3.0
 629       *
 630       * @param string $slug The slug of the duotone preset.
 631       * @return string The CSS variable.
 632       */
 633  	private static function get_css_var( $slug ) {
 634          $name = self::get_css_custom_property_name( $slug );
 635          return "var($name)";
 636      }
 637  
 638      /**
 639       * Get the URL for a duotone filter.
 640       *
 641       * Example output:
 642       *  url(#wp-duotone-blue-orange)
 643       *
 644       * @internal
 645       *
 646       * @since 6.3.0
 647       *
 648       * @param string $filter_id The ID of the filter.
 649       * @return string The URL for the duotone filter.
 650       */
 651  	private static function get_filter_url( $filter_id ) {
 652          return "url(#$filter_id)";
 653      }
 654  
 655      /**
 656       * Gets the SVG for the duotone filter definition.
 657       *
 658       * Whitespace is removed when SCRIPT_DEBUG is not enabled.
 659       *
 660       * @internal
 661       *
 662       * @since 6.3.0
 663       *
 664       * @param string $filter_id The ID of the filter.
 665       * @param array  $colors    An array of color strings.
 666       * @return string An SVG with a duotone filter definition.
 667       */
 668  	private static function get_filter_svg( $filter_id, $colors ) {
 669          $duotone_values = array(
 670              'r' => array(),
 671              'g' => array(),
 672              'b' => array(),
 673              'a' => array(),
 674          );
 675  
 676          foreach ( $colors as $color_str ) {
 677              $color = self::colord_parse( $color_str );
 678  
 679              if ( null === $color ) {
 680                  $error_message = sprintf(
 681                      /* translators: 1: Duotone colors, 2: theme.json, 3: settings.color.duotone */
 682                      __( '"%1$s" in %2$s %3$s is not a hex or rgb string.' ),
 683                      $color_str,
 684                      'theme.json',
 685                      'settings.color.duotone'
 686                  );
 687                  _doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
 688              } else {
 689                  $duotone_values['r'][] = $color['r'] / 255;
 690                  $duotone_values['g'][] = $color['g'] / 255;
 691                  $duotone_values['b'][] = $color['b'] / 255;
 692                  $duotone_values['a'][] = $color['a'];
 693              }
 694          }
 695  
 696          ob_start();
 697  
 698          ?>
 699  
 700          <svg
 701              xmlns="http://www.w3.org/2000/svg"
 702              viewBox="0 0 0 0"
 703              width="0"
 704              height="0"
 705              focusable="false"
 706              role="none"
 707              style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
 708          >
 709              <defs>
 710                  <filter id="<?php echo esc_attr( $filter_id ); ?>">
 711                      <feColorMatrix
 712                          color-interpolation-filters="sRGB"
 713                          type="matrix"
 714                          values="
 715                              .299 .587 .114 0 0
 716                              .299 .587 .114 0 0
 717                              .299 .587 .114 0 0
 718                              .299 .587 .114 0 0
 719                          "
 720                      />
 721                      <feComponentTransfer color-interpolation-filters="sRGB" >
 722                          <feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
 723                          <feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
 724                          <feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
 725                          <feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" />
 726                      </feComponentTransfer>
 727                      <feComposite in2="SourceGraphic" operator="in" />
 728                  </filter>
 729              </defs>
 730          </svg>
 731  
 732          <?php
 733  
 734          $svg = ob_get_clean();
 735  
 736          if ( ! SCRIPT_DEBUG ) {
 737              // Clean up the whitespace.
 738              $svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg );
 739              $svg = str_replace( '> <', '><', $svg );
 740              $svg = trim( $svg );
 741          }
 742  
 743          return $svg;
 744      }
 745  
 746      /**
 747       * Returns the prefixed id for the duotone filter for use as a CSS id.
 748       *
 749       * Exported for the deprecated function wp_get_duotone_filter_id().
 750       *
 751       * @internal
 752       *
 753       * @since 6.3.0
 754       * @deprecated 6.3.0
 755       *
 756       * @param  array $preset Duotone preset value as seen in theme.json.
 757       * @return string        Duotone filter CSS id.
 758       */
 759  	public static function get_filter_id_from_preset( $preset ) {
 760          _deprecated_function( __FUNCTION__, '6.3.0' );
 761  
 762          $filter_id = '';
 763          if ( isset( $preset['slug'] ) ) {
 764              $filter_id = self::get_filter_id( $preset['slug'] );
 765          }
 766          return $filter_id;
 767      }
 768  
 769      /**
 770       * Gets the SVG for the duotone filter definition from a preset.
 771       *
 772       * Exported for the deprecated function wp_get_duotone_filter_property().
 773       *
 774       * @internal
 775       *
 776       * @since 6.3.0
 777       * @deprecated 6.3.0
 778       *
 779       * @param array $preset The duotone preset.
 780       * @return string The SVG for the filter definition.
 781       */
 782  	public static function get_filter_svg_from_preset( $preset ) {
 783          _deprecated_function( __FUNCTION__, '6.3.0' );
 784  
 785          $filter_id = self::get_filter_id_from_preset( $preset );
 786          return self::get_filter_svg( $filter_id, $preset['colors'] );
 787      }
 788  
 789      /**
 790       * Get the SVGs for the duotone filters.
 791       *
 792       * Example output:
 793       *  <svg><defs><filter id="wp-duotone-blue-orange">…</filter></defs></svg><svg>…</svg>
 794       *
 795       * @internal
 796       *
 797       * @since 6.3.0
 798       *
 799       * @param array $sources The duotone presets.
 800       * @return string The SVGs for the duotone filters.
 801       */
 802  	private static function get_svg_definitions( $sources ) {
 803          $svgs = '';
 804          foreach ( $sources as $filter_id => $filter_data ) {
 805              $colors = $filter_data['colors'];
 806              $svgs  .= self::get_filter_svg( $filter_id, $colors );
 807          }
 808          return $svgs;
 809      }
 810  
 811      /**
 812       * Get the CSS for global styles.
 813       *
 814       * Example output:
 815       *  body{--wp--preset--duotone--blue-orange:url('#wp-duotone-blue-orange');}
 816       *
 817       * @internal
 818       *
 819       * @since 6.3.0
 820       * @since 6.6.0 Replaced body selector with `WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR`.
 821       *
 822       * @param array $sources The duotone presets.
 823       * @return string The CSS for global styles.
 824       */
 825  	private static function get_global_styles_presets( $sources ) {
 826          $css = WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR . '{';
 827          foreach ( $sources as $filter_id => $filter_data ) {
 828              $slug              = $filter_data['slug'];
 829              $colors            = $filter_data['colors'];
 830              $css_property_name = self::get_css_custom_property_name( $slug );
 831              $declaration_value = is_string( $colors ) ? $colors : self::get_filter_url( $filter_id );
 832              $css              .= "$css_property_name:$declaration_value;";
 833          }
 834          $css .= '}';
 835          return $css;
 836      }
 837  
 838      /**
 839       * Enqueue a block CSS declaration for the page.
 840       *
 841       * This does not include any SVGs.
 842       *
 843       * @internal
 844       *
 845       * @since 6.3.0
 846       *
 847       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-000000-ffffff-2'.
 848       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 849       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'.
 850       */
 851  	private static function enqueue_block_css( $filter_id, $duotone_selector, $filter_value ) {
 852          // Build the CSS selectors to which the filter will be applied.
 853          $selectors = explode( ',', $duotone_selector );
 854  
 855          $selectors_scoped = array();
 856          foreach ( $selectors as $selector_part ) {
 857              /*
 858               * Assuming the selector part is a subclass selector (not a tag name)
 859               * so we can prepend the filter id class. If we want to support elements
 860               * such as `img` or namespaces, we'll need to add a case for that here.
 861               */
 862              $selectors_scoped[] = '.' . $filter_id . trim( $selector_part );
 863          }
 864  
 865          $selector = implode( ', ', $selectors_scoped );
 866  
 867          self::$block_css_declarations[] = array(
 868              'selector'     => $selector,
 869              'declarations' => array(
 870                  'filter' => $filter_value,
 871              ),
 872          );
 873      }
 874  
 875      /**
 876       * Enqueue custom filter assets for the page.
 877       *
 878       * Includes an SVG filter and block CSS declaration.
 879       *
 880       * @internal
 881       *
 882       * @since 6.3.0
 883       *
 884       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-000000-ffffff-2'.
 885       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 886       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'.
 887       * @param array  $filter_data      Duotone filter data with 'slug' and 'colors' keys.
 888       */
 889  	private static function enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ) {
 890          self::$used_svg_filter_data[ $filter_id ] = $filter_data;
 891          self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value );
 892      }
 893  
 894      /**
 895       * Enqueue preset assets for the page.
 896       *
 897       * Includes a CSS custom property, SVG filter, and block CSS declaration.
 898       *
 899       * @internal
 900       *
 901       * @since 6.3.0
 902       *
 903       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-blue-orange'.
 904       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 905       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'.
 906       */
 907  	private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) {
 908          $global_styles_presets = self::get_all_global_styles_presets();
 909          if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) {
 910              $error_message = sprintf(
 911                  /* translators: 1: Duotone filter ID, 2: theme.json */
 912                  __( 'The duotone id "%1$s" is not registered in %2$s settings' ),
 913                  $filter_id,
 914                  'theme.json'
 915              );
 916              _doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
 917              return;
 918          }
 919          self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ];
 920          self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] );
 921      }
 922  
 923      /**
 924       * Registers the style and colors block attributes for block types that support it.
 925       *
 926       * Block support is added with `supports.filter.duotone` in block.json.
 927       *
 928       * @since 6.3.0
 929       *
 930       * @param WP_Block_Type $block_type Block Type.
 931       */
 932  	public static function register_duotone_support( $block_type ) {
 933          /*
 934           * Previous `color.__experimentalDuotone` support flag is migrated
 935           * to `filter.duotone` via `block_type_metadata_settings` filter.
 936           */
 937          if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) {
 938              if ( ! $block_type->attributes ) {
 939                  $block_type->attributes = array();
 940              }
 941  
 942              if ( ! array_key_exists( 'style', $block_type->attributes ) ) {
 943                  $block_type->attributes['style'] = array(
 944                      'type' => 'object',
 945                  );
 946              }
 947          }
 948      }
 949  
 950      /**
 951       * Get the CSS selector for a block type.
 952       *
 953       * This handles selectors defined in `color.__experimentalDuotone` support
 954       * if `filter.duotone` support is not defined.
 955       *
 956       * @internal
 957       * @since 6.3.0
 958       *
 959       * @param WP_Block_Type $block_type Block type to check for support.
 960       * @return string|null The CSS selector or null if there is no support.
 961       */
 962  	private static function get_selector( $block_type ) {
 963          if ( ! ( $block_type instanceof WP_Block_Type ) ) {
 964              return null;
 965          }
 966  
 967          /*
 968           * Backward compatibility with `supports.color.__experimentalDuotone`
 969           * is provided via the `block_type_metadata_settings` filter. If
 970           * `supports.filter.duotone` has not been set and the experimental
 971           * property has been, the experimental property value is copied into
 972           * `supports.filter.duotone`.
 973           */
 974          $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) );
 975          if ( ! $duotone_support ) {
 976              return null;
 977          }
 978  
 979          /*
 980           * If the experimental duotone support was set, that value is to be
 981           * treated as a selector and requires scoping.
 982           */
 983          $experimental_duotone = $block_type->supports['color']['__experimentalDuotone'] ?? false;
 984          if ( $experimental_duotone ) {
 985              $root_selector = wp_get_block_css_selector( $block_type );
 986              return is_string( $experimental_duotone )
 987                  ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone )
 988                  : $root_selector;
 989          }
 990  
 991          // Regular filter.duotone support uses filter.duotone selectors with fallbacks.
 992          return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true );
 993      }
 994  
 995      /**
 996       * Scrape all possible duotone presets from global and theme styles and
 997       * store them in self::$global_styles_presets.
 998       *
 999       * Used in conjunction with self::render_duotone_support for blocks that
1000       * use duotone preset filters.
1001       *
1002       * @since 6.3.0
1003       *
1004       * @return array An array of global styles presets, keyed on the filter ID.
1005       */
1006  	private static function get_all_global_styles_presets() {
1007          if ( isset( self::$global_styles_presets ) ) {
1008              return self::$global_styles_presets;
1009          }
1010          // Get the per block settings from the theme.json.
1011          $tree              = wp_get_global_settings();
1012          $presets_by_origin = $tree['color']['duotone'] ?? array();
1013  
1014          self::$global_styles_presets = array();
1015          foreach ( $presets_by_origin as $presets ) {
1016              foreach ( $presets as $preset ) {
1017                  $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) );
1018  
1019                  self::$global_styles_presets[ $filter_id ] = $preset;
1020              }
1021          }
1022  
1023          return self::$global_styles_presets;
1024      }
1025  
1026      /**
1027       * Scrape all block names from global styles and store in self::$global_styles_block_names.
1028       *
1029       * Used in conjunction with self::render_duotone_support to output the
1030       * duotone filters defined in the theme.json global styles.
1031       *
1032       * @since 6.3.0
1033       *
1034       * @return string[] An array of global style block slugs, keyed on the block name.
1035       */
1036  	private static function get_all_global_style_block_names() {
1037          if ( isset( self::$global_styles_block_names ) ) {
1038              return self::$global_styles_block_names;
1039          }
1040          // Get the per block settings from the theme.json.
1041          $tree        = WP_Theme_JSON_Resolver::get_merged_data();
1042          $block_nodes = $tree->get_styles_block_nodes();
1043          $theme_json  = $tree->get_raw_data();
1044  
1045          self::$global_styles_block_names = array();
1046  
1047          foreach ( $block_nodes as $block_node ) {
1048              // This block definition doesn't include any duotone settings. Skip it.
1049              if ( empty( $block_node['duotone'] ) ) {
1050                  continue;
1051              }
1052  
1053              // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'.
1054              $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) );
1055              $duotone_attr      = _wp_array_get( $theme_json, $duotone_attr_path, array() );
1056  
1057              if ( empty( $duotone_attr ) ) {
1058                  continue;
1059              }
1060              // If it has a duotone filter preset, save the block name and the preset slug.
1061              // Only process if it's a string (preset reference), not an array (custom colors).
1062              if ( ! is_string( $duotone_attr ) ) {
1063                  continue;
1064              }
1065  
1066              $slug = self::get_slug_from_attribute( $duotone_attr );
1067  
1068              if ( $slug && $slug !== $duotone_attr ) {
1069                  self::$global_styles_block_names[ $block_node['name'] ] = $slug;
1070              }
1071          }
1072          return self::$global_styles_block_names;
1073      }
1074  
1075      /**
1076       * Render out the duotone CSS styles and SVG.
1077       *
1078       * The hooks self::set_global_style_block_names and self::set_global_styles_presets
1079       * must be called before this function.
1080       *
1081       * @since 6.3.0
1082       *
1083       * @param  string   $block_content Rendered block content.
1084       * @param  array    $block         Block object.
1085       * @param  WP_Block $wp_block      The block instance.
1086       * @return string Filtered block content.
1087       */
1088  	public static function render_duotone_support( $block_content, $block, $wp_block ) {
1089          if ( ! $block['blockName'] ) {
1090              return $block_content;
1091          }
1092          $duotone_selector = self::get_selector( $wp_block->block_type );
1093  
1094          if ( ! $duotone_selector ) {
1095              return $block_content;
1096          }
1097  
1098          $global_styles_block_names = self::get_all_global_style_block_names();
1099  
1100          // The block should have a duotone attribute or have duotone defined in its theme.json to be processed.
1101          $has_duotone_attribute     = isset( $block['attrs']['style']['color']['duotone'] );
1102          $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names );
1103  
1104          if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) {
1105              return $block_content;
1106          }
1107  
1108          // Generate the pieces needed for rendering a duotone to the page.
1109          if ( $has_duotone_attribute ) {
1110  
1111              /*
1112               * Possible values for duotone attribute:
1113               * 1. Array of colors - e.g. array('#000000', '#ffffff').
1114               * 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|blue-orange' or 'var(--wp--preset--duotone--blue-orange)''
1115               * 3. A CSS string - e.g. 'unset' to remove globally applied duotone.
1116               */
1117  
1118              $duotone_attr = $block['attrs']['style']['color']['duotone'];
1119              $is_preset    = is_string( $duotone_attr ) && self::is_preset( $duotone_attr );
1120              $is_css       = is_string( $duotone_attr ) && ! $is_preset;
1121              $is_custom    = is_array( $duotone_attr );
1122  
1123              if ( $is_preset ) {
1124  
1125                  $slug         = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'blue-orange'.
1126                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
1127                  $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
1128  
1129                  // CSS custom property, SVG filter, and block CSS.
1130                  self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value );
1131  
1132              } elseif ( $is_css ) {
1133                  $slug         = wp_unique_id( sanitize_key( $duotone_attr . '-' ) ); // e.g. 'unset-1'.
1134                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-unset-1'.
1135                  $filter_value = $duotone_attr; // e.g. 'unset'.
1136  
1137                  // Just block CSS.
1138                  self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value );
1139              } elseif ( $is_custom ) {
1140                  $slug         = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); // e.g. '000000-ffffff-2'.
1141                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-000000-ffffff-2'.
1142                  $filter_value = self::get_filter_url( $filter_id ); // e.g. 'url(#wp-duotone-filter-000000-ffffff-2)'.
1143                  $filter_data  = array(
1144                      'slug'   => $slug,
1145                      'colors' => $duotone_attr,
1146                  );
1147  
1148                  // SVG filter and block CSS.
1149                  self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data );
1150              }
1151          } elseif ( $has_global_styles_duotone ) {
1152              $slug         = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
1153              $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
1154              $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
1155  
1156              // CSS custom property, SVG filter, and block CSS.
1157              self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value );
1158          }
1159  
1160          // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper.
1161          $tags = new WP_HTML_Tag_Processor( $block_content );
1162          if ( $tags->next_tag() ) {
1163              $tags->add_class( $filter_id );
1164          }
1165          return $tags->get_updated_html();
1166      }
1167  
1168      /**
1169       * Fixes the issue with our generated class name not being added to the block's outer container
1170       * in classic themes due to gutenberg_restore_image_outer_container from layout block supports.
1171       *
1172       * @since 6.6.0
1173       *
1174       * @param string $block_content Rendered block content.
1175       * @return string Filtered block content.
1176       */
1177  	public static function restore_image_outer_container( $block_content ) {
1178          if ( wp_theme_has_theme_json() ) {
1179              return $block_content;
1180          }
1181  
1182          $tags          = new WP_HTML_Tag_Processor( $block_content );
1183          $wrapper_query = array(
1184              'tag_name'   => 'div',
1185              'class_name' => 'wp-block-image',
1186          );
1187          if ( ! $tags->next_tag( $wrapper_query ) ) {
1188              return $block_content;
1189          }
1190  
1191          $tags->set_bookmark( 'wrapper-div' );
1192          $tags->next_tag();
1193  
1194          $inner_classnames = explode( ' ', $tags->get_attribute( 'class' ) );
1195          foreach ( $inner_classnames as $classname ) {
1196              if ( str_starts_with( $classname, 'wp-duotone' ) ) {
1197                  $tags->remove_class( $classname );
1198                  $tags->seek( 'wrapper-div' );
1199                  $tags->add_class( $classname );
1200                  break;
1201              }
1202          }
1203  
1204          return $tags->get_updated_html();
1205      }
1206  
1207      /**
1208       * Appends the used block duotone filter declarations to the inline block supports CSS.
1209       *
1210       * Uses the declarations saved in earlier calls to self::enqueue_block_css.
1211       *
1212       * @since 6.3.0
1213       */
1214  	public static function output_block_styles() {
1215          if ( ! empty( self::$block_css_declarations ) ) {
1216              wp_style_engine_get_stylesheet_from_css_rules(
1217                  self::$block_css_declarations,
1218                  array(
1219                      'context' => 'block-supports',
1220                  )
1221              );
1222          }
1223      }
1224  
1225      /**
1226       * Appends the used global style duotone filter presets (CSS custom
1227       * properties) to the inline global styles CSS.
1228       *
1229       * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset.
1230       *
1231       * @since 6.3.0
1232       */
1233  	public static function output_global_styles() {
1234          if ( ! empty( self::$used_global_styles_presets ) ) {
1235              wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) );
1236          }
1237      }
1238  
1239      /**
1240       * Outputs all necessary SVG for duotone filters, CSS for classic themes.
1241       *
1242       * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset
1243       * and self::enqueue_custom_filter.
1244       *
1245       * @since 6.3.0
1246       */
1247  	public static function output_footer_assets() {
1248          if ( ! empty( self::$used_svg_filter_data ) ) {
1249              echo self::get_svg_definitions( self::$used_svg_filter_data );
1250          }
1251  
1252          // In block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action.
1253          if ( ! wp_is_block_theme() ) {
1254              $style_tag_id = 'core-block-supports-duotone';
1255              wp_register_style( $style_tag_id, false );
1256              if ( ! empty( self::$used_global_styles_presets ) ) {
1257                  wp_add_inline_style( $style_tag_id, self::get_global_styles_presets( self::$used_global_styles_presets ) );
1258              }
1259              if ( ! empty( self::$block_css_declarations ) ) {
1260                  wp_add_inline_style( $style_tag_id, wp_style_engine_get_stylesheet_from_css_rules( self::$block_css_declarations ) );
1261              }
1262              wp_enqueue_style( $style_tag_id );
1263          }
1264      }
1265  
1266      /**
1267       * Adds the duotone SVGs and CSS custom properties to the editor settings.
1268       *
1269       * This allows the properties to be pulled in by the EditorStyles component
1270       * in JS and rendered in the post editor.
1271       *
1272       * @since 6.3.0
1273       *
1274       * @param array $settings The block editor settings from the `block_editor_settings_all` filter.
1275       * @return array The editor settings with duotone SVGs and CSS custom properties.
1276       */
1277  	public static function add_editor_settings( $settings ) {
1278          $global_styles_presets = self::get_all_global_styles_presets();
1279          if ( ! empty( $global_styles_presets ) ) {
1280              if ( ! isset( $settings['styles'] ) ) {
1281                  $settings['styles'] = array();
1282              }
1283  
1284              $settings['styles'][] = array(
1285                  // For the editor we can add all of the presets by default.
1286                  'assets'         => self::get_svg_definitions( $global_styles_presets ),
1287                  // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work.
1288                  '__unstableType' => 'svgs',
1289                  // These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings.
1290                  'isGlobalStyles' => false,
1291              );
1292  
1293              $settings['styles'][] = array(
1294                  // For the editor we can add all of the presets by default.
1295                  'css'            => self::get_global_styles_presets( $global_styles_presets ),
1296                  // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component.
1297                  '__unstableType' => 'presets',
1298                  // 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.
1299                  'isGlobalStyles' => false,
1300              );
1301          }
1302  
1303          return $settings;
1304      }
1305  
1306      /**
1307       * Migrates the experimental duotone support flag to the stabilized location.
1308       *
1309       * This moves `supports.color.__experimentalDuotone` to `supports.filter.duotone`.
1310       *
1311       * @since 6.3.0
1312       *
1313       * @param array $settings Current block type settings.
1314       * @param array $metadata Block metadata as read in via block.json.
1315       * @return array Filtered block type settings.
1316       */
1317  	public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) {
1318          $duotone_support = $metadata['supports']['color']['__experimentalDuotone'] ?? null;
1319  
1320          if ( ! isset( $settings['supports']['filter']['duotone'] ) && null !== $duotone_support ) {
1321              _wp_array_set( $settings, array( 'supports', 'filter', 'duotone' ), (bool) $duotone_support );
1322          }
1323  
1324          return $settings;
1325      }
1326  
1327      /**
1328       * Gets the CSS filter property value from a preset.
1329       *
1330       * Exported for the deprecated function wp_get_duotone_filter_id().
1331       *
1332       * @internal
1333       *
1334       * @since 6.3.0
1335       * @deprecated 6.3.0
1336       *
1337       * @param array $preset The duotone preset.
1338       * @return string The CSS filter property value.
1339       */
1340  	public static function get_filter_css_property_value_from_preset( $preset ) {
1341          _deprecated_function( __FUNCTION__, '6.3.0' );
1342  
1343          if ( isset( $preset['colors'] ) && is_string( $preset['colors'] ) ) {
1344              return $preset['colors'];
1345          }
1346  
1347          $filter_id = self::get_filter_id_from_preset( $preset );
1348  
1349          return 'url(#' . $filter_id . ')';
1350      }
1351  }


Generated : Thu Jun 18 08:20:10 2026 Cross-referenced by PHPXref