author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* Border block support flag. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @since 5.8.0 |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Registers the style attribute used by the border feature if needed for block |
|
11 |
* types that support borders. |
|
12 |
* |
|
13 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
* @since 6.1.0 Improved conditional blocks optimization. |
18 | 15 |
* @access private |
16 |
* |
|
17 |
* @param WP_Block_Type $block_type Block Type. |
|
18 |
*/ |
|
19 |
function wp_register_border_support( $block_type ) { |
|
20 |
// Setup attributes and styles within that if needed. |
|
21 |
if ( ! $block_type->attributes ) { |
|
22 |
$block_type->attributes = array(); |
|
23 |
} |
|
24 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) { |
18 | 26 |
$block_type->attributes['style'] = array( |
27 |
'type' => 'object', |
|
28 |
); |
|
29 |
} |
|
30 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { |
18 | 32 |
$block_type->attributes['borderColor'] = array( |
33 |
'type' => 'string', |
|
34 |
); |
|
35 |
} |
|
36 |
} |
|
37 |
||
38 |
/** |
|
39 |
* Adds CSS classes and inline styles for border styles to the incoming |
|
40 |
* attributes array. This will be applied to the block markup in the front-end. |
|
41 |
* |
|
42 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
43 |
* @since 6.1.0 Implemented the style engine to generate CSS and classnames. |
18 | 44 |
* @access private |
45 |
* |
|
46 |
* @param WP_Block_Type $block_type Block type. |
|
47 |
* @param array $block_attributes Block attributes. |
|
48 |
* @return array Border CSS classes and inline styles. |
|
49 |
*/ |
|
50 |
function wp_apply_border_support( $block_type, $block_attributes ) { |
|
19 | 51 |
if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) { |
18 | 52 |
return array(); |
53 |
} |
|
54 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
$border_block_styles = array(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
56 |
$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' ); |
18 | 58 |
|
59 |
// Border radius. |
|
60 |
if ( |
|
61 |
wp_has_border_feature_support( $block_type, 'radius' ) && |
|
19 | 62 |
isset( $block_attributes['style']['border']['radius'] ) && |
63 |
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) |
|
18 | 64 |
) { |
19 | 65 |
$border_radius = $block_attributes['style']['border']['radius']; |
66 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
if ( is_numeric( $border_radius ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
$border_radius .= 'px'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
} |
19 | 70 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
$border_block_styles['radius'] = $border_radius; |
18 | 72 |
} |
73 |
||
74 |
// Border style. |
|
75 |
if ( |
|
76 |
wp_has_border_feature_support( $block_type, 'style' ) && |
|
19 | 77 |
isset( $block_attributes['style']['border']['style'] ) && |
78 |
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) |
|
18 | 79 |
) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
$border_block_styles['style'] = $block_attributes['style']['border']['style']; |
18 | 81 |
} |
82 |
||
83 |
// Border width. |
|
84 |
if ( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
$has_border_width_support && |
19 | 86 |
isset( $block_attributes['style']['border']['width'] ) && |
87 |
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) |
|
18 | 88 |
) { |
19 | 89 |
$border_width = $block_attributes['style']['border']['width']; |
90 |
||
91 |
// This check handles original unitless implementation. |
|
92 |
if ( is_numeric( $border_width ) ) { |
|
93 |
$border_width .= 'px'; |
|
94 |
} |
|
95 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
$border_block_styles['width'] = $border_width; |
18 | 97 |
} |
98 |
||
99 |
// Border color. |
|
19 | 100 |
if ( |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
101 |
$has_border_color_support && |
19 | 102 |
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) |
103 |
) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
$custom_border_color = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
} |
18 | 108 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
// Generates styles for individual border sides. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
if ( $has_border_color_support || $has_border_width_support ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
$border = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
$border_side_values = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
$border_block_styles[ $side ] = $border_side_values; |
18 | 119 |
} |
120 |
} |
|
121 |
||
122 |
// Collect classes and styles. |
|
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( 'border' => $border_block_styles ) ); |
18 | 125 |
|
21
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 |
} |
129 |
||
21
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 |
/** |
|
138 |
* Checks whether the current block type supports the border feature requested. |
|
139 |
* |
|
140 |
* If the `__experimentalBorder` support flag is a boolean `true` all border |
|
141 |
* support features are available. Otherwise, the specific feature's support |
|
142 |
* flag nested under `experimentalBorder` must be enabled for the feature |
|
143 |
* to be opted into. |
|
144 |
* |
|
145 |
* @since 5.8.0 |
|
146 |
* @access private |
|
147 |
* |
|
19 | 148 |
* @param WP_Block_Type $block_type Block type to check for support. |
149 |
* @param string $feature Name of the feature to check support for. |
|
150 |
* @param mixed $default_value Fallback value for feature support, defaults to false. |
|
151 |
* @return bool Whether the feature is supported. |
|
18 | 152 |
*/ |
19 | 153 |
function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) { |
18 | 154 |
// Check if all border support features have been opted into via `"__experimentalBorder": true`. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
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
|
156 |
$block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
? $block_type->supports['__experimentalBorder'] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
: $default_value; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
if ( true === $block_type_supports_border ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
} |
18 | 162 |
} |
163 |
||
164 |
// Check if the specific feature has been opted into individually |
|
165 |
// via nested flag under `__experimentalBorder`. |
|
19 | 166 |
return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value ); |
18 | 167 |
} |
168 |
||
169 |
// Register the block support. |
|
170 |
WP_Block_Supports::get_instance()->register( |
|
171 |
'border', |
|
172 |
array( |
|
173 |
'register_attribute' => 'wp_register_border_support', |
|
174 |
'apply' => 'wp_apply_border_support', |
|
175 |
) |
|
176 |
); |