PluginProbe
Gutenberg / 10.1.0
Gutenberg v10.1.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
← All changes | lib/block-supports/colors.php +79 -59 23.2.2 → 10.1.0 View file →
@@ -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,100 @@
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 );
76 63
77 64 if (
78 65 is_array( $color_support ) &&
79 - wp_should_skip_block_supports_serialization( $block_type, 'color' )
66 + array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
67 + $color_support['__experimentalSkipSerialization']
80 68 ) {
81 69 return array();
82 70 }
83 71
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();
72 + $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) );
73 + $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) );
74 + $has_link_colors_support = gutenberg_experimental_get( $color_support, array( 'link' ), false );
75 + $has_gradients_support = gutenberg_experimental_get( $color_support, array( 'gradients' ), false );
76 + $classes = array();
77 + $styles = array();
92 78
93 - // Text colors.
79 + // Text Colors.
94 80 // 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;
81 + if ( $has_text_colors_support ) {
82 + $has_named_text_color = array_key_exists( 'textColor', $block_attributes );
83 + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
84 +
85 + // Apply required generic class.
86 + if ( $has_custom_text_color || $has_named_text_color ) {
87 + $classes[] = 'has-text-color';
88 + }
89 + // Apply color class or inline style.
90 + if ( $has_named_text_color ) {
91 + $classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
92 + } elseif ( $has_custom_text_color ) {
93 + $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
94 + }
99 95 }
100 96
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;
97 + // Link Colors.
98 + if ( $has_link_colors_support ) {
99 + $has_link_color = isset( $block_attributes['style']['color']['link'] );
100 + // Apply required class and style.
101 + if ( $has_link_color ) {
102 + $classes[] = 'has-link-color';
103 + // If link is a named color.
104 + if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) {
105 + // Get the name from the string and add proper styles.
106 + $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
107 + $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
108 + $styles[] = sprintf( '--wp--style--color--link: var(--wp--preset--color--%s);', $link_color_name );
109 + } else {
110 + $styles[] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
111 + }
112 + }
106 113 }
107 114
115 + // Background Colors.
116 + if ( $has_background_colors_support ) {
117 + $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes );
118 + $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
119 +
120 + // Apply required background class.
121 + if ( $has_custom_background_color || $has_named_background_color ) {
122 + $classes[] = 'has-background';
123 + }
124 + // Apply background color classes or styles.
125 + if ( $has_named_background_color ) {
126 + $classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
127 + } elseif ( $has_custom_background_color ) {
128 + $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
129 + }
130 + }
131 +
108 132 // 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'] );
133 + if ( $has_gradients_support ) {
134 + $has_named_gradient = array_key_exists( 'gradient', $block_attributes );
135 + $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
114 136
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;
137 + if ( $has_named_gradient || $has_custom_gradient ) {
138 + $classes[] = 'has-background';
139 + }
140 + // Apply required background class.
141 + if ( $has_named_gradient ) {
142 + $classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
143 + } elseif ( $has_custom_gradient ) {
144 + $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
145 + }
123 146 }
124 147
125 148 $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'];
149 + if ( ! empty( $classes ) ) {
150 + $attributes['class'] = implode( ' ', $classes );
130 151 }
131 -
132 - if ( ! empty( $styles['css'] ) ) {
133 - $attributes['style'] = $styles['css'];
152 + if ( ! empty( $styles ) ) {
153 + $attributes['style'] = implode( ' ', $styles );
134 154 }
135 155
136 156 return $attributes;
137 157 }