PluginProbe
Gutenberg / 15.2.3
Gutenberg v15.2.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 / compat / wordpress-6.1 / script-loader.php

script-loader.php in Gutenberg 15.2.3, at lib/compat/wordpress-6.1/script-loader.php

126 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Load global styles assets in the front-end.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * This applies a filter to the list of style nodes that comes from `get_style_nodes` in WP_Theme_JSON.
10 * This particular filter removes all of the blocks from the array.
11 *
12 * We want WP_Theme_JSON to be ignorant of the implementation details of how the CSS is being used.
13 * This filter allows us to modify the output of WP_Theme_JSON depending on whether or not we are loading separate assets,
14 * without making the class aware of that detail.
15 *
16 * @since 6.1
17 *
18 * @param array $nodes The nodes to filter.
19 * @return array A filtered array of style nodes.
20 */
21 function gutenberg_filter_out_block_nodes( $nodes ) {
22 return array_filter(
23 $nodes,
24 function( $node ) {
25 return ! in_array( 'blocks', $node['path'], true );
26 },
27 ARRAY_FILTER_USE_BOTH
28 );
29 }
30
31 /**
32 * Enqueues the global styles defined via theme.json.
33 * This should replace wp_enqueue_global_styles.
34 *
35 * @since 5.8.0
36 */
37 function gutenberg_enqueue_global_styles() {
38 $separate_assets = wp_should_load_separate_core_block_assets();
39 $is_block_theme = wp_is_block_theme();
40 $is_classic_theme = ! $is_block_theme;
41
42 /*
43 * Global styles should be printed in the head when loading all styles combined.
44 * The footer should only be used to print global styles for classic themes with separate core assets enabled.
45 *
46 * See https://core.trac.wordpress.org/ticket/53494.
47 */
48 if (
49 ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
50 ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) ||
51 ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets )
52 ) {
53 return;
54 }
55
56 /**
57 * If we are loading CSS for each block separately, then we can load the theme.json CSS conditionally.
58 * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
59 * This filter has to be registered before we call gutenberg_get_global_stylesheet();
60 */
61 add_filter( 'wp_theme_json_get_style_nodes', 'gutenberg_filter_out_block_nodes', 10, 1 );
62
63 $stylesheet = gutenberg_get_global_stylesheet();
64 if ( empty( $stylesheet ) ) {
65 return;
66 }
67
68 wp_register_style( 'global-styles', false, array(), true, true );
69 wp_add_inline_style( 'global-styles', $stylesheet );
70 wp_enqueue_style( 'global-styles' );
71
72 // add each block as an inline css.
73 gutenberg_add_global_styles_for_blocks();
74 }
75
76 remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
77 remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
78
79 // Enqueue global styles, and then block supports styles.
80 add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' );
81 add_action( 'wp_footer', 'gutenberg_enqueue_global_styles', 1 );
82
83 /**
84 * Loads classic theme styles on classic themes in the frontend.
85 *
86 * This is needed for backwards compatibility for button blocks specifically.
87 */
88 function gutenberg_enqueue_classic_theme_styles() {
89 if ( ! wp_is_block_theme() ) {
90 wp_register_style( 'classic-theme-styles', gutenberg_url( 'build/block-library/classic.css' ), array(), true );
91 wp_enqueue_style( 'classic-theme-styles' );
92 }
93 }
94 add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_classic_theme_styles' );
95
96 /**
97 * Loads classic theme styles on classic themes in the editor.
98 *
99 * This is needed for backwards compatibility for button blocks specifically.
100 *
101 * @since 6.1
102 *
103 * @param array $editor_settings The array of editor settings.
104 * @return array A filtered array of editor settings.
105 */
106 function gutenberg_add_editor_classic_theme_styles( $editor_settings ) {
107 if ( wp_is_block_theme() ) {
108 return $editor_settings;
109 }
110
111 $classic_theme_styles = gutenberg_dir_path() . '/build/block-library/classic.css';
112
113 // This follows the pattern of get_block_editor_theme_styles,
114 // but we can't use get_block_editor_theme_styles directly as it
115 // only handles external files or theme files.
116 $editor_settings['styles'][] = array(
117 'css' => file_get_contents( $classic_theme_styles ),
118 'baseURL' => get_theme_file_uri( $classic_theme_styles ),
119 '__unstableType' => 'theme',
120 'isGlobalStyles' => false,
121 );
122
123 return $editor_settings;
124 }
125 add_filter( 'block_editor_settings_all', 'gutenberg_add_editor_classic_theme_styles' );
126