Make WordPress Core

Changeset 49080


Ignore:
Timestamp:
10/01/2020 12:37:54 AM (4 years ago)
Author:
noisysocks
Message:

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.

File:
1 edited

Legend:

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

    r49079 r49080  
    21612161 */
    21622162function wp_common_block_scripts_and_styles() {
    2163     global $current_screen;
    2164 
    2165     if ( is_admin() && ( $current_screen instanceof WP_Screen ) && ! $current_screen->is_block_editor() ) {
     2163    if ( is_admin() && ! _should_load_block_editor_scripts_and_styles() ) {
    21662164        return;
    21672165    }
     
    21872185
    21882186/**
     2187 * Checks if the editor scripts and styles for all registered block types
     2188 * should be enqueued on the current screen.
     2189 *
     2190 * @access private
     2191 *
     2192 * @return boolean
     2193 */
     2194function _should_load_block_editor_scripts_and_styles() {
     2195    global $current_screen;
     2196
     2197    $is_block_editor_screen = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();
     2198
     2199    /**
     2200     * Filters the flag that decides whether or not block editor scripts and
     2201     * styles are going to be enqueued on the current screen.
     2202     *
     2203     * @since 5.6.0
     2204     *
     2205     * @param boolean $is_block_editor_screen Current value of the flag
     2206     */
     2207    return apply_filters( 'should_load_block_editor_scripts_and_styles', $is_block_editor_screen );
     2208}
     2209
     2210/**
    21892211 * Enqueues registered block scripts and styles, depending on current rendered
    21902212 * context (only enqueuing editor scripts while in context of the editor).
     
    21972219    global $current_screen;
    21982220
    2199     $is_editor = ( ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor() );
     2221    $load_editor_scripts = _should_load_block_editor_scripts_and_styles();
    22002222
    22012223    $block_registry = WP_Block_Type_Registry::get_instance();
     
    22122234
    22132235        // Editor styles.
    2214         if ( $is_editor && ! empty( $block_type->editor_style ) ) {
     2236        if ( $load_editor_scripts && ! empty( $block_type->editor_style ) ) {
    22152237            wp_enqueue_style( $block_type->editor_style );
    22162238        }
    22172239
    22182240        // Editor script.
    2219         if ( $is_editor && ! empty( $block_type->editor_script ) ) {
     2241        if ( $load_editor_scripts && ! empty( $block_type->editor_script ) ) {
    22202242            wp_enqueue_script( $block_type->editor_script );
    22212243        }
Note: See TracChangeset for help on using the changeset viewer.