[ 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              $match[ $i ] ??= '';
 329          }
 330  
 331          if ( $match[2] !== $match[4] || $match[4] !== $match[6] ) {
 332              return null;
 333          }
 334  
 335          return self::colord_clamp_rgba(
 336              array(
 337                  'r' => (float) $match[1] / ( $match[2] ? 100 / 255 : 1 ),
 338                  'g' => (float) $match[3] / ( $match[4] ? 100 / 255 : 1 ),
 339                  'b' => (float) $match[5] / ( $match[6] ? 100 / 255 : 1 ),
 340                  'a' => '' === $match[7] ? 1 : (float) $match[7] / ( $match[8] ? 100 : 1 ),
 341              )
 342          );
 343      }
 344  
 345      /**
 346       * Clamps an array of HSLA values.
 347       *
 348       * Direct port of colord's clampHsla function.
 349       *
 350       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L6 Sourced from colord.
 351       *
 352       * @internal
 353       *
 354       * @since 6.3.0
 355       *
 356       * @param array $hsla The HSLA array to clamp.
 357       * @return array The clamped HSLA array.
 358       */
 359  	private static function colord_clamp_hsla( $hsla ) {
 360          $hsla['h'] = self::colord_clamp_hue( $hsla['h'] );
 361          $hsla['s'] = self::colord_clamp( $hsla['s'], 0, 100 );
 362          $hsla['l'] = self::colord_clamp( $hsla['l'], 0, 100 );
 363          $hsla['a'] = self::colord_clamp( $hsla['a'] );
 364  
 365          return $hsla;
 366      }
 367  
 368      /**
 369       * Converts an HSVA array to RGBA.
 370       *
 371       * Direct port of colord's hsvaToRgba function.
 372       *
 373       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsv.ts#L52 Sourced from colord.
 374       *
 375       * @internal
 376       *
 377       * @since 6.3.0
 378       *
 379       * @param array $hsva The HSVA array to convert.
 380       * @return array The RGBA array.
 381       */
 382  	private static function colord_hsva_to_rgba( $hsva ) {
 383          $h = ( $hsva['h'] / 360 ) * 6;
 384          $s = $hsva['s'] / 100;
 385          $v = $hsva['v'] / 100;
 386          $a = $hsva['a'];
 387  
 388          $hh     = floor( $h );
 389          $b      = $v * ( 1 - $s );
 390          $c      = $v * ( 1 - ( $h - $hh ) * $s );
 391          $d      = $v * ( 1 - ( 1 - $h + $hh ) * $s );
 392          $module = $hh % 6;
 393  
 394          return array(
 395              'r' => array( $v, $c, $b, $b, $d, $v )[ $module ] * 255,
 396              'g' => array( $d, $v, $v, $c, $b, $b )[ $module ] * 255,
 397              'b' => array( $b, $b, $d, $v, $v, $c )[ $module ] * 255,
 398              'a' => $a,
 399          );
 400      }
 401  
 402      /**
 403       * Converts an HSLA array to HSVA.
 404       *
 405       * Direct port of colord's hslaToHsva function.
 406       *
 407       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L33 Sourced from colord.
 408       *
 409       * @internal
 410       *
 411       * @since 6.3.0
 412       *
 413       * @param array $hsla The HSLA array to convert.
 414       * @return array The HSVA array.
 415       */
 416  	private static function colord_hsla_to_hsva( $hsla ) {
 417          $h = $hsla['h'];
 418          $s = $hsla['s'];
 419          $l = $hsla['l'];
 420          $a = $hsla['a'];
 421  
 422          $s *= ( $l < 50 ? $l : 100 - $l ) / 100;
 423  
 424          return array(
 425              'h' => $h,
 426              's' => $s > 0 ? ( ( 2 * $s ) / ( $l + $s ) ) * 100 : 0,
 427              'v' => $l + $s,
 428              'a' => $a,
 429          );
 430      }
 431  
 432      /**
 433       * Converts an HSLA array to RGBA.
 434       *
 435       * Direct port of colord's hslaToRgba function.
 436       *
 437       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L55 Sourced from colord.
 438       *
 439       * @internal
 440       *
 441       * @since 6.3.0
 442       *
 443       * @param array $hsla The HSLA array to convert.
 444       * @return array The RGBA array.
 445       */
 446  	private static function colord_hsla_to_rgba( $hsla ) {
 447          return self::colord_hsva_to_rgba( self::colord_hsla_to_hsva( $hsla ) );
 448      }
 449  
 450      /**
 451       * Parses a valid HSL[A] CSS color function/string.
 452       *
 453       * Direct port of colord's parseHslaString function.
 454       *
 455       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hslString.ts#L17 Sourced from colord.
 456       *
 457       * @internal
 458       *
 459       * @since 6.3.0
 460       *
 461       * @param string $input The HSLA string to parse.
 462       * @return array|null An array of RGBA values or null if the RGB string is invalid.
 463       */
 464  	private static function colord_parse_hsla_string( $input ) {
 465          // Functional syntax.
 466          $is_match = preg_match(
 467              '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 468              $input,
 469              $match
 470          );
 471  
 472          if ( ! $is_match ) {
 473              // Whitespace syntax.
 474              $is_match = preg_match(
 475                  '/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i',
 476                  $input,
 477                  $match
 478              );
 479          }
 480  
 481          if ( ! $is_match ) {
 482              return null;
 483          }
 484  
 485          /*
 486           * For some reason, preg_match doesn't include empty matches at the end
 487           * of the array, so we add them manually to make things easier later.
 488           */
 489          for ( $i = 1; $i <= 6; $i++ ) {
 490              $match[ $i ] ??= '';
 491          }
 492  
 493          $hsla = self::colord_clamp_hsla(
 494              array(
 495                  'h' => self::colord_parse_hue( (float) $match[1], $match[2] ),
 496                  's' => (float) $match[3],
 497                  'l' => (float) $match[4],
 498                  'a' => '' === $match[5] ? 1 : (float) $match[5] / ( $match[6] ? 100 : 1 ),
 499              )
 500          );
 501  
 502          return self::colord_hsla_to_rgba( $hsla );
 503      }
 504  
 505      /**
 506       * Tries to convert an incoming string into RGBA values.
 507       *
 508       * Direct port of colord's parse function simplified for our use case. This
 509       * version only supports string parsing and only returns RGBA values.
 510       *
 511       * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/parse.ts#L37 Sourced from colord.
 512       *
 513       * @internal
 514       *
 515       * @since 6.3.0
 516       *
 517       * @param string $input The string to parse.
 518       * @return array|null An array of RGBA values or null if the string is invalid.
 519       */
 520  	private static function colord_parse( $input ) {
 521          $result = self::colord_parse_hex( $input );
 522  
 523          if ( ! $result ) {
 524              $result = self::colord_parse_rgba_string( $input );
 525          }
 526  
 527          if ( ! $result ) {
 528              $result = self::colord_parse_hsla_string( $input );
 529          }
 530  
 531          return $result;
 532      }
 533  
 534      /**
 535       * Takes the inline CSS duotone variable from a block and return the slug.
 536       *
 537       * Handles styles slugs like:
 538       * var:preset|duotone|blue-orange
 539       * var(--wp--preset--duotone--blue-orange)
 540       *
 541       * @internal
 542       *
 543       * @since 6.3.0
 544       *
 545       * @param string|string[] $duotone_attr The duotone attribute from a block.
 546       * @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed).
 547       */
 548  	private static function get_slug_from_attribute( $duotone_attr ) {
 549          if ( ! is_string( $duotone_attr ) ) {
 550              return '';
 551          }
 552  
 553          // Uses Branch Reset Groups `(?|…)` to return one capture group.
 554          preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches );
 555  
 556          return ! empty( $matches[1] ) ? $matches[1] : '';
 557      }
 558  
 559      /**
 560       * Checks if we have a valid duotone preset.
 561       *
 562       * Valid presets are defined in the $global_styles_presets array.
 563       *
 564       * @internal
 565       *
 566       * @since 6.3.0
 567       *
 568       * @param string|string[] $duotone_attr The duotone attribute from a block.
 569       * @return bool True if the duotone preset present and valid.
 570       */
 571  	private static function is_preset( $duotone_attr ) {
 572          if ( ! is_string( $duotone_attr ) ) {
 573              return false;
 574          }
 575  
 576          $slug      = self::get_slug_from_attribute( $duotone_attr );
 577          $filter_id = self::get_filter_id( $slug );
 578  
 579          return array_key_exists( $filter_id, self::get_all_global_styles_presets() );
 580      }
 581  
 582      /**
 583       * Gets the CSS variable name for a duotone preset.
 584       *
 585       * Example output:
 586       *  --wp--preset--duotone--blue-orange
 587       *
 588       * @internal
 589       *
 590       * @since 6.3.0
 591       *
 592       * @param string $slug The slug of the duotone preset.
 593       * @return string The CSS variable name.
 594       */
 595  	private static function get_css_custom_property_name( $slug ) {
 596          return "--wp--preset--duotone--$slug";
 597      }
 598  
 599      /**
 600       * Get the ID of the duotone filter.
 601       *
 602       * Example output:
 603       *  wp-duotone-blue-orange
 604       *
 605       * @internal
 606       *
 607       * @since 6.3.0
 608       *
 609       * @param string $slug The slug of the duotone preset.
 610       * @return string The ID of the duotone filter.
 611       */
 612  	private static function get_filter_id( $slug ) {
 613          return "wp-duotone-$slug";
 614      }
 615  
 616      /**
 617       * Get the CSS variable for a duotone preset.
 618       *
 619       * Example output:
 620       *  var(--wp--preset--duotone--blue-orange)
 621       *
 622       * @internal
 623       *
 624       * @since 6.3.0
 625       *
 626       * @param string $slug The slug of the duotone preset.
 627       * @return string The CSS variable.
 628       */
 629  	private static function get_css_var( $slug ) {
 630          $name = self::get_css_custom_property_name( $slug );
 631          return "var($name)";
 632      }
 633  
 634      /**
 635       * Get the URL for a duotone filter.
 636       *
 637       * Example output:
 638       *  url(#wp-duotone-blue-orange)
 639       *
 640       * @internal
 641       *
 642       * @since 6.3.0
 643       *
 644       * @param string $filter_id The ID of the filter.
 645       * @return string The URL for the duotone filter.
 646       */
 647  	private static function get_filter_url( $filter_id ) {
 648          return "url(#$filter_id)";
 649      }
 650  
 651      /**
 652       * Gets the SVG for the duotone filter definition.
 653       *
 654       * Whitespace is removed when SCRIPT_DEBUG is not enabled.
 655       *
 656       * @internal
 657       *
 658       * @since 6.3.0
 659       *
 660       * @param string $filter_id The ID of the filter.
 661       * @param array  $colors    An array of color strings.
 662       * @return string An SVG with a duotone filter definition.
 663       */
 664  	private static function get_filter_svg( $filter_id, $colors ) {
 665          $duotone_values = array(
 666              'r' => array(),
 667              'g' => array(),
 668              'b' => array(),
 669              'a' => array(),
 670          );
 671  
 672          foreach ( $colors as $color_str ) {
 673              $color = self::colord_parse( $color_str );
 674  
 675              if ( null === $color ) {
 676                  $error_message = sprintf(
 677                      /* translators: 1: Duotone colors, 2: theme.json, 3: settings.color.duotone */
 678                      __( '"%1$s" in %2$s %3$s is not a hex or rgb string.' ),
 679                      $color_str,
 680                      'theme.json',
 681                      'settings.color.duotone'
 682                  );
 683                  _doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
 684              } else {
 685                  $duotone_values['r'][] = $color['r'] / 255;
 686                  $duotone_values['g'][] = $color['g'] / 255;
 687                  $duotone_values['b'][] = $color['b'] / 255;
 688                  $duotone_values['a'][] = $color['a'];
 689              }
 690          }
 691  
 692          ob_start();
 693  
 694          ?>
 695  
 696          <svg
 697              xmlns="http://www.w3.org/2000/svg"
 698              viewBox="0 0 0 0"
 699              width="0"
 700              height="0"
 701              focusable="false"
 702              role="none"
 703              style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
 704          >
 705              <defs>
 706                  <filter id="<?php echo esc_attr( $filter_id ); ?>">
 707                      <feColorMatrix
 708                          color-interpolation-filters="sRGB"
 709                          type="matrix"
 710                          values="
 711                              .299 .587 .114 0 0
 712                              .299 .587 .114 0 0
 713                              .299 .587 .114 0 0
 714                              .299 .587 .114 0 0
 715                          "
 716                      />
 717                      <feComponentTransfer color-interpolation-filters="sRGB" >
 718                          <feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
 719                          <feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
 720                          <feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
 721                          <feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" />
 722                      </feComponentTransfer>
 723                      <feComposite in2="SourceGraphic" operator="in" />
 724                  </filter>
 725              </defs>
 726          </svg>
 727  
 728          <?php
 729  
 730          $svg = ob_get_clean();
 731  
 732          if ( ! SCRIPT_DEBUG ) {
 733              // Clean up the whitespace.
 734              $svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg );
 735              $svg = str_replace( '> <', '><', $svg );
 736              $svg = trim( $svg );
 737          }
 738  
 739          return $svg;
 740      }
 741  
 742      /**
 743       * Returns the prefixed id for the duotone filter for use as a CSS id.
 744       *
 745       * Exported for the deprecated function wp_get_duotone_filter_id().
 746       *
 747       * @internal
 748       *
 749       * @since 6.3.0
 750       * @deprecated 6.3.0
 751       *
 752       * @param array $preset Duotone preset value as seen in theme.json.
 753       * @return string Duotone filter CSS id.
 754       */
 755  	public static function get_filter_id_from_preset( $preset ) {
 756          _deprecated_function( __FUNCTION__, '6.3.0' );
 757  
 758          $filter_id = '';
 759          if ( isset( $preset['slug'] ) ) {
 760              $filter_id = self::get_filter_id( $preset['slug'] );
 761          }
 762          return $filter_id;
 763      }
 764  
 765      /**
 766       * Gets the SVG for the duotone filter definition from a preset.
 767       *
 768       * Exported for the deprecated function wp_get_duotone_filter_property().
 769       *
 770       * @internal
 771       *
 772       * @since 6.3.0
 773       * @deprecated 6.3.0
 774       *
 775       * @param array $preset The duotone preset.
 776       * @return string The SVG for the filter definition.
 777       */
 778  	public static function get_filter_svg_from_preset( $preset ) {
 779          _deprecated_function( __FUNCTION__, '6.3.0' );
 780  
 781          $filter_id = self::get_filter_id_from_preset( $preset );
 782          return self::get_filter_svg( $filter_id, $preset['colors'] );
 783      }
 784  
 785      /**
 786       * Get the SVGs for the duotone filters.
 787       *
 788       * Example output:
 789       *  <svg><defs><filter id="wp-duotone-blue-orange">…</filter></defs></svg><svg>…</svg>
 790       *
 791       * @internal
 792       *
 793       * @since 6.3.0
 794       *
 795       * @param array $sources The duotone presets.
 796       * @return string The SVGs for the duotone filters.
 797       */
 798  	private static function get_svg_definitions( $sources ) {
 799          $svgs = '';
 800          foreach ( $sources as $filter_id => $filter_data ) {
 801              $colors = $filter_data['colors'];
 802              $svgs  .= self::get_filter_svg( $filter_id, $colors );
 803          }
 804          return $svgs;
 805      }
 806  
 807      /**
 808       * Get the CSS for global styles.
 809       *
 810       * Example output:
 811       *  body{--wp--preset--duotone--blue-orange:url('#wp-duotone-blue-orange');}
 812       *
 813       * @internal
 814       *
 815       * @since 6.3.0
 816       * @since 6.6.0 Replaced body selector with `WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR`.
 817       *
 818       * @param array $sources The duotone presets.
 819       * @return string The CSS for global styles.
 820       */
 821  	private static function get_global_styles_presets( $sources ) {
 822          $css = WP_Theme_JSON::ROOT_CSS_PROPERTIES_SELECTOR . '{';
 823          foreach ( $sources as $filter_id => $filter_data ) {
 824              $slug              = $filter_data['slug'];
 825              $colors            = $filter_data['colors'];
 826              $css_property_name = self::get_css_custom_property_name( $slug );
 827              $declaration_value = is_string( $colors ) ? $colors : self::get_filter_url( $filter_id );
 828              $css              .= "$css_property_name:$declaration_value;";
 829          }
 830          $css .= '}';
 831          return $css;
 832      }
 833  
 834      /**
 835       * Enqueue a block CSS declaration for the page.
 836       *
 837       * This does not include any SVGs.
 838       *
 839       * @internal
 840       *
 841       * @since 6.3.0
 842       *
 843       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-000000-ffffff-2'.
 844       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 845       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'.
 846       */
 847  	private static function enqueue_block_css( $filter_id, $duotone_selector, $filter_value ) {
 848          // Build the CSS selectors to which the filter will be applied.
 849          $selectors = explode( ',', $duotone_selector );
 850  
 851          $selectors_scoped = array();
 852          foreach ( $selectors as $selector_part ) {
 853              /*
 854               * Assuming the selector part is a subclass selector (not a tag name)
 855               * so we can prepend the filter id class. If we want to support elements
 856               * such as `img` or namespaces, we'll need to add a case for that here.
 857               */
 858              $selectors_scoped[] = '.' . $filter_id . trim( $selector_part );
 859          }
 860  
 861          $selector = implode( ', ', $selectors_scoped );
 862  
 863          self::$block_css_declarations[] = array(
 864              'selector'     => $selector,
 865              'declarations' => array(
 866                  'filter' => $filter_value,
 867              ),
 868          );
 869      }
 870  
 871      /**
 872       * Enqueue custom filter assets for the page.
 873       *
 874       * Includes an SVG filter and block CSS declaration.
 875       *
 876       * @internal
 877       *
 878       * @since 6.3.0
 879       *
 880       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-000000-ffffff-2'.
 881       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 882       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'.
 883       * @param array  $filter_data      Duotone filter data with 'slug' and 'colors' keys.
 884       */
 885  	private static function enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ) {
 886          self::$used_svg_filter_data[ $filter_id ] = $filter_data;
 887          self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value );
 888      }
 889  
 890      /**
 891       * Enqueue preset assets for the page.
 892       *
 893       * Includes a CSS custom property, SVG filter, and block CSS declaration.
 894       *
 895       * @internal
 896       *
 897       * @since 6.3.0
 898       *
 899       * @param string $filter_id        The filter ID. e.g. 'wp-duotone-blue-orange'.
 900       * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'.
 901       * @param string $filter_value     The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'.
 902       */
 903  	private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) {
 904          $global_styles_presets = self::get_all_global_styles_presets();
 905          if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) {
 906              $error_message = sprintf(
 907                  /* translators: 1: Duotone filter ID, 2: theme.json */
 908                  __( 'The duotone id "%1$s" is not registered in %2$s settings' ),
 909                  $filter_id,
 910                  'theme.json'
 911              );
 912              _doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
 913              return;
 914          }
 915          self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ];
 916          self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] );
 917      }
 918  
 919      /**
 920       * Registers the style and colors block attributes for block types that support it.
 921       *
 922       * Block support is added with `supports.filter.duotone` in block.json.
 923       *
 924       * @since 6.3.0
 925       *
 926       * @param WP_Block_Type $block_type Block Type.
 927       */
 928  	public static function register_duotone_support( $block_type ) {
 929          /*
 930           * Previous `color.__experimentalDuotone` support flag is migrated
 931           * to `filter.duotone` via `block_type_metadata_settings` filter.
 932           */
 933          if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) {
 934              if ( ! $block_type->attributes ) {
 935                  $block_type->attributes = array();
 936              }
 937  
 938              if ( ! array_key_exists( 'style', $block_type->attributes ) ) {
 939                  $block_type->attributes['style'] = array(
 940                      'type' => 'object',
 941                  );
 942              }
 943          }
 944      }
 945  
 946      /**
 947       * Get the CSS selector for a block type.
 948       *
 949       * This handles selectors defined in `color.__experimentalDuotone` support
 950       * if `filter.duotone` support is not defined.
 951       *
 952       * @internal
 953       * @since 6.3.0
 954       *
 955       * @param WP_Block_Type $block_type Block type to check for support.
 956       * @return string|null The CSS selector or null if there is no support.
 957       */
 958  	private static function get_selector( $block_type ) {
 959          if ( ! ( $block_type instanceof WP_Block_Type ) ) {
 960              return null;
 961          }
 962  
 963          /*
 964           * Backward compatibility with `supports.color.__experimentalDuotone`
 965           * is provided via the `block_type_metadata_settings` filter. If
 966           * `supports.filter.duotone` has not been set and the experimental
 967           * property has been, the experimental property value is copied into
 968           * `supports.filter.duotone`.
 969           */
 970          $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) );
 971          if ( ! $duotone_support ) {
 972              return null;
 973          }
 974  
 975          /*
 976           * If the experimental duotone support was set, that value is to be
 977           * treated as a selector and requires scoping.
 978           */
 979          $experimental_duotone = $block_type->supports['color']['__experimentalDuotone'] ?? false;
 980          if ( $experimental_duotone ) {
 981              $root_selector = wp_get_block_css_selector( $block_type );
 982              return is_string( $experimental_duotone )
 983                  ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone )
 984                  : $root_selector;
 985          }
 986  
 987          // Regular filter.duotone support uses filter.duotone selectors with fallbacks.
 988          return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true );
 989      }
 990  
 991      /**
 992       * Scrape all possible duotone presets from global and theme styles and
 993       * store them in self::$global_styles_presets.
 994       *
 995       * Used in conjunction with self::render_duotone_support for blocks that
 996       * use duotone preset filters.
 997       *
 998       * @since 6.3.0
 999       *
1000       * @return array An array of global styles presets, keyed on the filter ID.
1001       */
1002  	private static function get_all_global_styles_presets() {
1003          if ( isset( self::$global_styles_presets ) ) {
1004              return self::$global_styles_presets;
1005          }
1006          // Get the per block settings from the theme.json.
1007          $tree              = wp_get_global_settings();
1008          $presets_by_origin = $tree['color']['duotone'] ?? array();
1009  
1010          self::$global_styles_presets = array();
1011          foreach ( $presets_by_origin as $presets ) {
1012              foreach ( $presets as $preset ) {
1013                  $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) );
1014  
1015                  self::$global_styles_presets[ $filter_id ] = $preset;
1016              }
1017          }
1018  
1019          return self::$global_styles_presets;
1020      }
1021  
1022      /**
1023       * Scrape all block names from global styles and store in self::$global_styles_block_names.
1024       *
1025       * Used in conjunction with self::render_duotone_support to output the
1026       * duotone filters defined in the theme.json global styles.
1027       *
1028       * @since 6.3.0
1029       *
1030       * @return string[] An array of global style block slugs, keyed on the block name.
1031       */
1032  	private static function get_all_global_style_block_names() {
1033          if ( isset( self::$global_styles_block_names ) ) {
1034              return self::$global_styles_block_names;
1035          }
1036          // Get the per block settings from the theme.json.
1037          $tree        = WP_Theme_JSON_Resolver::get_merged_data();
1038          $block_nodes = $tree->get_styles_block_nodes();
1039          $theme_json  = $tree->get_raw_data();
1040  
1041          self::$global_styles_block_names = array();
1042  
1043          foreach ( $block_nodes as $block_node ) {
1044              // This block definition doesn't include any duotone settings. Skip it.
1045              if ( empty( $block_node['duotone'] ) ) {
1046                  continue;
1047              }
1048  
1049              // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'.
1050              $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) );
1051              $duotone_attr      = _wp_array_get( $theme_json, $duotone_attr_path, array() );
1052  
1053              if ( empty( $duotone_attr ) ) {
1054                  continue;
1055              }
1056              // If it has a duotone filter preset, save the block name and the preset slug.
1057              // Only process if it's a string (preset reference), not an array (custom colors).
1058              if ( ! is_string( $duotone_attr ) ) {
1059                  continue;
1060              }
1061  
1062              $slug = self::get_slug_from_attribute( $duotone_attr );
1063  
1064              if ( $slug && $slug !== $duotone_attr ) {
1065                  self::$global_styles_block_names[ $block_node['name'] ] = $slug;
1066              }
1067          }
1068          return self::$global_styles_block_names;
1069      }
1070  
1071      /**
1072       * Render out the duotone CSS styles and SVG.
1073       *
1074       * The hooks self::set_global_style_block_names and self::set_global_styles_presets
1075       * must be called before this function.
1076       *
1077       * @since 6.3.0
1078       *
1079       * @param string   $block_content Rendered block content.
1080       * @param array    $block         Block object.
1081       * @param WP_Block $wp_block      The block instance.
1082       * @return string Filtered block content.
1083       */
1084  	public static function render_duotone_support( $block_content, $block, $wp_block ) {
1085          if ( ! $block['blockName'] ) {
1086              return $block_content;
1087          }
1088          $duotone_selector = self::get_selector( $wp_block->block_type );
1089  
1090          if ( ! $duotone_selector ) {
1091              return $block_content;
1092          }
1093  
1094          $global_styles_block_names = self::get_all_global_style_block_names();
1095  
1096          // The block should have a duotone attribute or have duotone defined in its theme.json to be processed.
1097          $has_duotone_attribute     = isset( $block['attrs']['style']['color']['duotone'] );
1098          $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names );
1099  
1100          if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) {
1101              return $block_content;
1102          }
1103  
1104          // Generate the pieces needed for rendering a duotone to the page.
1105          if ( $has_duotone_attribute ) {
1106  
1107              /*
1108               * Possible values for duotone attribute:
1109               * 1. Array of colors - e.g. array('#000000', '#ffffff').
1110               * 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|blue-orange' or 'var(--wp--preset--duotone--blue-orange)''
1111               * 3. A CSS string - e.g. 'unset' to remove globally applied duotone.
1112               */
1113  
1114              $duotone_attr = $block['attrs']['style']['color']['duotone'];
1115              if ( ! is_string( $duotone_attr ) && ! is_array( $duotone_attr ) ) {
1116                  return $block_content;
1117              }
1118  
1119              $is_preset = is_string( $duotone_attr ) && self::is_preset( $duotone_attr );
1120              $is_css    = is_string( $duotone_attr ) && ! $is_preset;
1121  
1122              if ( $is_preset ) {
1123  
1124                  $slug         = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'blue-orange'.
1125                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
1126                  $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
1127  
1128                  // CSS custom property, SVG filter, and block CSS.
1129                  self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value );
1130  
1131              } elseif ( $is_css ) {
1132                  $slug         = wp_unique_id( sanitize_key( $duotone_attr . '-' ) ); // e.g. 'unset-1'.
1133                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-unset-1'.
1134                  $filter_value = $duotone_attr; // e.g. 'unset'.
1135  
1136                  // Just block CSS.
1137                  self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value );
1138              } else {
1139                  $slug         = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); // e.g. '000000-ffffff-2'.
1140                  $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-000000-ffffff-2'.
1141                  $filter_value = self::get_filter_url( $filter_id ); // e.g. 'url(#wp-duotone-filter-000000-ffffff-2)'.
1142                  $filter_data  = array(
1143                      'slug'   => $slug,
1144                      'colors' => $duotone_attr,
1145                  );
1146  
1147                  // SVG filter and block CSS.
1148                  self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data );
1149              }
1150          } else {
1151              $slug         = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
1152              $filter_id    = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
1153              $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
1154  
1155              // CSS custom property, SVG filter, and block CSS.
1156              self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value );
1157          }
1158  
1159          // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper.
1160          $tags = new WP_HTML_Tag_Processor( $block_content );
1161          if ( $tags->next_tag() ) {
1162              $tags->add_class( $filter_id );
1163          }
1164          return $tags->get_updated_html();
1165      }
1166  
1167      /**
1168       * Fixes the issue with our generated class name not being added to the block's outer container
1169       * in classic themes due to gutenberg_restore_image_outer_container from layout block supports.
1170       *
1171       * @since 6.6.0
1172       *
1173       * @param string $block_content Rendered block content.
1174       * @return string Filtered block content.
1175       */
1176  	public static function restore_image_outer_container( $block_content ) {
1177          if ( wp_theme_has_theme_json() ) {
1178              return $block_content;
1179          }
1180  
1181          $tags          = new WP_HTML_Tag_Processor( $block_content );
1182          $wrapper_query = array(
1183              'tag_name'   => 'div',
1184              'class_name' => 'wp-block-image',
1185          );
1186          if ( ! $tags->next_tag( $wrapper_query ) ) {
1187              return $block_content;
1188          }
1189  
1190          $tags->set_bookmark( 'wrapper-div' );
1191          $tags->next_tag();
1192  
1193          foreach ( $tags->class_list() as $classname ) {
1194              if ( str_starts_with( $classname, 'wp-duotone' ) ) {
1195                  $tags->remove_class( $classname );
1196                  $tags->seek( 'wrapper-div' );
1197                  $tags->add_class( $classname );
1198                  break;
1199              }
1200          }
1201  
1202          return $tags->get_updated_html();
1203      }
1204  
1205      /**
1206       * Appends the used block duotone filter declarations to the inline block supports CSS.
1207       *
1208       * Uses the declarations saved in earlier calls to self::enqueue_block_css.
1209       *
1210       * @since 6.3.0
1211       */
1212  	public static function output_block_styles() {
1213          if ( ! empty( self::$block_css_declarations ) ) {
1214              wp_style_engine_get_stylesheet_from_css_rules(
1215                  self::$block_css_declarations,
1216                  array(
1217                      'context' => 'block-supports',
1218                  )
1219              );
1220          }
1221      }
1222  
1223      /**
1224       * Appends the used global style duotone filter presets (CSS custom
1225       * properties) to the inline global styles CSS.
1226       *
1227       * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset.
1228       *
1229       * @since 6.3.0
1230       */
1231  	public static function output_global_styles() {
1232          if ( ! empty( self::$used_global_styles_presets ) ) {
1233              wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) );
1234          }
1235      }
1236  
1237      /**
1238       * Outputs all necessary SVG for duotone filters, CSS for classic themes.
1239       *
1240       * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset
1241       * and self::enqueue_custom_filter.
1242       *
1243       * @since 6.3.0
1244       */
1245  	public static function output_footer_assets() {
1246          if ( ! empty( self::$used_svg_filter_data ) ) {
1247              echo self::get_svg_definitions( self::$used_svg_filter_data );
1248          }
1249  
1250          // In block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action.
1251          if ( ! wp_is_block_theme() ) {
1252              $style_tag_id = 'core-block-supports-duotone';
1253              wp_register_style( $style_tag_id, false );
1254              if ( ! empty( self::$used_global_styles_presets ) ) {
1255                  wp_add_inline_style( $style_tag_id, self::get_global_styles_presets( self::$used_global_styles_presets ) );
1256              }
1257              if ( ! empty( self::$block_css_declarations ) ) {
1258                  wp_add_inline_style( $style_tag_id, wp_style_engine_get_stylesheet_from_css_rules( self::$block_css_declarations ) );
1259              }
1260              wp_enqueue_style( $style_tag_id );
1261          }
1262      }
1263  
1264      /**
1265       * Adds the duotone SVGs and CSS custom properties to the editor settings.
1266       *
1267       * This allows the properties to be pulled in by the EditorStyles component
1268       * in JS and rendered in the post editor.
1269       *
1270       * @since 6.3.0
1271       *
1272       * @param array $settings The block editor settings from the `block_editor_settings_all` filter.
1273       * @return array The editor settings with duotone SVGs and CSS custom properties.
1274       */
1275  	public static function add_editor_settings( $settings ) {
1276          $global_styles_presets = self::get_all_global_styles_presets();
1277          if ( ! empty( $global_styles_presets ) ) {
1278              $settings['styles'] ??= array();
1279  
1280              $settings['styles'][] = array(
1281                  // For the editor we can add all of the presets by default.
1282                  'assets'         => self::get_svg_definitions( $global_styles_presets ),
1283                  // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work.
1284                  '__unstableType' => 'svgs',
1285                  // These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings.
1286                  'isGlobalStyles' => false,
1287              );
1288  
1289              $settings['styles'][] = array(
1290                  // For the editor we can add all of the presets by default.
1291                  'css'            => self::get_global_styles_presets( $global_styles_presets ),
1292                  // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component.
1293                  '__unstableType' => 'presets',
1294                  // 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.
1295                  'isGlobalStyles' => false,
1296              );
1297          }
1298  
1299          return $settings;
1300      }
1301  
1302      /**
1303       * Migrates the experimental duotone support flag to the stabilized location.
1304       *
1305       * This moves `supports.color.__experimentalDuotone` to `supports.filter.duotone`.
1306       *
1307       * @since 6.3.0
1308       *
1309       * @param array $settings Current block type settings.
1310       * @param array $metadata Block metadata as read in via block.json.
1311       * @return array Filtered block type settings.
1312       */
1313  	public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) {
1314          $duotone_support = $metadata['supports']['color']['__experimentalDuotone'] ?? null;
1315  
1316          if ( ! isset( $settings['supports']['filter']['duotone'] ) && null !== $duotone_support ) {
1317              _wp_array_set( $settings, array( 'supports', 'filter', 'duotone' ), (bool) $duotone_support );
1318          }
1319  
1320          return $settings;
1321      }
1322  
1323      /**
1324       * Gets the CSS filter property value from a preset.
1325       *
1326       * Exported for the deprecated function wp_get_duotone_filter_id().
1327       *
1328       * @internal
1329       *
1330       * @since 6.3.0
1331       * @deprecated 6.3.0
1332       *
1333       * @param array $preset The duotone preset.
1334       * @return string The CSS filter property value.
1335       */
1336  	public static function get_filter_css_property_value_from_preset( $preset ) {
1337          _deprecated_function( __FUNCTION__, '6.3.0' );
1338  
1339          if ( isset( $preset['colors'] ) && is_string( $preset['colors'] ) ) {
1340              return $preset['colors'];
1341          }
1342  
1343          $filter_id = self::get_filter_id_from_preset( $preset );
1344  
1345          return 'url(#' . $filter_id . ')';
1346      }
1347  }


Generated : Tue Nov 17 08:20:01 2026 Cross-referenced by PHPXref