PluginProbe
Gutenberg / 19.6.1
Gutenberg v19.6.1
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 +358 -139 23.3.219.6.1 View file →
@@ -34,8 +34,70 @@
34 34 return plugins_url( $path, __DIR__ );
35 35 }
36 36
37 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.
42 + *
43 + * @since 4.1.0
44 + *
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'.
55 + */
56 +function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
57 + /*
58 + * Force `wp-i18n` script to be registered in the <head> as a
59 + * temporary workaround for https://meta.trac.wordpress.org/ticket/6195.
60 + */
61 + $in_footer = 'wp-i18n' === $handle ? false : $in_footer;
62 +
63 + $script = $scripts->query( $handle, 'registered' );
64 + if ( $script ) {
65 + /*
66 + * In many ways, this is a reimplementation of `wp_register_script` but
67 + * bypassing consideration of whether a script by the given handle had
68 + * already been registered.
69 + */
70 +
71 + // See: `_WP_Dependency::__construct` .
72 + $script->src = $src;
73 + $script->deps = $deps;
74 + $script->ver = $ver;
75 + $script->args = $in_footer ? 1 : null;
76 + } else {
77 + $scripts->add( $handle, $src, $deps, $ver, ( $in_footer ? 1 : null ) );
78 + }
79 +
80 + if ( in_array( 'wp-i18n', $deps, true ) ) {
81 + $scripts->set_translations( $handle );
82 + }
83 +
84 + /*
85 + * Wp-editor module is exposed as window.wp.editor.
86 + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
87 + * Solution: fuse the two objects together to maintain backward compatibility.
88 + * For more context, see https://github.com/WordPress/gutenberg/issues/33203
89 + */
90 + if ( 'wp-editor' === $handle ) {
91 + $scripts->add_inline_script(
92 + 'wp-editor',
93 + 'Object.assign( window.wp.editor, window.wp.oldEditor );',
94 + 'after'
95 + );
96 + }
97 +}
98 +
99 +/**
38 100 * Filters the default translation file load behavior to load the Gutenberg
39 101 * plugin translation file, if available.
40 102 *
41 103 * @param string|false $file Path to the translation file to load. False if
@@ -55,10 +117,10 @@
55 117 if ( ! str_starts_with( $handle, 'wp-' ) ) {
56 118 return $file;
57 119 }
58 120
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';
121 + // Ignore scripts that are not found in the expected `build/` location.
122 + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js';
61 123 if ( ! file_exists( $script_path ) ) {
62 124 return $file;
63 125 }
64 126
@@ -86,33 +148,101 @@
86 148 }
87 149 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
88 150
89 151 /**
90 - * Adds the 'editor' dependency to wp-block-library, required by the Classic block.
152 + * Registers a style according to `wp_register_style`. Honors this request by
153 + * deregistering any style by the same handler before registration.
91 154 *
92 - * @param WP_Scripts $scripts WP_Scripts instance.
155 + * @since 4.1.0
156 + *
157 + * @param WP_Styles $styles WP_Styles instance.
158 + * @param string $handle Name of the stylesheet. Should be unique.
159 + * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
160 + * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
161 + * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
162 + * as a query string for cache busting purposes. If version is set to false, a version
163 + * number is automatically added equal to current installed WordPress version.
164 + * If set to null, no version is added.
165 + * @param string $media Optional. The media for which this stylesheet has been defined.
166 + * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
167 + * '(orientation: portrait)' and '(max-width: 640px)'.
93 168 */
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';
169 +function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
170 + $style = $styles->query( $handle, 'registered' );
171 + if ( $style ) {
172 + $styles->remove( $handle );
99 173 }
174 + $styles->add( $handle, $src, $deps, $ver, $media );
100 175 }
101 -add_action( 'wp_default_scripts', 'gutenberg_register_block_library_script_special_case', 11 );
102 176
103 177 /**
104 - * Registers WordPress package styles with complex requirements.
178 + * Registers all the WordPress packages scripts that are in the standardized
179 + * `build/` location.
105 180 *
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
181 + * @since 4.5.0
113 182 *
114 - * These override calls will replace the auto-registered versions as needed.
183 + * @param WP_Scripts $scripts WP_Scripts instance.
184 + */
185 +function gutenberg_register_packages_scripts( $scripts ) {
186 + // When in production, use the plugin's version as the default asset version;
187 + // else (for development or test) default to use the current time.
188 + $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
189 +
190 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.min.js' ) as $path ) {
191 + // Prefix `wp-` to package directory to get script handle.
192 + // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`.
193 + $handle = 'wp-' . basename( dirname( $path ) );
194 +
195 + // Replace extension with `.asset.php` to find the generated dependencies file.
196 + $asset_file = substr( $path, 0, -( strlen( '.js' ) ) ) . '.asset.php';
197 + $asset = file_exists( $asset_file )
198 + ? require $asset_file
199 + : null;
200 + $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
201 + $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
202 +
203 + // Add dependencies that cannot be detected and generated by build tools.
204 + switch ( $handle ) {
205 + case 'wp-block-library':
206 + if (
207 + ! gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ||
208 + ! empty( $_GET['requiresTinymce'] ) ||
209 + gutenberg_post_being_edited_requires_classic_block()
210 + ) {
211 + array_push( $dependencies, 'editor' );
212 + }
213 + break;
214 +
215 + case 'wp-edit-post':
216 + array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
217 + break;
218 +
219 + case 'wp-edit-site':
220 + array_push( $dependencies, 'wp-dom-ready' );
221 + break;
222 + case 'wp-preferences':
223 + array_push( $dependencies, 'wp-preferences-persistence' );
224 + break;
225 + }
226 +
227 + // Get the path from Gutenberg directory as expected by `gutenberg_url`.
228 + $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
229 +
230 + gutenberg_override_script(
231 + $scripts,
232 + $handle,
233 + gutenberg_url( $gutenberg_path ),
234 + $dependencies,
235 + $version,
236 + true
237 + );
238 + }
239 +}
240 +add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
241 +
242 +/**
243 + * Registers all the WordPress packages styles that are in the standardized
244 + * `build/` location.
115 245 *
116 246 * @since 6.7.0
117 247 *
118 248 * @global array $editor_styles
@@ -122,63 +252,75 @@
122 252 function gutenberg_register_packages_styles( $styles ) {
123 253 // When in production, use the plugin's version as the asset version;
124 254 // else (for development or test) default to use the current time.
125 255 $version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time();
126 - $suffix = SCRIPT_DEBUG ? '' : '.min';
127 256
128 - // wp-components: add dashicons (icon font dependency)
129 - $styles->query( 'wp-components', 'registered' )->deps[] = 'dashicons';
257 + gutenberg_override_style(
258 + $styles,
259 + 'wp-block-editor-content',
260 + gutenberg_url( 'build/block-editor/content.css' ),
261 + array( 'wp-components' ),
262 + $version
263 + );
264 + $styles->add_data( 'wp-block-editor-content', 'rtl', 'replace' );
130 265
131 - // wp-edit-post: add wp-edit-blocks (custom handle not auto-inferred)
132 - $styles->query( 'wp-edit-post', 'registered' )->deps[] = 'wp-edit-blocks';
266 + // Editor Styles.
267 + gutenberg_override_style(
268 + $styles,
269 + 'wp-block-editor',
270 + gutenberg_url( 'build/block-editor/style.css' ),
271 + array( 'wp-components', 'wp-preferences' ),
272 + $version
273 + );
274 + $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
133 275
134 - // wp-edit-site: add core WP styles and custom handles
135 - $edit_site_style = $styles->query( 'wp-edit-site', 'registered' );
136 - $edit_site_style->deps[] = 'common';
137 - $edit_site_style->deps[] = 'forms';
138 - $edit_site_style->deps[] = 'wp-block-library-editor';
276 + gutenberg_override_style(
277 + $styles,
278 + 'wp-editor',
279 + gutenberg_url( 'build/editor/style.css' ),
280 + array( 'wp-components', 'wp-block-editor', 'wp-patterns', 'wp-reusable-blocks', 'wp-preferences' ),
281 + $version
282 + );
283 + $styles->add_data( 'wp-editor', 'rtl', 'replace' );
139 284
140 - // wp-edit-widgets: add wp-edit-blocks (custom handle not auto-inferred)
141 - $styles->query( 'wp-edit-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
142 -
143 - // wp-customize-widgets: add wp-edit-blocks (custom handle not auto-inferred)
144 - $styles->query( 'wp-customize-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
145 -
146 - // Register wp-base-styles and add it to the already registered wp-admin stylesheet
147 285 gutenberg_override_style(
148 286 $styles,
149 - 'wp-base-styles',
150 - gutenberg_url( 'build/styles/base-styles/admin-schemes' . $suffix . '.css' ),
151 - array(),
287 + 'wp-edit-post',
288 + gutenberg_url( 'build/edit-post/style.css' ),
289 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-commands', 'wp-preferences' ),
152 290 $version
153 291 );
154 - $styles->add_data( 'wp-base-styles', 'rtl', 'replace' );
155 - $styles->add_data( 'wp-base-styles', 'suffix', $suffix );
156 - $styles->add_data( 'wp-base-styles', 'path', gutenberg_dir_path() . 'build/styles/base-styles/admin-schemes' . $suffix . '.css' );
157 - $styles->query( 'wp-admin', 'registered' )->deps[] = 'wp-base-styles';
292 + $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
158 293
159 294 gutenberg_override_style(
160 295 $styles,
161 - 'wp-block-editor-content',
162 - gutenberg_url( 'build/styles/block-editor/content' . $suffix . '.css' ),
163 - array( 'wp-components' ),
296 + 'wp-components',
297 + gutenberg_url( 'build/components/style.css' ),
298 + array( 'dashicons' ),
164 299 $version
165 300 );
166 - $styles->add_data( 'wp-block-editor-content', 'rtl', 'replace' );
167 - $styles->add_data( 'wp-block-editor-content', 'suffix', $suffix );
301 + $styles->add_data( 'wp-components', 'rtl', 'replace' );
168 302
169 303 $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
170 304 gutenberg_override_style(
171 305 $styles,
172 306 'wp-block-library',
173 - gutenberg_url( 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' ),
307 + gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
174 308 array(),
175 309 $version
176 310 );
177 311 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
178 - $styles->add_data( 'wp-block-library', 'suffix', $suffix );
179 - $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' );
312 + $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
180 313
314 + gutenberg_override_style(
315 + $styles,
316 + 'wp-format-library',
317 + gutenberg_url( 'build/format-library/style.css' ),
318 + array( 'wp-block-editor', 'wp-components' ),
319 + $version
320 + );
321 + $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
322 +
181 323 // Only add CONTENT styles here that should be enqueued in the iframe!
182 324 $wp_edit_blocks_dependencies = array(
183 325 'wp-components',
184 326 // This need to be added before the block library styles,
@@ -187,9 +329,8 @@
187 329 'wp-block-library',
188 330 // Until #37466, we can't specifically add them as editor styles yet,
189 331 // so we must hard-code it here as a dependency.
190 332 'wp-block-editor-content',
191 - 'wp-base-styles',
192 333 );
193 334
194 335 // Only load the default layout and margin styles for themes without theme.json file.
195 336 if ( ! wp_theme_has_theme_json() ) {
@@ -204,67 +345,149 @@
204 345
205 346 gutenberg_override_style(
206 347 $styles,
207 348 'wp-reset-editor-styles',
208 - gutenberg_url( 'build/styles/block-library/reset' . $suffix . '.css' ),
349 + gutenberg_url( 'build/block-library/reset.css' ),
209 350 array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles.
210 351 $version
211 352 );
212 353 $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
213 - $styles->add_data( 'wp-reset-editor-styles', 'suffix', $suffix );
214 354
215 355 gutenberg_override_style(
216 356 $styles,
217 357 'wp-editor-classic-layout-styles',
218 - gutenberg_url( 'build/styles/edit-post/classic' . $suffix . '.css' ),
358 + gutenberg_url( 'build/edit-post/classic.css' ),
219 359 array(),
220 360 $version
221 361 );
222 362 $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
223 - $styles->add_data( 'wp-editor-classic-layout-styles', 'suffix', $suffix );
224 363
225 364 gutenberg_override_style(
226 365 $styles,
227 366 'wp-block-library-editor',
228 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
367 + gutenberg_url( 'build/block-library/editor.css' ),
229 368 array(),
230 369 $version
231 370 );
232 371 $styles->add_data( 'wp-block-library-editor', 'rtl', 'replace' );
233 - $styles->add_data( 'wp-block-library-editor', 'suffix', $suffix );
234 372
235 373 gutenberg_override_style(
236 374 $styles,
237 375 'wp-edit-blocks',
238 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
376 + gutenberg_url( 'build/block-library/editor.css' ),
239 377 $wp_edit_blocks_dependencies,
240 378 $version
241 379 );
242 380 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
243 - $styles->add_data( 'wp-edit-blocks', 'suffix', $suffix );
244 381
245 382 gutenberg_override_style(
246 383 $styles,
384 + 'wp-nux',
385 + gutenberg_url( 'build/nux/style.css' ),
386 + array( 'wp-components' ),
387 + $version
388 + );
389 + $styles->add_data( 'wp-nux', 'rtl', 'replace' );
390 +
391 + gutenberg_override_style(
392 + $styles,
247 393 'wp-block-library-theme',
248 - gutenberg_url( 'build/styles/block-library/theme' . $suffix . '.css' ),
394 + gutenberg_url( 'build/block-library/theme.css' ),
249 395 array(),
250 396 $version
251 397 );
252 398 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
253 - $styles->add_data( 'wp-block-library-theme', 'suffix', $suffix );
254 399
255 400 gutenberg_override_style(
256 401 $styles,
257 - 'classic-theme-styles',
258 - gutenberg_url( 'build/styles/block-library/classic' . $suffix . '.css' ),
259 - array(),
402 + 'wp-list-reusable-blocks',
403 + gutenberg_url( 'build/list-reusable-blocks/style.css' ),
404 + array( 'wp-components' ),
260 405 $version
261 406 );
262 - $styles->add_data( 'classic-theme-styles', 'rtl', 'replace' );
263 - $styles->add_data( 'classic-theme-styles', 'suffix', $suffix );
264 - $styles->add_data( 'classic-theme-styles', 'path', gutenberg_dir_path() . 'build/styles/block-library/classic' . $suffix . '.css' );
407 + $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
408 +
409 + gutenberg_override_style(
410 + $styles,
411 + 'wp-commands',
412 + gutenberg_url( 'build/commands/style.css' ),
413 + array( 'wp-components' ),
414 + $version
415 + );
416 + $styles->add_data( 'wp-commands', 'rtl', 'replace' );
417 +
418 + gutenberg_override_style(
419 + $styles,
420 + 'wp-edit-site',
421 + gutenberg_url( 'build/edit-site/style.css' ),
422 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-block-library-editor', 'common', 'forms', 'wp-commands', 'wp-preferences' ),
423 + $version
424 + );
425 + $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
426 +
427 + gutenberg_override_style(
428 + $styles,
429 + 'wp-edit-widgets',
430 + gutenberg_url( 'build/edit-widgets/style.css' ),
431 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-patterns', 'wp-widgets', 'wp-preferences' ),
432 + $version
433 + );
434 + $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
435 +
436 + gutenberg_override_style(
437 + $styles,
438 + 'wp-block-directory',
439 + gutenberg_url( 'build/block-directory/style.css' ),
440 + array( 'wp-block-editor', 'wp-components' ),
441 + $version
442 + );
443 + $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
444 +
445 + gutenberg_override_style(
446 + $styles,
447 + 'wp-customize-widgets',
448 + gutenberg_url( 'build/customize-widgets/style.css' ),
449 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-widgets', 'wp-preferences' ),
450 + $version
451 + );
452 + $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
453 +
454 + gutenberg_override_style(
455 + $styles,
456 + 'wp-patterns',
457 + gutenberg_url( 'build/patterns/style.css' ),
458 + array( 'wp-components' ),
459 + $version
460 + );
461 + $styles->add_data( 'wp-patterns', 'rtl', 'replace' );
462 +
463 + gutenberg_override_style(
464 + $styles,
465 + 'wp-reusable-blocks',
466 + gutenberg_url( 'build/reusable-blocks/style.css' ),
467 + array( 'wp-components' ),
468 + $version
469 + );
470 + $styles->add_data( 'wp-reusable-blocks', 'rtl', 'replace' );
471 +
472 + gutenberg_override_style(
473 + $styles,
474 + 'wp-widgets',
475 + gutenberg_url( 'build/widgets/style.css' ),
476 + array( 'wp-components' )
477 + );
478 + $styles->add_data( 'wp-widgets', 'rtl', 'replace' );
479 +
480 + gutenberg_override_style(
481 + $styles,
482 + 'wp-preferences',
483 + gutenberg_url( 'build/preferences/style.css' ),
484 + array( 'wp-components' ),
485 + $version
486 + );
487 + $styles->add_data( 'wp-preferences', 'rtl', 'replace' );
265 488 }
266 -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles', 15 );
489 +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
267 490
268 491 /**
269 492 * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
270 493 * Styles are stored via the Style Engine API.
@@ -273,9 +496,9 @@
273 496 * However, it is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuous development.
274 497 *
275 498 * @since 6.1
276 499 *
277 - * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
500 + * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
278 501 *
279 502 * @param array $options {
280 503 * Optional. An array of options to pass to gutenberg_style_engine_get_stylesheet_from_context(). Default empty array.
281 504 *
@@ -315,9 +538,9 @@
315 538 }
316 539
317 540 // Combines Core styles.
318 541 if ( ! empty( $compiled_core_stylesheet ) ) {
319 - wp_register_style( $style_tag_id, false, array(), true );
542 + wp_register_style( $style_tag_id, false, array(), true, true );
320 543 wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
321 544 wp_enqueue_style( $style_tag_id );
322 545 }
323 546
@@ -338,9 +561,9 @@
338 561 }
339 562 $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name, $options );
340 563 if ( ! empty( $styles ) ) {
341 564 $key = "wp-style-engine-$store_name";
342 - wp_register_style( $key, false, array(), true );
565 + wp_register_style( $key, false, array(), true, true );
343 566 wp_add_inline_style( $key, $styles );
344 567 wp_enqueue_style( $key );
345 568 }
346 569 }
@@ -357,36 +580,33 @@
357 580 *
358 581 * @param WP_Scripts $scripts WP_Scripts instance.
359 582 */
360 583 function gutenberg_register_vendor_scripts( $scripts ) {
361 - $extension = SCRIPT_DEBUG ? '.js' : '.min.js';
362 - $vendors_dir = gutenberg_dir_path() . 'build/scripts/vendors/';
584 + $extension = SCRIPT_DEBUG ? '.js' : '.min.js';
363 585
364 - $vendor_handles = array( 'react', 'react-dom', 'react-jsx-runtime' );
586 + gutenberg_override_script(
587 + $scripts,
588 + 'react',
589 + gutenberg_url( 'build/vendors/react' . $extension ),
590 + // See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react.
591 + SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' ),
592 + '18'
593 + );
594 + gutenberg_override_script(
595 + $scripts,
596 + 'react-dom',
597 + gutenberg_url( 'build/vendors/react-dom' . $extension ),
598 + array( 'react' ),
599 + '18'
600 + );
365 601
366 - foreach ( $vendor_handles as $handle ) {
367 - $asset_file = $vendors_dir . $handle . '.min.asset.php';
368 - $asset = file_exists( $asset_file ) ? require $asset_file : array();
369 - $dependencies = $asset['dependencies'] ?? array();
370 - $version = $asset['version'] ?? '0';
371 -
372 - gutenberg_override_script(
373 - $scripts,
374 - $handle,
375 - gutenberg_url( 'build/scripts/vendors/' . $handle . $extension ),
376 - $dependencies,
377 - $version
378 - );
379 - }
380 -
381 - // WordPress Core in `wp_register_development_scripts` sets `wp-react-refresh-entry`
382 - // as a dependency to `react` when `SCRIPT_DEBUG` is true. Preserve that here.
383 - if ( SCRIPT_DEBUG ) {
384 - $react = $scripts->query( 'react', 'registered' );
385 - if ( $react && ! in_array( 'wp-react-refresh-entry', $react->deps, true ) ) {
386 - $react->deps[] = 'wp-react-refresh-entry';
387 - }
388 - }
602 + gutenberg_override_script(
603 + $scripts,
604 + 'react-jsx-runtime',
605 + gutenberg_url( 'build/vendors/react-jsx-runtime' . $extension ),
606 + array( 'react' ),
607 + '18'
608 + );
389 609 }
390 610 add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
391 611
392 612 /**
@@ -395,28 +615,53 @@
395 615 * Script modules that are registered by Core will be re-registered by Gutenberg.
396 616 *
397 617 * @since 19.3.0
398 618 */
399 -function gutenberg_define_interactivity_modules_support() {
400 - // Load the auto-generated module registry.
401 - $modules_registry_file = gutenberg_dir_path() . 'build/modules/index.php';
402 - if ( ! file_exists( $modules_registry_file ) ) {
403 - return;
404 - }
619 +function gutenberg_default_script_modules() {
620 + /*
621 + * Expects multidimensional array like:
622 + *
623 + * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
624 + * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
625 + * 'interactivity-router/index.min.js' => …
626 + */
627 + $assets = include gutenberg_dir_path() . '/build-module/assets.php';
405 628
406 - $modules = require $modules_registry_file;
629 + foreach ( $assets as $file_name => $script_module_data ) {
630 + /*
631 + * Build the WordPress Script Module ID from the file name.
632 + * Prepend `@wordpress/` and remove extensions and `/index` if present:
633 + * - interactivity/index.min.js => @wordpress/interactivity
634 + * - interactivity/debug.min.js => @wordpress/interactivity/debug
635 + * - block-library/query/view.js => @wordpress/block-library/query/view
636 + */
637 + $script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?\.min\.js$~D', '', $file_name, 1 );
638 + switch ( $script_module_id ) {
639 + /*
640 + * Interactivity exposes two entrypoints, "/index" and "/debug".
641 + * "/debug" should replace "/index" in development.
642 + */
643 + case '@wordpress/interactivity/debug':
644 + if ( ! SCRIPT_DEBUG ) {
645 + continue 2;
646 + }
647 + $script_module_id = '@wordpress/interactivity';
648 + break;
649 + case '@wordpress/interactivity':
650 + if ( SCRIPT_DEBUG ) {
651 + continue 2;
652 + }
653 + break;
654 + }
407 655
408 - // Add client navigation support to block library modules.
409 - foreach ( $modules as $module ) {
410 - if ( str_starts_with( $module['id'], '@wordpress/block-library' ) && method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
411 - wp_interactivity()->add_client_navigation_support_to_script_module( $module['id'] );
412 - }
656 + $path = gutenberg_url( "build-module/{$file_name}" );
657 + wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
413 658 }
414 659 }
415 -remove_action( 'wp_default_scripts', 'wp_define_interactivity_modules_support' );
416 -add_action( 'wp_default_scripts', 'gutenberg_define_interactivity_modules_support' );
660 +remove_action( 'wp_default_scripts', 'wp_default_script_modules' );
661 +add_action( 'wp_default_scripts', 'gutenberg_default_script_modules' );
417 662
418 -/**
663 +/*
419 664 * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice.
420 665 * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes,
421 666 * which are in continuous development and generally ahead of Core.
422 667 */
@@ -425,30 +670,4 @@
425 670
426 671 // Enqueue stored styles.
427 672 add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
428 673 add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );
429 -
430 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_latex_to_mathml_loader' );
431 -function gutenberg_enqueue_latex_to_mathml_loader() {
432 - wp_enqueue_script_module( '@wordpress/latex-to-mathml/loader' );
433 -}
434 -
435 -/**
436 - * Enqueue the vips loader script module in the block editor.
437 - *
438 - * This registers @wordpress/vips/worker as a dynamic dependency in the import map,
439 - * enabling on-demand loading of the ~3.8MB WASM-based image processing module
440 - * when client-side media processing is triggered via @wordpress/upload-media.
441 - *
442 - * @see packages/vips/src/loader.ts
443 - */
444 -if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
445 - add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_vips_loader' );
446 -}
447 -function gutenberg_enqueue_vips_loader() {
448 - wp_enqueue_script_module( '@wordpress/vips/loader' );
449 -}
450 -
451 -add_action( 'admin_enqueue_scripts', 'gutenberg_enqueue_core_abilities' );
452 -function gutenberg_enqueue_core_abilities() {
453 - wp_enqueue_script_module( '@wordpress/core-abilities' );
454 -}