Make WordPress Core

Changeset 54305


Ignore:
Timestamp:
09/26/2022 09:06:02 AM (22 months ago)
Author:
jorgefilipecosta
Message:

Editor: Fix missing frontend section presets output.

Backports PHP changes in WordPress/gutenberg#42124 to the core. Adds the missing mechanism to output frontend styles of block level presets to the core.

Props mcsf, oandregal, dmsnell, draganescu.
See #56467.

Location:
trunk/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r54272 r54305  
    774774            }
    775775
    776             // Assign defaults, then overwrite those that the block sets by itself.
     776            // Assign defaults, then override those that the block sets by itself.
    777777            // If the block selector is compounded, will append the element to each
    778778            // individual block selector.
     
    867867     *                       - `variables`: only the CSS Custom Properties for presets & custom ones.
    868868     *                       - `styles`: only the styles section in theme.json.
    869      *                       - `presets`: only the classes for the presets.
    870      * @param array $origins A list of origins to include. By default it includes VALID_ORIGINS.
     869     *                       - `presets`: only the classes for the presets. @param array $origins A list of origins to include. By default it includes VALID_ORIGINS.
     870     * @param array $options An array of options for now used for internal purposes only (may change without notice).
     871     *                       The options currently supported are 'scope' that makes sure all style are scoped to a given selector,
     872     *                       and root_selector which overwrites and forces a given selector to be used on the root node.
    871873     * @return string Stylesheet.
    872874     */
    873     public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = null ) {
     875    public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' ), $origins = null, $options = array() ) {
    874876        if ( null === $origins ) {
    875877            $origins = static::VALID_ORIGINS;
     
    892894        $setting_nodes   = static::get_setting_nodes( $this->theme_json, $blocks_metadata );
    893895
     896        $root_style_key    = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true );
     897        $root_settings_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $setting_nodes, 'selector' ), true );
     898
     899        if ( ! empty( $options['scope'] ) ) {
     900            foreach ( $setting_nodes as &$node ) {
     901                $node['selector'] = static::scope_selector( $options['scope'], $node['selector'] );
     902            }
     903            foreach ( $style_nodes as &$node ) {
     904                $node['selector'] = static::scope_selector( $options['scope'], $node['selector'] );
     905            }
     906        }
     907
     908        if ( ! empty( $options['root_selector'] ) ) {
     909            if ( false !== $root_settings_key ) {
     910                $setting_nodes[ $root_settings_key ]['selector'] = $options['root_selector'];
     911            }
     912            if ( false !== $root_style_key ) {
     913                $setting_nodes[ $root_style_key ]['selector'] = $options['root_selector'];
     914            }
     915        }
     916
    894917        $stylesheet = '';
    895918
     
    899922
    900923        if ( in_array( 'styles', $types, true ) ) {
    901             $root_block_key = array_search( static::ROOT_BLOCK_SELECTOR, array_column( $style_nodes, 'selector' ), true );
    902 
    903             if ( false !== $root_block_key ) {
    904                 $stylesheet .= $this->get_root_layout_rules( static::ROOT_BLOCK_SELECTOR, $style_nodes[ $root_block_key ] );
     924            if ( false !== $root_style_key ) {
     925                $stylesheet .= $this->get_root_layout_rules( $style_nodes[ $root_style_key ]['selector'], $style_nodes[ $root_style_key ] );
    905926            }
    906927            $stylesheet .= $this->get_block_classes( $style_nodes );
    907928        } elseif ( in_array( 'base-layout-styles', $types, true ) ) {
     929            $root_selector    = static::ROOT_BLOCK_SELECTOR;
     930            $columns_selector = '.wp-block-columns';
     931            if ( ! empty( $options['scope'] ) ) {
     932                $root_selector    = static::scope_selector( $options['scope'], $root_selector );
     933                $columns_selector = static::scope_selector( $options['scope'], $columns_selector );
     934            }
     935            if ( ! empty( $options['root_selector'] ) ) {
     936                $root_selector = $options['root_selector'];
     937            }
    908938            // Base layout styles are provided as part of `styles`, so only output separately if explicitly requested.
    909939            // For backwards compatibility, the Columns block is explicitly included, to support a different default gap value.
     
    911941                array(
    912942                    'path'     => array( 'styles' ),
    913                     'selector' => static::ROOT_BLOCK_SELECTOR,
     943                    'selector' => $root_selector,
    914944                ),
    915945                array(
    916946                    'path'     => array( 'styles', 'blocks', 'core/columns' ),
    917                     'selector' => '.wp-block-columns',
     947                    'selector' => $columns_selector,
    918948                    'name'     => 'core/columns',
    919949                ),
     
    13661396     * @return string Scoped selector.
    13671397     */
    1368     protected static function scope_selector( $scope, $selector ) {
     1398    public static function scope_selector( $scope, $selector ) {
    13691399        $scopes    = explode( ',', $scope );
    13701400        $selectors = explode( ',', $selector );
     
    13731403        foreach ( $scopes as $outer ) {
    13741404            foreach ( $selectors as $inner ) {
    1375                 $selectors_scoped[] = trim( $outer ) . ' ' . trim( $inner );
    1376             }
    1377         }
    1378 
    1379         return implode( ', ', $selectors_scoped );
     1405                $outer = trim( $outer );
     1406                $inner = trim( $inner );
     1407                if ( ! empty( $outer ) && ! empty( $inner ) ) {
     1408                    $selectors_scoped[] = $outer . ' ' . $inner;
     1409                } elseif ( empty( $outer ) ) {
     1410                    $selectors_scoped[] = $inner;
     1411                } elseif ( empty( $inner ) ) {
     1412                    $selectors_scoped[] = $outer;
     1413                }
     1414            }
     1415        }
     1416
     1417        $result = implode( ', ', $selectors_scoped );
     1418        return $result;
    13801419    }
    13811420
  • trunk/src/wp-settings.php

    r54251 r54305  
    323323require ABSPATH . WPINC . '/class-wp-block-supports.php';
    324324require ABSPATH . WPINC . '/block-supports/utils.php';
     325require ABSPATH . WPINC . '/block-supports/settings.php';
    325326require ABSPATH . WPINC . '/block-supports/align.php';
    326327require ABSPATH . WPINC . '/block-supports/border.php';
Note: See TracChangeset for help on using the changeset viewer.