| @@ -11,33 +11,20 @@ | ||
| 11 | 11 | * @param WP_Block_Type $block_type Block Type. |
| 12 | 12 | */ |
| 13 | 13 | function gutenberg_register_colors_support( $block_type ) { |
| 14 | 14 | $color_support = false; |
| 15 | - if ( $block_type instanceof WP_Block_Type ) { | |
| 16 | - $color_support = $block_type->supports['color'] ?? false; | |
| 15 | + if ( property_exists( $block_type, 'supports' ) ) { | |
| 16 | + $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); | |
| 17 | 17 | } |
| 18 | - $has_text_colors_support = true === $color_support || | |
| 19 | - ( isset( $color_support['text'] ) && $color_support['text'] ) || | |
| 20 | - ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); | |
| 21 | - $has_background_colors_support = true === $color_support || | |
| 22 | - ( isset( $color_support['background'] ) && $color_support['background'] ) || | |
| 23 | - ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); | |
| 24 | - $has_gradients_support = $color_support['gradients'] ?? false; | |
| 25 | - $has_link_colors_support = $color_support['link'] ?? false; | |
| 26 | - $has_button_colors_support = $color_support['button'] ?? false; | |
| 27 | - $has_heading_colors_support = $color_support['heading'] ?? false; | |
| 28 | - $has_color_support = $has_text_colors_support || | |
| 29 | - $has_background_colors_support || | |
| 30 | - $has_gradients_support || | |
| 31 | - $has_link_colors_support || | |
| 32 | - $has_button_colors_support || | |
| 33 | - $has_heading_colors_support; | |
| 18 | + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) ); | |
| 19 | + $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) ); | |
| 20 | + $has_gradients_support = gutenberg_experimental_get( $color_support, array( 'gradients' ), false ); | |
| 34 | 21 | |
| 35 | 22 | if ( ! $block_type->attributes ) { |
| 36 | 23 | $block_type->attributes = array(); |
| 37 | 24 | } |
| 38 | 25 | |
| 39 | - if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 26 | + if ( $has_text_colors_support && ! array_key_exists( 'style', $block_type->attributes ) ) { | |
| 40 | 27 | $block_type->attributes['style'] = array( |
| 41 | 28 | 'type' => 'object', |
| 42 | 29 | ); |
| 43 | 30 | } |
| @@ -71,67 +58,91 @@ | ||
| 71 | 58 | * |
| 72 | 59 | * @return array Colors CSS classes and inline styles. |
| 73 | 60 | */ |
| 74 | 61 | function gutenberg_apply_colors_support( $block_type, $block_attributes ) { |
| 75 | - $color_support = $block_type->supports['color'] ?? false; | |
| 62 | + $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false ); | |
| 63 | + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) ); | |
| 64 | + $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) ); | |
| 65 | + $has_link_colors_support = gutenberg_experimental_get( $color_support, array( 'link' ), false ); | |
| 66 | + $has_gradients_support = gutenberg_experimental_get( $color_support, array( 'gradients' ), false ); | |
| 67 | + $classes = array(); | |
| 68 | + $styles = array(); | |
| 76 | 69 | |
| 77 | - if ( | |
| 78 | - is_array( $color_support ) && | |
| 79 | - wp_should_skip_block_supports_serialization( $block_type, 'color' ) | |
| 80 | - ) { | |
| 81 | - return array(); | |
| 70 | + // Text Colors. | |
| 71 | + // Check support for text colors. | |
| 72 | + if ( $has_text_colors_support ) { | |
| 73 | + $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); | |
| 74 | + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); | |
| 75 | + | |
| 76 | + // Apply required generic class. | |
| 77 | + if ( $has_custom_text_color || $has_named_text_color ) { | |
| 78 | + $classes[] = 'has-text-color'; | |
| 79 | + } | |
| 80 | + // Apply color class or inline style. | |
| 81 | + if ( $has_named_text_color ) { | |
| 82 | + $classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] ); | |
| 83 | + } elseif ( $has_custom_text_color ) { | |
| 84 | + $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); | |
| 85 | + } | |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | - $has_text_colors_support = true === $color_support || | |
| 85 | - ( isset( $color_support['text'] ) && $color_support['text'] ) || | |
| 86 | - ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); | |
| 87 | - $has_background_colors_support = true === $color_support || | |
| 88 | - ( isset( $color_support['background'] ) && $color_support['background'] ) || | |
| 89 | - ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); | |
| 90 | - $has_gradients_support = $color_support['gradients'] ?? false; | |
| 91 | - $color_block_styles = array(); | |
| 88 | + // Link Colors. | |
| 89 | + if ( $has_link_colors_support ) { | |
| 90 | + $has_link_color = isset( $block_attributes['style']['color']['link'] ); | |
| 91 | + // Apply required class and style. | |
| 92 | + if ( $has_link_color ) { | |
| 93 | + $classes[] = 'has-link-color'; | |
| 94 | + // If link is a named color. | |
| 95 | + if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { | |
| 96 | + // Get the name from the string and add proper styles. | |
| 97 | + $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1; | |
| 98 | + $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice ); | |
| 99 | + $styles[] = sprintf( '--wp--style--color--link: var(--wp--preset--color--%s);', $link_color_name ); | |
| 100 | + } else { | |
| 101 | + $styles[] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + } | |
| 92 | 105 | |
| 93 | - // Text colors. | |
| 94 | - // Check support for text colors. | |
| 95 | - if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { | |
| 96 | - $preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; | |
| 97 | - $custom_text_color = $block_attributes['style']['color']['text'] ?? null; | |
| 98 | - $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; | |
| 99 | - } | |
| 106 | + // Background Colors. | |
| 107 | + if ( $has_background_colors_support ) { | |
| 108 | + $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); | |
| 109 | + $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); | |
| 100 | 110 | |
| 101 | - // Background colors. | |
| 102 | - if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { | |
| 103 | - $preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; | |
| 104 | - $custom_background_color = $block_attributes['style']['color']['background'] ?? null; | |
| 105 | - $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; | |
| 111 | + // Apply required background class. | |
| 112 | + if ( $has_custom_background_color || $has_named_background_color ) { | |
| 113 | + $classes[] = 'has-background'; | |
| 114 | + } | |
| 115 | + // Apply background color classes or styles. | |
| 116 | + if ( $has_named_background_color ) { | |
| 117 | + $classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] ); | |
| 118 | + } elseif ( $has_custom_background_color ) { | |
| 119 | + $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); | |
| 120 | + } | |
| 106 | 121 | } |
| 107 | 122 | |
| 108 | 123 | // Gradients. |
| 109 | - // Suppress color.gradient CSS when background.gradient is supported and | |
| 110 | - // explicitly set. background.php owns CSS generation in that case, and | |
| 111 | - // emitting the background shorthand here would conflict with it. | |
| 112 | - $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); | |
| 113 | - $has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] ); | |
| 124 | + if ( $has_gradients_support ) { | |
| 125 | + $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); | |
| 126 | + $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); | |
| 114 | 127 | |
| 115 | - if ( | |
| 116 | - $has_gradients_support && | |
| 117 | - ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) && | |
| 118 | - ! ( $has_background_gradient_support && $has_background_gradient_value ) | |
| 119 | - ) { | |
| 120 | - $preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; | |
| 121 | - $custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null; | |
| 122 | - $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; | |
| 128 | + if ( $has_named_gradient || $has_custom_gradient ) { | |
| 129 | + $classes[] = 'has-background'; | |
| 130 | + } | |
| 131 | + // Apply required background class. | |
| 132 | + if ( $has_named_gradient ) { | |
| 133 | + $classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] ); | |
| 134 | + } elseif ( $has_custom_gradient ) { | |
| 135 | + $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); | |
| 136 | + } | |
| 123 | 137 | } |
| 124 | 138 | |
| 125 | 139 | $attributes = array(); |
| 126 | - $styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); | |
| 127 | - | |
| 128 | - if ( ! empty( $styles['classnames'] ) ) { | |
| 129 | - $attributes['class'] = $styles['classnames']; | |
| 140 | + if ( ! empty( $classes ) ) { | |
| 141 | + $attributes['class'] = implode( ' ', $classes ); | |
| 130 | 142 | } |
| 131 | - | |
| 132 | - if ( ! empty( $styles['css'] ) ) { | |
| 133 | - $attributes['style'] = $styles['css']; | |
| 143 | + if ( ! empty( $styles ) ) { | |
| 144 | + $attributes['style'] = implode( ' ', $styles ); | |
| 134 | 145 | } |
| 135 | 146 | |
| 136 | 147 | return $attributes; |
| 137 | 148 | } |