| @@ -10,12 +10,9 @@ | ||
| 10 | 10 | * |
| 11 | 11 | * @param WP_Block_Type $block_type Block Type. |
| 12 | 12 | */ |
| 13 | 13 | function gutenberg_register_colors_support( $block_type ) { |
| 14 | - $color_support = false; | |
| 15 | - if ( property_exists( $block_type, 'supports' ) ) { | |
| 16 | - $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false ); | |
| 17 | - } | |
| 14 | + $color_support = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( 'color' ), false ) : false; | |
| 18 | 15 | $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) ); |
| 19 | 16 | $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); |
| 20 | 17 | $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false ); |
| 21 | 18 | $has_link_colors_support = _wp_array_get( $color_support, array( 'link' ), false ); |
| @@ -67,10 +64,9 @@ | ||
| 67 | 64 | $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false ); |
| 68 | 65 | |
| 69 | 66 | if ( |
| 70 | 67 | is_array( $color_support ) && |
| 71 | - array_key_exists( '__experimentalSkipSerialization', $color_support ) && | |
| 72 | - $color_support['__experimentalSkipSerialization'] | |
| 68 | + gutenberg_should_skip_block_supports_serialization( $block_type, 'color' ) | |
| 73 | 69 | ) { |
| 74 | 70 | return array(); |
| 75 | 71 | } |
| 76 | 72 | |
| @@ -76,68 +72,42 @@ | ||
| 76 | 72 | |
| 77 | 73 | $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) ); |
| 78 | 74 | $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) ); |
| 79 | 75 | $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false ); |
| 80 | - $classes = array(); | |
| 81 | - $styles = array(); | |
| 76 | + $color_block_styles = array(); | |
| 82 | 77 | |
| 83 | 78 | // Text colors. |
| 84 | 79 | // Check support for text colors. |
| 85 | - if ( $has_text_colors_support ) { | |
| 86 | - $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); | |
| 87 | - $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); | |
| 88 | - | |
| 89 | - // Apply required generic class. | |
| 90 | - if ( $has_custom_text_color || $has_named_text_color ) { | |
| 91 | - $classes[] = 'has-text-color'; | |
| 92 | - } | |
| 93 | - // Apply color class or inline style. | |
| 94 | - if ( $has_named_text_color ) { | |
| 95 | - $classes[] = sprintf( 'has-%s-color', _wp_to_kebab_case( $block_attributes['textColor'] ) ); | |
| 96 | - } elseif ( $has_custom_text_color ) { | |
| 97 | - $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); | |
| 98 | - } | |
| 80 | + if ( $has_text_colors_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { | |
| 81 | + $preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; | |
| 82 | + $custom_text_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'text' ), null ); | |
| 83 | + $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; | |
| 99 | 84 | } |
| 100 | 85 | |
| 101 | 86 | // Background colors. |
| 102 | - if ( $has_background_colors_support ) { | |
| 103 | - $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); | |
| 104 | - $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); | |
| 105 | - | |
| 106 | - // Apply required background class. | |
| 107 | - if ( $has_custom_background_color || $has_named_background_color ) { | |
| 108 | - $classes[] = 'has-background'; | |
| 109 | - } | |
| 110 | - // Apply background color classes or styles. | |
| 111 | - if ( $has_named_background_color ) { | |
| 112 | - $classes[] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $block_attributes['backgroundColor'] ) ); | |
| 113 | - } elseif ( $has_custom_background_color ) { | |
| 114 | - $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); | |
| 115 | - } | |
| 87 | + if ( $has_background_colors_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { | |
| 88 | + $preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; | |
| 89 | + $custom_background_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'background' ), null ); | |
| 90 | + $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; | |
| 116 | 91 | } |
| 117 | 92 | |
| 118 | 93 | // Gradients. |
| 119 | - if ( $has_gradients_support ) { | |
| 120 | - $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); | |
| 121 | - $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); | |
| 122 | 94 | |
| 123 | - if ( $has_named_gradient || $has_custom_gradient ) { | |
| 124 | - $classes[] = 'has-background'; | |
| 125 | - } | |
| 126 | - // Apply required background class. | |
| 127 | - if ( $has_named_gradient ) { | |
| 128 | - $classes[] = sprintf( 'has-%s-gradient-background', _wp_to_kebab_case( $block_attributes['gradient'] ) ); | |
| 129 | - } elseif ( $has_custom_gradient ) { | |
| 130 | - $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); | |
| 131 | - } | |
| 95 | + if ( $has_gradients_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) { | |
| 96 | + $preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; | |
| 97 | + $custom_gradient_color = _wp_array_get( $block_attributes, array( 'style', 'color', 'gradient' ), null ); | |
| 98 | + $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; | |
| 132 | 99 | } |
| 133 | 100 | |
| 134 | 101 | $attributes = array(); |
| 135 | - if ( ! empty( $classes ) ) { | |
| 136 | - $attributes['class'] = implode( ' ', $classes ); | |
| 102 | + $styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); | |
| 103 | + | |
| 104 | + if ( ! empty( $styles['classnames'] ) ) { | |
| 105 | + $attributes['class'] = $styles['classnames']; | |
| 137 | 106 | } |
| 138 | - if ( ! empty( $styles ) ) { | |
| 139 | - $attributes['style'] = implode( ' ', $styles ); | |
| 107 | + | |
| 108 | + if ( ! empty( $styles['css'] ) ) { | |
| 109 | + $attributes['style'] = $styles['css']; | |
| 140 | 110 | } |
| 141 | 111 | |
| 142 | 112 | return $attributes; |
| 143 | 113 | } |