wp/wp-includes/block-supports/border.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    45  * @since 5.8.0
    45  * @since 5.8.0
    46  * @access private
    46  * @access private
    47  *
    47  *
    48  * @param WP_Block_Type $block_type       Block type.
    48  * @param WP_Block_Type $block_type       Block type.
    49  * @param array         $block_attributes Block attributes.
    49  * @param array         $block_attributes Block attributes.
    50  *
       
    51  * @return array Border CSS classes and inline styles.
    50  * @return array Border CSS classes and inline styles.
    52  */
    51  */
    53 function wp_apply_border_support( $block_type, $block_attributes ) {
    52 function wp_apply_border_support( $block_type, $block_attributes ) {
    54 	if ( wp_skip_border_serialization( $block_type ) ) {
    53 	if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
    55 		return array();
    54 		return array();
    56 	}
    55 	}
    57 
    56 
    58 	$classes = array();
    57 	$classes = array();
    59 	$styles  = array();
    58 	$styles  = array();
    60 
    59 
    61 	// Border radius.
    60 	// Border radius.
    62 	if (
    61 	if (
    63 		wp_has_border_feature_support( $block_type, 'radius' ) &&
    62 		wp_has_border_feature_support( $block_type, 'radius' ) &&
    64 		isset( $block_attributes['style']['border']['radius'] )
    63 		isset( $block_attributes['style']['border']['radius'] ) &&
       
    64 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
    65 	) {
    65 	) {
    66 		$border_radius = (int) $block_attributes['style']['border']['radius'];
    66 		$border_radius = $block_attributes['style']['border']['radius'];
    67 		$styles[]      = sprintf( 'border-radius: %dpx;', $border_radius );
    67 
       
    68 		if ( is_array( $border_radius ) ) {
       
    69 			// We have individual border radius corner values.
       
    70 			foreach ( $border_radius as $key => $radius ) {
       
    71 				// Convert CamelCase corner name to kebab-case.
       
    72 				$corner   = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
       
    73 				$styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius );
       
    74 			}
       
    75 		} else {
       
    76 			// This check handles original unitless implementation.
       
    77 			if ( is_numeric( $border_radius ) ) {
       
    78 				$border_radius .= 'px';
       
    79 			}
       
    80 
       
    81 			$styles[] = sprintf( 'border-radius: %s;', $border_radius );
       
    82 		}
    68 	}
    83 	}
    69 
    84 
    70 	// Border style.
    85 	// Border style.
    71 	if (
    86 	if (
    72 		wp_has_border_feature_support( $block_type, 'style' ) &&
    87 		wp_has_border_feature_support( $block_type, 'style' ) &&
    73 		isset( $block_attributes['style']['border']['style'] )
    88 		isset( $block_attributes['style']['border']['style'] ) &&
       
    89 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
    74 	) {
    90 	) {
    75 		$border_style = $block_attributes['style']['border']['style'];
    91 		$border_style = $block_attributes['style']['border']['style'];
    76 		$styles[]     = sprintf( 'border-style: %s;', $border_style );
    92 		$styles[]     = sprintf( 'border-style: %s;', $border_style );
    77 	}
    93 	}
    78 
    94 
    79 	// Border width.
    95 	// Border width.
    80 	if (
    96 	if (
    81 		wp_has_border_feature_support( $block_type, 'width' ) &&
    97 		wp_has_border_feature_support( $block_type, 'width' ) &&
    82 		isset( $block_attributes['style']['border']['width'] )
    98 		isset( $block_attributes['style']['border']['width'] ) &&
       
    99 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
    83 	) {
   100 	) {
    84 		$border_width = intval( $block_attributes['style']['border']['width'] );
   101 		$border_width = $block_attributes['style']['border']['width'];
    85 		$styles[]     = sprintf( 'border-width: %dpx;', $border_width );
   102 
       
   103 		// This check handles original unitless implementation.
       
   104 		if ( is_numeric( $border_width ) ) {
       
   105 			$border_width .= 'px';
       
   106 		}
       
   107 
       
   108 		$styles[] = sprintf( 'border-width: %s;', $border_width );
    86 	}
   109 	}
    87 
   110 
    88 	// Border color.
   111 	// Border color.
    89 	if ( wp_has_border_feature_support( $block_type, 'color' ) ) {
   112 	if (
       
   113 		wp_has_border_feature_support( $block_type, 'color' ) &&
       
   114 		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
       
   115 	) {
    90 		$has_named_border_color  = array_key_exists( 'borderColor', $block_attributes );
   116 		$has_named_border_color  = array_key_exists( 'borderColor', $block_attributes );
    91 		$has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
   117 		$has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
    92 
   118 
    93 		if ( $has_named_border_color || $has_custom_border_color ) {
   119 		if ( $has_named_border_color || $has_custom_border_color ) {
    94 			$classes[] = 'has-border-color';
   120 			$classes[] = 'has-border-color';
   115 
   141 
   116 	return $attributes;
   142 	return $attributes;
   117 }
   143 }
   118 
   144 
   119 /**
   145 /**
   120  * Checks whether serialization of the current block's border properties should
       
   121  * occur.
       
   122  *
       
   123  * @since 5.8.0
       
   124  * @access private
       
   125  *
       
   126  * @param WP_Block_Type $block_type Block type.
       
   127  *
       
   128  * @return boolean
       
   129  */
       
   130 function wp_skip_border_serialization( $block_type ) {
       
   131 	$border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false );
       
   132 
       
   133 	return is_array( $border_support ) &&
       
   134 		array_key_exists( '__experimentalSkipSerialization', $border_support ) &&
       
   135 		$border_support['__experimentalSkipSerialization'];
       
   136 }
       
   137 
       
   138 /**
       
   139  * Checks whether the current block type supports the border feature requested.
   146  * Checks whether the current block type supports the border feature requested.
   140  *
   147  *
   141  * If the `__experimentalBorder` support flag is a boolean `true` all border
   148  * If the `__experimentalBorder` support flag is a boolean `true` all border
   142  * support features are available. Otherwise, the specific feature's support
   149  * support features are available. Otherwise, the specific feature's support
   143  * flag nested under `experimentalBorder` must be enabled for the feature
   150  * flag nested under `experimentalBorder` must be enabled for the feature
   144  * to be opted into.
   151  * to be opted into.
   145  *
   152  *
   146  * @since 5.8.0
   153  * @since 5.8.0
   147  * @access private
   154  * @access private
   148  *
   155  *
   149  * @param WP_Block_Type $block_type Block type to check for support.
   156  * @param WP_Block_Type $block_type    Block type to check for support.
   150  * @param string        $feature    Name of the feature to check support for.
   157  * @param string        $feature       Name of the feature to check support for.
   151  * @param mixed         $default    Fallback value for feature support, defaults to false.
   158  * @param mixed         $default_value Fallback value for feature support, defaults to false.
   152  *
   159  * @return bool Whether the feature is supported.
   153  * @return boolean Whether or not the feature is supported.
       
   154  */
   160  */
   155 function wp_has_border_feature_support( $block_type, $feature, $default = false ) {
   161 function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
   156 	// Check if all border support features have been opted into via `"__experimentalBorder": true`.
   162 	// Check if all border support features have been opted into via `"__experimentalBorder": true`.
   157 	if (
   163 	if (
   158 		property_exists( $block_type, 'supports' ) &&
   164 		property_exists( $block_type, 'supports' ) &&
   159 		( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default ) )
   165 		( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default_value ) )
   160 	) {
   166 	) {
   161 		return true;
   167 		return true;
   162 	}
   168 	}
   163 
   169 
   164 	// Check if the specific feature has been opted into individually
   170 	// Check if the specific feature has been opted into individually
   165 	// via nested flag under `__experimentalBorder`.
   171 	// via nested flag under `__experimentalBorder`.
   166 	return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default );
   172 	return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
   167 }
   173 }
   168 
   174 
   169 // Register the block support.
   175 // Register the block support.
   170 WP_Block_Supports::get_instance()->register(
   176 WP_Block_Supports::get_instance()->register(
   171 	'border',
   177 	'border',