Skip to content

Commit

Permalink
Plugin: Copy wp_should_load_separate_core_block_assets function fro…
Browse files Browse the repository at this point in the history
…m core (#32611)

* copy function from core

* add @todo note.
  • Loading branch information
aristath authored Jun 11, 2021
1 parent b933da3 commit 91b8c56
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function gutenberg_reregister_core_block_types() {
* @return void
*/
function gutenberg_register_core_block_styles( $block_name ) {
if ( ! gutenberg_should_load_separate_block_assets() ) {
if ( ! wp_should_load_separate_core_block_assets() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function gutenberg_register_packages_styles( $styles ) {
);
$styles->add_data( 'wp-components', 'rtl', 'replace' );

$block_library_filename = gutenberg_should_load_separate_block_assets() ? 'common' : 'style';
$block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
gutenberg_override_style(
$styles,
'wp-block-library',
Expand Down
42 changes: 22 additions & 20 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@
*/

/**
* Determine if the current theme needs to load separate block styles or not.
* Backporting wp_should_load_separate_core_block_assets from WP-Core.
*
* @todo Remove this function when the minimum supported version is WordPress 5.8.
*
* @return bool
*/
function gutenberg_should_load_separate_block_assets() {
if ( function_exists( 'wp_should_load_separate_core_block_assets' ) ) {
return wp_should_load_separate_core_block_assets();
}

if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return false;
}

// The `should_load_separate_core_block_assets` filter was added in WP 5.8.
$load_separate_styles = apply_filters( 'should_load_separate_core_block_assets', gutenberg_is_fse_theme() );

if ( ! function_exists( 'wp_should_load_separate_core_block_assets' ) ) {
/**
* Determine if separate styles will be loaded for blocks on-render or not.
* Checks whether separate assets should be loaded for core blocks on-render.
*
* @param bool $load_separate_styles Whether separate styles will be loaded or not.
* @since 5.8.0
*
* @return bool
* @return bool Whether separate assets will be loaded.
*/
return apply_filters( 'load_separate_block_assets', $load_separate_styles );
function wp_should_load_separate_core_block_assets() {
if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return false;
}

/**
* Filters the flag that decides whether separate scripts and styles
* will be loaded for core blocks on-render.
*
* @since 5.8.0
*
* @param bool $load_separate_assets Whether separate assets will be loaded.
* Default false.
*/
return apply_filters( 'should_load_separate_core_block_assets', false );
}
}

/**
Expand All @@ -58,7 +60,7 @@ function( $load_separate_styles ) {
* @return void
*/
function gutenberg_remove_hook_wp_enqueue_registered_block_scripts_and_styles() {
if ( gutenberg_should_load_separate_block_assets() ) {
if ( wp_should_load_separate_core_block_assets() ) {
/**
* Avoid enqueueing block assets of all registered blocks for all posts, instead
* deferring to block render mechanics to enqueue scripts, thereby ensuring only
Expand Down

0 comments on commit 91b8c56

Please sign in to comment.