[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/block-supports/ -> layout.php (source)

   1  <?php
   2  /**
   3   * Layout block support flag.
   4   *
   5   * @package WordPress
   6   * @since 5.8.0
   7   */
   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   */
  18  function 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  }
  39  
  40  /**
  41   * Returns the child-layout-only subset of a layout object.
  42   *
  43   * @since 7.1.0
  44   *
  45   * @param mixed $layout Layout object.
  46   * @return array Child layout values, or an empty array.
  47   */
  48  function wp_get_layout_child_values( $layout ) {
  49      if ( ! is_array( $layout ) ) {
  50          return array();
  51      }
  52  
  53      return array_intersect_key(
  54          $layout,
  55          array_flip( array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' ) )
  56      );
  57  }
  58  
  59  /**
  60   * Returns the container-layout subset of a layout object.
  61   *
  62   * @since 7.1.0
  63   *
  64   * @param mixed $layout Layout object.
  65   * @return array Container layout values, or an empty array.
  66   */
  67  function wp_get_layout_container_values( $layout ) {
  68      if ( ! is_array( $layout ) ) {
  69          return array();
  70      }
  71  
  72      return array_diff_key(
  73          $layout,
  74          array_flip( array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' ) )
  75      );
  76  }
  77  
  78  /**
  79   * Sanitizes a block gap value before layout style generation.
  80   *
  81   * @since 7.1.0
  82   *
  83   * @param string|array|null $gap_value Block gap value.
  84   * @return string|array|null Sanitized block gap value.
  85   */
  86  function wp_sanitize_block_gap_value( $gap_value ) {
  87      if ( is_array( $gap_value ) ) {
  88          foreach ( $gap_value as $key => $value ) {
  89              $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
  90          }
  91  
  92          return $gap_value;
  93      }
  94  
  95      return $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
  96  }
  97  
  98  /**
  99   * Returns child layout styles for a block affected by its parent's layout.
 100   *
 101   * @since 7.1.0
 102   *
 103   * @param string     $selector           CSS selector.
 104   * @param array      $child_layout       Child layout values.
 105   * @param array      $parent_layout      Parent layout values.
 106   * @param array|null $viewport_overrides Optional. Child viewport layout overrides to emit.
 107   * @return array Child layout style rules.
 108   */
 109  function wp_get_child_layout_style_rules( $selector, $child_layout, $parent_layout = array(), $viewport_overrides = null ) {
 110      $base_child_layout              = is_array( $child_layout ) ? $child_layout : array();
 111      $viewport_overrides             = is_array( $viewport_overrides ) ? $viewport_overrides : null;
 112      $child_layout                   = null === $viewport_overrides ? $base_child_layout : array_replace( $base_child_layout, $viewport_overrides );
 113      $child_layout_declarations      = array();
 114      $child_layout_styles            = array();
 115      $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
 116          return array_key_exists( $property, $viewport_overrides );
 117      };
 118  
 119      $self_stretch      = $child_layout['selfStretch'] ?? null;
 120      $base_self_stretch = $base_child_layout['selfStretch'] ?? null;
 121  
 122      /*
 123       * These are the serialized `selfStretch` values. `max` used to be called
 124       * "Fixed" in the UI, but was renamed and replaced by `fixedNoShrink`.
 125       */
 126      $flex_child_layout_values = array(
 127          'fit'   => 'fit',
 128          'grow'  => 'fill',
 129          'max'   => 'fixed',
 130          'fixed' => 'fixedNoShrink',
 131      );
 132      $flex_size_values         = array(
 133          $flex_child_layout_values['max'],
 134          $flex_child_layout_values['fixed'],
 135      );
 136  
 137      if ( null === $viewport_overrides || $has_viewport_property_override( 'selfStretch' ) || $has_viewport_property_override( 'flexSize' ) ) {
 138          if (
 139              null !== $viewport_overrides &&
 140              ( $flex_child_layout_values['fit'] === $self_stretch || $flex_child_layout_values['grow'] === $self_stretch ) &&
 141              in_array( $base_self_stretch, $flex_size_values, true ) &&
 142              isset( $base_child_layout['flexSize'] )
 143          ) {
 144              $child_layout_declarations['flex-basis'] = 'unset';
 145              if ( $flex_child_layout_values['fixed'] === $base_self_stretch ) {
 146                  $child_layout_declarations['flex-shrink'] = 'unset';
 147              }
 148          }
 149          if ( in_array( $self_stretch, $flex_size_values, true ) && isset( $child_layout['flexSize'] ) ) {
 150              $child_layout_declarations['flex-basis'] = $child_layout['flexSize'];
 151              if ( $flex_child_layout_values['fixed'] === $self_stretch ) {
 152                  $child_layout_declarations['flex-shrink'] = '0';
 153              } elseif ( null !== $viewport_overrides && $flex_child_layout_values['fixed'] === $base_self_stretch ) {
 154                  $child_layout_declarations['flex-shrink'] = 'unset';
 155              }
 156              $child_layout_declarations['box-sizing'] = 'border-box';
 157          } elseif ( $flex_child_layout_values['grow'] === $self_stretch ) {
 158              $child_layout_declarations['flex-grow'] = '1';
 159          }
 160      }
 161  
 162      $column_start = $child_layout['columnStart'] ?? null;
 163      $column_span  = $child_layout['columnSpan'] ?? null;
 164      if ( null === $viewport_overrides || $has_viewport_property_override( 'columnStart' ) || $has_viewport_property_override( 'columnSpan' ) ) {
 165          if ( $column_start && $column_span ) {
 166              $child_layout_declarations['grid-column'] = "$column_start / span $column_span";
 167          } elseif ( $column_start ) {
 168              $child_layout_declarations['grid-column'] = "$column_start";
 169          } elseif ( $column_span ) {
 170              $child_layout_declarations['grid-column'] = "span $column_span";
 171          }
 172      }
 173  
 174      $row_start = $child_layout['rowStart'] ?? null;
 175      $row_span  = $child_layout['rowSpan'] ?? null;
 176      if ( null === $viewport_overrides || $has_viewport_property_override( 'rowStart' ) || $has_viewport_property_override( 'rowSpan' ) ) {
 177          if ( $row_start && $row_span ) {
 178              $child_layout_declarations['grid-row'] = "$row_start / span $row_span";
 179          } elseif ( $row_start ) {
 180              $child_layout_declarations['grid-row'] = "$row_start";
 181          } elseif ( $row_span ) {
 182              $child_layout_declarations['grid-row'] = "span $row_span";
 183          }
 184      }
 185  
 186      if ( ! empty( $child_layout_declarations ) ) {
 187          $child_layout_styles[] = array(
 188              'selector'     => $selector,
 189              'declarations' => $child_layout_declarations,
 190          );
 191      }
 192  
 193      $minimum_column_width = $parent_layout['minimumColumnWidth'] ?? null;
 194      $column_count         = $parent_layout['columnCount'] ?? null;
 195  
 196      /*
 197       * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set,
 198       * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed
 199       * once the grid has less columns than the start.
 200       * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set.
 201       * In that case, if columnCount doesn't exist, we can assume that the grid is responsive.
 202       */
 203      if ( null === $viewport_overrides && ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) {
 204          $column_span_number  = floatval( $column_span );
 205          $column_start_number = floatval( $column_start );
 206          $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem';
 207          $parent_column_value = floatval( $parent_column_width );
 208          $parent_column_unit  = explode( $parent_column_value, $parent_column_width );
 209  
 210          $num_cols_to_break_at = 2;
 211          if ( $column_span_number && $column_start_number ) {
 212              $num_cols_to_break_at = $column_start_number + $column_span_number - 1;
 213          } elseif ( $column_span_number ) {
 214              $num_cols_to_break_at = $column_span_number;
 215          } else {
 216              $num_cols_to_break_at = $column_start_number;
 217          }
 218  
 219          /*
 220           * If there is no unit, the width has somehow been mangled so we reset both unit and value
 221           * to defaults.
 222           * Additionally, the unit should be one of px, rem or em, so that also needs to be checked.
 223           */
 224          if ( count( $parent_column_unit ) <= 1 ) {
 225              $parent_column_unit  = 'rem';
 226              $parent_column_value = 12;
 227          } else {
 228              $parent_column_unit = $parent_column_unit[1];
 229  
 230              if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) {
 231                  $parent_column_unit = 'rem';
 232              }
 233          }
 234  
 235          /*
 236           * A default gap value is used for this computation because custom gap values may not be
 237           * viable to use in the computation of the container query value.
 238           */
 239          $default_gap_value             = 'px' === $parent_column_unit ? 24 : 1.5;
 240          $container_query_value         = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value;
 241          $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1;
 242          $container_query_value         = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit;
 243          // If a span is set we want to preserve it as long as possible, otherwise we just reset the value.
 244          $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto';
 245  
 246          $child_layout_styles[] = array(
 247              'rules_group'  => "@container (max-width: $container_query_value )",
 248              'selector'     => $selector,
 249              'declarations' => array(
 250                  'grid-column' => $grid_column_value,
 251                  'grid-row'    => 'auto',
 252              ),
 253          );
 254      }
 255  
 256      return $child_layout_styles;
 257  }
 258  
 259  /**
 260   * Returns layout definitions, keyed by layout type.
 261   *
 262   * Provides a common definition of slugs, classnames, base styles, and spacing styles for each layout type.
 263   * When making changes or additions to layout definitions, the corresponding JavaScript definitions should
 264   * also be updated.
 265   *
 266   * @since 6.3.0
 267   * @since 6.6.0 Updated specificity for compatibility with 0-1-0 global styles specificity.
 268   * @access private
 269   *
 270   * @return array[] Layout definitions.
 271   */
 272  function wp_get_layout_definitions() {
 273      $layout_definitions = array(
 274          'default'     => array(
 275              'name'          => 'default',
 276              'slug'          => 'flow',
 277              'className'     => 'is-layout-flow',
 278              'baseStyles'    => array(
 279                  array(
 280                      'selector' => ' > .alignleft',
 281                      'rules'    => array(
 282                          'float'               => 'left',
 283                          'margin-inline-start' => '0',
 284                          'margin-inline-end'   => '2em',
 285                      ),
 286                  ),
 287                  array(
 288                      'selector' => ' > .alignright',
 289                      'rules'    => array(
 290                          'float'               => 'right',
 291                          'margin-inline-start' => '2em',
 292                          'margin-inline-end'   => '0',
 293                      ),
 294                  ),
 295                  array(
 296                      'selector' => ' > .aligncenter',
 297                      'rules'    => array(
 298                          'margin-left'  => 'auto !important',
 299                          'margin-right' => 'auto !important',
 300                      ),
 301                  ),
 302              ),
 303              'spacingStyles' => array(
 304                  array(
 305                      'selector' => ' > :first-child',
 306                      'rules'    => array(
 307                          'margin-block-start' => '0',
 308                      ),
 309                  ),
 310                  array(
 311                      'selector' => ' > :last-child',
 312                      'rules'    => array(
 313                          'margin-block-end' => '0',
 314                      ),
 315                  ),
 316                  array(
 317                      'selector' => ' > *',
 318                      'rules'    => array(
 319                          'margin-block-start' => null,
 320                          'margin-block-end'   => '0',
 321                      ),
 322                  ),
 323              ),
 324          ),
 325          'constrained' => array(
 326              'name'          => 'constrained',
 327              'slug'          => 'constrained',
 328              'className'     => 'is-layout-constrained',
 329              'baseStyles'    => array(
 330                  array(
 331                      'selector' => ' > .alignleft',
 332                      'rules'    => array(
 333                          'float'               => 'left',
 334                          'margin-inline-start' => '0',
 335                          'margin-inline-end'   => '2em',
 336                      ),
 337                  ),
 338                  array(
 339                      'selector' => ' > .alignright',
 340                      'rules'    => array(
 341                          'float'               => 'right',
 342                          'margin-inline-start' => '2em',
 343                          'margin-inline-end'   => '0',
 344                      ),
 345                  ),
 346                  array(
 347                      'selector' => ' > .aligncenter',
 348                      'rules'    => array(
 349                          'margin-left'  => 'auto !important',
 350                          'margin-right' => 'auto !important',
 351                      ),
 352                  ),
 353                  array(
 354                      'selector' => ' > :where(:not(.alignleft):not(.alignright):not(.alignfull))',
 355                      'rules'    => array(
 356                          'max-width'    => 'var(--wp--style--global--content-size)',
 357                          'margin-left'  => 'auto !important',
 358                          'margin-right' => 'auto !important',
 359                      ),
 360                  ),
 361                  array(
 362                      'selector' => ' > .alignwide',
 363                      'rules'    => array(
 364                          'max-width' => 'var(--wp--style--global--wide-size)',
 365                      ),
 366                  ),
 367              ),
 368              'spacingStyles' => array(
 369                  array(
 370                      'selector' => ' > :first-child',
 371                      'rules'    => array(
 372                          'margin-block-start' => '0',
 373                      ),
 374                  ),
 375                  array(
 376                      'selector' => ' > :last-child',
 377                      'rules'    => array(
 378                          'margin-block-end' => '0',
 379                      ),
 380                  ),
 381                  array(
 382                      'selector' => ' > *',
 383                      'rules'    => array(
 384                          'margin-block-start' => null,
 385                          'margin-block-end'   => '0',
 386                      ),
 387                  ),
 388              ),
 389          ),
 390          'flex'        => array(
 391              'name'          => 'flex',
 392              'slug'          => 'flex',
 393              'className'     => 'is-layout-flex',
 394              'displayMode'   => 'flex',
 395              'baseStyles'    => array(
 396                  array(
 397                      'selector' => '',
 398                      'rules'    => array(
 399                          'flex-wrap'   => 'wrap',
 400                          'align-items' => 'center',
 401                      ),
 402                  ),
 403                  array(
 404                      'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
 405                      'rules'    => array(
 406                          'margin' => '0',
 407                      ),
 408                  ),
 409              ),
 410              'spacingStyles' => array(
 411                  array(
 412                      'selector' => '',
 413                      'rules'    => array(
 414                          'gap' => null,
 415                      ),
 416                  ),
 417              ),
 418          ),
 419          'grid'        => array(
 420              'name'          => 'grid',
 421              'slug'          => 'grid',
 422              'className'     => 'is-layout-grid',
 423              'displayMode'   => 'grid',
 424              'baseStyles'    => array(
 425                  array(
 426                      'selector' => ' > :is(*, div)', // :is(*, div) instead of just * increases the specificity by 001.
 427                      'rules'    => array(
 428                          'margin' => '0',
 429                      ),
 430                  ),
 431              ),
 432              'spacingStyles' => array(
 433                  array(
 434                      'selector' => '',
 435                      'rules'    => array(
 436                          'gap' => null,
 437                      ),
 438                  ),
 439              ),
 440          ),
 441      );
 442  
 443      return $layout_definitions;
 444  }
 445  
 446  /**
 447   * Registers the layout block attribute for block types that support it.
 448   *
 449   * @since 5.8.0
 450   * @since 6.3.0 Check for layout support via the `layout` key with fallback to `__experimentalLayout`.
 451   * @access private
 452   *
 453   * @param WP_Block_Type $block_type Block Type.
 454   */
 455  function wp_register_layout_support( $block_type ) {
 456      $support_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
 457      if ( $support_layout ) {
 458          if ( ! $block_type->attributes ) {
 459              $block_type->attributes = array();
 460          }
 461  
 462          if ( ! array_key_exists( 'layout', $block_type->attributes ) ) {
 463              $block_type->attributes['layout'] = array(
 464                  'type' => 'object',
 465              );
 466          }
 467      }
 468  }
 469  
 470  /**
 471   * Generates the CSS corresponding to the provided layout.
 472   *
 473   * @since 5.9.0
 474   * @since 6.1.0 Added `$block_spacing` param, use style engine to enqueue styles.
 475   * @since 6.3.0 Added grid layout type.
 476   * @since 6.6.0 Removed duplicated selector from layout styles.
 477   *              Enabled negative margins for alignfull children of blocks with custom padding.
 478   * @since 7.1.0 Added options array with options to process responsive styles.
 479   * @access private
 480   *
 481   * @param string               $selector                      CSS selector.
 482   * @param array                $layout                        Layout object. The one that is passed has already checked
 483   *                                                            the existence of default block layout.
 484   * @param bool                 $has_block_gap_support         Optional. Whether the theme has support for the block gap. Default false.
 485   * @param string|string[]|null $gap_value                     Optional. The block gap value to apply. Default null.
 486   * @param bool                 $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
 487   * @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'.
 488   * @param array|null           $block_spacing                 Optional. Custom spacing set on the block. Default null.
 489   * @param array                $options                       {
 490   *     Optional. Extra options for internal callers. Default empty array.
 491   *
 492   *     @type array       $viewport_overrides     An array of layout property overrides for the sake of style generation,
 493   *                                               keyed by property name.
 494   *     @type string|null $rules_group            Optional group name for the rules. Default null.
 495   *     @type bool        $has_block_gap_override Whether the block gap has been overridden. Default false.
 496   * }
 497   * @return string CSS styles on success. Else, empty string.
 498   */
 499  function wp_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() ) {
 500      $base_layout                    = is_array( $layout ) ? $layout : array();
 501      $viewport_overrides             = $options['viewport_overrides'] ?? null;
 502      $layout_for_styles              = null === $viewport_overrides ? $base_layout : array_replace( $base_layout, $viewport_overrides );
 503      $layout_type                    = $base_layout['type'] ?? 'default';
 504      $rules_group                    = $options['rules_group'] ?? null;
 505      $has_block_gap_override         = ! empty( $options['has_block_gap_override'] );
 506      $should_output_block_gap        = null === $viewport_overrides || $has_block_gap_override;
 507      $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
 508          return array_key_exists( $property, $viewport_overrides );
 509      };
 510      $layout_styles                  = array();
 511  
 512      if ( 'default' === $layout_type ) {
 513          if ( $has_block_gap_support && $should_output_block_gap ) {
 514              if ( is_array( $gap_value ) ) {
 515                  $gap_value = $gap_value['top'] ?? null;
 516              }
 517              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 518                  // Get spacing CSS variable from preset value if provided.
 519                  if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
 520                      $index_to_splice = strrpos( $gap_value, '|' ) + 1;
 521                      $slug            = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
 522                      $gap_value       = "var(--wp--preset--spacing--$slug)";
 523                  }
 524  
 525                  array_push(
 526                      $layout_styles,
 527                      array(
 528                          'selector'     => "$selector > *",
 529                          'declarations' => array(
 530                              'margin-block-start' => '0',
 531                              'margin-block-end'   => '0',
 532                          ),
 533                      ),
 534                      array(
 535                          'selector'     => "$selector > * + *",
 536                          'declarations' => array(
 537                              'margin-block-start' => $gap_value,
 538                              'margin-block-end'   => '0',
 539                          ),
 540                      )
 541                  );
 542              }
 543          }
 544      } elseif ( 'constrained' === $layout_type ) {
 545          $content_size    = $layout_for_styles['contentSize'] ?? '';
 546          $wide_size       = $layout_for_styles['wideSize'] ?? '';
 547          $justify_content = $layout_for_styles['justifyContent'] ?? 'center';
 548  
 549          $all_max_width_value  = $content_size ? $content_size : $wide_size;
 550          $wide_max_width_value = $wide_size ? $wide_size : $content_size;
 551  
 552          // Make sure there is a single CSS rule, and all tags are stripped for security.
 553          $all_max_width_value  = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
 554          $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
 555  
 556          $margin_left  = 'left' === $justify_content ? '0 !important' : 'auto !important';
 557          $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
 558  
 559          $has_justify_content_override    = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' );
 560          $should_output_constrained_sizes = null === $viewport_overrides || $has_viewport_property_override( 'contentSize' ) || $has_viewport_property_override( 'wideSize' );
 561          if ( $should_output_constrained_sizes && ( $content_size || $wide_size ) ) {
 562              $content_size_declarations = array(
 563                  'max-width' => $all_max_width_value,
 564              );
 565  
 566              if ( null === $viewport_overrides || $has_justify_content_override ) {
 567                  $content_size_declarations['margin-left']  = $margin_left;
 568                  $content_size_declarations['margin-right'] = $margin_right;
 569              }
 570  
 571              array_push(
 572                  $layout_styles,
 573                  array(
 574                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 575                      'declarations' => $content_size_declarations,
 576                  ),
 577                  array(
 578                      'selector'     => "$selector > .alignwide",
 579                      'declarations' => array( 'max-width' => $wide_max_width_value ),
 580                  ),
 581                  array(
 582                      'selector'     => "$selector .alignfull",
 583                      'declarations' => array( 'max-width' => 'none' ),
 584                  )
 585              );
 586          }
 587  
 588          if ( null === $viewport_overrides && isset( $block_spacing ) ) {
 589              $block_spacing_values = wp_style_engine_get_styles(
 590                  array(
 591                      'spacing' => $block_spacing,
 592                  )
 593              );
 594  
 595              /*
 596               * Handle negative margins for alignfull children of blocks with custom padding set.
 597               * They're added separately because padding might only be set on one side.
 598               */
 599              if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
 600                  $padding_right = $block_spacing_values['declarations']['padding-right'];
 601                  // Add unit if 0.
 602                  if ( '0' === $padding_right ) {
 603                      $padding_right = '0px';
 604                  }
 605                  $layout_styles[] = array(
 606                      'selector'     => "$selector > .alignfull",
 607                      'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
 608                  );
 609              }
 610              if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
 611                  $padding_left = $block_spacing_values['declarations']['padding-left'];
 612                  // Add unit if 0.
 613                  if ( '0' === $padding_left ) {
 614                      $padding_left = '0px';
 615                  }
 616                  $layout_styles[] = array(
 617                      'selector'     => "$selector > .alignfull",
 618                      'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
 619                  );
 620              }
 621          }
 622  
 623          if ( $has_justify_content_override && ! $should_output_constrained_sizes ) {
 624              $layout_styles[] = array(
 625                  'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 626                  'declarations' => array(
 627                      'margin-left'  => $margin_left,
 628                      'margin-right' => $margin_right,
 629                  ),
 630              );
 631          } elseif ( null === $viewport_overrides ) {
 632              if ( 'left' === $justify_content ) {
 633                  $layout_styles[] = array(
 634                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 635                      'declarations' => array( 'margin-left' => '0 !important' ),
 636                  );
 637              }
 638  
 639              if ( 'right' === $justify_content ) {
 640                  $layout_styles[] = array(
 641                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 642                      'declarations' => array( 'margin-right' => '0 !important' ),
 643                  );
 644              }
 645          }
 646  
 647          if ( $has_block_gap_support && $should_output_block_gap ) {
 648              if ( is_array( $gap_value ) ) {
 649                  $gap_value = $gap_value['top'] ?? null;
 650              }
 651              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 652                  // Get spacing CSS variable from preset value if provided.
 653                  if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
 654                      $index_to_splice = strrpos( $gap_value, '|' ) + 1;
 655                      $slug            = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
 656                      $gap_value       = "var(--wp--preset--spacing--$slug)";
 657                  }
 658  
 659                  array_push(
 660                      $layout_styles,
 661                      array(
 662                          'selector'     => "$selector > *",
 663                          'declarations' => array(
 664                              'margin-block-start' => '0',
 665                              'margin-block-end'   => '0',
 666                          ),
 667                      ),
 668                      array(
 669                          'selector'     => "$selector > * + *",
 670                          'declarations' => array(
 671                              'margin-block-start' => $gap_value,
 672                              'margin-block-end'   => '0',
 673                          ),
 674                      )
 675                  );
 676              }
 677          }
 678      } elseif ( 'flex' === $layout_type ) {
 679          $layout_orientation = $layout_for_styles['orientation'] ?? 'horizontal';
 680  
 681          $justify_content_options = array(
 682              'left'   => 'flex-start',
 683              'right'  => 'flex-end',
 684              'center' => 'center',
 685          );
 686  
 687          $vertical_alignment_options = array(
 688              'top'    => 'flex-start',
 689              'center' => 'center',
 690              'bottom' => 'flex-end',
 691          );
 692  
 693          if ( 'horizontal' === $layout_orientation ) {
 694              $justify_content_options    += array( 'space-between' => 'space-between' );
 695              $vertical_alignment_options += array( 'stretch' => 'stretch' );
 696          } else {
 697              $justify_content_options    += array( 'stretch' => 'stretch' );
 698              $vertical_alignment_options += array( 'space-between' => 'space-between' );
 699          }
 700  
 701          $should_output_flex_wrap          = null === $viewport_overrides || $has_viewport_property_override( 'flexWrap' );
 702          $should_output_flex_orientation   = null === $viewport_overrides || $has_viewport_property_override( 'orientation' );
 703          $should_output_flex_justification = null === $viewport_overrides || $has_viewport_property_override( 'justifyContent' ) || $has_viewport_property_override( 'orientation' );
 704          $should_output_flex_alignment     = null === $viewport_overrides || $has_viewport_property_override( 'verticalAlignment' ) || $has_viewport_property_override( 'orientation' );
 705  
 706          if ( $should_output_flex_wrap && ! empty( $layout_for_styles['flexWrap'] ) && 'nowrap' === $layout_for_styles['flexWrap'] ) {
 707              $layout_styles[] = array(
 708                  'selector'     => $selector,
 709                  'declarations' => array( 'flex-wrap' => 'nowrap' ),
 710              );
 711          }
 712  
 713          if ( $has_block_gap_support && $should_output_block_gap && isset( $gap_value ) ) {
 714              $combined_gap_value = '';
 715              $gap_sides          = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
 716  
 717              foreach ( $gap_sides as $gap_side ) {
 718                  $process_value = $gap_value;
 719                  if ( is_array( $gap_value ) ) {
 720                      if ( is_array( $fallback_gap_value ) ) {
 721                          $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
 722                      } else {
 723                          $fallback_value = $fallback_gap_value;
 724                      }
 725                      $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
 726                  }
 727                  // Get spacing CSS variable from preset value if provided.
 728                  if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
 729                      $index_to_splice = strrpos( $process_value, '|' ) + 1;
 730                      $slug            = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
 731                      $process_value   = "var(--wp--preset--spacing--$slug)";
 732                  }
 733                  $combined_gap_value .= "$process_value ";
 734              }
 735              $gap_value = trim( $combined_gap_value );
 736  
 737              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 738                  $layout_styles[] = array(
 739                      'selector'     => $selector,
 740                      'declarations' => array( 'gap' => $gap_value ),
 741                  );
 742              }
 743          }
 744  
 745          if ( 'horizontal' === $layout_orientation ) {
 746              /*
 747               * Add this style only if is not empty for backwards compatibility,
 748               * since we intend to convert blocks that had flex layout implemented
 749               * by custom css.
 750               */
 751              if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
 752                  $layout_styles[] = array(
 753                      'selector'     => $selector,
 754                      'declarations' => array( 'justify-content' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
 755                  );
 756              }
 757  
 758              if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
 759                  $layout_styles[] = array(
 760                      'selector'     => $selector,
 761                      'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
 762                  );
 763              }
 764          } else {
 765              if ( $should_output_flex_orientation ) {
 766                  $layout_styles[] = array(
 767                      'selector'     => $selector,
 768                      'declarations' => array( 'flex-direction' => 'column' ),
 769                  );
 770              }
 771              if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
 772                  $layout_styles[] = array(
 773                      'selector'     => $selector,
 774                      'declarations' => array( 'align-items' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
 775                  );
 776              } elseif ( $should_output_flex_justification ) {
 777                  $layout_styles[] = array(
 778                      'selector'     => $selector,
 779                      'declarations' => array( 'align-items' => 'flex-start' ),
 780                  );
 781              }
 782              if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
 783                  $layout_styles[] = array(
 784                      'selector'     => $selector,
 785                      'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
 786                  );
 787              }
 788          }
 789      } elseif ( 'grid' === $layout_type ) {
 790          /*
 791           * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
 792           * is the relevant one for computation of responsive grid columns.
 793           */
 794          if ( is_array( $fallback_gap_value ) ) {
 795              $responsive_gap_value = $fallback_gap_value['left'] ?? reset( $fallback_gap_value );
 796          } else {
 797              $responsive_gap_value = $fallback_gap_value;
 798          }
 799  
 800          if ( $has_block_gap_support && isset( $gap_value ) ) {
 801              $combined_gap_value = '';
 802              $gap_sides          = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
 803  
 804              foreach ( $gap_sides as $gap_side ) {
 805                  $process_value = $gap_value;
 806                  if ( is_array( $gap_value ) ) {
 807                      if ( is_array( $fallback_gap_value ) ) {
 808                          $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
 809                      } else {
 810                          $fallback_value = $fallback_gap_value;
 811                      }
 812                      $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
 813                  }
 814                  // Get spacing CSS variable from preset value if provided.
 815                  if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
 816                      $index_to_splice = strrpos( $process_value, '|' ) + 1;
 817                      $slug            = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
 818                      $process_value   = "var(--wp--preset--spacing--$slug)";
 819                  }
 820                  $combined_gap_value .= "$process_value ";
 821              }
 822              $gap_value            = trim( $combined_gap_value );
 823              $responsive_gap_value = $gap_value;
 824          }
 825  
 826          // Ensure 0 values have a unit so they work in calc().
 827          if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) {
 828              $responsive_gap_value = '0px';
 829          }
 830  
 831          $should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' );
 832          $uses_gap_in_grid_columns   = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
 833          if ( $has_block_gap_override && $uses_gap_in_grid_columns ) {
 834              $should_output_grid_columns = true;
 835          }
 836  
 837          $should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] );
 838          $grid_declarations       = array();
 839  
 840          if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
 841              $max_value                                  = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')';
 842              $grid_declarations['grid-template-columns'] = 'repeat(auto-fill, minmax(' . $max_value . ', 1fr))';
 843          } elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) {
 844              $grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))';
 845          } elseif ( $should_output_grid_columns ) {
 846              $minimum_column_width                       = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem';
 847              $grid_declarations['grid-template-columns'] = 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))';
 848          }
 849  
 850          if ( ! empty( $grid_declarations ) ) {
 851              $base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) );
 852              if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
 853                  if ( null === $viewport_overrides || ! $base_has_container_type ) {
 854                      $grid_declarations['container-type'] = 'inline-size';
 855                  }
 856              }
 857              $layout_styles[] = array(
 858                  'selector'     => $selector,
 859                  'declarations' => $grid_declarations,
 860              );
 861          }
 862  
 863          if ( $should_output_grid_rows ) {
 864              $layout_styles[] = array(
 865                  'selector'     => $selector,
 866                  'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ),
 867              );
 868          }
 869  
 870          if ( $has_block_gap_support && $should_output_block_gap && null !== $gap_value && ! $should_skip_gap_serialization ) {
 871              $layout_styles[] = array(
 872                  'selector'     => $selector,
 873                  'declarations' => array( 'gap' => $gap_value ),
 874              );
 875          }
 876      }
 877  
 878      if ( ! empty( $layout_styles ) ) {
 879          if ( ! empty( $rules_group ) ) {
 880              foreach ( $layout_styles as $index => $layout_style ) {
 881                  $layout_styles[ $index ]['rules_group'] = $rules_group;
 882              }
 883          }
 884  
 885          /*
 886           * Add to the style engine store to enqueue and render layout styles.
 887           * Return compiled layout styles to retain backwards compatibility.
 888           * Since https://github.com/WordPress/gutenberg/pull/42452,
 889           * wp_enqueue_block_support_styles is no longer called in this block supports file.
 890           */
 891          return wp_style_engine_get_stylesheet_from_css_rules(
 892              $layout_styles,
 893              array(
 894                  'context'  => 'block-supports',
 895                  'prettify' => false,
 896              )
 897          );
 898      }
 899  
 900      return '';
 901  }
 902  
 903  /**
 904   * Renders the layout config to the block wrapper.
 905   *
 906   * @since 5.8.0
 907   * @since 6.3.0 Adds compound class to layout wrapper for global spacing styles.
 908   * @since 6.3.0 Check for layout support via the `layout` key with fallback to `__experimentalLayout`.
 909   * @since 6.6.0 Removed duplicate container class from layout styles.
 910   * @access private
 911   *
 912   * @param string $block_content Rendered block content.
 913   * @param array  $block         Block object.
 914   * @return string Filtered block content.
 915   */
 916  function wp_render_layout_support_flag( $block_content, $block ) {
 917      static $global_styles = null;
 918  
 919      $block_type            = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
 920      $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
 921      $style_attr            = $block['attrs']['style'] ?? array();
 922      $child_layout          = $style_attr['layout'] ?? null;
 923  
 924      /*
 925       * Collect responsive viewport child layout overrides so that a block with
 926       * only responsive child layout (no base child layout) is still processed.
 927       */
 928      $viewport_child_layouts = array();
 929      foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
 930          $viewport_child = wp_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null );
 931  
 932          if ( ! empty( $viewport_child ) ) {
 933              $viewport_child_layouts[ $breakpoint ] = array(
 934                  'media_query'  => $media_query,
 935                  'child_layout' => $viewport_child,
 936              );
 937          }
 938      }
 939  
 940      if ( ! $block_supports_layout && ! $child_layout && empty( $viewport_child_layouts ) ) {
 941          return $block_content;
 942      }
 943  
 944      $outer_class_names = array();
 945  
 946      // Child layout specific logic.
 947      if ( $child_layout || ! empty( $viewport_child_layouts ) ) {
 948          $base_child_layout = wp_get_layout_child_values( $child_layout );
 949          $parent_layout     = $block['parentLayout'] ?? array();
 950          /*
 951           * Generates a unique class for child block layout styles.
 952           *
 953           * To ensure consistent class generation across different page renders,
 954           * only properties that affect layout styling are used. These properties
 955           * come from `$block['attrs']['style']['layout']`, viewport overrides in
 956           * `$block['attrs']['style'][$breakpoint]['layout']`, and `$block['parentLayout']`.
 957           *
 958           * As long as these properties coincide, the generated class will be the same.
 959           */
 960          $container_content_hash_input = array(
 961              'layout'       => $base_child_layout,
 962              'parentLayout' => array_intersect_key(
 963                  $parent_layout,
 964                  array_flip( array( 'minimumColumnWidth', 'columnCount' ) )
 965              ),
 966          );
 967  
 968          foreach ( $viewport_child_layouts as $breakpoint => $viewport_data ) {
 969              $container_content_hash_input[ $breakpoint ] = $viewport_data['child_layout'];
 970          }
 971  
 972          $container_content_class = wp_unique_id_from_values(
 973              $container_content_hash_input,
 974              'wp-container-content-'
 975          );
 976  
 977          $child_layout_styles = wp_get_child_layout_style_rules( ".$container_content_class", $base_child_layout, $parent_layout );
 978  
 979          /*
 980           * Emit responsive child layout CSS using the same container-content class
 981           * so that base and responsive child layout share the exact same selector.
 982           */
 983          foreach ( $viewport_child_layouts as $viewport_data ) {
 984              $viewport_child_styles = wp_get_child_layout_style_rules(
 985                  ".$container_content_class",
 986                  $base_child_layout,
 987                  $parent_layout,
 988                  $viewport_data['child_layout']
 989              );
 990  
 991              foreach ( $viewport_child_styles as $index => $rule ) {
 992                  $viewport_child_styles[ $index ]['rules_group'] = $viewport_data['media_query'];
 993              }
 994  
 995              $child_layout_styles = array_merge( $child_layout_styles, $viewport_child_styles );
 996          }
 997  
 998          /*
 999           * Add to the style engine store to enqueue and render layout styles.
1000           * Return styles here just to check if any exist.
1001           */
1002          $child_css = wp_style_engine_get_stylesheet_from_css_rules(
1003              $child_layout_styles,
1004              array(
1005                  'context'  => 'block-supports',
1006                  'prettify' => false,
1007              )
1008          );
1009  
1010          if ( $child_css ) {
1011              $outer_class_names[] = $container_content_class;
1012          }
1013      }
1014  
1015      // Prep the processor for modifying the block output.
1016      $processor = new WP_HTML_Tag_Processor( $block_content );
1017  
1018      // Having no tags implies there are no tags onto which to add class names.
1019      if ( ! $processor->next_tag() ) {
1020          return $block_content;
1021      }
1022  
1023      /*
1024       * A block may not support layout but still be affected by a parent block's layout.
1025       *
1026       * In these cases add the appropriate class names and then return early; there's
1027       * no need to investigate on this block whether additional layout constraints apply.
1028       */
1029      if ( ! $block_supports_layout && ! empty( $outer_class_names ) ) {
1030          foreach ( $outer_class_names as $class_name ) {
1031              $processor->add_class( $class_name );
1032          }
1033          return $processor->get_updated_html();
1034      } elseif ( ! $block_supports_layout ) {
1035          // Ensure layout classnames are not injected if there is no layout support.
1036          return $block_content;
1037      }
1038  
1039      $global_settings = wp_get_global_settings();
1040      $fallback_layout = $block_type->supports['layout']['default'] ?? array();
1041      if ( empty( $fallback_layout ) ) {
1042          $fallback_layout = $block_type->supports['__experimentalLayout']['default'] ?? array();
1043      }
1044      $used_layout = $block['attrs']['layout'] ?? $fallback_layout;
1045  
1046      $class_names        = array();
1047      $layout_definitions = wp_get_layout_definitions();
1048  
1049      // Set the correct layout type for blocks using legacy content width.
1050      if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
1051          $used_layout['type'] = 'constrained';
1052      }
1053  
1054      $root_padding_aware_alignments = $global_settings['useRootPaddingAwareAlignments'] ?? false;
1055  
1056      if (
1057          $root_padding_aware_alignments &&
1058          isset( $used_layout['type'] ) &&
1059          'constrained' === $used_layout['type']
1060      ) {
1061          $class_names[] = 'has-global-padding';
1062      }
1063  
1064      /*
1065       * The following section was added to reintroduce a small set of layout classnames that were
1066       * removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is
1067       * not intended to provide an extended set of classes to match all block layout attributes
1068       * here.
1069       */
1070      if ( ! empty( $block['attrs']['layout']['orientation'] ) ) {
1071          $class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] );
1072      }
1073  
1074      if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
1075          $class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] );
1076      }
1077  
1078      if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) {
1079          $class_names[] = 'is-nowrap';
1080      }
1081  
1082      // Get classname for layout type.
1083      if ( isset( $used_layout['type'] ) ) {
1084          $layout_classname = $layout_definitions[ $used_layout['type'] ]['className'] ?? '';
1085      } else {
1086          $layout_classname = $layout_definitions['default']['className'] ?? '';
1087      }
1088  
1089      if ( $layout_classname && is_string( $layout_classname ) ) {
1090          $class_names[] = sanitize_title( $layout_classname );
1091      }
1092  
1093      /*
1094       * Only generate Layout styles if the theme has not opted-out.
1095       * Attribute-based Layout classnames are output in all cases.
1096       */
1097      if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
1098  
1099          $gap_value          = wp_sanitize_block_gap_value( $style_attr['spacing']['blockGap'] ?? null );
1100          $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
1101          $block_spacing      = $style_attr['spacing'] ?? null;
1102  
1103          /*
1104           * If a block's block.json skips serialization for spacing or spacing.blockGap,
1105           * don't apply the user-defined value to the styles.
1106           */
1107          $should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
1108  
1109          $block_gap             = $global_settings['spacing']['blockGap'] ?? null;
1110          $has_block_gap_support = isset( $block_gap );
1111  
1112          // Get default blockGap value from global styles for use in layouts like grid.
1113          // Check style variation first, then block-specific styles, then fall back to root styles.
1114          $block_name = $block['blockName'] ?? '';
1115          if ( null === $global_styles ) {
1116              $global_styles = wp_get_global_styles();
1117          }
1118  
1119          // Check if the block has an active style variation with a blockGap value.
1120          // Only check the registry if the className contains a variation class to avoid unnecessary lookups.
1121          $variation_block_gap_value = null;
1122          $block_class_name          = $block['attrs']['className'] ?? '';
1123          if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) {
1124              $styles_registry   = WP_Block_Styles_Registry::get_instance();
1125              $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name );
1126              $variation_name    = wp_get_block_style_variation_name_from_registered_style( $block_class_name, $registered_styles );
1127              if ( $variation_name ) {
1128                  $variation_block_gap_value = $global_styles['blocks'][ $block_name ]['variations'][ $variation_name ]['spacing']['blockGap'] ?? null;
1129              }
1130          }
1131  
1132          $global_block_gap_value = $variation_block_gap_value ?? $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null;
1133  
1134          if ( null !== $global_block_gap_value ) {
1135              $fallback_gap_value = $global_block_gap_value;
1136          }
1137  
1138          $container_class_hash_input = array(
1139              $used_layout,
1140              $has_block_gap_support,
1141              $gap_value,
1142              $should_skip_gap_serialization,
1143              $fallback_gap_value,
1144              $block_spacing,
1145          );
1146  
1147          foreach ( array_keys( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
1148              $viewport_style = $style_attr[ $breakpoint ] ?? null;
1149              if ( ! is_array( $viewport_style ) ) {
1150                  continue;
1151              }
1152  
1153              $viewport_container_layout = wp_get_layout_container_values( $viewport_style['layout'] ?? null );
1154              if ( ! empty( $viewport_container_layout ) ) {
1155                  $container_class_hash_input[] = array(
1156                      'breakpoint' => $breakpoint,
1157                      'layout'     => $viewport_container_layout,
1158                  );
1159              }
1160  
1161              if ( isset( $viewport_style['spacing']['blockGap'] ) ) {
1162                  $container_class_hash_input[] = array(
1163                      'breakpoint' => $breakpoint,
1164                      'blockGap'   => wp_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ),
1165                  );
1166              }
1167          }
1168  
1169          /*
1170           * Generates a unique ID based on all the data required to obtain the
1171           * corresponding layout style. Keeps the CSS class names the same
1172           * even for different blocks on different places, as long as they have
1173           * the same layout definition. Makes the CSS class names stable across
1174           * paginations for features like the enhanced pagination of the Query block.
1175           */
1176          $container_class = wp_unique_id_from_values(
1177              $container_class_hash_input,
1178              'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
1179          );
1180  
1181          $style = wp_get_layout_style(
1182              ".$container_class",
1183              $used_layout,
1184              $has_block_gap_support,
1185              $gap_value,
1186              $should_skip_gap_serialization,
1187              $fallback_gap_value,
1188              $block_spacing
1189          );
1190  
1191          /*
1192           * Emit responsive container layout styles using the same $container_class
1193           * selector as the base layout so they target the inner block wrapper.
1194           */
1195          foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
1196              $viewport_style = $style_attr[ $breakpoint ] ?? null;
1197              if ( ! is_array( $viewport_style ) ) {
1198                  continue;
1199              }
1200  
1201              $viewport_container_layout = wp_get_layout_container_values( $viewport_style['layout'] ?? null );
1202              $has_viewport_layout       = ! empty( $viewport_container_layout );
1203              $has_viewport_block_gap    = isset( $viewport_style['spacing']['blockGap'] );
1204  
1205              if ( ! $has_viewport_layout && ! $has_viewport_block_gap ) {
1206                  continue;
1207              }
1208  
1209              $viewport_gap_value = $has_viewport_block_gap
1210                  ? wp_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] )
1211                  : $gap_value;
1212  
1213              $viewport_block_spacing = is_array( $viewport_style['spacing'] ?? null )
1214                  ? array_replace( is_array( $block_spacing ) ? $block_spacing : array(), $viewport_style['spacing'] )
1215                  : $block_spacing;
1216  
1217              $viewport_styles = wp_get_layout_style(
1218                  ".$container_class",
1219                  $used_layout,
1220                  $has_block_gap_support,
1221                  $viewport_gap_value,
1222                  $should_skip_gap_serialization,
1223                  $fallback_gap_value,
1224                  $viewport_block_spacing,
1225                  array(
1226                      'rules_group'            => $media_query,
1227                      'viewport_overrides'     => $viewport_container_layout,
1228                      'has_block_gap_override' => $has_viewport_block_gap,
1229                  )
1230              );
1231  
1232              if ( ! empty( $viewport_styles ) && ! in_array( $container_class, $class_names, true ) ) {
1233                  $class_names[] = $container_class;
1234              }
1235          }
1236  
1237          // Only add container class and enqueue block support styles if unique styles were generated.
1238          if ( ! empty( $style ) ) {
1239              $class_names[] = $container_class;
1240          }
1241      }
1242  
1243      // Add combined layout and block classname for global styles to hook onto.
1244      $split_block_name = explode( '/', $block['blockName'] );
1245      $full_block_name  = 'core' === $split_block_name[0] ? end( $split_block_name ) : implode( '-', $split_block_name );
1246      $class_names[]    = 'wp-block-' . $full_block_name . '-' . $layout_classname;
1247  
1248      // Add classes to the outermost HTML tag if necessary.
1249      if ( ! empty( $outer_class_names ) ) {
1250          foreach ( $outer_class_names as $outer_class_name ) {
1251              $processor->add_class( $outer_class_name );
1252          }
1253      }
1254  
1255      /**
1256       * Attempts to refer to the inner-block wrapping element by its class attribute.
1257       *
1258       * When examining a block's inner content, if a block has inner blocks, then
1259       * the first content item will likely be a text (HTML) chunk immediately
1260       * preceding the inner blocks. The last HTML tag in that chunk would then be
1261       * an opening tag for an element that wraps the inner blocks.
1262       *
1263       * There's no reliable way to associate this wrapper in $block_content because
1264       * it may have changed during the rendering pipeline (as inner contents is
1265       * provided before rendering) and through previous filters. In many cases,
1266       * however, the `class` attribute will be a good-enough identifier, so this
1267       * code finds the last tag in that chunk and stores the `class` attribute
1268       * so that it can be used later when working through the rendered block output
1269       * to identify the wrapping element and add the remaining class names to it.
1270       *
1271       * It's also possible that no inner block wrapper even exists. If that's the
1272       * case this code could apply the class names to an invalid element.
1273       *
1274       * Example:
1275       *
1276       *     $block['innerBlocks']  = array( $list_item );
1277       *     $block['innerContent'] = array( '<ul class="list-wrapper is-unordered">', null, '</ul>' );
1278       *
1279       *     // After rendering, the initial contents may have been modified by other renderers or filters.
1280       *     $block_content = <<<HTML
1281       *         <figure>
1282       *             <ul class="annotated-list list-wrapper is-unordered">
1283       *                 <li>Code</li>
1284       *             </ul><figcaption>It's a list!</figcaption>
1285       *         </figure>
1286       *     HTML;
1287       *
1288       * Although it is possible that the original block-wrapper classes are changed in $block_content
1289       * from how they appear in $block['innerContent'], it's likely that the original class attributes
1290       * are still present in the wrapper as they are in this example. Frequently, additional classes
1291       * will also be present; rarely should classes be removed.
1292       *
1293       * @todo Find a better way to match the first inner block. If it's possible to identify where the
1294       *       first inner block starts, then it will be possible to find the last tag before it starts
1295       *       and then that tag, if an opening tag, can be solidly identified as a wrapping element.
1296       *       Can some unique value or class or ID be added to the inner blocks when they process
1297       *       so that they can be extracted here safely without guessing? Can the block rendering function
1298       *       return information about where the rendered inner blocks start?
1299       *
1300       * @var string|null
1301       */
1302      $inner_block_wrapper_classes = null;
1303      $first_chunk                 = $block['innerContent'][0] ?? null;
1304      if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
1305          $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
1306          /*
1307           * Use a stack to track open elements as tags are visited. Void elements
1308           * (those without a matching closing tag) are excluded so they don't
1309           * accumulate on the stack. At the end of the chunk, every element still
1310           * on the stack is unclosed — meaning its closing tag lives in a later
1311           * innerContent entry alongside the inner blocks, which makes it the
1312           * inner-block container. Elements that open and close within this chunk
1313           * are siblings that precede the inner blocks and should be ignored.
1314           * The last unclosed element with a class attribute is the best candidate
1315           * for the inner-block wrapper.
1316           */
1317          $tag_stack = array();
1318          while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
1319              if ( $first_chunk_processor->is_tag_closer() ) {
1320                  array_pop( $tag_stack );
1321              } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) {
1322                  $tag_stack[] = $first_chunk_processor->get_attribute( 'class' );
1323              }
1324          }
1325          foreach ( array_reverse( $tag_stack ) as $class_attribute ) {
1326              if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
1327                  $inner_block_wrapper_classes = $class_attribute;
1328                  break;
1329              }
1330          }
1331      }
1332  
1333      /*
1334       * If necessary, advance to what is likely to be an inner block wrapper tag.
1335       *
1336       * This advances until it finds the first tag containing the original class
1337       * attribute from above. If none is found it will scan to the end of the block
1338       * and fail to add any class names.
1339       *
1340       * If there is no block wrapper it won't advance at all, in which case the
1341       * class names will be added to the first and outermost tag of the block.
1342       * For cases where this outermost tag is the only tag surrounding inner
1343       * blocks then the outer wrapper and inner wrapper are the same.
1344       */
1345      do {
1346          if ( ! $inner_block_wrapper_classes ) {
1347              break;
1348          }
1349  
1350          $class_attribute = $processor->get_attribute( 'class' );
1351          if ( is_string( $class_attribute ) && str_contains( $class_attribute, $inner_block_wrapper_classes ) ) {
1352              break;
1353          }
1354      } while ( $processor->next_tag() );
1355  
1356      // Add the remaining class names.
1357      foreach ( $class_names as $class_name ) {
1358          $processor->add_class( $class_name );
1359      }
1360  
1361      return $processor->get_updated_html();
1362  }
1363  
1364  /**
1365   * Check if the parent block exists and if it has a layout attribute.
1366   * If it does, add the parent layout to the parsed block
1367   *
1368   * @since 6.6.0
1369   * @access private
1370   *
1371   * @param array    $parsed_block The parsed block.
1372   * @param array    $source_block The source block.
1373   * @param WP_Block $parent_block The parent block.
1374   * @return array The parsed block with parent layout attribute if it exists.
1375   */
1376  function wp_add_parent_layout_to_parsed_block( $parsed_block, $source_block, $parent_block ) {
1377      if ( $parent_block && isset( $parent_block->parsed_block['attrs']['layout'] ) ) {
1378          $parsed_block['parentLayout'] = $parent_block->parsed_block['attrs']['layout'];
1379      }
1380      return $parsed_block;
1381  }
1382  
1383  add_filter( 'render_block_data', 'wp_add_parent_layout_to_parsed_block', 10, 3 );
1384  
1385  // Register the block support.
1386  WP_Block_Supports::get_instance()->register(
1387      'layout',
1388      array(
1389          'register_attribute' => 'wp_register_layout_support',
1390      )
1391  );
1392  add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
1393  
1394  /**
1395   * For themes without theme.json file, make sure
1396   * to restore the inner div for the group block
1397   * to avoid breaking styles relying on that div.
1398   *
1399   * @since 5.8.0
1400   * @since 6.6.1 Removed inner container from Grid variations.
1401   * @access private
1402   *
1403   * @param string $block_content Rendered block content.
1404   * @param array  $block         Block object.
1405   * @return string Filtered block content.
1406   */
1407  function wp_restore_group_inner_container( $block_content, $block ) {
1408      $tag_name                         = $block['attrs']['tagName'] ?? 'div';
1409      $group_with_inner_container_regex = sprintf(
1410          '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
1411          preg_quote( $tag_name, '/' )
1412      );
1413  
1414      if (
1415          wp_theme_has_theme_json() ||
1416          1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
1417          ( isset( $block['attrs']['layout']['type'] ) && ( 'flex' === $block['attrs']['layout']['type'] || 'grid' === $block['attrs']['layout']['type'] ) )
1418      ) {
1419          return $block_content;
1420      }
1421  
1422      /*
1423       * This filter runs after the layout classnames have been added to the block, so they
1424       * have to be removed from the outer wrapper and then added to the inner.
1425       */
1426      $layout_classes = array();
1427      $processor      = new WP_HTML_Tag_Processor( $block_content );
1428  
1429      if ( $processor->next_tag( array( 'class_name' => 'wp-block-group' ) ) ) {
1430          foreach ( $processor->class_list() as $class_name ) {
1431              if ( str_contains( $class_name, 'is-layout-' ) ) {
1432                  $layout_classes[] = $class_name;
1433                  $processor->remove_class( $class_name );
1434              }
1435          }
1436      }
1437  
1438      $content_without_layout_classes = $processor->get_updated_html();
1439      $replace_regex                  = sprintf(
1440          '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
1441          preg_quote( $tag_name, '/' )
1442      );
1443      $updated_content                = preg_replace_callback(
1444          $replace_regex,
1445          static function ( $matches ) {
1446              return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
1447          },
1448          $content_without_layout_classes
1449      );
1450  
1451      // Add layout classes to inner wrapper.
1452      if ( ! empty( $layout_classes ) ) {
1453          $processor = new WP_HTML_Tag_Processor( $updated_content );
1454          if ( $processor->next_tag( array( 'class_name' => 'wp-block-group__inner-container' ) ) ) {
1455              foreach ( $layout_classes as $class_name ) {
1456                  $processor->add_class( $class_name );
1457              }
1458          }
1459          $updated_content = $processor->get_updated_html();
1460      }
1461      return $updated_content;
1462  }
1463  
1464  add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
1465  
1466  /**
1467   * For themes without theme.json file, make sure
1468   * to restore the outer div for the aligned image block
1469   * to avoid breaking styles relying on that div.
1470   *
1471   * @since 6.0.0
1472   * @access private
1473   *
1474   * @param string $block_content Rendered block content.
1475   * @param  array  $block        Block object.
1476   * @return string Filtered block content.
1477   */
1478  function wp_restore_image_outer_container( $block_content, $block ) {
1479      if ( wp_theme_has_theme_json() ) {
1480          return $block_content;
1481      }
1482  
1483      $figure_processor = new WP_HTML_Tag_Processor( $block_content );
1484      if (
1485          ! $figure_processor->next_tag( 'FIGURE' ) ||
1486          ! $figure_processor->has_class( 'wp-block-image' ) ||
1487          ! (
1488              $figure_processor->has_class( 'alignleft' ) ||
1489              $figure_processor->has_class( 'aligncenter' ) ||
1490              $figure_processor->has_class( 'alignright' )
1491          )
1492      ) {
1493          return $block_content;
1494      }
1495  
1496      /*
1497       * The next section of code wraps the existing figure in a new DIV element.
1498       * While doing it, it needs to transfer the layout and the additional CSS
1499       * class names from the original figure upward to the wrapper.
1500       *
1501       * Example:
1502       *
1503       *     // From this…
1504       *     <!-- wp:image {"className":"hires"} -->
1505       *     <figure class="wp-block-image wide hires">…
1506       *
1507       *     // To this…
1508       *     <div class="wp-block-image hires"><figure class="wide">…
1509       */
1510      $wrapper_processor = new WP_HTML_Tag_Processor( '<div>' );
1511      $wrapper_processor->next_token();
1512      $wrapper_processor->set_attribute(
1513          'class',
1514          is_string( $block['attrs']['className'] ?? null )
1515              ? "wp-block-image {$block['attrs']['className']}"
1516              : 'wp-block-image'
1517      );
1518  
1519      // And remove them from the existing content; it has been transferred upward.
1520      $figure_processor->remove_class( 'wp-block-image' );
1521      foreach ( $wrapper_processor->class_list() as $class_name ) {
1522          $figure_processor->remove_class( $class_name );
1523      }
1524  
1525      return "{$wrapper_processor->get_updated_html()}{$figure_processor->get_updated_html()}</div>";
1526  }
1527  
1528  add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );


Generated : Thu Jun 25 08:20:12 2026 Cross-referenced by PHPXref