Make WordPress Core


Ignore:
Timestamp:
02/16/2026 11:50:37 PM (3 months ago)
Author:
isabel_brison
Message:

Editor: fix grid layout for style variations defining blockGap.

Ensures the grid block columns computation takes into account any blockGap value output by an active block style variation.

Props isabel_brison, mukesh27, westonruter, andrewserong.
Fixes #64624.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/layout.php

    r61513 r61655  
    66 * @since 5.8.0
    77 */
     8
     9/**
     10 * Gets the first style variation name from a className string that matches a registered style.
     11 *
     12 * @since 7.0.0
     13 *
     14 * @param string                              $class_name        CSS class string for a block.
     15 * @param array<string, array<string, mixed>> $registered_styles Currently registered block styles.
     16 * @return string|null The name of the first registered variation, or null if none found.
     17 */
     18function wp_get_block_style_variation_name_from_registered_style( string $class_name, array $registered_styles = array() ): ?string {
     19    if ( ! $class_name ) {
     20        return null;
     21    }
     22
     23    $registered_names = array_filter( array_column( $registered_styles, 'name' ) );
     24
     25    $prefix = 'is-style-';
     26    $length = strlen( $prefix );
     27
     28    foreach ( explode( ' ', $class_name ) as $class ) {
     29        if ( str_starts_with( $class, $prefix ) ) {
     30            $variation = substr( $class, $length );
     31            if ( 'default' !== $variation && in_array( $variation, $registered_names, true ) ) {
     32                return $variation;
     33            }
     34        }
     35    }
     36
     37    return null;
     38}
    839
    940/**
     
    855886
    856887        // 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.
     888        // Check style variation first, then block-specific styles, then fall back to root styles.
    858889        $block_name = $block['blockName'] ?? '';
    859890        if ( null === $global_styles ) {
    860891            $global_styles = wp_get_global_styles();
    861892        }
    862         $global_block_gap_value = $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? ( $global_styles['spacing']['blockGap'] ?? null );
     893
     894        // Check if the block has an active style variation with a blockGap value.
     895        // Only check the registry if the className contains a variation class to avoid unnecessary lookups.
     896        $variation_block_gap_value = null;
     897        $block_class_name          = $block['attrs']['className'] ?? '';
     898        if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) {
     899            $styles_registry   = WP_Block_Styles_Registry::get_instance();
     900            $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name );
     901            $variation_name    = wp_get_block_style_variation_name_from_registered_style( $block_class_name, $registered_styles );
     902            if ( $variation_name ) {
     903                $variation_block_gap_value = $global_styles['blocks'][ $block_name ]['variations'][ $variation_name ]['spacing']['blockGap'] ?? null;
     904            }
     905        }
     906
     907        $global_block_gap_value = $variation_block_gap_value ?? $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null;
    863908
    864909        if ( null !== $global_block_gap_value ) {
Note: See TracChangeset for help on using the changeset viewer.