@@ -233,7 +233,7 @@ function wp_register_layout_support( $block_type ) {
233233 * @param bool $has_block_gap_support Optional. Whether the theme has support for the block gap. Default false.
234234 * @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null.
235235 * @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
236- * @param string $fallback_gap_value Optional. The block gap value to apply. Default '0.5em'.
236+ * @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'.
237237 * @param array|null $block_spacing Optional. Custom spacing set on the block. Default null.
238238 * @return string CSS styles on success. Else, empty string.
239239 */
@@ -427,7 +427,12 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
427427 foreach ( $ gap_sides as $ gap_side ) {
428428 $ process_value = $ gap_value ;
429429 if ( is_array ( $ gap_value ) ) {
430- $ process_value = $ gap_value [ $ gap_side ] ?? $ fallback_gap_value ;
430+ if ( is_array ( $ fallback_gap_value ) ) {
431+ $ fallback_value = $ fallback_gap_value [ $ gap_side ] ?? reset ( $ fallback_gap_value );
432+ } else {
433+ $ fallback_value = $ fallback_gap_value ;
434+ }
435+ $ process_value = $ gap_value [ $ gap_side ] ?? $ fallback_value ;
431436 }
432437 // Get spacing CSS variable from preset value if provided.
433438 if ( is_string ( $ process_value ) && str_contains ( $ process_value , 'var:preset|spacing| ' ) ) {
@@ -490,21 +495,14 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
490495 }
491496 }
492497 } elseif ( 'grid ' === $ layout_type ) {
493- if ( ! empty ( $ layout ['columnCount ' ] ) ) {
494- $ layout_styles [] = array (
495- 'selector ' => $ selector ,
496- 'declarations ' => array ( 'grid-template-columns ' => 'repeat( ' . $ layout ['columnCount ' ] . ', minmax(0, 1fr)) ' ),
497- );
498+ /*
499+ * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
500+ * is the relevant one for computation of responsive grid columns.
501+ */
502+ if ( is_array ( $ fallback_gap_value ) ) {
503+ $ responsive_gap_value = $ fallback_gap_value ['left ' ] ?? reset ( $ fallback_gap_value );
498504 } else {
499- $ minimum_column_width = ! empty ( $ layout ['minimumColumnWidth ' ] ) ? $ layout ['minimumColumnWidth ' ] : '12rem ' ;
500-
501- $ layout_styles [] = array (
502- 'selector ' => $ selector ,
503- 'declarations ' => array (
504- 'grid-template-columns ' => 'repeat(auto-fill, minmax(min( ' . $ minimum_column_width . ', 100%), 1fr)) ' ,
505- 'container-type ' => 'inline-size ' ,
506- ),
507- );
505+ $ responsive_gap_value = $ fallback_gap_value ;
508506 }
509507
510508 if ( $ has_block_gap_support && isset ( $ gap_value ) ) {
@@ -514,7 +512,12 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
514512 foreach ( $ gap_sides as $ gap_side ) {
515513 $ process_value = $ gap_value ;
516514 if ( is_array ( $ gap_value ) ) {
517- $ process_value = $ gap_value [ $ gap_side ] ?? $ fallback_gap_value ;
515+ if ( is_array ( $ fallback_gap_value ) ) {
516+ $ fallback_value = $ fallback_gap_value [ $ gap_side ] ?? reset ( $ fallback_gap_value );
517+ } else {
518+ $ fallback_value = $ fallback_gap_value ;
519+ }
520+ $ process_value = $ gap_value [ $ gap_side ] ?? $ fallback_value ;
518521 }
519522 // Get spacing CSS variable from preset value if provided.
520523 if ( is_string ( $ process_value ) && str_contains ( $ process_value , 'var:preset|spacing| ' ) ) {
@@ -524,14 +527,58 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
524527 }
525528 $ combined_gap_value .= "$ process_value " ;
526529 }
527- $ gap_value = trim ( $ combined_gap_value );
530+ $ gap_value = trim ( $ combined_gap_value );
531+ $ responsive_gap_value = $ gap_value ;
532+ }
528533
529- if ( null !== $ gap_value && ! $ should_skip_gap_serialization ) {
534+ // Ensure 0 values have a unit so they work in calc().
535+ if ( '0 ' === $ responsive_gap_value || 0 === $ responsive_gap_value ) {
536+ $ responsive_gap_value = '0px ' ;
537+ }
538+
539+ if ( ! empty ( $ layout ['columnCount ' ] ) && ! empty ( $ layout ['minimumColumnWidth ' ] ) ) {
540+ $ max_value = 'max(min( ' . $ layout ['minimumColumnWidth ' ] . ', 100%), (100% - ( ' . $ responsive_gap_value . ' * ( ' . $ layout ['columnCount ' ] . ' - 1))) / ' . $ layout ['columnCount ' ] . ') ' ;
541+ $ layout_styles [] = array (
542+ 'selector ' => $ selector ,
543+ 'declarations ' => array (
544+ 'grid-template-columns ' => 'repeat(auto-fill, minmax( ' . $ max_value . ', 1fr)) ' ,
545+ 'container-type ' => 'inline-size ' ,
546+ ),
547+ );
548+ if ( ! empty ( $ layout ['rowCount ' ] ) ) {
530549 $ layout_styles [] = array (
531550 'selector ' => $ selector ,
532- 'declarations ' => array ( 'gap ' => $ gap_value ),
551+ 'declarations ' => array ( 'grid-template-rows ' => ' repeat( ' . $ layout [ ' rowCount ' ] . ' , minmax(1rem, auto)) ' ),
533552 );
534553 }
554+ } elseif ( ! empty ( $ layout ['columnCount ' ] ) ) {
555+ $ layout_styles [] = array (
556+ 'selector ' => $ selector ,
557+ 'declarations ' => array ( 'grid-template-columns ' => 'repeat( ' . $ layout ['columnCount ' ] . ', minmax(0, 1fr)) ' ),
558+ );
559+ if ( ! empty ( $ layout ['rowCount ' ] ) ) {
560+ $ layout_styles [] = array (
561+ 'selector ' => $ selector ,
562+ 'declarations ' => array ( 'grid-template-rows ' => 'repeat( ' . $ layout ['rowCount ' ] . ', minmax(1rem, auto)) ' ),
563+ );
564+ }
565+ } else {
566+ $ minimum_column_width = ! empty ( $ layout ['minimumColumnWidth ' ] ) ? $ layout ['minimumColumnWidth ' ] : '12rem ' ;
567+
568+ $ layout_styles [] = array (
569+ 'selector ' => $ selector ,
570+ 'declarations ' => array (
571+ 'grid-template-columns ' => 'repeat(auto-fill, minmax(min( ' . $ minimum_column_width . ', 100%), 1fr)) ' ,
572+ 'container-type ' => 'inline-size ' ,
573+ ),
574+ );
575+ }
576+
577+ if ( $ has_block_gap_support && null !== $ gap_value && ! $ should_skip_gap_serialization ) {
578+ $ layout_styles [] = array (
579+ 'selector ' => $ selector ,
580+ 'declarations ' => array ( 'gap ' => $ gap_value ),
581+ );
535582 }
536583 }
537584
@@ -568,6 +615,8 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
568615 * @return string Filtered block content.
569616 */
570617function wp_render_layout_support_flag ( $ block_content , $ block ) {
618+ static $ global_styles = null ;
619+
571620 $ block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( $ block ['blockName ' ] );
572621 $ block_supports_layout = block_has_support ( $ block_type , 'layout ' , false ) || block_has_support ( $ block_type , '__experimentalLayout ' , false );
573622 $ child_layout = $ block ['attrs ' ]['style ' ]['layout ' ] ?? null ;
@@ -804,6 +853,18 @@ function wp_render_layout_support_flag( $block_content, $block ) {
804853 $ block_gap = $ global_settings ['spacing ' ]['blockGap ' ] ?? null ;
805854 $ has_block_gap_support = isset ( $ block_gap );
806855
856+ // Get default blockGap value from global styles for use in layouts like grid.
857+ // Check block-specific styles first, then fall back to root styles.
858+ $ block_name = $ block ['blockName ' ] ?? '' ;
859+ if ( null === $ global_styles ) {
860+ $ global_styles = wp_get_global_styles ();
861+ }
862+ $ global_block_gap_value = $ global_styles ['blocks ' ][ $ block_name ]['spacing ' ]['blockGap ' ] ?? ( $ global_styles ['spacing ' ]['blockGap ' ] ?? null );
863+
864+ if ( null !== $ global_block_gap_value ) {
865+ $ fallback_gap_value = $ global_block_gap_value ;
866+ }
867+
807868 /*
808869 * Generates a unique ID based on all the data required to obtain the
809870 * corresponding layout style. Keeps the CSS class names the same
0 commit comments