PluginProbe
Gutenberg / 8.9.2
Gutenberg v8.9.2
24.0.0 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 All 403 releases
← All changes | lib/client-assets.php +479 -291 24.0.08.9.2 View file →
@@ -1,8 +1,8 @@
1 1 <?php
2 2 /**
3 - * Functions to register client-side assets (scripts and stylesheets) specific
4 - * for the Gutenberg editor plugin.
3 + * Functions to register client-side assets (scripts and stylesheets) for the
4 + * Gutenberg editor plugin.
5 5 *
6 6 * @package gutenberg
7 7 */
8 8
@@ -12,27 +12,90 @@
12 12
13 13 /**
14 14 * Retrieves the root plugin path.
15 15 *
16 + * @return string Root path to the gutenberg plugin.
17 + *
16 18 * @since 0.1.0
17 - *
18 - * @return string Root path to the gutenberg plugin.
19 19 */
20 20 function gutenberg_dir_path() {
21 - return plugin_dir_path( __DIR__ );
21 + return plugin_dir_path( dirname( __FILE__ ) );
22 22 }
23 23
24 24 /**
25 25 * Retrieves a URL to a file in the gutenberg plugin.
26 26 *
27 + * @param string $path Relative path of the desired file.
28 + *
29 + * @return string Fully qualified URL pointing to the desired file.
30 + *
27 31 * @since 0.1.0
32 + */
33 +function gutenberg_url( $path ) {
34 + return plugins_url( $path, dirname( __FILE__ ) );
35 +}
36 +
37 +/**
38 + * Registers a script according to `wp_register_script`. Honors this request by
39 + * reassigning internal dependency properties of any script handle already
40 + * registered by that name. It does not deregister the original script, to
41 + * avoid losing inline scripts which may have been attached.
28 42 *
29 - * @param string $path Relative path of the desired file.
43 + * @since 4.1.0
30 44 *
31 - * @return string Fully qualified URL pointing to the desired file.
45 + * @param WP_Scripts $scripts WP_Scripts instance.
46 + * @param string $handle Name of the script. Should be unique.
47 + * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
48 + * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
49 + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
50 + * as a query string for cache busting purposes. If version is set to false, a version
51 + * number is automatically added equal to current installed WordPress version.
52 + * If set to null, no version is added.
53 + * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
54 + * Default 'false'.
32 55 */
33 -function gutenberg_url( $path ) {
34 - return plugins_url( $path, __DIR__ );
56 +function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
57 + $script = $scripts->query( $handle, 'registered' );
58 + if ( $script ) {
59 + /*
60 + * In many ways, this is a reimplementation of `wp_register_script` but
61 + * bypassing consideration of whether a script by the given handle had
62 + * already been registered.
63 + */
64 +
65 + // See: `_WP_Dependency::__construct` .
66 + $script->src = $src;
67 + $script->deps = $deps;
68 + $script->ver = $ver;
69 + $script->args = $in_footer;
70 +
71 + /*
72 + * The script's `group` designation is an indication of whether it is
73 + * to be printed in the header or footer. The behavior here defers to
74 + * the arguments as passed. Specifically, group data is not assigned
75 + * for a script unless it is designated to be printed in the footer.
76 + */
77 +
78 + // See: `wp_register_script` .
79 + unset( $script->extra['group'] );
80 + if ( $in_footer ) {
81 + $script->add_data( 'group', 1 );
82 + }
83 + } else {
84 + $scripts->add( $handle, $src, $deps, $ver, $in_footer );
85 + }
86 +
87 + /*
88 + * `WP_Dependencies::set_translations` will fall over on itself if setting
89 + * translations on the `wp-i18n` handle, since it internally adds `wp-i18n`
90 + * as a dependency of itself, exhausting memory. The same applies for the
91 + * polyfill script, which is a dependency _of_ `wp-i18n`.
92 + *
93 + * See: https://core.trac.wordpress.org/ticket/46089
94 + */
95 + if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
96 + $scripts->set_translations( $handle, 'default' );
97 + }
35 98 }
36 99
37 100 /**
38 101 * Filters the default translation file load behavior to load the Gutenberg
@@ -51,14 +114,14 @@
51 114 return $file;
52 115 }
53 116
54 117 // Ignore scripts whose handle does not have the "wp-" prefix.
55 - if ( ! str_starts_with( $handle, 'wp-' ) ) {
118 + if ( 'wp-' !== substr( $handle, 0, 3 ) ) {
56 119 return $file;
57 120 }
58 121
59 - // Ignore scripts that are not found in the expected `build/scripts/` location.
60 - $script_path = gutenberg_dir_path() . 'build/scripts/' . substr( $handle, 3 ) . '/index.min.js';
122 + // Ignore scripts that are not found in the expected `build/` location.
123 + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js';
61 124 if ( ! file_exists( $script_path ) ) {
62 125 return $file;
63 126 }
64 127
@@ -86,404 +149,529 @@
86 149 }
87 150 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
88 151
89 152 /**
90 - * Adds the 'editor' dependency to wp-block-library, required by the Classic block.
153 + * Filters the default labels for common post types to change the case style
154 + * from capitalized (e.g. "Featured Image") to sentence-style (e.g. "Featured
155 + * image").
91 156 *
92 - * @param WP_Scripts $scripts WP_Scripts instance.
157 + * See: https://github.com/WordPress/gutenberg/pull/18758
158 + *
159 + * @param object $labels Object with all the labels as member variables.
160 + *
161 + * @return object Object with all the labels, including overridden ones.
93 162 */
94 -function gutenberg_register_block_library_script_special_case( $scripts ) {
95 - $handle = 'wp-block-library';
96 - $script = $scripts->query( $handle, 'registered' );
97 - if ( ! in_array( 'editor', $script->deps, true ) ) {
98 - $script->deps[] = 'editor';
163 +function gutenberg_override_posttype_labels( $labels ) {
164 + $labels->featured_image = __( 'Featured image', 'gutenberg' );
165 + return $labels;
166 +}
167 +foreach ( array( 'post', 'page' ) as $post_type ) {
168 + add_filter( "post_type_labels_{$post_type}", 'gutenberg_override_posttype_labels' );
169 +}
170 +
171 +/**
172 + * Registers a style according to `wp_register_style`. Honors this request by
173 + * deregistering any style by the same handler before registration.
174 + *
175 + * @since 4.1.0
176 + *
177 + * @param WP_Styles $styles WP_Styles instance.
178 + * @param string $handle Name of the stylesheet. Should be unique.
179 + * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
180 + * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
181 + * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
182 + * as a query string for cache busting purposes. If version is set to false, a version
183 + * number is automatically added equal to current installed WordPress version.
184 + * If set to null, no version is added.
185 + * @param string $media Optional. The media for which this stylesheet has been defined.
186 + * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
187 + * '(orientation: portrait)' and '(max-width: 640px)'.
188 + */
189 +function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
190 + $style = $styles->query( $handle, 'registered' );
191 + if ( $style ) {
192 + $styles->remove( $handle );
99 193 }
194 + $styles->add( $handle, $src, $deps, $ver, $media );
100 195 }
101 -add_action( 'wp_default_scripts', 'gutenberg_register_block_library_script_special_case', 11 );
102 196
103 197 /**
104 - * Registers WordPress package styles with complex requirements.
198 + * Registers vendor JavaScript files to be used as dependencies of the editor
199 + * and plugins.
105 200 *
106 - * Simple styles (main style.css with inferred dependencies) are auto-registered
107 - * via build/styles.php at default priority (10). This function runs at priority 15 to handle:
108 - * - Multiple style files per package (content.css, classic.css, etc.)
109 - * - Non-WordPress dependencies (dashicons, common, forms)
110 - * - Custom handles that don't match wp-{package} pattern
111 - * - Conditional dependencies based on theme/settings
112 - * - Dynamic filename logic
201 + * This function is called from a script during the plugin build process, so it
202 + * should not call any WordPress PHP functions.
113 203 *
114 - * These override calls will replace the auto-registered versions as needed.
204 + * @since 0.1.0
115 205 *
116 - * @since 6.7.0
206 + * @param WP_Scripts $scripts WP_Scripts instance.
207 + */
208 +function gutenberg_register_vendor_scripts( $scripts ) {
209 + $suffix = SCRIPT_DEBUG ? '' : '.min';
210 +
211 + $react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
212 + gutenberg_register_vendor_script(
213 + $scripts,
214 + 'react',
215 + 'https://unpkg.com/react@16.13.1/umd/react' . $react_suffix . '.js',
216 + array( 'wp-polyfill' )
217 + );
218 + gutenberg_register_vendor_script(
219 + $scripts,
220 + 'react-dom',
221 + 'https://unpkg.com/react-dom@16.13.1/umd/react-dom' . $react_suffix . '.js',
222 + array( 'react' )
223 + );
224 +
225 + /*
226 + * This script registration and the corresponding function should be removed
227 + * removed once the plugin is updated to support WordPress 5.6.0 and newer.
228 + */
229 + gutenberg_register_vendor_script(
230 + $scripts,
231 + 'lodash',
232 + 'https://unpkg.com/lodash@4.17.19/lodash.js',
233 + array(),
234 + '4.17.19',
235 + true
236 + );
237 +}
238 +add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
239 +
240 +/**
241 + * Registers all the WordPress packages scripts that are in the standardized
242 + * `build/` location.
117 243 *
118 - * @global array $editor_styles
244 + * @since 4.5.0
119 245 *
120 - * @param WP_Styles $styles WP_Styles instance.
246 + * @param WP_Scripts $scripts WP_Scripts instance.
121 247 */
122 -function gutenberg_register_packages_styles( $styles ) {
123 - // When in production, use the plugin's version as the asset version;
124 - // else (for development or test) default to use the current time.
125 - $version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
126 - $suffix = SCRIPT_DEBUG ? '' : '.min';
248 +function gutenberg_register_packages_scripts( $scripts ) {
249 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
250 + // Prefix `wp-` to package directory to get script handle.
251 + // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
252 + $handle = 'wp-' . basename( dirname( $path ) );
127 253
128 - // wp-components: add dashicons (icon font dependency) and design tokens.
129 - $components_style = $styles->query( 'wp-components', 'registered' );
130 - $components_style->deps[] = 'dashicons';
131 - $components_style->deps[] = 'wp-theme';
254 + // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
255 + $asset_file = substr( $path, 0, -3 ) . '.asset.php';
256 + $asset = file_exists( $asset_file )
257 + ? require( $asset_file )
258 + : null;
259 + $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
260 + $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
132 261
133 - // wp-edit-post: add wp-edit-blocks (custom handle not auto-inferred)
134 - $styles->query( 'wp-edit-post', 'registered' )->deps[] = 'wp-edit-blocks';
262 + // Add dependencies that cannot be detected and generated by build tools.
263 + switch ( $handle ) {
264 + case 'wp-block-library':
265 + array_push( $dependencies, 'editor' );
266 + break;
135 267
136 - // wp-edit-site: add core WP styles and custom handles
137 - $edit_site_style = $styles->query( 'wp-edit-site', 'registered' );
138 - $edit_site_style->deps[] = 'common';
139 - $edit_site_style->deps[] = 'forms';
140 - $edit_site_style->deps[] = 'wp-block-library-editor';
268 + case 'wp-edit-post':
269 + array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
270 + break;
141 271
142 - // wp-edit-widgets: add wp-edit-blocks (custom handle not auto-inferred)
143 - $styles->query( 'wp-edit-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
272 + case 'wp-edit-site':
273 + array_push( $dependencies, 'wp-dom-ready' );
274 + break;
275 + }
144 276
145 - // wp-customize-widgets: add wp-edit-blocks (custom handle not auto-inferred)
146 - $styles->query( 'wp-customize-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
277 + // Get the path from Gutenberg directory as expected by `gutenberg_url`.
278 + $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
147 279
280 + gutenberg_override_script(
281 + $scripts,
282 + $handle,
283 + gutenberg_url( $gutenberg_path ),
284 + $dependencies,
285 + $version,
286 + true
287 + );
288 + }
289 +}
290 +add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
291 +
292 +/**
293 + * Registers all the WordPress packages styles that are in the standardized
294 + * `build/` location.
295 + *
296 + * @since 6.7.0
297 +
298 + * @param WP_Styles $styles WP_Styles instance.
299 + */
300 +function gutenberg_register_packages_styles( $styles ) {
301 + // Editor Styles.
148 302 gutenberg_override_style(
149 303 $styles,
150 - 'wp-theme',
151 - gutenberg_url( 'build/styles/theme/design-tokens' . $suffix . '.css' ),
152 - array(),
153 - $version
304 + 'wp-block-editor',
305 + gutenberg_url( 'build/block-editor/style.css' ),
306 + array( 'wp-components', 'wp-editor-font' ),
307 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
154 308 );
155 - $styles->add_data( 'wp-theme', 'rtl', 'replace' );
156 - $styles->add_data( 'wp-theme', 'suffix', $suffix );
157 - $styles->add_data( 'wp-theme', 'path', gutenberg_dir_path() . 'build/styles/theme/design-tokens' . $suffix . '.css' );
309 + $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
158 310
159 - // Register wp-base-styles and add it to the already registered wp-admin stylesheet
160 311 gutenberg_override_style(
161 312 $styles,
162 - 'wp-base-styles',
163 - gutenberg_url( 'build/styles/base-styles/admin-schemes' . $suffix . '.css' ),
164 - array(),
165 - $version
313 + 'wp-editor',
314 + gutenberg_url( 'build/editor/style.css' ),
315 + array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
316 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
166 317 );
167 - $styles->add_data( 'wp-base-styles', 'rtl', 'replace' );
168 - $styles->add_data( 'wp-base-styles', 'suffix', $suffix );
169 - $styles->add_data( 'wp-base-styles', 'path', gutenberg_dir_path() . 'build/styles/base-styles/admin-schemes' . $suffix . '.css' );
170 - $styles->query( 'wp-admin', 'registered' )->deps[] = 'wp-base-styles';
318 + $styles->add_data( 'wp-editor', 'rtl', 'replace' );
171 319
172 320 gutenberg_override_style(
173 321 $styles,
174 - 'wp-block-editor-content',
175 - gutenberg_url( 'build/styles/block-editor/content' . $suffix . '.css' ),
176 - array( 'wp-components' ),
177 - $version
322 + 'wp-edit-post',
323 + gutenberg_url( 'build/edit-post/style.css' ),
324 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
325 + filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
178 326 );
179 - $styles->add_data( 'wp-block-editor-content', 'rtl', 'replace' );
180 - $styles->add_data( 'wp-block-editor-content', 'suffix', $suffix );
327 + $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
181 328
182 - $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
183 329 gutenberg_override_style(
184 330 $styles,
331 + 'wp-components',
332 + gutenberg_url( 'build/components/style.css' ),
333 + array(),
334 + filemtime( gutenberg_dir_path() . 'build/components/style.css' )
335 + );
336 + $styles->add_data( 'wp-components', 'rtl', 'replace' );
337 +
338 + gutenberg_override_style(
339 + $styles,
185 340 'wp-block-library',
186 - gutenberg_url( 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' ),
341 + gutenberg_url( 'build/block-library/style.css' ),
187 342 array(),
188 - $version
343 + filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
189 344 );
190 345 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
191 - $styles->add_data( 'wp-block-library', 'suffix', $suffix );
192 - $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' );
193 346
194 - // Only add CONTENT styles here that should be enqueued in the iframe!
195 - $wp_edit_blocks_dependencies = array(
196 - // Design System tokens load first so the `:root` CSS custom
197 - // properties are defined before any consuming stylesheet reads
198 - // them inside the editor iframe.
199 - 'wp-theme',
200 - 'wp-components',
201 - // This need to be added before the block library styles,
202 - // The block library styles override the "reset" styles.
203 - 'wp-reset-editor-styles',
204 - 'wp-block-library',
205 - // Until #37466, we can't specifically add them as editor styles yet,
206 - // so we must hard-code it here as a dependency.
207 - 'wp-block-editor-content',
208 - 'wp-base-styles',
347 + gutenberg_override_style(
348 + $styles,
349 + 'wp-format-library',
350 + gutenberg_url( 'build/format-library/style.css' ),
351 + array( 'wp-block-editor', 'wp-components' ),
352 + filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
209 353 );
354 + $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
210 355
211 - // Only load the default layout and margin styles for themes without theme.json file.
212 - if ( ! wp_theme_has_theme_json() ) {
213 - $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
214 - }
356 + gutenberg_override_style(
357 + $styles,
358 + 'wp-edit-blocks',
359 + gutenberg_url( 'build/block-library/editor.css' ),
360 + array(
361 + 'wp-components',
362 + 'wp-editor',
363 + 'wp-block-library',
364 + // Always include visual styles so the editor never appears broken.
365 + 'wp-block-library-theme',
366 + ),
367 + filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
368 + );
369 + $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
215 370
216 - global $editor_styles;
217 - if ( current_theme_supports( 'wp-block-styles' ) && ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) ) {
218 - // Include opinionated block styles if the theme supports block styles and no $editor_styles are declared, so the editor never appears broken.
219 - $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
220 - }
371 + gutenberg_override_style(
372 + $styles,
373 + 'wp-nux',
374 + gutenberg_url( 'build/nux/style.css' ),
375 + array( 'wp-components' ),
376 + filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
377 + );
378 + $styles->add_data( 'wp-nux', 'rtl', 'replace' );
221 379
222 380 gutenberg_override_style(
223 381 $styles,
224 - 'wp-reset-editor-styles',
225 - gutenberg_url( 'build/styles/block-library/reset' . $suffix . '.css' ),
226 - array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles.
227 - $version
382 + 'wp-block-library-theme',
383 + gutenberg_url( 'build/block-library/theme.css' ),
384 + array(),
385 + filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
228 386 );
229 - $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
230 - $styles->add_data( 'wp-reset-editor-styles', 'suffix', $suffix );
387 + $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
231 388
232 389 gutenberg_override_style(
233 390 $styles,
234 - 'wp-editor-classic-layout-styles',
235 - gutenberg_url( 'build/styles/edit-post/classic' . $suffix . '.css' ),
236 - array(),
237 - $version
391 + 'wp-list-reusable-blocks',
392 + gutenberg_url( 'build/list-reusable-blocks/style.css' ),
393 + array( 'wp-components' ),
394 + filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
238 395 );
239 - $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
240 - $styles->add_data( 'wp-editor-classic-layout-styles', 'suffix', $suffix );
396 + $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
241 397
242 398 gutenberg_override_style(
243 399 $styles,
244 - 'wp-block-library-editor',
245 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
246 - array(),
247 - $version
400 + 'wp-edit-navigation',
401 + gutenberg_url( 'build/edit-navigation/style.css' ),
402 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
403 + filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' )
248 404 );
249 - $styles->add_data( 'wp-block-library-editor', 'rtl', 'replace' );
250 - $styles->add_data( 'wp-block-library-editor', 'suffix', $suffix );
405 + $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
251 406
252 407 gutenberg_override_style(
253 408 $styles,
254 - 'wp-edit-blocks',
255 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
256 - $wp_edit_blocks_dependencies,
257 - $version
409 + 'wp-edit-site',
410 + gutenberg_url( 'build/edit-site/style.css' ),
411 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
412 + filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
258 413 );
259 - $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
260 - $styles->add_data( 'wp-edit-blocks', 'suffix', $suffix );
414 + $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
261 415
262 416 gutenberg_override_style(
263 417 $styles,
264 - 'wp-block-library-theme',
265 - gutenberg_url( 'build/styles/block-library/theme' . $suffix . '.css' ),
266 - array(),
267 - $version
418 + 'wp-edit-widgets',
419 + gutenberg_url( 'build/edit-widgets/style.css' ),
420 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
421 + filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
268 422 );
269 - $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
270 - $styles->add_data( 'wp-block-library-theme', 'suffix', $suffix );
423 + $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
271 424
272 425 gutenberg_override_style(
273 426 $styles,
274 - 'classic-theme-styles',
275 - gutenberg_url( 'build/styles/block-library/classic' . $suffix . '.css' ),
276 - array(),
277 - $version
427 + 'wp-block-directory',
428 + gutenberg_url( 'build/block-directory/style.css' ),
429 + array( 'wp-block-editor', 'wp-components' ),
430 + filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
278 431 );
279 - $styles->add_data( 'classic-theme-styles', 'rtl', 'replace' );
280 - $styles->add_data( 'classic-theme-styles', 'suffix', $suffix );
281 - $styles->add_data( 'classic-theme-styles', 'path', gutenberg_dir_path() . 'build/styles/block-library/classic' . $suffix . '.css' );
432 + $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
282 433 }
283 -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles', 15 );
434 +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
284 435
285 436 /**
286 - * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
287 - * Styles are stored via the Style Engine API.
437 + * Registers common scripts and styles to be used as dependencies of the editor
438 + * and plugins.
288 439 *
289 - * This hook also exists, and should be backported to Core in future versions.
290 - * However, it is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuous development.
440 + * @since 0.1.0
441 + */
442 +function gutenberg_enqueue_block_editor_assets() {
443 + if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
444 + $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
445 +
446 + wp_enqueue_script(
447 + 'gutenberg-live-reload',
448 + $live_reload_url
449 + );
450 + }
451 +}
452 +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
453 +
454 +/**
455 + * Retrieves a unique and reasonably short and human-friendly filename for a
456 + * vendor script based on a URL and the script handle.
291 457 *
292 - * @since 6.1
458 + * @param string $handle The name of the script.
459 + * @param string $src Full URL of the external script.
293 460 *
294 - * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
461 + * @return string Script filename suitable for local caching.
295 462 *
296 - * @param array $options {
297 - * Optional. An array of options to pass to gutenberg_style_engine_get_stylesheet_from_context(). Default empty array.
463 + * @since 0.1.0
464 + */
465 +function gutenberg_vendor_script_filename( $handle, $src ) {
466 + $filename = basename( $src );
467 + $match = preg_match(
468 + '/^'
469 + . '(?P<ignore>.*?)'
470 + . '(?P<suffix>\.min)?'
471 + . '(?P<extension>\.js)'
472 + . '(?P<extra>.*)'
473 + . '$/',
474 + $filename,
475 + $filename_pieces
476 + );
477 +
478 + $prefix = $handle;
479 + $suffix = $match ? $filename_pieces['suffix'] : '';
480 + $hash = substr( md5( $src ), 0, 8 );
481 +
482 + return "${prefix}${suffix}.${hash}.js";
483 +}
484 +
485 +/**
486 + * Registers a vendor script from a URL, preferring a locally cached version if
487 + * possible, or downloading it if the cached version is unavailable or
488 + * outdated.
298 489 *
299 - * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
300 - * @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
301 - * }
490 + * @param WP_Scripts $scripts WP_Scripts instance.
491 + * @param string $handle Name of the script.
492 + * @param string $src Full URL of the external script.
493 + * @param array $deps Optional. An array of registered script handles this
494 + * script depends on.
495 + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
496 + * as a query string for cache busting purposes. If version is set to false, a version
497 + * number is automatically added equal to current installed WordPress version.
498 + * If set to null, no version is added.
499 + * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
500 + * Default 'false'.
302 501 *
303 - * @return void
502 + * @since 0.1.0
304 503 */
305 -function gutenberg_enqueue_stored_styles( $options = array() ) {
306 - $is_block_theme = wp_is_block_theme();
307 - $is_classic_theme = ! $is_block_theme;
308 -
309 - /*
310 - * For block themes, print stored styles in the header.
311 - * For classic themes, in the footer.
312 - */
313 - if (
314 - ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
315 - ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) )
316 - ) {
504 +function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
505 + if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
317 506 return;
318 507 }
319 508
320 - $core_styles_keys = array( 'block-supports' );
321 - $compiled_core_stylesheet = '';
322 - $style_tag_id = 'core';
323 - foreach ( $core_styles_keys as $style_key ) {
324 - // Adds comment if code is prettified to identify core styles sections in debugging.
325 - $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : SCRIPT_DEBUG;
326 - if ( $should_prettify ) {
327 - $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n";
328 - }
329 - // Chains core store ids to signify what the styles contain.
330 - $style_tag_id .= '-' . $style_key;
331 - $compiled_core_stylesheet .= gutenberg_style_engine_get_stylesheet_from_context( $style_key, $options );
332 - }
509 + $filename = gutenberg_vendor_script_filename( $handle, $src );
333 510
334 - // Combines Core styles.
335 - if ( ! empty( $compiled_core_stylesheet ) ) {
336 - wp_register_style( $style_tag_id, false, array(), true );
337 - wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
338 - wp_enqueue_style( $style_tag_id );
511 + if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
512 + echo "$src|$filename\n";
513 + return;
339 514 }
340 515
341 - // If there are any other stores registered by themes etc., print them out.
342 - $additional_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores();
516 + $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
343 517
344 - /*
345 - * Since the corresponding action hook in Core is removed below,
346 - * this function should still honour any styles stored using the Core Style Engine store.
347 - */
348 - if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
349 - $additional_stores = array_merge( $additional_stores, WP_Style_Engine_CSS_Rules_Store::get_stores() );
350 - }
518 + $needs_fetch = (
519 + defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
520 + ! file_exists( $full_path ) ||
521 + time() - filemtime( $full_path ) >= DAY_IN_SECONDS
522 + )
523 + );
351 524
352 - foreach ( array_keys( $additional_stores ) as $store_name ) {
353 - if ( in_array( $store_name, $core_styles_keys, true ) ) {
354 - continue;
525 + if ( $needs_fetch ) {
526 + // Determine whether we can write to this file. If not, don't waste
527 + // time doing a network request.
528 + // @codingStandardsIgnoreStart
529 + $f = @fopen( $full_path, 'a' );
530 + // @codingStandardsIgnoreEnd
531 + if ( ! $f ) {
532 + // Failed to open the file for writing, probably due to server
533 + // permissions. Enqueue the script directly from the URL instead.
534 + gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
535 + return;
355 536 }
356 - $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name, $options );
357 - if ( ! empty( $styles ) ) {
358 - $key = "wp-style-engine-$store_name";
359 - wp_register_style( $key, false, array(), true );
360 - wp_add_inline_style( $key, $styles );
361 - wp_enqueue_style( $key );
537 + fclose( $f );
538 + $response = wp_remote_get( $src );
539 + if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
540 + $f = fopen( $full_path, 'w' );
541 + fwrite( $f, wp_remote_retrieve_body( $response ) );
542 + fclose( $f );
543 + } elseif ( ! filesize( $full_path ) ) {
544 + // The request failed. If the file is already cached, continue to
545 + // use this file. If not, then unlink the 0 byte file, and enqueue
546 + // the script directly from the URL.
547 + gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
548 + unlink( $full_path );
549 + return;
362 550 }
363 551 }
552 + gutenberg_override_script(
553 + $scripts,
554 + $handle,
555 + gutenberg_url( 'vendor/' . $filename ),
556 + $deps,
557 + $ver,
558 + $in_footer
559 + );
364 560 }
365 561
366 562 /**
367 - * Registers vendor JavaScript files to be used as dependencies of the editor
368 - * and plugins.
563 + * Extends block editor settings to include Gutenberg's `editor-styles.css` as
564 + * taking precedent those styles shipped with core.
369 565 *
370 - * This function is called from a script during the plugin build process, so it
371 - * should not call any WordPress PHP functions.
566 + * @param array $settings Default editor settings.
372 567 *
373 - * @since 13.0
374 - *
375 - * @param WP_Scripts $scripts WP_Scripts instance.
568 + * @return array Filtered editor settings.
376 569 */
377 -function gutenberg_register_vendor_scripts( $scripts ) {
378 - $extension = SCRIPT_DEBUG ? '.js' : '.min.js';
379 - $vendors_dir = gutenberg_dir_path() . 'build/scripts/vendors/';
570 +function gutenberg_extend_block_editor_styles( $settings ) {
571 + $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
380 572
381 - // When the React 19 experiment is enabled, register React 19 vendor
382 - // scripts under the `react`, `react-dom`, and `react-jsx-runtime` handles.
383 - $use_react_19 = gutenberg_is_experiment_enabled( 'gutenberg-react-19' );
573 + /*
574 + * If, for whatever reason, the built editor styles do not exist, avoid
575 + * override and fall back to the default.
576 + */
577 + if ( ! file_exists( $editor_styles_file ) ) {
578 + return $settings;
579 + }
384 580
385 - $vendor_handles = array( 'react', 'react-dom', 'react-jsx-runtime' );
581 + if ( empty( $settings['styles'] ) ) {
582 + $settings['styles'] = array();
583 + } else {
584 + /*
585 + * The styles setting is an array of CSS strings, so there is no direct
586 + * way to find the default styles. To maximize stability, load (again)
587 + * the default styles from disk and find its place in the array.
588 + *
589 + * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
590 + */
386 591
387 - foreach ( $vendor_handles as $handle ) {
388 - $source = $use_react_19 ? $handle . '-19' : $handle;
389 - $asset_file = $vendors_dir . $source . '.min.asset.php';
390 - $asset = file_exists( $asset_file ) ? require $asset_file : array();
391 - $dependencies = $asset['dependencies'] ?? array();
392 - $version = $asset['version'] ?? '0';
592 + $default_styles = file_get_contents(
593 + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
594 + );
393 595
394 - gutenberg_override_script(
395 - $scripts,
396 - $handle,
397 - gutenberg_url( 'build/scripts/vendors/' . $source . $extension ),
398 - $dependencies,
399 - $version
400 - );
596 + /*
597 + * Iterate backwards from the end of the array since the preferred
598 + * insertion point in case not found is prepended as first entry.
599 + */
600 + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
601 + if ( isset( $settings['styles'][ $i ]['css'] ) &&
602 + $default_styles === $settings['styles'][ $i ]['css'] ) {
603 + break;
604 + }
605 + }
401 606 }
402 607
403 - // WordPress Core in `wp_register_development_scripts` sets `wp-react-refresh-entry`
404 - // as a dependency to `react` when `SCRIPT_DEBUG` is true. Preserve that here.
405 - if ( SCRIPT_DEBUG ) {
406 - $react = $scripts->query( 'react', 'registered' );
407 - if ( $react && ! in_array( 'wp-react-refresh-entry', $react->deps, true ) ) {
408 - $react->deps[] = 'wp-react-refresh-entry';
409 - }
608 + $editor_styles = array(
609 + 'css' => file_get_contents( $editor_styles_file ),
610 + );
611 +
612 + // Substitute default styles if found. Otherwise, prepend to setting array.
613 + if ( isset( $i ) && $i >= 0 ) {
614 + $settings['styles'][ $i ] = $editor_styles;
615 + } else {
616 + array_unshift( $settings['styles'], $editor_styles );
410 617 }
618 +
619 + return $settings;
411 620 }
412 -add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
621 +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
413 622
414 623 /**
415 - * Registers or re-registers Gutenberg Script Modules.
624 + * Extends block editor settings to determine whether to use custom line height controls.
416 625 *
417 - * Script modules that are registered by Core will be re-registered by Gutenberg.
626 + * @param array $settings Default editor settings.
418 627 *
419 - * @since 19.3.0
628 + * @return array Filtered editor settings.
420 629 */
421 -function gutenberg_define_interactivity_modules_support() {
422 - // Load the auto-generated module registry.
423 - $modules_registry_file = gutenberg_dir_path() . 'build/modules/index.php';
424 - if ( ! file_exists( $modules_registry_file ) ) {
425 - return;
426 - }
427 -
428 - $modules = require $modules_registry_file;
429 -
430 - // Add client navigation support to block library modules.
431 - foreach ( $modules as $module ) {
432 - if ( str_starts_with( $module['id'], '@wordpress/block-library' ) && method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
433 - wp_interactivity()->add_client_navigation_support_to_script_module( $module['id'] );
434 - }
435 - }
630 +function gutenberg_extend_settings_custom_line_height( $settings ) {
631 + $settings['enableCustomLineHeight'] = get_theme_support( 'custom-line-height' );
632 + return $settings;
436 633 }
437 -remove_action( 'wp_default_scripts', 'wp_define_interactivity_modules_support' );
438 -add_action( 'wp_default_scripts', 'gutenberg_define_interactivity_modules_support' );
634 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_line_height' );
439 635
440 636 /**
441 - * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice.
442 - * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes,
443 - * which are in continuous development and generally ahead of Core.
637 + * Extends block editor settings to determine whether to use custom unit controls.
638 + * Currently experimental.
639 + *
640 + * @param array $settings Default editor settings.
641 + *
642 + * @return array Filtered editor settings.
444 643 */
445 -remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
446 -remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
447 -
448 -// Enqueue stored styles.
449 -add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
450 -add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );
451 -
452 -/**
453 - * Enqueues the LaTeX to MathML loader script module in the block editor.
454 - */
455 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_latex_to_mathml_loader' );
456 -function gutenberg_enqueue_latex_to_mathml_loader() {
457 - wp_enqueue_script_module( '@wordpress/latex-to-mathml/loader' );
644 +function gutenberg_extend_settings_custom_units( $settings ) {
645 + $settings['enableCustomUnits'] = get_theme_support( 'custom-units' );
646 + return $settings;
458 647 }
648 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_units' );
459 649
460 650 /**
461 - * Enqueue the vips loader script module in the block editor.
651 + * Extends block editor settings to determine whether to use custom spacing controls.
652 + * Currently experimental.
462 653 *
463 - * This registers @wordpress/vips/worker as a dynamic dependency in the import map,
464 - * enabling on-demand loading of the ~3.8MB WASM-based image processing module
465 - * when client-side media processing is triggered via @wordpress/upload-media.
654 + * @param array $settings Default editor settings.
466 655 *
467 - * @see packages/vips/src/loader.ts
656 + * @return array Filtered editor settings.
468 657 */
469 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_vips_loader' );
470 -function gutenberg_enqueue_vips_loader() {
471 - wp_enqueue_script_module( '@wordpress/vips/loader' );
658 +function gutenberg_extend_settings_custom_spacing( $settings ) {
659 + $settings['__experimentalEnableCustomSpacing'] = get_theme_support( 'experimental-custom-spacing' );
660 + return $settings;
472 661 }
662 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_custom_spacing' );
473 663
664 +
474 665 /**
475 - * Enqueue the video-conversion loader script module in the block editor.
666 + * Extends block editor settings to determine whether to use custom spacing controls.
667 + * Currently experimental.
476 668 *
477 - * This registers @wordpress/video-conversion/worker as a dynamic dependency
478 - * in the import map, enabling on-demand loading of the WebCodecs-based
479 - * GIF-to-video processing module when animated GIF conversion is triggered
480 - * via @wordpress/upload-media.
669 + * @param array $settings Default editor settings.
481 670 *
482 - * @see packages/video-conversion/src/loader.ts
671 + * @return array Filtered editor settings.
483 672 */
484 -if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
485 - add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_video_conversion_loader' );
673 +function gutenberg_extend_settings_link_color( $settings ) {
674 + $settings['__experimentalEnableLinkColor'] = get_theme_support( 'experimental-link-color' );
675 + return $settings;
486 676 }
487 -function gutenberg_enqueue_video_conversion_loader() {
488 - wp_enqueue_script_module( '@wordpress/video-conversion/loader' );
489 -}
677 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_link_color' );