| @@ -237,228 +237,8 @@ | ||
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
| 241 | - * Returns the child-layout-only subset of a layout object. | |
| 242 | - * | |
| 243 | - * Child layout keys are the ones controlling how the block lays itself out | |
| 244 | - * inside its parent's grid or flex container. | |
| 245 | - * | |
| 246 | - * @param mixed $layout Layout object. | |
| 247 | - * @return array Child layout values, or an empty array. | |
| 248 | - */ | |
| 249 | -function gutenberg_get_layout_child_values( $layout ) { | |
| 250 | - if ( ! is_array( $layout ) ) { | |
| 251 | - return array(); | |
| 252 | - } | |
| 253 | - | |
| 254 | - return array_intersect_key( | |
| 255 | - $layout, | |
| 256 | - array_flip( | |
| 257 | - array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' ) | |
| 258 | - ) | |
| 259 | - ); | |
| 260 | -} | |
| 261 | - | |
| 262 | -/** | |
| 263 | - * Returns the container-layout subset of a layout object (everything except child layout keys). | |
| 264 | - * | |
| 265 | - * @param mixed $layout Layout object. | |
| 266 | - * @return array Container layout values, or an empty array. | |
| 267 | - */ | |
| 268 | -function gutenberg_get_layout_container_values( $layout ) { | |
| 269 | - if ( ! is_array( $layout ) ) { | |
| 270 | - return array(); | |
| 271 | - } | |
| 272 | - | |
| 273 | - return array_diff_key( | |
| 274 | - $layout, | |
| 275 | - array_flip( | |
| 276 | - array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' ) | |
| 277 | - ) | |
| 278 | - ); | |
| 279 | -} | |
| 280 | - | |
| 281 | -/** | |
| 282 | - * Sanitizes a block gap value before layout style generation. | |
| 283 | - * | |
| 284 | - * Regex for CSS value borrowed from `safecss_filter_attr`, used here to only match | |
| 285 | - * against the value, not the CSS attribute. | |
| 286 | - * | |
| 287 | - * @param string|array|null $gap_value Block gap value. | |
| 288 | - * @return string|array|null Sanitized block gap value. | |
| 289 | - */ | |
| 290 | -function gutenberg_sanitize_block_gap_value( $gap_value ) { | |
| 291 | - if ( is_array( $gap_value ) ) { | |
| 292 | - foreach ( $gap_value as $key => $value ) { | |
| 293 | - $gap_value[ $key ] = ! is_scalar( $value ) || ( $value && preg_match( '%[\\\(&=}]|/\*%', (string) $value ) ) ? null : $value; | |
| 294 | - } | |
| 295 | - return $gap_value; | |
| 296 | - } | |
| 297 | - | |
| 298 | - return $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; | |
| 299 | -} | |
| 300 | - | |
| 301 | -/** | |
| 302 | - * Returns child layout styles for a block affected by its parent's layout. | |
| 303 | - * | |
| 304 | - * @param string $selector CSS selector. | |
| 305 | - * @param array $child_layout Child layout values. | |
| 306 | - * @param array $parent_layout Parent layout values. | |
| 307 | - * @param array|null $viewport_overrides Optional. Child viewport layout overrides to emit. | |
| 308 | - * @return array Child layout style rules. | |
| 309 | - */ | |
| 310 | -function gutenberg_get_child_layout_style_rules( $selector, $child_layout, $parent_layout = array(), $viewport_overrides = null ) { | |
| 311 | - $base_child_layout = is_array( $child_layout ) ? $child_layout : array(); | |
| 312 | - $viewport_overrides = is_array( $viewport_overrides ) ? $viewport_overrides : null; | |
| 313 | - $child_layout = null === $viewport_overrides ? $base_child_layout : array_replace( $base_child_layout, $viewport_overrides ); | |
| 314 | - $child_layout_declarations = array(); | |
| 315 | - $child_layout_styles = array(); | |
| 316 | - $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) { | |
| 317 | - return array_key_exists( $property, $viewport_overrides ); | |
| 318 | - }; | |
| 319 | - | |
| 320 | - $self_stretch = $child_layout['selfStretch'] ?? null; | |
| 321 | - $base_self_stretch = $base_child_layout['selfStretch'] ?? null; | |
| 322 | - | |
| 323 | - /* | |
| 324 | - * These are the serialized `selfStretch` values. `max` used to be called | |
| 325 | - * "Fixed" in the UI, but was renamed and replaced by `fixedNoShrink`. | |
| 326 | - */ | |
| 327 | - $flex_child_layout_values = array( | |
| 328 | - 'fit' => 'fit', | |
| 329 | - 'grow' => 'fill', | |
| 330 | - 'max' => 'fixed', | |
| 331 | - 'fixed' => 'fixedNoShrink', | |
| 332 | - ); | |
| 333 | - $flex_size_values = array( | |
| 334 | - $flex_child_layout_values['max'], | |
| 335 | - $flex_child_layout_values['fixed'], | |
| 336 | - ); | |
| 337 | - | |
| 338 | - if ( null === $viewport_overrides || $has_viewport_property_override( 'selfStretch' ) || $has_viewport_property_override( 'flexSize' ) ) { | |
| 339 | - if ( | |
| 340 | - null !== $viewport_overrides && | |
| 341 | - ( $flex_child_layout_values['fit'] === $self_stretch || $flex_child_layout_values['grow'] === $self_stretch ) && | |
| 342 | - in_array( $base_self_stretch, $flex_size_values, true ) && | |
| 343 | - isset( $base_child_layout['flexSize'] ) | |
| 344 | - ) { | |
| 345 | - $child_layout_declarations['flex-basis'] = 'unset'; | |
| 346 | - if ( $flex_child_layout_values['fixed'] === $base_self_stretch ) { | |
| 347 | - $child_layout_declarations['flex-shrink'] = 'unset'; | |
| 348 | - } | |
| 349 | - } | |
| 350 | - if ( in_array( $self_stretch, $flex_size_values, true ) && isset( $child_layout['flexSize'] ) ) { | |
| 351 | - $child_layout_declarations['flex-basis'] = $child_layout['flexSize']; | |
| 352 | - if ( $flex_child_layout_values['fixed'] === $self_stretch ) { | |
| 353 | - $child_layout_declarations['flex-shrink'] = '0'; | |
| 354 | - } elseif ( null !== $viewport_overrides && $flex_child_layout_values['fixed'] === $base_self_stretch ) { | |
| 355 | - $child_layout_declarations['flex-shrink'] = 'unset'; | |
| 356 | - } | |
| 357 | - $child_layout_declarations['box-sizing'] = 'border-box'; | |
| 358 | - } elseif ( $flex_child_layout_values['grow'] === $self_stretch ) { | |
| 359 | - $child_layout_declarations['flex-grow'] = '1'; | |
| 360 | - } | |
| 361 | - } | |
| 362 | - | |
| 363 | - $column_start = $child_layout['columnStart'] ?? null; | |
| 364 | - $column_span = $child_layout['columnSpan'] ?? null; | |
| 365 | - if ( null === $viewport_overrides || $has_viewport_property_override( 'columnStart' ) || $has_viewport_property_override( 'columnSpan' ) ) { | |
| 366 | - if ( $column_start && $column_span ) { | |
| 367 | - $child_layout_declarations['grid-column'] = "$column_start / span $column_span"; | |
| 368 | - } elseif ( $column_start ) { | |
| 369 | - $child_layout_declarations['grid-column'] = "$column_start"; | |
| 370 | - } elseif ( $column_span ) { | |
| 371 | - $child_layout_declarations['grid-column'] = "span $column_span"; | |
| 372 | - } | |
| 373 | - } | |
| 374 | - | |
| 375 | - $row_start = $child_layout['rowStart'] ?? null; | |
| 376 | - $row_span = $child_layout['rowSpan'] ?? null; | |
| 377 | - if ( null === $viewport_overrides || $has_viewport_property_override( 'rowStart' ) || $has_viewport_property_override( 'rowSpan' ) ) { | |
| 378 | - if ( $row_start && $row_span ) { | |
| 379 | - $child_layout_declarations['grid-row'] = "$row_start / span $row_span"; | |
| 380 | - } elseif ( $row_start ) { | |
| 381 | - $child_layout_declarations['grid-row'] = "$row_start"; | |
| 382 | - } elseif ( $row_span ) { | |
| 383 | - $child_layout_declarations['grid-row'] = "span $row_span"; | |
| 384 | - } | |
| 385 | - } | |
| 386 | - | |
| 387 | - if ( ! empty( $child_layout_declarations ) ) { | |
| 388 | - $child_layout_styles[] = array( | |
| 389 | - 'selector' => $selector, | |
| 390 | - 'declarations' => $child_layout_declarations, | |
| 391 | - ); | |
| 392 | - } | |
| 393 | - | |
| 394 | - $minimum_column_width = $parent_layout['minimumColumnWidth'] ?? null; | |
| 395 | - $column_count = $parent_layout['columnCount'] ?? null; | |
| 396 | - | |
| 397 | - /* | |
| 398 | - * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set, | |
| 399 | - * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed | |
| 400 | - * once the grid has less columns than the start. | |
| 401 | - * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set. | |
| 402 | - * In that case, if columnCount doesn't exist, we can assume that the grid is responsive. | |
| 403 | - */ | |
| 404 | - if ( null === $viewport_overrides && ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) { | |
| 405 | - $column_span_number = floatval( $column_span ); | |
| 406 | - $column_start_number = floatval( $column_start ); | |
| 407 | - $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem'; | |
| 408 | - $parent_column_value = floatval( $parent_column_width ); | |
| 409 | - $parent_column_unit = explode( $parent_column_value, $parent_column_width ); | |
| 410 | - | |
| 411 | - $num_cols_to_break_at = 2; | |
| 412 | - if ( $column_span_number && $column_start_number ) { | |
| 413 | - $num_cols_to_break_at = $column_start_number + $column_span_number - 1; | |
| 414 | - } elseif ( $column_span_number ) { | |
| 415 | - $num_cols_to_break_at = $column_span_number; | |
| 416 | - } else { | |
| 417 | - $num_cols_to_break_at = $column_start_number; | |
| 418 | - } | |
| 419 | - | |
| 420 | - /* | |
| 421 | - * If there is no unit, the width has somehow been mangled so we reset both unit and value | |
| 422 | - * to defaults. | |
| 423 | - * Additionally, the unit should be one of px, rem or em, so that also needs to be checked. | |
| 424 | - */ | |
| 425 | - if ( count( $parent_column_unit ) <= 1 ) { | |
| 426 | - $parent_column_unit = 'rem'; | |
| 427 | - $parent_column_value = 12; | |
| 428 | - } else { | |
| 429 | - $parent_column_unit = $parent_column_unit[1]; | |
| 430 | - | |
| 431 | - if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) { | |
| 432 | - $parent_column_unit = 'rem'; | |
| 433 | - } | |
| 434 | - } | |
| 435 | - | |
| 436 | - /* | |
| 437 | - * A default gap value is used for this computation because custom gap values may not be | |
| 438 | - * viable to use in the computation of the container query value. | |
| 439 | - */ | |
| 440 | - $default_gap_value = 'px' === $parent_column_unit ? 24 : 1.5; | |
| 441 | - $container_query_value = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value; | |
| 442 | - $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1; | |
| 443 | - $container_query_value = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit; | |
| 444 | - // If a span is set we want to preserve it as long as possible, otherwise we just reset the value. | |
| 445 | - $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto'; | |
| 446 | - | |
| 447 | - $child_layout_styles[] = array( | |
| 448 | - 'rules_group' => "@container (max-width: $container_query_value )", | |
| 449 | - 'selector' => $selector, | |
| 450 | - 'declarations' => array( | |
| 451 | - 'grid-column' => $grid_column_value, | |
| 452 | - 'grid-row' => 'auto', | |
| 453 | - ), | |
| 454 | - ); | |
| 455 | - } | |
| 456 | - | |
| 457 | - return $child_layout_styles; | |
| 458 | -} | |
| 459 | - | |
| 460 | -/** | |
| 461 | 241 | * Generates the CSS corresponding to the provided layout. |
| 462 | 242 | * |
| 463 | 243 | * @param string $selector CSS selector. |
| 464 | 244 | * @param array $layout Layout object. The one that is passed has already checked |
| @@ -467,29 +247,16 @@ | ||
| 467 | 247 | * @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null. |
| 468 | 248 | * @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false. |
| 469 | 249 | * @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'. |
| 470 | 250 | * @param array|null $block_spacing Optional. Custom spacing set on the block. Default null. |
| 471 | - * @param array $options Optional. Extra options for internal callers. Default empty array. | |
| 472 | - * @return string CSS styles, or empty string. | |
| 251 | + * @return string CSS styles on success. Else, empty string. | |
| 473 | 252 | */ |
| 474 | -function gutenberg_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() ) { | |
| 475 | - $base_layout = is_array( $layout ) ? $layout : array(); | |
| 476 | - $viewport_overrides = $options['viewport_overrides'] ?? null; | |
| 477 | - $layout_for_styles = null === $viewport_overrides ? $base_layout : array_replace( $base_layout, $viewport_overrides ); | |
| 478 | - $layout_type = $base_layout['type'] ?? 'default'; | |
| 479 | - $rules_group = $options['rules_group'] ?? null; | |
| 480 | - $has_block_gap_override = ! empty( $options['has_block_gap_override'] ); | |
| 481 | - $should_output_block_gap = null === $viewport_overrides || $has_block_gap_override; | |
| 482 | - // Viewport styles only store changed fields. If a field is present with null, | |
| 483 | - // the user cleared a value inherited from the default viewport, so check | |
| 484 | - // whether the key exists rather than whether the value is truthy. | |
| 485 | - $has_viewport_property_override = static function ( $property ) use ( $viewport_overrides ) { | |
| 486 | - return array_key_exists( $property, $viewport_overrides ); | |
| 487 | - }; | |
| 488 | - $layout_styles = array(); | |
| 253 | +function gutenberg_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 ) { | |
| 254 | + $layout_type = $layout['type'] ?? 'default'; | |
| 255 | + $layout_styles = array(); | |
| 489 | 256 | |
| 490 | 257 | if ( 'default' === $layout_type ) { |
| 491 | - if ( $has_block_gap_support && $should_output_block_gap ) { | |
| 258 | + if ( $has_block_gap_support ) { | |
| 492 | 259 | if ( is_array( $gap_value ) ) { |
| 493 | 260 | $gap_value = $gap_value['top'] ?? null; |
| 494 | 261 | } |
| 495 | 262 | if ( null !== $gap_value && ! $should_skip_gap_serialization ) { |
| @@ -519,35 +286,15 @@ | ||
| 519 | 286 | ); |
| 520 | 287 | } |
| 521 | 288 | } |
| 522 | 289 | } elseif ( 'constrained' === $layout_type ) { |
| 523 | - $content_size = $layout_for_styles['contentSize'] ?? ''; | |
| 524 | - $wide_size = $layout_for_styles['wideSize'] ?? ''; | |
| 525 | - $justify_content = $layout_for_styles['justifyContent'] ?? 'center'; | |
| 290 | + $content_size = $layout['contentSize'] ?? ''; | |
| 291 | + $wide_size = $layout['wideSize'] ?? ''; | |
| 292 | + $justify_content = $layout['justifyContent'] ?? 'center'; | |
| 526 | 293 | |
| 527 | - // Check if viewport-specific ("override") values exist. Null values are valid and mean the user cleared a value inherited from the default viewport. | |
| 528 | - $has_justify_content_override = null !== $viewport_overrides && $has_viewport_property_override( 'justifyContent' ); | |
| 529 | - $has_content_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'contentSize' ); | |
| 530 | - $has_wide_size_override = null !== $viewport_overrides && $has_viewport_property_override( 'wideSize' ); | |
| 294 | + $all_max_width_value = $content_size ? $content_size : $wide_size; | |
| 295 | + $wide_max_width_value = $wide_size ? $wide_size : $content_size; | |
| 531 | 296 | |
| 532 | - /* 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 | |
| 533 | - * value for contentSize or wideSize. If a viewport clears a custom constrained size, reset to the global layout variable. | |
| 534 | - */ | |
| 535 | - $should_output_constrained_sizes = null === $viewport_overrides || $has_content_size_override || $has_wide_size_override; | |
| 536 | - $is_resetting_constrained_sizes = null !== $viewport_overrides && | |
| 537 | - ( | |
| 538 | - ( $has_content_size_override && ! $content_size ) || | |
| 539 | - ( $has_wide_size_override && ! $wide_size ) | |
| 540 | - ); | |
| 541 | - | |
| 542 | - // If a viewport clears a custom constrained size, reset to the global layout variable. | |
| 543 | - $all_max_width_value = $content_size | |
| 544 | - ? $content_size | |
| 545 | - : ( $wide_size && ! $has_content_size_override ? $wide_size : 'var(--wp--style--global--content-size, none)' ); | |
| 546 | - $wide_max_width_value = $wide_size | |
| 547 | - ? $wide_size | |
| 548 | - : ( $content_size && ! $has_wide_size_override ? $content_size : 'var(--wp--style--global--wide-size, none)' ); | |
| 549 | - | |
| 550 | 297 | // Make sure there is a single CSS rule, and all tags are stripped for security. |
| 551 | 298 | $all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] ); |
| 552 | 299 | $wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] ); |
| 553 | 300 | |
| @@ -553,23 +300,18 @@ | ||
| 553 | 300 | |
| 554 | 301 | $margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important'; |
| 555 | 302 | $margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important'; |
| 556 | 303 | |
| 557 | - if ( $should_output_constrained_sizes && ( $content_size || $wide_size || $is_resetting_constrained_sizes ) ) { | |
| 558 | - $content_size_declarations = array( | |
| 559 | - 'max-width' => $all_max_width_value, | |
| 560 | - ); | |
| 561 | - | |
| 562 | - if ( null === $viewport_overrides || $has_justify_content_override ) { | |
| 563 | - $content_size_declarations['margin-left'] = $margin_left; | |
| 564 | - $content_size_declarations['margin-right'] = $margin_right; | |
| 565 | - } | |
| 566 | - | |
| 304 | + if ( $content_size || $wide_size ) { | |
| 567 | 305 | array_push( |
| 568 | 306 | $layout_styles, |
| 569 | 307 | array( |
| 570 | 308 | 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", |
| 571 | - 'declarations' => $content_size_declarations, | |
| 309 | + 'declarations' => array( | |
| 310 | + 'max-width' => $all_max_width_value, | |
| 311 | + 'margin-left' => $margin_left, | |
| 312 | + 'margin-right' => $margin_right, | |
| 313 | + ), | |
| 572 | 314 | ), |
| 573 | 315 | array( |
| 574 | 316 | 'selector' => "$selector > .alignwide", |
| 575 | 317 | 'declarations' => array( 'max-width' => $wide_max_width_value ), |
| @@ -580,9 +322,9 @@ | ||
| 580 | 322 | ) |
| 581 | 323 | ); |
| 582 | 324 | } |
| 583 | 325 | |
| 584 | - if ( null === $viewport_overrides && isset( $block_spacing ) ) { | |
| 326 | + if ( isset( $block_spacing ) ) { | |
| 585 | 327 | $block_spacing_values = gutenberg_style_engine_get_styles( |
| 586 | 328 | array( |
| 587 | 329 | 'spacing' => $block_spacing, |
| 588 | 330 | ) |
| @@ -615,33 +357,23 @@ | ||
| 615 | 357 | ); |
| 616 | 358 | } |
| 617 | 359 | } |
| 618 | 360 | |
| 619 | - if ( $has_justify_content_override && ! $should_output_constrained_sizes ) { | |
| 361 | + if ( 'left' === $justify_content ) { | |
| 620 | 362 | $layout_styles[] = array( |
| 621 | 363 | 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", |
| 622 | - 'declarations' => array( | |
| 623 | - 'margin-left' => $margin_left, | |
| 624 | - 'margin-right' => $margin_right, | |
| 625 | - ), | |
| 364 | + 'declarations' => array( 'margin-left' => '0 !important' ), | |
| 626 | 365 | ); |
| 627 | - } elseif ( null === $viewport_overrides ) { | |
| 628 | - if ( 'left' === $justify_content ) { | |
| 629 | - $layout_styles[] = array( | |
| 630 | - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", | |
| 631 | - 'declarations' => array( 'margin-left' => '0 !important' ), | |
| 632 | - ); | |
| 633 | - } | |
| 366 | + } | |
| 634 | 367 | |
| 635 | - if ( 'right' === $justify_content ) { | |
| 636 | - $layout_styles[] = array( | |
| 637 | - 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", | |
| 638 | - 'declarations' => array( 'margin-right' => '0 !important' ), | |
| 639 | - ); | |
| 640 | - } | |
| 368 | + if ( 'right' === $justify_content ) { | |
| 369 | + $layout_styles[] = array( | |
| 370 | + 'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))", | |
| 371 | + 'declarations' => array( 'margin-right' => '0 !important' ), | |
| 372 | + ); | |
| 641 | 373 | } |
| 642 | 374 | |
| 643 | - if ( $has_block_gap_support && $should_output_block_gap ) { | |
| 375 | + if ( $has_block_gap_support ) { | |
| 644 | 376 | if ( is_array( $gap_value ) ) { |
| 645 | 377 | $gap_value = $gap_value['top'] ?? null; |
| 646 | 378 | } |
| 647 | 379 | if ( null !== $gap_value && ! $should_skip_gap_serialization ) { |
| @@ -671,9 +403,9 @@ | ||
| 671 | 403 | ); |
| 672 | 404 | } |
| 673 | 405 | } |
| 674 | 406 | } elseif ( 'flex' === $layout_type ) { |
| 675 | - $layout_orientation = $layout_for_styles['orientation'] ?? 'horizontal'; | |
| 407 | + $layout_orientation = $layout['orientation'] ?? 'horizontal'; | |
| 676 | 408 | |
| 677 | 409 | $justify_content_options = array( |
| 678 | 410 | 'left' => 'flex-start', |
| 679 | 411 | 'right' => 'flex-end', |
| @@ -693,17 +425,9 @@ | ||
| 693 | 425 | $justify_content_options += array( 'stretch' => 'stretch' ); |
| 694 | 426 | $vertical_alignment_options += array( 'space-between' => 'space-between' ); |
| 695 | 427 | } |
| 696 | 428 | |
| 697 | - /* 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 | |
| 698 | - * value for any of the flex properties. | |
| 699 | - */ | |
| 700 | - $should_output_flex_wrap = null === $viewport_overrides || $has_viewport_property_override( 'flexWrap' ); | |
| 701 | - $should_output_flex_orientation = null === $viewport_overrides || $has_viewport_property_override( 'orientation' ); | |
| 702 | - $should_output_flex_justification = null === $viewport_overrides || $has_viewport_property_override( 'justifyContent' ) || $has_viewport_property_override( 'orientation' ); | |
| 703 | - $should_output_flex_alignment = null === $viewport_overrides || $has_viewport_property_override( 'verticalAlignment' ) || $has_viewport_property_override( 'orientation' ); | |
| 704 | - | |
| 705 | - if ( $should_output_flex_wrap && ! empty( $layout_for_styles['flexWrap'] ) && 'nowrap' === $layout_for_styles['flexWrap'] ) { | |
| 429 | + if ( ! empty( $layout['flexWrap'] ) && 'nowrap' === $layout['flexWrap'] ) { | |
| 706 | 430 | $layout_styles[] = array( |
| 707 | 431 | 'selector' => $selector, |
| 708 | 432 | 'declarations' => array( 'flex-wrap' => 'nowrap' ), |
| 709 | 433 | ); |
| @@ -708,9 +432,9 @@ | ||
| 708 | 432 | 'declarations' => array( 'flex-wrap' => 'nowrap' ), |
| 709 | 433 | ); |
| 710 | 434 | } |
| 711 | 435 | |
| 712 | - if ( $has_block_gap_support && $should_output_block_gap && isset( $gap_value ) ) { | |
| 436 | + if ( $has_block_gap_support && isset( $gap_value ) ) { | |
| 713 | 437 | $combined_gap_value = ''; |
| 714 | 438 | $gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' ); |
| 715 | 439 | |
| 716 | 440 | foreach ( $gap_sides as $gap_side ) { |
| @@ -746,43 +470,41 @@ | ||
| 746 | 470 | * Add this style only if is not empty for backwards compatibility, |
| 747 | 471 | * since we intend to convert blocks that had flex layout implemented |
| 748 | 472 | * by custom css. |
| 749 | 473 | */ |
| 750 | - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) { | |
| 474 | + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { | |
| 751 | 475 | $layout_styles[] = array( |
| 752 | 476 | 'selector' => $selector, |
| 753 | - 'declarations' => array( 'justify-content' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ), | |
| 477 | + 'declarations' => array( 'justify-content' => $justify_content_options[ $layout['justifyContent'] ] ), | |
| 754 | 478 | ); |
| 755 | 479 | } |
| 756 | 480 | |
| 757 | - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) { | |
| 481 | + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) { | |
| 758 | 482 | $layout_styles[] = array( |
| 759 | 483 | 'selector' => $selector, |
| 760 | - 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ), | |
| 484 | + 'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ), | |
| 761 | 485 | ); |
| 762 | 486 | } |
| 763 | 487 | } else { |
| 764 | - if ( $should_output_flex_orientation ) { | |
| 488 | + $layout_styles[] = array( | |
| 489 | + 'selector' => $selector, | |
| 490 | + 'declarations' => array( 'flex-direction' => 'column' ), | |
| 491 | + ); | |
| 492 | + if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) { | |
| 765 | 493 | $layout_styles[] = array( |
| 766 | 494 | 'selector' => $selector, |
| 767 | - 'declarations' => array( 'flex-direction' => 'column' ), | |
| 495 | + 'declarations' => array( 'align-items' => $justify_content_options[ $layout['justifyContent'] ] ), | |
| 768 | 496 | ); |
| 769 | - } | |
| 770 | - if ( $should_output_flex_justification && ! empty( $layout_for_styles['justifyContent'] ) && array_key_exists( $layout_for_styles['justifyContent'], $justify_content_options ) ) { | |
| 497 | + } else { | |
| 771 | 498 | $layout_styles[] = array( |
| 772 | 499 | 'selector' => $selector, |
| 773 | - 'declarations' => array( 'align-items' => $justify_content_options[ $layout_for_styles['justifyContent'] ] ), | |
| 774 | - ); | |
| 775 | - } elseif ( $should_output_flex_justification ) { | |
| 776 | - $layout_styles[] = array( | |
| 777 | - 'selector' => $selector, | |
| 778 | 500 | 'declarations' => array( 'align-items' => 'flex-start' ), |
| 779 | 501 | ); |
| 780 | 502 | } |
| 781 | - if ( $should_output_flex_alignment && ! empty( $layout_for_styles['verticalAlignment'] ) && array_key_exists( $layout_for_styles['verticalAlignment'], $vertical_alignment_options ) ) { | |
| 503 | + if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) { | |
| 782 | 504 | $layout_styles[] = array( |
| 783 | 505 | 'selector' => $selector, |
| 784 | - 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout_for_styles['verticalAlignment'] ] ), | |
| 506 | + 'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ), | |
| 785 | 507 | ); |
| 786 | 508 | } |
| 787 | 509 | } |
| 788 | 510 | } elseif ( 'grid' === $layout_type ) { |
| @@ -826,56 +548,47 @@ | ||
| 826 | 548 | if ( '0' === $responsive_gap_value || 0 === $responsive_gap_value ) { |
| 827 | 549 | $responsive_gap_value = '0px'; |
| 828 | 550 | } |
| 829 | 551 | |
| 830 | - /* 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 | |
| 831 | - * value for any of the grid properties. | |
| 832 | - */ | |
| 833 | - $should_output_grid_columns = null === $viewport_overrides || $has_viewport_property_override( 'minimumColumnWidth' ) || $has_viewport_property_override( 'columnCount' ) || $has_viewport_property_override( 'autoFit' ); | |
| 834 | - $uses_gap_in_grid_columns = ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ); | |
| 835 | - if ( $has_block_gap_override && $uses_gap_in_grid_columns ) { | |
| 836 | - $should_output_grid_columns = true; | |
| 837 | - } | |
| 838 | - | |
| 839 | - $should_output_grid_rows = ( null === $viewport_overrides || $has_viewport_property_override( 'rowCount' ) ) && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['rowCount'] ); | |
| 840 | - $grid_declarations = array(); | |
| 841 | - | |
| 842 | - /* When enabled, columns stretch to fill the available space using | |
| 843 | - * `auto-fit`; otherwise empty tracks are preserved with `auto-fill`. | |
| 844 | - */ | |
| 845 | - $auto_placement = ! empty( $layout_for_styles['autoFit'] ) ? 'auto-fit' : 'auto-fill'; | |
| 846 | - | |
| 847 | - if ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) && ! empty( $layout_for_styles['minimumColumnWidth'] ) ) { | |
| 848 | - $max_value = 'max(min(' . $layout_for_styles['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout_for_styles['columnCount'] . ' - 1))) /' . $layout_for_styles['columnCount'] . ')'; | |
| 849 | - $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(' . $max_value . ', 1fr))'; | |
| 850 | - } elseif ( $should_output_grid_columns && ! empty( $layout_for_styles['columnCount'] ) ) { | |
| 851 | - $grid_declarations['grid-template-columns'] = 'repeat(' . $layout_for_styles['columnCount'] . ', minmax(0, 1fr))'; | |
| 852 | - } elseif ( $should_output_grid_columns ) { | |
| 853 | - $minimum_column_width = ! empty( $layout_for_styles['minimumColumnWidth'] ) ? $layout_for_styles['minimumColumnWidth'] : '12rem'; | |
| 854 | - $grid_declarations['grid-template-columns'] = 'repeat(' . $auto_placement . ', minmax(min(' . $minimum_column_width . ', 100%), 1fr))'; | |
| 855 | - } | |
| 856 | - | |
| 857 | - if ( ! empty( $grid_declarations ) ) { | |
| 858 | - $base_has_container_type = empty( $base_layout['columnCount'] ) || ( ! empty( $base_layout['columnCount'] ) && ! empty( $base_layout['minimumColumnWidth'] ) ); | |
| 859 | - if ( empty( $layout_for_styles['columnCount'] ) || ! empty( $layout_for_styles['minimumColumnWidth'] ) ) { | |
| 860 | - if ( null === $viewport_overrides || ! $base_has_container_type ) { | |
| 861 | - $grid_declarations['container-type'] = 'inline-size'; | |
| 862 | - } | |
| 552 | + if ( ! empty( $layout['columnCount'] ) && ! empty( $layout['minimumColumnWidth'] ) ) { | |
| 553 | + $max_value = 'max(min(' . $layout['minimumColumnWidth'] . ', 100%), (100% - (' . $responsive_gap_value . ' * (' . $layout['columnCount'] . ' - 1))) /' . $layout['columnCount'] . ')'; | |
| 554 | + $layout_styles[] = array( | |
| 555 | + 'selector' => $selector, | |
| 556 | + 'declarations' => array( | |
| 557 | + 'grid-template-columns' => 'repeat(auto-fill, minmax(' . $max_value . ', 1fr))', | |
| 558 | + 'container-type' => 'inline-size', | |
| 559 | + ), | |
| 560 | + ); | |
| 561 | + if ( ! empty( $layout['rowCount'] ) ) { | |
| 562 | + $layout_styles[] = array( | |
| 563 | + 'selector' => $selector, | |
| 564 | + 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ), | |
| 565 | + ); | |
| 863 | 566 | } |
| 567 | + } elseif ( ! empty( $layout['columnCount'] ) ) { | |
| 864 | 568 | $layout_styles[] = array( |
| 865 | 569 | 'selector' => $selector, |
| 866 | - 'declarations' => $grid_declarations, | |
| 570 | + 'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ), | |
| 867 | 571 | ); |
| 868 | - } | |
| 572 | + if ( ! empty( $layout['rowCount'] ) ) { | |
| 573 | + $layout_styles[] = array( | |
| 574 | + 'selector' => $selector, | |
| 575 | + 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(1rem, auto))' ), | |
| 576 | + ); | |
| 577 | + } | |
| 578 | + } else { | |
| 579 | + $minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem'; | |
| 869 | 580 | |
| 870 | - if ( $should_output_grid_rows ) { | |
| 871 | 581 | $layout_styles[] = array( |
| 872 | 582 | 'selector' => $selector, |
| 873 | - 'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout_for_styles['rowCount'] . ', minmax(1rem, auto))' ), | |
| 583 | + 'declarations' => array( | |
| 584 | + 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))', | |
| 585 | + 'container-type' => 'inline-size', | |
| 586 | + ), | |
| 874 | 587 | ); |
| 875 | 588 | } |
| 876 | 589 | |
| 877 | - if ( $has_block_gap_support && $should_output_block_gap && null !== $gap_value && ! $should_skip_gap_serialization ) { | |
| 590 | + if ( $has_block_gap_support && null !== $gap_value && ! $should_skip_gap_serialization ) { | |
| 878 | 591 | $layout_styles[] = array( |
| 879 | 592 | 'selector' => $selector, |
| 880 | 593 | 'declarations' => array( 'gap' => $gap_value ), |
| 881 | 594 | ); |
| @@ -882,14 +595,8 @@ | ||
| 882 | 595 | } |
| 883 | 596 | } |
| 884 | 597 | |
| 885 | 598 | if ( ! empty( $layout_styles ) ) { |
| 886 | - if ( ! empty( $rules_group ) ) { | |
| 887 | - foreach ( $layout_styles as $index => $layout_style ) { | |
| 888 | - $layout_styles[ $index ]['rules_group'] = $rules_group; | |
| 889 | - } | |
| 890 | - } | |
| 891 | - | |
| 892 | 599 | /* |
| 893 | 600 | * Add to the style engine store to enqueue and render layout styles. |
| 894 | 601 | * Return compiled layout styles to retain backwards compatibility. |
| 895 | 602 | * Since https://github.com/WordPress/gutenberg/pull/42452, |
| @@ -956,47 +663,12 @@ | ||
| 956 | 663 | static $global_styles = null; |
| 957 | 664 | |
| 958 | 665 | $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 959 | 666 | $block_supports_layout = block_has_support( $block_type, array( 'layout' ), false ) || block_has_support( $block_type, array( '__experimentalLayout' ), false ); |
| 960 | - $style_attr = gutenberg_resolve_style_state_aliases( | |
| 961 | - $block['attrs']['style'] ?? array(), | |
| 962 | - $block['blockName'] | |
| 963 | - ); | |
| 964 | - /* | |
| 965 | - * A block with no layout support and no style attribute at all cannot | |
| 966 | - * produce layout output, so return before resolving global settings. | |
| 967 | - * | |
| 968 | - * Resolving settings is not read-only: on a cold cache it queries the | |
| 969 | - * user's `wp_global_styles` post, which fires `the_posts`. A callback on | |
| 970 | - * that hook that renders blocks re-enters this filter, and the content it | |
| 971 | - * renders at that point is the global styles post itself, which parses to a | |
| 972 | - * single block with no name and no attributes. Without this return that | |
| 973 | - * block resolves settings again and the recursion has no base case. | |
| 974 | - */ | |
| 975 | - if ( ! $block_supports_layout && empty( $style_attr ) ) { | |
| 976 | - return $block_content; | |
| 977 | - } | |
| 978 | - | |
| 979 | - $global_settings = gutenberg_get_global_settings(); | |
| 980 | - $viewport_settings = $global_settings['viewport'] ?? null; | |
| 981 | - $responsive_media_queries = WP_Theme_JSON_Gutenberg::get_viewport_media_queries( $viewport_settings ); | |
| 982 | 667 | // If there is any value in style -> layout, the block has a child layout. |
| 983 | - $child_layout = $style_attr['layout'] ?? null; | |
| 668 | + $child_layout = $block['attrs']['style']['layout'] ?? null; | |
| 984 | 669 | |
| 985 | - // Collect responsive viewport child layout overrides so that a block with | |
| 986 | - // only responsive child layout (no base child layout) is still processed. | |
| 987 | - $viewport_child_layouts = array(); | |
| 988 | - foreach ( $responsive_media_queries as $breakpoint => $media_query ) { | |
| 989 | - $viewport_child = gutenberg_get_layout_child_values( $style_attr[ $breakpoint ]['layout'] ?? null ); | |
| 990 | - if ( ! empty( $viewport_child ) ) { | |
| 991 | - $viewport_child_layouts[ $breakpoint ] = array( | |
| 992 | - 'media_query' => $media_query, | |
| 993 | - 'child_layout' => $viewport_child, | |
| 994 | - ); | |
| 995 | - } | |
| 996 | - } | |
| 997 | - | |
| 998 | - if ( ! $block_supports_layout && ! $child_layout && empty( $viewport_child_layouts ) ) { | |
| 670 | + if ( ! $block_supports_layout && ! $child_layout ) { | |
| 999 | 671 | return $block_content; |
| 1000 | 672 | } |
| 1001 | 673 | |
| 1002 | 674 | $outer_class_names = array(); |
| @@ -1001,58 +673,134 @@ | ||
| 1001 | 673 | |
| 1002 | 674 | $outer_class_names = array(); |
| 1003 | 675 | |
| 1004 | 676 | // Child layout specific logic. |
| 1005 | - if ( $child_layout || ! empty( $viewport_child_layouts ) ) { | |
| 1006 | - $base_child_layout = gutenberg_get_layout_child_values( $child_layout ); | |
| 1007 | - $parent_layout = $block['parentLayout'] ?? array(); | |
| 1008 | - | |
| 677 | + if ( $child_layout ) { | |
| 1009 | 678 | /* |
| 1010 | 679 | * Generates a unique class for child block layout styles. |
| 1011 | 680 | * |
| 1012 | 681 | * To ensure consistent class generation across different page renders, |
| 1013 | 682 | * only properties that affect layout styling are used. These properties |
| 1014 | - * come from `$block['attrs']['style']['layout']`, viewport overrides in | |
| 1015 | - * `$block['attrs']['style'][$breakpoint]['layout']`, and | |
| 1016 | - * `$block['parentLayout']`. | |
| 683 | + * come from `$block['attrs']['style']['layout']` and `$block['parentLayout']`. | |
| 1017 | 684 | * |
| 1018 | 685 | * As long as these properties coincide, the generated class will be the same. |
| 1019 | 686 | */ |
| 1020 | - $container_content_hash_input = array( | |
| 1021 | - 'layout' => $base_child_layout, | |
| 1022 | - 'parentLayout' => array_intersect_key( | |
| 1023 | - $parent_layout, | |
| 1024 | - array_flip( array( 'minimumColumnWidth', 'columnCount' ) ) | |
| 687 | + $container_content_class = gutenberg_unique_id_from_values( | |
| 688 | + array( | |
| 689 | + 'layout' => array_intersect_key( | |
| 690 | + $block['attrs']['style']['layout'] ?? array(), | |
| 691 | + array_flip( | |
| 692 | + array( 'selfStretch', 'flexSize', 'columnStart', 'columnSpan', 'rowStart', 'rowSpan' ) | |
| 693 | + ) | |
| 694 | + ), | |
| 695 | + 'parentLayout' => array_intersect_key( | |
| 696 | + $block['parentLayout'] ?? array(), | |
| 697 | + array_flip( | |
| 698 | + array( 'minimumColumnWidth', 'columnCount' ) | |
| 699 | + ) | |
| 700 | + ), | |
| 1025 | 701 | ), |
| 702 | + 'wp-container-content-' | |
| 1026 | 703 | ); |
| 1027 | - foreach ( $viewport_child_layouts as $breakpoint => $viewport_data ) { | |
| 1028 | - $container_content_hash_input[ $breakpoint ] = $viewport_data['child_layout']; | |
| 704 | + | |
| 705 | + $child_layout_declarations = array(); | |
| 706 | + $child_layout_styles = array(); | |
| 707 | + | |
| 708 | + $self_stretch = $block['attrs']['style']['layout']['selfStretch'] ?? null; | |
| 709 | + | |
| 710 | + if ( 'fixed' === $self_stretch && isset( $block['attrs']['style']['layout']['flexSize'] ) ) { | |
| 711 | + $child_layout_declarations['flex-basis'] = $block['attrs']['style']['layout']['flexSize']; | |
| 712 | + $child_layout_declarations['box-sizing'] = 'border-box'; | |
| 713 | + } elseif ( 'fill' === $self_stretch ) { | |
| 714 | + $child_layout_declarations['flex-grow'] = '1'; | |
| 1029 | 715 | } |
| 1030 | - $container_content_class = gutenberg_unique_id_from_values( | |
| 1031 | - $container_content_hash_input, | |
| 1032 | - 'wp-container-content-' | |
| 716 | + | |
| 717 | + $column_start = $block['attrs']['style']['layout']['columnStart'] ?? null; | |
| 718 | + $column_span = $block['attrs']['style']['layout']['columnSpan'] ?? null; | |
| 719 | + if ( $column_start && $column_span ) { | |
| 720 | + $child_layout_declarations['grid-column'] = "$column_start / span $column_span"; | |
| 721 | + } elseif ( $column_start ) { | |
| 722 | + $child_layout_declarations['grid-column'] = "$column_start"; | |
| 723 | + } elseif ( $column_span ) { | |
| 724 | + $child_layout_declarations['grid-column'] = "span $column_span"; | |
| 725 | + } | |
| 726 | + | |
| 727 | + $row_start = $block['attrs']['style']['layout']['rowStart'] ?? null; | |
| 728 | + $row_span = $block['attrs']['style']['layout']['rowSpan'] ?? null; | |
| 729 | + if ( $row_start && $row_span ) { | |
| 730 | + $child_layout_declarations['grid-row'] = "$row_start / span $row_span"; | |
| 731 | + } elseif ( $row_start ) { | |
| 732 | + $child_layout_declarations['grid-row'] = "$row_start"; | |
| 733 | + } elseif ( $row_span ) { | |
| 734 | + $child_layout_declarations['grid-row'] = "span $row_span"; | |
| 735 | + } | |
| 736 | + | |
| 737 | + $child_layout_styles[] = array( | |
| 738 | + 'selector' => ".$container_content_class", | |
| 739 | + 'declarations' => $child_layout_declarations, | |
| 1033 | 740 | ); |
| 1034 | 741 | |
| 1035 | - $child_layout_styles = gutenberg_get_child_layout_style_rules( | |
| 1036 | - ".$container_content_class", | |
| 1037 | - $base_child_layout, | |
| 1038 | - $parent_layout | |
| 1039 | - ); | |
| 742 | + $minimum_column_width = $block['parentLayout']['minimumColumnWidth'] ?? null; | |
| 743 | + $column_count = $block['parentLayout']['columnCount'] ?? null; | |
| 1040 | 744 | |
| 1041 | - // Emit responsive child layout CSS using the same container-content class | |
| 1042 | - // so that base and responsive child layout share the exact same selector. | |
| 1043 | - foreach ( $viewport_child_layouts as $viewport_data ) { | |
| 1044 | - $viewport_child_styles = gutenberg_get_child_layout_style_rules( | |
| 1045 | - ".$container_content_class", | |
| 1046 | - $base_child_layout, | |
| 1047 | - $parent_layout, | |
| 1048 | - $viewport_data['child_layout'] | |
| 1049 | - ); | |
| 1050 | - foreach ( $viewport_child_styles as $index => $rule ) { | |
| 1051 | - $viewport_child_styles[ $index ]['rules_group'] = $viewport_data['media_query']; | |
| 745 | + /* | |
| 746 | + * If columnSpan or columnStart is set, and the parent grid is responsive, i.e. if it has a minimumColumnWidth set, | |
| 747 | + * the columnSpan should be removed once the grid is smaller than the span, and columnStart should be removed | |
| 748 | + * once the grid has less columns than the start. | |
| 749 | + * If there's a minimumColumnWidth, the grid is responsive. But if the minimumColumnWidth value wasn't changed, it won't be set. | |
| 750 | + * In that case, if columnCount doesn't exist, we can assume that the grid is responsive. | |
| 751 | + */ | |
| 752 | + if ( ( $column_span || $column_start ) && ( $minimum_column_width || ! $column_count ) ) { | |
| 753 | + $column_span_number = floatval( $column_span ); | |
| 754 | + $column_start_number = floatval( $column_start ); | |
| 755 | + $parent_column_width = $minimum_column_width ? $minimum_column_width : '12rem'; | |
| 756 | + $parent_column_value = floatval( $parent_column_width ); | |
| 757 | + $parent_column_unit = explode( $parent_column_value, $parent_column_width ); | |
| 758 | + | |
| 759 | + $num_cols_to_break_at = 2; | |
| 760 | + if ( $column_span_number && $column_start_number ) { | |
| 761 | + $num_cols_to_break_at = $column_start_number + $column_span_number - 1; | |
| 762 | + } elseif ( $column_span_number ) { | |
| 763 | + $num_cols_to_break_at = $column_span_number; | |
| 764 | + } else { | |
| 765 | + $num_cols_to_break_at = $column_start_number; | |
| 1052 | 766 | } |
| 1053 | 767 | |
| 1054 | - $child_layout_styles = array_merge( $child_layout_styles, $viewport_child_styles ); | |
| 768 | + /* | |
| 769 | + * If there is no unit, the width has somehow been mangled so we reset both unit and value | |
| 770 | + * to defaults. | |
| 771 | + * Additionally, the unit should be one of px, rem or em, so that also needs to be checked. | |
| 772 | + */ | |
| 773 | + if ( count( $parent_column_unit ) <= 1 ) { | |
| 774 | + $parent_column_unit = 'rem'; | |
| 775 | + $parent_column_value = 12; | |
| 776 | + } else { | |
| 777 | + $parent_column_unit = $parent_column_unit[1]; | |
| 778 | + | |
| 779 | + if ( ! in_array( $parent_column_unit, array( 'px', 'rem', 'em' ), true ) ) { | |
| 780 | + $parent_column_unit = 'rem'; | |
| 781 | + } | |
| 782 | + } | |
| 783 | + | |
| 784 | + /* | |
| 785 | + * A default gap value is used for this computation because custom gap values may not be | |
| 786 | + * viable to use in the computation of the container query value. | |
| 787 | + */ | |
| 788 | + $default_gap_value = 'px' === $parent_column_unit ? 24 : 1.5; | |
| 789 | + $container_query_value = $num_cols_to_break_at * $parent_column_value + ( $num_cols_to_break_at - 1 ) * $default_gap_value; | |
| 790 | + $minimum_container_query_value = $parent_column_value * 2 + $default_gap_value - 1; | |
| 791 | + $container_query_value = max( $container_query_value, $minimum_container_query_value ) . $parent_column_unit; | |
| 792 | + // If a span is set we want to preserve it as long as possible, otherwise we just reset the value. | |
| 793 | + $grid_column_value = $column_span && $column_span > 1 ? '1/-1' : 'auto'; | |
| 794 | + | |
| 795 | + $child_layout_styles[] = array( | |
| 796 | + 'rules_group' => "@container (max-width: $container_query_value )", | |
| 797 | + 'selector' => ".$container_content_class", | |
| 798 | + 'declarations' => array( | |
| 799 | + 'grid-column' => $grid_column_value, | |
| 800 | + 'grid-row' => 'auto', | |
| 801 | + ), | |
| 802 | + ); | |
| 1055 | 803 | } |
| 1056 | 804 | |
| 1057 | 805 | /* |
| 1058 | 806 | * Add to the style engine store to enqueue and render layout styles. |
| @@ -1094,8 +842,9 @@ | ||
| 1094 | 842 | // Ensure layout classnames are not injected if there is no layout support. |
| 1095 | 843 | return $block_content; |
| 1096 | 844 | } |
| 1097 | 845 | |
| 846 | + $global_settings = gutenberg_get_global_settings(); | |
| 1098 | 847 | $fallback_layout = $block_type->supports['layout']['default'] ?? array(); |
| 1099 | 848 | if ( empty( $fallback_layout ) ) { |
| 1100 | 849 | $fallback_layout = $block_type->supports['__experimentalLayout']['default'] ?? array(); |
| 1101 | 850 | } |
| @@ -1149,10 +898,23 @@ | ||
| 1149 | 898 | * Attribute-based Layout classnames are output in all cases. |
| 1150 | 899 | */ |
| 1151 | 900 | if ( ! current_theme_supports( 'disable-layout-styles' ) ) { |
| 1152 | 901 | |
| 1153 | - $gap_value = gutenberg_sanitize_block_gap_value( $block['attrs']['style']['spacing']['blockGap'] ?? null ); | |
| 902 | + $gap_value = $block['attrs']['style']['spacing']['blockGap'] ?? null; | |
| 1154 | 903 | |
| 904 | + /* | |
| 905 | + * Skip if gap value contains unsupported characters. | |
| 906 | + * Regex for CSS value borrowed from `safecss_filter_attr`, and used here | |
| 907 | + * to only match against the value, not the CSS attribute. | |
| 908 | + */ | |
| 909 | + if ( is_array( $gap_value ) ) { | |
| 910 | + foreach ( $gap_value as $key => $value ) { | |
| 911 | + $gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; | |
| 912 | + } | |
| 913 | + } else { | |
| 914 | + $gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; | |
| 915 | + } | |
| 916 | + | |
| 1155 | 917 | $fallback_gap_value = $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ?? '0.5em'; |
| 1156 | 918 | $block_spacing = $block['attrs']['style']['spacing'] ?? null; |
| 1157 | 919 | |
| 1158 | 920 | /* |
| @@ -1173,11 +935,9 @@ | ||
| 1173 | 935 | |
| 1174 | 936 | // Check if the block has an active style variation with a blockGap value. |
| 1175 | 937 | // Only check the registry if the className contains a variation class to avoid unnecessary lookups. |
| 1176 | 938 | $variation_block_gap_value = null; |
| 1177 | - $block_class_name = is_string( $block['attrs']['className'] ?? null ) | |
| 1178 | - ? $block['attrs']['className'] | |
| 1179 | - : ''; | |
| 939 | + $block_class_name = $block['attrs']['className'] ?? ''; | |
| 1180 | 940 | if ( $block_class_name && str_contains( $block_class_name, 'is-style-' ) && $block_name ) { |
| 1181 | 941 | $styles_registry = WP_Block_Styles_Registry::get_instance(); |
| 1182 | 942 | $registered_styles = $styles_registry->get_registered_styles_for_block( $block_name ); |
| 1183 | 943 | $variation_name = gutenberg_get_block_style_variation_name_from_registered_style( $block_class_name, $registered_styles ); |
| @@ -1198,41 +958,17 @@ | ||
| 1198 | 958 | * even for different blocks with the same layout definition. We need this to |
| 1199 | 959 | * make the CSS class names stable across paginations for features like the |
| 1200 | 960 | * enhanced pagination of the Query block. |
| 1201 | 961 | */ |
| 1202 | - $container_class_hash_input = array( | |
| 1203 | - $used_layout, | |
| 1204 | - $has_block_gap_support, | |
| 1205 | - $gap_value, | |
| 1206 | - $should_skip_gap_serialization, | |
| 1207 | - $fallback_gap_value, | |
| 1208 | - $block_spacing, | |
| 1209 | - ); | |
| 1210 | - | |
| 1211 | - foreach ( array_keys( $responsive_media_queries ) as $breakpoint ) { | |
| 1212 | - $viewport_style = $style_attr[ $breakpoint ] ?? null; | |
| 1213 | - if ( ! is_array( $viewport_style ) ) { | |
| 1214 | - continue; | |
| 1215 | - } | |
| 1216 | - | |
| 1217 | - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null ); | |
| 1218 | - if ( ! empty( $viewport_container_layout ) ) { | |
| 1219 | - $container_class_hash_input[] = array( | |
| 1220 | - 'breakpoint' => $breakpoint, | |
| 1221 | - 'layout' => $viewport_container_layout, | |
| 1222 | - ); | |
| 1223 | - } | |
| 1224 | - | |
| 1225 | - if ( isset( $viewport_style['spacing']['blockGap'] ) ) { | |
| 1226 | - $container_class_hash_input[] = array( | |
| 1227 | - 'breakpoint' => $breakpoint, | |
| 1228 | - 'blockGap' => gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ), | |
| 1229 | - ); | |
| 1230 | - } | |
| 1231 | - } | |
| 1232 | - | |
| 1233 | 962 | $container_class = gutenberg_unique_id_from_values( |
| 1234 | - $container_class_hash_input, | |
| 963 | + array( | |
| 964 | + $used_layout, | |
| 965 | + $has_block_gap_support, | |
| 966 | + $gap_value, | |
| 967 | + $should_skip_gap_serialization, | |
| 968 | + $fallback_gap_value, | |
| 969 | + $block_spacing, | |
| 970 | + ), | |
| 1235 | 971 | 'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-' |
| 1236 | 972 | ); |
| 1237 | 973 | |
| 1238 | 974 | $style = gutenberg_get_layout_style( |
| @@ -1248,53 +984,8 @@ | ||
| 1248 | 984 | // Only add container class and enqueue block support styles if unique styles were generated. |
| 1249 | 985 | if ( ! empty( $style ) ) { |
| 1250 | 986 | $class_names[] = $container_class; |
| 1251 | 987 | } |
| 1252 | - | |
| 1253 | - /* | |
| 1254 | - * Emit responsive container layout styles using the same $container_class | |
| 1255 | - * selector as the base layout so they target the inner block wrapper. | |
| 1256 | - */ | |
| 1257 | - foreach ( $responsive_media_queries as $breakpoint => $media_query ) { | |
| 1258 | - $viewport_style = $style_attr[ $breakpoint ] ?? null; | |
| 1259 | - if ( ! is_array( $viewport_style ) ) { | |
| 1260 | - continue; | |
| 1261 | - } | |
| 1262 | - | |
| 1263 | - $viewport_container_layout = gutenberg_get_layout_container_values( $viewport_style['layout'] ?? null ); | |
| 1264 | - $has_viewport_layout = ! empty( $viewport_container_layout ); | |
| 1265 | - $has_viewport_block_gap = isset( $viewport_style['spacing']['blockGap'] ); | |
| 1266 | - | |
| 1267 | - if ( ! $has_viewport_layout && ! $has_viewport_block_gap ) { | |
| 1268 | - continue; | |
| 1269 | - } | |
| 1270 | - | |
| 1271 | - $viewport_gap_value = $has_viewport_block_gap | |
| 1272 | - ? gutenberg_sanitize_block_gap_value( $viewport_style['spacing']['blockGap'] ) | |
| 1273 | - : $gap_value; | |
| 1274 | - $viewport_block_spacing = is_array( $viewport_style['spacing'] ?? null ) | |
| 1275 | - ? array_replace( is_array( $block_spacing ) ? $block_spacing : array(), $viewport_style['spacing'] ) | |
| 1276 | - : $block_spacing; | |
| 1277 | - | |
| 1278 | - $viewport_styles = gutenberg_get_layout_style( | |
| 1279 | - ".$container_class", | |
| 1280 | - $used_layout, | |
| 1281 | - $has_block_gap_support, | |
| 1282 | - $viewport_gap_value, | |
| 1283 | - $should_skip_gap_serialization, | |
| 1284 | - $fallback_gap_value, | |
| 1285 | - $viewport_block_spacing, | |
| 1286 | - array( | |
| 1287 | - 'rules_group' => $media_query, | |
| 1288 | - 'viewport_overrides' => $viewport_container_layout, | |
| 1289 | - 'has_block_gap_override' => $has_viewport_block_gap, | |
| 1290 | - ) | |
| 1291 | - ); | |
| 1292 | - | |
| 1293 | - if ( ! empty( $viewport_styles ) && ! in_array( $container_class, $class_names, true ) ) { | |
| 1294 | - $class_names[] = $container_class; | |
| 1295 | - } | |
| 1296 | - } | |
| 1297 | 988 | } |
| 1298 | 989 | |
| 1299 | 990 | // Add combined layout and block classname for global styles to hook onto. |
| 1300 | 991 | $split_block_name = explode( '/', $block['blockName'] ); |
| @@ -1358,31 +1049,12 @@ | ||
| 1358 | 1049 | $inner_block_wrapper_classes = null; |
| 1359 | 1050 | $first_chunk = $block['innerContent'][0] ?? null; |
| 1360 | 1051 | if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) { |
| 1361 | 1052 | $first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk ); |
| 1362 | - /* | |
| 1363 | - * Use a stack to track open elements as tags are visited. Void elements | |
| 1364 | - * (those without a matching closing tag) are excluded so they don't | |
| 1365 | - * accumulate on the stack. At the end of the chunk, every element still | |
| 1366 | - * on the stack is unclosed — meaning its closing tag lives in a later | |
| 1367 | - * innerContent entry alongside the inner blocks, which makes it the | |
| 1368 | - * inner-block container. Elements that open and close within this chunk | |
| 1369 | - * are siblings that precede the inner blocks and should be ignored. | |
| 1370 | - * The last unclosed element with a class attribute is the best candidate | |
| 1371 | - * for the inner-block wrapper. | |
| 1372 | - */ | |
| 1373 | - $tag_stack = array(); | |
| 1374 | - while ( $first_chunk_processor->next_tag( array( 'tag_closers' => 'visit' ) ) ) { | |
| 1375 | - if ( $first_chunk_processor->is_tag_closer() ) { | |
| 1376 | - array_pop( $tag_stack ); | |
| 1377 | - } elseif ( ! WP_HTML_Processor::is_void( $first_chunk_processor->get_tag() ) ) { | |
| 1378 | - $tag_stack[] = $first_chunk_processor->get_attribute( 'class' ); | |
| 1379 | - } | |
| 1380 | - } | |
| 1381 | - foreach ( array_reverse( $tag_stack ) as $class_attribute ) { | |
| 1053 | + while ( $first_chunk_processor->next_tag() ) { | |
| 1054 | + $class_attribute = $first_chunk_processor->get_attribute( 'class' ); | |
| 1382 | 1055 | if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) { |
| 1383 | 1056 | $inner_block_wrapper_classes = $class_attribute; |
| 1384 | - break; | |
| 1385 | 1057 | } |
| 1386 | 1058 | } |
| 1387 | 1059 | } |
| 1388 | 1060 | |