@@ -52,76 +52,104 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
5252 return array ();
5353 }
5454
55- $ border_block_styles = array ();
5655 $ has_border_color_support = wp_has_border_feature_support ( $ block_type , 'color ' );
5756 $ has_border_width_support = wp_has_border_feature_support ( $ block_type , 'width ' );
57+ $ attributes = $ block_attributes ;
5858
59- // Border radius.
60- if (
61- wp_has_border_feature_support ( $ block_type , 'radius ' ) &&
62- isset ( $ block_attributes ['style ' ]['border ' ]['radius ' ] ) &&
63- ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'radius ' )
64- ) {
65- $ border_radius = $ block_attributes ['style ' ]['border ' ]['radius ' ];
59+ if ( ! isset ( $ attributes ['style ' ]['border ' ] ) || ! is_array ( $ attributes ['style ' ]['border ' ] ) ) {
60+ unset( $ attributes ['style ' ] );
61+ }
6662
67- if ( is_numeric ( $ border_radius ) ) {
68- $ border_radius .= 'px ' ;
63+ foreach ( array ( 'radius ' , 'style ' , 'width ' , 'color ' ) as $ feature ) {
64+ if (
65+ ! wp_has_border_feature_support ( $ block_type , $ feature ) ||
66+ wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , $ feature )
67+ ) {
68+ unset( $ attributes ['style ' ]['border ' ][ $ feature ] );
6969 }
70-
71- $ border_block_styles ['radius ' ] = $ border_radius ;
7270 }
7371
74- // Border style.
7572 if (
76- wp_has_border_feature_support ( $ block_type , 'style ' ) &&
77- isset ( $ block_attributes ['style ' ]['border ' ]['style ' ] ) &&
78- ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'style ' )
73+ ! $ has_border_color_support ||
74+ wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'color ' )
7975 ) {
80- $ border_block_styles [ ' style ' ] = $ block_attributes [ ' style ' ][ ' border ' ][ ' style ' ] ;
76+ unset( $ attributes [ ' borderColor ' ] ) ;
8177 }
8278
83- // Border width .
84- if (
85- $ has_border_width_support &&
86- isset ( $ block_attributes [ ' style ' ][ ' border ' ][ ' width ' ] ) &&
87- ! wp_should_skip_block_supports_serialization ( $ block_type , ' __experimentalBorder ' , ' width ' )
88- ) {
89- $ border_width = $ block_attributes [ ' style ' ][ ' border ' ][ ' width ' ];
79+ // Sides are filtered by skipped serialization only, not by feature support .
80+ $ skipped_side_features = array ();
81+ foreach ( array ( ' width ' , ' color ' , ' style ' ) as $ feature ) {
82+ if ( wp_should_skip_block_supports_serialization ( $ block_type , ' __experimentalBorder ' , $ feature ) ) {
83+ $ skipped_side_features [] = $ feature ;
84+ }
85+ }
9086
91- // This check handles original unitless implementation.
92- if ( is_numeric ( $ border_width ) ) {
93- $ border_width .= 'px ' ;
87+ foreach ( array ( 'top ' , 'right ' , 'bottom ' , 'left ' ) as $ side ) {
88+ if (
89+ ! ( $ has_border_color_support || $ has_border_width_support ) ||
90+ ! isset ( $ attributes ['style ' ]['border ' ][ $ side ] ) ||
91+ ! is_array ( $ attributes ['style ' ]['border ' ][ $ side ] )
92+ ) {
93+ unset( $ attributes ['style ' ]['border ' ][ $ side ] );
94+ continue ;
95+ }
96+ foreach ( $ skipped_side_features as $ feature ) {
97+ unset( $ attributes ['style ' ]['border ' ][ $ side ][ $ feature ] );
9498 }
99+ }
100+
101+ return wp_get_border_classes_and_styles ( $ attributes );
102+ }
103+
104+ /**
105+ * Returns border classes and inline styles for block attributes, like the JS
106+ * `getBorderClassesAndStyles()`. Does not check block support or skipped
107+ * serialization.
108+ *
109+ * @since 7.2.0
110+ *
111+ * @param array $block_attributes Block attributes.
112+ * @return array Array with `class` and `style` keys, each present only when non-empty.
113+ */
114+ function wp_get_border_classes_and_styles ( $ block_attributes ) {
115+ if ( ! is_array ( $ block_attributes ) ) {
116+ return array ();
117+ }
118+
119+ $ border = isset ( $ block_attributes ['style ' ]['border ' ] ) && is_array ( $ block_attributes ['style ' ]['border ' ] )
120+ ? $ block_attributes ['style ' ]['border ' ]
121+ : array ();
122+ $ border_styles = array ();
95123
96- $ border_block_styles ['width ' ] = $ border_width ;
124+ // Unitless radius and width are from the original implementation.
125+ if ( isset ( $ border ['radius ' ] ) ) {
126+ $ border_styles ['radius ' ] = is_numeric ( $ border ['radius ' ] ) ? "{$ border ['radius ' ]}px " : $ border ['radius ' ];
97127 }
98128
99- // Border color.
100- if (
101- $ has_border_color_support &&
102- ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'color ' )
103- ) {
104- $ preset_border_color = array_key_exists ( 'borderColor ' , $ block_attributes ) ? "var:preset|color| {$ block_attributes ['borderColor ' ]}" : null ;
105- $ custom_border_color = $ block_attributes ['style ' ]['border ' ]['color ' ] ?? null ;
106- $ border_block_styles ['color ' ] = $ preset_border_color ? $ preset_border_color : $ custom_border_color ;
107- }
108-
109- // Generates styles for individual border sides.
110- if ( $ has_border_color_support || $ has_border_width_support ) {
111- foreach ( array ( 'top ' , 'right ' , 'bottom ' , 'left ' ) as $ side ) {
112- $ border = $ block_attributes ['style ' ]['border ' ][ $ side ] ?? null ;
113- $ border_side_values = array (
114- 'width ' => isset ( $ border ['width ' ] ) && ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'width ' ) ? $ border ['width ' ] : null ,
115- 'color ' => isset ( $ border ['color ' ] ) && ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'color ' ) ? $ border ['color ' ] : null ,
116- 'style ' => isset ( $ border ['style ' ] ) && ! wp_should_skip_block_supports_serialization ( $ block_type , '__experimentalBorder ' , 'style ' ) ? $ border ['style ' ] : null ,
117- );
118- $ border_block_styles [ $ side ] = $ border_side_values ;
129+ if ( isset ( $ border ['style ' ] ) ) {
130+ $ border_styles ['style ' ] = $ border ['style ' ];
131+ }
132+
133+ if ( isset ( $ border ['width ' ] ) ) {
134+ $ border_styles ['width ' ] = is_numeric ( $ border ['width ' ] ) ? "{$ border ['width ' ]}px " : $ border ['width ' ];
135+ }
136+
137+ $ preset_border_color = array_key_exists ( 'borderColor ' , $ block_attributes ) ? "var:preset|color| {$ block_attributes ['borderColor ' ]}" : null ;
138+ $ border_styles ['color ' ] = $ preset_border_color ? $ preset_border_color : ( $ border ['color ' ] ?? null );
139+
140+ foreach ( array ( 'top ' , 'right ' , 'bottom ' , 'left ' ) as $ side ) {
141+ if ( ! isset ( $ border [ $ side ] ) || ! is_array ( $ border [ $ side ] ) ) {
142+ continue ;
119143 }
144+ $ border_styles [ $ side ] = array (
145+ 'width ' => $ border [ $ side ]['width ' ] ?? null ,
146+ 'color ' => $ border [ $ side ]['color ' ] ?? null ,
147+ 'style ' => $ border [ $ side ]['style ' ] ?? null ,
148+ );
120149 }
121150
122- // Collect classes and styles.
123151 $ attributes = array ();
124- $ styles = wp_style_engine_get_styles ( array ( 'border ' => $ border_block_styles ) );
152+ $ styles = wp_style_engine_get_styles ( array ( 'border ' => $ border_styles ) );
125153
126154 if ( ! empty ( $ styles ['classnames ' ] ) ) {
127155 $ attributes ['class ' ] = $ styles ['classnames ' ];
0 commit comments