author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* Colors block support flag. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @since 5.6.0 |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Registers the style and colors block attributes for block types that support it. |
|
11 |
* |
|
12 |
* @since 5.6.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* @since 6.1.0 Improved $color_support assignment optimization. |
18 | 14 |
* @access private |
15 |
* |
|
16 |
* @param WP_Block_Type $block_type Block Type. |
|
17 |
*/ |
|
18 |
function wp_register_colors_support( $block_type ) { |
|
19 |
$color_support = false; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
if ( $block_type instanceof WP_Block_Type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false; |
18 | 22 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
$has_text_colors_support = true === $color_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
24 |
( isset( $color_support['text'] ) && $color_support['text'] ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
( is_array( $color_support ) && ! isset( $color_support['text'] ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
26 |
$has_background_colors_support = true === $color_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
( isset( $color_support['background'] ) && $color_support['background'] ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
( is_array( $color_support ) && ! isset( $color_support['background'] ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$has_link_colors_support = isset( $color_support['link'] ) ? $color_support['link'] : false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
$has_button_colors_support = isset( $color_support['button'] ) ? $color_support['button'] : false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
$has_heading_colors_support = isset( $color_support['heading'] ) ? $color_support['heading'] : false; |
18 | 33 |
$has_color_support = $has_text_colors_support || |
34 |
$has_background_colors_support || |
|
35 |
$has_gradients_support || |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
$has_link_colors_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
37 |
$has_button_colors_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
38 |
$has_heading_colors_support; |
18 | 39 |
|
40 |
if ( ! $block_type->attributes ) { |
|
41 |
$block_type->attributes = array(); |
|
42 |
} |
|
43 |
||
44 |
if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
|
45 |
$block_type->attributes['style'] = array( |
|
46 |
'type' => 'object', |
|
47 |
); |
|
48 |
} |
|
49 |
||
50 |
if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) { |
|
51 |
$block_type->attributes['backgroundColor'] = array( |
|
52 |
'type' => 'string', |
|
53 |
); |
|
54 |
} |
|
55 |
||
56 |
if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) { |
|
57 |
$block_type->attributes['textColor'] = array( |
|
58 |
'type' => 'string', |
|
59 |
); |
|
60 |
} |
|
61 |
||
62 |
if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) { |
|
63 |
$block_type->attributes['gradient'] = array( |
|
64 |
'type' => 'string', |
|
65 |
); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
||
70 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
* Adds CSS classes and inline styles for colors to the incoming attributes array. |
18 | 72 |
* This will be applied to the block markup in the front-end. |
73 |
* |
|
74 |
* @since 5.6.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
* @since 6.1.0 Implemented the style engine to generate CSS and classnames. |
18 | 76 |
* @access private |
77 |
* |
|
78 |
* @param WP_Block_Type $block_type Block type. |
|
79 |
* @param array $block_attributes Block attributes. |
|
80 |
* |
|
81 |
* @return array Colors CSS classes and inline styles. |
|
82 |
*/ |
|
83 |
function wp_apply_colors_support( $block_type, $block_attributes ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false; |
18 | 85 |
|
86 |
if ( |
|
87 |
is_array( $color_support ) && |
|
19 | 88 |
wp_should_skip_block_supports_serialization( $block_type, 'color' ) |
18 | 89 |
) { |
90 |
return array(); |
|
91 |
} |
|
92 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
$has_text_colors_support = true === $color_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
( isset( $color_support['text'] ) && $color_support['text'] ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
( is_array( $color_support ) && ! isset( $color_support['text'] ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
$has_background_colors_support = true === $color_support || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
( isset( $color_support['background'] ) && $color_support['background'] ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
( is_array( $color_support ) && ! isset( $color_support['background'] ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
$color_block_styles = array(); |
18 | 101 |
|
102 |
// Text colors. |
|
19 | 103 |
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
$custom_text_color = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; |
18 | 107 |
} |
108 |
||
109 |
// Background colors. |
|
19 | 110 |
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
$custom_background_color = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; |
18 | 114 |
} |
115 |
||
116 |
// Gradients. |
|
19 | 117 |
if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
$custom_gradient_color = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; |
18 | 121 |
} |
122 |
||
123 |
$attributes = array(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
124 |
$styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
125 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
126 |
if ( ! empty( $styles['classnames'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
127 |
$attributes['class'] = $styles['classnames']; |
18 | 128 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
if ( ! empty( $styles['css'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
$attributes['style'] = $styles['css']; |
18 | 132 |
} |
133 |
||
134 |
return $attributes; |
|
135 |
} |
|
136 |
||
137 |
// Register the block support. |
|
138 |
WP_Block_Supports::get_instance()->register( |
|
139 |
'colors', |
|
140 |
array( |
|
141 |
'register_attribute' => 'wp_register_colors_support', |
|
142 |
'apply' => 'wp_apply_colors_support', |
|
143 |
) |
|
144 |
); |