[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 = isset( $angle_units[ $unit ] ) ? $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( 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 $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. 551 */ 552 private static function get_slug_from_attribute( $duotone_attr ) { 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 $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 $slug = self::get_slug_from_attribute( $duotone_attr ); 573 $filter_id = self::get_filter_id( $slug ); 574 575 return array_key_exists( $filter_id, self::get_all_global_styles_presets() ); 576 } 577 578 /** 579 * Gets the CSS variable name for a duotone preset. 580 * 581 * Example output: 582 * --wp--preset--duotone--blue-orange 583 * 584 * @internal 585 * 586 * @since 6.3.0 587 * 588 * @param string $slug The slug of the duotone preset. 589 * @return string The CSS variable name. 590 */ 591 private static function get_css_custom_property_name( $slug ) { 592 return "--wp--preset--duotone--$slug"; 593 } 594 595 /** 596 * Get the ID of the duotone filter. 597 * 598 * Example output: 599 * wp-duotone-blue-orange 600 * 601 * @internal 602 * 603 * @since 6.3.0 604 * 605 * @param string $slug The slug of the duotone preset. 606 * @return string The ID of the duotone filter. 607 */ 608 private static function get_filter_id( $slug ) { 609 return "wp-duotone-$slug"; 610 } 611 612 /** 613 * Get the CSS variable for a duotone preset. 614 * 615 * Example output: 616 * var(--wp--preset--duotone--blue-orange) 617 * 618 * @internal 619 * 620 * @since 6.3.0 621 * 622 * @param string $slug The slug of the duotone preset. 623 * @return string The CSS variable. 624 */ 625 private static function get_css_var( $slug ) { 626 $name = self::get_css_custom_property_name( $slug ); 627 return "var($name)"; 628 } 629 630 /** 631 * Get the URL for a duotone filter. 632 * 633 * Example output: 634 * url(#wp-duotone-blue-orange) 635 * 636 * @internal 637 * 638 * @since 6.3.0 639 * 640 * @param string $filter_id The ID of the filter. 641 * @return string The URL for the duotone filter. 642 */ 643 private static function get_filter_url( $filter_id ) { 644 return "url(#$filter_id)"; 645 } 646 647 /** 648 * Gets the SVG for the duotone filter definition. 649 * 650 * Whitespace is removed when SCRIPT_DEBUG is not enabled. 651 * 652 * @internal 653 * 654 * @since 6.3.0 655 * 656 * @param string $filter_id The ID of the filter. 657 * @param array $colors An array of color strings. 658 * @return string An SVG with a duotone filter definition. 659 */ 660 private static function get_filter_svg( $filter_id, $colors ) { 661 $duotone_values = array( 662 'r' => array(), 663 'g' => array(), 664 'b' => array(), 665 'a' => array(), 666 ); 667 668 foreach ( $colors as $color_str ) { 669 $color = self::colord_parse( $color_str ); 670 671 if ( null === $color ) { 672 $error_message = sprintf( 673 /* translators: 1: Duotone colors, 2: theme.json, 3: settings.color.duotone */ 674 __( '"%1$s" in %2$s %3$s is not a hex or rgb string.' ), 675 $color_str, 676 'theme.json', 677 'settings.color.duotone' 678 ); 679 _doing_it_wrong( __METHOD__, $error_message, '6.3.0' ); 680 } else { 681 $duotone_values['r'][] = $color['r'] / 255; 682 $duotone_values['g'][] = $color['g'] / 255; 683 $duotone_values['b'][] = $color['b'] / 255; 684 $duotone_values['a'][] = $color['a']; 685 } 686 } 687 688 ob_start(); 689 690 ?> 691 692 <svg 693 xmlns="http://www.w3.org/2000/svg" 694 viewBox="0 0 0 0" 695 width="0" 696 height="0" 697 focusable="false" 698 role="none" 699 style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;" 700 > 701 <defs> 702 <filter id="<?php echo esc_attr( $filter_id ); ?>"> 703 <feColorMatrix 704 color-interpolation-filters="sRGB" 705 type="matrix" 706 values=" 707 .299 .587 .114 0 0 708 .299 .587 .114 0 0 709 .299 .587 .114 0 0 710 .299 .587 .114 0 0 711 " 712 /> 713 <feComponentTransfer color-interpolation-filters="sRGB" > 714 <feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" /> 715 <feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" /> 716 <feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" /> 717 <feFuncA type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['a'] ) ); ?>" /> 718 </feComponentTransfer> 719 <feComposite in2="SourceGraphic" operator="in" /> 720 </filter> 721 </defs> 722 </svg> 723 724 <?php 725 726 $svg = ob_get_clean(); 727 728 if ( ! SCRIPT_DEBUG ) { 729 // Clean up the whitespace. 730 $svg = preg_replace( "/[\r\n\t ]+/", ' ', $svg ); 731 $svg = str_replace( '> <', '><', $svg ); 732 $svg = trim( $svg ); 733 } 734 735 return $svg; 736 } 737 738 /** 739 * Returns the prefixed id for the duotone filter for use as a CSS id. 740 * 741 * Exported for the deprecated function wp_get_duotone_filter_id(). 742 * 743 * @internal 744 * 745 * @since 6.3.0 746 * @deprecated 6.3.0 747 * 748 * @param array $preset Duotone preset value as seen in theme.json. 749 * @return string Duotone filter CSS id. 750 */ 751 public static function get_filter_id_from_preset( $preset ) { 752 _deprecated_function( __FUNCTION__, '6.3.0' ); 753 754 $filter_id = ''; 755 if ( isset( $preset['slug'] ) ) { 756 $filter_id = self::get_filter_id( $preset['slug'] ); 757 } 758 return $filter_id; 759 } 760 761 /** 762 * Gets the SVG for the duotone filter definition from a preset. 763 * 764 * Exported for the deprecated function wp_get_duotone_filter_property(). 765 * 766 * @internal 767 * 768 * @since 6.3.0 769 * @deprecated 6.3.0 770 * 771 * @param array $preset The duotone preset. 772 * @return string The SVG for the filter definition. 773 */ 774 public static function get_filter_svg_from_preset( $preset ) { 775 _deprecated_function( __FUNCTION__, '6.3.0' ); 776 777 $filter_id = self::get_filter_id_from_preset( $preset ); 778 return self::get_filter_svg( $filter_id, $preset['colors'] ); 779 } 780 781 /** 782 * Get the SVGs for the duotone filters. 783 * 784 * Example output: 785 * <svg><defs><filter id="wp-duotone-blue-orange">…</filter></defs></svg><svg>…</svg> 786 * 787 * @internal 788 * 789 * @since 6.3.0 790 * 791 * @param array $sources The duotone presets. 792 * @return string The SVGs for the duotone filters. 793 */ 794 private static function get_svg_definitions( $sources ) { 795 $svgs = ''; 796 foreach ( $sources as $filter_id => $filter_data ) { 797 $colors = $filter_data['colors']; 798 $svgs .= self::get_filter_svg( $filter_id, $colors ); 799 } 800 return $svgs; 801 } 802 803 /** 804 * Get the CSS for global styles. 805 * 806 * Example output: 807 * body{--wp--preset--duotone--blue-orange:url('#wp-duotone-blue-orange');} 808 * 809 * @internal 810 * 811 * @since 6.3.0 812 * 813 * @param array $sources The duotone presets. 814 * @return string The CSS for global styles. 815 */ 816 private static function get_global_styles_presets( $sources ) { 817 $css = 'body{'; 818 foreach ( $sources as $filter_id => $filter_data ) { 819 $slug = $filter_data['slug']; 820 $colors = $filter_data['colors']; 821 $css_property_name = self::get_css_custom_property_name( $slug ); 822 $declaration_value = is_string( $colors ) ? $colors : self::get_filter_url( $filter_id ); 823 $css .= "$css_property_name:$declaration_value;"; 824 } 825 $css .= '}'; 826 return $css; 827 } 828 829 /** 830 * Enqueue a block CSS declaration for the page. 831 * 832 * This does not include any SVGs. 833 * 834 * @internal 835 * 836 * @since 6.3.0 837 * 838 * @param string $filter_id The filter ID. e.g. 'wp-duotone-000000-ffffff-2'. 839 * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. 840 * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'. 841 */ 842 private static function enqueue_block_css( $filter_id, $duotone_selector, $filter_value ) { 843 // Build the CSS selectors to which the filter will be applied. 844 $selectors = explode( ',', $duotone_selector ); 845 846 $selectors_scoped = array(); 847 foreach ( $selectors as $selector_part ) { 848 /* 849 * Assuming the selector part is a subclass selector (not a tag name) 850 * so we can prepend the filter id class. If we want to support elements 851 * such as `img` or namespaces, we'll need to add a case for that here. 852 */ 853 $selectors_scoped[] = '.' . $filter_id . trim( $selector_part ); 854 } 855 856 $selector = implode( ', ', $selectors_scoped ); 857 858 self::$block_css_declarations[] = array( 859 'selector' => $selector, 860 'declarations' => array( 861 'filter' => $filter_value, 862 ), 863 ); 864 } 865 866 /** 867 * Enqueue custom filter assets for the page. 868 * 869 * Includes an SVG filter and block CSS declaration. 870 * 871 * @internal 872 * 873 * @since 6.3.0 874 * 875 * @param string $filter_id The filter ID. e.g. 'wp-duotone-000000-ffffff-2'. 876 * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. 877 * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-000000-ffffff-2)' or 'unset'. 878 * @param array $filter_data Duotone filter data with 'slug' and 'colors' keys. 879 */ 880 private static function enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ) { 881 self::$used_svg_filter_data[ $filter_id ] = $filter_data; 882 self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value ); 883 } 884 885 /** 886 * Enqueue preset assets for the page. 887 * 888 * Includes a CSS custom property, SVG filter, and block CSS declaration. 889 * 890 * @internal 891 * 892 * @since 6.3.0 893 * 894 * @param string $filter_id The filter ID. e.g. 'wp-duotone-blue-orange'. 895 * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. 896 * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'. 897 */ 898 private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) { 899 $global_styles_presets = self::get_all_global_styles_presets(); 900 if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) { 901 $error_message = sprintf( 902 /* translators: 1: Duotone filter ID, 2: theme.json */ 903 __( 'The duotone id "%1$s" is not registered in %2$s settings' ), 904 $filter_id, 905 'theme.json' 906 ); 907 _doing_it_wrong( __METHOD__, $error_message, '6.3.0' ); 908 return; 909 } 910 self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ]; 911 self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] ); 912 } 913 914 /** 915 * Registers the style and colors block attributes for block types that support it. 916 * 917 * Block support is added with `supports.filter.duotone` in block.json. 918 * 919 * @since 6.3.0 920 * 921 * @param WP_Block_Type $block_type Block Type. 922 */ 923 public static function register_duotone_support( $block_type ) { 924 /* 925 * Previous `color.__experimentalDuotone` support flag is migrated 926 * to `filter.duotone` via `block_type_metadata_settings` filter. 927 */ 928 if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) { 929 if ( ! $block_type->attributes ) { 930 $block_type->attributes = array(); 931 } 932 933 if ( ! array_key_exists( 'style', $block_type->attributes ) ) { 934 $block_type->attributes['style'] = array( 935 'type' => 'object', 936 ); 937 } 938 } 939 } 940 941 /** 942 * Get the CSS selector for a block type. 943 * 944 * This handles selectors defined in `color.__experimentalDuotone` support 945 * if `filter.duotone` support is not defined. 946 * 947 * @internal 948 * @since 6.3.0 949 * 950 * @param WP_Block_Type $block_type Block type to check for support. 951 * @return string|null The CSS selector or null if there is no support. 952 */ 953 private static function get_selector( $block_type ) { 954 if ( ! ( $block_type instanceof WP_Block_Type ) ) { 955 return null; 956 } 957 958 /* 959 * Backward compatibility with `supports.color.__experimentalDuotone` 960 * is provided via the `block_type_metadata_settings` filter. If 961 * `supports.filter.duotone` has not been set and the experimental 962 * property has been, the experimental property value is copied into 963 * `supports.filter.duotone`. 964 */ 965 $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) ); 966 if ( ! $duotone_support ) { 967 return null; 968 } 969 970 /* 971 * If the experimental duotone support was set, that value is to be 972 * treated as a selector and requires scoping. 973 */ 974 $experimental_duotone = isset( $block_type->supports['color']['__experimentalDuotone'] ) 975 ? $block_type->supports['color']['__experimentalDuotone'] 976 : false; 977 if ( $experimental_duotone ) { 978 $root_selector = wp_get_block_css_selector( $block_type ); 979 return is_string( $experimental_duotone ) 980 ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone ) 981 : $root_selector; 982 } 983 984 // Regular filter.duotone support uses filter.duotone selectors with fallbacks. 985 return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); 986 } 987 988 /** 989 * Scrape all possible duotone presets from global and theme styles and 990 * store them in self::$global_styles_presets. 991 * 992 * Used in conjunction with self::render_duotone_support for blocks that 993 * use duotone preset filters. 994 * 995 * @since 6.3.0 996 * 997 * @return array An array of global styles presets, keyed on the filter ID. 998 */ 999 private static function get_all_global_styles_presets() { 1000 if ( isset( self::$global_styles_presets ) ) { 1001 return self::$global_styles_presets; 1002 } 1003 // Get the per block settings from the theme.json. 1004 $tree = wp_get_global_settings(); 1005 $presets_by_origin = isset( $tree['color']['duotone'] ) ? $tree['color']['duotone'] : array(); 1006 1007 self::$global_styles_presets = array(); 1008 foreach ( $presets_by_origin as $presets ) { 1009 foreach ( $presets as $preset ) { 1010 $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); 1011 1012 self::$global_styles_presets[ $filter_id ] = $preset; 1013 } 1014 } 1015 1016 return self::$global_styles_presets; 1017 } 1018 1019 /** 1020 * Scrape all block names from global styles and store in self::$global_styles_block_names. 1021 * 1022 * Used in conjunction with self::render_duotone_support to output the 1023 * duotone filters defined in the theme.json global styles. 1024 * 1025 * @since 6.3.0 1026 * 1027 * @return string[] An array of global style block slugs, keyed on the block name. 1028 */ 1029 private static function get_all_global_style_block_names() { 1030 if ( isset( self::$global_styles_block_names ) ) { 1031 return self::$global_styles_block_names; 1032 } 1033 // Get the per block settings from the theme.json. 1034 $tree = WP_Theme_JSON_Resolver::get_merged_data(); 1035 $block_nodes = $tree->get_styles_block_nodes(); 1036 $theme_json = $tree->get_raw_data(); 1037 1038 self::$global_styles_block_names = array(); 1039 1040 foreach ( $block_nodes as $block_node ) { 1041 // This block definition doesn't include any duotone settings. Skip it. 1042 if ( empty( $block_node['duotone'] ) ) { 1043 continue; 1044 } 1045 1046 // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'. 1047 $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) ); 1048 $duotone_attr = _wp_array_get( $theme_json, $duotone_attr_path, array() ); 1049 1050 if ( empty( $duotone_attr ) ) { 1051 continue; 1052 } 1053 // If it has a duotone filter preset, save the block name and the preset slug. 1054 $slug = self::get_slug_from_attribute( $duotone_attr ); 1055 1056 if ( $slug && $slug !== $duotone_attr ) { 1057 self::$global_styles_block_names[ $block_node['name'] ] = $slug; 1058 } 1059 } 1060 return self::$global_styles_block_names; 1061 } 1062 1063 /** 1064 * Render out the duotone CSS styles and SVG. 1065 * 1066 * The hooks self::set_global_style_block_names and self::set_global_styles_presets 1067 * must be called before this function. 1068 * 1069 * @since 6.3.0 1070 * 1071 * @param string $block_content Rendered block content. 1072 * @param array $block Block object. 1073 * @param WP_Block $wp_block The block instance. 1074 * @return string Filtered block content. 1075 */ 1076 public static function render_duotone_support( $block_content, $block, $wp_block ) { 1077 if ( ! $block['blockName'] ) { 1078 return $block_content; 1079 } 1080 $duotone_selector = self::get_selector( $wp_block->block_type ); 1081 1082 if ( ! $duotone_selector ) { 1083 return $block_content; 1084 } 1085 1086 $global_styles_block_names = self::get_all_global_style_block_names(); 1087 1088 // The block should have a duotone attribute or have duotone defined in its theme.json to be processed. 1089 $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] ); 1090 $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names ); 1091 1092 if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) { 1093 return $block_content; 1094 } 1095 1096 // Generate the pieces needed for rendering a duotone to the page. 1097 if ( $has_duotone_attribute ) { 1098 1099 /* 1100 * Possible values for duotone attribute: 1101 * 1. Array of colors - e.g. array('#000000', '#ffffff'). 1102 * 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|blue-orange' or 'var(--wp--preset--duotone--blue-orange)'' 1103 * 3. A CSS string - e.g. 'unset' to remove globally applied duotone. 1104 */ 1105 1106 $duotone_attr = $block['attrs']['style']['color']['duotone']; 1107 $is_preset = is_string( $duotone_attr ) && self::is_preset( $duotone_attr ); 1108 $is_css = is_string( $duotone_attr ) && ! $is_preset; 1109 $is_custom = is_array( $duotone_attr ); 1110 1111 if ( $is_preset ) { 1112 1113 $slug = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'blue-orange'. 1114 $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. 1115 $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. 1116 1117 // CSS custom property, SVG filter, and block CSS. 1118 self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); 1119 1120 } elseif ( $is_css ) { 1121 $slug = wp_unique_id( sanitize_key( $duotone_attr . '-' ) ); // e.g. 'unset-1'. 1122 $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-unset-1'. 1123 $filter_value = $duotone_attr; // e.g. 'unset'. 1124 1125 // Just block CSS. 1126 self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value ); 1127 } elseif ( $is_custom ) { 1128 $slug = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); // e.g. '000000-ffffff-2'. 1129 $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-000000-ffffff-2'. 1130 $filter_value = self::get_filter_url( $filter_id ); // e.g. 'url(#wp-duotone-filter-000000-ffffff-2)'. 1131 $filter_data = array( 1132 'slug' => $slug, 1133 'colors' => $duotone_attr, 1134 ); 1135 1136 // SVG filter and block CSS. 1137 self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ); 1138 } 1139 } elseif ( $has_global_styles_duotone ) { 1140 $slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. 1141 $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. 1142 $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. 1143 1144 // CSS custom property, SVG filter, and block CSS. 1145 self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); 1146 } 1147 1148 // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper. 1149 $tags = new WP_HTML_Tag_Processor( $block_content ); 1150 if ( $tags->next_tag() ) { 1151 $tags->add_class( $filter_id ); 1152 } 1153 return $tags->get_updated_html(); 1154 } 1155 1156 /** 1157 * Appends the used block duotone filter declarations to the inline block supports CSS. 1158 * 1159 * Uses the declarations saved in earlier calls to self::enqueue_block_css. 1160 * 1161 * @since 6.3.0 1162 */ 1163 public static function output_block_styles() { 1164 if ( ! empty( self::$block_css_declarations ) ) { 1165 wp_style_engine_get_stylesheet_from_css_rules( 1166 self::$block_css_declarations, 1167 array( 1168 'context' => 'block-supports', 1169 ) 1170 ); 1171 } 1172 } 1173 1174 /** 1175 * Appends the used global style duotone filter presets (CSS custom 1176 * properties) to the inline global styles CSS. 1177 * 1178 * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset. 1179 * 1180 * @since 6.3.0 1181 */ 1182 public static function output_global_styles() { 1183 if ( ! empty( self::$used_global_styles_presets ) ) { 1184 wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) ); 1185 } 1186 } 1187 1188 /** 1189 * Outputs all necessary SVG for duotone filters, CSS for classic themes. 1190 * 1191 * Uses the declarations saved in earlier calls to self::enqueue_global_styles_preset 1192 * and self::enqueue_custom_filter. 1193 * 1194 * @since 6.3.0 1195 */ 1196 public static function output_footer_assets() { 1197 if ( ! empty( self::$used_svg_filter_data ) ) { 1198 echo self::get_svg_definitions( self::$used_svg_filter_data ); 1199 } 1200 1201 // In block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. 1202 if ( ! wp_is_block_theme() ) { 1203 $style_tag_id = 'core-block-supports-duotone'; 1204 wp_register_style( $style_tag_id, false ); 1205 if ( ! empty( self::$used_global_styles_presets ) ) { 1206 wp_add_inline_style( $style_tag_id, self::get_global_styles_presets( self::$used_global_styles_presets ) ); 1207 } 1208 if ( ! empty( self::$block_css_declarations ) ) { 1209 wp_add_inline_style( $style_tag_id, wp_style_engine_get_stylesheet_from_css_rules( self::$block_css_declarations ) ); 1210 } 1211 wp_enqueue_style( $style_tag_id ); 1212 } 1213 } 1214 1215 /** 1216 * Adds the duotone SVGs and CSS custom properties to the editor settings. 1217 * 1218 * This allows the properties to be pulled in by the EditorStyles component 1219 * in JS and rendered in the post editor. 1220 * 1221 * @since 6.3.0 1222 * 1223 * @param array $settings The block editor settings from the `block_editor_settings_all` filter. 1224 * @return array The editor settings with duotone SVGs and CSS custom properties. 1225 */ 1226 public static function add_editor_settings( $settings ) { 1227 $global_styles_presets = self::get_all_global_styles_presets(); 1228 if ( ! empty( $global_styles_presets ) ) { 1229 if ( ! isset( $settings['styles'] ) ) { 1230 $settings['styles'] = array(); 1231 } 1232 1233 $settings['styles'][] = array( 1234 // For the editor we can add all of the presets by default. 1235 'assets' => self::get_svg_definitions( $global_styles_presets ), 1236 // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. 1237 '__unstableType' => 'svgs', 1238 // These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings. 1239 'isGlobalStyles' => false, 1240 ); 1241 1242 $settings['styles'][] = array( 1243 // For the editor we can add all of the presets by default. 1244 'css' => self::get_global_styles_presets( $global_styles_presets ), 1245 // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component. 1246 '__unstableType' => 'presets', 1247 // 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. 1248 'isGlobalStyles' => false, 1249 ); 1250 } 1251 1252 return $settings; 1253 } 1254 1255 /** 1256 * Migrates the experimental duotone support flag to the stabilized location. 1257 * 1258 * This moves `supports.color.__experimentalDuotone` to `supports.filter.duotone`. 1259 * 1260 * @since 6.3.0 1261 * 1262 * @param array $settings Current block type settings. 1263 * @param array $metadata Block metadata as read in via block.json. 1264 * @return array Filtered block type settings. 1265 */ 1266 public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) { 1267 $duotone_support = isset( $metadata['supports']['color']['__experimentalDuotone'] ) 1268 ? $metadata['supports']['color']['__experimentalDuotone'] 1269 : null; 1270 1271 if ( ! isset( $settings['supports']['filter']['duotone'] ) && null !== $duotone_support ) { 1272 _wp_array_set( $settings, array( 'supports', 'filter', 'duotone' ), (bool) $duotone_support ); 1273 } 1274 1275 return $settings; 1276 } 1277 1278 /** 1279 * Gets the CSS filter property value from a preset. 1280 * 1281 * Exported for the deprecated function wp_get_duotone_filter_id(). 1282 * 1283 * @internal 1284 * 1285 * @since 6.3.0 1286 * @deprecated 6.3.0 1287 * 1288 * @param array $preset The duotone preset. 1289 * @return string The CSS filter property value. 1290 */ 1291 public static function get_filter_css_property_value_from_preset( $preset ) { 1292 _deprecated_function( __FUNCTION__, '6.3.0' ); 1293 1294 if ( isset( $preset['colors'] ) && is_string( $preset['colors'] ) ) { 1295 return $preset['colors']; 1296 } 1297 1298 $filter_id = self::get_filter_id_from_preset( $preset ); 1299 1300 return 'url(#' . $filter_id . ')'; 1301 } 1302 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Apr 27 08:20:02 2024 | Cross-referenced by PHPXref |