Make WordPress Core


Ignore:
Timestamp:
09/14/2022 02:19:16 PM (2 years ago)
Author:
audrasjb
Message:

Editor: Backport foundation for Layout block support refactor (part 1).

This change backports the following changes from Gutenberg repository:

  • [WordPress/gutenberg#40875 gutenberg#40875] Layout: Use semantic classnames, centralize layout definitions, reduce duplication, and fix blockGap in theme.json
  • [WordPress/gutenberg#42544 gutenberg#42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg#42544
  • [WordPress/gutenberg#42087 gutenberg#42087] Theme.json: Add block support feature level selectors for blocks gutenberg#42087
  • [WordPress/gutenberg#43792 gutenberg#43792] Global Styles: Split root layout rules into a different function gutenberg#43792
  • [WordPress/gutenberg#42544 gutenberg#42544] Layout: Add a disable-layout-styles theme supports flag to opt out of all layout styles gutenberg#42544
  • [WordPress/gutenberg#42665 gutenberg#42665] Layout: Reduce specificity of fallback blockGap styles gutenberg#42665
  • [WordPress/gutenberg#42085 gutenberg#42085] Core CSS support for root padding and alignfull blocks gutenberg#42085

Note that it doesn't entirely port over PR40875 — the remaining PHP changes for that PR will be explored in a separate PR targeting layout.php.

Props andrewserong, aaronrobertshaw, isabel_brison.
See #56467.

File:
1 edited

Legend:

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

    r54133 r54159  
    231231
    232232        return $with_theme_supports;
     233    }
     234
     235    /**
     236     * Gets the styles for blocks from the block.json file.
     237     *
     238     * @since 6.1.0
     239     *
     240     * @return WP_Theme_JSON
     241     */
     242    public static function get_block_data() {
     243        $registry = WP_Block_Type_Registry::get_instance();
     244        $blocks   = $registry->get_all_registered();
     245        $config   = array( 'version' => 1 );
     246        foreach ( $blocks as $block_name => $block_type ) {
     247            if ( isset( $block_type->supports['__experimentalStyle'] ) ) {
     248                $config['styles']['blocks'][ $block_name ] = static::remove_json_comments( $block_type->supports['__experimentalStyle'] );
     249            }
     250
     251            if (
     252                isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] ) &&
     253                null === _wp_array_get( $config, array( 'styles', 'blocks', $block_name, 'spacing', 'blockGap' ), null )
     254            ) {
     255                // Ensure an empty placeholder value exists for the block, if it provides a default blockGap value.
     256                // The real blockGap value to be used will be determined when the styles are rendered for output.
     257                $config['styles']['blocks'][ $block_name ]['spacing']['blockGap'] = null;
     258            }
     259        }
     260
     261        // Core here means it's the lower level part of the styles chain.
     262        // It can be a core or a third-party block.
     263        return new WP_Theme_JSON( $config, 'core' );
     264    }
     265
     266    /**
     267     * When given an array, this will remove any keys with the name `//`.
     268     *
     269     * @param array $array The array to filter.
     270     * @return array The filtered array.
     271     */
     272    private static function remove_json_comments( $array ) {
     273        unset( $array['//'] );
     274        foreach ( $array as $k => $v ) {
     275            if ( is_array( $v ) ) {
     276                $array[ $k ] = static::remove_json_comments( $v );
     277            }
     278        }
     279
     280        return $array;
    233281    }
    234282
     
    371419     * @since 5.9.0 Added user data, removed the `$settings` parameter,
    372420     *              added the `$origin` parameter.
     421     * @since 6.1.0 Added block data.
    373422     *
    374423     * @param string $origin Optional. To what level should we merge data.
     
    383432        $result = new WP_Theme_JSON();
    384433        $result->merge( static::get_core_data() );
     434        $result->merge( static::get_block_data() );
    385435        $result->merge( static::get_theme_data() );
    386436
Note: See TracChangeset for help on using the changeset viewer.