PluginProbe
Gutenberg / 17.0.2
Gutenberg v17.0.2
24.0.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 All 403 releases
← All changes | lib/block-supports/layout.php +188 -826 23.7.0 → 17.0.2 View file →
@@ -5,37 +5,8 @@
5 5 * @package gutenberg
6 6 */
7 7
8 8 /**
9 - * Get the first style variation name from a className string that matches a registered style.
10 - *
11 - * @param string $class_name CSS class string for a block.
12 - * @param array<string, array<string, mixed>> $registered_styles Currently registered block styles.
13 - * @return string|null The name of the first registered variation, or null if none found.
14 - */
15 -function gutenberg_get_block_style_variation_name_from_registered_style( string $class_name, array $registered_styles = array() ): ?string {
16 - if ( ! $class_name ) {
17 - return null;
18 - }
19 -
20 - $registered_names = array_filter( array_column( $registered_styles, 'name' ) );
21 -
22 - $prefix = 'is-style-';
23 - $length = strlen( $prefix );
24 -
25 - foreach ( explode( ' ', $class_name ) as $class ) {
26 - if ( str_starts_with( $class, $prefix ) ) {
27 - $variation = substr( $class, $length );
28 - if ( 'default' !== $variation && in_array( $variation, $registered_names, true ) ) {
29 - return $variation;
30 - }
31 - }
32 - }
33 -
34 - return null;
35 -}
36 -
37 -/**
38 9 * Returns layout definitions, keyed by layout type.
39 10 *
40 11 * Provides a common definition of slugs, classnames, base styles, and spacing styles for each layout type.
41 12 * When making changes or additions to layout definitions, the corresponding JavaScript definitions should
@@ -75,15 +46,15 @@
75 46 ),
76 47 ),
77 48 'spacingStyles' => array(
78 49 array(
79 - 'selector' => ' > :first-child',
50 + 'selector' => ' > :first-child:first-child',
80 51 'rules' => array(
81 52 'margin-block-start' => '0',
82 53 ),
83 54 ),
84 55 array(
85 - 'selector' => ' > :last-child',
56 + 'selector' => ' > :last-child:last-child',
86 57 'rules' => array(
87 58 'margin-block-end' => '0',
88 59 ),
89 60 ),
@@ -140,15 +111,15 @@
140 111 ),
141 112 ),
142 113 'spacingStyles' => array(
143 114 array(
144 - 'selector' => ' > :first-child',
115 + 'selector' => ' > :first-child:first-child',
145 116 'rules' => array(
146 117 'margin-block-start' => '0',
147 118 ),
148 119 ),
149 120 array(
150 - 'selector' => ' > :last-child',
121 + 'selector' => ' > :last-child:last-child',
151 122 'rules' => array(
152 123 'margin-block-end' => '0',
153 124 ),
154 125 ),
@@ -174,9 +145,9 @@
174 145 'align-items' => 'center',
175 146 ),
176 147 ),
177 148 array(
178 - 'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
149 + 'selector' => ' > *',
179 150 'rules' => array(
180 151 'margin' => '0',
181 152 ),
182 153 ),
@@ -196,9 +167,9 @@
196 167 'className' => 'is-layout-grid',
197 168 'displayMode' => 'grid',
198 169 'baseStyles' => array(
199 170 array(
200 - 'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
171 + 'selector' => ' > *',
201 172 'rules' => array(
202 173 'margin' => '0',
203 174 ),
204 175 ),
@@ -237,228 +208,8 @@
237 208 }
238 209 }
239 210
240 211 /**
241 - * Returns the child-layout-only subset of a layout object.
242 - *
243 - * Child layout keys are the ones controlling how the block lays itself out
244 - * inside its parent's grid or flex container.
245 - *
246 - * @param mixed $layout Layout object.
247 - * @return array Child layout values, or an empty array.
248 - */
249 -function gutenberg_get_layout_child_values( $layout ) {
250 - if ( ! is_array( $layout ) ) {
251 - return array();
252 - }
253 -
254 - return array_intersect_key(
255 - $layout,
256 - array_flip(
257 - array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' )
258 - )
259 - );
260 -}
261 -
262 -/**
263 - * Returns the container-layout subset of a layout object (everything except child layout keys).
264 - *
265 - * @param mixed $layout Layout object.
266 - * @return array Container layout values, or an empty array.
267 - */
268 -function gutenberg_get_layout_container_values( $layout ) {
269 - if ( ! is_array( $layout ) ) {
270 - return array();
271 - }
272 -
273 - return array_diff_key(
274 - $layout,
275 - array_flip(
276 - array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' )
277 - )
278 - );
279 -}
280 -
281 -/**
282 - * Sanitizes a block gap value before layout style generation.
283 - *
284 - * Regex for CSS value borrowed from `safecss_filter_attr`, used here to only match
285 - * against the value, not the CSS attribute.
286 - *
287 - * @param string|array|null $gap_value Block gap value.
288 - * @return string|array|null Sanitized block gap value.
289 - */
290 -function gutenberg_sanitize_block_gap_value( $gap_value ) {
291 - if ( is_array( $gap_value ) ) {
292 - foreach ( $gap_value as $key => $value ) {
293 - $gap_value[ $key ] = ! is_scalar( $value ) || ( $value && preg_match( '%[\\\(&=}]|/\*%', (string) $value ) ) ? null : $value;
294 - }
295 - return $gap_value;
296 - }
297 -
298 - return $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
299 -}
300 -
301 -/**
302 - * Returns child layout styles for a block affected by its parent's layout.
303 - *
304 - * @param string $selector CSS selector.
305 - * @param array $child_layout Child layout values.
306 - * @param array $parent_layout Parent layout values.
307 - * @param array|null $viewport_overrides Optional. Child viewport layout overrides to emit.
308 - * @return array Child layout style rules.
309 - */
310 -function gutenberg_get_child_layout_style_rules( $selector, $child_layout, $parent_layout = array(), $viewport_overrides = null ) {
311 - $base_child_layout = is_array( $child_layout ) ? $child_layout : array();
312 - $viewport_overrides = is_array( $viewport_overrides ) ? $viewport_overrides : null;
313 - $child_layout = null === $viewport_overrides ? $base_child_layout : array_replace( $base_child_layout, $viewport_overrides );
314 - $child_layout_declarations = array();
315 - $child_layout_styles = array();
316 - $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
317 - return array_key_exists( $property, $viewport_overrides );
318 - };
319 -
320 - $self_stretch = $child_layout['selfStretch'] ?? null;
321 - $base_self_stretch = $base_child_layout['selfStretch'] ?? null;
322 -
323 - /*
324 - * These are the serialized `selfStretch` values. `max` used to be called
325 - * "Fixed" in the UI, but was renamed and replaced by `fixedNoShrink`.
326 - */
327 - $flex_child_layout_values = array(
328 - 'fit' => 'fit',
329 - 'grow' => 'fill',
330 - 'max' => 'fixed',
331 - 'fixed' => 'fixedNoShrink',
332 - );
333 - $flex_size_values = array(
334 - $flex_child_layout_values['max'],
335 - $flex_child_layout_values['fixed'],
336 - );
337 -
338 - if ( null === $viewport_overrides || $has_viewport_property_override( 'selfStretch' ) || $has_viewport_property_override( 'flexSize' ) ) {
339 - if (
340 - null !== $viewport_overrides &&
341 - ( $flex_child_layout_values['fit'] === $self_stretch || $flex_child_layout_values['grow'] === $self_stretch ) &&
342 - in_array( $base_self_stretch, $flex_size_values, true ) &&
343 - isset( $base_child_layout['flexSize'] )
344 - ) {
345 - $child_layout_declarations['flex-basis'] = 'unset';
346 - if ( $flex_child_layout_values['fixed'] === $base_self_stretch ) {
347 - $child_layout_declarations['flex-shrink'] = 'unset';
348 - }
349 - }
350 - if ( in_array( $self_stretch, $flex_size_values, true ) && isset( $child_layout['flexSize'] ) ) {
351 - $child_layout_declarations['flex-basis'] = $child_layout['flexSize'];
352 - if ( $flex_child_layout_values['fixed'] === $self_stretch ) {
353 - $child_layout_declarations['flex-shrink'] = '0';
354 - } elseif ( null !== $viewport_overrides && $flex_child_layout_values['fixed'] === $base_self_stretch ) {
355 - $child_layout_declarations['flex-shrink'] = 'unset';
356 - }
357 - $child_layout_declarations['box-sizing'] = 'border-box';
358 - } elseif ( $flex_child_layout_values['grow'] === $self_stretch ) {
359 - $child_layout_declarations['flex-grow'] = '1';
360 - }
361 - }
362 -
363 - $column_start = $child_layout['columnStart'] ?? null;
364 - $column_span = $child_layout['columnSpan'] ?? null;
365 - if ( null === $viewport_overrides || $has_viewport_property_override( 'columnStart' ) || $has_viewport_property_override( 'columnSpan' ) ) {
366 - if ( $column_start && $column_span ) {
367 - $child_layout_declarations['grid-column'] = "$column_start / span $column_span";
368 - } elseif ( $column_start ) {
369 - $child_layout_declarations['grid-column'] = "$column_start";
370 - } elseif ( $column_span ) {
371 - $child_layout_declarations['grid-column'] = "span $column_span";
372 - }
373 - }
374 -
375 - $row_start = $child_layout['rowStart'] ?? null;
376 - $row_span = $child_layout['rowSpan'] ?? null;
377 - if ( null === $viewport_overrides || $has_viewport_property_override( 'rowStart' ) || $has_viewport_property_override( 'rowSpan' ) ) {
378 - if ( $row_start && $row_span ) {
379 - $child_layout_declarations['grid-row'] = "$row_start / span $row_span";
380 - } elseif ( $row_start ) {
381 - $child_layout_declarations['grid-row'] = "$row_start";
382 - } elseif ( $row_span ) {
383 - $child_layout_declarations['grid-row'] = "span $row_span";
384 - }
385 - }
386 -
387 - if ( ! empty( $child_layout_declarations ) ) {
388 - $child_layout_styles[] = array(
389 - 'selector' => $selector,
390 - 'declarations' => $child_layout_declarations,
391 - );
392 - }
393 -
394 - $minimum_column_width = $parent_layout['minimumColumnWidth'] ?? null;
395 - $column_count = $parent_layout['columnCount'] ?? null;
396 -
397 - /*
398 - * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
399 - * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed
400 - * once the grid has less columns than the start.
401 - * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set.
402 - * In that case, if columnCount doesn't exist, we can assume that the grid is responsive.
403 - */
404 - if ( null === $viewport_overrides && ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) {
405 - $column_span_number = floatval( $column_span );
406 - $column_start_number = floatval( $column_start );
407 - $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem';
408 - $parent_column_value = floatval( $parent_column_width );
409 - $parent_column_unit = explode( $parent_column_value, $parent_column_width );
410 -
411 - $num_cols_to_break_at = 2;
412 - if ( $column_span_number && $column_start_number ) {
413 - $num_cols_to_break_at = $column_start_number + $column_span_number - 1;
414 - } elseif ( $column_span_number ) {
415 - $num_cols_to_break_at = $column_span_number;
416 - } else {
417 - $num_cols_to_break_at = $column_start_number;
418 - }
419 -
420 - /*
421 - * If there is no unit, the width has somehow been mangled so we reset both unit and value
422 - * to defaults.
423 - * Additionally, the unit should be one of px, rem or em, so that also needs to be checked.
424 - */
425 - if ( count( $parent_column_unit ) <= 1 ) {
426 - $parent_column_unit = 'rem';
427 - $parent_column_value = 12;
428 - } else {
429 - $parent_column_unit = $parent_column_unit[1];
430 -
431 - if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) {
432 - $parent_column_unit = 'rem';
433 - }
434 - }
435 -
436 - /*
437 - * A default gap value is used for this computation because custom gap values may not be
438 - * viable to use in the computation of the container query value.
439 - */
440 - $default_gap_value = 'px' === $parent_column_unit ? 24 : 1.5;
441 - $container_query_value = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value;
442 - $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1;
443 - $container_query_value = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit;
444 - // If a span is set we want to preserve it as long as possible, otherwise we just reset the value.
445 - $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto';
446 -
447 - $child_layout_styles[] = array(
448 - 'rules_group' => "@container (max-width: $container_query_value )",
449 - 'selector' => $selector,
450 - 'declarations' => array(
451 - 'grid-column' => $grid_column_value,
452 - 'grid-row' => 'auto',
453 - ),
454 - );
455 - }
456 -
457 - return $child_layout_styles;
458 -}
459 -
460 -/**
461 212 * Generates the CSS corresponding to the provided layout.
462 213 *
463 214 * @param string $selector CSS selector.
464 215 * @param array $layout Layout object. The one that is passed has already checked
@@ -465,33 +216,20 @@
465 216 * the existence of default block layout.
466 217 * @param bool $has_block_gap_support Optional. Whether the theme has support for the block gap. Default false.
467 218 * @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null.
468 219 * @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
469 - * @param string|array $fallback_gap_value Optional. The block gap value to apply. If it's an array expected properties are "top" and/or "left". Default '0.5em'.
220 + * @param string $fallback_gap_value Optional. The block gap value to apply. Default '0.5em'.
470 221 * @param array|null $block_spacing Optional. Custom spacing set on the block. Default null.
471 - * @param array $options Optional. Extra options for internal callers. Default empty array.
472 - * @return string CSS styles, or empty string.
222 + * @return string CSS styles on success. Else, empty string.
473 223 */
474 -function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null, $options = array() ) {
475 - $base_layout = is_array( $layout ) ? $layout : array();
476 - $viewport_overrides = $options['viewport_overrides'] ?? null;
477 - $layout_for_styles = null === $viewport_overrides ? $base_layout : array_replace( $base_layout, $viewport_overrides );
478 - $layout_type = $base_layout['type'] ?? 'default';
479 - $rules_group = $options['rules_group'] ?? null;
480 - $has_block_gap_override = ! empty( $options['has_block_gap_override'] );
481 - $should_output_block_gap = null === $viewport_overrides || $has_block_gap_override;
482 - // Viewport styles only store changed fields. If a field is present with null,
483 - // the user cleared a value inherited from the default viewport, so check
484 - // whether the key exists rather than whether the value is truthy.
485 - $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
486 - return array_key_exists( $property, $viewport_overrides );
487 - };
488 - $layout_styles = array();
224 +function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null ) {
225 + $layout_type = $layout['type'] ?? 'default';
226 + $layout_styles = array();
489 227
490 228 if ( 'default' === $layout_type ) {
491 - if ( $has_block_gap_support && $should_output_block_gap ) {
229 + if ( $has_block_gap_support ) {
492 230 if ( is_array( $gap_value ) ) {
493 - $gap_value = $gap_value['top'] ?? null;
231 + $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
494 232 }
495 233 if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
496 234 // Get spacing CSS variable from preset value if provided.
497 235 if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
@@ -509,9 +247,9 @@
509 247 'margin-block-end' => '0',
510 248 ),
511 249 ),
512 250 array(
513 - 'selector' => "$selector > * + *",
251 + 'selector' => "$selector$selector > * + *",
514 252 'declarations' => array(
515 253 'margin-block-start' => $gap_value,
516 254 'margin-block-end' => '0',
517 255 ),
@@ -519,35 +257,15 @@
519 257 );
520 258 }
521 259 }
522 260 } elseif ( 'constrained' === $layout_type ) {
523 - $content_size = $layout_for_styles['contentSize'] ?? '';
524 - $wide_size = $layout_for_styles['wideSize'] ?? '';
525 - $justify_content = $layout_for_styles['justifyContent'] ?? 'center';
261 + $content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : '';
262 + $wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : '';
263 + $justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'center';
526 264
527 - // Check if viewport-specific ("override") values exist. Null values are valid and mean the user cleared a value inherited from the default viewport.
528 - $has_justify_content_override = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' );
529 - $has_content_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'contentSize' );
530 - $has_wide_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'wideSize' );
265 + $all_max_width_value = $content_size ? $content_size : $wide_size;
266 + $wide_max_width_value = $wide_size ? $wide_size : $content_size;
531 267
532 - /* Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
533 - * value for contentSize or wideSize. If a viewport clears a custom constrained size, reset to the global layout variable.
534 - */
535 - $should_output_constrained_sizes = null === $viewport_overrides || $has_content_size_override || $has_wide_size_override;
536 - $is_resetting_constrained_sizes = null !== $viewport_overrides &&
537 - (
538 - ( $has_content_size_override && ! $content_size ) ||
539 - ( $has_wide_size_override && ! $wide_size )
540 - );
541 -
542 - // If a viewport clears a custom constrained size, reset to the global layout variable.
543 - $all_max_width_value = $content_size
544 - ? $content_size
545 - : ( $wide_size && ! $has_content_size_override ? $wide_size : 'var(--wp--style--global--content-size, none)' );
546 - $wide_max_width_value = $wide_size
547 - ? $wide_size
548 - : ( $content_size && ! $has_wide_size_override ? $content_size : 'var(--wp--style--global--wide-size, none)' );
549 -
550 268 // Make sure there is a single CSS rule, and all tags are stripped for security.
551 269 $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
552 270 $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
553 271
@@ -553,23 +271,18 @@
553 271
554 272 $margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important';
555 273 $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
556 274
557 - if ( $should_output_constrained_sizes && ( $content_size || $wide_size || $is_resetting_constrained_sizes ) ) {
558 - $content_size_declarations = array(
559 - 'max-width' => $all_max_width_value,
560 - );
561 -
562 - if ( null === $viewport_overrides || $has_justify_content_override ) {
563 - $content_size_declarations['margin-left'] = $margin_left;
564 - $content_size_declarations['margin-right'] = $margin_right;
565 - }
566 -
275 + if ( $content_size || $wide_size ) {
567 276 array_push(
568 277 $layout_styles,
569 278 array(
570 279 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
571 - 'declarations' => $content_size_declarations,
280 + 'declarations' => array(
281 + 'max-width' => $all_max_width_value,
282 + 'margin-left' => $margin_left,
283 + 'margin-right' => $margin_right,
284 + ),
572 285 ),
573 286 array(
574 287 'selector' => "$selector > .alignwide",
575 288 'declarations' => array( 'max-width' => $wide_max_width_value ),
@@ -578,72 +291,54 @@
578 291 'selector' => "$selector .alignfull",
579 292 'declarations' => array( 'max-width' => 'none' ),
580 293 )
581 294 );
582 - }
583 295
584 - if ( null === $viewport_overrides && isset( $block_spacing ) ) {
585 - $block_spacing_values = gutenberg_style_engine_get_styles(
586 - array(
587 - 'spacing' => $block_spacing,
588 - )
589 - );
296 + if ( isset( $block_spacing ) ) {
297 + $block_spacing_values = gutenberg_style_engine_get_styles(
298 + array(
299 + 'spacing' => $block_spacing,
300 + )
301 + );
590 302
591 - /*
592 - * Handle negative margins for alignfull children of blocks with custom padding set.
593 - * They're added separately because padding might only be set on one side.
594 - */
595 - if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
596 - $padding_right = $block_spacing_values['declarations']['padding-right'];
597 - // Add unit if 0.
598 - if ( '0' === $padding_right ) {
599 - $padding_right = '0px';
303 + /*
304 + * Handle negative margins for alignfull children of blocks with custom padding set.
305 + * They're added separately because padding might only be set on one side.
306 + */
307 + if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
308 + $padding_right = $block_spacing_values['declarations']['padding-right'];
309 + $layout_styles[] = array(
310 + 'selector' => "$selector > .alignfull",
311 + 'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
312 + );
600 313 }
601 - $layout_styles[] = array(
602 - 'selector' => "$selector > .alignfull",
603 - 'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
604 - );
605 - }
606 - if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
607 - $padding_left = $block_spacing_values['declarations']['padding-left'];
608 - // Add unit if 0.
609 - if ( '0' === $padding_left ) {
610 - $padding_left = '0px';
314 + if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
315 + $padding_left = $block_spacing_values['declarations']['padding-left'];
316 + $layout_styles[] = array(
317 + 'selector' => "$selector > .alignfull",
318 + 'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
319 + );
611 320 }
612 - $layout_styles[] = array(
613 - 'selector' => "$selector > .alignfull",
614 - 'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
615 - );
616 321 }
617 322 }
618 323
619 - if ( $has_justify_content_override && ! $should_output_constrained_sizes ) {
324 + if ( 'left' === $justify_content ) {
620 325 $layout_styles[] = array(
621 326 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
622 - 'declarations' => array(
623 - 'margin-left' => $margin_left,
624 - 'margin-right' => $margin_right,
625 - ),
327 + 'declarations' => array( 'margin-left' => '0 !important' ),
626 328 );
627 - } elseif ( null === $viewport_overrides ) {
628 - if ( 'left' === $justify_content ) {
629 - $layout_styles[] = array(
630 - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
631 - 'declarations' => array( 'margin-left' => '0 !important' ),
632 - );
633 - }
329 + }
634 330
635 - if ( 'right' === $justify_content ) {
636 - $layout_styles[] = array(
637 - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
638 - 'declarations' => array( 'margin-right' => '0 !important' ),
639 - );
640 - }
331 + if ( 'right' === $justify_content ) {
332 + $layout_styles[] = array(
333 + 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
334 + 'declarations' => array( 'margin-right' => '0 !important' ),
335 + );
641 336 }
642 337
643 - if ( $has_block_gap_support && $should_output_block_gap ) {
338 + if ( $has_block_gap_support ) {
644 339 if ( is_array( $gap_value ) ) {
645 - $gap_value = $gap_value['top'] ?? null;
340 + $gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
646 341 }
647 342 if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
648 343 // Get spacing CSS variable from preset value if provided.
649 344 if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
@@ -661,9 +356,9 @@
661 356 'margin-block-end' => '0',
662 357 ),
663 358 ),
664 359 array(
665 - 'selector' => "$selector > * + *",
360 + 'selector' => "$selector$selector > * + *",
666 361 'declarations' => array(
667 362 'margin-block-start' => $gap_value,
668 363 'margin-block-end' => '0',
669 364 ),
@@ -671,9 +366,9 @@
671 366 );
672 367 }
673 368 }
674 369 } elseif ( 'flex' === $layout_type ) {
675 - $layout_orientation = $layout_for_styles['orientation'] ?? 'horizontal';
370 + $layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal';
676 371
677 372 $justify_content_options = array(
678 373 'left' => 'flex-start',
679 374 'right' => 'flex-end',
@@ -693,17 +388,9 @@
693 388 $justify_content_options += array( 'stretch' => 'stretch' );
694 389 $vertical_alignment_options += array( 'space-between' => 'space-between' );
695 390 }
696 391
697 - /* Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
698 - * value for any of the flex properties.
699 - */
700 - $should_output_flex_wrap = null === $viewport_overrides || $has_viewport_property_override( 'flexWrap' );
701 - $should_output_flex_orientation = null === $viewport_overrides || $has_viewport_property_override( 'orientation' );
702 - $should_output_flex_justification = null === $viewport_overrides || $has_viewport_property_override( 'justifyContent' ) || $has_viewport_property_override( 'orientation' );
703 - $should_output_flex_alignment = null === $viewport_overrides || $has_viewport_property_override( 'verticalAlignment' ) || $has_viewport_property_override( 'orientation' );
704 -
705 - if ( $should_output_flex_wrap && ! empty( $layout_for_styles['flexWrap'] ) && 'nowrap' === $layout_for_styles['flexWrap'] ) {
392 + if ( ! empty( $layout['flexWrap'] ) && 'nowrap' === $layout['flexWrap'] ) {
706 393 $layout_styles[] = array(
707 394 'selector' => $selector,
708 395 'declarations' => array( 'flex-wrap' => 'nowrap' ),
709 396 );
@@ -708,9 +395,9 @@
708 395 'declarations' => array( 'flex-wrap' => 'nowrap' ),
709 396 );
710 397 }
711 398
712 - if ( $has_block_gap_support && $should_output_block_gap && isset( $gap_value ) ) {
399 + if ( $has_block_gap_support && isset( $gap_value ) ) {
713 400 $combined_gap_value = '';
714 401 $gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
715 402
716 403 foreach ( $gap_sides as $gap_side ) {
@@ -715,14 +402,9 @@
715 402
716 403 foreach ( $gap_sides as $gap_side ) {
717 404 $process_value = $gap_value;
718 405 if ( is_array( $gap_value ) ) {
719 - if ( is_array( $fallback_gap_value ) ) {
720 - $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
721 - } else {
722 - $fallback_value = $fallback_gap_value;
723 - }
724 - $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
406 + $process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
725 407 }
726 408 // Get spacing CSS variable from preset value if provided.
727 409 if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
728 410 $index_to_splice = strrpos( $process_value, '|' ) + 1;
@@ -746,55 +428,57 @@
746 428 * Add this style only if is not empty for backwards compatibility,
747 429 * since we intend to convert blocks that had flex layout implemented
748 430 * by custom css.
749 431 */
750 - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
432 + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
751 433 $layout_styles[] = array(
752 434 'selector' => $selector,
753 - 'declarations' => array( 'justify-content' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
435 + 'declarations' => array( 'justify-content' => $justify_content_options[ $layout['justifyContent'] ] ),
754 436 );
755 437 }
756 438
757 - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
439 + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
758 440 $layout_styles[] = array(
759 441 'selector' => $selector,
760 - 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
442 + 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
761 443 );
762 444 }
763 445 } else {
764 - if ( $should_output_flex_orientation ) {
446 + $layout_styles[] = array(
447 + 'selector' => $selector,
448 + 'declarations' => array( 'flex-direction' => 'column' ),
449 + );
450 + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
765 451 $layout_styles[] = array(
766 452 'selector' => $selector,
767 - 'declarations' => array( 'flex-direction' => 'column' ),
453 + 'declarations' => array( 'align-items' => $justify_content_options[ $layout['justifyContent'] ] ),
768 454 );
769 - }
770 - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
455 + } else {
771 456 $layout_styles[] = array(
772 457 'selector' => $selector,
773 - 'declarations' => array( 'align-items' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
774 - );
775 - } elseif ( $should_output_flex_justification ) {
776 - $layout_styles[] = array(
777 - 'selector' => $selector,
778 458 'declarations' => array( 'align-items' => 'flex-start' ),
779 459 );
780 460 }
781 - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
461 + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
782 462 $layout_styles[] = array(
783 463 'selector' => $selector,
784 - 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
464 + 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
785 465 );
786 466 }
787 467 }
788 468 } elseif ( 'grid' === $layout_type ) {
789 - /*
790 - * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
791 - * is the relevant one for computation of responsive grid columns.
792 - */
793 - if ( is_array( $fallback_gap_value ) ) {
794 - $responsive_gap_value = $fallback_gap_value['left'] ?? reset( $fallback_gap_value );
469 + if ( ! empty( $layout['columnCount'] ) ) {
470 + $layout_styles[] = array(
471 + 'selector' => $selector,
472 + 'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
473 + );
795 474 } else {
796 - $responsive_gap_value = $fallback_gap_value;
475 + $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
476 +
477 + $layout_styles[] = array(
478 + 'selector' => $selector,
479 + 'declarations' => array( 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))' ),
480 + );
797 481 }
798 482
799 483 if ( $has_block_gap_support && isset( $gap_value ) ) {
800 484 $combined_gap_value = '';
@@ -802,14 +486,9 @@
802 486
803 487 foreach ( $gap_sides as $gap_side ) {
804 488 $process_value = $gap_value;
805 489 if ( is_array( $gap_value ) ) {
806 - if ( is_array( $fallback_gap_value ) ) {
807 - $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
808 - } else {
809 - $fallback_value = $fallback_gap_value;
810 - }
811 - $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
490 + $process_value = $gap_value[ $gap_side ] ?? $fallback_gap_value;
812 491 }
813 492 // Get spacing CSS variable from preset value if provided.
814 493 if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
815 494 $index_to_splice = strrpos( $process_value, '|' ) + 1;
@@ -817,79 +496,20 @@
817 496 $process_value = "var(--wp--preset--spacing--$slug)";
818 497 }
819 498 $combined_gap_value .= "$process_value ";
820 499 }
821 - $gap_value = trim( $combined_gap_value );
822 - $responsive_gap_value = $gap_value;
823 - }
500 + $gap_value = trim( $combined_gap_value );
824 501
825 - // Ensure 0 values have a unit so they work in calc().
826 - if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) {
827 - $responsive_gap_value = '0px';
828 - }
829 -
830 - /* Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
831 - * value for any of the grid properties.
832 - */
833 - $should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' ) || $has_viewport_property_override( 'autoFit' );
834 - $uses_gap_in_grid_columns = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
835 - if ( $has_block_gap_override && $uses_gap_in_grid_columns ) {
836 - $should_output_grid_columns = true;
837 - }
838 -
839 - $should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] );
840 - $grid_declarations = array();
841 -
842 - /* When enabled, columns stretch to fill the available space using
843 - * `auto-fit`; otherwise empty tracks are preserved with `auto-fill`.
844 - */
845 - $auto_placement = ! empty( $layout_for_styles['autoFit'] ) ? 'auto-fit' : 'auto-fill';
846 -
847 - if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
848 - $max_value = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')';
849 - $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(' . $max_value . ', 1fr))';
850 - } elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) {
851 - $grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))';
852 - } elseif ( $should_output_grid_columns ) {
853 - $minimum_column_width = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem';
854 - $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(min(' . $minimum_column_width . ', 100%), 1fr))';
855 - }
856 -
857 - if ( ! empty( $grid_declarations ) ) {
858 - $base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) );
859 - if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
860 - if ( null === $viewport_overrides || ! $base_has_container_type ) {
861 - $grid_declarations['container-type'] = 'inline-size';
862 - }
502 + if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
503 + $layout_styles[] = array(
504 + 'selector' => $selector,
505 + 'declarations' => array( 'gap' => $gap_value ),
506 + );
863 507 }
864 - $layout_styles[] = array(
865 - 'selector' => $selector,
866 - 'declarations' => $grid_declarations,
867 - );
868 508 }
869 -
870 - if ( $should_output_grid_rows ) {
871 - $layout_styles[] = array(
872 - 'selector' => $selector,
873 - 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ),
874 - );
875 - }
876 -
877 - if ( $has_block_gap_support && $should_output_block_gap && null !== $gap_value && ! $should_skip_gap_serialization ) {
878 - $layout_styles[] = array(
879 - 'selector' => $selector,
880 - 'declarations' => array( 'gap' => $gap_value ),
881 - );
882 - }
883 509 }
884 510
885 511 if ( ! empty( $layout_styles ) ) {
886 - if ( ! empty( $rules_group ) ) {
887 - foreach ( $layout_styles as $index => $layout_style ) {
888 - $layout_styles[ $index ]['rules_group'] = $rules_group;
889 - }
890 - }
891 -
892 512 /*
893 513 * Add to the style engine store to enqueue and render layout styles.
894 514 * Return compiled layout styles to retain backwards compatibility.
895 515 * Since https://github.com/WordPress/gutenberg/pull/42452,
@@ -927,26 +547,8 @@
927 547 return $prefix . (string) ++$id_counters[ $prefix ];
928 548 }
929 549
930 550 /**
931 - * Generates a unique ID based on the structure and values of a given array.
932 - *
933 - * This function serializes the array into a JSON string and generates a hash
934 - * that serves as a unique identifier. Optionally, a prefix can be added to
935 - * the generated ID for context or categorization.
936 - *
937 - * @param array $data The input array to generate an ID from.
938 - * @param string $prefix Optional. A prefix to prepend to the generated ID. Default ''.
939 - *
940 - * @return string The generated unique ID for the array.
941 - */
942 -function gutenberg_unique_id_from_values( array $data, string $prefix = '' ): string {
943 - $serialized = wp_json_encode( $data );
944 - $hash = substr( md5( $serialized ), 0, 8 );
945 - return $prefix . $hash;
946 -}
947 -
948 -/**
949 551 * Renders the layout config to the block wrapper.
950 552 *
951 553 * @param string $block_content Rendered block content.
952 554 * @param array $block Block object.
@@ -952,114 +554,41 @@
952 554 * @param array $block Block object.
953 555 * @return string Filtered block content.
954 556 */
955 557 function gutenberg_render_layout_support_flag( $block_content, $block ) {
956 - static $global_styles = null;
957 -
958 558 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
959 559 $block_supports_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
960 - $style_attr = gutenberg_resolve_style_state_aliases(
961 - $block['attrs']['style'] ?? array(),
962 - $block['blockName']
963 - );
964 - /*
965 - * A block with no layout support and no style attribute at all cannot
966 - * produce layout output, so return before resolving global settings.
967 - *
968 - * Resolving settings is not read-only: on a cold cache it queries the
969 - * user's `wp_global_styles` post, which fires `the_posts`. A callback on
970 - * that hook that renders blocks re-enters this filter, and the content it
971 - * renders at that point is the global styles post itself, which parses to a
972 - * single block with no name and no attributes. Without this return that
973 - * block resolves settings again and the recursion has no base case.
974 - */
975 - if ( ! $block_supports_layout && empty( $style_attr ) ) {
976 - return $block_content;
977 - }
560 + $layout_from_parent = $block['attrs']['style']['layout']['selfStretch'] ?? null;
978 561
979 - $global_settings = gutenberg_get_global_settings();
980 - $viewport_settings = $global_settings['viewport'] ?? null;
981 - $responsive_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries( $viewport_settings );
982 - // If there is any value in style -> layout, the block has a child layout.
983 - $child_layout = $style_attr['layout'] ?? null;
984 -
985 - // Collect responsive viewport child layout overrides so that a block with
986 - // only responsive child layout (no base child layout) is still processed.
987 - $viewport_child_layouts = array();
988 - foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
989 - $viewport_child = gutenberg_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null );
990 - if ( ! empty( $viewport_child ) ) {
991 - $viewport_child_layouts[ $breakpoint ] = array(
992 - 'media_query' => $media_query,
993 - 'child_layout' => $viewport_child,
994 - );
995 - }
996 - }
997 -
998 - if ( ! $block_supports_layout && ! $child_layout && empty( $viewport_child_layouts ) ) {
562 + if ( ! $block_supports_layout && ! $layout_from_parent ) {
999 563 return $block_content;
1000 564 }
1001 565
1002 566 $outer_class_names = array();
1003 567
1004 - // Child layout specific logic.
1005 - if ( $child_layout || ! empty( $viewport_child_layouts ) ) {
1006 - $base_child_layout = gutenberg_get_layout_child_values( $child_layout );
1007 - $parent_layout = $block['parentLayout'] ?? array();
568 + if ( 'fixed' === $layout_from_parent || 'fill' === $layout_from_parent ) {
569 + $container_content_class = wp_unique_id( 'wp-container-content-' );
1008 570
1009 - /*
1010 - * Generates a unique class for child block layout styles.
1011 - *
1012 - * To ensure consistent class generation across different page renders,
1013 - * only properties that affect layout styling are used. These properties
1014 - * come from `$block['attrs']['style']['layout']`, viewport overrides in
1015 - * `$block['attrs']['style'][$breakpoint]['layout']`, and
1016 - * `$block['parentLayout']`.
1017 - *
1018 - * As long as these properties coincide, the generated class will be the same.
1019 - */
1020 - $container_content_hash_input = array(
1021 - 'layout' => $base_child_layout,
1022 - 'parentLayout' => array_intersect_key(
1023 - $parent_layout,
1024 - array_flip( array( 'minimumColumnWidth', 'columnCount' ) )
1025 - ),
1026 - );
1027 - foreach ( $viewport_child_layouts as $breakpoint => $viewport_data ) {
1028 - $container_content_hash_input[ $breakpoint ] = $viewport_data['child_layout'];
1029 - }
1030 - $container_content_class = gutenberg_unique_id_from_values(
1031 - $container_content_hash_input,
1032 - 'wp-container-content-'
1033 - );
571 + $child_layout_styles = array();
1034 572
1035 - $child_layout_styles = gutenberg_get_child_layout_style_rules(
1036 - ".$container_content_class",
1037 - $base_child_layout,
1038 - $parent_layout
1039 - );
1040 -
1041 - // Emit responsive child layout CSS using the same container-content class
1042 - // so that base and responsive child layout share the exact same selector.
1043 - foreach ( $viewport_child_layouts as $viewport_data ) {
1044 - $viewport_child_styles = gutenberg_get_child_layout_style_rules(
1045 - ".$container_content_class",
1046 - $base_child_layout,
1047 - $parent_layout,
1048 - $viewport_data['child_layout']
573 + if ( 'fixed' === $layout_from_parent && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
574 + $child_layout_styles[] = array(
575 + 'selector' => ".$container_content_class",
576 + 'declarations' => array(
577 + 'flex-basis' => $block['attrs']['style']['layout']['flexSize'],
578 + 'box-sizing' => 'border-box',
579 + ),
1049 580 );
1050 - foreach ( $viewport_child_styles as $index => $rule ) {
1051 - $viewport_child_styles[ $index ]['rules_group'] = $viewport_data['media_query'];
1052 - }
1053 -
1054 - $child_layout_styles = array_merge( $child_layout_styles, $viewport_child_styles );
581 + } elseif ( 'fill' === $layout_from_parent ) {
582 + $child_layout_styles[] = array(
583 + 'selector' => ".$container_content_class",
584 + 'declarations' => array(
585 + 'flex-grow' => '1',
586 + ),
587 + );
1055 588 }
1056 589
1057 - /*
1058 - * Add to the style engine store to enqueue and render layout styles.
1059 - * Return styles here just to check if any exist.
1060 - */
1061 - $child_css = gutenberg_style_engine_get_stylesheet_from_css_rules(
590 + gutenberg_style_engine_get_stylesheet_from_css_rules(
1062 591 $child_layout_styles,
1063 592 array(
1064 593 'context' => 'block-supports',
1065 594 'prettify' => false,
@@ -1065,11 +594,9 @@
1065 594 'prettify' => false,
1066 595 )
1067 596 );
1068 597
1069 - if ( $child_css ) {
1070 - $outer_class_names[] = $container_content_class;
1071 - }
598 + $outer_class_names[] = $container_content_class;
1072 599 }
1073 600
1074 601 // Prep the processor for modifying the block output.
1075 602 $processor = new WP_HTML_Tag_Processor( $block_content );
@@ -1089,13 +616,11 @@
1089 616 foreach ( $outer_class_names as $class_name ) {
1090 617 $processor->add_class( $class_name );
1091 618 }
1092 619 return $processor->get_updated_html();
1093 - } elseif ( ! $block_supports_layout ) {
1094 - // Ensure layout classnames are not injected if there is no layout support.
1095 - return $block_content;
1096 620 }
1097 621
622 + $global_settings = gutenberg_get_global_settings();
1098 623 $fallback_layout = $block_type->supports['layout']['default'] ?? array();
1099 624 if ( empty( $fallback_layout ) ) {
1100 625 $fallback_layout = $block_type->supports['__experimentalLayout']['default'] ?? array();
1101 626 }
@@ -1103,8 +628,18 @@
1103 628
1104 629 $class_names = array();
1105 630 $layout_definitions = gutenberg_get_layout_definitions();
1106 631
632 + /*
633 + * We use an incremental ID that is independent per prefix to make sure that
634 + * rendering different numbers of blocks doesn't affect the IDs of other
635 + * blocks. We need this to make the CSS class names stable across paginations
636 + * for features like the enhanced pagination of the Query block.
637 + */
638 + $container_class = gutenberg_incremental_id_per_prefix(
639 + 'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
640 + );
641 +
1107 642 // Set the correct layout type for blocks using legacy content width.
1108 643 if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
1109 644 $used_layout['type'] = 'constrained';
1110 645 }
@@ -1149,10 +684,23 @@
1149 684 * Attribute-based Layout classnames are output in all cases.
1150 685 */
1151 686 if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
1152 687
1153 - $gap_value = gutenberg_sanitize_block_gap_value( $block['attrs']['style']['spacing']['blockGap'] ?? null );
688 + $gap_value = $block['attrs']['style']['spacing']['blockGap'] ?? null;
1154 689
690 + /*
691 + * Skip if gap value contains unsupported characters.
692 + * Regex for CSS value borrowed from `safecss_filter_attr`, and used here
693 + * to only match against the value, not the CSS attribute.
694 + */
695 + if ( is_array( $gap_value ) ) {
696 + foreach ( $gap_value as $key => $value ) {
697 + $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
698 + }
699 + } else {
700 + $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
701 + }
702 +
1155 703 $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
1156 704 $block_spacing = $block['attrs']['style']['spacing'] ?? null;
1157 705
1158 706 /*
@@ -1163,81 +711,10 @@
1163 711
1164 712 $block_gap = $global_settings['spacing']['blockGap'] ?? null;
1165 713 $has_block_gap_support = isset( $block_gap );
1166 714
1167 - // Get default blockGap value from global styles for use in layouts like grid.
1168 - // Check style variation first, then block-specific styles, then fall back to root styles.
1169 - $block_name = $block['blockName'] ?? '';
1170 - if ( null === $global_styles ) {
1171 - $global_styles = gutenberg_get_global_styles();
1172 - }
1173 -
1174 - // Check if the block has an active style variation with a blockGap value.
1175 - // Only check the registry if the className contains a variation class to avoid unnecessary lookups.
1176 - $variation_block_gap_value = null;
1177 - $block_class_name = is_string( $block['attrs']['className'] ?? null )
1178 - ? $block['attrs']['className']
1179 - : '';
1180 - if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) {
1181 - $styles_registry = WP_Block_Styles_Registry::get_instance();
1182 - $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name );
1183 - $variation_name = gutenberg_get_block_style_variation_name_from_registered_style( $block_class_name, $registered_styles );
1184 - if ( $variation_name ) {
1185 - $variation_block_gap_value = $global_styles['blocks'][ $block_name ]['variations'][ $variation_name ]['spacing']['blockGap'] ?? null;
1186 - }
1187 - }
1188 -
1189 - $global_block_gap_value = $variation_block_gap_value ?? $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null;
1190 -
1191 - if ( null !== $global_block_gap_value ) {
1192 - $fallback_gap_value = $global_block_gap_value;
1193 - }
1194 -
1195 - /*
1196 - * We generate a unique ID based on all the data required to obtain the
1197 - * corresponding layout style. This way, the CSS class names keep the same
1198 - * even for different blocks with the same layout definition. We need this to
1199 - * make the CSS class names stable across paginations for features like the
1200 - * enhanced pagination of the Query block.
1201 - */
1202 - $container_class_hash_input = array(
1203 - $used_layout,
1204 - $has_block_gap_support,
1205 - $gap_value,
1206 - $should_skip_gap_serialization,
1207 - $fallback_gap_value,
1208 - $block_spacing,
1209 - );
1210 -
1211 - foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) {
1212 - $viewport_style = $style_attr[ $breakpoint ] ?? null;
1213 - if ( ! is_array( $viewport_style ) ) {
1214 - continue;
1215 - }
1216 -
1217 - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null );
1218 - if ( ! empty( $viewport_container_layout ) ) {
1219 - $container_class_hash_input[] = array(
1220 - 'breakpoint' => $breakpoint,
1221 - 'layout' => $viewport_container_layout,
1222 - );
1223 - }
1224 -
1225 - if ( isset( $viewport_style['spacing']['blockGap'] ) ) {
1226 - $container_class_hash_input[] = array(
1227 - 'breakpoint' => $breakpoint,
1228 - 'blockGap' => gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ),
1229 - );
1230 - }
1231 - }
1232 -
1233 - $container_class = gutenberg_unique_id_from_values(
1234 - $container_class_hash_input,
1235 - 'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
1236 - );
1237 -
1238 715 $style = gutenberg_get_layout_style(
1239 - ".$container_class",
716 + ".$container_class.$container_class",
1240 717 $used_layout,
1241 718 $has_block_gap_support,
1242 719 $gap_value,
1243 720 $should_skip_gap_serialization,
@@ -1248,53 +725,8 @@
1248 725 // Only add container class and enqueue block support styles if unique styles were generated.
1249 726 if ( ! empty( $style ) ) {
1250 727 $class_names[] = $container_class;
1251 728 }
1252 -
1253 - /*
1254 - * Emit responsive container layout styles using the same $container_class
1255 - * selector as the base layout so they target the inner block wrapper.
1256 - */
1257 - foreach ( $responsive_media_queries as $breakpoint => $media_query ) {
1258 - $viewport_style = $style_attr[ $breakpoint ] ?? null;
1259 - if ( ! is_array( $viewport_style ) ) {
1260 - continue;
1261 - }
1262 -
1263 - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null );
1264 - $has_viewport_layout = ! empty( $viewport_container_layout );
1265 - $has_viewport_block_gap = isset( $viewport_style['spacing']['blockGap'] );
1266 -
1267 - if ( ! $has_viewport_layout && ! $has_viewport_block_gap ) {
1268 - continue;
1269 - }
1270 -
1271 - $viewport_gap_value = $has_viewport_block_gap
1272 - ? gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] )
1273 - : $gap_value;
1274 - $viewport_block_spacing = is_array( $viewport_style['spacing'] ?? null )
1275 - ? array_replace( is_array( $block_spacing ) ? $block_spacing : array(), $viewport_style['spacing'] )
1276 - : $block_spacing;
1277 -
1278 - $viewport_styles = gutenberg_get_layout_style(
1279 - ".$container_class",
1280 - $used_layout,
1281 - $has_block_gap_support,
1282 - $viewport_gap_value,
1283 - $should_skip_gap_serialization,
1284 - $fallback_gap_value,
1285 - $viewport_block_spacing,
1286 - array(
1287 - 'rules_group' => $media_query,
1288 - 'viewport_overrides' => $viewport_container_layout,
1289 - 'has_block_gap_override' => $has_viewport_block_gap,
1290 - )
1291 - );
1292 -
1293 - if ( ! empty( $viewport_styles ) && ! in_array( $container_class, $class_names, true ) ) {
1294 - $class_names[] = $container_class;
1295 - }
1296 - }
1297 729 }
1298 730
1299 731 // Add combined layout and block classname for global styles to hook onto.
1300 732 $split_block_name = explode( '/', $block['blockName'] );
@@ -1307,9 +739,9 @@
1307 739 $processor->add_class( $outer_class_name );
1308 740 }
1309 741 }
1310 742
1311 - /*
743 + /**
1312 744 * Attempts to refer to the inner-block wrapping element by its class attribute.
1313 745 *
1314 746 * When examining a block's inner content, if a block has inner blocks, then
1315 747 * the first content item will likely be a text (HTML) chunk immediately
@@ -1345,9 +777,9 @@
1345 777 * from how they appear in $block['innerContent'], it's likely that the original class attributes
1346 778 * are still present in the wrapper as they are in this example. Frequently, additional classes
1347 779 * will also be present; rarely should classes be removed.
1348 780 *
1349 - * @todo Find a better way to match the first inner block. If it's possible to identify where the
781 + * @TODO: Find a better way to match the first inner block. If it's possible to identify where the
1350 782 * first inner block starts, then it will be possible to find the last tag before it starts
1351 783 * and then that tag, if an opening tag, can be solidly identified as a wrapping element.
1352 784 * Can some unique value or class or ID be added to the inner blocks when they process
1353 785 * so that they can be extracted here safely without guessing? Can the block rendering function
@@ -1358,31 +790,12 @@
1358 790 $inner_block_wrapper_classes = null;
1359 791 $first_chunk = $block['innerContent'][0] ?? null;
1360 792 if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
1361 793 $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
1362 - /*
1363 - * Use a stack to track open elements as tags are visited. Void elements
1364 - * (those without a matching closing tag) are excluded so they don't
1365 - * accumulate on the stack. At the end of the chunk, every element still
1366 - * on the stack is unclosed — meaning its closing tag lives in a later
1367 - * innerContent entry alongside the inner blocks, which makes it the
1368 - * inner-block container. Elements that open and close within this chunk
1369 - * are siblings that precede the inner blocks and should be ignored.
1370 - * The last unclosed element with a class attribute is the best candidate
1371 - * for the inner-block wrapper.
1372 - */
1373 - $tag_stack = array();
1374 - while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
1375 - if ( $first_chunk_processor->is_tag_closer() ) {
1376 - array_pop( $tag_stack );
1377 - } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) {
1378 - $tag_stack[] = $first_chunk_processor->get_attribute( 'class' );
1379 - }
1380 - }
1381 - foreach ( array_reverse( $tag_stack ) as $class_attribute ) {
794 + while ( $first_chunk_processor->next_tag() ) {
795 + $class_attribute = $first_chunk_processor->get_attribute( 'class' );
1382 796 if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
1383 797 $inner_block_wrapper_classes = $class_attribute;
1384 - break;
1385 798 }
1386 799 }
1387 800 }
1388 801
@@ -1402,10 +815,9 @@
1402 815 if ( ! $inner_block_wrapper_classes ) {
1403 816 break;
1404 817 }
1405 818
1406 - $class_attribute = $processor->get_attribute( 'class' );
1407 - if ( is_string( $class_attribute ) && str_contains( $class_attribute, $inner_block_wrapper_classes ) ) {
819 + if ( false !== strpos( $processor->get_attribute( 'class' ), $inner_block_wrapper_classes ) ) {
1408 820 break;
1409 821 }
1410 822 } while ( $processor->next_tag() );
1411 823
@@ -1416,27 +828,8 @@
1416 828
1417 829 return $processor->get_updated_html();
1418 830 }
1419 831
1420 -/*
1421 - * Add a `render_block_data` filter to fetch the parent block layout data.
1422 - */
1423 -add_filter(
1424 - 'render_block_data',
1425 - function ( $parsed_block, $source_block, $parent_block ) {
1426 - /*
1427 - * Check if the parent block exists and if it has a layout attribute.
1428 - * If it does, add the parent layout to the parsed block.
1429 - */
1430 - if ( $parent_block && isset( $parent_block->parsed_block['attrs']['layout'] ) ) {
1431 - $parsed_block['parentLayout'] = $parent_block->parsed_block['attrs']['layout'];
1432 - }
1433 - return $parsed_block;
1434 - },
1435 - 10,
1436 - 3
1437 -);
1438 -
1439 832 // Register the block support. (overrides core one).
1440 833 WP_Block_Supports::get_instance()->register(
1441 834 'layout',
1442 835 array(
@@ -1458,9 +851,9 @@
1458 851 * @param array $block Block object.
1459 852 * @return string Filtered block content.
1460 853 */
1461 854 function gutenberg_restore_group_inner_container( $block_content, $block ) {
1462 - $tag_name = $block['attrs']['tagName'] ?? 'div';
855 + $tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div';
1463 856 $group_with_inner_container_regex = sprintf(
1464 857 '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
1465 858 preg_quote( $tag_name, '/' )
1466 859 );
@@ -1471,57 +864,29 @@
1471 864 ) {
1472 865 return $block_content;
1473 866 }
1474 867
1475 - /*
1476 - * This filter runs after the layout classnames have been added to the block, so they
1477 - * have to be removed from the outer wrapper and then added to the inner.
1478 - */
1479 - $layout_classes = array();
1480 - $processor = new WP_HTML_Tag_Processor( $block_content );
1481 -
1482 - if ( $processor->next_tag( array( 'class_name' => 'wp-block-group' ) ) ) {
1483 - foreach ( $processor->class_list() as $class_name ) {
1484 - if ( str_contains( $class_name, 'layout' ) ) {
1485 - array_push( $layout_classes, $class_name );
1486 - $processor->remove_class( $class_name );
1487 - }
1488 - }
1489 - }
1490 -
1491 - $content_without_layout_classes = $processor->get_updated_html();
1492 - $replace_regex = sprintf(
868 + $replace_regex = sprintf(
1493 869 '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
1494 870 preg_quote( $tag_name, '/' )
1495 871 );
1496 - $updated_content = preg_replace_callback(
872 + $updated_content = preg_replace_callback(
1497 873 $replace_regex,
1498 874 static function ( $matches ) {
1499 875 return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
1500 876 },
1501 - $content_without_layout_classes
877 + $block_content
1502 878 );
1503 -
1504 - // Add layout classes to inner wrapper.
1505 - if ( ! empty( $layout_classes ) ) {
1506 - $processor = new WP_HTML_Tag_Processor( $updated_content );
1507 - if ( $processor->next_tag( array( 'class_name' => 'wp-block-group__inner-container' ) ) ) {
1508 - foreach ( $layout_classes as $class_name ) {
1509 - $processor->add_class( $class_name );
1510 - }
1511 - }
1512 - $updated_content = $processor->get_updated_html();
1513 - }
1514 -
1515 879 return $updated_content;
1516 880 }
1517 881
1518 882 if ( function_exists( 'wp_restore_group_inner_container' ) ) {
1519 - remove_filter( 'render_block', 'wp_restore_group_inner_container', 10 );
1520 - remove_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10 );
883 + remove_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 );
884 + remove_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
1521 885 }
1522 886 add_filter( 'render_block_core/group', 'gutenberg_restore_group_inner_container', 10, 2 );
1523 887
888 +
1524 889 /**
1525 890 * For themes without theme.json file, make sure
1526 891 * to restore the outer div for the aligned image block
1527 892 * to avoid breaking styles relying on that div.
@@ -1530,57 +895,54 @@
1530 895 * @param array $block Block object.
1531 896 * @return string Filtered block content.
1532 897 */
1533 898 function gutenberg_restore_image_outer_container( $block_content, $block ) {
1534 - if ( wp_theme_has_theme_json() ) {
1535 - return $block_content;
1536 - }
899 + $image_with_align = "
900 +/# 1) everything up to the class attribute contents
901 +(
902 + ^\s*
903 + <figure\b
904 + [^>]*
905 + \bclass=
906 + [\"']
907 +)
908 +# 2) the class attribute contents
909 +(
910 + [^\"']*
911 + \bwp-block-image\b
912 + [^\"']*
913 + \b(?:alignleft|alignright|aligncenter)\b
914 + [^\"']*
915 +)
916 +# 3) everything after the class attribute contents
917 +(
918 + [\"']
919 + [^>]*
920 + >
921 + .*
922 + <\/figure>
923 +)/iUx";
1537 924
1538 - $figure_processor = new WP_HTML_Tag_Processor( $block_content );
1539 925 if (
1540 - ! $figure_processor->next_tag( 'FIGURE' ) ||
1541 - ! $figure_processor->has_class( 'wp-block-image' ) ||
1542 - ! (
1543 - $figure_processor->has_class( 'alignleft' ) ||
1544 - $figure_processor->has_class( 'aligncenter' ) ||
1545 - $figure_processor->has_class( 'alignright' )
1546 - )
926 + wp_theme_has_theme_json() ||
927 + 0 === preg_match( $image_with_align, $block_content, $matches )
1547 928 ) {
1548 929 return $block_content;
1549 930 }
1550 931
1551 - /*
1552 - * The next section of code wraps the existing figure in a new DIV element.
1553 - * While doing it, it needs to transfer the layout and the additional CSS
1554 - * class names from the original figure upward to the wrapper.
1555 - *
1556 - * Example:
1557 - *
1558 - * // From this…
1559 - * <!-- wp:image {"className":"hires"} -->
1560 - * <figure class="wp-block-image wide hires">…
1561 - *
1562 - * // To this…
1563 - * <div class="wp-block-image hires"><figure class="wide">…
1564 - */
1565 - $wrapper_processor = new WP_HTML_Tag_Processor( '<div>' );
1566 - $wrapper_processor->next_token();
1567 - $wrapper_processor->set_attribute(
1568 - 'class',
1569 - is_string( $block['attrs']['className'] ?? null )
1570 - ? "wp-block-image {$block['attrs']['className']}"
1571 - : 'wp-block-image'
1572 - );
932 + $wrapper_classnames = array( 'wp-block-image' );
1573 933
1574 - // And remove them from the existing content; it has been transferred upward.
1575 - $figure_processor->remove_class( 'wp-block-image' );
1576 - foreach ( $wrapper_processor->class_list() as $class_name ) {
1577 - $figure_processor->remove_class( $class_name );
934 + // If the block has a classNames attribute these classnames need to be removed from the content and added back
935 + // to the new wrapper div also.
936 + if ( ! empty( $block['attrs']['className'] ) ) {
937 + $wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) );
1578 938 }
939 + $content_classnames = explode( ' ', $matches[2] );
940 + $filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames );
1579 941
1580 - return "{$wrapper_processor->get_updated_html()}{$figure_processor->get_updated_html()}</div>";
942 + return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>';
1581 943 }
1582 944
1583 945 if ( function_exists( 'wp_restore_image_outer_container' ) ) {
1584 - remove_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10 );
946 + remove_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );
1585 947 }
1586 948 add_filter( 'render_block_core/image', 'gutenberg_restore_image_outer_container', 10, 2 );