PluginProbe
Gutenberg / 22.7.0
Gutenberg v22.7.0
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 +205 -447 23.3.222.7.0 View file →
@@ -237,196 +237,8 @@
237 237 }
238 238 }
239 239
240 240 /**
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 ] = $value && preg_match( '%[\\\(&=}]|/\*%', $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 -
322 - if ( null === $viewport_overrides || $has_viewport_property_override( 'selfStretch' ) || $has_viewport_property_override( 'flexSize' ) ) {
323 - if ( 'fixed' === $self_stretch && isset( $child_layout['flexSize'] ) ) {
324 - $child_layout_declarations['flex-basis'] = $child_layout['flexSize'];
325 - $child_layout_declarations['box-sizing'] = 'border-box';
326 - } elseif ( 'fill' === $self_stretch ) {
327 - $child_layout_declarations['flex-grow'] = '1';
328 - }
329 - }
330 -
331 - $column_start = $child_layout['columnStart'] ?? null;
332 - $column_span = $child_layout['columnSpan'] ?? null;
333 - if ( null === $viewport_overrides || $has_viewport_property_override( 'columnStart' ) || $has_viewport_property_override( 'columnSpan' ) ) {
334 - if ( $column_start && $column_span ) {
335 - $child_layout_declarations['grid-column'] = "$column_start / span $column_span";
336 - } elseif ( $column_start ) {
337 - $child_layout_declarations['grid-column'] = "$column_start";
338 - } elseif ( $column_span ) {
339 - $child_layout_declarations['grid-column'] = "span $column_span";
340 - }
341 - }
342 -
343 - $row_start = $child_layout['rowStart'] ?? null;
344 - $row_span = $child_layout['rowSpan'] ?? null;
345 - if ( null === $viewport_overrides || $has_viewport_property_override( 'rowStart' ) || $has_viewport_property_override( 'rowSpan' ) ) {
346 - if ( $row_start && $row_span ) {
347 - $child_layout_declarations['grid-row'] = "$row_start / span $row_span";
348 - } elseif ( $row_start ) {
349 - $child_layout_declarations['grid-row'] = "$row_start";
350 - } elseif ( $row_span ) {
351 - $child_layout_declarations['grid-row'] = "span $row_span";
352 - }
353 - }
354 -
355 - if ( ! empty( $child_layout_declarations ) ) {
356 - $child_layout_styles[] = array(
357 - 'selector' => $selector,
358 - 'declarations' => $child_layout_declarations,
359 - );
360 - }
361 -
362 - $minimum_column_width = $parent_layout['minimumColumnWidth'] ?? null;
363 - $column_count = $parent_layout['columnCount'] ?? null;
364 -
365 - /*
366 - * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
367 - * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed
368 - * once the grid has less columns than the start.
369 - * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set.
370 - * In that case, if columnCount doesn't exist, we can assume that the grid is responsive.
371 - */
372 - if ( null === $viewport_overrides && ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) {
373 - $column_span_number = floatval( $column_span );
374 - $column_start_number = floatval( $column_start );
375 - $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem';
376 - $parent_column_value = floatval( $parent_column_width );
377 - $parent_column_unit = explode( $parent_column_value, $parent_column_width );
378 -
379 - $num_cols_to_break_at = 2;
380 - if ( $column_span_number && $column_start_number ) {
381 - $num_cols_to_break_at = $column_start_number + $column_span_number - 1;
382 - } elseif ( $column_span_number ) {
383 - $num_cols_to_break_at = $column_span_number;
384 - } else {
385 - $num_cols_to_break_at = $column_start_number;
386 - }
387 -
388 - /*
389 - * If there is no unit, the width has somehow been mangled so we reset both unit and value
390 - * to defaults.
391 - * Additionally, the unit should be one of px, rem or em, so that also needs to be checked.
392 - */
393 - if ( count( $parent_column_unit ) <= 1 ) {
394 - $parent_column_unit = 'rem';
395 - $parent_column_value = 12;
396 - } else {
397 - $parent_column_unit = $parent_column_unit[1];
398 -
399 - if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) {
400 - $parent_column_unit = 'rem';
401 - }
402 - }
403 -
404 - /*
405 - * A default gap value is used for this computation because custom gap values may not be
406 - * viable to use in the computation of the container query value.
407 - */
408 - $default_gap_value = 'px' === $parent_column_unit ? 24 : 1.5;
409 - $container_query_value = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value;
410 - $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1;
411 - $container_query_value = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit;
412 - // If a span is set we want to preserve it as long as possible, otherwise we just reset the value.
413 - $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto';
414 -
415 - $child_layout_styles[] = array(
416 - 'rules_group' => "@container (max-width: $container_query_value )",
417 - 'selector' => $selector,
418 - 'declarations' => array(
419 - 'grid-column' => $grid_column_value,
420 - 'grid-row' => 'auto',
421 - ),
422 - );
423 - }
424 -
425 - return $child_layout_styles;
426 -}
427 -
428 -/**
429 241 * Generates the CSS corresponding to the provided layout.
430 242 *
431 243 * @param string $selector CSS selector.
432 244 * @param array $layout Layout object. The one that is passed has already checked
@@ -435,26 +247,16 @@
435 247 * @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null.
436 248 * @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
437 249 * @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'.
438 250 * @param array|null $block_spacing Optional. Custom spacing set on the block. Default null.
439 - * @param array $options Optional. Extra options for internal callers. Default empty array.
440 - * @return string CSS styles, or empty string.
251 + * @return string CSS styles on success. Else, empty string.
441 252 */
442 -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() ) {
443 - $base_layout = is_array( $layout ) ? $layout : array();
444 - $viewport_overrides = $options['viewport_overrides'] ?? null;
445 - $layout_for_styles = null === $viewport_overrides ? $base_layout : array_replace( $base_layout, $viewport_overrides );
446 - $layout_type = $base_layout['type'] ?? 'default';
447 - $rules_group = $options['rules_group'] ?? null;
448 - $has_block_gap_override = ! empty( $options['has_block_gap_override'] );
449 - $should_output_block_gap = null === $viewport_overrides || $has_block_gap_override;
450 - $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
451 - return array_key_exists( $property, $viewport_overrides );
452 - };
453 - $layout_styles = array();
253 +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 ) {
254 + $layout_type = $layout['type'] ?? 'default';
255 + $layout_styles = array();
454 256
455 257 if ( 'default' === $layout_type ) {
456 - if ( $has_block_gap_support && $should_output_block_gap ) {
258 + if ( $has_block_gap_support ) {
457 259 if ( is_array( $gap_value ) ) {
458 260 $gap_value = $gap_value['top'] ?? null;
459 261 }
460 262 if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
@@ -484,11 +286,11 @@
484 286 );
485 287 }
486 288 }
487 289 } elseif ( 'constrained' === $layout_type ) {
488 - $content_size = $layout_for_styles['contentSize'] ?? '';
489 - $wide_size = $layout_for_styles['wideSize'] ?? '';
490 - $justify_content = $layout_for_styles['justifyContent'] ?? 'center';
290 + $content_size = $layout['contentSize'] ?? '';
291 + $wide_size = $layout['wideSize'] ?? '';
292 + $justify_content = $layout['justifyContent'] ?? 'center';
491 293
492 294 $all_max_width_value = $content_size ? $content_size : $wide_size;
493 295 $wide_max_width_value = $wide_size ? $wide_size : $content_size;
494 296
@@ -498,25 +300,18 @@
498 300
499 301 $margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important';
500 302 $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
501 303
502 - $has_justify_content_override = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' );
503 - $should_output_constrained_sizes = null === $viewport_overrides || $has_viewport_property_override( 'contentSize' ) || $has_viewport_property_override( 'wideSize' );
504 - if ( $should_output_constrained_sizes && ( $content_size || $wide_size ) ) {
505 - $content_size_declarations = array(
506 - 'max-width' => $all_max_width_value,
507 - );
508 -
509 - if ( null === $viewport_overrides || $has_justify_content_override ) {
510 - $content_size_declarations['margin-left'] = $margin_left;
511 - $content_size_declarations['margin-right'] = $margin_right;
512 - }
513 -
304 + if ( $content_size || $wide_size ) {
514 305 array_push(
515 306 $layout_styles,
516 307 array(
517 308 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
518 - 'declarations' => $content_size_declarations,
309 + 'declarations' => array(
310 + 'max-width' => $all_max_width_value,
311 + 'margin-left' => $margin_left,
312 + 'margin-right' => $margin_right,
313 + ),
519 314 ),
520 315 array(
521 316 'selector' => "$selector > .alignwide",
522 317 'declarations' => array( 'max-width' => $wide_max_width_value ),
@@ -527,9 +322,9 @@
527 322 )
528 323 );
529 324 }
530 325
531 - if ( null === $viewport_overrides && isset( $block_spacing ) ) {
326 + if ( isset( $block_spacing ) ) {
532 327 $block_spacing_values = gutenberg_style_engine_get_styles(
533 328 array(
534 329 'spacing' => $block_spacing,
535 330 )
@@ -562,33 +357,23 @@
562 357 );
563 358 }
564 359 }
565 360
566 - if ( $has_justify_content_override && ! $should_output_constrained_sizes ) {
361 + if ( 'left' === $justify_content ) {
567 362 $layout_styles[] = array(
568 363 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
569 - 'declarations' => array(
570 - 'margin-left' => $margin_left,
571 - 'margin-right' => $margin_right,
572 - ),
364 + 'declarations' => array( 'margin-left' => '0 !important' ),
573 365 );
574 - } elseif ( null === $viewport_overrides ) {
575 - if ( 'left' === $justify_content ) {
576 - $layout_styles[] = array(
577 - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
578 - 'declarations' => array( 'margin-left' => '0 !important' ),
579 - );
580 - }
366 + }
581 367
582 - if ( 'right' === $justify_content ) {
583 - $layout_styles[] = array(
584 - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
585 - 'declarations' => array( 'margin-right' => '0 !important' ),
586 - );
587 - }
368 + if ( 'right' === $justify_content ) {
369 + $layout_styles[] = array(
370 + 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
371 + 'declarations' => array( 'margin-right' => '0 !important' ),
372 + );
588 373 }
589 374
590 - if ( $has_block_gap_support && $should_output_block_gap ) {
375 + if ( $has_block_gap_support ) {
591 376 if ( is_array( $gap_value ) ) {
592 377 $gap_value = $gap_value['top'] ?? null;
593 378 }
594 379 if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
@@ -618,9 +403,9 @@
618 403 );
619 404 }
620 405 }
621 406 } elseif ( 'flex' === $layout_type ) {
622 - $layout_orientation = $layout_for_styles['orientation'] ?? 'horizontal';
407 + $layout_orientation = $layout['orientation'] ?? 'horizontal';
623 408
624 409 $justify_content_options = array(
625 410 'left' => 'flex-start',
626 411 'right' => 'flex-end',
@@ -640,14 +425,9 @@
640 425 $justify_content_options += array( 'stretch' => 'stretch' );
641 426 $vertical_alignment_options += array( 'space-between' => 'space-between' );
642 427 }
643 428
644 - $should_output_flex_wrap = null === $viewport_overrides || $has_viewport_property_override( 'flexWrap' );
645 - $should_output_flex_orientation = null === $viewport_overrides || $has_viewport_property_override( 'orientation' );
646 - $should_output_flex_justification = null === $viewport_overrides || $has_viewport_property_override( 'justifyContent' ) || $has_viewport_property_override( 'orientation' );
647 - $should_output_flex_alignment = null === $viewport_overrides || $has_viewport_property_override( 'verticalAlignment' ) || $has_viewport_property_override( 'orientation' );
648 -
649 - if ( $should_output_flex_wrap && ! empty( $layout_for_styles['flexWrap'] ) && 'nowrap' === $layout_for_styles['flexWrap'] ) {
429 + if ( ! empty( $layout['flexWrap'] ) && 'nowrap' === $layout['flexWrap'] ) {
650 430 $layout_styles[] = array(
651 431 'selector' => $selector,
652 432 'declarations' => array( 'flex-wrap' => 'nowrap' ),
653 433 );
@@ -652,9 +432,9 @@
652 432 'declarations' => array( 'flex-wrap' => 'nowrap' ),
653 433 );
654 434 }
655 435
656 - if ( $has_block_gap_support && $should_output_block_gap && isset( $gap_value ) ) {
436 + if ( $has_block_gap_support && isset( $gap_value ) ) {
657 437 $combined_gap_value = '';
658 438 $gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
659 439
660 440 foreach ( $gap_sides as $gap_side ) {
@@ -690,43 +470,41 @@
690 470 * Add this style only if is not empty for backwards compatibility,
691 471 * since we intend to convert blocks that had flex layout implemented
692 472 * by custom css.
693 473 */
694 - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
474 + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
695 475 $layout_styles[] = array(
696 476 'selector' => $selector,
697 - 'declarations' => array( 'justify-content' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
477 + 'declarations' => array( 'justify-content' => $justify_content_options[ $layout['justifyContent'] ] ),
698 478 );
699 479 }
700 480
701 - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
481 + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
702 482 $layout_styles[] = array(
703 483 'selector' => $selector,
704 - 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
484 + 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
705 485 );
706 486 }
707 487 } else {
708 - if ( $should_output_flex_orientation ) {
488 + $layout_styles[] = array(
489 + 'selector' => $selector,
490 + 'declarations' => array( 'flex-direction' => 'column' ),
491 + );
492 + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
709 493 $layout_styles[] = array(
710 494 'selector' => $selector,
711 - 'declarations' => array( 'flex-direction' => 'column' ),
495 + 'declarations' => array( 'align-items' => $justify_content_options[ $layout['justifyContent'] ] ),
712 496 );
713 - }
714 - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
497 + } else {
715 498 $layout_styles[] = array(
716 499 'selector' => $selector,
717 - 'declarations' => array( 'align-items' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
718 - );
719 - } elseif ( $should_output_flex_justification ) {
720 - $layout_styles[] = array(
721 - 'selector' => $selector,
722 500 'declarations' => array( 'align-items' => 'flex-start' ),
723 501 );
724 502 }
725 - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
503 + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
726 504 $layout_styles[] = array(
727 505 'selector' => $selector,
728 - 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
506 + 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
729 507 );
730 508 }
731 509 }
732 510 } elseif ( 'grid' === $layout_type ) {
@@ -770,48 +548,47 @@
770 548 if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) {
771 549 $responsive_gap_value = '0px';
772 550 }
773 551
774 - $should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' );
775 - $uses_gap_in_grid_columns = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
776 - if ( $has_block_gap_override && $uses_gap_in_grid_columns ) {
777 - $should_output_grid_columns = true;
778 - }
779 -
780 - $should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] );
781 - $grid_declarations = array();
782 -
783 - if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
784 - $max_value = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')';
785 - $grid_declarations['grid-template-columns'] = 'repeat(auto-fill, minmax(' . $max_value . ', 1fr))';
786 - } elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) {
787 - $grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))';
788 - } elseif ( $should_output_grid_columns ) {
789 - $minimum_column_width = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem';
790 - $grid_declarations['grid-template-columns'] = 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))';
791 - }
792 -
793 - if ( ! empty( $grid_declarations ) ) {
794 - $base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) );
795 - if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
796 - if ( null === $viewport_overrides || ! $base_has_container_type ) {
797 - $grid_declarations['container-type'] = 'inline-size';
798 - }
552 + if ( ! empty( $layout['columnCount'] ) && ! empty( $layout['minimumColumnWidth'] ) ) {
553 + $max_value = 'max(min(' . $layout['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout['columnCount'] . ' - 1))) /' . $layout['columnCount'] . ')';
554 + $layout_styles[] = array(
555 + 'selector' => $selector,
556 + 'declarations' => array(
557 + 'grid-template-columns' => 'repeat(auto-fill, minmax(' . $max_value . ', 1fr))',
558 + 'container-type' => 'inline-size',
559 + ),
560 + );
561 + if ( ! empty( $layout['rowCount'] ) ) {
562 + $layout_styles[] = array(
563 + 'selector' => $selector,
564 + 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ),
565 + );
799 566 }
567 + } elseif ( ! empty( $layout['columnCount'] ) ) {
800 568 $layout_styles[] = array(
801 569 'selector' => $selector,
802 - 'declarations' => $grid_declarations,
570 + 'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
803 571 );
804 - }
572 + if ( ! empty( $layout['rowCount'] ) ) {
573 + $layout_styles[] = array(
574 + 'selector' => $selector,
575 + 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ),
576 + );
577 + }
578 + } else {
579 + $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
805 580
806 - if ( $should_output_grid_rows ) {
807 581 $layout_styles[] = array(
808 582 'selector' => $selector,
809 - 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ),
583 + 'declarations' => array(
584 + 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))',
585 + 'container-type' => 'inline-size',
586 + ),
810 587 );
811 588 }
812 589
813 - if ( $has_block_gap_support && $should_output_block_gap && null !== $gap_value && ! $should_skip_gap_serialization ) {
590 + if ( $has_block_gap_support && null !== $gap_value && ! $should_skip_gap_serialization ) {
814 591 $layout_styles[] = array(
815 592 'selector' => $selector,
816 593 'declarations' => array( 'gap' => $gap_value ),
817 594 );
@@ -818,14 +595,8 @@
818 595 }
819 596 }
820 597
821 598 if ( ! empty( $layout_styles ) ) {
822 - if ( ! empty( $rules_group ) ) {
823 - foreach ( $layout_styles as $index => $layout_style ) {
824 - $layout_styles[ $index ]['rules_group'] = $rules_group;
825 - }
826 - }
827 -
828 599 /*
829 600 * Add to the style engine store to enqueue and render layout styles.
830 601 * Return compiled layout styles to retain backwards compatibility.
831 602 * Since https://github.com/WordPress/gutenberg/pull/42452,
@@ -892,26 +663,12 @@
892 663 static $global_styles = null;
893 664
894 665 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
895 666 $block_supports_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false );
896 - $style_attr = $block['attrs']['style'] ?? array();
897 667 // If there is any value in style -> layout, the block has a child layout.
898 - $child_layout = $style_attr['layout'] ?? null;
668 + $child_layout = $block['attrs']['style']['layout'] ?? null;
899 669
900 - // Collect responsive viewport child layout overrides so that a block with
901 - // only responsive child layout (no base child layout) is still processed.
902 - $viewport_child_layouts = array();
903 - foreach ( WP_Theme_JSON_Gutenberg::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
904 - $viewport_child = gutenberg_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null );
905 - if ( ! empty( $viewport_child ) ) {
906 - $viewport_child_layouts[ $breakpoint ] = array(
907 - 'media_query' => $media_query,
908 - 'child_layout' => $viewport_child,
909 - );
910 - }
911 - }
912 -
913 - if ( ! $block_supports_layout && ! $child_layout && empty( $viewport_child_layouts ) ) {
670 + if ( ! $block_supports_layout && ! $child_layout ) {
914 671 return $block_content;
915 672 }
916 673
917 674 $outer_class_names = array();
@@ -916,58 +673,134 @@
916 673
917 674 $outer_class_names = array();
918 675
919 676 // Child layout specific logic.
920 - if ( $child_layout || ! empty( $viewport_child_layouts ) ) {
921 - $base_child_layout = gutenberg_get_layout_child_values( $child_layout );
922 - $parent_layout = $block['parentLayout'] ?? array();
923 -
677 + if ( $child_layout ) {
924 678 /*
925 679 * Generates a unique class for child block layout styles.
926 680 *
927 681 * To ensure consistent class generation across different page renders,
928 682 * only properties that affect layout styling are used. These properties
929 - * come from `$block['attrs']['style']['layout']`, viewport overrides in
930 - * `$block['attrs']['style'][$breakpoint]['layout']`, and
931 - * `$block['parentLayout']`.
683 + * come from `$block['attrs']['style']['layout']` and `$block['parentLayout']`.
932 684 *
933 685 * As long as these properties coincide, the generated class will be the same.
934 686 */
935 - $container_content_hash_input = array(
936 - 'layout' => $base_child_layout,
937 - 'parentLayout' => array_intersect_key(
938 - $parent_layout,
939 - array_flip( array( 'minimumColumnWidth', 'columnCount' ) )
687 + $container_content_class = gutenberg_unique_id_from_values(
688 + array(
689 + 'layout' => array_intersect_key(
690 + $block['attrs']['style']['layout'] ?? array(),
691 + array_flip(
692 + array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' )
693 + )
694 + ),
695 + 'parentLayout' => array_intersect_key(
696 + $block['parentLayout'] ?? array(),
697 + array_flip(
698 + array( 'minimumColumnWidth', 'columnCount' )
699 + )
700 + ),
940 701 ),
702 + 'wp-container-content-'
941 703 );
942 - foreach ( $viewport_child_layouts as $breakpoint => $viewport_data ) {
943 - $container_content_hash_input[ $breakpoint ] = $viewport_data['child_layout'];
704 +
705 + $child_layout_declarations = array();
706 + $child_layout_styles = array();
707 +
708 + $self_stretch = $block['attrs']['style']['layout']['selfStretch'] ?? null;
709 +
710 + if ( 'fixed' === $self_stretch && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
711 + $child_layout_declarations['flex-basis'] = $block['attrs']['style']['layout']['flexSize'];
712 + $child_layout_declarations['box-sizing'] = 'border-box';
713 + } elseif ( 'fill' === $self_stretch ) {
714 + $child_layout_declarations['flex-grow'] = '1';
944 715 }
945 - $container_content_class = gutenberg_unique_id_from_values(
946 - $container_content_hash_input,
947 - 'wp-container-content-'
716 +
717 + $column_start = $block['attrs']['style']['layout']['columnStart'] ?? null;
718 + $column_span = $block['attrs']['style']['layout']['columnSpan'] ?? null;
719 + if ( $column_start && $column_span ) {
720 + $child_layout_declarations['grid-column'] = "$column_start / span $column_span";
721 + } elseif ( $column_start ) {
722 + $child_layout_declarations['grid-column'] = "$column_start";
723 + } elseif ( $column_span ) {
724 + $child_layout_declarations['grid-column'] = "span $column_span";
725 + }
726 +
727 + $row_start = $block['attrs']['style']['layout']['rowStart'] ?? null;
728 + $row_span = $block['attrs']['style']['layout']['rowSpan'] ?? null;
729 + if ( $row_start && $row_span ) {
730 + $child_layout_declarations['grid-row'] = "$row_start / span $row_span";
731 + } elseif ( $row_start ) {
732 + $child_layout_declarations['grid-row'] = "$row_start";
733 + } elseif ( $row_span ) {
734 + $child_layout_declarations['grid-row'] = "span $row_span";
735 + }
736 +
737 + $child_layout_styles[] = array(
738 + 'selector' => ".$container_content_class",
739 + 'declarations' => $child_layout_declarations,
948 740 );
949 741
950 - $child_layout_styles = gutenberg_get_child_layout_style_rules(
951 - ".$container_content_class",
952 - $base_child_layout,
953 - $parent_layout
954 - );
742 + $minimum_column_width = $block['parentLayout']['minimumColumnWidth'] ?? null;
743 + $column_count = $block['parentLayout']['columnCount'] ?? null;
955 744
956 - // Emit responsive child layout CSS using the same container-content class
957 - // so that base and responsive child layout share the exact same selector.
958 - foreach ( $viewport_child_layouts as $viewport_data ) {
959 - $viewport_child_styles = gutenberg_get_child_layout_style_rules(
960 - ".$container_content_class",
961 - $base_child_layout,
962 - $parent_layout,
963 - $viewport_data['child_layout']
964 - );
965 - foreach ( $viewport_child_styles as $index => $rule ) {
966 - $viewport_child_styles[ $index ]['rules_group'] = $viewport_data['media_query'];
745 + /*
746 + * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
747 + * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed
748 + * once the grid has less columns than the start.
749 + * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set.
750 + * In that case, if columnCount doesn't exist, we can assume that the grid is responsive.
751 + */
752 + if ( ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) {
753 + $column_span_number = floatval( $column_span );
754 + $column_start_number = floatval( $column_start );
755 + $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem';
756 + $parent_column_value = floatval( $parent_column_width );
757 + $parent_column_unit = explode( $parent_column_value, $parent_column_width );
758 +
759 + $num_cols_to_break_at = 2;
760 + if ( $column_span_number && $column_start_number ) {
761 + $num_cols_to_break_at = $column_start_number + $column_span_number - 1;
762 + } elseif ( $column_span_number ) {
763 + $num_cols_to_break_at = $column_span_number;
764 + } else {
765 + $num_cols_to_break_at = $column_start_number;
967 766 }
968 767
969 - $child_layout_styles = array_merge( $child_layout_styles, $viewport_child_styles );
768 + /*
769 + * If there is no unit, the width has somehow been mangled so we reset both unit and value
770 + * to defaults.
771 + * Additionally, the unit should be one of px, rem or em, so that also needs to be checked.
772 + */
773 + if ( count( $parent_column_unit ) <= 1 ) {
774 + $parent_column_unit = 'rem';
775 + $parent_column_value = 12;
776 + } else {
777 + $parent_column_unit = $parent_column_unit[1];
778 +
779 + if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) {
780 + $parent_column_unit = 'rem';
781 + }
782 + }
783 +
784 + /*
785 + * A default gap value is used for this computation because custom gap values may not be
786 + * viable to use in the computation of the container query value.
787 + */
788 + $default_gap_value = 'px' === $parent_column_unit ? 24 : 1.5;
789 + $container_query_value = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value;
790 + $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1;
791 + $container_query_value = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit;
792 + // If a span is set we want to preserve it as long as possible, otherwise we just reset the value.
793 + $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto';
794 +
795 + $child_layout_styles[] = array(
796 + 'rules_group' => "@container (max-width: $container_query_value )",
797 + 'selector' => ".$container_content_class",
798 + 'declarations' => array(
799 + 'grid-column' => $grid_column_value,
800 + 'grid-row' => 'auto',
801 + ),
802 + );
970 803 }
971 804
972 805 /*
973 806 * Add to the style engine store to enqueue and render layout styles.
@@ -1065,10 +898,23 @@
1065 898 * Attribute-based Layout classnames are output in all cases.
1066 899 */
1067 900 if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
1068 901
1069 - $gap_value = gutenberg_sanitize_block_gap_value( $block['attrs']['style']['spacing']['blockGap'] ?? null );
902 + $gap_value = $block['attrs']['style']['spacing']['blockGap'] ?? null;
1070 903
904 + /*
905 + * Skip if gap value contains unsupported characters.
906 + * Regex for CSS value borrowed from `safecss_filter_attr`, and used here
907 + * to only match against the value, not the CSS attribute.
908 + */
909 + if ( is_array( $gap_value ) ) {
910 + foreach ( $gap_value as $key => $value ) {
911 + $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
912 + }
913 + } else {
914 + $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
915 + }
916 +
1071 917 $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
1072 918 $block_spacing = $block['attrs']['style']['spacing'] ?? null;
1073 919
1074 920 /*
@@ -1112,41 +958,17 @@
1112 958 * even for different blocks with the same layout definition. We need this to
1113 959 * make the CSS class names stable across paginations for features like the
1114 960 * enhanced pagination of the Query block.
1115 961 */
1116 - $container_class_hash_input = array(
1117 - $used_layout,
1118 - $has_block_gap_support,
1119 - $gap_value,
1120 - $should_skip_gap_serialization,
1121 - $fallback_gap_value,
1122 - $block_spacing,
1123 - );
1124 -
1125 - foreach ( array_keys( WP_Theme_JSON_Gutenberg::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
1126 - $viewport_style = $style_attr[ $breakpoint ] ?? null;
1127 - if ( ! is_array( $viewport_style ) ) {
1128 - continue;
1129 - }
1130 -
1131 - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null );
1132 - if ( ! empty( $viewport_container_layout ) ) {
1133 - $container_class_hash_input[] = array(
1134 - 'breakpoint' => $breakpoint,
1135 - 'layout' => $viewport_container_layout,
1136 - );
1137 - }
1138 -
1139 - if ( isset( $viewport_style['spacing']['blockGap'] ) ) {
1140 - $container_class_hash_input[] = array(
1141 - 'breakpoint' => $breakpoint,
1142 - 'blockGap' => gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ),
1143 - );
1144 - }
1145 - }
1146 -
1147 962 $container_class = gutenberg_unique_id_from_values(
1148 - $container_class_hash_input,
963 + array(
964 + $used_layout,
965 + $has_block_gap_support,
966 + $gap_value,
967 + $should_skip_gap_serialization,
968 + $fallback_gap_value,
969 + $block_spacing,
970 + ),
1149 971 'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
1150 972 );
1151 973
1152 974 $style = gutenberg_get_layout_style(
@@ -1162,53 +984,8 @@
1162 984 // Only add container class and enqueue block support styles if unique styles were generated.
1163 985 if ( ! empty( $style ) ) {
1164 986 $class_names[] = $container_class;
1165 987 }
1166 -
1167 - /*
1168 - * Emit responsive container layout styles using the same $container_class
1169 - * selector as the base layout so they target the inner block wrapper.
1170 - */
1171 - foreach ( WP_Theme_JSON_Gutenberg::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
1172 - $viewport_style = $style_attr[ $breakpoint ] ?? null;
1173 - if ( ! is_array( $viewport_style ) ) {
1174 - continue;
1175 - }
1176 -
1177 - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null );
1178 - $has_viewport_layout = ! empty( $viewport_container_layout );
1179 - $has_viewport_block_gap = isset( $viewport_style['spacing']['blockGap'] );
1180 -
1181 - if ( ! $has_viewport_layout && ! $has_viewport_block_gap ) {
1182 - continue;
1183 - }
1184 -
1185 - $viewport_gap_value = $has_viewport_block_gap
1186 - ? gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] )
1187 - : $gap_value;
1188 - $viewport_block_spacing = is_array( $viewport_style['spacing'] ?? null )
1189 - ? array_replace( is_array( $block_spacing ) ? $block_spacing : array(), $viewport_style['spacing'] )
1190 - : $block_spacing;
1191 -
1192 - $viewport_styles = gutenberg_get_layout_style(
1193 - ".$container_class",
1194 - $used_layout,
1195 - $has_block_gap_support,
1196 - $viewport_gap_value,
1197 - $should_skip_gap_serialization,
1198 - $fallback_gap_value,
1199 - $viewport_block_spacing,
1200 - array(
1201 - 'rules_group' => $media_query,
1202 - 'viewport_overrides' => $viewport_container_layout,
1203 - 'has_block_gap_override' => $has_viewport_block_gap,
1204 - )
1205 - );
1206 -
1207 - if ( ! empty( $viewport_styles ) && ! in_array( $container_class, $class_names, true ) ) {
1208 - $class_names[] = $container_class;
1209 - }
1210 - }
1211 988 }
1212 989
1213 990 // Add combined layout and block classname for global styles to hook onto.
1214 991 $split_block_name = explode( '/', $block['blockName'] );
@@ -1272,31 +1049,12 @@
1272 1049 $inner_block_wrapper_classes = null;
1273 1050 $first_chunk = $block['innerContent'][0] ?? null;
1274 1051 if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
1275 1052 $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
1276 - /*
1277 - * Use a stack to track open elements as tags are visited. Void elements
1278 - * (those without a matching closing tag) are excluded so they don't
1279 - * accumulate on the stack. At the end of the chunk, every element still
1280 - * on the stack is unclosed — meaning its closing tag lives in a later
1281 - * innerContent entry alongside the inner blocks, which makes it the
1282 - * inner-block container. Elements that open and close within this chunk
1283 - * are siblings that precede the inner blocks and should be ignored.
1284 - * The last unclosed element with a class attribute is the best candidate
1285 - * for the inner-block wrapper.
1286 - */
1287 - $tag_stack = array();
1288 - while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
1289 - if ( $first_chunk_processor->is_tag_closer() ) {
1290 - array_pop( $tag_stack );
1291 - } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) {
1292 - $tag_stack[] = $first_chunk_processor->get_attribute( 'class' );
1293 - }
1294 - }
1295 - foreach ( array_reverse( $tag_stack ) as $class_attribute ) {
1053 + while ( $first_chunk_processor->next_tag() ) {
1054 + $class_attribute = $first_chunk_processor->get_attribute( 'class' );
1296 1055 if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
1297 1056 $inner_block_wrapper_classes = $class_attribute;
1298 - break;
1299 1057 }
1300 1058 }
1301 1059 }
1302 1060