Make WordPress Core

Changeset 54214


Ignore:
Timestamp:
09/19/2022 08:54:20 PM (22 months ago)
Author:
audrasjb
Message:

Editor: Backport block supports filter callback, registrations and tests to 6.1.

This changeset backports the following changes:

Props ramonopoly, gziolo, bernhard-reiter, audrasjb, costdev.
See #56467.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r54182 r54214  
    576576add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
    577577
     578// Block supports, and other styles parsed and stored in the Style Engine.
     579add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
     580add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
     581
    578582// SVG filters like duotone have to be loaded at the beginning of the body in both admin and the front-end.
    579583add_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
  • trunk/src/wp-includes/script-loader.php

    r54209 r54214  
    29682968
    29692969/**
     2970 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
     2971 * Styles are stored via the style engine API.
     2972 *
     2973 * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
     2974 *
     2975 * @since 6.1.0
     2976 *
     2977 * @param array $options {
     2978 *     Optional. An array of options to pass to wp_style_engine_get_stylesheet_from_context(). Default empty array.
     2979 *
     2980 *     @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
     2981 *     @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
     2982 * }
     2983 *
     2984 * @return void
     2985 */
     2986function wp_enqueue_stored_styles( $options = array() ) {
     2987    $is_block_theme   = wp_is_block_theme();
     2988    $is_classic_theme = ! $is_block_theme;
     2989
     2990    /*
     2991     * For block themes, this function prints stored styles in the header.
     2992     * For classic themes, in the footer.
     2993     */
     2994    if (
     2995        ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
     2996        ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) )
     2997    ) {
     2998        return;
     2999    }
     3000
     3001    $core_styles_keys         = array( 'block-supports' );
     3002    $compiled_core_stylesheet = '';
     3003    $style_tag_id             = 'core';
     3004    // Adds comment if code is prettified to identify core styles sections in debugging.
     3005    $should_prettify          = isset( $options['prettify'] ) ? true === $options['prettify'] : defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
     3006    foreach ( $core_styles_keys as $style_key ) {
     3007        if ( $should_prettify ) {
     3008            $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n";
     3009        }
     3010        // Chains core store ids to signify what the styles contain.
     3011        $style_tag_id             .= '-' . $style_key;
     3012        $compiled_core_stylesheet .= wp_style_engine_get_stylesheet_from_context( $style_key, $options );
     3013    }
     3014
     3015    // Combines Core styles.
     3016    if ( ! empty( $compiled_core_stylesheet ) ) {
     3017        wp_register_style( $style_tag_id, false, array(), true, true );
     3018        wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
     3019        wp_enqueue_style( $style_tag_id );
     3020    }
     3021
     3022    // Prints out any other stores registered by themes or otherwise.
     3023    $additional_stores = WP_Style_Engine_CSS_Rules_Store::get_stores();
     3024    foreach ( array_keys( $additional_stores ) as $store_name ) {
     3025        if ( in_array( $store_name, $core_styles_keys, true ) ) {
     3026            continue;
     3027        }
     3028        $styles = wp_style_engine_get_stylesheet_from_context( $store_name, $options );
     3029        if ( ! empty( $styles ) ) {
     3030            $key = "wp-style-engine-$store_name";
     3031            wp_register_style( $key, false, array(), true, true );
     3032            wp_add_inline_style( $key, $styles );
     3033            wp_enqueue_style( $key );
     3034        }
     3035    }
     3036}
     3037
     3038/**
    29703039 * Enqueues a stylesheet for a specific block.
    29713040 *
  • trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php

    r53916 r54214  
    5555    }
    5656
     57    /**
     58     * Cleans up global scope.
     59     *
     60     * @global WP_Styles $wp_styles
     61     */
     62    public function clean_up_global_scope() {
     63        global $wp_styles;
     64        parent::clean_up_global_scope();
     65        $wp_styles = null;
     66    }
     67
    5768    public function filter_set_theme_root() {
    5869        return $this->theme_root;
     
    200211    }
    201212
     213    /**
     214     * Tests that stored CSS is enqueued.
     215     *
     216     * @ticket 56467
     217     *
     218     * @covers ::wp_enqueue_stored_styles
     219     */
     220    public function test_should_enqueue_stored_styles() {
     221        $core_styles_to_enqueue = array(
     222            array(
     223                'selector'     => '.saruman',
     224                'declarations' => array(
     225                    'color'        => 'white',
     226                    'height'       => '100px',
     227                    'border-style' => 'solid',
     228                ),
     229            ),
     230        );
     231
     232        // Enqueues a block supports (core styles).
     233        wp_style_engine_get_stylesheet_from_css_rules(
     234            $core_styles_to_enqueue,
     235            array(
     236                'context' => 'block-supports',
     237            )
     238        );
     239
     240        $my_styles_to_enqueue = array(
     241            array(
     242                'selector'     => '.gandalf',
     243                'declarations' => array(
     244                    'color'        => 'grey',
     245                    'height'       => '90px',
     246                    'border-style' => 'dotted',
     247                ),
     248            ),
     249        );
     250
     251        // Enqueues some other styles.
     252        wp_style_engine_get_stylesheet_from_css_rules(
     253            $my_styles_to_enqueue,
     254            array(
     255                'context' => 'my-styles',
     256            )
     257        );
     258
     259        wp_enqueue_stored_styles( array( 'prettify' => false ) );
     260
     261        $this->assertSame(
     262            array( '.saruman{color:white;height:100px;border-style:solid;}' ),
     263            wp_styles()->registered['core-block-supports']->extra['after'],
     264            'Registered styles with handle of "core-block-supports" do not match expected value from Style Engine store.'
     265        );
     266
     267        $this->assertSame(
     268            array( '.gandalf{color:grey;height:90px;border-style:dotted;}' ),
     269            wp_styles()->registered['wp-style-engine-my-styles']->extra['after'],
     270            'Registered styles with handle of "wp-style-engine-my-styles" do not match expected value from the Style Engine store.'
     271        );
     272    }
    202273}
Note: See TracChangeset for help on using the changeset viewer.