PluginProbe
Gutenberg / 11.2.1
Gutenberg v11.2.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 11.2.1, at lib/client-assets.php

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