PluginProbe
Gutenberg / 22.4.3
Gutenberg v22.4.3
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / script-loader.php

script-loader.php in Gutenberg 22.4.3, at lib/script-loader.php

134 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Overrides the script-loader.php file.
4 *
5 * @package gutenberg
6 */
7
8 // Remove core actions to override.
9 remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
10 remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
11
12 /**
13 * Enqueues the global styles defined via theme.json.
14 *
15 * Copy of the core `wp_enqueue_global_styles`. Uses helper methods bundled with the plugin.
16 *
17 * @return void
18 */
19 function gutenberg_enqueue_global_styles() {
20 $separate_assets = wp_should_load_separate_core_block_assets();
21 $is_block_theme = wp_is_block_theme();
22 $is_classic_theme = ! $is_block_theme;
23
24 /*
25 * Global styles should be printed in the head when loading all styles combined.
26 * The footer should only be used to print global styles for classic themes with separate core assets enabled.
27 *
28 * See https://core.trac.wordpress.org/ticket/53494.
29 */
30 if (
31 ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
32 ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
33 ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
34 ) {
35 return;
36 }
37
38 /*
39 * If loading the CSS for each block separately, then load the theme.json CSS conditionally.
40 * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
41 * This filter must be registered before calling wp_get_global_stylesheet();
42 */
43 add_filter( 'wp_theme_json_get_style_nodes', 'wp_filter_out_block_nodes' );
44
45 $stylesheet = gutenberg_get_global_stylesheet();
46
47 /*
48 * For block themes, merge Customizer's custom CSS into the global styles stylesheet
49 * before the global styles custom CSS, ensuring proper cascade order.
50 * For classic themes, let the Customizer CSS print separately via wp_custom_css_cb()
51 * at priority 101 in wp_head, preserving its position at the end of the <head>.
52 */
53 if ( $is_block_theme ) {
54 /*
55 * Dequeue the Customizer's custom CSS
56 * and add it before the global styles custom CSS.
57 */
58 remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
59
60 /*
61 * Get the custom CSS from the Customizer and add it to the global stylesheet.
62 * Always do this in Customizer preview for the sake of live preview since it be empty.
63 */
64 $custom_css = trim( wp_get_custom_css() );
65 if ( $custom_css || is_customize_preview() ) {
66 if ( is_customize_preview() ) {
67 /*
68 * When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
69 * to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
70 * the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
71 * would break live previewing.
72 */
73 $before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
74 $after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
75 $custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
76 $custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
77 }
78 $custom_css = "\n" . $custom_css;
79 }
80 $stylesheet .= $custom_css;
81
82 // Add the global styles custom CSS at the end.
83 $stylesheet .= gutenberg_get_global_stylesheet( array( 'custom-css' ) );
84 }
85
86 if ( empty( $stylesheet ) ) {
87 return;
88 }
89
90 wp_register_style( 'global-styles', false );
91 wp_add_inline_style( 'global-styles', $stylesheet );
92 wp_enqueue_style( 'global-styles' );
93
94 // Add each block as an inline css.
95 gutenberg_add_global_styles_for_blocks();
96 }
97 add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' );
98 add_action( 'wp_footer', 'gutenberg_enqueue_global_styles', 1 );
99
100 /**
101 * Enqueues the global styles custom css.
102 *
103 * @since 6.2.0
104 */
105 function gutenberg_enqueue_global_styles_custom_css() {
106 _deprecated_function( __FUNCTION__, 'Gutenberg 17.8.0', 'gutenberg_enqueue_global_styles' );
107 if ( ! wp_is_block_theme() ) {
108 return;
109 }
110
111 // Don't enqueue Customizer's custom CSS separately.
112 remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
113
114 $custom_css = wp_get_custom_css();
115 $custom_css .= gutenberg_get_global_styles_custom_css();
116
117 if ( ! empty( $custom_css ) ) {
118 wp_add_inline_style( 'global-styles', $custom_css );
119 }
120 }
121
122 /**
123 * Function that enqueues the CSS Custom Properties coming from theme.json.
124 *
125 * @since 5.9.0
126 */
127 function gutenberg_enqueue_global_styles_css_custom_properties() {
128 wp_register_style( 'global-styles-css-custom-properties', false );
129 wp_add_inline_style( 'global-styles-css-custom-properties', gutenberg_get_global_stylesheet( array( 'variables' ) ) );
130 wp_enqueue_style( 'global-styles-css-custom-properties' );
131 }
132 remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_global_styles_css_custom_properties' );
133 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_global_styles_css_custom_properties' );
134