| @@ -47,11 +47,11 @@ | ||
| 47 | 47 | * 'core/featured-image' => 'blue-orange', |
| 48 | 48 | * … |
| 49 | 49 | * ] |
| 50 | 50 | * |
| 51 | - * @var ?array<string, string> | |
| 51 | + * @var array | |
| 52 | 52 | */ |
| 53 | - private static $global_styles_block_names; | |
| 53 | + private static $global_styles_block_names = array(); | |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * An array of duotone filter data from global, theme, and custom presets. |
| 57 | 57 | * |
| @@ -67,11 +67,11 @@ | ||
| 67 | 67 | * ], |
| 68 | 68 | * … |
| 69 | 69 | * ] |
| 70 | 70 | * |
| 71 | - * @var ?array<string, array<string, string|string[]>> | |
| 71 | + * @var array | |
| 72 | 72 | */ |
| 73 | - private static $global_styles_presets; | |
| 73 | + private static $global_styles_presets = array(); | |
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * All of the duotone filter data from presets for CSS custom properties on |
| 77 | 77 | * the page. |
| @@ -131,9 +131,9 @@ | ||
| 131 | 131 | /** |
| 132 | 132 | * Direct port of colord's clamp function. Using min/max instead of |
| 133 | 133 | * nested ternaries. |
| 134 | 134 | * |
| 135 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L23 | |
| 135 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L23 | |
| 136 | 136 | * |
| 137 | 137 | * @param float $number The number to clamp. |
| 138 | 138 | * @param float $min The minimum value. |
| 139 | 139 | * @param float $max The maximum value. |
| @@ -145,9 +145,9 @@ | ||
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | 147 | * Direct port of colord's clampHue function. |
| 148 | 148 | * |
| 149 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L32 | |
| 149 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L32 | |
| 150 | 150 | * |
| 151 | 151 | * @param float $degrees The hue to clamp. |
| 152 | 152 | * @return float The clamped hue. |
| 153 | 153 | */ |
| @@ -158,9 +158,9 @@ | ||
| 158 | 158 | |
| 159 | 159 | /** |
| 160 | 160 | * Direct port of colord's parseHue function. |
| 161 | 161 | * |
| 162 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L40 | |
| 162 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/helpers.ts#L40 | |
| 163 | 163 | * |
| 164 | 164 | * @param float $value The hue value to parse. |
| 165 | 165 | * @param string $unit The unit of the hue value. |
| 166 | 166 | * @return float The parsed hue value. |
| @@ -182,9 +182,9 @@ | ||
| 182 | 182 | |
| 183 | 183 | /** |
| 184 | 184 | * Direct port of colord's parseHex function. |
| 185 | 185 | * |
| 186 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hex.ts#L8 | |
| 186 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hex.ts#L8 | |
| 187 | 187 | * |
| 188 | 188 | * @param string $hex The hex string to parse. |
| 189 | 189 | * @return array|null An array of RGBA values or null if the hex string is invalid. |
| 190 | 190 | */ |
| @@ -224,9 +224,9 @@ | ||
| 224 | 224 | |
| 225 | 225 | /** |
| 226 | 226 | * Direct port of colord's clampRgba function. |
| 227 | 227 | * |
| 228 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgb.ts#L5 | |
| 228 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgb.ts#L5 | |
| 229 | 229 | * |
| 230 | 230 | * @param array $rgba The RGBA array to clamp. |
| 231 | 231 | * @return array The clamped RGBA array. |
| 232 | 232 | */ |
| @@ -241,9 +241,9 @@ | ||
| 241 | 241 | |
| 242 | 242 | /** |
| 243 | 243 | * Direct port of colord's parseRgbaString function. |
| 244 | 244 | * |
| 245 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgbString.ts#L18 | |
| 245 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/rgbString.ts#L18 | |
| 246 | 246 | * |
| 247 | 247 | * @param string $input The RGBA string to parse. |
| 248 | 248 | * @return array|null An array of RGBA values or null if the RGB string is invalid. |
| 249 | 249 | */ |
| @@ -292,9 +292,9 @@ | ||
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | 294 | * Direct port of colord's clampHsla function. |
| 295 | 295 | * |
| 296 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L6 | |
| 296 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L6 | |
| 297 | 297 | * |
| 298 | 298 | * @param array $hsla The HSLA array to clamp. |
| 299 | 299 | * @return array The clamped HSLA array. |
| 300 | 300 | */ |
| @@ -309,9 +309,9 @@ | ||
| 309 | 309 | |
| 310 | 310 | /** |
| 311 | 311 | * Direct port of colord's hsvaToRgba function. |
| 312 | 312 | * |
| 313 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsv.ts#L52 | |
| 313 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsv.ts#L52 | |
| 314 | 314 | * |
| 315 | 315 | * @param array $hsva The HSVA array to convert. |
| 316 | 316 | * @return array The RGBA array. |
| 317 | 317 | */ |
| @@ -337,9 +337,9 @@ | ||
| 337 | 337 | |
| 338 | 338 | /** |
| 339 | 339 | * Direct port of colord's hslaToHsva function. |
| 340 | 340 | * |
| 341 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L33 | |
| 341 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L33 | |
| 342 | 342 | * |
| 343 | 343 | * @param array $hsla The HSLA array to convert. |
| 344 | 344 | * @return array The HSVA array. |
| 345 | 345 | */ |
| @@ -361,9 +361,9 @@ | ||
| 361 | 361 | |
| 362 | 362 | /** |
| 363 | 363 | * Direct port of colord's hslaToRgba function. |
| 364 | 364 | * |
| 365 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L55 | |
| 365 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hsl.ts#L55 | |
| 366 | 366 | * |
| 367 | 367 | * @param array $hsla The HSLA array to convert. |
| 368 | 368 | * @return array The RGBA array. |
| 369 | 369 | */ |
| @@ -373,9 +373,9 @@ | ||
| 373 | 373 | |
| 374 | 374 | /** |
| 375 | 375 | * Direct port of colord's parseHslaString function. |
| 376 | 376 | * |
| 377 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hslString.ts#L17 | |
| 377 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/colorModels/hslString.ts#L17 | |
| 378 | 378 | * |
| 379 | 379 | * @param string $input The HSLA string to parse. |
| 380 | 380 | * @return array|null An array of RGBA values or null if the RGB string is invalid. |
| 381 | 381 | */ |
| @@ -423,9 +423,9 @@ | ||
| 423 | 423 | /** |
| 424 | 424 | * Direct port of colord's parse function simplified for our use case. This |
| 425 | 425 | * version only supports string parsing and only returns RGBA values. |
| 426 | 426 | * |
| 427 | - * @link https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/parse.ts#L37 | |
| 427 | + * @see https://github.com/omgovich/colord/blob/3f859e03b0ca622eb15480f611371a0f15c9427f/src/parse.ts#L37 | |
| 428 | 428 | * |
| 429 | 429 | * @param string $input The string to parse. |
| 430 | 430 | * @return array|null An array of RGBA values or null if the string is invalid. |
| 431 | 431 | */ |
| @@ -447,16 +447,12 @@ | ||
| 447 | 447 | * Take the inline CSS duotone variable from a block and return the slug. Handles styles slugs like: |
| 448 | 448 | * var:preset|duotone|blue-orange |
| 449 | 449 | * var(--wp--preset--duotone--blue-orange) |
| 450 | 450 | * |
| 451 | - * @param string|string[] $duotone_attr The duotone attribute from a block. | |
| 452 | - * @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed). | |
| 451 | + * @param string $duotone_attr The duotone attribute from a block. | |
| 452 | + * @return string The slug of the duotone preset or an empty string if no slug is found. | |
| 453 | 453 | */ |
| 454 | 454 | private static function get_slug_from_attribute( $duotone_attr ) { |
| 455 | - if ( ! is_string( $duotone_attr ) ) { | |
| 456 | - return ''; | |
| 457 | - } | |
| 458 | - | |
| 459 | 455 | // Uses Branch Reset Groups `(?|…)` to return one capture group. |
| 460 | 456 | preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches ); |
| 461 | 457 | |
| 462 | 458 | return ! empty( $matches[1] ) ? $matches[1] : ''; |
| @@ -464,20 +460,16 @@ | ||
| 464 | 460 | |
| 465 | 461 | /** |
| 466 | 462 | * Check if we have a valid duotone preset. |
| 467 | 463 | * |
| 468 | - * @param string|string[] $duotone_attr The duotone attribute from a block. | |
| 464 | + * @param string $duotone_attr The duotone attribute from a block. | |
| 469 | 465 | * @return bool True if the duotone preset present and valid. |
| 470 | 466 | */ |
| 471 | 467 | private static function is_preset( $duotone_attr ) { |
| 472 | - if ( ! is_string( $duotone_attr ) ) { | |
| 473 | - return false; | |
| 474 | - } | |
| 475 | - | |
| 476 | 468 | $slug = self::get_slug_from_attribute( $duotone_attr ); |
| 477 | 469 | $filter_id = self::get_filter_id( $slug ); |
| 478 | 470 | |
| 479 | - return array_key_exists( $filter_id, self::get_all_global_styles_presets() ); | |
| 471 | + return array_key_exists( $filter_id, self::$global_styles_presets ); | |
| 480 | 472 | } |
| 481 | 473 | |
| 482 | 474 | /** |
| 483 | 475 | * Get the CSS variable name for a duotone preset. |
| @@ -485,9 +477,9 @@ | ||
| 485 | 477 | * @param string $slug The slug of the duotone preset. |
| 486 | 478 | * @return string The CSS variable name. |
| 487 | 479 | */ |
| 488 | 480 | private static function get_css_custom_property_name( $slug ) { |
| 489 | - return "--wp--preset--duotone--$slug"; | |
| 481 | + return "--wp--preset--duotone--$slug"; | |
| 490 | 482 | } |
| 491 | 483 | |
| 492 | 484 | /** |
| 493 | 485 | * Get the ID of the duotone filter. |
| @@ -495,9 +487,9 @@ | ||
| 495 | 487 | * @param string $slug The slug of the duotone preset. |
| 496 | 488 | * @return string The ID of the duotone filter. |
| 497 | 489 | */ |
| 498 | 490 | private static function get_filter_id( $slug ) { |
| 499 | - return "wp-duotone-$slug"; | |
| 491 | + return "wp-duotone-$slug"; | |
| 500 | 492 | } |
| 501 | 493 | |
| 502 | 494 | /** |
| 503 | 495 | * Get the CSS variable for a duotone preset. |
| @@ -630,9 +622,9 @@ | ||
| 630 | 622 | * @param array $sources The duotone presets. |
| 631 | 623 | * @return string The CSS for global styles. |
| 632 | 624 | */ |
| 633 | 625 | private static function get_global_styles_presets( $sources ) { |
| 634 | - $css = WP_Theme_JSON_Gutenberg::ROOT_CSS_PROPERTIES_SELECTOR . '{'; | |
| 626 | + $css = 'body{'; | |
| 635 | 627 | foreach ( $sources as $filter_id => $filter_data ) { |
| 636 | 628 | $slug = $filter_data['slug']; |
| 637 | 629 | $colors = $filter_data['colors']; |
| 638 | 630 | $css_property_name = self::get_css_custom_property_name( $slug ); |
| @@ -645,43 +637,39 @@ | ||
| 645 | 637 | |
| 646 | 638 | /** |
| 647 | 639 | * Get the CSS selector for a block type. |
| 648 | 640 | * |
| 649 | - * @param WP_Block_Type $block_type Block type to check for support. | |
| 641 | + * @param string $block_name The block name. | |
| 650 | 642 | * |
| 651 | - * @return string|null The CSS selector. Null if there is no support or the $block_type is not a WP_Block_Type. | |
| 643 | + * @return string The CSS selector or null if there is no support. | |
| 652 | 644 | */ |
| 653 | - private static function get_selector( $block_type ) { | |
| 654 | - if ( ! ( $block_type instanceof WP_Block_Type ) ) { | |
| 655 | - return null; | |
| 656 | - } | |
| 645 | + private static function get_selector( $block_name ) { | |
| 646 | + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); | |
| 657 | 647 | |
| 658 | - /* | |
| 659 | - * Backward compatibility with `supports.color.__experimentalDuotone` | |
| 660 | - * is provided via the `block_type_metadata_settings` filter. If | |
| 661 | - * `supports.filter.duotone` has not been set and the experimental | |
| 662 | - * property has been, the experimental property value is copied into | |
| 663 | - * `supports.filter.duotone`. | |
| 664 | - */ | |
| 665 | - $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) ); | |
| 666 | - if ( ! $duotone_support ) { | |
| 667 | - return null; | |
| 668 | - } | |
| 648 | + if ( $block_type && property_exists( $block_type, 'supports' ) ) { | |
| 649 | + // Backwards compatibility with `supports.color.__experimentalDuotone` | |
| 650 | + // is provided via the `block_type_metadata_settings` filter. If | |
| 651 | + // `supports.filter.duotone` has not been set and the experimental | |
| 652 | + // property has been, the experimental property value is copied into | |
| 653 | + // `supports.filter.duotone`. | |
| 654 | + $duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false ); | |
| 655 | + if ( ! $duotone_support ) { | |
| 656 | + return null; | |
| 657 | + } | |
| 669 | 658 | |
| 670 | - /* | |
| 671 | - * If the experimental duotone support was set, that value is to be | |
| 672 | - * treated as a selector and requires scoping. | |
| 673 | - */ | |
| 674 | - $experimental_duotone = $block_type->supports['color']['__experimentalDuotone'] ?? false; | |
| 675 | - if ( $experimental_duotone ) { | |
| 676 | - $root_selector = wp_get_block_css_selector( $block_type ); | |
| 677 | - return is_string( $experimental_duotone ) | |
| 678 | - ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone ) | |
| 679 | - : $root_selector; | |
| 659 | + // If the experimental duotone support was set, that value is to be | |
| 660 | + // treated as a selector and requires scoping. | |
| 661 | + $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false ); | |
| 662 | + if ( $experimental_duotone ) { | |
| 663 | + $root_selector = wp_get_block_css_selector( $block_type ); | |
| 664 | + return is_string( $experimental_duotone ) | |
| 665 | + ? WP_Theme_JSON_Gutenberg::scope_selector( $root_selector, $experimental_duotone ) | |
| 666 | + : $root_selector; | |
| 667 | + } | |
| 668 | + | |
| 669 | + // Regular filter.duotone support uses filter.duotone selectors with fallbacks. | |
| 670 | + return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); | |
| 680 | 671 | } |
| 681 | - | |
| 682 | - // Regular filter.duotone support uses filter.duotone selectors with fallbacks. | |
| 683 | - return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true ); | |
| 684 | 672 | } |
| 685 | 673 | |
| 686 | 674 | /** |
| 687 | 675 | * Enqueue a block CSS declaration for the page. |
| @@ -732,10 +720,9 @@ | ||
| 732 | 720 | * @param string $duotone_selector The block's duotone selector. e.g. '.wp-block-image img'. |
| 733 | 721 | * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'. |
| 734 | 722 | */ |
| 735 | 723 | private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) { |
| 736 | - $global_styles_presets = self::get_all_global_styles_presets(); | |
| 737 | - if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) { | |
| 724 | + if ( ! array_key_exists( $filter_id, self::$global_styles_presets ) ) { | |
| 738 | 725 | $error_message = sprintf( |
| 739 | 726 | /* translators: %s: duotone filter ID */ |
| 740 | 727 | __( 'The duotone id "%s" is not registered in theme.json settings', 'gutenberg' ), |
| 741 | 728 | $filter_id |
| @@ -742,10 +729,10 @@ | ||
| 742 | 729 | ); |
| 743 | 730 | _doing_it_wrong( __METHOD__, $error_message, '6.3.0' ); |
| 744 | 731 | return; |
| 745 | 732 | } |
| 746 | - self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ]; | |
| 747 | - self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] ); | |
| 733 | + self::$used_global_styles_presets[ $filter_id ] = self::$global_styles_presets[ $filter_id ]; | |
| 734 | + self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, self::$global_styles_presets[ $filter_id ] ); | |
| 748 | 735 | } |
| 749 | 736 | |
| 750 | 737 | /** |
| 751 | 738 | * Registers the style and colors block attributes for block types that support it. |
| @@ -754,11 +741,16 @@ | ||
| 754 | 741 | * |
| 755 | 742 | * @param WP_Block_Type $block_type Block Type. |
| 756 | 743 | */ |
| 757 | 744 | public static function register_duotone_support( $block_type ) { |
| 758 | - // Previous `color.__experimentalDuotone` support flag is migrated | |
| 759 | - // to `filter.duotone` via `block_type_metadata_settings` filter. | |
| 760 | - if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) { | |
| 745 | + $has_duotone_support = false; | |
| 746 | + if ( property_exists( $block_type, 'supports' ) ) { | |
| 747 | + // Previous `color.__experimentalDuotone` support flag is migrated | |
| 748 | + // to `filter.duotone` via `block_type_metadata_settings` filter. | |
| 749 | + $has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), null ); | |
| 750 | + } | |
| 751 | + | |
| 752 | + if ( $has_duotone_support ) { | |
| 761 | 753 | if ( ! $block_type->attributes ) { |
| 762 | 754 | $block_type->attributes = array(); |
| 763 | 755 | } |
| 764 | 756 | |
| @@ -774,21 +766,14 @@ | ||
| 774 | 766 | * Get all possible duotone presets from global and theme styles and store as slug => [ colors array ] |
| 775 | 767 | * We only want to process this one time. On block render we'll access and output only the needed presets for that page. |
| 776 | 768 | * |
| 777 | 769 | * @since 6.3.0 |
| 778 | - * | |
| 779 | - * @return array<string, array<string, string|string[]>> An array of global styles presets, keyed on the filter ID. | |
| 780 | 770 | */ |
| 781 | - private static function get_all_global_styles_presets() { | |
| 782 | - if ( isset( self::$global_styles_presets ) ) { | |
| 783 | - return self::$global_styles_presets; | |
| 784 | - } | |
| 785 | - | |
| 771 | + public static function set_global_styles_presets() { | |
| 786 | 772 | // Get the per block settings from the theme.json. |
| 787 | 773 | $tree = gutenberg_get_global_settings(); |
| 788 | - $presets_by_origin = $tree['color']['duotone'] ?? array(); | |
| 774 | + $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() ); | |
| 789 | 775 | |
| 790 | - self::$global_styles_presets = array(); | |
| 791 | 776 | foreach ( $presets_by_origin as $presets ) { |
| 792 | 777 | foreach ( $presets as $preset ) { |
| 793 | 778 | $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); |
| 794 | 779 | |
| @@ -794,44 +779,21 @@ | ||
| 794 | 779 | |
| 795 | 780 | self::$global_styles_presets[ $filter_id ] = $preset; |
| 796 | 781 | } |
| 797 | 782 | } |
| 798 | - | |
| 799 | - return self::$global_styles_presets; | |
| 800 | 783 | } |
| 801 | 784 | |
| 802 | 785 | /** |
| 803 | - * Ensure all possible duotone presets are ready. | |
| 804 | - * This function is deprecated, as it's no longer necessary to handle this manually. | |
| 805 | - * It is kept to ensure external users of this method don't break. | |
| 806 | - * | |
| 807 | - * @since 6.3.0 | |
| 808 | - * @deprecated | |
| 809 | - */ | |
| 810 | - public static function set_global_styles_presets() { | |
| 811 | - _deprecated_function( __METHOD__, 'Gutenberg 22.5.0' ); | |
| 812 | - self::get_all_global_styles_presets(); | |
| 813 | - } | |
| 814 | - | |
| 815 | - /** | |
| 816 | 786 | * Scrape all block names from global styles and store in self::$global_styles_block_names |
| 817 | 787 | * |
| 818 | 788 | * @since 6.3.0 |
| 819 | - * | |
| 820 | - * @return null|array<string, string> An array of global style block slugs, keyed on the block name. | |
| 821 | 789 | */ |
| 822 | - private static function get_all_global_style_block_names() { | |
| 823 | - if ( isset( self::$global_styles_block_names ) ) { | |
| 824 | - return self::$global_styles_block_names; | |
| 825 | - } | |
| 826 | - | |
| 790 | + public static function set_global_style_block_names() { | |
| 827 | 791 | // Get the per block settings from the theme.json. |
| 828 | 792 | $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); |
| 829 | 793 | $block_nodes = $tree->get_styles_block_nodes(); |
| 830 | 794 | $theme_json = $tree->get_raw_data(); |
| 831 | 795 | |
| 832 | - self::$global_styles_block_names = array(); | |
| 833 | - | |
| 834 | 796 | foreach ( $block_nodes as $block_node ) { |
| 835 | 797 | // This block definition doesn't include any duotone settings. Skip it. |
| 836 | 798 | if ( empty( $block_node['duotone'] ) ) { |
| 837 | 799 | continue; |
| @@ -844,13 +806,8 @@ | ||
| 844 | 806 | if ( empty( $duotone_attr ) ) { |
| 845 | 807 | continue; |
| 846 | 808 | } |
| 847 | 809 | // If it has a duotone filter preset, save the block name and the preset slug. |
| 848 | - // Only process if it's a string (preset reference), not an array (custom colors). | |
| 849 | - if ( ! is_string( $duotone_attr ) ) { | |
| 850 | - continue; | |
| 851 | - } | |
| 852 | - | |
| 853 | 810 | $slug = self::get_slug_from_attribute( $duotone_attr ); |
| 854 | 811 | |
| 855 | 812 | if ( $slug && $slug !== $duotone_attr ) { |
| 856 | 813 | self::$global_styles_block_names[ $block_node['name'] ] = $slug; |
| @@ -855,52 +812,31 @@ | ||
| 855 | 812 | if ( $slug && $slug !== $duotone_attr ) { |
| 856 | 813 | self::$global_styles_block_names[ $block_node['name'] ] = $slug; |
| 857 | 814 | } |
| 858 | 815 | } |
| 859 | - | |
| 860 | - return self::$global_styles_block_names; | |
| 861 | 816 | } |
| 862 | 817 | |
| 863 | 818 | /** |
| 864 | - * Ensure all possible global style block names are ready. | |
| 865 | - * This function is deprecated, as it's no longer necessary to handle this manually. | |
| 866 | - * It is kept to ensure external users of this method don't break. | |
| 867 | - * | |
| 868 | - * @since 6.3.0 | |
| 869 | - * @deprecated | |
| 870 | - */ | |
| 871 | - public static function set_global_style_block_names() { | |
| 872 | - _deprecated_function( __METHOD__, 'Gutenberg 22.5.0' ); | |
| 873 | - self::get_all_global_style_block_names(); | |
| 874 | - } | |
| 875 | - | |
| 876 | - /** | |
| 877 | 819 | * Render out the duotone CSS styles and SVG. |
| 878 | 820 | * |
| 879 | 821 | * @since 6.3.0 |
| 880 | 822 | * |
| 881 | - * @param string $block_content Rendered block content. | |
| 882 | - * @param array $block Block object. | |
| 883 | - * @param WP_Block $wp_block The block instance. | |
| 884 | - * @return string Filtered block content. | |
| 823 | + * @param string $block_content Rendered block content. | |
| 824 | + * @param array $block Block object. | |
| 825 | + * @return string Filtered block content. | |
| 885 | 826 | */ |
| 886 | - public static function render_duotone_support( $block_content, $block, $wp_block ) { | |
| 887 | - if ( ! $block['blockName'] ) { | |
| 888 | - return $block_content; | |
| 889 | - } | |
| 890 | - $duotone_selector = self::get_selector( $wp_block->block_type ); | |
| 827 | + public static function render_duotone_support( $block_content, $block ) { | |
| 828 | + $duotone_selector = self::get_selector( $block['blockName'] ); | |
| 891 | 829 | |
| 892 | - if ( ! $duotone_selector ) { | |
| 893 | - return $block_content; | |
| 894 | - } | |
| 895 | - | |
| 896 | - $global_styles_block_names = self::get_all_global_style_block_names(); | |
| 897 | - | |
| 898 | 830 | // The block should have a duotone attribute or have duotone defined in its theme.json to be processed. |
| 899 | 831 | $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] ); |
| 900 | - $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names ); | |
| 832 | + $has_global_styles_duotone = array_key_exists( $block['blockName'], self::$global_styles_block_names ); | |
| 901 | 833 | |
| 902 | - if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) { | |
| 834 | + if ( | |
| 835 | + empty( $block_content ) || | |
| 836 | + ! $duotone_selector || | |
| 837 | + ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) | |
| 838 | + ) { | |
| 903 | 839 | return $block_content; |
| 904 | 840 | } |
| 905 | 841 | |
| 906 | 842 | // Generate the pieces needed for rendering a duotone to the page. |
| @@ -943,9 +879,9 @@ | ||
| 943 | 879 | // SVG filter and block CSS. |
| 944 | 880 | self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ); |
| 945 | 881 | } |
| 946 | 882 | } elseif ( $has_global_styles_duotone ) { |
| 947 | - $slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. | |
| 883 | + $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. | |
| 948 | 884 | $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. |
| 949 | 885 | $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. |
| 950 | 886 | |
| 951 | 887 | // CSS custom property, SVG filter, and block CSS. |
| @@ -953,9 +889,9 @@ | ||
| 953 | 889 | } |
| 954 | 890 | |
| 955 | 891 | // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper. |
| 956 | 892 | $tags = new WP_HTML_Tag_Processor( $block_content ); |
| 957 | - if ( $tags->next_tag() && isset( $filter_id ) ) { | |
| 893 | + if ( $tags->next_tag() ) { | |
| 958 | 894 | $tags->add_class( $filter_id ); |
| 959 | 895 | } |
| 960 | 896 | |
| 961 | 897 | return $tags->get_updated_html(); |
| @@ -961,46 +897,8 @@ | ||
| 961 | 897 | return $tags->get_updated_html(); |
| 962 | 898 | } |
| 963 | 899 | |
| 964 | 900 | /** |
| 965 | - * Fixes the issue with our generated class name not being added to the block's outer container | |
| 966 | - * in classic themes due to gutenberg_restore_image_outer_container from layout block supports. | |
| 967 | - * | |
| 968 | - * @since 6.5.0 | |
| 969 | - * | |
| 970 | - * @param string $block_content Rendered block content. | |
| 971 | - * @return string Filtered block content. | |
| 972 | - */ | |
| 973 | - public static function restore_image_outer_container( $block_content ) { | |
| 974 | - if ( wp_theme_has_theme_json() ) { | |
| 975 | - return $block_content; | |
| 976 | - } | |
| 977 | - | |
| 978 | - $tags = new WP_HTML_Tag_Processor( $block_content ); | |
| 979 | - $wrapper_query = array( | |
| 980 | - 'tag_name' => 'div', | |
| 981 | - 'class_name' => 'wp-block-image', | |
| 982 | - ); | |
| 983 | - if ( ! $tags->next_tag( $wrapper_query ) ) { | |
| 984 | - return $block_content; | |
| 985 | - } | |
| 986 | - | |
| 987 | - $tags->set_bookmark( 'wrapper-div' ); | |
| 988 | - $tags->next_tag(); | |
| 989 | - | |
| 990 | - foreach ( $tags->class_list() as $classname ) { | |
| 991 | - if ( str_starts_with( $classname, 'wp-duotone' ) ) { | |
| 992 | - $tags->remove_class( $classname ); | |
| 993 | - $tags->seek( 'wrapper-div' ); | |
| 994 | - $tags->add_class( $classname ); | |
| 995 | - break; | |
| 996 | - } | |
| 997 | - } | |
| 998 | - | |
| 999 | - return $tags->get_updated_html(); | |
| 1000 | - } | |
| 1001 | - | |
| 1002 | - /** | |
| 1003 | 901 | * Appends the used block duotone filter declarations to the inline block supports CSS. |
| 1004 | 902 | * |
| 1005 | 903 | * @since 6.3.0 |
| 1006 | 904 | */ |
| @@ -1036,19 +934,11 @@ | ||
| 1036 | 934 | if ( ! empty( self::$used_svg_filter_data ) ) { |
| 1037 | 935 | echo self::get_svg_definitions( self::$used_svg_filter_data ); |
| 1038 | 936 | } |
| 1039 | 937 | |
| 1040 | - // In block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. | |
| 1041 | - if ( ! wp_is_block_theme() ) { | |
| 1042 | - $style_tag_id = 'core-block-supports-duotone'; | |
| 1043 | - wp_register_style( $style_tag_id, false ); | |
| 1044 | - if ( ! empty( self::$used_global_styles_presets ) ) { | |
| 1045 | - wp_add_inline_style( $style_tag_id, self::get_global_styles_presets( self::$used_global_styles_presets ) ); | |
| 1046 | - } | |
| 1047 | - if ( ! empty( self::$block_css_declarations ) ) { | |
| 1048 | - wp_add_inline_style( $style_tag_id, gutenberg_style_engine_get_stylesheet_from_css_rules( self::$block_css_declarations ) ); | |
| 1049 | - } | |
| 1050 | - wp_enqueue_style( $style_tag_id ); | |
| 938 | + // This is for classic themes - in block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. | |
| 939 | + if ( ! wp_is_block_theme() && ! empty( self::$used_global_styles_presets ) ) { | |
| 940 | + wp_add_inline_style( 'core-block-supports', self::get_global_styles_presets( self::$used_global_styles_presets ) ); | |
| 1051 | 941 | } |
| 1052 | 942 | } |
| 1053 | 943 | |
| 1054 | 944 | /** |
| @@ -1061,10 +951,9 @@ | ||
| 1061 | 951 | * @param array $settings The block editor settings from the `block_editor_settings_all` filter. |
| 1062 | 952 | * @return array The editor settings with duotone SVGs and CSS custom properties. |
| 1063 | 953 | */ |
| 1064 | 954 | public static function add_editor_settings( $settings ) { |
| 1065 | - $global_styles_presets = self::get_all_global_styles_presets(); | |
| 1066 | - if ( ! empty( $global_styles_presets ) ) { | |
| 955 | + if ( ! empty( self::$global_styles_presets ) ) { | |
| 1067 | 956 | if ( ! isset( $settings['styles'] ) ) { |
| 1068 | 957 | $settings['styles'] = array(); |
| 1069 | 958 | } |
| 1070 | 959 | |
| @@ -1069,9 +958,9 @@ | ||
| 1069 | 958 | } |
| 1070 | 959 | |
| 1071 | 960 | $settings['styles'][] = array( |
| 1072 | 961 | // For the editor we can add all of the presets by default. |
| 1073 | - 'assets' => self::get_svg_definitions( $global_styles_presets ), | |
| 962 | + 'assets' => self::get_svg_definitions( self::$global_styles_presets ), | |
| 1074 | 963 | // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. |
| 1075 | 964 | '__unstableType' => 'svgs', |
| 1076 | 965 | // These styles not generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. |
| 1077 | 966 | 'isGlobalStyles' => false, |
| @@ -1078,9 +967,9 @@ | ||
| 1078 | 967 | ); |
| 1079 | 968 | |
| 1080 | 969 | $settings['styles'][] = array( |
| 1081 | 970 | // For the editor we can add all of the presets by default. |
| 1082 | - 'css' => self::get_global_styles_presets( $global_styles_presets ), | |
| 971 | + 'css' => self::get_global_styles_presets( self::$global_styles_presets ), | |
| 1083 | 972 | // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component. |
| 1084 | 973 | '__unstableType' => 'presets', |
| 1085 | 974 | // These styles are no longer generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. |
| 1086 | 975 | 'isGlobalStyles' => false, |
| @@ -1101,9 +990,9 @@ | ||
| 1101 | 990 | * |
| 1102 | 991 | * @return array Filtered block type settings. |
| 1103 | 992 | */ |
| 1104 | 993 | public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) { |
| 1105 | - $duotone_support = $metadata['supports']['color']['__experimentalDuotone'] ?? null; | |
| 994 | + $duotone_support = _wp_array_get( $metadata, array( 'supports', 'color', '__experimentalDuotone' ), null ); | |
| 1106 | 995 | |
| 1107 | 996 | if ( ! isset( $settings['supports']['filter']['duotone'] ) && null !== $duotone_support ) { |
| 1108 | 997 | _wp_array_set( $settings, array( 'supports', 'filter', 'duotone' ), (bool) $duotone_support ); |
| 1109 | 998 | } |