@@ -321,10 +321,53 @@ function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false
321
321
function wp_render_layout_support_flag ( $ block_content , $ block ) {
322
322
$ block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( $ block ['blockName ' ] );
323
323
$ support_layout = block_has_support ( $ block_type , array ( '__experimentalLayout ' ), false );
324
+ $ has_child_layout = isset ( $ block ['attrs ' ]['style ' ]['layout ' ]['selfStretch ' ] );
324
325
325
- if ( ! $ support_layout ) {
326
+ if ( ! $ support_layout && ! $ has_child_layout ) {
326
327
return $ block_content ;
327
328
}
329
+ $ outer_class_names = array ();
330
+
331
+ if ( $ has_child_layout && ( 'fixed ' === $ block ['attrs ' ]['style ' ]['layout ' ]['selfStretch ' ] || 'fill ' === $ block ['attrs ' ]['style ' ]['layout ' ]['selfStretch ' ] ) ) {
332
+ $ container_content_class = wp_unique_id ( 'wp-container-content- ' );
333
+
334
+ $ child_layout_styles = array ();
335
+
336
+ if ( 'fixed ' === $ block ['attrs ' ]['style ' ]['layout ' ]['selfStretch ' ] && isset ( $ block ['attrs ' ]['style ' ]['layout ' ]['flexSize ' ] ) ) {
337
+ $ child_layout_styles [] = array (
338
+ 'selector ' => ". $ container_content_class " ,
339
+ 'declarations ' => array (
340
+ 'flex-basis ' => $ block ['attrs ' ]['style ' ]['layout ' ]['flexSize ' ],
341
+ 'box-sizing ' => 'border-box ' ,
342
+ ),
343
+ );
344
+ } elseif ( 'fill ' === $ block ['attrs ' ]['style ' ]['layout ' ]['selfStretch ' ] ) {
345
+ $ child_layout_styles [] = array (
346
+ 'selector ' => ". $ container_content_class " ,
347
+ 'declarations ' => array (
348
+ 'flex-grow ' => '1 ' ,
349
+ ),
350
+ );
351
+ }
352
+
353
+ wp_style_engine_get_stylesheet_from_css_rules (
354
+ $ child_layout_styles ,
355
+ array (
356
+ 'context ' => 'block-supports ' ,
357
+ 'prettify ' => false ,
358
+ )
359
+ );
360
+
361
+ $ outer_class_names [] = $ container_content_class ;
362
+ }
363
+
364
+ // Return early if only child layout exists.
365
+ if ( ! $ support_layout && ! empty ( $ outer_class_names ) ) {
366
+ $ content = new WP_HTML_Tag_Processor ( $ block_content );
367
+ $ content ->next_tag ();
368
+ $ content ->add_class ( implode ( ' ' , $ outer_class_names ) );
369
+ return (string ) $ content ;
370
+ }
328
371
329
372
$ global_settings = wp_get_global_settings ();
330
373
$ block_gap = _wp_array_get ( $ global_settings , array ( 'spacing ' , 'blockGap ' ), null );
@@ -341,7 +384,6 @@ function wp_render_layout_support_flag( $block_content, $block ) {
341
384
342
385
$ class_names = array ();
343
386
$ layout_definitions = _wp_array_get ( $ global_layout_settings , array ( 'definitions ' ), array () );
344
- $ block_classname = wp_get_block_default_classname ( $ block ['blockName ' ] );
345
387
$ container_class = wp_unique_id ( 'wp-container- ' );
346
388
$ layout_classname = '' ;
347
389
@@ -417,7 +459,7 @@ function wp_render_layout_support_flag( $block_content, $block ) {
417
459
$ should_skip_gap_serialization = wp_should_skip_block_supports_serialization ( $ block_type , 'spacing ' , 'blockGap ' );
418
460
419
461
$ style = wp_get_layout_style (
420
- ". $ block_classname . $ container_class " ,
462
+ ". $ container_class . $ container_class " ,
421
463
$ used_layout ,
422
464
$ has_block_gap_support ,
423
465
$ gap_value ,
@@ -432,18 +474,49 @@ function wp_render_layout_support_flag( $block_content, $block ) {
432
474
}
433
475
}
434
476
435
- /*
436
- * This assumes the hook only applies to blocks with a single wrapper.
437
- * A limitation of this hook is that nested inner blocks wrappers are not yet supported.
438
- */
439
- $ content = preg_replace (
440
- '/ ' . preg_quote ( 'class=" ' , '/ ' ) . '/ ' ,
441
- 'class=" ' . esc_attr ( implode ( ' ' , $ class_names ) ) . ' ' ,
442
- $ block_content ,
443
- 1
444
- );
477
+ $ content_with_outer_classnames = '' ;
478
+
479
+ if ( ! empty ( $ outer_class_names ) ) {
480
+ $ content_with_outer_classnames = new WP_HTML_Tag_Processor ( $ block_content );
481
+ $ content_with_outer_classnames ->next_tag ();
482
+ foreach ( $ outer_class_names as $ outer_class_name ) {
483
+ $ content_with_outer_classnames ->add_class ( $ outer_class_name );
484
+ }
485
+
486
+ $ content_with_outer_classnames = (string ) $ content_with_outer_classnames ;
487
+ }
488
+
489
+ /**
490
+ * The first chunk of innerContent contains the block markup up until the inner blocks start.
491
+ * This targets the opening tag of the inner blocks wrapper, which is the last tag in that chunk.
492
+ */
493
+ $ inner_content_classnames = '' ;
494
+
495
+ if ( isset ( $ block ['innerContent ' ][0 ] ) && 'string ' === gettype ( $ block ['innerContent ' ][0 ] ) ) {
496
+ $ tags = new WP_HTML_Tag_Processor ( $ block ['innerContent ' ][0 ] );
497
+ $ last_classnames = '' ;
498
+ while ( $ tags ->next_tag () ) {
499
+ $ last_classnames = $ tags ->get_attribute ( 'class ' );
500
+ }
501
+
502
+ $ inner_content_classnames = (string ) $ last_classnames ;
503
+ }
504
+
505
+ $ content = $ content_with_outer_classnames ? new WP_HTML_Tag_Processor ( $ content_with_outer_classnames ) : new WP_HTML_Tag_Processor ( $ block_content );
506
+
507
+ if ( $ inner_content_classnames ) {
508
+ $ content ->next_tag ( array ( 'class_name ' => $ inner_content_classnames ) );
509
+ foreach ( $ class_names as $ class_name ) {
510
+ $ content ->add_class ( $ class_name );
511
+ }
512
+ } else {
513
+ $ content ->next_tag ();
514
+ foreach ( $ class_names as $ class_name ) {
515
+ $ content ->add_class ( $ class_name );
516
+ }
517
+ }
445
518
446
- return $ content ;
519
+ return ( string ) $ content ;
447
520
}
448
521
449
522
// Register the block support.
0 commit comments