Skip to content

Commit

Permalink
Editor: Some documentation and test improvements for loading separate…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
SergeyBiryukov committed May 11, 2021
1 parent 7f01308 commit bab911f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
69 changes: 35 additions & 34 deletions src/wp-includes/script-loader.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -2276,16 +2253,16 @@ 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;

$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
*
Expand All @@ -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).
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
28 changes: 11 additions & 17 deletions tests/phpunit/tests/dependencies/styles.php
Expand Up @@ -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
);
}
}

0 comments on commit bab911f

Please sign in to comment.