PluginProbe
Gutenberg / 7.7.2
Gutenberg v7.7.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 +466 -281 23.5.17.7.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,30 +12,93 @@
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 - * @since 0.1.0
28 - *
29 27 * @param string $path Relative path of the desired file.
30 28 *
31 29 * @return string Fully qualified URL pointing to the desired file.
30 + *
31 + * @since 0.1.0
32 32 */
33 33 function gutenberg_url( $path ) {
34 - return plugins_url( $path, __DIR__ );
34 + return plugins_url( $path, dirname( __FILE__ ) );
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 (passed by reference).
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 + $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 + }
98 +}
99 +
100 +/**
38 101 * Filters the default translation file load behavior to load the Gutenberg
39 102 * plugin translation file, if available.
40 103 *
41 104 * @param string|false $file Path to the translation file to load. False if
@@ -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,391 +149,513 @@
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 (passed by reference).
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 (passed by reference).
207 + */
208 +function gutenberg_register_vendor_scripts( &$scripts ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
209 + // This function is intentionally left empty.
210 + //
211 + // Scripts such as react and react-dom are expected to be overridden soon,
212 + // and it is preferred to keep this function in place so as not to disturb
213 + // tooling related to the plugin build process.
214 + //
215 + // TODO: Remove phpcs exception in function signature once this function
216 + // regains its use.
217 + //
218 + // See https://github.com/WordPress/gutenberg/pull/20628.
219 +}
220 +add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
221 +
222 +/**
223 + * Registers all the WordPress packages scripts that are in the standardized
224 + * `build/` location.
117 225 *
118 - * @global array $editor_styles
226 + * @since 4.5.0
119 227 *
120 - * @param WP_Styles $styles WP_Styles instance.
228 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
121 229 */
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';
230 +function gutenberg_register_packages_scripts( &$scripts ) {
231 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
232 + // Prefix `wp-` to package directory to get script handle.
233 + // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
234 + $handle = 'wp-' . basename( dirname( $path ) );
127 235
128 - // wp-components: add dashicons (icon font dependency)
129 - $styles->query( 'wp-components', 'registered' )->deps[] = 'dashicons';
236 + // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
237 + $asset_file = substr( $path, 0, -3 ) . '.asset.php';
238 + $asset = file_exists( $asset_file )
239 + ? require( $asset_file )
240 + : null;
241 + $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
242 + $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
130 243
131 - // wp-edit-post: add wp-edit-blocks (custom handle not auto-inferred)
132 - $styles->query( 'wp-edit-post', 'registered' )->deps[] = 'wp-edit-blocks';
244 + // Add dependencies that cannot be detected and generated by build tools.
245 + switch ( $handle ) {
246 + case 'wp-block-library':
247 + array_push( $dependencies, 'editor' );
248 + break;
133 249
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';
250 + case 'wp-edit-post':
251 + array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
252 + break;
139 253
140 - // wp-edit-widgets: add wp-edit-blocks (custom handle not auto-inferred)
141 - $styles->query( 'wp-edit-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
254 + case 'wp-edit-site':
255 + array_push( $dependencies, 'wp-dom-ready' );
256 + break;
257 + }
142 258
143 - // wp-customize-widgets: add wp-edit-blocks (custom handle not auto-inferred)
144 - $styles->query( 'wp-customize-widgets', 'registered' )->deps[] = 'wp-edit-blocks';
259 + // Get the path from Gutenberg directory as expected by `gutenberg_url`.
260 + $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
145 261
146 - // Register wp-theme (Design System tokens from @wordpress/theme) as a
147 - // dependency of wp-base-styles so its `:root` token block loads
148 - // everywhere wp-base-styles does, including the editor iframe via
149 - // $wp_edit_blocks_dependencies below.
262 + gutenberg_override_script(
263 + $scripts,
264 + $handle,
265 + gutenberg_url( $gutenberg_path ),
266 + $dependencies,
267 + $version,
268 + true
269 + );
270 + }
271 +}
272 +add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
273 +
274 +/**
275 + * Registers all the WordPress packages styles that are in the standardized
276 + * `build/` location.
277 + *
278 + * @since 6.7.0
279 +
280 + * @param WP_Styles $styles WP_Styles instance (passed by reference).
281 + */
282 +function gutenberg_register_packages_styles( &$styles ) {
283 + // Editor Styles.
150 284 gutenberg_override_style(
151 285 $styles,
152 - 'wp-theme',
153 - gutenberg_url( 'build/styles/theme/design-tokens' . $suffix . '.css' ),
154 - array(),
155 - $version
286 + 'wp-block-editor',
287 + gutenberg_url( 'build/block-editor/style.css' ),
288 + array( 'wp-components', 'wp-editor-font' ),
289 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
156 290 );
157 - $styles->add_data( 'wp-theme', 'rtl', 'replace' );
158 - $styles->add_data( 'wp-theme', 'suffix', $suffix );
159 - $styles->add_data( 'wp-theme', 'path', gutenberg_dir_path() . 'build/styles/theme/design-tokens' . $suffix . '.css' );
291 + $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
160 292
161 - // Register wp-base-styles and add it to the already registered wp-admin stylesheet
162 293 gutenberg_override_style(
163 294 $styles,
164 - 'wp-base-styles',
165 - gutenberg_url( 'build/styles/base-styles/admin-schemes' . $suffix . '.css' ),
166 - array( 'wp-theme' ),
167 - $version
295 + 'wp-editor',
296 + gutenberg_url( 'build/editor/style.css' ),
297 + array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
298 + filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
168 299 );
169 - $styles->add_data( 'wp-base-styles', 'rtl', 'replace' );
170 - $styles->add_data( 'wp-base-styles', 'suffix', $suffix );
171 - $styles->add_data( 'wp-base-styles', 'path', gutenberg_dir_path() . 'build/styles/base-styles/admin-schemes' . $suffix . '.css' );
172 - $styles->query( 'wp-admin', 'registered' )->deps[] = 'wp-base-styles';
300 + $styles->add_data( 'wp-editor', 'rtl', 'replace' );
173 301
174 302 gutenberg_override_style(
175 303 $styles,
176 - 'wp-block-editor-content',
177 - gutenberg_url( 'build/styles/block-editor/content' . $suffix . '.css' ),
178 - array( 'wp-components' ),
179 - $version
304 + 'wp-edit-post',
305 + gutenberg_url( 'build/edit-post/style.css' ),
306 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
307 + filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
180 308 );
181 - $styles->add_data( 'wp-block-editor-content', 'rtl', 'replace' );
182 - $styles->add_data( 'wp-block-editor-content', 'suffix', $suffix );
309 + $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
183 310
184 - $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
185 311 gutenberg_override_style(
186 312 $styles,
313 + 'wp-components',
314 + gutenberg_url( 'build/components/style.css' ),
315 + array(),
316 + filemtime( gutenberg_dir_path() . 'build/components/style.css' )
317 + );
318 + $styles->add_data( 'wp-components', 'rtl', 'replace' );
319 +
320 + gutenberg_override_style(
321 + $styles,
187 322 'wp-block-library',
188 - gutenberg_url( 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' ),
323 + gutenberg_url( 'build/block-library/style.css' ),
189 324 array(),
190 - $version
325 + filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
191 326 );
192 327 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
193 - $styles->add_data( 'wp-block-library', 'suffix', $suffix );
194 - $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/styles/block-library/' . $block_library_filename . $suffix . '.css' );
195 328
196 - // Only add CONTENT styles here that should be enqueued in the iframe!
197 - $wp_edit_blocks_dependencies = array(
198 - // Design System tokens load first so the `:root` CSS custom
199 - // properties are defined before any consuming stylesheet reads
200 - // them inside the editor iframe.
201 - 'wp-theme',
202 - 'wp-components',
203 - // This need to be added before the block library styles,
204 - // The block library styles override the "reset" styles.
205 - 'wp-reset-editor-styles',
206 - 'wp-block-library',
207 - // Until #37466, we can't specifically add them as editor styles yet,
208 - // so we must hard-code it here as a dependency.
209 - 'wp-block-editor-content',
210 - 'wp-base-styles',
329 + gutenberg_override_style(
330 + $styles,
331 + 'wp-format-library',
332 + gutenberg_url( 'build/format-library/style.css' ),
333 + array( 'wp-block-editor', 'wp-components' ),
334 + filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
211 335 );
336 + $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
212 337
213 - // Only load the default layout and margin styles for themes without theme.json file.
214 - if ( ! wp_theme_has_theme_json() ) {
215 - $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
216 - }
338 + gutenberg_override_style(
339 + $styles,
340 + 'wp-edit-blocks',
341 + gutenberg_url( 'build/block-library/editor.css' ),
342 + array(
343 + 'wp-components',
344 + 'wp-editor',
345 + 'wp-block-library',
346 + // Always include visual styles so the editor never appears broken.
347 + 'wp-block-library-theme',
348 + ),
349 + filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
350 + );
351 + $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
217 352
218 - global $editor_styles;
219 - if ( current_theme_supports( 'wp-block-styles' ) && ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) ) {
220 - // Include opinionated block styles if the theme supports block styles and no $editor_styles are declared, so the editor never appears broken.
221 - $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
222 - }
223 -
224 353 gutenberg_override_style(
225 354 $styles,
226 - 'wp-reset-editor-styles',
227 - gutenberg_url( 'build/styles/block-library/reset' . $suffix . '.css' ),
228 - array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles.
229 - $version
355 + 'wp-nux',
356 + gutenberg_url( 'build/nux/style.css' ),
357 + array( 'wp-components' ),
358 + filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
230 359 );
231 - $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
232 - $styles->add_data( 'wp-reset-editor-styles', 'suffix', $suffix );
360 + $styles->add_data( 'wp-nux', 'rtl', 'replace' );
233 361
234 362 gutenberg_override_style(
235 363 $styles,
236 - 'wp-editor-classic-layout-styles',
237 - gutenberg_url( 'build/styles/edit-post/classic' . $suffix . '.css' ),
364 + 'wp-block-library-theme',
365 + gutenberg_url( 'build/block-library/theme.css' ),
238 366 array(),
239 - $version
367 + filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
240 368 );
241 - $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
242 - $styles->add_data( 'wp-editor-classic-layout-styles', 'suffix', $suffix );
369 + $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
243 370
244 371 gutenberg_override_style(
245 372 $styles,
246 - 'wp-block-library-editor',
247 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
248 - array(),
249 - $version
373 + 'wp-list-reusable-blocks',
374 + gutenberg_url( 'build/list-reusable-blocks/style.css' ),
375 + array( 'wp-components' ),
376 + filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
250 377 );
251 - $styles->add_data( 'wp-block-library-editor', 'rtl', 'replace' );
252 - $styles->add_data( 'wp-block-library-editor', 'suffix', $suffix );
378 + $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
253 379
254 380 gutenberg_override_style(
255 381 $styles,
256 - 'wp-edit-blocks',
257 - gutenberg_url( 'build/styles/block-library/editor' . $suffix . '.css' ),
258 - $wp_edit_blocks_dependencies,
259 - $version
382 + 'wp-edit-site',
383 + gutenberg_url( 'build/edit-site/style.css' ),
384 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
385 + filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
260 386 );
261 - $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
262 - $styles->add_data( 'wp-edit-blocks', 'suffix', $suffix );
387 + $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
263 388
264 389 gutenberg_override_style(
265 390 $styles,
266 - 'wp-block-library-theme',
267 - gutenberg_url( 'build/styles/block-library/theme' . $suffix . '.css' ),
268 - array(),
269 - $version
391 + 'wp-edit-widgets',
392 + gutenberg_url( 'build/edit-widgets/style.css' ),
393 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
394 + filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
270 395 );
271 - $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
272 - $styles->add_data( 'wp-block-library-theme', 'suffix', $suffix );
396 + $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
273 397
274 398 gutenberg_override_style(
275 399 $styles,
276 - 'classic-theme-styles',
277 - gutenberg_url( 'build/styles/block-library/classic' . $suffix . '.css' ),
278 - array(),
279 - $version
400 + 'wp-block-directory',
401 + gutenberg_url( 'build/block-directory/style.css' ),
402 + array( 'wp-block-editor', 'wp-components' ),
403 + filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
280 404 );
281 - $styles->add_data( 'classic-theme-styles', 'rtl', 'replace' );
282 - $styles->add_data( 'classic-theme-styles', 'suffix', $suffix );
283 - $styles->add_data( 'classic-theme-styles', 'path', gutenberg_dir_path() . 'build/styles/block-library/classic' . $suffix . '.css' );
405 + $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
284 406 }
285 -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles', 15 );
407 +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
286 408
287 409 /**
288 - * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
289 - * Styles are stored via the Style Engine API.
410 + * Registers common scripts and styles to be used as dependencies of the editor
411 + * and plugins.
290 412 *
291 - * This hook also exists, and should be backported to Core in future versions.
292 - * However, it is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuous development.
413 + * @since 0.1.0
414 + */
415 +function gutenberg_enqueue_block_editor_assets() {
416 + wp_add_inline_script(
417 + 'wp-api-fetch',
418 + sprintf(
419 + 'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );' .
420 + 'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );' .
421 + 'wp.apiFetch.nonceEndpoint = "%s";' .
422 + 'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
423 + ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
424 + admin_url( 'admin-ajax.php?action=gutenberg_rest_nonce' )
425 + ),
426 + 'after'
427 + );
428 +
429 + if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
430 + $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
431 +
432 + wp_enqueue_script(
433 + 'gutenberg-live-reload',
434 + $live_reload_url
435 + );
436 + }
437 +}
438 +add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
439 +
440 +/**
441 + * Retrieves a unique and reasonably short and human-friendly filename for a
442 + * vendor script based on a URL and the script handle.
293 443 *
294 - * @since 6.1
444 + * @param string $handle The name of the script.
445 + * @param string $src Full URL of the external script.
295 446 *
296 - * @link https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
447 + * @return string Script filename suitable for local caching.
297 448 *
298 - * @param array $options {
299 - * Optional. An array of options to pass to gutenberg_style_engine_get_stylesheet_from_context(). Default empty array.
449 + * @since 0.1.0
450 + */
451 +function gutenberg_vendor_script_filename( $handle, $src ) {
452 + $filename = basename( $src );
453 + $match = preg_match(
454 + '/^'
455 + . '(?P<ignore>.*?)'
456 + . '(?P<suffix>\.min)?'
457 + . '(?P<extension>\.js)'
458 + . '(?P<extra>.*)'
459 + . '$/',
460 + $filename,
461 + $filename_pieces
462 + );
463 +
464 + $prefix = $handle;
465 + $suffix = $match ? $filename_pieces['suffix'] : '';
466 + $hash = substr( md5( $src ), 0, 8 );
467 +
468 + return "${prefix}${suffix}.${hash}.js";
469 +}
470 +
471 +/**
472 + * Registers a vendor script from a URL, preferring a locally cached version if
473 + * possible, or downloading it if the cached version is unavailable or
474 + * outdated.
300 475 *
301 - * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
302 - * @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.
303 - * }
476 + * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
477 + * @param string $handle Name of the script.
478 + * @param string $src Full URL of the external script.
479 + * @param array $deps Optional. An array of registered script handles this
480 + * script depends on.
481 + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
482 + * as a query string for cache busting purposes. If version is set to false, a version
483 + * number is automatically added equal to current installed WordPress version.
484 + * If set to null, no version is added.
485 + * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
486 + * Default 'false'.
304 487 *
305 - * @return void
488 + * @since 0.1.0
306 489 */
307 -function gutenberg_enqueue_stored_styles( $options = array() ) {
308 - $is_block_theme = wp_is_block_theme();
309 - $is_classic_theme = ! $is_block_theme;
310 -
311 - /*
312 - * For block themes, print stored styles in the header.
313 - * For classic themes, in the footer.
314 - */
315 - if (
316 - ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
317 - ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) )
318 - ) {
490 +function gutenberg_register_vendor_script( &$scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
491 + if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
319 492 return;
320 493 }
321 494
322 - $core_styles_keys = array( 'block-supports' );
323 - $compiled_core_stylesheet = '';
324 - $style_tag_id = 'core';
325 - foreach ( $core_styles_keys as $style_key ) {
326 - // Adds comment if code is prettified to identify core styles sections in debugging.
327 - $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : SCRIPT_DEBUG;
328 - if ( $should_prettify ) {
329 - $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n";
330 - }
331 - // Chains core store ids to signify what the styles contain.
332 - $style_tag_id .= '-' . $style_key;
333 - $compiled_core_stylesheet .= gutenberg_style_engine_get_stylesheet_from_context( $style_key, $options );
334 - }
495 + $filename = gutenberg_vendor_script_filename( $handle, $src );
335 496
336 - // Combines Core styles.
337 - if ( ! empty( $compiled_core_stylesheet ) ) {
338 - wp_register_style( $style_tag_id, false, array(), true );
339 - wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
340 - wp_enqueue_style( $style_tag_id );
497 + if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
498 + echo "$src|$filename\n";
499 + return;
341 500 }
342 501
343 - // If there are any other stores registered by themes etc., print them out.
344 - $additional_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores();
502 + $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
345 503
346 - /*
347 - * Since the corresponding action hook in Core is removed below,
348 - * this function should still honour any styles stored using the Core Style Engine store.
349 - */
350 - if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
351 - $additional_stores = array_merge( $additional_stores, WP_Style_Engine_CSS_Rules_Store::get_stores() );
352 - }
504 + $needs_fetch = (
505 + defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
506 + ! file_exists( $full_path ) ||
507 + time() - filemtime( $full_path ) >= DAY_IN_SECONDS
508 + )
509 + );
353 510
354 - foreach ( array_keys( $additional_stores ) as $store_name ) {
355 - if ( in_array( $store_name, $core_styles_keys, true ) ) {
356 - continue;
511 + if ( $needs_fetch ) {
512 + // Determine whether we can write to this file. If not, don't waste
513 + // time doing a network request.
514 + // @codingStandardsIgnoreStart
515 + $f = @fopen( $full_path, 'a' );
516 + // @codingStandardsIgnoreEnd
517 + if ( ! $f ) {
518 + // Failed to open the file for writing, probably due to server
519 + // permissions. Enqueue the script directly from the URL instead.
520 + gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
521 + return;
357 522 }
358 - $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name, $options );
359 - if ( ! empty( $styles ) ) {
360 - $key = "wp-style-engine-$store_name";
361 - wp_register_style( $key, false, array(), true );
362 - wp_add_inline_style( $key, $styles );
363 - wp_enqueue_style( $key );
523 + fclose( $f );
524 + $response = wp_remote_get( $src );
525 + if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
526 + $f = fopen( $full_path, 'w' );
527 + fwrite( $f, wp_remote_retrieve_body( $response ) );
528 + fclose( $f );
529 + } elseif ( ! filesize( $full_path ) ) {
530 + // The request failed. If the file is already cached, continue to
531 + // use this file. If not, then unlink the 0 byte file, and enqueue
532 + // the script directly from the URL.
533 + gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
534 + unlink( $full_path );
535 + return;
364 536 }
365 537 }
538 + gutenberg_override_script(
539 + $scripts,
540 + $handle,
541 + gutenberg_url( 'vendor/' . $filename ),
542 + $deps,
543 + $ver,
544 + $in_footer
545 + );
366 546 }
367 547
368 548 /**
369 - * Registers vendor JavaScript files to be used as dependencies of the editor
370 - * and plugins.
549 + * Extends block editor settings to include Gutenberg's `editor-styles.css` as
550 + * taking precedent those styles shipped with core.
371 551 *
372 - * This function is called from a script during the plugin build process, so it
373 - * should not call any WordPress PHP functions.
552 + * @param array $settings Default editor settings.
374 553 *
375 - * @since 13.0
376 - *
377 - * @param WP_Scripts $scripts WP_Scripts instance.
554 + * @return array Filtered editor settings.
378 555 */
379 -function gutenberg_register_vendor_scripts( $scripts ) {
380 - $extension = SCRIPT_DEBUG ? '.js' : '.min.js';
381 - $vendors_dir = gutenberg_dir_path() . 'build/scripts/vendors/';
556 +function gutenberg_extend_block_editor_styles( $settings ) {
557 + $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
382 558
383 - // When the React 19 experiment is enabled, register React 19 vendor
384 - // scripts under the `react`, `react-dom`, and `react-jsx-runtime` handles.
385 - $use_react_19 = gutenberg_is_experiment_enabled( 'gutenberg-react-19' );
559 + /*
560 + * If, for whatever reason, the built editor styles do not exist, avoid
561 + * override and fall back to the default.
562 + */
563 + if ( ! file_exists( $editor_styles_file ) ) {
564 + return $settings;
565 + }
386 566
387 - $vendor_handles = array( 'react', 'react-dom', 'react-jsx-runtime' );
567 + if ( empty( $settings['styles'] ) ) {
568 + $settings['styles'] = array();
569 + } else {
570 + /*
571 + * The styles setting is an array of CSS strings, so there is no direct
572 + * way to find the default styles. To maximize stability, load (again)
573 + * the default styles from disk and find its place in the array.
574 + *
575 + * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
576 + */
388 577
389 - foreach ( $vendor_handles as $handle ) {
390 - $source = $use_react_19 ? $handle . '-19' : $handle;
391 - $asset_file = $vendors_dir . $source . '.min.asset.php';
392 - $asset = file_exists( $asset_file ) ? require $asset_file : array();
393 - $dependencies = $asset['dependencies'] ?? array();
394 - $version = $asset['version'] ?? '0';
578 + $default_styles = file_get_contents(
579 + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
580 + );
395 581
396 - gutenberg_override_script(
397 - $scripts,
398 - $handle,
399 - gutenberg_url( 'build/scripts/vendors/' . $source . $extension ),
400 - $dependencies,
401 - $version
402 - );
582 + /*
583 + * Iterate backwards from the end of the array since the preferred
584 + * insertion point in case not found is prepended as first entry.
585 + */
586 + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
587 + if ( isset( $settings['styles'][ $i ]['css'] ) &&
588 + $default_styles === $settings['styles'][ $i ]['css'] ) {
589 + break;
590 + }
591 + }
403 592 }
404 593
405 - // WordPress Core in `wp_register_development_scripts` sets `wp-react-refresh-entry`
406 - // as a dependency to `react` when `SCRIPT_DEBUG` is true. Preserve that here.
407 - if ( SCRIPT_DEBUG ) {
408 - $react = $scripts->query( 'react', 'registered' );
409 - if ( $react && ! in_array( 'wp-react-refresh-entry', $react->deps, true ) ) {
410 - $react->deps[] = 'wp-react-refresh-entry';
411 - }
594 + $editor_styles = array(
595 + 'css' => file_get_contents( $editor_styles_file ),
596 + );
597 +
598 + // Substitute default styles if found. Otherwise, prepend to setting array.
599 + if ( isset( $i ) && $i >= 0 ) {
600 + $settings['styles'][ $i ] = $editor_styles;
601 + } else {
602 + array_unshift( $settings['styles'], $editor_styles );
412 603 }
604 +
605 + return $settings;
413 606 }
414 -add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
607 +add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
415 608
416 609 /**
417 - * Registers or re-registers Gutenberg Script Modules.
610 + * Extends block editor settings to include a list of image dimensions per size.
418 611 *
419 - * Script modules that are registered by Core will be re-registered by Gutenberg.
612 + * @param array $settings Default editor settings.
420 613 *
421 - * @since 19.3.0
614 + * @return array Filtered editor settings.
422 615 */
423 -function gutenberg_define_interactivity_modules_support() {
424 - // Load the auto-generated module registry.
425 - $modules_registry_file = gutenberg_dir_path() . 'build/modules/index.php';
426 - if ( ! file_exists( $modules_registry_file ) ) {
427 - return;
428 - }
429 -
430 - $modules = require $modules_registry_file;
431 -
432 - // Add client navigation support to block library modules.
433 - foreach ( $modules as $module ) {
434 - if ( str_starts_with( $module['id'], '@wordpress/block-library' ) && method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) {
435 - wp_interactivity()->add_client_navigation_support_to_script_module( $module['id'] );
616 +function gutenberg_extend_settings_image_dimensions( $settings ) {
617 + $image_dimensions = array();
618 + $all_sizes = wp_get_registered_image_subsizes();
619 + foreach ( $settings['imageSizes'] as $size ) {
620 + $key = $size['slug'];
621 + if ( isset( $all_sizes[ $key ] ) ) {
622 + $image_dimensions[ $key ] = $all_sizes[ $key ];
436 623 }
437 624 }
625 + $settings['imageDimensions'] = $image_dimensions;
626 + return $settings;
438 627 }
439 -remove_action( 'wp_default_scripts', 'wp_define_interactivity_modules_support' );
440 -add_action( 'wp_default_scripts', 'gutenberg_define_interactivity_modules_support' );
628 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_image_dimensions' );
441 629
442 630 /**
443 - * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice.
444 - * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes,
445 - * which are in continuous development and generally ahead of Core.
631 + * Load a block pattern by name.
632 + *
633 + * @param string $name Block Pattern File name.
634 + *
635 + * @return array Block Pattern Array.
446 636 */
447 -remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
448 -remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
449 -
450 -// Enqueue stored styles.
451 -add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
452 -add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );
453 -
454 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_latex_to_mathml_loader' );
455 -function gutenberg_enqueue_latex_to_mathml_loader() {
456 - wp_enqueue_script_module( '@wordpress/latex-to-mathml/loader' );
637 +function gutenberg_load_block_pattern( $name ) {
638 + return json_decode(
639 + file_get_contents( __DIR__ . '/patterns/' . $name . '.json' ),
640 + true
641 + );
457 642 }
458 643
459 644 /**
460 - * Enqueue the vips loader script module in the block editor.
645 + * Extends block editor settings to include a list of default block patterns.
461 646 *
462 - * This registers @wordpress/vips/worker as a dynamic dependency in the import map,
463 - * enabling on-demand loading of the ~3.8MB WASM-based image processing module
464 - * when client-side media processing is triggered via @wordpress/upload-media.
647 + * @param array $settings Default editor settings.
465 648 *
466 - * @see packages/vips/src/loader.ts
649 + * @return array Filtered editor settings.
467 650 */
468 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_vips_loader' );
469 -function gutenberg_enqueue_vips_loader() {
470 - wp_enqueue_script_module( '@wordpress/vips/loader' );
651 +function gutenberg_extend_settings_block_patterns( $settings ) {
652 + $block_patterns = [
653 + gutenberg_load_block_pattern( 'text-two-columns' ),
654 + gutenberg_load_block_pattern( 'two-buttons' ),
655 + gutenberg_load_block_pattern( 'cover-abc' ),
656 + gutenberg_load_block_pattern( 'two-images' ),
657 + ];
658 + $settings['__experimentalBlockPatterns'] = $block_patterns;
659 + return $settings;
471 660 }
472 -
473 -add_action( 'admin_enqueue_scripts', 'gutenberg_enqueue_core_abilities' );
474 -function gutenberg_enqueue_core_abilities() {
475 - wp_enqueue_script_module( '@wordpress/core-abilities' );
476 -}
661 +add_filter( 'block_editor_settings', 'gutenberg_extend_settings_block_patterns' );