From 9a7e62485724056199b83a7d7ccce56d48ef5060 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Thu, 17 Feb 2022 09:09:12 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/branches/5.9@52745 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-editor.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index f175eff85dc64..185dad1ecb42d 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -425,7 +425,9 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex * * @since 5.8.0 * - * @global WP_Post $post Global post object. + * @global WP_Post $post Global post object. + * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. + * @global WP_Styles $wp_styles The WP_Styles object for printing styles. * * @param string[] $preload_paths List of paths to preload. * @param WP_Block_Editor_Context $block_editor_context The current block editor context. @@ -433,7 +435,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex * @return void */ function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) { - global $post; + global $post, $wp_scripts, $wp_styles; /** * Filters the array of REST API paths that will be used to preloaded common data for the block editor. @@ -467,11 +469,15 @@ function block_editor_rest_api_preload( array $preload_paths, $block_editor_cont } /* - * Ensure the global $post remains the same after API data is preloaded. + * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after + * API data is preloaded. * Because API preloading can call the_content and other filters, plugins - * can unexpectedly modify $post. + * can unexpectedly modify the global $post or enqueue assets which are not + * intended for the block editor. */ $backup_global_post = ! empty( $post ) ? clone $post : $post; + $backup_wp_scripts = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts; + $backup_wp_styles = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; foreach ( $preload_paths as &$path ) { if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) { @@ -480,7 +486,7 @@ function block_editor_rest_api_preload( array $preload_paths, $block_editor_cont } if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) { - $path[0] = '/' . $path[0]; + $path[0] = '/' . $path[0]; } } @@ -492,8 +498,10 @@ function block_editor_rest_api_preload( array $preload_paths, $block_editor_cont array() ); - // Restore the global $post as it was before API preloading. - $post = $backup_global_post; + // Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading. + $post = $backup_global_post; + $wp_scripts = $backup_wp_scripts; + $wp_styles = $backup_wp_styles; wp_add_inline_script( 'wp-api-fetch',