From bab911f0d74fcfed6f2258c682ad2d2eb38c53d0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 11 May 2021 16:26:28 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@50838 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/script-loader.php | 69 +++++++++++---------- tests/phpunit/tests/blocks/register.php | 2 + tests/phpunit/tests/dependencies/styles.php | 28 ++++----- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 6558159020de..515622825a0c 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1364,29 +1364,6 @@ function wp_default_scripts( $scripts ) { } } -/** - * Checks whether separate assets should be loaded for core blocks. - * - * @since 5.8 - * - * @return bool - */ -function should_load_separate_core_block_assets() { - if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { - return false; - } - /** - * Determine if separate styles & scripts will be loaded for blocks on-render or not. - * - * @since 5.8.0 - * - * @param bool $load_separate_styles Whether separate styles will be loaded or not. - * - * @return bool Whether separate styles will be loaded or not. - */ - return apply_filters( 'separate_core_block_assets', false ); -} - /** * Assign default styles to $styles object. * @@ -2276,7 +2253,7 @@ function wp_common_block_scripts_and_styles() { * * @since 5.6.0 * - * @return bool + * @return bool Whether scripts and styles should be enqueued. */ function wp_should_load_block_editor_scripts_and_styles() { global $current_screen; @@ -2284,8 +2261,8 @@ function wp_should_load_block_editor_scripts_and_styles() { $is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor(); /** - * Filters the flag that decides whether or not block editor scripts and - * styles are going to be enqueued on the current screen. + * Filters the flag that decides whether or not block editor scripts and styles + * are going to be enqueued on the current screen. * * @since 5.6.0 * @@ -2294,6 +2271,30 @@ function wp_should_load_block_editor_scripts_and_styles() { return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen ); } +/** + * Checks whether separate assets should be loaded for core blocks on-render. + * + * @since 5.8.0 + * + * @return bool Whether separate assets will be loaded or not. + */ +function should_load_separate_core_block_assets() { + if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + return false; + } + + /** + * Filters the flag that decides whether or not separate scripts and styles + * will be loaded for core blocks on-render or not. + * + * @since 5.8.0 + * + * @param bool $load_separate_assets Whether separate assets will be loaded or not. + * Default false. + */ + return apply_filters( 'separate_core_block_assets', false ); +} + /** * Enqueues registered block scripts and styles, depending on current rendered * context (only enqueuing editor scripts while in context of the editor). @@ -2529,28 +2530,28 @@ function wp_print_inline_script_tag( $javascript, $attributes = array() ) { } /** - * Allow small styles to be inlined. - * This improves performance and sustainability, and is opt-in. + * Allows small styles to be inlined. + * + * This improves performance and sustainability, and is opt-in. Stylesheets can opt in + * by adding `path` data using `wp_style_add_data`, and defining the file's absolute path: * - * Stylesheets can opt-in by adding `path` data using `wp_style_add_data`, and defining the file's absolute path. - * wp_style_add_data( $style_handle, 'path', $file_path ); + * wp_style_add_data( $style_handle, 'path', $file_path ); * * @since 5.8.0 * - * @return void + * @global WP_Styles $wp_styles */ function wp_maybe_inline_styles() { + global $wp_styles; $total_inline_limit = 20000; /** * The maximum size of inlined styles in bytes. * * @param int $total_inline_limit The file-size threshold, in bytes. Defaults to 20000. - * @return int The file-size threshold, in bytes. */ $total_inline_limit = apply_filters( 'styles_inline_size_limit', $total_inline_limit ); - global $wp_styles; $styles = array(); // Build an array of styles that have a path defined. @@ -2573,7 +2574,7 @@ function( $a, $b ) { } ); - /** + /* * The total inlined size. * * On each iteration of the loop, if a style gets added inline the value of this var increases diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 8369a8b52418..5f8baec88b9f 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -259,6 +259,7 @@ function test_handle_passed_register_block_style_handle() { /** * @ticket 50263 + * @ticket 50328 */ function test_success_register_block_style_handle() { $metadata = array( @@ -307,6 +308,7 @@ function test_metadata_not_found_in_the_current_directory() { * is found in the fixtures directory. * * @ticket 50263 + * @ticket 50328 */ function test_block_registers_with_metadata_fixture() { $result = register_block_type_from_metadata( diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index c02a8cc7dd42..c3b04bbdac25 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -422,42 +422,36 @@ function test_block_styles_for_viewing_with_theme_support() { } /** - * Tests that the main "style.css" file gets enqueued when the site doesn't opt-in to separate_core_block_assets. + * Tests that the main "style.css" file gets enqueued when the site doesn't opt in to separate core block assets. * * @ticket 50263 + * + * @covers ::wp_default_styles */ - function test_common_block_styles_for_viewing_without_split_styles() { + function test_block_styles_for_viewing_without_split_styles() { add_filter( 'separate_core_block_assets', '__return_false' ); wp_default_styles( $GLOBALS['wp_styles'] ); $this->assertSame( - $GLOBALS['wp_styles']->registered['wp-block-library']->src, - '/' . WPINC . '/css/dist/block-library/style.css' + '/' . WPINC . '/css/dist/block-library/style.css', + $GLOBALS['wp_styles']->registered['wp-block-library']->src ); } /** - * Tests that the "common.css" file gets enqueued when the site opts-in to separate_core_block_assets. + * Tests that the "common.css" file gets enqueued when the site opts in to separate core block assets. * * @ticket 50263 + * + * @covers ::wp_default_styles */ - function test_common_block_styles_for_viewing_with_split_styles() { - add_filter( 'separate_core_block_assets', '__return_false' ); - wp_default_styles( $GLOBALS['wp_styles'] ); - - $this->assertSame( - $GLOBALS['wp_styles']->registered['wp-block-library']->src, - '/' . WPINC . '/css/dist/block-library/style.css' - ); - } - function test_block_styles_for_viewing_with_split_styles() { add_filter( 'separate_core_block_assets', '__return_true' ); wp_default_styles( $GLOBALS['wp_styles'] ); $this->assertSame( - $GLOBALS['wp_styles']->registered['wp-block-library']->src, - '/' . WPINC . '/css/dist/block-library/common.css' + '/' . WPINC . '/css/dist/block-library/common.css', + $GLOBALS['wp_styles']->registered['wp-block-library']->src ); } }