From ff2b3047557a591757d1b37c4c4fb4af2884cf7e Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 16 Jun 2023 17:25:33 +1000 Subject: [PATCH 1/2] Add new grid layout type --- src/wp-includes/block-supports/layout.php | 38 +++++++++++++++++++++++ src/wp-includes/class-wp-theme-json.php | 2 +- src/wp-includes/theme.json | 22 +++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index 98b44079ba747..76f935732c3f8 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -287,6 +287,44 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false ); } } + } elseif ( 'grid' === $layout_type ) { + if ( ! empty( $layout['columnCount'] ) ) { + $layout_styles[] = array( + 'selector' => $selector, + 'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ), + ); + } else { + $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem'; + + $layout_styles[] = array( + 'selector' => $selector, + 'declarations' => array( 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))' ), + ); + } + + if ( $has_block_gap_support && isset( $gap_value ) ) { + $combined_gap_value = ''; + $gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' ); + + foreach ( $gap_sides as $gap_side ) { + $process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value ); + // Get spacing CSS variable from preset value if provided. + if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) { + $index_to_splice = strrpos( $process_value, '|' ) + 1; + $slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) ); + $process_value = "var(--wp--preset--spacing--$slug)"; + } + $combined_gap_value .= "$process_value "; + } + $gap_value = trim( $combined_gap_value ); + + if ( null !== $gap_value && ! $should_skip_gap_serialization ) { + $layout_styles[] = array( + 'selector' => $selector, + 'declarations' => array( 'gap' => $gap_value ), + ); + } + } } if ( ! empty( $layout_styles ) ) { diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 700cb20f7e8e2..c157b2462cb3f 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1370,7 +1370,7 @@ protected function get_layout_styles( $block_metadata ) { if ( ! empty( $class_name ) && - ! empty( $base_style_rules ) + is_array( $base_style_rules ) ) { // Output display mode. This requires special handling as `display` is not exposed in `safe_style_css_filter`. if ( diff --git a/src/wp-includes/theme.json b/src/wp-includes/theme.json index 581bdb0b32406..650072457d478 100644 --- a/src/wp-includes/theme.json +++ b/src/wp-includes/theme.json @@ -332,6 +332,28 @@ } } ] + }, + "grid": { + "name": "grid", + "slug": "grid", + "className": "is-layout-grid", + "displayMode": "grid", + "baseStyles": [ + { + "selector": " > *", + "rules": { + "margin": "0" + } + } + ], + "spacingStyles": [ + { + "selector": "", + "rules": { + "gap": null + } + } + ] } } }, From ff36b0a5925cad8d8954dda28d5bc8d7a86e5bae Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Wed, 21 Jun 2023 13:28:44 +1000 Subject: [PATCH 2/2] Add since comment for grid layout --- src/wp-includes/block-supports/layout.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/block-supports/layout.php b/src/wp-includes/block-supports/layout.php index 76f935732c3f8..4e19785318fbc 100644 --- a/src/wp-includes/block-supports/layout.php +++ b/src/wp-includes/block-supports/layout.php @@ -34,6 +34,7 @@ function wp_register_layout_support( $block_type ) { * * @since 5.9.0 * @since 6.1.0 Added `$block_spacing` param, use style engine to enqueue styles. + * @since 6.3.0 Added grid layout type. * @access private * * @param string $selector CSS selector.