Make WordPress Core

Changeset 52745


Ignore:
Timestamp:
02/17/2022 09:09:12 AM (3 years ago)
Author:
ocean90
Message:

Editor: Prevent front-end assets of a block from being enqueued in the block editor.

Since WordPress 5.9 you can set a view script for a block which is supposed to be only loaded on the front end. Unfortunately it's currently also loaded in the editor which can cause unexpected behaviour and also performance issues depending on the size of the scripts.
This is caused by the preloading of REST API routes via block_editor_rest_api_preload() which doesn't happen in an encapsulated process and so does pollute any global state like the one for scripts and styles.
Similar to the global $post, core now backups the globals $wp_scripts and $wp_styles and restores the backup after the preloading.

Props gziolo, ocean90.
Merges [52733] to the 5.9 branch.
Fixes #55151.

Location:
branches/5.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.9

  • branches/5.9/src/wp-includes/block-editor.php

    r52364 r52745  
    426426 * @since 5.8.0
    427427 *
    428  * @global WP_Post $post Global post object.
     428 * @global WP_Post    $post       Global post object.
     429 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     430 * @global WP_Styles  $wp_styles  The WP_Styles object for printing styles.
    429431 *
    430432 * @param string[]                $preload_paths        List of paths to preload.
     
    434436 */
    435437function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {
    436     global $post;
     438    global $post, $wp_scripts, $wp_styles;
    437439
    438440    /**
     
    468470
    469471    /*
    470      * Ensure the global $post remains the same after API data is preloaded.
     472     * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after
     473     * API data is preloaded.
    471474     * Because API preloading can call the_content and other filters, plugins
    472      * can unexpectedly modify $post.
     475     * can unexpectedly modify the global $post or enqueue assets which are not
     476     * intended for the block editor.
    473477     */
    474478    $backup_global_post = ! empty( $post ) ? clone $post : $post;
     479    $backup_wp_scripts  = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;
     480    $backup_wp_styles   = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles;
    475481
    476482    foreach ( $preload_paths as &$path ) {
     
    481487
    482488        if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {
    483                 $path[0] = '/' . $path[0];
     489            $path[0] = '/' . $path[0];
    484490        }
    485491    }
     
    493499    );
    494500
    495     // Restore the global $post as it was before API preloading.
    496     $post = $backup_global_post;
     501    // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.
     502    $post       = $backup_global_post;
     503    $wp_scripts = $backup_wp_scripts;
     504    $wp_styles  = $backup_wp_styles;
    497505
    498506    wp_add_inline_script(
Note: See TracChangeset for help on using the changeset viewer.