| 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 types that |
| 10 |
* support borders. |
| 11 |
* |
| 12 |
* @param WP_Block_Type $block_type Block Type. |
| 13 |
*/ |
| 14 |
function gutenberg_register_border_support( $block_type ) { |
| 15 |
// Determine border related features supported. |
| 16 |
// Border width, style etc can be added in the future. |
| 17 |
$has_border_radius_support = gutenberg_has_border_support( $block_type, 'radius' ); |
| 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_radius_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
| 25 |
$block_type->attributes['style'] = array( |
| 26 |
'type' => 'object', |
| 27 |
); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Adds CSS classes and inline styles for border styles to the incoming |
| 33 |
* attributes array. This will be applied to the block markup in the front-end. |
| 34 |
* |
| 35 |
* @param WP_Block_type $block_type Block type. |
| 36 |
* @param array $block_attributes Block attributes. |
| 37 |
* |
| 38 |
* @return array Border CSS classes and inline styles. |
| 39 |
*/ |
| 40 |
function gutenberg_apply_border_support( $block_type, $block_attributes ) { |
| 41 |
// Arrays used to ease addition of further border related features in future. |
| 42 |
$styles = array(); |
| 43 |
|
| 44 |
// Border Radius. |
| 45 |
if ( gutenberg_has_border_support( $block_type, 'radius' ) ) { |
| 46 |
if ( isset( $block_attributes['style']['border']['radius'] ) ) { |
| 47 |
$border_radius = intval( $block_attributes['style']['border']['radius'] ); |
| 48 |
$styles[] = sprintf( 'border-radius: %dpx;', $border_radius ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Border width, style etc can be added here. |
| 53 |
|
| 54 |
// Collect classes and styles. |
| 55 |
$attributes = array(); |
| 56 |
|
| 57 |
if ( ! empty( $styles ) ) { |
| 58 |
$attributes['style'] = implode( ' ', $styles ); |
| 59 |
} |
| 60 |
|
| 61 |
return $attributes; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Checks whether the current block type supports the feature requested. |
| 66 |
* |
| 67 |
* @param WP_Block_Type $block_type Block type to check for support. |
| 68 |
* @param string $feature Name of the feature to check support for. |
| 69 |
* @param mixed $default Fallback value for feature support, defaults to false. |
| 70 |
* |
| 71 |
* @return boolean Whether or not the feature is supported. |
| 72 |
*/ |
| 73 |
function gutenberg_has_border_support( $block_type, $feature, $default = false ) { |
| 74 |
$block_support = false; |
| 75 |
if ( property_exists( $block_type, 'supports' ) ) { |
| 76 |
$block_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalBorder' ), $default ); |
| 77 |
} |
| 78 |
|
| 79 |
return true === $block_support || ( is_array( $block_support ) && gutenberg_experimental_get( $block_support, array( $feature ), false ) ); |
| 80 |
} |
| 81 |
|
| 82 |
// Register the block support. |
| 83 |
WP_Block_Supports::get_instance()->register( |
| 84 |
'border', |
| 85 |
array( |
| 86 |
'register_attribute' => 'gutenberg_register_border_support', |
| 87 |
'apply' => 'gutenberg_apply_border_support', |
| 88 |
) |
| 89 |
); |
| 90 |
|