From 96f963a2b9b8dd8cc8340df7e1432decc15134a7 Mon Sep 17 00:00:00 2001 From: noisysocks Date: Thu, 1 Oct 2020 00:39:04 +0000 Subject: [PATCH] Editor: Add should_load_block_editor_scripts_and_styles Adds a new should_load_block_editor_scripts_and_styles filter which can be used by plugins including Gutenberg to more precisely customise when block editor scripts and styles should be loaded by script-loader.php. Previously, plugins had to fiddle with $current_screen->is_block_editor(). Props zieladam. See #51330. Built from https://develop.svn.wordpress.org/trunk@49080 git-svn-id: http://core.svn.wordpress.org/trunk@48842 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/script-loader.php | 34 ++++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 26f0a56782..72cc45d8a0 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2160,9 +2160,7 @@ function script_concat_settings() { * @global WP_Screen $current_screen WordPress current screen object. */ function wp_common_block_scripts_and_styles() { - global $current_screen; - - if ( is_admin() && ( $current_screen instanceof WP_Screen ) && ! $current_screen->is_block_editor() ) { + if ( is_admin() && ! _should_load_block_editor_scripts_and_styles() ) { return; } @@ -2185,6 +2183,30 @@ function wp_common_block_scripts_and_styles() { do_action( 'enqueue_block_assets' ); } +/** + * Checks if the editor scripts and styles for all registered block types + * should be enqueued on the current screen. + * + * @access private + * + * @return boolean + */ +function _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. + * + * @since 5.6.0 + * + * @param boolean $is_block_editor_screen Current value of the flag + */ + return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen ); +} + /** * Enqueues registered block scripts and styles, depending on current rendered * context (only enqueuing editor scripts while in context of the editor). @@ -2196,7 +2218,7 @@ function wp_common_block_scripts_and_styles() { function wp_enqueue_registered_block_scripts_and_styles() { global $current_screen; - $is_editor = ( ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor() ); + $load_editor_scripts = _should_load_block_editor_scripts_and_styles(); $block_registry = WP_Block_Type_Registry::get_instance(); foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { @@ -2211,12 +2233,12 @@ function wp_enqueue_registered_block_scripts_and_styles() { } // Editor styles. - if ( $is_editor && ! empty( $block_type->editor_style ) ) { + if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) { wp_enqueue_style( $block_type->editor_style ); } // Editor script. - if ( $is_editor && ! empty( $block_type->editor_script ) ) { + if ( $load_editor_scripts && ! empty( $block_type->editor_script ) ) { wp_enqueue_script( $block_type->editor_script ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 9bcc338b01..99493cdc0b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49079'; +$wp_version = '5.6-alpha-49080'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.