PluginProbe
Gutenberg / 23.3.0
Gutenberg v23.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / block-supports / border.php

border.php in Gutenberg 23.3.0, at lib/block-supports/border.php

165 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Setup attributes and styles within that if needed.
16 if ( ! $block_type->attributes ) {
17 $block_type->attributes = array();
18 }
19
20 if ( block_has_support( $block_type, array( '__experimentalBorder' ) ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
21 $block_type->attributes['style'] = array(
22 'type' => 'object',
23 );
24 }
25
26 if ( gutenberg_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
27 $block_type->attributes['borderColor'] = array(
28 'type' => 'string',
29 );
30 }
31 }
32
33 /**
34 * Adds CSS classes and inline styles for border styles to the incoming
35 * attributes array. This will be applied to the block markup in the front-end.
36 *
37 * @param WP_Block_Type $block_type Block type.
38 * @param array $block_attributes Block attributes.
39 *
40 * @return array Border CSS classes and inline styles.
41 */
42 function gutenberg_apply_border_support( $block_type, $block_attributes ) {
43 if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
44 return array();
45 }
46
47 $border_block_styles = array();
48 $has_border_color_support = gutenberg_has_border_feature_support( $block_type, 'color' );
49 $has_border_width_support = gutenberg_has_border_feature_support( $block_type, 'width' );
50
51 // Border radius.
52 if (
53 gutenberg_has_border_feature_support( $block_type, 'radius' ) &&
54 isset( $block_attributes['style']['border']['radius'] ) &&
55 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
56 ) {
57 $border_radius = $block_attributes['style']['border']['radius'];
58
59 if ( is_numeric( $border_radius ) ) {
60 $border_radius .= 'px';
61 }
62
63 $border_block_styles['radius'] = $border_radius;
64 }
65
66 // Border style.
67 if (
68 gutenberg_has_border_feature_support( $block_type, 'style' ) &&
69 isset( $block_attributes['style']['border']['style'] ) &&
70 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
71 ) {
72 $border_block_styles['style'] = $block_attributes['style']['border']['style'];
73 }
74
75 // Border width.
76 if (
77 $has_border_width_support &&
78 isset( $block_attributes['style']['border']['width'] ) &&
79 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
80 ) {
81 $border_width = $block_attributes['style']['border']['width'];
82
83 // This check handles original unitless implementation.
84 if ( is_numeric( $border_width ) ) {
85 $border_width .= 'px';
86 }
87
88 $border_block_styles['width'] = $border_width;
89 }
90
91 // Border color.
92 if (
93 $has_border_color_support &&
94 ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
95 ) {
96 $preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
97 $custom_border_color = $block_attributes['style']['border']['color'] ?? null;
98 $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
99 }
100
101 // Generate styles for individual border sides.
102 if ( $has_border_color_support || $has_border_width_support ) {
103 foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
104 $border = $block_attributes['style']['border'][ $side ] ?? null;
105 $border_side_values = array(
106 'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
107 'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
108 'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
109 );
110 $border_block_styles[ $side ] = $border_side_values;
111 }
112 }
113
114 // Collect classes and styles.
115 $attributes = array();
116 $styles = gutenberg_style_engine_get_styles( array( 'border' => $border_block_styles ) );
117
118 if ( ! empty( $styles['classnames'] ) ) {
119 $attributes['class'] = $styles['classnames'];
120 }
121
122 if ( ! empty( $styles['css'] ) ) {
123 $attributes['style'] = $styles['css'];
124 }
125
126 return $attributes;
127 }
128
129 /**
130 * Checks whether the current block type supports the border feature requested.
131 *
132 * If the `__experimentalBorder` support flag is a boolean `true` all border
133 * support features are available. Otherwise, the specific feature's support
134 * flag nested under `experimentalBorder` must be enabled for the feature
135 * to be opted into.
136 *
137 * @param WP_Block_Type $block_type Block type to check for support.
138 * @param string $feature Name of the feature to check support for.
139 * @param mixed $default_value Fallback value for feature support, defaults to false.
140 *
141 * @return boolean Whether or not the feature is supported.
142 */
143 function gutenberg_has_border_feature_support( $block_type, $feature, $default_value = false ) {
144 // Check if all border support features have been opted into via `"__experimentalBorder": true`.
145 if ( $block_type instanceof WP_Block_Type ) {
146 $block_type_supports_border = $block_type->supports['__experimentalBorder'] ?? $default_value;
147 if ( true === $block_type_supports_border ) {
148 return true;
149 }
150 }
151
152 // Check if the specific feature has been opted into individually
153 // via nested flag under `__experimentalBorder`.
154 return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
155 }
156
157 // Register the block support.
158 WP_Block_Supports::get_instance()->register(
159 'border',
160 array(
161 'register_attribute' => 'gutenberg_register_border_support',
162 'apply' => 'gutenberg_apply_border_support',
163 )
164 );
165