PluginProbe
Gutenberg / 14.5.2
Gutenberg v14.5.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 +210 -349 7.4.014.5.2 View file →
@@ -1,8 +1,8 @@
1 1 <?php
2 2 /**
3 - * Functions to register client-side assets (scripts and stylesheets) for the
4 - * Gutenberg editor plugin.
3 + * Functions to register client-side assets (scripts and stylesheets) specific
4 + * for the Gutenberg editor plugin.
5 5 *
6 6 * @package gutenberg
7 7 */
8 8
@@ -17,9 +17,9 @@
17 17 *
18 18 * @since 0.1.0
19 19 */
20 20 function gutenberg_dir_path() {
21 - return plugin_dir_path( dirname( __FILE__ ) );
21 + return plugin_dir_path( __DIR__ );
22 22 }
23 23
24 24 /**
25 25 * Retrieves a URL to a file in the gutenberg plugin.
@@ -30,9 +30,9 @@
30 30 *
31 31 * @since 0.1.0
32 32 */
33 33 function gutenberg_url( $path ) {
34 - return plugins_url( $path, dirname( __FILE__ ) );
34 + return plugins_url( $path, __DIR__ );
35 35 }
36 36
37 37 /**
38 38 * Registers a script according to `wp_register_script`. Honors this request by
@@ -41,9 +41,9 @@
41 41 * avoid losing inline scripts which may have been attached.
42 42 *
43 43 * @since 4.1.0
44 44 *
45 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
45 + * @param WP_Scripts $scripts WP_Scripts instance.
46 46 * @param string $handle Name of the script. Should be unique.
47 47 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
48 48 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
49 49 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
@@ -52,9 +52,15 @@
52 52 * If set to null, no version is added.
53 53 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
54 54 * Default 'false'.
55 55 */
56 -function gutenberg_override_script( &$scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
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 +
57 63 $script = $scripts->query( $handle, 'registered' );
58 64 if ( $script ) {
59 65 /*
60 66 * In many ways, this is a reimplementation of `wp_register_script` but
@@ -65,24 +71,11 @@
65 71 // See: `_WP_Dependency::__construct` .
66 72 $script->src = $src;
67 73 $script->deps = $deps;
68 74 $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 - }
75 + $script->args = $in_footer ? 1 : null;
83 76 } else {
84 - $scripts->add( $handle, $src, $deps, $ver, $in_footer );
77 + $scripts->add( $handle, $src, $deps, $ver, ( $in_footer ? 1 : null ) );
85 78 }
86 79
87 80 /*
88 81 * `WP_Dependencies::set_translations` will fall over on itself if setting
@@ -87,15 +80,29 @@
87 80 /*
88 81 * `WP_Dependencies::set_translations` will fall over on itself if setting
89 82 * translations on the `wp-i18n` handle, since it internally adds `wp-i18n`
90 83 * as a dependency of itself, exhausting memory. The same applies for the
91 - * polyfill script, which is a dependency _of_ `wp-i18n`.
84 + * polyfill and hooks scripts, which are dependencies _of_ `wp-i18n`.
92 85 *
93 86 * See: https://core.trac.wordpress.org/ticket/46089
94 87 */
95 - if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
88 + if ( ! in_array( $handle, array( 'wp-i18n', 'wp-polyfill', 'wp-hooks' ), true ) ) {
96 89 $scripts->set_translations( $handle, 'default' );
97 90 }
91 +
92 + /*
93 + * Wp-editor module is exposed as window.wp.editor.
94 + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
95 + * Solution: fuse the two objects together to maintain backward compatibility.
96 + * For more context, see https://github.com/WordPress/gutenberg/issues/33203
97 + */
98 + if ( 'wp-editor' === $handle ) {
99 + $scripts->add_inline_script(
100 + 'wp-editor',
101 + 'Object.assign( window.wp.editor, window.wp.oldEditor );',
102 + 'after'
103 + );
104 + }
98 105 }
99 106
100 107 /**
101 108 * Filters the default translation file load behavior to load the Gutenberg
@@ -114,14 +121,14 @@
114 121 return $file;
115 122 }
116 123
117 124 // Ignore scripts whose handle does not have the "wp-" prefix.
118 - if ( 'wp-' !== substr( $handle, 0, 3 ) ) {
125 + if ( ! str_starts_with( $handle, 'wp-' ) ) {
119 126 return $file;
120 127 }
121 128
122 129 // Ignore scripts that are not found in the expected `build/` location.
123 - $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js';
130 + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js';
124 131 if ( ! file_exists( $script_path ) ) {
125 132 return $file;
126 133 }
127 134
@@ -149,33 +156,14 @@
149 156 }
150 157 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
151 158
152 159 /**
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").
156 - *
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.
162 - */
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 160 * Registers a style according to `wp_register_style`. Honors this request by
173 161 * deregistering any style by the same handler before registration.
174 162 *
175 163 * @since 4.1.0
176 164 *
177 - * @param WP_Styles $styles WP_Styles instance (passed by reference).
165 + * @param WP_Styles $styles WP_Styles instance.
178 166 * @param string $handle Name of the stylesheet. Should be unique.
179 167 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
180 168 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
181 169 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
@@ -185,9 +173,9 @@
185 173 * @param string $media Optional. The media for which this stylesheet has been defined.
186 174 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
187 175 * '(orientation: portrait)' and '(max-width: 640px)'.
188 176 */
189 -function gutenberg_override_style( &$styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
177 +function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
190 178 $style = $styles->query( $handle, 'registered' );
191 179 if ( $style ) {
192 180 $styles->remove( $handle );
193 181 }
@@ -194,74 +182,32 @@
194 182 $styles->add( $handle, $src, $deps, $ver, $media );
195 183 }
196 184
197 185 /**
198 - * Registers vendor JavaScript files to be used as dependencies of the editor
199 - * and plugins.
200 - *
201 - * This function is called from a script during the plugin build process, so it
202 - * should not call any WordPress PHP functions.
203 - *
204 - * @since 0.1.0
205 - *
206 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
207 - */
208 -function gutenberg_register_vendor_scripts( &$scripts ) {
209 - $suffix = SCRIPT_DEBUG ? '' : '.min';
210 -
211 - // Vendor Scripts.
212 - $react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
213 -
214 - // TODO: Overrides for react, react-dom and lodash are necessary
215 - // until WordPress 5.3 is released.
216 - gutenberg_register_vendor_script(
217 - $scripts,
218 - 'react',
219 - 'https://unpkg.com/[email protected]/umd/react' . $react_suffix . '.js',
220 - array( 'wp-polyfill' ),
221 - '16.9.0',
222 - true
223 - );
224 - gutenberg_register_vendor_script(
225 - $scripts,
226 - 'react-dom',
227 - 'https://unpkg.com/[email protected]/umd/react-dom' . $react_suffix . '.js',
228 - array( 'react' ),
229 - '16.9.0',
230 - true
231 - );
232 - gutenberg_register_vendor_script(
233 - $scripts,
234 - 'lodash',
235 - 'https://unpkg.com/[email protected]/lodash' . $suffix . '.js',
236 - array(),
237 - '4.17.15',
238 - true
239 - );
240 -}
241 -add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
242 -
243 -/**
244 186 * Registers all the WordPress packages scripts that are in the standardized
245 187 * `build/` location.
246 188 *
247 189 * @since 4.5.0
248 190 *
249 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
191 + * @param WP_Scripts $scripts WP_Scripts instance.
250 192 */
251 -function gutenberg_register_packages_scripts( &$scripts ) {
252 - foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
193 +function gutenberg_register_packages_scripts( $scripts ) {
194 + // When in production, use the plugin's version as the default asset version;
195 + // else (for development or test) default to use the current time.
196 + $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
197 +
198 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.min.js' ) as $path ) {
253 199 // Prefix `wp-` to package directory to get script handle.
254 - // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
200 + // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`.
255 201 $handle = 'wp-' . basename( dirname( $path ) );
256 202
257 - // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
258 - $asset_file = substr( $path, 0, -3 ) . '.asset.php';
203 + // Replace extension with `.asset.php` to find the generated dependencies file.
204 + $asset_file = substr( $path, 0, -( strlen( '.js' ) ) ) . '.asset.php';
259 205 $asset = file_exists( $asset_file )
260 - ? require( $asset_file )
206 + ? require $asset_file
261 207 : null;
262 208 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
263 - $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
209 + $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
264 210
265 211 // Add dependencies that cannot be detected and generated by build tools.
266 212 switch ( $handle ) {
267 213 case 'wp-block-library':
@@ -297,18 +243,22 @@
297 243 * `build/` location.
298 244 *
299 245 * @since 6.7.0
300 246
301 - * @param WP_Styles $styles WP_Styles instance (passed by reference).
247 + * @param WP_Styles $styles WP_Styles instance.
302 248 */
303 -function gutenberg_register_packages_styles( &$styles ) {
249 +function gutenberg_register_packages_styles( $styles ) {
250 + // When in production, use the plugin's version as the asset version;
251 + // else (for development or test) default to use the current time.
252 + $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
253 +
304 254 // Editor Styles.
305 255 gutenberg_override_style(
306 256 $styles,
307 257 'wp-block-editor',
308 258 gutenberg_url( 'build/block-editor/style.css' ),
309 - array( 'wp-components', 'wp-editor-font' ),
310 - filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
259 + array( 'wp-components' ),
260 + $version
311 261 );
312 262 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
313 263
314 264 gutenberg_override_style(
@@ -314,10 +264,10 @@
314 264 gutenberg_override_style(
315 265 $styles,
316 266 'wp-editor',
317 267 gutenberg_url( 'build/editor/style.css' ),
318 - array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
319 - filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
268 + array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
269 + $version
320 270 );
321 271 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
322 272
323 273 gutenberg_override_style(
@@ -324,9 +274,9 @@
324 274 $styles,
325 275 'wp-edit-post',
326 276 gutenberg_url( 'build/edit-post/style.css' ),
327 277 array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
328 - filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
278 + $version
329 279 );
330 280 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
331 281
332 282 gutenberg_override_style(
@@ -332,21 +282,23 @@
332 282 gutenberg_override_style(
333 283 $styles,
334 284 'wp-components',
335 285 gutenberg_url( 'build/components/style.css' ),
336 - array(),
337 - filemtime( gutenberg_dir_path() . 'build/components/style.css' )
286 + array( 'dashicons' ),
287 + $version
338 288 );
339 289 $styles->add_data( 'wp-components', 'rtl', 'replace' );
340 290
291 + $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
341 292 gutenberg_override_style(
342 293 $styles,
343 294 'wp-block-library',
344 - gutenberg_url( 'build/block-library/style.css' ),
295 + gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
345 296 array(),
346 - filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
297 + $version
347 298 );
348 299 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
300 + $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
349 301
350 302 gutenberg_override_style(
351 303 $styles,
352 304 'wp-format-library',
@@ -351,24 +303,56 @@
351 303 $styles,
352 304 'wp-format-library',
353 305 gutenberg_url( 'build/format-library/style.css' ),
354 306 array( 'wp-block-editor', 'wp-components' ),
355 - filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
307 + $version
356 308 );
357 309 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
358 310
311 + $wp_edit_blocks_dependencies = array(
312 + 'wp-components',
313 + // This need to be added before the block library styles,
314 + // The block library styles override the "reset" styles.
315 + 'wp-reset-editor-styles',
316 + 'wp-block-library',
317 + 'wp-reusable-blocks',
318 + );
319 +
320 + // Only load the default layout and margin styles for themes without theme.json file.
321 + if ( ! wp_theme_has_theme_json() ) {
322 + $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
323 + }
324 +
325 + global $editor_styles;
326 + if ( current_theme_supports( 'wp-block-styles' ) && ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) ) {
327 + // Include opinionated block styles if the theme supports block styles and no $editor_styles are declared, so the editor never appears broken.
328 + $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
329 + }
330 +
359 331 gutenberg_override_style(
360 332 $styles,
333 + 'wp-reset-editor-styles',
334 + gutenberg_url( 'build/block-library/reset.css' ),
335 + array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Admin styles.
336 + $version
337 + );
338 + $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
339 +
340 + gutenberg_override_style(
341 + $styles,
342 + 'wp-editor-classic-layout-styles',
343 + gutenberg_url( 'build/edit-post/classic.css' ),
344 + array(),
345 + $version
346 + );
347 + $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
348 +
349 + gutenberg_override_style(
350 + $styles,
361 351 'wp-edit-blocks',
362 352 gutenberg_url( 'build/block-library/editor.css' ),
363 - array(
364 - 'wp-components',
365 - 'wp-editor',
366 - 'wp-block-library',
367 - // Always include visual styles so the editor never appears broken.
368 - 'wp-block-library-theme',
369 - ),
370 - filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
353 + $wp_edit_blocks_dependencies,
354 + $version
371 355 );
372 356 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
373 357
374 358 gutenberg_override_style(
@@ -375,9 +359,9 @@
375 359 $styles,
376 360 'wp-nux',
377 361 gutenberg_url( 'build/nux/style.css' ),
378 362 array( 'wp-components' ),
379 - filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
363 + $version
380 364 );
381 365 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
382 366
383 367 gutenberg_override_style(
@@ -384,9 +368,9 @@
384 368 $styles,
385 369 'wp-block-library-theme',
386 370 gutenberg_url( 'build/block-library/theme.css' ),
387 371 array(),
388 - filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
372 + $version
389 373 );
390 374 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
391 375
392 376 gutenberg_override_style(
@@ -393,18 +377,27 @@
393 377 $styles,
394 378 'wp-list-reusable-blocks',
395 379 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
396 380 array( 'wp-components' ),
397 - filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
381 + $version
398 382 );
399 383 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
400 384
401 385 gutenberg_override_style(
402 386 $styles,
387 + 'wp-edit-navigation',
388 + gutenberg_url( 'build/edit-navigation/style.css' ),
389 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks' ),
390 + $version
391 + );
392 + $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
393 +
394 + gutenberg_override_style(
395 + $styles,
403 396 'wp-edit-site',
404 397 gutenberg_url( 'build/edit-site/style.css' ),
405 - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
406 - filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
398 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks' ),
399 + $version
407 400 );
408 401 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
409 402
410 403 gutenberg_override_style(
@@ -410,10 +403,10 @@
410 403 gutenberg_override_style(
411 404 $styles,
412 405 'wp-edit-widgets',
413 406 gutenberg_url( 'build/edit-widgets/style.css' ),
414 - array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
415 - filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
407 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ),
408 + $version
416 409 );
417 410 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
418 411
419 412 gutenberg_override_style(
@@ -420,260 +413,128 @@
420 413 $styles,
421 414 'wp-block-directory',
422 415 gutenberg_url( 'build/block-directory/style.css' ),
423 416 array( 'wp-block-editor', 'wp-components' ),
424 - filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
417 + $version
425 418 );
426 419 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
427 -}
428 -add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
429 420
430 -/**
431 - * Registers common scripts and styles to be used as dependencies of the editor
432 - * and plugins.
433 - *
434 - * @since 0.1.0
435 - */
436 -function gutenberg_enqueue_block_editor_assets() {
437 - wp_add_inline_script(
438 - 'wp-api-fetch',
439 - sprintf(
440 - 'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );' .
441 - 'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );' .
442 - 'wp.apiFetch.nonceEndpoint = "%s";' .
443 - 'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
444 - ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
445 - admin_url( 'admin-ajax.php?action=gutenberg_rest_nonce' )
446 - ),
447 - 'after'
421 + gutenberg_override_style(
422 + $styles,
423 + 'wp-customize-widgets',
424 + gutenberg_url( 'build/customize-widgets/style.css' ),
425 + array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-widgets' ),
426 + $version
448 427 );
428 + $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
449 429
450 - if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
451 - $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
430 + gutenberg_override_style(
431 + $styles,
432 + 'wp-reusable-blocks',
433 + gutenberg_url( 'build/reusable-blocks/style.css' ),
434 + array( 'wp-components' ),
435 + $version
436 + );
437 + $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
452 438
453 - wp_enqueue_script(
454 - 'gutenberg-live-reload',
455 - $live_reload_url
456 - );
457 - }
439 + gutenberg_override_style(
440 + $styles,
441 + 'wp-widgets',
442 + gutenberg_url( 'build/widgets/style.css' ),
443 + array( 'wp-components' )
444 + );
445 + $styles->add_data( 'wp-widgets', 'rtl', 'replace' );
458 446 }
459 -add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
447 +add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
460 448
461 449 /**
462 - * Retrieves a unique and reasonably short and human-friendly filename for a
463 - * vendor script based on a URL and the script handle.
450 + * Fetches, processes and compiles stored core styles, then combines and renders them to the page.
451 + * Styles are stored via the Style Engine API.
464 452 *
465 - * @param string $handle The name of the script.
466 - * @param string $src Full URL of the external script.
453 + * This hook also exists, and should be backported to Core in future versions.
454 + * However, it is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuous development.
467 455 *
468 - * @return string Script filename suitable for local caching.
456 + * See: https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/
469 457 *
470 - * @since 0.1.0
471 - */
472 -function gutenberg_vendor_script_filename( $handle, $src ) {
473 - $filename = basename( $src );
474 - $match = preg_match(
475 - '/^'
476 - . '(?P<ignore>.*?)'
477 - . '(?P<suffix>\.min)?'
478 - . '(?P<extension>\.js)'
479 - . '(?P<extra>.*)'
480 - . '$/',
481 - $filename,
482 - $filename_pieces
483 - );
484 -
485 - $prefix = $handle;
486 - $suffix = $match ? $filename_pieces['suffix'] : '';
487 - $hash = substr( md5( $src ), 0, 8 );
488 -
489 - return "${prefix}${suffix}.${hash}.js";
490 -}
491 -
492 -/**
493 - * Registers a vendor script from a URL, preferring a locally cached version if
494 - * possible, or downloading it if the cached version is unavailable or
495 - * outdated.
458 + * @param array $options {
459 + * Optional. An array of options to pass to gutenberg_style_engine_get_stylesheet_from_context(). Default empty array.
496 460 *
497 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
498 - * @param string $handle Name of the script.
499 - * @param string $src Full URL of the external script.
500 - * @param array $deps Optional. An array of registered script handles this
501 - * script depends on.
502 - * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
503 - * as a query string for cache busting purposes. If version is set to false, a version
504 - * number is automatically added equal to current installed WordPress version.
505 - * If set to null, no version is added.
506 - * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
507 - * Default 'false'.
461 + * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
462 + * @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.
463 + * }
508 464 *
509 - * @since 0.1.0
510 - */
511 -function gutenberg_register_vendor_script( &$scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
512 - if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
513 - return;
514 - }
515 -
516 - $filename = gutenberg_vendor_script_filename( $handle, $src );
517 -
518 - if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
519 - echo "$src|$filename\n";
520 - return;
521 - }
522 -
523 - $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
524 -
525 - $needs_fetch = (
526 - defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
527 - ! file_exists( $full_path ) ||
528 - time() - filemtime( $full_path ) >= DAY_IN_SECONDS
529 - )
530 - );
531 -
532 - if ( $needs_fetch ) {
533 - // Determine whether we can write to this file. If not, don't waste
534 - // time doing a network request.
535 - // @codingStandardsIgnoreStart
536 - $f = @fopen( $full_path, 'a' );
537 - // @codingStandardsIgnoreEnd
538 - if ( ! $f ) {
539 - // Failed to open the file for writing, probably due to server
540 - // permissions. Enqueue the script directly from the URL instead.
541 - gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
542 - return;
543 - }
544 - fclose( $f );
545 - $response = wp_remote_get( $src );
546 - if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
547 - $f = fopen( $full_path, 'w' );
548 - fwrite( $f, wp_remote_retrieve_body( $response ) );
549 - fclose( $f );
550 - } elseif ( ! filesize( $full_path ) ) {
551 - // The request failed. If the file is already cached, continue to
552 - // use this file. If not, then unlink the 0 byte file, and enqueue
553 - // the script directly from the URL.
554 - gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
555 - unlink( $full_path );
556 - return;
557 - }
558 - }
559 - gutenberg_override_script(
560 - $scripts,
561 - $handle,
562 - gutenberg_url( 'vendor/' . $filename ),
563 - $deps,
564 - $ver,
565 - $in_footer
566 - );
567 -}
568 -
569 -/**
570 - * Extends block editor settings to include Gutenberg's `editor-styles.css` as
571 - * taking precedent those styles shipped with core.
465 + * @since 6.1
572 466 *
573 - * @param array $settings Default editor settings.
574 - *
575 - * @return array Filtered editor settings.
467 + * @return void
576 468 */
577 -function gutenberg_extend_block_editor_styles( $settings ) {
578 - $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
469 +function gutenberg_enqueue_stored_styles( $options = array() ) {
470 + $is_block_theme = wp_is_block_theme();
471 + $is_classic_theme = ! $is_block_theme;
579 472
580 473 /*
581 - * If, for whatever reason, the built editor styles do not exist, avoid
582 - * override and fall back to the default.
474 + * For block themes, print stored styles in the header.
475 + * For classic themes, in the footer.
583 476 */
584 - if ( ! file_exists( $editor_styles_file ) ) {
585 - return $settings;
477 + if (
478 + ( $is_block_theme && doing_action( 'wp_footer' ) ) ||
479 + ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) )
480 + ) {
481 + return;
586 482 }
587 483
588 - if ( empty( $settings['styles'] ) ) {
589 - $settings['styles'] = array();
590 - } else {
591 - /*
592 - * The styles setting is an array of CSS strings, so there is no direct
593 - * way to find the default styles. To maximize stability, load (again)
594 - * the default styles from disk and find its place in the array.
595 - *
596 - * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
597 - */
598 -
599 - $default_styles = file_get_contents(
600 - ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
601 - );
602 -
603 - /*
604 - * Iterate backwards from the end of the array since the preferred
605 - * insertion point in case not found is prepended as first entry.
606 - */
607 - for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
608 - if ( isset( $settings['styles'][ $i ]['css'] ) &&
609 - $default_styles === $settings['styles'][ $i ]['css'] ) {
610 - break;
611 - }
484 + $core_styles_keys = array( 'block-supports' );
485 + $compiled_core_stylesheet = '';
486 + $style_tag_id = 'core';
487 + foreach ( $core_styles_keys as $style_key ) {
488 + // Adds comment if code is prettified to identify core styles sections in debugging.
489 + $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
490 + if ( $should_prettify ) {
491 + $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n";
612 492 }
493 + // Chains core store ids to signify what the styles contain.
494 + $style_tag_id .= '-' . $style_key;
495 + $compiled_core_stylesheet .= gutenberg_style_engine_get_stylesheet_from_context( $style_key, $options );
613 496 }
614 497
615 - $editor_styles = array(
616 - 'css' => file_get_contents( $editor_styles_file ),
617 - );
618 -
619 - // Substitute default styles if found. Otherwise, prepend to setting array.
620 - if ( isset( $i ) && $i >= 0 ) {
621 - $settings['styles'][ $i ] = $editor_styles;
622 - } else {
623 - array_unshift( $settings['styles'], $editor_styles );
498 + // Combines Core styles.
499 + if ( ! empty( $compiled_core_stylesheet ) ) {
500 + wp_register_style( $style_tag_id, false, array(), true, true );
501 + wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet );
502 + wp_enqueue_style( $style_tag_id );
624 503 }
625 504
626 - return $settings;
627 -}
628 -add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
505 + // If there are any other stores registered by themes etc., print them out.
506 + $additional_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores();
629 507
630 -/**
631 - * Extends block editor preload paths to preload additional data. Note that any
632 - * additions here should be complemented with a corresponding core ticket to
633 - * reconcile the change upstream for future removal from Gutenberg.
634 - *
635 - * @param array $preload_paths Array of paths to preload.
636 - * @param WP_Post $post Post being edited.
637 - *
638 - * @return array Filtered array of paths to preload.
639 - */
640 -function gutenberg_extend_block_editor_preload_paths( $preload_paths, $post ) {
641 508 /*
642 - * Preload any autosaves for the post. (see https://github.com/WordPress/gutenberg/pull/7945)
643 - *
644 - * Trac ticket: https://core.trac.wordpress.org/ticket/46974
645 - *
646 - * At the time of writing, the change is not committed or released
647 - * in core. This path should be removed from Gutenberg when the code is
648 - * released in core, and the corresponding release version becomes
649 - * the minimum supported version.
509 + * Since the corresponding action hook in Core is removed below,
510 + * this function should still honour any styles stored using the Core Style Engine store.
650 511 */
651 - $post_type_object = get_post_type_object( $post->post_type );
512 + if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
513 + $additional_stores = array_merge( $additional_stores, WP_Style_Engine_CSS_Rules_Store::get_stores() );
514 + }
652 515
653 - if ( isset( $post_type_object ) ) {
654 - $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
655 - $autosaves_path = sprintf( '/wp/v2/%s/%d/autosaves?context=edit', $rest_base, $post->ID );
656 -
657 - if ( ! in_array( $autosaves_path, $preload_paths, true ) ) {
658 - $preload_paths[] = $autosaves_path;
516 + foreach ( array_keys( $additional_stores ) as $store_name ) {
517 + if ( in_array( $store_name, $core_styles_keys, true ) ) {
518 + continue;
659 519 }
520 + $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name, $options );
521 + if ( ! empty( $styles ) ) {
522 + $key = "wp-style-engine-$store_name";
523 + wp_register_style( $key, false, array(), true, true );
524 + wp_add_inline_style( $key, $styles );
525 + wp_enqueue_style( $key );
526 + }
660 527 }
528 +}
661 529
662 - /*
663 - * Used in considering user permissions for creating and updating blocks,
664 - * as condition for displaying relevant actions in the interface.
665 - *
666 - * Trac ticket: https://core.trac.wordpress.org/ticket/46429
667 - *
668 - * This is present in WordPress 5.2 and should be removed from Gutenberg
669 - * once WordPress 5.2 is the minimum supported version.
670 - */
671 - $blocks_path = array( '/wp/v2/blocks', 'OPTIONS' );
530 +/*
531 + * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice.
532 + * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes,
533 + * which are in continuous development and generally ahead of Core.
534 + */
535 +remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
536 +remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
672 537
673 - if ( ! in_array( $blocks_path, $preload_paths, true ) ) {
674 - $preload_paths[] = $blocks_path;
675 - }
676 -
677 - return $preload_paths;
678 -}
679 -add_filter( 'block_editor_preload_paths', 'gutenberg_extend_block_editor_preload_paths', 10, 2 );
538 +// Enqueue stored styles.
539 +add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' );
540 +add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 );