| 1 |
<?php |
| 2 |
/** |
| 3 |
* Border block support flag. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the style attribute used by the border feature if needed for block |
| 10 |
* types that support borders. |
| 11 |
* |
| 12 |
* @param WP_Block_Type $block_type Block Type. |
| 13 |
*/ |
| 14 |
function gutenberg_register_border_support( $block_type ) { |
| 15 |
// Determine if any border related features are supported. |
| 16 |
$has_border_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder' ) ); |
| 17 |
$has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' ); |
| 18 |
|
| 19 |
// Setup attributes and styles within that if needed. |
| 20 |
if ( ! $block_type->attributes ) { |
| 21 |
$block_type->attributes = array(); |
| 22 |
} |
| 23 |
|
| 24 |
if ( $has_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
| 25 |
$block_type->attributes['style'] = array( |
| 26 |
'type' => 'object', |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) { |
| 31 |
$block_type->attributes['borderColor'] = array( |
| 32 |
'type' => 'string', |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Adds CSS classes and inline styles for border styles to the incoming |
| 39 |
* attributes array. This will be applied to the block markup in the front-end. |
| 40 |
* |
| 41 |
* @param WP_Block_type $block_type Block type. |
| 42 |
* @param array $block_attributes Block attributes. |
| 43 |
* |
| 44 |
* @return array Border CSS classes and inline styles. |
| 45 |
*/ |
| 46 |
function gutenberg_apply_border_support( $block_type, $block_attributes ) { |
| 47 |
if ( gutenberg_skip_border_serialization( $block_type ) ) { |
| 48 |
return array(); |
| 49 |
} |
| 50 |
|
| 51 |
$classes = array(); |
| 52 |
$styles = array(); |
| 53 |
|
| 54 |
// Border radius. |
| 55 |
if ( |
| 56 |
gutenberg_has_border_feature_support( $block_type, 'radius' ) && |
| 57 |
isset( $block_attributes['style']['border']['radius'] ) |
| 58 |
) { |
| 59 |
$border_radius = $block_attributes['style']['border']['radius']; |
| 60 |
|
| 61 |
if ( is_array( $border_radius ) ) { |
| 62 |
// We have individual border radius corner values. |
| 63 |
foreach ( $border_radius as $key => $radius ) { |
| 64 |
// Convert CamelCase corner name to kebab-case. |
| 65 |
$corner = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) ); |
| 66 |
$styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius ); |
| 67 |
} |
| 68 |
} else { |
| 69 |
// This check handles original unitless implementation. |
| 70 |
if ( is_numeric( $border_radius ) ) { |
| 71 |
$border_radius .= 'px'; |
| 72 |
} |
| 73 |
|
| 74 |
$styles[] = sprintf( 'border-radius: %s;', $border_radius ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Border style. |
| 79 |
if ( |
| 80 |
gutenberg_has_border_feature_support( $block_type, 'style' ) && |
| 81 |
isset( $block_attributes['style']['border']['style'] ) |
| 82 |
) { |
| 83 |
$border_style = $block_attributes['style']['border']['style']; |
| 84 |
$styles[] = sprintf( 'border-style: %s;', $border_style ); |
| 85 |
} |
| 86 |
|
| 87 |
// Border width. |
| 88 |
if ( |
| 89 |
gutenberg_has_border_feature_support( $block_type, 'width' ) && |
| 90 |
isset( $block_attributes['style']['border']['width'] ) |
| 91 |
) { |
| 92 |
$border_width = $block_attributes['style']['border']['width']; |
| 93 |
|
| 94 |
// This check handles original unitless implementation. |
| 95 |
if ( is_numeric( $border_width ) ) { |
| 96 |
$border_width .= 'px'; |
| 97 |
} |
| 98 |
|
| 99 |
$styles[] = sprintf( 'border-width: %s;', $border_width ); |
| 100 |
} |
| 101 |
|
| 102 |
// Border color. |
| 103 |
if ( gutenberg_has_border_feature_support( $block_type, 'color' ) ) { |
| 104 |
$has_named_border_color = array_key_exists( 'borderColor', $block_attributes ); |
| 105 |
$has_custom_border_color = isset( $block_attributes['style']['border']['color'] ); |
| 106 |
|
| 107 |
if ( $has_named_border_color || $has_custom_border_color ) { |
| 108 |
$classes[] = 'has-border-color'; |
| 109 |
} |
| 110 |
|
| 111 |
if ( $has_named_border_color ) { |
| 112 |
$classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] ); |
| 113 |
} elseif ( $has_custom_border_color ) { |
| 114 |
$border_color = $block_attributes['style']['border']['color']; |
| 115 |
$styles[] = sprintf( 'border-color: %s;', $border_color ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Collect classes and styles. |
| 120 |
$attributes = array(); |
| 121 |
|
| 122 |
if ( ! empty( $classes ) ) { |
| 123 |
$attributes['class'] = implode( ' ', $classes ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( ! empty( $styles ) ) { |
| 127 |
$attributes['style'] = implode( ' ', $styles ); |
| 128 |
} |
| 129 |
|
| 130 |
return $attributes; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Checks whether serialization of the current block's border properties should |
| 135 |
* occur. |
| 136 |
* |
| 137 |
* @param WP_Block_type $block_type Block type. |
| 138 |
* |
| 139 |
* @return boolean |
| 140 |
*/ |
| 141 |
function gutenberg_skip_border_serialization( $block_type ) { |
| 142 |
$border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false ); |
| 143 |
|
| 144 |
return is_array( $border_support ) && |
| 145 |
array_key_exists( '__experimentalSkipSerialization', $border_support ) && |
| 146 |
$border_support['__experimentalSkipSerialization']; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Checks whether the current block type supports the border feature requested. |
| 151 |
* |
| 152 |
* If the `__experimentalBorder` support flag is a boolean `true` all border |
| 153 |
* support features are available. Otherwise, the specific feature's support |
| 154 |
* flag nested under `experimentalBorder` must be enabled for the feature |
| 155 |
* to be opted into. |
| 156 |
* |
| 157 |
* @param WP_Block_Type $block_type Block type to check for support. |
| 158 |
* @param string $feature Name of the feature to check support for. |
| 159 |
* @param mixed $default Fallback value for feature support, defaults to false. |
| 160 |
* |
| 161 |
* @return boolean Whether or not the feature is supported. |
| 162 |
*/ |
| 163 |
function gutenberg_has_border_feature_support( $block_type, $feature, $default = false ) { |
| 164 |
// Check if all border support features have been opted into via `"__experimentalBorder": true`. |
| 165 |
if ( |
| 166 |
property_exists( $block_type, 'supports' ) && |
| 167 |
( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default ) ) |
| 168 |
) { |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
// Check if the specific feature has been opted into individually |
| 173 |
// via nested flag under `__experimentalBorder`. |
| 174 |
return gutenberg_block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default ); |
| 175 |
} |
| 176 |
|
| 177 |
// Register the block support. |
| 178 |
WP_Block_Supports::get_instance()->register( |
| 179 |
'border', |
| 180 |
array( |
| 181 |
'register_attribute' => 'gutenberg_register_border_support', |
| 182 |
'apply' => 'gutenberg_apply_border_support', |
| 183 |
) |
| 184 |
); |
| 185 |
|