18
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Layout block support flag. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @since 5.8.0 |
|
7 |
*/ |
|
8 |
|
|
9 |
/** |
|
10 |
* Registers the layout block attribute for block types that support it. |
|
11 |
* |
|
12 |
* @since 5.8.0 |
|
13 |
* @access private |
|
14 |
* |
|
15 |
* @param WP_Block_Type $block_type Block Type. |
|
16 |
*/ |
|
17 |
function wp_register_layout_support( $block_type ) { |
|
18 |
$support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
|
19 |
if ( $support_layout ) { |
|
20 |
if ( ! $block_type->attributes ) { |
|
21 |
$block_type->attributes = array(); |
|
22 |
} |
|
23 |
|
|
24 |
if ( ! array_key_exists( 'layout', $block_type->attributes ) ) { |
|
25 |
$block_type->attributes['layout'] = array( |
|
26 |
'type' => 'object', |
|
27 |
); |
|
28 |
} |
|
29 |
} |
|
30 |
} |
|
31 |
|
|
32 |
/** |
19
|
33 |
* Generates the CSS corresponding to the provided layout. |
|
34 |
* |
|
35 |
* @since 5.9.0 |
|
36 |
* @access private |
|
37 |
* |
|
38 |
* @param string $selector CSS selector. |
|
39 |
* @param array $layout Layout object. The one that is passed has already checked |
|
40 |
* the existence of default block layout. |
|
41 |
* @param boolean $has_block_gap_support Whether the theme has support for the block gap. |
|
42 |
* @param string $gap_value The block gap value to apply. |
|
43 |
* @param boolean $should_skip_gap_serialization Whether to skip applying the user-defined value set in the editor. |
|
44 |
* @param string $fallback_gap_value The custom fallback value for block gap. |
|
45 |
* @return string CSS style. |
|
46 |
*/ |
|
47 |
function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em' ) { |
|
48 |
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default'; |
|
49 |
|
|
50 |
$style = ''; |
|
51 |
if ( 'default' === $layout_type ) { |
|
52 |
$content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : ''; |
|
53 |
$wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : ''; |
|
54 |
|
|
55 |
$all_max_width_value = $content_size ? $content_size : $wide_size; |
|
56 |
$wide_max_width_value = $wide_size ? $wide_size : $content_size; |
|
57 |
|
|
58 |
// Make sure there is a single CSS rule, and all tags are stripped for security. |
|
59 |
$all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] ); |
|
60 |
$wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] ); |
|
61 |
|
|
62 |
if ( $content_size || $wide_size ) { |
|
63 |
$style = "$selector > :where(:not(.alignleft):not(.alignright)) {"; |
|
64 |
$style .= 'max-width: ' . esc_html( $all_max_width_value ) . ';'; |
|
65 |
$style .= 'margin-left: auto !important;'; |
|
66 |
$style .= 'margin-right: auto !important;'; |
|
67 |
$style .= '}'; |
|
68 |
|
|
69 |
$style .= "$selector > .alignwide { max-width: " . esc_html( $wide_max_width_value ) . ';}'; |
|
70 |
$style .= "$selector .alignfull { max-width: none; }"; |
|
71 |
} |
|
72 |
|
|
73 |
$style .= "$selector > .alignleft { float: left; margin-inline-start: 0; margin-inline-end: 2em; }"; |
|
74 |
$style .= "$selector > .alignright { float: right; margin-inline-start: 2em; margin-inline-end: 0; }"; |
|
75 |
$style .= "$selector > .aligncenter { margin-left: auto !important; margin-right: auto !important; }"; |
|
76 |
if ( $has_block_gap_support ) { |
|
77 |
if ( is_array( $gap_value ) ) { |
|
78 |
$gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null; |
|
79 |
} |
|
80 |
$gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : 'var( --wp--style--block-gap )'; |
|
81 |
$style .= "$selector > * { margin-block-start: 0; margin-block-end: 0; }"; |
|
82 |
$style .= "$selector > * + * { margin-block-start: $gap_style; margin-block-end: 0; }"; |
|
83 |
} |
|
84 |
} elseif ( 'flex' === $layout_type ) { |
|
85 |
$layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal'; |
|
86 |
|
|
87 |
$justify_content_options = array( |
|
88 |
'left' => 'flex-start', |
|
89 |
'right' => 'flex-end', |
|
90 |
'center' => 'center', |
|
91 |
); |
|
92 |
|
|
93 |
if ( 'horizontal' === $layout_orientation ) { |
|
94 |
$justify_content_options += array( 'space-between' => 'space-between' ); |
|
95 |
} |
|
96 |
|
|
97 |
$flex_wrap_options = array( 'wrap', 'nowrap' ); |
|
98 |
$flex_wrap = ! empty( $layout['flexWrap'] ) && in_array( $layout['flexWrap'], $flex_wrap_options, true ) ? |
|
99 |
$layout['flexWrap'] : |
|
100 |
'wrap'; |
|
101 |
|
|
102 |
$style = "$selector {"; |
|
103 |
$style .= 'display: flex;'; |
|
104 |
if ( $has_block_gap_support ) { |
|
105 |
if ( is_array( $gap_value ) ) { |
|
106 |
$gap_row = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap_value; |
|
107 |
$gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap_value; |
|
108 |
$gap_value = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column; |
|
109 |
} |
|
110 |
$gap_style = $gap_value && ! $should_skip_gap_serialization ? $gap_value : "var( --wp--style--block-gap, $fallback_gap_value )"; |
|
111 |
$style .= "gap: $gap_style;"; |
|
112 |
} else { |
|
113 |
$style .= "gap: $fallback_gap_value;"; |
|
114 |
} |
|
115 |
|
|
116 |
$style .= "flex-wrap: $flex_wrap;"; |
|
117 |
if ( 'horizontal' === $layout_orientation ) { |
|
118 |
$style .= 'align-items: center;'; |
|
119 |
/** |
|
120 |
* Add this style only if is not empty for backwards compatibility, |
|
121 |
* since we intend to convert blocks that had flex layout implemented |
|
122 |
* by custom css. |
|
123 |
*/ |
|
124 |
if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { |
|
125 |
$style .= "justify-content: {$justify_content_options[ $layout['justifyContent'] ]};"; |
|
126 |
} |
|
127 |
} else { |
|
128 |
$style .= 'flex-direction: column;'; |
|
129 |
if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { |
|
130 |
$style .= "align-items: {$justify_content_options[ $layout['justifyContent'] ]};"; |
|
131 |
} else { |
|
132 |
$style .= 'align-items: flex-start;'; |
|
133 |
} |
|
134 |
} |
|
135 |
$style .= '}'; |
|
136 |
|
|
137 |
$style .= "$selector > * { margin: 0; }"; |
|
138 |
} |
|
139 |
|
|
140 |
return $style; |
|
141 |
} |
|
142 |
|
|
143 |
/** |
18
|
144 |
* Renders the layout config to the block wrapper. |
|
145 |
* |
|
146 |
* @since 5.8.0 |
|
147 |
* @access private |
|
148 |
* |
19
|
149 |
* @param string $block_content Rendered block content. |
|
150 |
* @param array $block Block object. |
|
151 |
* @return string Filtered block content. |
18
|
152 |
*/ |
|
153 |
function wp_render_layout_support_flag( $block_content, $block ) { |
|
154 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
|
155 |
$support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
19
|
156 |
|
|
157 |
if ( ! $support_layout ) { |
18
|
158 |
return $block_content; |
|
159 |
} |
|
160 |
|
19
|
161 |
$block_gap = wp_get_global_settings( array( 'spacing', 'blockGap' ) ); |
|
162 |
$default_layout = wp_get_global_settings( array( 'layout' ) ); |
|
163 |
$has_block_gap_support = isset( $block_gap ) ? null !== $block_gap : false; |
|
164 |
$default_block_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout', 'default' ), array() ); |
|
165 |
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $default_block_layout; |
18
|
166 |
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] ) { |
|
167 |
if ( ! $default_layout ) { |
|
168 |
return $block_content; |
|
169 |
} |
|
170 |
$used_layout = $default_layout; |
|
171 |
} |
|
172 |
|
19
|
173 |
$class_names = array(); |
|
174 |
$container_class = wp_unique_id( 'wp-container-' ); |
|
175 |
$class_names[] = $container_class; |
18
|
176 |
|
19
|
177 |
// The following section was added to reintroduce a small set of layout classnames that were |
|
178 |
// removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is |
|
179 |
// not intended to provide an extended set of classes to match all block layout attributes |
|
180 |
// here. |
|
181 |
if ( ! empty( $block['attrs']['layout']['orientation'] ) ) { |
|
182 |
$class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] ); |
|
183 |
} |
18
|
184 |
|
19
|
185 |
if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) { |
|
186 |
$class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] ); |
18
|
187 |
} |
|
188 |
|
19
|
189 |
if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) { |
|
190 |
$class_names[] = 'is-nowrap'; |
|
191 |
} |
18
|
192 |
|
19
|
193 |
$gap_value = _wp_array_get( $block, array( 'attrs', 'style', 'spacing', 'blockGap' ) ); |
|
194 |
// Skip if gap value contains unsupported characters. |
|
195 |
// Regex for CSS value borrowed from `safecss_filter_attr`, and used here |
|
196 |
// because we only want to match against the value, not the CSS attribute. |
|
197 |
if ( is_array( $gap_value ) ) { |
|
198 |
foreach ( $gap_value as $key => $value ) { |
|
199 |
$gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; |
|
200 |
} |
|
201 |
} else { |
|
202 |
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; |
|
203 |
} |
|
204 |
|
|
205 |
$fallback_gap_value = _wp_array_get( $block_type->supports, array( 'spacing', 'blockGap', '__experimentalDefault' ), '0.5em' ); |
|
206 |
|
|
207 |
// If a block's block.json skips serialization for spacing or spacing.blockGap, |
|
208 |
// don't apply the user-defined value to the styles. |
|
209 |
$should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' ); |
|
210 |
$style = wp_get_layout_style( ".$container_class", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization, $fallback_gap_value ); |
18
|
211 |
// This assumes the hook only applies to blocks with a single wrapper. |
|
212 |
// I think this is a reasonable limitation for that particular hook. |
|
213 |
$content = preg_replace( |
|
214 |
'/' . preg_quote( 'class="', '/' ) . '/', |
19
|
215 |
'class="' . esc_attr( implode( ' ', $class_names ) ) . ' ', |
18
|
216 |
$block_content, |
|
217 |
1 |
|
218 |
); |
|
219 |
|
19
|
220 |
wp_enqueue_block_support_styles( $style ); |
|
221 |
|
|
222 |
return $content; |
18
|
223 |
} |
|
224 |
|
|
225 |
// Register the block support. |
|
226 |
WP_Block_Supports::get_instance()->register( |
|
227 |
'layout', |
|
228 |
array( |
|
229 |
'register_attribute' => 'wp_register_layout_support', |
|
230 |
) |
|
231 |
); |
|
232 |
add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 ); |
|
233 |
|
|
234 |
/** |
|
235 |
* For themes without theme.json file, make sure |
|
236 |
* to restore the inner div for the group block |
|
237 |
* to avoid breaking styles relying on that div. |
|
238 |
* |
|
239 |
* @since 5.8.0 |
|
240 |
* @access private |
|
241 |
* |
|
242 |
* @param string $block_content Rendered block content. |
|
243 |
* @param array $block Block object. |
|
244 |
* @return string Filtered block content. |
|
245 |
*/ |
|
246 |
function wp_restore_group_inner_container( $block_content, $block ) { |
19
|
247 |
$tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div'; |
|
248 |
$group_with_inner_container_regex = sprintf( |
|
249 |
'/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U', |
|
250 |
preg_quote( $tag_name, '/' ) |
|
251 |
); |
18
|
252 |
|
|
253 |
if ( |
|
254 |
WP_Theme_JSON_Resolver::theme_has_support() || |
19
|
255 |
1 === preg_match( $group_with_inner_container_regex, $block_content ) || |
|
256 |
( isset( $block['attrs']['layout']['type'] ) && 'default' !== $block['attrs']['layout']['type'] ) |
18
|
257 |
) { |
|
258 |
return $block_content; |
|
259 |
} |
|
260 |
|
19
|
261 |
$replace_regex = sprintf( |
|
262 |
'/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms', |
|
263 |
preg_quote( $tag_name, '/' ) |
|
264 |
); |
18
|
265 |
$updated_content = preg_replace_callback( |
|
266 |
$replace_regex, |
19
|
267 |
static function( $matches ) { |
18
|
268 |
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3]; |
|
269 |
}, |
|
270 |
$block_content |
|
271 |
); |
|
272 |
return $updated_content; |
|
273 |
} |
|
274 |
|
19
|
275 |
add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 ); |
|
276 |
|
|
277 |
/** |
|
278 |
* For themes without theme.json file, make sure |
|
279 |
* to restore the outer div for the aligned image block |
|
280 |
* to avoid breaking styles relying on that div. |
|
281 |
* |
|
282 |
* @since 6.0.0 |
|
283 |
* @access private |
|
284 |
* |
|
285 |
* @param string $block_content Rendered block content. |
|
286 |
* @param array $block Block object. |
|
287 |
* @return string Filtered block content. |
|
288 |
*/ |
|
289 |
function wp_restore_image_outer_container( $block_content, $block ) { |
|
290 |
$image_with_align = " |
|
291 |
/# 1) everything up to the class attribute contents |
|
292 |
( |
|
293 |
^\s* |
|
294 |
<figure\b |
|
295 |
[^>]* |
|
296 |
\bclass= |
|
297 |
[\"'] |
|
298 |
) |
|
299 |
# 2) the class attribute contents |
|
300 |
( |
|
301 |
[^\"']* |
|
302 |
\bwp-block-image\b |
|
303 |
[^\"']* |
|
304 |
\b(?:alignleft|alignright|aligncenter)\b |
|
305 |
[^\"']* |
|
306 |
) |
|
307 |
# 3) everything after the class attribute contents |
|
308 |
( |
|
309 |
[\"'] |
|
310 |
[^>]* |
|
311 |
> |
|
312 |
.* |
|
313 |
<\/figure> |
|
314 |
)/iUx"; |
|
315 |
|
|
316 |
if ( |
|
317 |
WP_Theme_JSON_Resolver::theme_has_support() || |
|
318 |
0 === preg_match( $image_with_align, $block_content, $matches ) |
|
319 |
) { |
|
320 |
return $block_content; |
|
321 |
} |
|
322 |
|
|
323 |
$wrapper_classnames = array( 'wp-block-image' ); |
|
324 |
|
|
325 |
// If the block has a classNames attribute these classnames need to be removed from the content and added back |
|
326 |
// to the new wrapper div also. |
|
327 |
if ( ! empty( $block['attrs']['className'] ) ) { |
|
328 |
$wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) ); |
|
329 |
} |
|
330 |
$content_classnames = explode( ' ', $matches[2] ); |
|
331 |
$filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames ); |
|
332 |
|
|
333 |
return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>'; |
|
334 |
} |
|
335 |
|
|
336 |
add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 ); |