PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 7.4.0 All 402 releases
gutenberg / lib / block-supports / colors.php

colors.php in Gutenberg 12.6.0, at lib/block-supports/colors.php

153 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Colors block support flag.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Registers the style and colors block attributes for block types that support it.
10 *
11 * @param WP_Block_Type $block_type Block Type.
12 */
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 }
18 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
19 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
20 $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
21 $has_link_colors_support = _wp_array_get( $color_support, array( 'link' ), false );
22 $has_color_support = $has_text_colors_support ||
23 $has_background_colors_support ||
24 $has_gradients_support ||
25 $has_link_colors_support;
26
27 if ( ! $block_type->attributes ) {
28 $block_type->attributes = array();
29 }
30
31 if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
32 $block_type->attributes['style'] = array(
33 'type' => 'object',
34 );
35 }
36
37 if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) {
38 $block_type->attributes['backgroundColor'] = array(
39 'type' => 'string',
40 );
41 }
42
43 if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) {
44 $block_type->attributes['textColor'] = array(
45 'type' => 'string',
46 );
47 }
48
49 if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) {
50 $block_type->attributes['gradient'] = array(
51 'type' => 'string',
52 );
53 }
54 }
55
56
57 /**
58 * Add CSS classes and inline styles for colors to the incoming attributes array.
59 * This will be applied to the block markup in the front-end.
60 *
61 * @param WP_Block_Type $block_type Block type.
62 * @param array $block_attributes Block attributes.
63 *
64 * @return array Colors CSS classes and inline styles.
65 */
66 function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
67 $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
68
69 if (
70 is_array( $color_support ) &&
71 array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
72 $color_support['__experimentalSkipSerialization']
73 ) {
74 return array();
75 }
76
77 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
78 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
79 $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
80 $classes = array();
81 $styles = array();
82
83 // Text colors.
84 // 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 }
99 }
100
101 // 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 }
116 }
117
118 // 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
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 }
132 }
133
134 $attributes = array();
135 if ( ! empty( $classes ) ) {
136 $attributes['class'] = implode( ' ', $classes );
137 }
138 if ( ! empty( $styles ) ) {
139 $attributes['style'] = implode( ' ', $styles );
140 }
141
142 return $attributes;
143 }
144
145 // Register the block support.
146 WP_Block_Supports::get_instance()->register(
147 'colors',
148 array(
149 'register_attribute' => 'gutenberg_register_colors_support',
150 'apply' => 'gutenberg_apply_colors_support',
151 )
152 );
153