PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 7.4.0 All 402 releases
← All changes | lib/client-assets.php +279 -178 7.4.012.1.0 View file →
@@ -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,9 @@
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 57 $script = $scripts->query( $handle, 'registered' );
58 58 if ( $script ) {
59 59 /*
60 60 * In many ways, this is a reimplementation of `wp_register_script` but
@@ -87,15 +87,29 @@
87 87 /*
88 88 * `WP_Dependencies::set_translations` will fall over on itself if setting
89 89 * translations on the `wp-i18n` handle, since it internally adds `wp-i18n`
90 90 * as a dependency of itself, exhausting memory. The same applies for the
91 - * polyfill script, which is a dependency _of_ `wp-i18n`.
91 + * polyfill and hooks scripts, which are dependencies _of_ `wp-i18n`.
92 92 *
93 93 * See: https://core.trac.wordpress.org/ticket/46089
94 94 */
95 - if ( 'wp-i18n' !== $handle && 'wp-polyfill' !== $handle ) {
95 + if ( ! in_array( $handle, array( 'wp-i18n', 'wp-polyfill', 'wp-hooks' ), true ) ) {
96 96 $scripts->set_translations( $handle, 'default' );
97 97 }
98 +
99 + /*
100 + * Wp-editor module is exposed as window.wp.editor.
101 + * Problem: there is quite some code expecting window.wp.oldEditor object available under window.wp.editor.
102 + * Solution: fuse the two objects together to maintain backward compatibility.
103 + * For more context, see https://github.com/WordPress/gutenberg/issues/33203
104 + */
105 + if ( 'wp-editor' === $handle ) {
106 + $scripts->add_inline_script(
107 + 'wp-editor',
108 + 'Object.assign( window.wp.editor, window.wp.oldEditor );',
109 + 'after'
110 + );
111 + }
98 112 }
99 113
100 114 /**
101 115 * Filters the default translation file load behavior to load the Gutenberg
@@ -119,9 +133,9 @@
119 133 return $file;
120 134 }
121 135
122 136 // Ignore scripts that are not found in the expected `build/` location.
123 - $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.js';
137 + $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js';
124 138 if ( ! file_exists( $script_path ) ) {
125 139 return $file;
126 140 }
127 141
@@ -149,33 +163,14 @@
149 163 }
150 164 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
151 165
152 166 /**
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 167 * Registers a style according to `wp_register_style`. Honors this request by
173 168 * deregistering any style by the same handler before registration.
174 169 *
175 170 * @since 4.1.0
176 171 *
177 - * @param WP_Styles $styles WP_Styles instance (passed by reference).
172 + * @param WP_Styles $styles WP_Styles instance.
178 173 * @param string $handle Name of the stylesheet. Should be unique.
179 174 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
180 175 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
181 176 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
@@ -185,9 +180,9 @@
185 180 * @param string $media Optional. The media for which this stylesheet has been defined.
186 181 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
187 182 * '(orientation: portrait)' and '(max-width: 640px)'.
188 183 */
189 -function gutenberg_override_style( &$styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
184 +function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
190 185 $style = $styles->query( $handle, 'registered' );
191 186 if ( $style ) {
192 187 $styles->remove( $handle );
193 188 }
@@ -202,42 +197,26 @@
202 197 * should not call any WordPress PHP functions.
203 198 *
204 199 * @since 0.1.0
205 200 *
206 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
201 + * @param WP_Scripts $scripts WP_Scripts instance.
207 202 */
208 -function gutenberg_register_vendor_scripts( &$scripts ) {
203 +function gutenberg_register_vendor_scripts( $scripts ) {
209 204 $suffix = SCRIPT_DEBUG ? '' : '.min';
210 205
211 - // Vendor Scripts.
212 206 $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 207 gutenberg_register_vendor_script(
217 208 $scripts,
218 209 'react',
219 - 'https://unpkg.com/react@16.9.0/umd/react' . $react_suffix . '.js',
220 - array( 'wp-polyfill' ),
221 - '16.9.0',
222 - true
210 + 'https://unpkg.com/react@17.0.1/umd/react' . $react_suffix . '.js',
211 + array( 'wp-polyfill' )
223 212 );
224 213 gutenberg_register_vendor_script(
225 214 $scripts,
226 215 'react-dom',
227 - 'https://unpkg.com/react-dom@16.9.0/umd/react-dom' . $react_suffix . '.js',
228 - array( 'react' ),
229 - '16.9.0',
230 - true
216 + 'https://unpkg.com/react-dom@17.0.1/umd/react-dom' . $react_suffix . '.js',
217 + array( 'react' )
231 218 );
232 - gutenberg_register_vendor_script(
233 - $scripts,
234 - 'lodash',
235 - 'https://unpkg.com/lodash@4.17.15/lodash' . $suffix . '.js',
236 - array(),
237 - '4.17.15',
238 - true
239 - );
240 219 }
241 220 add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
242 221
243 222 /**
@@ -245,23 +224,27 @@
245 224 * `build/` location.
246 225 *
247 226 * @since 4.5.0
248 227 *
249 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
228 + * @param WP_Scripts $scripts WP_Scripts instance.
250 229 */
251 -function gutenberg_register_packages_scripts( &$scripts ) {
252 - foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
230 +function gutenberg_register_packages_scripts( $scripts ) {
231 + // When in production, use the plugin's version as the default asset version;
232 + // else (for development or test) default to use the current time.
233 + $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
234 +
235 + foreach ( glob( gutenberg_dir_path() . 'build/*/index.min.js' ) as $path ) {
253 236 // Prefix `wp-` to package directory to get script handle.
254 - // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
237 + // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`.
255 238 $handle = 'wp-' . basename( dirname( $path ) );
256 239
257 - // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
258 - $asset_file = substr( $path, 0, -3 ) . '.asset.php';
240 + // Replace extension with `.asset.php` to find the generated dependencies file.
241 + $asset_file = substr( $path, 0, -( strlen( '.js' ) ) ) . '.asset.php';
259 242 $asset = file_exists( $asset_file )
260 243 ? require( $asset_file )
261 244 : null;
262 245 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
263 - $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
246 + $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
264 247
265 248 // Add dependencies that cannot be detected and generated by build tools.
266 249 switch ( $handle ) {
267 250 case 'wp-block-library':
@@ -297,18 +280,22 @@
297 280 * `build/` location.
298 281 *
299 282 * @since 6.7.0
300 283
301 - * @param WP_Styles $styles WP_Styles instance (passed by reference).
284 + * @param WP_Styles $styles WP_Styles instance.
302 285 */
303 -function gutenberg_register_packages_styles( &$styles ) {
286 +function gutenberg_register_packages_styles( $styles ) {
287 + // When in production, use the plugin's version as the asset version;
288 + // else (for development or test) default to use the current time.
289 + $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
290 +
304 291 // Editor Styles.
305 292 gutenberg_override_style(
306 293 $styles,
307 294 'wp-block-editor',
308 295 gutenberg_url( 'build/block-editor/style.css' ),
309 - array( 'wp-components', 'wp-editor-font' ),
310 - filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
296 + array( 'wp-components' ),
297 + $version
311 298 );
312 299 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
313 300
314 301 gutenberg_override_style(
@@ -314,10 +301,10 @@
314 301 gutenberg_override_style(
315 302 $styles,
316 303 'wp-editor',
317 304 gutenberg_url( 'build/editor/style.css' ),
318 - array( 'wp-components', 'wp-block-editor', 'wp-nux' ),
319 - filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
305 + array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
306 + $version
320 307 );
321 308 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
322 309
323 310 gutenberg_override_style(
@@ -324,9 +311,9 @@
324 311 $styles,
325 312 'wp-edit-post',
326 313 gutenberg_url( 'build/edit-post/style.css' ),
327 314 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' )
315 + $version
329 316 );
330 317 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
331 318
332 319 gutenberg_override_style(
@@ -332,21 +319,23 @@
332 319 gutenberg_override_style(
333 320 $styles,
334 321 'wp-components',
335 322 gutenberg_url( 'build/components/style.css' ),
336 - array(),
337 - filemtime( gutenberg_dir_path() . 'build/components/style.css' )
323 + array( 'dashicons' ),
324 + $version
338 325 );
339 326 $styles->add_data( 'wp-components', 'rtl', 'replace' );
340 327
328 + $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
341 329 gutenberg_override_style(
342 330 $styles,
343 331 'wp-block-library',
344 - gutenberg_url( 'build/block-library/style.css' ),
332 + gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
345 333 array(),
346 - filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
334 + $version
347 335 );
348 336 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
337 + $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
349 338
350 339 gutenberg_override_style(
351 340 $styles,
352 341 'wp-format-library',
@@ -351,24 +340,57 @@
351 340 $styles,
352 341 'wp-format-library',
353 342 gutenberg_url( 'build/format-library/style.css' ),
354 343 array( 'wp-block-editor', 'wp-components' ),
355 - filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
344 + $version
356 345 );
357 346 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
358 347
348 + $wp_edit_blocks_dependencies = array(
349 + 'wp-components',
350 + 'wp-editor',
351 + // This need to be added before the block library styles,
352 + // The block library styles override the "reset" styles.
353 + 'wp-reset-editor-styles',
354 + 'wp-block-library',
355 + 'wp-reusable-blocks',
356 + );
357 +
358 + // Only load the default layout and margin styles for themes without theme.json file.
359 + if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
360 + $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
361 + }
362 +
363 + global $editor_styles;
364 + if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) {
365 + // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken.
366 + $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
367 + }
368 +
359 369 gutenberg_override_style(
360 370 $styles,
371 + 'wp-reset-editor-styles',
372 + gutenberg_url( 'build/block-library/reset.css' ),
373 + array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Adminn styles.
374 + $version
375 + );
376 + $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
377 +
378 + gutenberg_override_style(
379 + $styles,
380 + 'wp-editor-classic-layout-styles',
381 + gutenberg_url( 'build/edit-post/classic.css' ),
382 + array(),
383 + $version
384 + );
385 + $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
386 +
387 + gutenberg_override_style(
388 + $styles,
361 389 'wp-edit-blocks',
362 390 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' )
391 + $wp_edit_blocks_dependencies,
392 + $version
371 393 );
372 394 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
373 395
374 396 gutenberg_override_style(
@@ -375,9 +397,9 @@
375 397 $styles,
376 398 'wp-nux',
377 399 gutenberg_url( 'build/nux/style.css' ),
378 400 array( 'wp-components' ),
379 - filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
401 + $version
380 402 );
381 403 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
382 404
383 405 gutenberg_override_style(
@@ -384,9 +406,9 @@
384 406 $styles,
385 407 'wp-block-library-theme',
386 408 gutenberg_url( 'build/block-library/theme.css' ),
387 409 array(),
388 - filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
410 + $version
389 411 );
390 412 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
391 413
392 414 gutenberg_override_style(
@@ -393,18 +415,27 @@
393 415 $styles,
394 416 'wp-list-reusable-blocks',
395 417 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
396 418 array( 'wp-components' ),
397 - filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
419 + $version
398 420 );
399 421 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
400 422
401 423 gutenberg_override_style(
402 424 $styles,
425 + 'wp-edit-navigation',
426 + gutenberg_url( 'build/edit-navigation/style.css' ),
427 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
428 + $version
429 + );
430 + $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
431 +
432 + gutenberg_override_style(
433 + $styles,
403 434 'wp-edit-site',
404 435 gutenberg_url( 'build/edit-site/style.css' ),
405 436 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
406 - filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
437 + $version
407 438 );
408 439 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
409 440
410 441 gutenberg_override_style(
@@ -410,10 +441,10 @@
410 441 gutenberg_override_style(
411 442 $styles,
412 443 'wp-edit-widgets',
413 444 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' )
445 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ),
446 + $version
416 447 );
417 448 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
418 449
419 450 gutenberg_override_style(
@@ -420,11 +451,37 @@
420 451 $styles,
421 452 'wp-block-directory',
422 453 gutenberg_url( 'build/block-directory/style.css' ),
423 454 array( 'wp-block-editor', 'wp-components' ),
424 - filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
455 + $version
425 456 );
426 457 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
458 +
459 + gutenberg_override_style(
460 + $styles,
461 + 'wp-customize-widgets',
462 + gutenberg_url( 'build/customize-widgets/style.css' ),
463 + array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ),
464 + $version
465 + );
466 + $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
467 +
468 + gutenberg_override_style(
469 + $styles,
470 + 'wp-reusable-blocks',
471 + gutenberg_url( 'build/reusable-blocks/style.css' ),
472 + array( 'wp-components' ),
473 + $version
474 + );
475 + $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
476 +
477 + gutenberg_override_style(
478 + $styles,
479 + 'wp-widgets',
480 + gutenberg_url( 'build/widgets/style.css' ),
481 + array( 'wp-components' )
482 + );
483 + $styles->add_data( 'wp-widgets', 'rtl', 'replace' );
427 484 }
428 485 add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
429 486
430 487 /**
@@ -433,21 +490,8 @@
433 490 *
434 491 * @since 0.1.0
435 492 */
436 493 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'
448 - );
449 -
450 494 if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
451 495 $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
452 496
453 497 wp_enqueue_script(
@@ -493,9 +537,9 @@
493 537 * Registers a vendor script from a URL, preferring a locally cached version if
494 538 * possible, or downloading it if the cached version is unavailable or
495 539 * outdated.
496 540 *
497 - * @param WP_Scripts $scripts WP_Scripts instance (passed by reference).
541 + * @param WP_Scripts $scripts WP_Scripts instance.
498 542 * @param string $handle Name of the script.
499 543 * @param string $src Full URL of the external script.
500 544 * @param array $deps Optional. An array of registered script handles this
501 545 * script depends on.
@@ -507,9 +551,9 @@
507 551 * Default 'false'.
508 552 *
509 553 * @since 0.1.0
510 554 */
511 -function gutenberg_register_vendor_script( &$scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
555 +function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
512 556 if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
513 557 return;
514 558 }
515 559
@@ -532,17 +576,27 @@
532 576 if ( $needs_fetch ) {
533 577 // Determine whether we can write to this file. If not, don't waste
534 578 // time doing a network request.
535 579 // @codingStandardsIgnoreStart
536 - $f = @fopen( $full_path, 'a' );
580 +
581 + $is_writable = is_writable( $full_path );
582 + if ( $is_writable ) {
583 + $f = @fopen( $full_path, 'a' );
584 + if ( ! $f ) {
585 + $is_writable = false;
586 + } else {
587 + fclose( $f );
588 + }
589 + }
590 +
537 591 // @codingStandardsIgnoreEnd
538 - if ( ! $f ) {
592 + if ( ! $is_writable ) {
539 593 // Failed to open the file for writing, probably due to server
540 594 // permissions. Enqueue the script directly from the URL instead.
541 595 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
542 596 return;
543 597 }
544 - fclose( $f );
598 +
545 599 $response = wp_remote_get( $src );
546 600 if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
547 601 $f = fopen( $full_path, 'w' );
548 602 fwrite( $f, wp_remote_retrieve_body( $response ) );
@@ -566,114 +620,161 @@
566 620 );
567 621 }
568 622
569 623 /**
570 - * Extends block editor settings to include Gutenberg's `editor-styles.css` as
571 - * taking precedent those styles shipped with core.
624 + * Extends block editor settings to remove the Gutenberg's `editor-styles.css`;
572 625 *
626 + * This can be removed when plugin support requires WordPress 5.8.0+.
627 + *
573 628 * @param array $settings Default editor settings.
574 629 *
575 630 * @return array Filtered editor settings.
576 631 */
577 632 function gutenberg_extend_block_editor_styles( $settings ) {
578 - $editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
579 -
580 - /*
581 - * If, for whatever reason, the built editor styles do not exist, avoid
582 - * override and fall back to the default.
583 - */
584 - if ( ! file_exists( $editor_styles_file ) ) {
585 - return $settings;
586 - }
587 -
588 633 if ( empty( $settings['styles'] ) ) {
589 634 $settings['styles'] = array();
590 635 } else {
591 636 /*
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
637 + * WordPress versions prior to 5.8 include a legacy editor styles file
638 + * that need to be removed.
639 + * This code can be removed from the Gutenberg plugin when the supported WP
640 + * version is 5.8
597 641 */
642 + $default_styles_file = is_rtl() ?
643 + ABSPATH . WPINC . '/css/dist/editor/editor-styles-rtl.css' :
644 + ABSPATH . WPINC . '/css/dist/editor/editor-styles.css';
598 645
599 - $default_styles = file_get_contents(
600 - ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
601 - );
646 + if ( file_exists( $default_styles_file ) ) {
647 + $default_styles = file_get_contents(
648 + $default_styles_file
649 + );
602 650
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;
651 + /*
652 + * Iterate backwards from the end of the array since the preferred
653 + * insertion point in case not found is prepended as first entry.
654 + */
655 + for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
656 + if ( isset( $settings['styles'][ $i ]['css'] ) &&
657 + $default_styles === $settings['styles'][ $i ]['css'] ) {
658 + break;
659 + }
611 660 }
661 +
662 + if ( isset( $i ) && $i >= 0 ) {
663 + unset( $settings['styles'][ $i ] );
664 + }
612 665 }
613 666 }
614 667
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 );
668 + // Remove the default font editor styles for FSE themes.
669 + if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
670 + foreach ( $settings['styles'] as $j => $style ) {
671 + if ( 0 === strpos( $style['css'], 'body { font-family:' ) ) {
672 + unset( $settings['styles'][ $j ] );
673 + }
674 + }
624 675 }
625 676
626 677 return $settings;
627 678 }
628 -add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
679 +// This can be removed when plugin support requires WordPress 5.8.0+.
680 +if ( function_exists( 'get_block_editor_settings' ) ) {
681 + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_styles' );
682 +} else {
683 + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
684 +}
629 685
630 686 /**
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.
687 + * Adds a flag to the editor settings to know whether we're in FSE theme or not.
634 688 *
635 - * @param array $preload_paths Array of paths to preload.
636 - * @param WP_Post $post Post being edited.
689 + * This can be removed when plugin support requires WordPress 5.8.0+.
637 690 *
638 - * @return array Filtered array of paths to preload.
691 + * @param array $settings Default editor settings.
692 + *
693 + * @return array Filtered editor settings.
639 694 */
640 -function gutenberg_extend_block_editor_preload_paths( $preload_paths, $post ) {
641 - /*
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.
650 - */
651 - $post_type_object = get_post_type_object( $post->post_type );
695 +function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) {
696 + $settings['supportsTemplateMode'] = gutenberg_supports_block_templates();
652 697
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 );
698 + // Enable the new layout options for themes with a theme.json file.
699 + $settings['supportsLayout'] = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
656 700
657 - if ( ! in_array( $autosaves_path, $preload_paths, true ) ) {
658 - $preload_paths[] = $autosaves_path;
701 + return $settings;
702 +}
703 +// This can be removed when plugin support requires WordPress 5.8.0+.
704 +if ( function_exists( 'get_block_editor_settings' ) ) {
705 + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' );
706 +} else {
707 + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' );
708 +}
709 +
710 +/**
711 + * Sets the editor styles to be consumed by JS.
712 + */
713 +function gutenberg_extend_block_editor_styles_html() {
714 + global $pagenow;
715 +
716 + $script_handles = array();
717 + $style_handles = array(
718 + 'wp-block-editor',
719 + 'wp-block-library',
720 + 'wp-block-library-theme',
721 + 'wp-edit-blocks',
722 + );
723 +
724 + if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
725 + $style_handles[] = 'wp-widgets';
726 + $style_handles[] = 'wp-edit-widgets';
727 + }
728 +
729 + $block_registry = WP_Block_Type_Registry::get_instance();
730 +
731 + foreach ( $block_registry->get_all_registered() as $block_type ) {
732 + if ( ! empty( $block_type->style ) ) {
733 + $style_handles[] = $block_type->style;
659 734 }
735 +
736 + if ( ! empty( $block_type->editor_style ) ) {
737 + $style_handles[] = $block_type->editor_style;
738 + }
739 +
740 + if ( ! empty( $block_type->script ) ) {
741 + $script_handles[] = $block_type->script;
742 + }
660 743 }
661 744
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' );
745 + $style_handles = array_unique( $style_handles );
746 + $done = wp_styles()->done;
672 747
673 - if ( ! in_array( $blocks_path, $preload_paths, true ) ) {
674 - $preload_paths[] = $blocks_path;
675 - }
748 + ob_start();
676 749
677 - return $preload_paths;
750 + // We do not need reset styles for the iframed editor.
751 + wp_styles()->done = array( 'wp-reset-editor-styles' );
752 + wp_styles()->do_items( $style_handles );
753 + wp_styles()->done = $done;
754 +
755 + $styles = ob_get_clean();
756 +
757 + $script_handles = array_unique( $script_handles );
758 + $done = wp_scripts()->done;
759 +
760 + ob_start();
761 +
762 + wp_scripts()->done = array();
763 + wp_scripts()->do_items( $script_handles );
764 + wp_scripts()->done = $done;
765 +
766 + $scripts = ob_get_clean();
767 +
768 + $editor_assets = wp_json_encode(
769 + array(
770 + 'styles' => $styles,
771 + 'scripts' => $scripts,
772 + )
773 + );
774 +
775 + echo "<script>window.__editorAssets = $editor_assets</script>";
678 776 }
679 -add_filter( 'block_editor_preload_paths', 'gutenberg_extend_block_editor_preload_paths', 10, 2 );
777 +add_action( 'admin_footer-appearance_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' );
778 +add_action( 'admin_footer-post.php', 'gutenberg_extend_block_editor_styles_html' );
779 +add_action( 'admin_footer-post-new.php', 'gutenberg_extend_block_editor_styles_html' );
780 +add_action( 'admin_footer-widgets.php', 'gutenberg_extend_block_editor_styles_html' );