wp/wp-includes/block-supports/border.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
--- a/wp/wp-includes/block-supports/border.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/block-supports/border.php	Fri Sep 05 18:40:08 2025 +0200
@@ -11,27 +11,24 @@
  * types that support borders.
  *
  * @since 5.8.0
+ * @since 6.1.0 Improved conditional blocks optimization.
  * @access private
  *
  * @param WP_Block_Type $block_type Block Type.
  */
 function wp_register_border_support( $block_type ) {
-	// Determine if any border related features are supported.
-	$has_border_support       = block_has_support( $block_type, array( '__experimentalBorder' ) );
-	$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
-
 	// Setup attributes and styles within that if needed.
 	if ( ! $block_type->attributes ) {
 		$block_type->attributes = array();
 	}
 
-	if ( $has_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
+	if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
 		$block_type->attributes['style'] = array(
 			'type' => 'object',
 		);
 	}
 
-	if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
+	if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
 		$block_type->attributes['borderColor'] = array(
 			'type' => 'string',
 		);
@@ -43,6 +40,7 @@
  * attributes array. This will be applied to the block markup in the front-end.
  *
  * @since 5.8.0
+ * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
  * @access private
  *
  * @param WP_Block_Type $block_type       Block type.
@@ -54,8 +52,9 @@
 		return array();
 	}
 
-	$classes = array();
-	$styles  = array();
+	$border_block_styles      = array();
+	$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
+	$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
 
 	// Border radius.
 	if (
@@ -65,21 +64,11 @@
 	) {
 		$border_radius = $block_attributes['style']['border']['radius'];
 
-		if ( is_array( $border_radius ) ) {
-			// We have individual border radius corner values.
-			foreach ( $border_radius as $key => $radius ) {
-				// Convert CamelCase corner name to kebab-case.
-				$corner   = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
-				$styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius );
-			}
-		} else {
-			// This check handles original unitless implementation.
-			if ( is_numeric( $border_radius ) ) {
-				$border_radius .= 'px';
-			}
+		if ( is_numeric( $border_radius ) ) {
+			$border_radius .= 'px';
+		}
 
-			$styles[] = sprintf( 'border-radius: %s;', $border_radius );
-		}
+		$border_block_styles['radius'] = $border_radius;
 	}
 
 	// Border style.
@@ -88,13 +77,12 @@
 		isset( $block_attributes['style']['border']['style'] ) &&
 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
 	) {
-		$border_style = $block_attributes['style']['border']['style'];
-		$styles[]     = sprintf( 'border-style: %s;', $border_style );
+		$border_block_styles['style'] = $block_attributes['style']['border']['style'];
 	}
 
 	// Border width.
 	if (
-		wp_has_border_feature_support( $block_type, 'width' ) &&
+		$has_border_width_support &&
 		isset( $block_attributes['style']['border']['width'] ) &&
 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
 	) {
@@ -105,38 +93,42 @@
 			$border_width .= 'px';
 		}
 
-		$styles[] = sprintf( 'border-width: %s;', $border_width );
+		$border_block_styles['width'] = $border_width;
 	}
 
 	// Border color.
 	if (
-		wp_has_border_feature_support( $block_type, 'color' ) &&
+		$has_border_color_support &&
 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
 	) {
-		$has_named_border_color  = array_key_exists( 'borderColor', $block_attributes );
-		$has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
+		$preset_border_color          = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
+		$custom_border_color          = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null;
+		$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
+	}
 
-		if ( $has_named_border_color || $has_custom_border_color ) {
-			$classes[] = 'has-border-color';
-		}
-
-		if ( $has_named_border_color ) {
-			$classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] );
-		} elseif ( $has_custom_border_color ) {
-			$border_color = $block_attributes['style']['border']['color'];
-			$styles[]     = sprintf( 'border-color: %s;', $border_color );
+	// Generates styles for individual border sides.
+	if ( $has_border_color_support || $has_border_width_support ) {
+		foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
+			$border                       = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null;
+			$border_side_values           = array(
+				'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
+				'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
+				'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
+			);
+			$border_block_styles[ $side ] = $border_side_values;
 		}
 	}
 
 	// Collect classes and styles.
 	$attributes = array();
+	$styles     = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
 
-	if ( ! empty( $classes ) ) {
-		$attributes['class'] = implode( ' ', $classes );
+	if ( ! empty( $styles['classnames'] ) ) {
+		$attributes['class'] = $styles['classnames'];
 	}
 
-	if ( ! empty( $styles ) ) {
-		$attributes['style'] = implode( ' ', $styles );
+	if ( ! empty( $styles['css'] ) ) {
+		$attributes['style'] = $styles['css'];
 	}
 
 	return $attributes;
@@ -160,11 +152,13 @@
  */
 function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
 	// Check if all border support features have been opted into via `"__experimentalBorder": true`.
-	if (
-		property_exists( $block_type, 'supports' ) &&
-		( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default_value ) )
-	) {
-		return true;
+	if ( $block_type instanceof WP_Block_Type ) {
+		$block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] )
+			? $block_type->supports['__experimentalBorder']
+			: $default_value;
+		if ( true === $block_type_supports_border ) {
+			return true;
+		}
 	}
 
 	// Check if the specific feature has been opted into individually