PluginProbe
Gutenberg / 10.9.1
Gutenberg v10.9.1
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
gutenberg / lib / client-assets.php

client-assets.php in Gutenberg 10.9.1, at lib/client-assets.php

806 lines 25.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to register client-side assets (scripts and stylesheets) for the
4 * Gutenberg editor plugin.
5 *
6 * @package gutenberg
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'Silence is golden.' );
11 }
12
13 /**
14 * Retrieves the root plugin path.
15 *
16 * @return string Root path to the gutenberg plugin.
17 *
18 * @since 0.1.0
19 */
20 function gutenberg_dir_path() {
21 return plugin_dir_path( __DIR__ );
22 }
23
24 /**
25 * Retrieves a URL to a file in the gutenberg plugin.
26 *
27 * @param string $path Relative path of the desired file.
28 *
29 * @return string Fully qualified URL pointing to the desired file.
30 *
31 * @since 0.1.0
32 */
33 function gutenberg_url( $path ) {
34 return plugins_url( $path, __DIR__ );
35 }
36
37 /**
38 * Registers a script according to `wp_register_script`. Honors this request by
39 * reassigning internal dependency properties of any script handle already
40 * registered by that name. It does not deregister the original script, to
41 * avoid losing inline scripts which may have been attached.
42 *
43 * @since 4.1.0
44 *
45 * @param WP_Scripts $scripts WP_Scripts instance.
46 * @param string $handle Name of the script. Should be unique.
47 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
48 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
49 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
50 * as a query string for cache busting purposes. If version is set to false, a version
51 * number is automatically added equal to current installed WordPress version.
52 * If set to null, no version is added.
53 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
54 * Default 'false'.
55 */
56 function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
57 $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 and hooks scripts, which are dependencies _of_ `wp-i18n`.
92 *
93 * See: https://core.trac.wordpress.org/ticket/46089
94 */
95 if ( ! in_array( $handle, array( 'wp-i18n', 'wp-polyfill', 'wp-hooks' ), true ) ) {
96 $scripts->set_translations( $handle, 'default' );
97 }
98
99 // Remove this check once the minimum supported WordPress version is at least 5.7.
100 if ( 'wp-i18n' === $handle ) {
101 $ltr = 'rtl' === _x( 'ltr', 'text direction', 'default' ) ? 'rtl' : 'ltr';
102 $output = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] }, 'default' );", $ltr );
103 $scripts->add_inline_script( 'wp-i18n', $output, 'after' );
104 }
105 }
106
107 /**
108 * Filters the default translation file load behavior to load the Gutenberg
109 * plugin translation file, if available.
110 *
111 * @param string|false $file Path to the translation file to load. False if
112 * there isn't one.
113 * @param string $handle Name of the script to register a translation
114 * domain to.
115 *
116 * @return string|false Filtered path to the Gutenberg translation file, if
117 * available.
118 */
119 function gutenberg_override_translation_file( $file, $handle ) {
120 if ( ! $file ) {
121 return $file;
122 }
123
124 // Ignore scripts whose handle does not have the "wp-" prefix.
125 if ( 'wp-' !== substr( $handle, 0, 3 ) ) {
126 return $file;
127 }
128
129 // Ignore scripts that are not found in the expected `build/` location.
130 $script_path = gutenberg_dir_path() . 'build/' . substr( $handle, 3 ) . '/index.min.js';
131 if ( ! file_exists( $script_path ) ) {
132 return $file;
133 }
134
135 /*
136 * The default file will be in the plugins language directory, omitting the
137 * domain since Gutenberg assigns the script translations as the default.
138 *
139 * Example: /www/wp-content/languages/plugins/de_DE-07d88e6a803e01276b9bfcc1203e862e.json
140 *
141 * The logic of `load_script_textdomain` is such that it will assume to
142 * search in the plugins language directory, since the assigned source of
143 * the overridden Gutenberg script originates in the plugins directory.
144 *
145 * The plugin translation files each begin with the slug of the plugin, so
146 * it's a simple matter of prepending the Gutenberg plugin slug.
147 */
148 $path_parts = pathinfo( $file );
149 $plugin_translation_file = (
150 $path_parts['dirname'] .
151 '/gutenberg-' .
152 $path_parts['basename']
153 );
154
155 return $plugin_translation_file;
156 }
157 add_filter( 'load_script_translation_file', 'gutenberg_override_translation_file', 10, 2 );
158
159 /**
160 * Registers a style according to `wp_register_style`. Honors this request by
161 * deregistering any style by the same handler before registration.
162 *
163 * @since 4.1.0
164 *
165 * @param WP_Styles $styles WP_Styles instance.
166 * @param string $handle Name of the stylesheet. Should be unique.
167 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
168 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
169 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
170 * as a query string for cache busting purposes. If version is set to false, a version
171 * number is automatically added equal to current installed WordPress version.
172 * If set to null, no version is added.
173 * @param string $media Optional. The media for which this stylesheet has been defined.
174 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
175 * '(orientation: portrait)' and '(max-width: 640px)'.
176 */
177 function gutenberg_override_style( $styles, $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
178 $style = $styles->query( $handle, 'registered' );
179 if ( $style ) {
180 $styles->remove( $handle );
181 }
182 $styles->add( $handle, $src, $deps, $ver, $media );
183 }
184
185 /**
186 * Registers vendor JavaScript files to be used as dependencies of the editor
187 * and plugins.
188 *
189 * This function is called from a script during the plugin build process, so it
190 * should not call any WordPress PHP functions.
191 *
192 * @since 0.1.0
193 *
194 * @param WP_Scripts $scripts WP_Scripts instance.
195 */
196 function gutenberg_register_vendor_scripts( $scripts ) {
197 $suffix = SCRIPT_DEBUG ? '' : '.min';
198
199 $react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
200 gutenberg_register_vendor_script(
201 $scripts,
202 'react',
203 'https://unpkg.com/react@17.0.1/umd/react' . $react_suffix . '.js',
204 array( 'wp-polyfill' )
205 );
206 gutenberg_register_vendor_script(
207 $scripts,
208 'react-dom',
209 'https://unpkg.com/react-dom@17.0.1/umd/react-dom' . $react_suffix . '.js',
210 array( 'react' )
211 );
212
213 /*
214 * This script registration and the corresponding function should be removed
215 * removed once the plugin is updated to support WordPress 5.7.0 and newer.
216 */
217 gutenberg_register_vendor_script(
218 $scripts,
219 'object-fit-polyfill',
220 'https://unpkg.com/objectFitPolyfill@2.3.5/dist/objectFitPolyfill.min.js',
221 array(),
222 '2.3.5'
223 );
224 }
225 add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' );
226
227 /**
228 * Registers all the WordPress packages scripts that are in the standardized
229 * `build/` location.
230 *
231 * @since 4.5.0
232 *
233 * @param WP_Scripts $scripts WP_Scripts instance.
234 */
235 function gutenberg_register_packages_scripts( $scripts ) {
236 // When in production, use the plugin's version as the default asset version;
237 // else (for development or test) default to use the current time.
238 $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
239 $suffix = '.min.js';
240
241 foreach ( glob( gutenberg_dir_path() . "build/*/index$suffix" ) as $path ) {
242 // Prefix `wp-` to package directory to get script handle.
243 // For example, `…/build/a11y/index.min.js` becomes `wp-a11y`.
244 $handle = 'wp-' . basename( dirname( $path ) );
245
246 // Replace suffix and extension with `.asset.php` to find the generated dependencies file.
247 $asset_file = substr( $path, 0, -( strlen( $suffix ) ) ) . '.asset.php';
248 $asset = file_exists( $asset_file )
249 ? require( $asset_file )
250 : null;
251 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
252 $version = isset( $asset['version'] ) ? $asset['version'] : $default_version;
253
254 // Add dependencies that cannot be detected and generated by build tools.
255 switch ( $handle ) {
256 case 'wp-block-library':
257 array_push( $dependencies, 'editor' );
258 break;
259
260 case 'wp-edit-post':
261 array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
262 break;
263
264 case 'wp-edit-site':
265 array_push( $dependencies, 'wp-dom-ready' );
266 break;
267 }
268
269 // Get the path from Gutenberg directory as expected by `gutenberg_url`.
270 $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
271
272 gutenberg_override_script(
273 $scripts,
274 $handle,
275 gutenberg_url( $gutenberg_path ),
276 $dependencies,
277 $version,
278 true
279 );
280 }
281 }
282 add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
283
284 /**
285 * Registers all the WordPress packages styles that are in the standardized
286 * `build/` location.
287 *
288 * @since 6.7.0
289
290 * @param WP_Styles $styles WP_Styles instance.
291 */
292 function gutenberg_register_packages_styles( $styles ) {
293 // When in production, use the plugin's version as the asset version;
294 // else (for development or test) default to use the current time.
295 $version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();
296
297 // Editor Styles.
298 gutenberg_override_style(
299 $styles,
300 'wp-block-editor',
301 gutenberg_url( 'build/block-editor/style.css' ),
302 array( 'wp-components' ),
303 $version
304 );
305 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
306
307 gutenberg_override_style(
308 $styles,
309 'wp-editor',
310 gutenberg_url( 'build/editor/style.css' ),
311 array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
312 $version
313 );
314 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
315
316 gutenberg_override_style(
317 $styles,
318 'wp-edit-post',
319 gutenberg_url( 'build/edit-post/style.css' ),
320 array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
321 $version
322 );
323 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
324
325 gutenberg_override_style(
326 $styles,
327 'wp-components',
328 gutenberg_url( 'build/components/style.css' ),
329 array( 'dashicons' ),
330 $version
331 );
332 $styles->add_data( 'wp-components', 'rtl', 'replace' );
333
334 $block_library_filename = wp_should_load_separate_core_block_assets() ? 'common' : 'style';
335 gutenberg_override_style(
336 $styles,
337 'wp-block-library',
338 gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
339 array(),
340 $version
341 );
342 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
343 $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
344
345 gutenberg_override_style(
346 $styles,
347 'wp-format-library',
348 gutenberg_url( 'build/format-library/style.css' ),
349 array( 'wp-block-editor', 'wp-components' ),
350 $version
351 );
352 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
353
354 $wp_edit_blocks_dependencies = array(
355 'wp-components',
356 'wp-editor',
357 // This need to be added before the block library styles,
358 // The block library styles override the "reset" styles.
359 'wp-reset-editor-styles',
360 'wp-block-library',
361 'wp-reusable-blocks',
362 );
363
364 // Only load the default layout and margin styles for themes without theme.json file.
365 if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
366 $wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
367 }
368
369 global $editor_styles;
370 if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) {
371 // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken.
372 $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
373 }
374
375 gutenberg_override_style(
376 $styles,
377 'wp-reset-editor-styles',
378 gutenberg_url( 'build/block-library/reset.css' ),
379 array( 'common', 'forms' ), // Make sure the reset is loaded after the default WP Adminn styles.
380 $version
381 );
382 $styles->add_data( 'wp-reset-editor-styles', 'rtl', 'replace' );
383
384 gutenberg_override_style(
385 $styles,
386 'wp-editor-classic-layout-styles',
387 gutenberg_url( 'build/edit-post/classic.css' ),
388 array(),
389 $version
390 );
391 $styles->add_data( 'wp-editor-classic-layout-styles', 'rtl', 'replace' );
392
393 gutenberg_override_style(
394 $styles,
395 'wp-edit-blocks',
396 gutenberg_url( 'build/block-library/editor.css' ),
397 $wp_edit_blocks_dependencies,
398 $version
399 );
400 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
401
402 gutenberg_override_style(
403 $styles,
404 'wp-nux',
405 gutenberg_url( 'build/nux/style.css' ),
406 array( 'wp-components' ),
407 $version
408 );
409 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
410
411 gutenberg_override_style(
412 $styles,
413 'wp-block-library-theme',
414 gutenberg_url( 'build/block-library/theme.css' ),
415 array(),
416 $version
417 );
418 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
419
420 gutenberg_override_style(
421 $styles,
422 'wp-list-reusable-blocks',
423 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
424 array( 'wp-components' ),
425 $version
426 );
427 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
428
429 gutenberg_override_style(
430 $styles,
431 'wp-edit-navigation',
432 gutenberg_url( 'build/edit-navigation/style.css' ),
433 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
434 $version
435 );
436 $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
437
438 gutenberg_override_style(
439 $styles,
440 'wp-edit-site',
441 gutenberg_url( 'build/edit-site/style.css' ),
442 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
443 $version
444 );
445 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
446
447 gutenberg_override_style(
448 $styles,
449 'wp-edit-widgets',
450 gutenberg_url( 'build/edit-widgets/style.css' ),
451 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks', 'wp-widgets' ),
452 $version
453 );
454 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
455
456 gutenberg_override_style(
457 $styles,
458 'wp-block-directory',
459 gutenberg_url( 'build/block-directory/style.css' ),
460 array( 'wp-block-editor', 'wp-components' ),
461 $version
462 );
463 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
464
465 gutenberg_override_style(
466 $styles,
467 'wp-customize-widgets',
468 gutenberg_url( 'build/customize-widgets/style.css' ),
469 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-widgets' ),
470 $version
471 );
472 $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
473
474 gutenberg_override_style(
475 $styles,
476 'wp-reusable-blocks',
477 gutenberg_url( 'build/reusable-blocks/style.css' ),
478 array( 'wp-components' ),
479 $version
480 );
481 $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
482
483 gutenberg_override_style(
484 $styles,
485 'wp-widgets',
486 gutenberg_url( 'build/widgets/style.css' ),
487 array( 'wp-components' )
488 );
489 $styles->add_data( 'wp-widgets', 'rtl', 'replace' );
490 }
491 add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
492
493 /**
494 * Registers common scripts and styles to be used as dependencies of the editor
495 * and plugins.
496 *
497 * @since 0.1.0
498 */
499 function gutenberg_enqueue_block_editor_assets() {
500 if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
501 $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
502
503 wp_enqueue_script(
504 'gutenberg-live-reload',
505 $live_reload_url
506 );
507 }
508 }
509 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
510
511 /**
512 * Retrieves a unique and reasonably short and human-friendly filename for a
513 * vendor script based on a URL and the script handle.
514 *
515 * @param string $handle The name of the script.
516 * @param string $src Full URL of the external script.
517 *
518 * @return string Script filename suitable for local caching.
519 *
520 * @since 0.1.0
521 */
522 function gutenberg_vendor_script_filename( $handle, $src ) {
523 $filename = basename( $src );
524 $match = preg_match(
525 '/^'
526 . '(?P<ignore>.*?)'
527 . '(?P<suffix>\.min)?'
528 . '(?P<extension>\.js)'
529 . '(?P<extra>.*)'
530 . '$/',
531 $filename,
532 $filename_pieces
533 );
534
535 $prefix = $handle;
536 $suffix = $match ? $filename_pieces['suffix'] : '';
537 $hash = substr( md5( $src ), 0, 8 );
538
539 return "${prefix}${suffix}.${hash}.js";
540 }
541
542 /**
543 * Registers a vendor script from a URL, preferring a locally cached version if
544 * possible, or downloading it if the cached version is unavailable or
545 * outdated.
546 *
547 * @param WP_Scripts $scripts WP_Scripts instance.
548 * @param string $handle Name of the script.
549 * @param string $src Full URL of the external script.
550 * @param array $deps Optional. An array of registered script handles this
551 * script depends on.
552 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
553 * as a query string for cache busting purposes. If version is set to false, a version
554 * number is automatically added equal to current installed WordPress version.
555 * If set to null, no version is added.
556 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
557 * Default 'false'.
558 *
559 * @since 0.1.0
560 */
561 function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
562 if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
563 return;
564 }
565
566 $filename = gutenberg_vendor_script_filename( $handle, $src );
567
568 if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
569 echo "$src|$filename\n";
570 return;
571 }
572
573 $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
574
575 $needs_fetch = (
576 defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
577 ! file_exists( $full_path ) ||
578 time() - filemtime( $full_path ) >= DAY_IN_SECONDS
579 )
580 );
581
582 if ( $needs_fetch ) {
583 // Determine whether we can write to this file. If not, don't waste
584 // time doing a network request.
585 // @codingStandardsIgnoreStart
586
587 $is_writable = is_writable( $full_path );
588 if ( $is_writable ) {
589 $f = @fopen( $full_path, 'a' );
590 if ( ! $f ) {
591 $is_writable = false;
592 } else {
593 fclose( $f );
594 }
595 }
596
597 // @codingStandardsIgnoreEnd
598 if ( ! $is_writable ) {
599 // Failed to open the file for writing, probably due to server
600 // permissions. Enqueue the script directly from the URL instead.
601 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
602 return;
603 }
604
605 $response = wp_remote_get( $src );
606 if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
607 $f = fopen( $full_path, 'w' );
608 fwrite( $f, wp_remote_retrieve_body( $response ) );
609 fclose( $f );
610 } elseif ( ! filesize( $full_path ) ) {
611 // The request failed. If the file is already cached, continue to
612 // use this file. If not, then unlink the 0 byte file, and enqueue
613 // the script directly from the URL.
614 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
615 unlink( $full_path );
616 return;
617 }
618 }
619 gutenberg_override_script(
620 $scripts,
621 $handle,
622 gutenberg_url( 'vendor/' . $filename ),
623 $deps,
624 $ver,
625 $in_footer
626 );
627 }
628
629 /**
630 * Extends block editor settings to remove the Gutenberg's `editor-styles.css`;
631 *
632 * This can be removed when plugin support requires WordPress 5.8.0+.
633 *
634 * @param array $settings Default editor settings.
635 *
636 * @return array Filtered editor settings.
637 */
638 function gutenberg_extend_block_editor_styles( $settings ) {
639 if ( empty( $settings['styles'] ) ) {
640 $settings['styles'] = array();
641 } else {
642 /*
643 * WordPress versions prior to 5.8 include a legacy editor styles file
644 * that need to be removed.
645 * This code can be removed from the Gutenberg plugin when the supported WP
646 * version is 5.8
647 */
648 $default_styles_file = is_rtl() ?
649 ABSPATH . WPINC . '/css/dist/editor/editor-styles-rtl.css' :
650 ABSPATH . WPINC . '/css/dist/editor/editor-styles.css';
651
652 if ( file_exists( $default_styles_file ) ) {
653 $default_styles = file_get_contents(
654 $default_styles_file
655 );
656
657 /*
658 * Iterate backwards from the end of the array since the preferred
659 * insertion point in case not found is prepended as first entry.
660 */
661 for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
662 if ( isset( $settings['styles'][ $i ]['css'] ) &&
663 $default_styles === $settings['styles'][ $i ]['css'] ) {
664 break;
665 }
666 }
667
668 if ( isset( $i ) && $i >= 0 ) {
669 unset( $settings['styles'][ $i ] );
670 }
671 }
672 }
673
674 // Remove the default font editor styles for FSE themes.
675 if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
676 foreach ( $settings['styles'] as $j => $style ) {
677 if ( 0 === strpos( $style['css'], 'body { font-family:' ) ) {
678 unset( $settings['styles'][ $j ] );
679 }
680 }
681 }
682
683 return $settings;
684 }
685 // This can be removed when plugin support requires WordPress 5.8.0+.
686 if ( function_exists( 'get_block_editor_settings' ) ) {
687 add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_styles' );
688 } else {
689 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
690 }
691
692 /**
693 * Adds a flag to the editor settings to know whether we're in FSE theme or not.
694 *
695 * This can be removed when plugin support requires WordPress 5.8.0+.
696 *
697 * @param array $settings Default editor settings.
698 *
699 * @return array Filtered editor settings.
700 */
701 function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) {
702 $settings['supportsTemplateMode'] = gutenberg_supports_block_templates();
703
704 // Enable the new layout options for themes with a theme.json file.
705 $settings['supportsLayout'] = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
706
707 return $settings;
708 }
709 // This can be removed when plugin support requires WordPress 5.8.0+.
710 if ( function_exists( 'get_block_editor_settings' ) ) {
711 add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' );
712 } else {
713 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' );
714 }
715
716 /**
717 * Sets the editor styles to be consumed by JS.
718 */
719 function gutenberg_extend_block_editor_styles_html() {
720 $script_handles = array();
721 $style_handles = array(
722 'wp-block-editor',
723 'wp-block-library',
724 'wp-block-library-theme',
725 'wp-edit-blocks',
726 );
727
728 $block_registry = WP_Block_Type_Registry::get_instance();
729
730 foreach ( $block_registry->get_all_registered() as $block_type ) {
731 if ( ! empty( $block_type->style ) ) {
732 $style_handles[] = $block_type->style;
733 }
734
735 if ( ! empty( $block_type->editor_style ) ) {
736 $style_handles[] = $block_type->editor_style;
737 }
738
739 if ( ! empty( $block_type->script ) ) {
740 $script_handles[] = $block_type->script;
741 }
742 }
743
744 $style_handles = array_unique( $style_handles );
745 $done = wp_styles()->done;
746
747 ob_start();
748
749 wp_styles()->done = array();
750 wp_styles()->do_items( $style_handles );
751 wp_styles()->done = $done;
752
753 $styles = ob_get_clean();
754
755 $script_handles = array_unique( $script_handles );
756 $done = wp_scripts()->done;
757
758 ob_start();
759
760 wp_scripts()->done = array();
761 wp_scripts()->do_items( $script_handles );
762 wp_scripts()->done = $done;
763
764 $scripts = ob_get_clean();
765
766 $editor_assets = wp_json_encode(
767 array(
768 'styles' => $styles,
769 'scripts' => $scripts,
770 )
771 );
772
773 echo "<script>window.__editorAssets = $editor_assets</script>";
774 }
775 add_action( 'admin_footer-toplevel_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' );
776 add_action( 'admin_footer-post.php', 'gutenberg_extend_block_editor_styles_html' );
777 add_action( 'admin_footer-post-new.php', 'gutenberg_extend_block_editor_styles_html' );
778
779 /**
780 * Adds a polyfill for object-fit in environments which do not support it.
781 *
782 * The script registration occurs in `gutenberg_register_vendor_scripts`, which
783 * should be removed in coordination with this function.
784 *
785 * Remove this when the minimum supported version is WordPress 5.7
786 *
787 * @see gutenberg_register_vendor_scripts
788 * @see https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
789 *
790 * @since 9.1.0
791 *
792 * @param WP_Scripts $scripts WP_Scripts object.
793 */
794 function gutenberg_add_object_fit_polyfill( $scripts ) {
795 did_action( 'init' ) && $scripts->add_inline_script(
796 'wp-polyfill',
797 wp_get_script_polyfill(
798 $scripts,
799 array(
800 '"objectFit" in document.documentElement.style' => 'object-fit-polyfill',
801 )
802 )
803 );
804 }
805 add_action( 'wp_default_scripts', 'gutenberg_add_object_fit_polyfill', 20 );
806