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
gutenberg / lib / block-supports / colors.php

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

167 lines 6.2 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 = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false );
17 }
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 );
21
22 if ( ! $block_type->attributes ) {
23 $block_type->attributes = array();
24 }
25
26 if ( $has_text_colors_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
27 $block_type->attributes['style'] = array(
28 'type' => 'object',
29 );
30 }
31
32 if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) {
33 $block_type->attributes['backgroundColor'] = array(
34 'type' => 'string',
35 );
36 }
37
38 if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) {
39 $block_type->attributes['textColor'] = array(
40 'type' => 'string',
41 );
42 }
43
44 if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) {
45 $block_type->attributes['gradient'] = array(
46 'type' => 'string',
47 );
48 }
49 }
50
51
52 /**
53 * Add CSS classes and inline styles for colors to the incoming attributes array.
54 * This will be applied to the block markup in the front-end.
55 *
56 * @param WP_Block_Type $block_type Block type.
57 * @param array $block_attributes Block attributes.
58 *
59 * @return array Colors CSS classes and inline styles.
60 */
61 function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
62 $color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false );
63
64 if (
65 is_array( $color_support ) &&
66 array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
67 $color_support['__experimentalSkipSerialization']
68 ) {
69 return array();
70 }
71
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();
78
79 // Text Colors.
80 // Check support for text colors.
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 }
95 }
96
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 }
113 }
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
132 // Gradients.
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'] );
136
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 }
146 }
147
148 $attributes = array();
149 if ( ! empty( $classes ) ) {
150 $attributes['class'] = implode( ' ', $classes );
151 }
152 if ( ! empty( $styles ) ) {
153 $attributes['style'] = implode( ' ', $styles );
154 }
155
156 return $attributes;
157 }
158
159 // Register the block support.
160 WP_Block_Supports::get_instance()->register(
161 'colors',
162 array(
163 'register_attribute' => 'gutenberg_register_colors_support',
164 'apply' => 'gutenberg_apply_colors_support',
165 )
166 );
167