[ 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  
 508      /*
 509       * Viewport styles only store changed fields. If a field is present with null,
 510       * the user cleared a value inherited from the default viewport, so check
 511       * whether the key exists rather than whether the value is truthy.
 512       */
 513      $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) {
 514          return array_key_exists( $property, $viewport_overrides );
 515      };
 516      $layout_styles                  = array();
 517  
 518      if ( 'default' === $layout_type ) {
 519          if ( $has_block_gap_support && $should_output_block_gap ) {
 520              if ( is_array( $gap_value ) ) {
 521                  $gap_value = $gap_value['top'] ?? null;
 522              }
 523              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 524                  // Get spacing CSS variable from preset value if provided.
 525                  if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
 526                      $index_to_splice = strrpos( $gap_value, '|' ) + 1;
 527                      $slug            = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
 528                      $gap_value       = "var(--wp--preset--spacing--$slug)";
 529                  }
 530  
 531                  array_push(
 532                      $layout_styles,
 533                      array(
 534                          'selector'     => "$selector > *",
 535                          'declarations' => array(
 536                              'margin-block-start' => '0',
 537                              'margin-block-end'   => '0',
 538                          ),
 539                      ),
 540                      array(
 541                          'selector'     => "$selector > * + *",
 542                          'declarations' => array(
 543                              'margin-block-start' => $gap_value,
 544                              'margin-block-end'   => '0',
 545                          ),
 546                      )
 547                  );
 548              }
 549          }
 550      } elseif ( 'constrained' === $layout_type ) {
 551          $content_size    = $layout_for_styles['contentSize'] ?? '';
 552          $wide_size       = $layout_for_styles['wideSize'] ?? '';
 553          $justify_content = $layout_for_styles['justifyContent'] ?? 'center';
 554  
 555          // Check if viewport-specific ("override") values exist. Null values are valid and mean the user cleared a value inherited from the default viewport.
 556          $has_justify_content_override = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' );
 557          $has_content_size_override    = null !== $viewport_overrides && $has_viewport_property_override( 'contentSize' );
 558          $has_wide_size_override       = null !== $viewport_overrides && $has_viewport_property_override( 'wideSize' );
 559  
 560          /*
 561           * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
 562           * value for contentSize or wideSize. If a viewport clears a custom constrained size, reset to the global layout variable.
 563           */
 564          $should_output_constrained_sizes = null === $viewport_overrides || $has_content_size_override || $has_wide_size_override;
 565          $is_resetting_constrained_sizes  = null !== $viewport_overrides &&
 566              (
 567                  ( $has_content_size_override && ! $content_size ) ||
 568                  ( $has_wide_size_override && ! $wide_size )
 569              );
 570  
 571          // If a viewport clears a custom constrained size, reset to the global layout variable.
 572          $all_max_width_value  = $content_size
 573              ? $content_size
 574              : ( $wide_size && ! $has_content_size_override ? $wide_size : 'var(--wp--style--global--content-size, none)' );
 575          $wide_max_width_value = $wide_size
 576              ? $wide_size
 577              : ( $content_size && ! $has_wide_size_override ? $content_size : 'var(--wp--style--global--wide-size, none)' );
 578  
 579          // Make sure there is a single CSS rule, and all tags are stripped for security.
 580          $all_max_width_value  = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
 581          $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
 582  
 583          $margin_left  = 'left' === $justify_content ? '0 !important' : 'auto !important';
 584          $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
 585  
 586          if ( $should_output_constrained_sizes && ( $content_size || $wide_size || $is_resetting_constrained_sizes ) ) {
 587              $content_size_declarations = array(
 588                  'max-width' => $all_max_width_value,
 589              );
 590  
 591              if ( null === $viewport_overrides || $has_justify_content_override ) {
 592                  $content_size_declarations['margin-left']  = $margin_left;
 593                  $content_size_declarations['margin-right'] = $margin_right;
 594              }
 595  
 596              array_push(
 597                  $layout_styles,
 598                  array(
 599                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 600                      'declarations' => $content_size_declarations,
 601                  ),
 602                  array(
 603                      'selector'     => "$selector > .alignwide",
 604                      'declarations' => array( 'max-width' => $wide_max_width_value ),
 605                  ),
 606                  array(
 607                      'selector'     => "$selector .alignfull",
 608                      'declarations' => array( 'max-width' => 'none' ),
 609                  )
 610              );
 611          }
 612  
 613          if ( null === $viewport_overrides && isset( $block_spacing ) ) {
 614              $block_spacing_values = wp_style_engine_get_styles(
 615                  array(
 616                      'spacing' => $block_spacing,
 617                  )
 618              );
 619  
 620              /*
 621               * Handle negative margins for alignfull children of blocks with custom padding set.
 622               * They're added separately because padding might only be set on one side.
 623               */
 624              if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
 625                  $padding_right = $block_spacing_values['declarations']['padding-right'];
 626                  // Add unit if 0.
 627                  if ( '0' === $padding_right ) {
 628                      $padding_right = '0px';
 629                  }
 630                  $layout_styles[] = array(
 631                      'selector'     => "$selector > .alignfull",
 632                      'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
 633                  );
 634              }
 635              if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
 636                  $padding_left = $block_spacing_values['declarations']['padding-left'];
 637                  // Add unit if 0.
 638                  if ( '0' === $padding_left ) {
 639                      $padding_left = '0px';
 640                  }
 641                  $layout_styles[] = array(
 642                      'selector'     => "$selector > .alignfull",
 643                      'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
 644                  );
 645              }
 646          }
 647  
 648          if ( $has_justify_content_override && ! $should_output_constrained_sizes ) {
 649              $layout_styles[] = array(
 650                  'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 651                  'declarations' => array(
 652                      'margin-left'  => $margin_left,
 653                      'margin-right' => $margin_right,
 654                  ),
 655              );
 656          } elseif ( null === $viewport_overrides ) {
 657              if ( 'left' === $justify_content ) {
 658                  $layout_styles[] = array(
 659                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 660                      'declarations' => array( 'margin-left' => '0 !important' ),
 661                  );
 662              }
 663  
 664              if ( 'right' === $justify_content ) {
 665                  $layout_styles[] = array(
 666                      'selector'     => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
 667                      'declarations' => array( 'margin-right' => '0 !important' ),
 668                  );
 669              }
 670          }
 671  
 672          if ( $has_block_gap_support && $should_output_block_gap ) {
 673              if ( is_array( $gap_value ) ) {
 674                  $gap_value = $gap_value['top'] ?? null;
 675              }
 676              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 677                  // Get spacing CSS variable from preset value if provided.
 678                  if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
 679                      $index_to_splice = strrpos( $gap_value, '|' ) + 1;
 680                      $slug            = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
 681                      $gap_value       = "var(--wp--preset--spacing--$slug)";
 682                  }
 683  
 684                  array_push(
 685                      $layout_styles,
 686                      array(
 687                          'selector'     => "$selector > *",
 688                          'declarations' => array(
 689                              'margin-block-start' => '0',
 690                              'margin-block-end'   => '0',
 691                          ),
 692                      ),
 693                      array(
 694                          'selector'     => "$selector > * + *",
 695                          'declarations' => array(
 696                              'margin-block-start' => $gap_value,
 697                              'margin-block-end'   => '0',
 698                          ),
 699                      )
 700                  );
 701              }
 702          }
 703      } elseif ( 'flex' === $layout_type ) {
 704          $layout_orientation = $layout_for_styles['orientation'] ?? 'horizontal';
 705  
 706          $justify_content_options = array(
 707              'left'   => 'flex-start',
 708              'right'  => 'flex-end',
 709              'center' => 'center',
 710          );
 711  
 712          $vertical_alignment_options = array(
 713              'top'    => 'flex-start',
 714              'center' => 'center',
 715              'bottom' => 'flex-end',
 716          );
 717  
 718          if ( 'horizontal' === $layout_orientation ) {
 719              $justify_content_options    += array( 'space-between' => 'space-between' );
 720              $vertical_alignment_options += array( 'stretch' => 'stretch' );
 721          } else {
 722              $justify_content_options    += array( 'stretch' => 'stretch' );
 723              $vertical_alignment_options += array( 'space-between' => 'space-between' );
 724          }
 725  
 726          /*
 727           * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
 728           * value for any of the flex properties.
 729           */
 730          $should_output_flex_wrap          = null === $viewport_overrides || $has_viewport_property_override( 'flexWrap' );
 731          $should_output_flex_orientation   = null === $viewport_overrides || $has_viewport_property_override( 'orientation' );
 732          $should_output_flex_justification = null === $viewport_overrides || $has_viewport_property_override( 'justifyContent' ) || $has_viewport_property_override( 'orientation' );
 733          $should_output_flex_alignment     = null === $viewport_overrides || $has_viewport_property_override( 'verticalAlignment' ) || $has_viewport_property_override( 'orientation' );
 734  
 735          if ( $should_output_flex_wrap && ! empty( $layout_for_styles['flexWrap'] ) && 'nowrap' === $layout_for_styles['flexWrap'] ) {
 736              $layout_styles[] = array(
 737                  'selector'     => $selector,
 738                  'declarations' => array( 'flex-wrap' => 'nowrap' ),
 739              );
 740          }
 741  
 742          if ( $has_block_gap_support && $should_output_block_gap && isset( $gap_value ) ) {
 743              $combined_gap_value = '';
 744              $gap_sides          = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
 745  
 746              foreach ( $gap_sides as $gap_side ) {
 747                  $process_value = $gap_value;
 748                  if ( is_array( $gap_value ) ) {
 749                      if ( is_array( $fallback_gap_value ) ) {
 750                          $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
 751                      } else {
 752                          $fallback_value = $fallback_gap_value;
 753                      }
 754                      $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
 755                  }
 756                  // Get spacing CSS variable from preset value if provided.
 757                  if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
 758                      $index_to_splice = strrpos( $process_value, '|' ) + 1;
 759                      $slug            = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
 760                      $process_value   = "var(--wp--preset--spacing--$slug)";
 761                  }
 762                  $combined_gap_value .= "$process_value ";
 763              }
 764              $gap_value = trim( $combined_gap_value );
 765  
 766              if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
 767                  $layout_styles[] = array(
 768                      'selector'     => $selector,
 769                      'declarations' => array( 'gap' => $gap_value ),
 770                  );
 771              }
 772          }
 773  
 774          if ( 'horizontal' === $layout_orientation ) {
 775              /*
 776               * Add this style only if is not empty for backwards compatibility,
 777               * since we intend to convert blocks that had flex layout implemented
 778               * by custom css.
 779               */
 780              if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
 781                  $layout_styles[] = array(
 782                      'selector'     => $selector,
 783                      'declarations' => array( 'justify-content' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
 784                  );
 785              }
 786  
 787              if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
 788                  $layout_styles[] = array(
 789                      'selector'     => $selector,
 790                      'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
 791                  );
 792              }
 793          } else {
 794              if ( $should_output_flex_orientation ) {
 795                  $layout_styles[] = array(
 796                      'selector'     => $selector,
 797                      'declarations' => array( 'flex-direction' => 'column' ),
 798                  );
 799              }
 800              if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) {
 801                  $layout_styles[] = array(
 802                      'selector'     => $selector,
 803                      'declarations' => array( 'align-items' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ),
 804                  );
 805              } elseif ( $should_output_flex_justification ) {
 806                  $layout_styles[] = array(
 807                      'selector'     => $selector,
 808                      'declarations' => array( 'align-items' => 'flex-start' ),
 809                  );
 810              }
 811              if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) {
 812                  $layout_styles[] = array(
 813                      'selector'     => $selector,
 814                      'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ),
 815                  );
 816              }
 817          }
 818      } elseif ( 'grid' === $layout_type ) {
 819          /*
 820           * If the gap value is an array, we use the "left" value because it represents the vertical gap, which
 821           * is the relevant one for computation of responsive grid columns.
 822           */
 823          if ( is_array( $fallback_gap_value ) ) {
 824              $responsive_gap_value = $fallback_gap_value['left'] ?? reset( $fallback_gap_value );
 825          } else {
 826              $responsive_gap_value = $fallback_gap_value;
 827          }
 828  
 829          if ( $has_block_gap_support && isset( $gap_value ) ) {
 830              $combined_gap_value = '';
 831              $gap_sides          = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
 832  
 833              foreach ( $gap_sides as $gap_side ) {
 834                  $process_value = $gap_value;
 835                  if ( is_array( $gap_value ) ) {
 836                      if ( is_array( $fallback_gap_value ) ) {
 837                          $fallback_value = $fallback_gap_value[ $gap_side ] ?? reset( $fallback_gap_value );
 838                      } else {
 839                          $fallback_value = $fallback_gap_value;
 840                      }
 841                      $process_value = $gap_value[ $gap_side ] ?? $fallback_value;
 842                  }
 843                  // Get spacing CSS variable from preset value if provided.
 844                  if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
 845                      $index_to_splice = strrpos( $process_value, '|' ) + 1;
 846                      $slug            = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
 847                      $process_value   = "var(--wp--preset--spacing--$slug)";
 848                  }
 849                  $combined_gap_value .= "$process_value ";
 850              }
 851              $gap_value            = trim( $combined_gap_value );
 852              $responsive_gap_value = $gap_value;
 853          }
 854  
 855          // Ensure 0 values have a unit so they work in calc().
 856          if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) {
 857              $responsive_gap_value = '0px';
 858          }
 859  
 860          /*
 861           * Styles should be output either if there are no viewport overrides (this is the default case), or if the user has set a new viewport-specific
 862           * value for any of the grid properties.
 863           */
 864          $should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' ) || $has_viewport_property_override( 'autoFit' );
 865          $uses_gap_in_grid_columns   = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] );
 866          if ( $has_block_gap_override && $uses_gap_in_grid_columns ) {
 867              $should_output_grid_columns = true;
 868          }
 869  
 870          $should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] );
 871          $grid_declarations       = array();
 872  
 873          /*
 874           * When enabled, columns stretch to fill the available space using
 875           * `auto-fit`; otherwise empty tracks are preserved with `auto-fill`.
 876           */
 877          $auto_placement = ! empty( $layout_for_styles['autoFit'] ) ? 'auto-fit' : 'auto-fill';
 878  
 879          if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
 880              $max_value                                  = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')';
 881              $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(' . $max_value . ', 1fr))';
 882          } elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) {
 883              $grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))';
 884          } elseif ( $should_output_grid_columns ) {
 885              $minimum_column_width                       = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem';
 886              $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(min(' . $minimum_column_width . ', 100%), 1fr))';
 887          }
 888  
 889          if ( ! empty( $grid_declarations ) ) {
 890              $base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) );
 891              if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) {
 892                  if ( null === $viewport_overrides || ! $base_has_container_type ) {
 893                      $grid_declarations['container-type'] = 'inline-size';
 894                  }
 895              }
 896              $layout_styles[] = array(
 897                  'selector'     => $selector,
 898                  'declarations' => $grid_declarations,
 899              );
 900          }
 901  
 902          if ( $should_output_grid_rows ) {
 903              $layout_styles[] = array(
 904                  'selector'     => $selector,
 905                  'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ),
 906              );
 907          }
 908  
 909          if ( $has_block_gap_support && $should_output_block_gap && null !== $gap_value && ! $should_skip_gap_serialization ) {
 910              $layout_styles[] = array(
 911                  'selector'     => $selector,
 912                  'declarations' => array( 'gap' => $gap_value ),
 913              );
 914          }
 915      }
 916  
 917      if ( ! empty( $layout_styles ) ) {
 918          if ( ! empty( $rules_group ) ) {
 919              foreach ( $layout_styles as $index => $layout_style ) {
 920                  $layout_styles[ $index ]['rules_group'] = $rules_group;
 921              }
 922          }
 923  
 924          /*
 925           * Add to the style engine store to enqueue and render layout styles.
 926           * Return compiled layout styles to retain backwards compatibility.
 927           * Since https://github.com/WordPress/gutenberg/pull/42452,
 928           * wp_enqueue_block_support_styles is no longer called in this block supports file.
 929           */
 930          return wp_style_engine_get_stylesheet_from_css_rules(
 931              $layout_styles,
 932              array(
 933                  'context'  => 'block-supports',
 934                  'prettify' => false,
 935              )
 936          );
 937      }
 938  
 939      return '';
 940  }
 941  
 942  /**
 943   * Renders the layout config to the block wrapper.
 944   *
 945   * @since 5.8.0
 946   * @since 6.3.0 Adds compound class to layout wrapper for global spacing styles.
 947   * @since 6.3.0 Check for layout support via the `layout` key with fallback to `__experimentalLayout`.
 948   * @since 6.6.0 Removed duplicate container class from layout styles.
 949   * @access private
 950   *
 951   * @param string $block_content Rendered block content.
 952   * @param array  $block         Block object.
 953   * @return string Filtered block content.
 954   */
 955  function wp_render_layout_support_flag( $block_content, $block ) {
 956      static $global_styles = null;
 957  
 958      $block_type            = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
 959      $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
 960      $style_attr            = $block['attrs']['style'] ?? array();
 961      $child_layout          = $style_attr['layout'] ?? null;
 962  
 963      /*
 964       * Collect responsive viewport child layout overrides so that a block with
 965       * only responsive child layout (no base child layout) is still processed.
 966       */
 967      $viewport_child_layouts = array();
 968      foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
 969          $viewport_child = wp_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null );
 970  
 971          if ( ! empty( $viewport_child ) ) {
 972              $viewport_child_layouts[ $breakpoint ] = array(
 973                  'media_query'  => $media_query,
 974                  'child_layout' => $viewport_child,
 975              );
 976          }
 977      }
 978  
 979      if ( ! $block_supports_layout && ! $child_layout && empty( $viewport_child_layouts ) ) {
 980          return $block_content;
 981      }
 982  
 983      $outer_class_names = array();
 984  
 985      // Child layout specific logic.
 986      if ( $child_layout || ! empty( $viewport_child_layouts ) ) {
 987          $base_child_layout = wp_get_layout_child_values( $child_layout );
 988          $parent_layout     = $block['parentLayout'] ?? array();
 989          /*
 990           * Generates a unique class for child block layout styles.
 991           *
 992           * To ensure consistent class generation across different page renders,
 993           * only properties that affect layout styling are used. These properties
 994           * come from `$block['attrs']['style']['layout']`, viewport overrides in
 995           * `$block['attrs']['style'][$breakpoint]['layout']`, and `$block['parentLayout']`.
 996           *
 997           * As long as these properties coincide, the generated class will be the same.
 998           */
 999          $container_content_hash_input = array(
1000              'layout'       => $base_child_layout,
1001              'parentLayout' => array_intersect_key(
1002                  $parent_layout,
1003                  array_flip( array( 'minimumColumnWidth', 'columnCount' ) )
1004              ),
1005          );
1006  
1007          foreach ( $viewport_child_layouts as $breakpoint => $viewport_data ) {
1008              $container_content_hash_input[ $breakpoint ] = $viewport_data['child_layout'];
1009          }
1010  
1011          $container_content_class = wp_unique_id_from_values(
1012              $container_content_hash_input,
1013              'wp-container-content-'
1014          );
1015  
1016          $child_layout_styles = wp_get_child_layout_style_rules( ".$container_content_class", $base_child_layout, $parent_layout );
1017  
1018          /*
1019           * Emit responsive child layout CSS using the same container-content class
1020           * so that base and responsive child layout share the exact same selector.
1021           */
1022          foreach ( $viewport_child_layouts as $viewport_data ) {
1023              $viewport_child_styles = wp_get_child_layout_style_rules(
1024                  ".$container_content_class",
1025                  $base_child_layout,
1026                  $parent_layout,
1027                  $viewport_data['child_layout']
1028              );
1029  
1030              foreach ( $viewport_child_styles as $index => $rule ) {
1031                  $viewport_child_styles[ $index ]['rules_group'] = $viewport_data['media_query'];
1032              }
1033  
1034              $child_layout_styles = array_merge( $child_layout_styles, $viewport_child_styles );
1035          }
1036  
1037          /*
1038           * Add to the style engine store to enqueue and render layout styles.
1039           * Return styles here just to check if any exist.
1040           */
1041          $child_css = wp_style_engine_get_stylesheet_from_css_rules(
1042              $child_layout_styles,
1043              array(
1044                  'context'  => 'block-supports',
1045                  'prettify' => false,
1046              )
1047          );
1048  
1049          if ( $child_css ) {
1050              $outer_class_names[] = $container_content_class;
1051          }
1052      }
1053  
1054      // Prep the processor for modifying the block output.
1055      $processor = new WP_HTML_Tag_Processor( $block_content );
1056  
1057      // Having no tags implies there are no tags onto which to add class names.
1058      if ( ! $processor->next_tag() ) {
1059          return $block_content;
1060      }
1061  
1062      /*
1063       * A block may not support layout but still be affected by a parent block's layout.
1064       *
1065       * In these cases add the appropriate class names and then return early; there's
1066       * no need to investigate on this block whether additional layout constraints apply.
1067       */
1068      if ( ! $block_supports_layout && ! empty( $outer_class_names ) ) {
1069          foreach ( $outer_class_names as $class_name ) {
1070              $processor->add_class( $class_name );
1071          }
1072          return $processor->get_updated_html();
1073      } elseif ( ! $block_supports_layout ) {
1074          // Ensure layout classnames are not injected if there is no layout support.
1075          return $block_content;
1076      }
1077  
1078      $global_settings = wp_get_global_settings();
1079      $fallback_layout = $block_type->supports['layout']['default'] ?? array();
1080      if ( empty( $fallback_layout ) ) {
1081          $fallback_layout = $block_type->supports['__experimentalLayout']['default'] ?? array();
1082      }
1083      $used_layout = $block['attrs']['layout'] ?? $fallback_layout;
1084  
1085      $class_names        = array();
1086      $layout_definitions = wp_get_layout_definitions();
1087  
1088      // Set the correct layout type for blocks using legacy content width.
1089      if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
1090          $used_layout['type'] = 'constrained';
1091      }
1092  
1093      $root_padding_aware_alignments = $global_settings['useRootPaddingAwareAlignments'] ?? false;
1094  
1095      if (
1096          $root_padding_aware_alignments &&
1097          isset( $used_layout['type'] ) &&
1098          'constrained' === $used_layout['type']
1099      ) {
1100          $class_names[] = 'has-global-padding';
1101      }
1102  
1103      /*
1104       * The following section was added to reintroduce a small set of layout classnames that were
1105       * removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is
1106       * not intended to provide an extended set of classes to match all block layout attributes
1107       * here.
1108       */
1109      if ( ! empty( $block['attrs']['layout']['orientation'] ) ) {
1110          $class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] );
1111      }
1112  
1113      if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
1114          $class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] );
1115      }
1116  
1117      if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) {
1118          $class_names[] = 'is-nowrap';
1119      }
1120  
1121      // Get classname for layout type.
1122      if ( isset( $used_layout['type'] ) ) {
1123          $layout_classname = $layout_definitions[ $used_layout['type'] ]['className'] ?? '';
1124      } else {
1125          $layout_classname = $layout_definitions['default']['className'] ?? '';
1126      }
1127  
1128      if ( $layout_classname && is_string( $layout_classname ) ) {
1129          $class_names[] = sanitize_title( $layout_classname );
1130      }
1131  
1132      /*
1133       * Only generate Layout styles if the theme has not opted-out.
1134       * Attribute-based Layout classnames are output in all cases.
1135       */
1136      if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
1137  
1138          $gap_value          = wp_sanitize_block_gap_value( $style_attr['spacing']['blockGap'] ?? null );
1139          $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em';
1140          $block_spacing      = $style_attr['spacing'] ?? null;
1141  
1142          /*
1143           * If a block's block.json skips serialization for spacing or spacing.blockGap,
1144           * don't apply the user-defined value to the styles.
1145           */
1146          $should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
1147  
1148          $block_gap             = $global_settings['spacing']['blockGap'] ?? null;
1149          $has_block_gap_support = isset( $block_gap );
1150  
1151          // Get default blockGap value from global styles for use in layouts like grid.
1152          // Check style variation first, then block-specific styles, then fall back to root styles.
1153          $block_name = $block['blockName'] ?? '';
1154          if ( null === $global_styles ) {
1155              $global_styles = wp_get_global_styles();
1156          }
1157  
1158          // Check if the block has an active style variation with a blockGap value.
1159          // Only check the registry if the className contains a variation class to avoid unnecessary lookups.
1160          $variation_block_gap_value = null;
1161          $block_class_name          = $block['attrs']['className'] ?? '';
1162          if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) {
1163              $styles_registry   = WP_Block_Styles_Registry::get_instance();
1164              $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name );
1165              $variation_name    = wp_get_block_style_variation_name_from_registered_style( $block_class_name, $registered_styles );
1166              if ( $variation_name ) {
1167                  $variation_block_gap_value = $global_styles['blocks'][ $block_name ]['variations'][ $variation_name ]['spacing']['blockGap'] ?? null;
1168              }
1169          }
1170  
1171          $global_block_gap_value = $variation_block_gap_value ?? $global_styles['blocks'][ $block_name ]['spacing']['blockGap'] ?? $global_styles['spacing']['blockGap'] ?? null;
1172  
1173          if ( null !== $global_block_gap_value ) {
1174              $fallback_gap_value = $global_block_gap_value;
1175          }
1176  
1177          $container_class_hash_input = array(
1178              $used_layout,
1179              $has_block_gap_support,
1180              $gap_value,
1181              $should_skip_gap_serialization,
1182              $fallback_gap_value,
1183              $block_spacing,
1184          );
1185  
1186          foreach ( array_keys( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS ) as $breakpoint ) {
1187              $viewport_style = $style_attr[ $breakpoint ] ?? null;
1188              if ( ! is_array( $viewport_style ) ) {
1189                  continue;
1190              }
1191  
1192              $viewport_container_layout = wp_get_layout_container_values( $viewport_style['layout'] ?? null );
1193              if ( ! empty( $viewport_container_layout ) ) {
1194                  $container_class_hash_input[] = array(
1195                      'breakpoint' => $breakpoint,
1196                      'layout'     => $viewport_container_layout,
1197                  );
1198              }
1199  
1200              if ( isset( $viewport_style['spacing']['blockGap'] ) ) {
1201                  $container_class_hash_input[] = array(
1202                      'breakpoint' => $breakpoint,
1203                      'blockGap'   => wp_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ),
1204                  );
1205              }
1206          }
1207  
1208          /*
1209           * Generates a unique ID based on all the data required to obtain the
1210           * corresponding layout style. Keeps the CSS class names the same
1211           * even for different blocks on different places, as long as they have
1212           * the same layout definition. Makes the CSS class names stable across
1213           * paginations for features like the enhanced pagination of the Query block.
1214           */
1215          $container_class = wp_unique_id_from_values(
1216              $container_class_hash_input,
1217              'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
1218          );
1219  
1220          $style = wp_get_layout_style(
1221              ".$container_class",
1222              $used_layout,
1223              $has_block_gap_support,
1224              $gap_value,
1225              $should_skip_gap_serialization,
1226              $fallback_gap_value,
1227              $block_spacing
1228          );
1229  
1230          /*
1231           * Emit responsive container layout styles using the same $container_class
1232           * selector as the base layout so they target the inner block wrapper.
1233           */
1234          foreach ( WP_Theme_JSON::RESPONSIVE_BREAKPOINTS as $breakpoint => $media_query ) {
1235              $viewport_style = $style_attr[ $breakpoint ] ?? null;
1236              if ( ! is_array( $viewport_style ) ) {
1237                  continue;
1238              }
1239  
1240              $viewport_container_layout = wp_get_layout_container_values( $viewport_style['layout'] ?? null );
1241              $has_viewport_layout       = ! empty( $viewport_container_layout );
1242              $has_viewport_block_gap    = isset( $viewport_style['spacing']['blockGap'] );
1243  
1244              if ( ! $has_viewport_layout && ! $has_viewport_block_gap ) {
1245                  continue;
1246              }
1247  
1248              $viewport_gap_value = $has_viewport_block_gap
1249                  ? wp_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] )
1250                  : $gap_value;
1251  
1252              $viewport_block_spacing = is_array( $viewport_style['spacing'] ?? null )
1253                  ? array_replace( is_array( $block_spacing ) ? $block_spacing : array(), $viewport_style['spacing'] )
1254                  : $block_spacing;
1255  
1256              $viewport_styles = wp_get_layout_style(
1257                  ".$container_class",
1258                  $used_layout,
1259                  $has_block_gap_support,
1260                  $viewport_gap_value,
1261                  $should_skip_gap_serialization,
1262                  $fallback_gap_value,
1263                  $viewport_block_spacing,
1264                  array(
1265                      'rules_group'            => $media_query,
1266                      'viewport_overrides'     => $viewport_container_layout,
1267                      'has_block_gap_override' => $has_viewport_block_gap,
1268                  )
1269              );
1270  
1271              if ( ! empty( $viewport_styles ) && ! in_array( $container_class, $class_names, true ) ) {
1272                  $class_names[] = $container_class;
1273              }
1274          }
1275  
1276          // Only add container class and enqueue block support styles if unique styles were generated.
1277          if ( ! empty( $style ) ) {
1278              $class_names[] = $container_class;
1279          }
1280      }
1281  
1282      // Add combined layout and block classname for global styles to hook onto.
1283      $split_block_name = explode( '/', $block['blockName'] );
1284      $full_block_name  = 'core' === $split_block_name[0] ? end( $split_block_name ) : implode( '-', $split_block_name );
1285      $class_names[]    = 'wp-block-' . $full_block_name . '-' . $layout_classname;
1286  
1287      // Add classes to the outermost HTML tag if necessary.
1288      if ( ! empty( $outer_class_names ) ) {
1289          foreach ( $outer_class_names as $outer_class_name ) {
1290              $processor->add_class( $outer_class_name );
1291          }
1292      }
1293  
1294      /**
1295       * Attempts to refer to the inner-block wrapping element by its class attribute.
1296       *
1297       * When examining a block's inner content, if a block has inner blocks, then
1298       * the first content item will likely be a text (HTML) chunk immediately
1299       * preceding the inner blocks. The last HTML tag in that chunk would then be
1300       * an opening tag for an element that wraps the inner blocks.
1301       *
1302       * There's no reliable way to associate this wrapper in $block_content because
1303       * it may have changed during the rendering pipeline (as inner contents is
1304       * provided before rendering) and through previous filters. In many cases,
1305       * however, the `class` attribute will be a good-enough identifier, so this
1306       * code finds the last tag in that chunk and stores the `class` attribute
1307       * so that it can be used later when working through the rendered block output
1308       * to identify the wrapping element and add the remaining class names to it.
1309       *
1310       * It's also possible that no inner block wrapper even exists. If that's the
1311       * case this code could apply the class names to an invalid element.
1312       *
1313       * Example:
1314       *
1315       *     $block['innerBlocks']  = array( $list_item );
1316       *     $block['innerContent'] = array( '<ul class="list-wrapper is-unordered">', null, '</ul>' );
1317       *
1318       *     // After rendering, the initial contents may have been modified by other renderers or filters.
1319       *     $block_content = <<<HTML
1320       *         <figure>
1321       *             <ul class="annotated-list list-wrapper is-unordered">
1322       *                 <li>Code</li>
1323       *             </ul><figcaption>It's a list!</figcaption>
1324       *         </figure>
1325       *     HTML;
1326       *
1327       * Although it is possible that the original block-wrapper classes are changed in $block_content
1328       * from how they appear in $block['innerContent'], it's likely that the original class attributes
1329       * are still present in the wrapper as they are in this example. Frequently, additional classes
1330       * will also be present; rarely should classes be removed.
1331       *
1332       * @todo Find a better way to match the first inner block. If it's possible to identify where the
1333       *       first inner block starts, then it will be possible to find the last tag before it starts
1334       *       and then that tag, if an opening tag, can be solidly identified as a wrapping element.
1335       *       Can some unique value or class or ID be added to the inner blocks when they process
1336       *       so that they can be extracted here safely without guessing? Can the block rendering function
1337       *       return information about where the rendered inner blocks start?
1338       *
1339       * @var string|null
1340       */
1341      $inner_block_wrapper_classes = null;
1342      $first_chunk                 = $block['innerContent'][0] ?? null;
1343      if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
1344          $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
1345          /*
1346           * Use a stack to track open elements as tags are visited. Void elements
1347           * (those without a matching closing tag) are excluded so they don't
1348           * accumulate on the stack. At the end of the chunk, every element still
1349           * on the stack is unclosed — meaning its closing tag lives in a later
1350           * innerContent entry alongside the inner blocks, which makes it the
1351           * inner-block container. Elements that open and close within this chunk
1352           * are siblings that precede the inner blocks and should be ignored.
1353           * The last unclosed element with a class attribute is the best candidate
1354           * for the inner-block wrapper.
1355           */
1356          $tag_stack = array();
1357          while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
1358              if ( $first_chunk_processor->is_tag_closer() ) {
1359                  array_pop( $tag_stack );
1360              } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) {
1361                  $tag_stack[] = $first_chunk_processor->get_attribute( 'class' );
1362              }
1363          }
1364          foreach ( array_reverse( $tag_stack ) as $class_attribute ) {
1365              if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
1366                  $inner_block_wrapper_classes = $class_attribute;
1367                  break;
1368              }
1369          }
1370      }
1371  
1372      /*
1373       * If necessary, advance to what is likely to be an inner block wrapper tag.
1374       *
1375       * This advances until it finds the first tag containing the original class
1376       * attribute from above. If none is found it will scan to the end of the block
1377       * and fail to add any class names.
1378       *
1379       * If there is no block wrapper it won't advance at all, in which case the
1380       * class names will be added to the first and outermost tag of the block.
1381       * For cases where this outermost tag is the only tag surrounding inner
1382       * blocks then the outer wrapper and inner wrapper are the same.
1383       */
1384      do {
1385          if ( ! $inner_block_wrapper_classes ) {
1386              break;
1387          }
1388  
1389          $class_attribute = $processor->get_attribute( 'class' );
1390          if ( is_string( $class_attribute ) && str_contains( $class_attribute, $inner_block_wrapper_classes ) ) {
1391              break;
1392          }
1393      } while ( $processor->next_tag() );
1394  
1395      // Add the remaining class names.
1396      foreach ( $class_names as $class_name ) {
1397          $processor->add_class( $class_name );
1398      }
1399  
1400      return $processor->get_updated_html();
1401  }
1402  
1403  /**
1404   * Check if the parent block exists and if it has a layout attribute.
1405   * If it does, add the parent layout to the parsed block
1406   *
1407   * @since 6.6.0
1408   * @access private
1409   *
1410   * @param array    $parsed_block The parsed block.
1411   * @param array    $source_block The source block.
1412   * @param WP_Block $parent_block The parent block.
1413   * @return array The parsed block with parent layout attribute if it exists.
1414   */
1415  function wp_add_parent_layout_to_parsed_block( $parsed_block, $source_block, $parent_block ) {
1416      if ( $parent_block && isset( $parent_block->parsed_block['attrs']['layout'] ) ) {
1417          $parsed_block['parentLayout'] = $parent_block->parsed_block['attrs']['layout'];
1418      }
1419      return $parsed_block;
1420  }
1421  
1422  add_filter( 'render_block_data', 'wp_add_parent_layout_to_parsed_block', 10, 3 );
1423  
1424  // Register the block support.
1425  WP_Block_Supports::get_instance()->register(
1426      'layout',
1427      array(
1428          'register_attribute' => 'wp_register_layout_support',
1429      )
1430  );
1431  add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
1432  
1433  /**
1434   * For themes without theme.json file, make sure
1435   * to restore the inner div for the group block
1436   * to avoid breaking styles relying on that div.
1437   *
1438   * @since 5.8.0
1439   * @since 6.6.1 Removed inner container from Grid variations.
1440   * @access private
1441   *
1442   * @param string $block_content Rendered block content.
1443   * @param array  $block         Block object.
1444   * @return string Filtered block content.
1445   */
1446  function wp_restore_group_inner_container( $block_content, $block ) {
1447      $tag_name                         = $block['attrs']['tagName'] ?? 'div';
1448      $group_with_inner_container_regex = sprintf(
1449          '/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
1450          preg_quote( $tag_name, '/' )
1451      );
1452  
1453      if (
1454          wp_theme_has_theme_json() ||
1455          1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
1456          ( isset( $block['attrs']['layout']['type'] ) && ( 'flex' === $block['attrs']['layout']['type'] || 'grid' === $block['attrs']['layout']['type'] ) )
1457      ) {
1458          return $block_content;
1459      }
1460  
1461      /*
1462       * This filter runs after the layout classnames have been added to the block, so they
1463       * have to be removed from the outer wrapper and then added to the inner.
1464       */
1465      $layout_classes = array();
1466      $processor      = new WP_HTML_Tag_Processor( $block_content );
1467  
1468      if ( $processor->next_tag( array( 'class_name' => 'wp-block-group' ) ) ) {
1469          foreach ( $processor->class_list() as $class_name ) {
1470              if ( str_contains( $class_name, 'is-layout-' ) ) {
1471                  $layout_classes[] = $class_name;
1472                  $processor->remove_class( $class_name );
1473              }
1474          }
1475      }
1476  
1477      $content_without_layout_classes = $processor->get_updated_html();
1478      $replace_regex                  = sprintf(
1479          '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
1480          preg_quote( $tag_name, '/' )
1481      );
1482      $updated_content                = preg_replace_callback(
1483          $replace_regex,
1484          static function ( $matches ) {
1485              return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
1486          },
1487          $content_without_layout_classes
1488      );
1489  
1490      // Add layout classes to inner wrapper.
1491      if ( ! empty( $layout_classes ) ) {
1492          $processor = new WP_HTML_Tag_Processor( $updated_content );
1493          if ( $processor->next_tag( array( 'class_name' => 'wp-block-group__inner-container' ) ) ) {
1494              foreach ( $layout_classes as $class_name ) {
1495                  $processor->add_class( $class_name );
1496              }
1497          }
1498          $updated_content = $processor->get_updated_html();
1499      }
1500      return $updated_content;
1501  }
1502  
1503  add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
1504  
1505  /**
1506   * For themes without theme.json file, make sure
1507   * to restore the outer div for the aligned image block
1508   * to avoid breaking styles relying on that div.
1509   *
1510   * @since 6.0.0
1511   * @access private
1512   *
1513   * @param string $block_content Rendered block content.
1514   * @param  array  $block        Block object.
1515   * @return string Filtered block content.
1516   */
1517  function wp_restore_image_outer_container( $block_content, $block ) {
1518      if ( wp_theme_has_theme_json() ) {
1519          return $block_content;
1520      }
1521  
1522      $figure_processor = new WP_HTML_Tag_Processor( $block_content );
1523      if (
1524          ! $figure_processor->next_tag( 'FIGURE' ) ||
1525          ! $figure_processor->has_class( 'wp-block-image' ) ||
1526          ! (
1527              $figure_processor->has_class( 'alignleft' ) ||
1528              $figure_processor->has_class( 'aligncenter' ) ||
1529              $figure_processor->has_class( 'alignright' )
1530          )
1531      ) {
1532          return $block_content;
1533      }
1534  
1535      /*
1536       * The next section of code wraps the existing figure in a new DIV element.
1537       * While doing it, it needs to transfer the layout and the additional CSS
1538       * class names from the original figure upward to the wrapper.
1539       *
1540       * Example:
1541       *
1542       *     // From this…
1543       *     <!-- wp:image {"className":"hires"} -->
1544       *     <figure class="wp-block-image wide hires">…
1545       *
1546       *     // To this…
1547       *     <div class="wp-block-image hires"><figure class="wide">…
1548       */
1549      $wrapper_processor = new WP_HTML_Tag_Processor( '<div>' );
1550      $wrapper_processor->next_token();
1551      $wrapper_processor->set_attribute(
1552          'class',
1553          is_string( $block['attrs']['className'] ?? null )
1554              ? "wp-block-image {$block['attrs']['className']}"
1555              : 'wp-block-image'
1556      );
1557  
1558      // And remove them from the existing content; it has been transferred upward.
1559      $figure_processor->remove_class( 'wp-block-image' );
1560      foreach ( $wrapper_processor->class_list() as $class_name ) {
1561          $figure_processor->remove_class( $class_name );
1562      }
1563  
1564      return "{$wrapper_processor->get_updated_html()}{$figure_processor->get_updated_html()}</div>";
1565  }
1566  
1567  add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );


Generated : Wed Jul 1 08:20:12 2026 Cross-referenced by PHPXref