WordPress.org

Make WordPress Core

Changeset 50838


Ignore:
Timestamp:
05/11/2021 04:26:28 PM (7 months ago)
Author:
SergeyBiryukov
Message:

Editor: Some documentation and test improvements for loading separate assets for core blocks:

  • Move should_load_separate_core_block_assets() to a more appropriate place.
  • Update DocBlocks and inline comments per the documentation standards.
  • Document the $wp_styles global in wp_maybe_inline_styles().
  • List the expected result first in unit test assertions.
  • Remove a duplicate unit test.
  • Add missing @covers tags.

Follow-up to [50836], [50837].

See #50328, #52620, #53180.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r50837 r50838  
    13661366
    13671367/**
    1368  * Checks whether separate assets should be loaded for core blocks.
    1369  *
    1370  * @since 5.8
    1371  *
    1372  * @return bool
    1373  */
    1374 function should_load_separate_core_block_assets() {
    1375     if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
    1376         return false;
    1377     }
    1378     /**
    1379      * Determine if separate styles & scripts will be loaded for blocks on-render or not.
    1380      *
    1381      * @since 5.8.0
    1382      *
    1383      * @param bool $load_separate_styles Whether separate styles will be loaded or not.
    1384      *
    1385      * @return bool Whether separate styles will be loaded or not.
    1386      */
    1387     return apply_filters( 'separate_core_block_assets', false );
    1388 }
    1389 
    1390 /**
    13911368 * Assign default styles to $styles object.
    13921369 *
     
    22772254 * @since 5.6.0
    22782255 *
    2279  * @return bool
     2256 * @return bool Whether scripts and styles should be enqueued.
    22802257 */
    22812258function wp_should_load_block_editor_scripts_and_styles() {
     
    22852262
    22862263    /**
    2287      * Filters the flag that decides whether or not block editor scripts and
    2288      * styles are going to be enqueued on the current screen.
     2264     * Filters the flag that decides whether or not block editor scripts and styles
     2265     * are going to be enqueued on the current screen.
    22892266     *
    22902267     * @since 5.6.0
     
    22932270     */
    22942271    return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
     2272}
     2273
     2274/**
     2275 * Checks whether separate assets should be loaded for core blocks on-render.
     2276 *
     2277 * @since 5.8.0
     2278 *
     2279 * @return bool Whether separate assets will be loaded or not.
     2280 */
     2281function should_load_separate_core_block_assets() {
     2282    if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
     2283        return false;
     2284    }
     2285
     2286    /**
     2287     * Filters the flag that decides whether or not separate scripts and styles
     2288     * will be loaded for core blocks on-render or not.
     2289     *
     2290     * @since 5.8.0
     2291     *
     2292     * @param bool $load_separate_assets Whether separate assets will be loaded or not.
     2293     *                                   Default false.
     2294     */
     2295    return apply_filters( 'separate_core_block_assets', false );
    22952296}
    22962297
     
    25302531
    25312532/**
    2532  * Allow small styles to be inlined.
    2533  * This improves performance and sustainability, and is opt-in.
    2534  *
    2535  * Stylesheets can opt-in by adding `path` data using `wp_style_add_data`, and defining the file's absolute path.
    2536  * wp_style_add_data( $style_handle, 'path', $file_path );
     2533 * Allows small styles to be inlined.
     2534 *
     2535 * This improves performance and sustainability, and is opt-in. Stylesheets can opt in
     2536 * by adding `path` data using `wp_style_add_data`, and defining the file's absolute path:
     2537 *
     2538 *     wp_style_add_data( $style_handle, 'path', $file_path );
    25372539 *
    25382540 * @since 5.8.0
    25392541 *
    2540  * @return void
     2542 * @global WP_Styles $wp_styles
    25412543 */
    25422544function wp_maybe_inline_styles() {
     2545    global $wp_styles;
    25432546
    25442547    $total_inline_limit = 20000;
     
    25472550     *
    25482551     * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000.
    2549      * @return int                    The file-size threshold, in bytes.
    25502552     */
    25512553    $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit );
    25522554
    2553     global $wp_styles;
    25542555    $styles = array();
    25552556
     
    25742575        );
    25752576
    2576         /**
     2577        /*
    25772578         * The total inlined size.
    25782579         *
  • trunk/tests/phpunit/tests/blocks/register.php

    r50836 r50838  
    260260    /**
    261261     * @ticket 50263
     262     * @ticket 50328
    262263     */
    263264    function test_success_register_block_style_handle() {
     
    308309     *
    309310     * @ticket 50263
     311     * @ticket 50328
    310312     */
    311313    function test_block_registers_with_metadata_fixture() {
  • trunk/tests/phpunit/tests/dependencies/styles.php

    r50836 r50838  
    423423
    424424    /**
    425      * Tests that the main "style.css" file gets enqueued when the site doesn't opt-in to separate_core_block_assets.
     425     * Tests that the main "style.css" file gets enqueued when the site doesn't opt in to separate core block assets.
    426426     *
    427427     * @ticket 50263
    428      */
    429     function test_common_block_styles_for_viewing_without_split_styles() {
     428     *
     429     * @covers ::wp_default_styles
     430     */
     431    function test_block_styles_for_viewing_without_split_styles() {
    430432        add_filter( 'separate_core_block_assets', '__return_false' );
    431433        wp_default_styles( $GLOBALS['wp_styles'] );
    432434
    433435        $this->assertSame(
    434             $GLOBALS['wp_styles']->registered['wp-block-library']->src,
    435             '/' . WPINC . '/css/dist/block-library/style.css'
     436            '/' . WPINC . '/css/dist/block-library/style.css',
     437            $GLOBALS['wp_styles']->registered['wp-block-library']->src
    436438        );
    437439    }
    438440
    439441    /**
    440      * Tests that the "common.css" file gets enqueued when the site opts-in to separate_core_block_assets.
     442     * Tests that the "common.css" file gets enqueued when the site opts in to separate core block assets.
    441443     *
    442444     * @ticket 50263
    443      */
    444     function test_common_block_styles_for_viewing_with_split_styles() {
    445         add_filter( 'separate_core_block_assets', '__return_false' );
    446         wp_default_styles( $GLOBALS['wp_styles'] );
    447 
    448         $this->assertSame(
    449             $GLOBALS['wp_styles']->registered['wp-block-library']->src,
    450             '/' . WPINC . '/css/dist/block-library/style.css'
    451         );
    452     }
    453 
     445     *
     446     * @covers ::wp_default_styles
     447     */
    454448    function test_block_styles_for_viewing_with_split_styles() {
    455449        add_filter( 'separate_core_block_assets', '__return_true' );
     
    457451
    458452        $this->assertSame(
    459             $GLOBALS['wp_styles']->registered['wp-block-library']->src,
    460             '/' . WPINC . '/css/dist/block-library/common.css'
     453            '/' . WPINC . '/css/dist/block-library/common.css',
     454            $GLOBALS['wp_styles']->registered['wp-block-library']->src
    461455        );
    462456    }
Note: See TracChangeset for help on using the changeset viewer.