From 8477f505be84c1052e52a5fa008aa1eb3326bc42 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 2 Jul 2021 18:46:58 +0000 Subject: [PATCH] Editor: Ensure global styles are loaded in the footer when loading core assets individually. This fixes the logic in `wp_enqueue_global_styles()` to ensure that global styles are loaded in the footer when a site opts-in to loading Core block assets individually. This fixes a bug where styles defined in `themes.json` are not respected. Props walbo, nosolosw, mcsf, aristath, desrosj. Fixes #53494. git-svn-id: https://develop.svn.wordpress.org/trunk@51309 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 5 ++++- src/wp-includes/script-loader.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 0295e7d7e2..068ca78b4d 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -554,7 +554,6 @@ add_action( 'wp_default_scripts', 'wp_default_packages' ); add_action( 'wp_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 ); add_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); -add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 ); add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' ); @@ -568,6 +567,10 @@ add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' ); add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' ); add_filter( 'customize_controls_print_styles', 'wp_resource_hints', 1 ); +// Global styles can be enqueued in both the header and the footer. See https://core.trac.wordpress.org/ticket/53494. +add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); +add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 ); + add_action( 'wp_default_styles', 'wp_default_styles' ); add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 ); diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 23fe1166f7..d517cac8be 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2267,6 +2267,18 @@ function wp_enqueue_global_styles() { return; } + $separate_assets = wp_should_load_separate_core_block_assets(); + + /* + * Global styles should be printed in the head when loading all styles combined. + * The footer should only be used to print global styles for classic themes with separate core assets enabled. + * + * See https://core.trac.wordpress.org/ticket/53494. + */ + if ( ( ! $separate_assets && doing_action( 'wp_footer' ) ) || ( $separate_assets && doing_action( 'wp_enqueue_scripts' ) ) ) { + return; + } + $can_use_cache = ( ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&