Skip to content

Commit

Permalink
Fix global styles loading logic (WordPress#38745)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal authored Feb 11, 2022
1 parent 0da1f33 commit 88671f7
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions lib/compat/wordpress-5.9/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,44 @@
* @package gutenberg
*/

/*
/**
* This code lives in script-loader.php
* where we just load styles using wp_get_global_stylesheet.
*/
if ( ! function_exists( 'wp_get_global_styles' ) ) {
/**
* Fetches the preferences for each origin (core, theme, user)
* and enqueues the resulting stylesheet.
function gutenberg_enqueue_global_styles_assets() {
$separate_assets = wp_should_load_separate_core_block_assets();
$is_block_theme = wp_is_block_theme();
$is_classic_theme = ! $is_block_theme;

/*
* 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.
*/
function gutenberg_enqueue_global_styles_assets() {
$stylesheet = wp_get_global_stylesheet();
if ( empty( $stylesheet ) ) {
return;
}
if (
( $is_block_theme && doing_action( 'wp_footer' ) ) ||
( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
) {
return;
}

$stylesheet = wp_get_global_stylesheet();

if ( empty( $stylesheet ) ) {
return;
}

if ( isset( wp_styles()->registered['global-styles'] ) ) {
// We're in WordPress 5.8, so we overwrite the existing.
wp_styles()->registered['global-styles']->extra['after'][0] = $stylesheet;
} else {
// WordPress 5.7 or lower.
wp_register_style( 'global-styles', false, array(), true, true );
wp_add_inline_style( 'global-styles', $stylesheet );
wp_enqueue_style( 'global-styles' );
}
if ( isset( wp_styles()->registered['global-styles'] ) ) {
// There's a GS stylesheet (theme has theme.json), so we overwrite it.
wp_styles()->registered['global-styles']->extra['after'][0] = $stylesheet;
} else {
// No GS stylesheet (theme has no theme.json), so we enqueue a new one.
wp_register_style( 'global-styles', false, array(), true, true );
wp_add_inline_style( 'global-styles', $stylesheet );
wp_enqueue_style( 'global-styles' );
}
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_assets' );
}
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_assets' );
add_action( 'wp_footer', 'gutenberg_enqueue_global_styles_assets' );

0 comments on commit 88671f7

Please sign in to comment.