PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.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 14.7.0, at lib/block-supports/colors.php

123 lines 5.0 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 = property_exists( $block_type, 'supports' ) ? _wp_array_get( $block_type->supports, array( 'color' ), false ) : false;
15 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
16 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
17 $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
18 $has_link_colors_support = _wp_array_get( $color_support, array( 'link' ), false );
19 $has_color_support = $has_text_colors_support ||
20 $has_background_colors_support ||
21 $has_gradients_support ||
22 $has_link_colors_support;
23
24 if ( ! $block_type->attributes ) {
25 $block_type->attributes = array();
26 }
27
28 if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
29 $block_type->attributes['style'] = array(
30 'type' => 'object',
31 );
32 }
33
34 if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) {
35 $block_type->attributes['backgroundColor'] = array(
36 'type' => 'string',
37 );
38 }
39
40 if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) {
41 $block_type->attributes['textColor'] = array(
42 'type' => 'string',
43 );
44 }
45
46 if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) {
47 $block_type->attributes['gradient'] = array(
48 'type' => 'string',
49 );
50 }
51 }
52
53
54 /**
55 * Add CSS classes and inline styles for colors to the incoming attributes array.
56 * This will be applied to the block markup in the front-end.
57 *
58 * @param WP_Block_Type $block_type Block type.
59 * @param array $block_attributes Block attributes.
60 *
61 * @return array Colors CSS classes and inline styles.
62 */
63 function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
64 $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
65
66 if (
67 is_array( $color_support ) &&
68 gutenberg_should_skip_block_supports_serialization( $block_type, 'color' )
69 ) {
70 return array();
71 }
72
73 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
74 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
75 $has_gradients_support = _wp_array_get( $color_support, array( 'gradients' ), false );
76 $color_block_styles = array();
77
78 // Text colors.
79 // Check support for text colors.
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;
84 }
85
86 // Background colors.
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;
91 }
92
93 // Gradients.
94
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;
99 }
100
101 $attributes = array();
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'];
106 }
107
108 if ( ! empty( $styles['css'] ) ) {
109 $attributes['style'] = $styles['css'];
110 }
111
112 return $attributes;
113 }
114
115 // Register the block support.
116 WP_Block_Supports::get_instance()->register(
117 'colors',
118 array(
119 'register_attribute' => 'gutenberg_register_colors_support',
120 'apply' => 'gutenberg_apply_colors_support',
121 )
122 );
123