-
Notifications
You must be signed in to change notification settings - Fork 4k
/
border.php
135 lines (116 loc) · 4.13 KB
/
border.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Border block support flag.
*
* @package gutenberg
*/
/**
* Registers the style attribute used by the border feature if needed for block
* types that support borders.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_border_support( $block_type ) {
// Determine if any border related features are supported.
$has_border_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder' ) );
$has_border_color_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder', '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 ) ) {
$block_type->attributes['style'] = array(
'type' => 'object',
);
}
if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
$block_type->attributes['borderColor'] = array(
'type' => 'string',
);
}
}
/**
* Adds CSS classes and inline styles for border styles to the incoming
* attributes array. This will be applied to the block markup in the front-end.
*
* @param WP_Block_type $block_type Block type.
* @param array $block_attributes Block attributes.
*
* @return array Border CSS classes and inline styles.
*/
function gutenberg_apply_border_support( $block_type, $block_attributes ) {
if ( gutenberg_skip_border_serialization( $block_type ) ) {
return array();
}
$classes = array();
$styles = array();
// Border radius.
if (
gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'radius' ) ) &&
isset( $block_attributes['style']['border']['radius'] )
) {
$border_radius = (int) $block_attributes['style']['border']['radius'];
$styles[] = sprintf( 'border-radius: %dpx;', $border_radius );
}
// Border style.
if (
gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'style' ) ) &&
isset( $block_attributes['style']['border']['style'] )
) {
$border_style = $block_attributes['style']['border']['style'];
$styles[] = sprintf( 'border-style: %s;', $border_style );
}
// Border width.
if (
gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'width' ) ) &&
isset( $block_attributes['style']['border']['width'] )
) {
$border_width = intval( $block_attributes['style']['border']['width'] );
$styles[] = sprintf( 'border-width: %dpx;', $border_width );
}
// Border color.
if ( gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'color' ) ) ) {
$has_named_border_color = array_key_exists( 'borderColor', $block_attributes );
$has_custom_border_color = isset( $block_attributes['style']['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 );
}
}
// Collect classes and styles.
$attributes = array();
if ( ! empty( $classes ) ) {
$attributes['class'] = implode( ' ', $classes );
}
if ( ! empty( $styles ) ) {
$attributes['style'] = implode( ' ', $styles );
}
return $attributes;
}
/**
* Checks whether serialization of the current block's border properties should
* occur.
*
* @param WP_Block_type $block_type Block type.
*
* @return boolean
*/
function gutenberg_skip_border_serialization( $block_type ) {
$border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false );
return is_array( $border_support ) &&
array_key_exists( '__experimentalSkipSerialization', $border_support ) &&
$border_support['__experimentalSkipSerialization'];
}
// Register the block support.
WP_Block_Supports::get_instance()->register(
'border',
array(
'register_attribute' => 'gutenberg_register_border_support',
'apply' => 'gutenberg_apply_border_support',
)
);