PluginProbe
Gutenberg / 9.8.0
Gutenberg v9.8.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 9.8.0, at lib/block-supports/colors.php

158 lines 6.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 = 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 $has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) );
64 $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) );
65 $has_link_colors_support = gutenberg_experimental_get( $color_support, array( 'link' ), false );
66 $has_gradients_support = gutenberg_experimental_get( $color_support, array( 'gradients' ), false );
67 $classes = array();
68 $styles = array();
69
70 // Text Colors.
71 // Check support for text colors.
72 if ( $has_text_colors_support ) {
73 $has_named_text_color = array_key_exists( 'textColor', $block_attributes );
74 $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
75
76 // Apply required generic class.
77 if ( $has_custom_text_color || $has_named_text_color ) {
78 $classes[] = 'has-text-color';
79 }
80 // Apply color class or inline style.
81 if ( $has_named_text_color ) {
82 $classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
83 } elseif ( $has_custom_text_color ) {
84 $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
85 }
86 }
87
88 // Link Colors.
89 if ( $has_link_colors_support ) {
90 $has_link_color = isset( $block_attributes['style']['color']['link'] );
91 // Apply required class and style.
92 if ( $has_link_color ) {
93 $classes[] = 'has-link-color';
94 // If link is a named color.
95 if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) {
96 // Get the name from the string and add proper styles.
97 $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
98 $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
99 $styles[] = sprintf( '--wp--style--color--link: var(--wp--preset--color--%s);', $link_color_name );
100 } else {
101 $styles[] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
102 }
103 }
104 }
105
106 // Background Colors.
107 if ( $has_background_colors_support ) {
108 $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes );
109 $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
110
111 // Apply required background class.
112 if ( $has_custom_background_color || $has_named_background_color ) {
113 $classes[] = 'has-background';
114 }
115 // Apply background color classes or styles.
116 if ( $has_named_background_color ) {
117 $classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
118 } elseif ( $has_custom_background_color ) {
119 $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
120 }
121 }
122
123 // Gradients.
124 if ( $has_gradients_support ) {
125 $has_named_gradient = array_key_exists( 'gradient', $block_attributes );
126 $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
127
128 if ( $has_named_gradient || $has_custom_gradient ) {
129 $classes[] = 'has-background';
130 }
131 // Apply required background class.
132 if ( $has_named_gradient ) {
133 $classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
134 } elseif ( $has_custom_gradient ) {
135 $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
136 }
137 }
138
139 $attributes = array();
140 if ( ! empty( $classes ) ) {
141 $attributes['class'] = implode( ' ', $classes );
142 }
143 if ( ! empty( $styles ) ) {
144 $attributes['style'] = implode( ' ', $styles );
145 }
146
147 return $attributes;
148 }
149
150 // Register the block support.
151 WP_Block_Supports::get_instance()->register(
152 'colors',
153 array(
154 'register_attribute' => 'gutenberg_register_colors_support',
155 'apply' => 'gutenberg_apply_colors_support',
156 )
157 );
158