Make WordPress Core


Ignore:
Timestamp:
01/08/2024 06:12:00 AM (9 months ago)
Author:
isabel_brison
Message:

Editor: add layout classes to legacy Group inner container.

Moves generated layout classes into the Group block inner container in classic themes, so that block gap support can work correctly.

Props flixos90, mukesh27.
Fixes #60130.

File:
1 edited

Legend:

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

    r57077 r57246  
    639639     */
    640640    $container_class = wp_unique_prefixed_id(
    641         'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
     641        'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
    642642    );
    643643
     
    884884    }
    885885
    886     $replace_regex   = sprintf(
     886    /*
     887     * This filter runs after the layout classnames have been added to the block, so they
     888     * have to be removed from the outer wrapper and then added to the inner.
     889     */
     890    $layout_classes = array();
     891    $processor      = new WP_HTML_Tag_Processor( $block_content );
     892
     893    if ( $processor->next_tag( array( 'class_name' => 'wp-block-group' ) ) ) {
     894        foreach ( $processor->class_list() as $class_name ) {
     895            if ( str_contains( $class_name, 'is-layout-' ) ) {
     896                $layout_classes[] = $class_name;
     897                $processor->remove_class( $class_name );
     898            }
     899        }
     900    }
     901
     902    $content_without_layout_classes = $processor->get_updated_html();
     903    $replace_regex                  = sprintf(
    887904        '/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
    888905        preg_quote( $tag_name, '/' )
    889906    );
    890     $updated_content = preg_replace_callback(
     907    $updated_content                = preg_replace_callback(
    891908        $replace_regex,
    892909        static function ( $matches ) {
    893910            return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
    894911        },
    895         $block_content
     912        $content_without_layout_classes
    896913    );
     914
     915    // Add layout classes to inner wrapper.
     916    if ( ! empty( $layout_classes ) ) {
     917        $processor = new WP_HTML_Tag_Processor( $updated_content );
     918        if ( $processor->next_tag( array( 'class_name' => 'wp-block-group__inner-container' ) ) ) {
     919            foreach ( $layout_classes as $class_name ) {
     920                $processor->add_class( $class_name );
     921            }
     922        }
     923        $updated_content = $processor->get_updated_html();
     924    }
    897925    return $updated_content;
    898926}
Note: See TracChangeset for help on using the changeset viewer.