Make WordPress Core


Ignore:
Timestamp:
02/07/2023 05:41:11 PM (20 months ago)
Author:
flixos90
Message:

Editor: Add layout controls to children of flex layout blocks.

Props isabel_brison, andrewserong, davidbaumwald, flixos90, mamaduka, ntsekouras, hellofromtonya.
Fixes #57584.

File:
1 edited

Legend:

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

    r55201 r55282  
    322322    $block_type     = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    323323    $support_layout = block_has_support( $block_type, array( '__experimentalLayout' ), false );
    324 
    325     if ( ! $support_layout ) {
     324    $has_child_layout = isset( $block['attrs']['style']['layout']['selfStretch'] );
     325
     326    if ( ! $support_layout && ! $has_child_layout ) {
    326327        return $block_content;
     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;
    327370    }
    328371
     
    342385    $class_names        = array();
    343386    $layout_definitions = _wp_array_get( $global_layout_settings, array( 'definitions' ), array() );
    344     $block_classname    = wp_get_block_default_classname( $block['blockName'] );
    345387    $container_class    = wp_unique_id( 'wp-container-' );
    346388    $layout_classname   = '';
     
    418460
    419461        $style = wp_get_layout_style(
    420             ".$block_classname.$container_class",
     462            ".$container_class.$container_class",
    421463            $used_layout,
    422464            $has_block_gap_support,
     
    433475    }
    434476
    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     );
    445 
    446     return $content;
     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    }
     518
     519    return (string) $content;
    447520}
    448521
Note: See TracChangeset for help on using the changeset viewer.