PluginProbe
Gutenberg / 10.2.1
Gutenberg v10.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 10.2.1, at lib/client-assets.php

759 lines 25.0 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.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@16.13.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@16.13.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.0/dist/objectFitPolyfill.min.js',
221 array(),
222 '2.3.0'
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 foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
237 // Prefix `wp-` to package directory to get script handle.
238 // For example, `…/build/a11y/index.js` becomes `wp-a11y`.
239 $handle = 'wp-' . basename( dirname( $path ) );
240
241 // Replace `.js` extension with `.asset.php` to find the generated dependencies file.
242 $asset_file = substr( $path, 0, -3 ) . '.asset.php';
243 $asset = file_exists( $asset_file )
244 ? require( $asset_file )
245 : null;
246 $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
247 $version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
248
249 // Add dependencies that cannot be detected and generated by build tools.
250 switch ( $handle ) {
251 case 'wp-block-library':
252 array_push( $dependencies, 'editor' );
253 break;
254
255 case 'wp-edit-post':
256 array_push( $dependencies, 'media-models', 'media-views', 'postbox' );
257 break;
258
259 case 'wp-edit-site':
260 array_push( $dependencies, 'wp-dom-ready' );
261 break;
262 }
263
264 // Get the path from Gutenberg directory as expected by `gutenberg_url`.
265 $gutenberg_path = substr( $path, strlen( gutenberg_dir_path() ) );
266
267 gutenberg_override_script(
268 $scripts,
269 $handle,
270 gutenberg_url( $gutenberg_path ),
271 $dependencies,
272 $version,
273 true
274 );
275 }
276 }
277 add_action( 'wp_default_scripts', 'gutenberg_register_packages_scripts' );
278
279 /**
280 * Registers all the WordPress packages styles that are in the standardized
281 * `build/` location.
282 *
283 * @since 6.7.0
284
285 * @param WP_Styles $styles WP_Styles instance.
286 */
287 function gutenberg_register_packages_styles( $styles ) {
288 // Editor Styles.
289 gutenberg_override_style(
290 $styles,
291 'wp-block-editor',
292 gutenberg_url( 'build/block-editor/style.css' ),
293 array( 'wp-components' ),
294 filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
295 );
296 $styles->add_data( 'wp-block-editor', 'rtl', 'replace' );
297
298 gutenberg_override_style(
299 $styles,
300 'wp-editor',
301 gutenberg_url( 'build/editor/style.css' ),
302 array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
303 filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
304 );
305 $styles->add_data( 'wp-editor', 'rtl', 'replace' );
306
307 gutenberg_override_style(
308 $styles,
309 'wp-edit-post',
310 gutenberg_url( 'build/edit-post/style.css' ),
311 array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
312 filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
313 );
314 $styles->add_data( 'wp-edit-post', 'rtl', 'replace' );
315
316 gutenberg_override_style(
317 $styles,
318 'wp-components',
319 gutenberg_url( 'build/components/style.css' ),
320 array( 'dashicons' ),
321 filemtime( gutenberg_dir_path() . 'build/components/style.css' )
322 );
323 $styles->add_data( 'wp-components', 'rtl', 'replace' );
324
325 $block_library_filename = gutenberg_should_load_separate_block_assets() ? 'common' : 'style';
326 gutenberg_override_style(
327 $styles,
328 'wp-block-library',
329 gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
330 array(),
331 filemtime( gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' )
332 );
333 $styles->add_data( 'wp-block-library', 'rtl', 'replace' );
334 $styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
335
336 gutenberg_override_style(
337 $styles,
338 'wp-format-library',
339 gutenberg_url( 'build/format-library/style.css' ),
340 array( 'wp-block-editor', 'wp-components' ),
341 filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
342 );
343 $styles->add_data( 'wp-format-library', 'rtl', 'replace' );
344
345 $wp_edit_blocks_dependencies = array(
346 'wp-components',
347 'wp-editor',
348 'wp-block-library',
349 'wp-reusable-blocks',
350 );
351
352 global $editor_styles;
353 if ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) {
354 // Include opinionated block styles if no $editor_styles are declared, so the editor never appears broken.
355 $wp_edit_blocks_dependencies[] = 'wp-block-library-theme';
356 }
357
358 gutenberg_override_style(
359 $styles,
360 'wp-edit-blocks',
361 gutenberg_url( 'build/block-library/editor.css' ),
362 $wp_edit_blocks_dependencies,
363 filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
364 );
365 $styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );
366
367 gutenberg_override_style(
368 $styles,
369 'wp-nux',
370 gutenberg_url( 'build/nux/style.css' ),
371 array( 'wp-components' ),
372 filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
373 );
374 $styles->add_data( 'wp-nux', 'rtl', 'replace' );
375
376 gutenberg_override_style(
377 $styles,
378 'wp-block-library-theme',
379 gutenberg_url( 'build/block-library/theme.css' ),
380 array(),
381 filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
382 );
383 $styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );
384
385 gutenberg_override_style(
386 $styles,
387 'wp-list-reusable-blocks',
388 gutenberg_url( 'build/list-reusable-blocks/style.css' ),
389 array( 'wp-components' ),
390 filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
391 );
392 $styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
393
394 gutenberg_override_style(
395 $styles,
396 'wp-edit-navigation',
397 gutenberg_url( 'build/edit-navigation/style.css' ),
398 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
399 filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' )
400 );
401 $styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );
402
403 gutenberg_override_style(
404 $styles,
405 'wp-edit-site',
406 gutenberg_url( 'build/edit-site/style.css' ),
407 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
408 filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
409 );
410 $styles->add_data( 'wp-edit-site', 'rtl', 'replace' );
411
412 gutenberg_override_style(
413 $styles,
414 'wp-edit-widgets',
415 gutenberg_url( 'build/edit-widgets/style.css' ),
416 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks' ),
417 filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
418 );
419 $styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );
420
421 gutenberg_override_style(
422 $styles,
423 'wp-block-directory',
424 gutenberg_url( 'build/block-directory/style.css' ),
425 array( 'wp-block-editor', 'wp-components' ),
426 filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
427 );
428 $styles->add_data( 'wp-block-directory', 'rtl', 'replace' );
429
430 gutenberg_override_style(
431 $styles,
432 'wp-customize-widgets',
433 gutenberg_url( 'build/customize-widgets/style.css' ),
434 array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
435 filemtime( gutenberg_dir_path() . 'build/customize-widgets/style.css' )
436 );
437 $styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );
438
439 gutenberg_override_style(
440 $styles,
441 'wp-reusable-blocks',
442 gutenberg_url( 'build/reusable-blocks/style.css' ),
443 array( 'wp-components' ),
444 filemtime( gutenberg_dir_path() . 'build/reusable-blocks/style.css' )
445 );
446 $styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
447 }
448 add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' );
449
450 /**
451 * Registers common scripts and styles to be used as dependencies of the editor
452 * and plugins.
453 *
454 * @since 0.1.0
455 */
456 function gutenberg_enqueue_block_editor_assets() {
457 if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
458 $live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
459
460 wp_enqueue_script(
461 'gutenberg-live-reload',
462 $live_reload_url
463 );
464 }
465 }
466 add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets' );
467
468 /**
469 * Retrieves a unique and reasonably short and human-friendly filename for a
470 * vendor script based on a URL and the script handle.
471 *
472 * @param string $handle The name of the script.
473 * @param string $src Full URL of the external script.
474 *
475 * @return string Script filename suitable for local caching.
476 *
477 * @since 0.1.0
478 */
479 function gutenberg_vendor_script_filename( $handle, $src ) {
480 $filename = basename( $src );
481 $match = preg_match(
482 '/^'
483 . '(?P<ignore>.*?)'
484 . '(?P<suffix>\.min)?'
485 . '(?P<extension>\.js)'
486 . '(?P<extra>.*)'
487 . '$/',
488 $filename,
489 $filename_pieces
490 );
491
492 $prefix = $handle;
493 $suffix = $match ? $filename_pieces['suffix'] : '';
494 $hash = substr( md5( $src ), 0, 8 );
495
496 return "${prefix}${suffix}.${hash}.js";
497 }
498
499 /**
500 * Registers a vendor script from a URL, preferring a locally cached version if
501 * possible, or downloading it if the cached version is unavailable or
502 * outdated.
503 *
504 * @param WP_Scripts $scripts WP_Scripts instance.
505 * @param string $handle Name of the script.
506 * @param string $src Full URL of the external script.
507 * @param array $deps Optional. An array of registered script handles this
508 * script depends on.
509 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
510 * as a query string for cache busting purposes. If version is set to false, a version
511 * number is automatically added equal to current installed WordPress version.
512 * If set to null, no version is added.
513 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
514 * Default 'false'.
515 *
516 * @since 0.1.0
517 */
518 function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = array(), $ver = null, $in_footer = false ) {
519 if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
520 return;
521 }
522
523 $filename = gutenberg_vendor_script_filename( $handle, $src );
524
525 if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
526 echo "$src|$filename\n";
527 return;
528 }
529
530 $full_path = gutenberg_dir_path() . 'vendor/' . $filename;
531
532 $needs_fetch = (
533 defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
534 ! file_exists( $full_path ) ||
535 time() - filemtime( $full_path ) >= DAY_IN_SECONDS
536 )
537 );
538
539 if ( $needs_fetch ) {
540 // Determine whether we can write to this file. If not, don't waste
541 // time doing a network request.
542 // @codingStandardsIgnoreStart
543
544 $is_writable = is_writable( $full_path );
545 if ( $is_writable ) {
546 $f = @fopen( $full_path, 'a' );
547 if ( ! $f ) {
548 $is_writable = false;
549 } else {
550 fclose( $f );
551 }
552 }
553
554 // @codingStandardsIgnoreEnd
555 if ( ! $is_writable ) {
556 // Failed to open the file for writing, probably due to server
557 // permissions. Enqueue the script directly from the URL instead.
558 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
559 return;
560 }
561
562 $response = wp_remote_get( $src );
563 if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
564 $f = fopen( $full_path, 'w' );
565 fwrite( $f, wp_remote_retrieve_body( $response ) );
566 fclose( $f );
567 } elseif ( ! filesize( $full_path ) ) {
568 // The request failed. If the file is already cached, continue to
569 // use this file. If not, then unlink the 0 byte file, and enqueue
570 // the script directly from the URL.
571 gutenberg_override_script( $scripts, $handle, $src, $deps, $ver, $in_footer );
572 unlink( $full_path );
573 return;
574 }
575 }
576 gutenberg_override_script(
577 $scripts,
578 $handle,
579 gutenberg_url( 'vendor/' . $filename ),
580 $deps,
581 $ver,
582 $in_footer
583 );
584 }
585
586 /**
587 * Extends block editor settings to include Gutenberg's `editor-styles.css` as
588 * taking precedent those styles shipped with core.
589 *
590 * @param array $settings Default editor settings.
591 *
592 * @return array Filtered editor settings.
593 */
594 function gutenberg_extend_block_editor_styles( $settings ) {
595 $editor_styles_file = is_rtl() ?
596 gutenberg_dir_path() . 'build/editor/editor-styles-rtl.css' :
597 gutenberg_dir_path() . 'build/editor/editor-styles.css';
598
599 /*
600 * If, for whatever reason, the built editor styles do not exist, avoid
601 * override and fall back to the default.
602 */
603 if ( ! file_exists( $editor_styles_file ) ) {
604 return $settings;
605 }
606
607 if ( empty( $settings['styles'] ) ) {
608 $settings['styles'] = array();
609 } else {
610 /*
611 * The styles setting is an array of CSS strings, so there is no direct
612 * way to find the default styles. To maximize stability, load (again)
613 * the default styles from disk and find its place in the array.
614 *
615 * See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
616 */
617
618 $default_styles = file_get_contents(
619 is_rtl() ?
620 ABSPATH . WPINC . '/css/dist/editor/editor-styles-rtl.css' :
621 ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
622 );
623
624 /*
625 * Iterate backwards from the end of the array since the preferred
626 * insertion point in case not found is prepended as first entry.
627 */
628 for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
629 if ( isset( $settings['styles'][ $i ]['css'] ) &&
630 $default_styles === $settings['styles'][ $i ]['css'] ) {
631 break;
632 }
633 }
634 }
635
636 $editor_styles = array(
637 'css' => file_get_contents( $editor_styles_file ),
638 );
639
640 // Substitute default styles if found. Otherwise, prepend to setting array.
641 if ( isset( $i ) && $i >= 0 ) {
642 $settings['styles'][ $i ] = $editor_styles;
643 } else {
644 array_unshift( $settings['styles'], $editor_styles );
645 }
646
647 // Remove the default font editor styles.
648 // When Gutenberg is updated to have minimum version of WordPress 5.8
649 // This could be removed.
650 foreach ( $settings['styles'] as $j => $style ) {
651 if ( 0 === strpos( $style['css'], 'body { font-family:' ) ) {
652 unset( $settings['styles'][ $j ] );
653 }
654 }
655
656 return $settings;
657 }
658 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
659
660 /**
661 * Load the default editor styles.
662 * These styles are used if the "no theme styles" options is triggered.
663 *
664 * @param array $settings Default editor settings.
665 *
666 * @return array Filtered editor settings.
667 */
668 function gutenberg_extend_block_editor_settings_with_default_editor_styles( $settings ) {
669 $editor_styles_file = is_rtl() ?
670 gutenberg_dir_path() . 'build/editor/editor-styles-rtl.css' :
671 gutenberg_dir_path() . 'build/editor/editor-styles.css';
672 $settings['defaultEditorStyles'] = array(
673 array(
674 'css' => file_get_contents( $editor_styles_file ),
675 ),
676 );
677
678 return $settings;
679 }
680 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_default_editor_styles' );
681
682 /**
683 * Adds a flag to the editor settings to know whether we're in FSE theme or not.
684 *
685 * @param array $settings Default editor settings.
686 *
687 * @return array Filtered editor settings.
688 */
689 function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) {
690 $settings['isFSETheme'] = gutenberg_is_fse_theme();
691 return $settings;
692 }
693 add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' );
694
695 /**
696 * Sets the editor styles to be consumed by JS.
697 */
698 function gutenberg_extend_block_editor_styles_html() {
699 $handles = array(
700 'wp-block-editor',
701 'wp-block-library',
702 'wp-edit-blocks',
703 );
704
705 $block_registry = WP_Block_Type_Registry::get_instance();
706
707 foreach ( $block_registry->get_all_registered() as $block_type ) {
708 if ( ! empty( $block_type->style ) ) {
709 $handles[] = $block_type->style;
710 }
711
712 if ( ! empty( $block_type->editor_style ) ) {
713 $handles[] = $block_type->editor_style;
714 }
715 }
716
717 $handles = array_unique( $handles );
718 $done = wp_styles()->done;
719
720 ob_start();
721
722 wp_styles()->done = array();
723 wp_styles()->do_items( $handles );
724 wp_styles()->done = $done;
725
726 $editor_styles = wp_json_encode( array( 'html' => ob_get_clean() ) );
727
728 echo "<script>window.__editorStyles = $editor_styles</script>";
729 }
730 add_action( 'admin_footer-toplevel_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' );
731
732 /**
733 * Adds a polyfill for object-fit in environments which do not support it.
734 *
735 * The script registration occurs in `gutenberg_register_vendor_scripts`, which
736 * should be removed in coordination with this function.
737 *
738 * Remove this when the minimum supported version is WordPress 5.7
739 *
740 * @see gutenberg_register_vendor_scripts
741 * @see https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
742 *
743 * @since 9.1.0
744 *
745 * @param WP_Scripts $scripts WP_Scripts object.
746 */
747 function gutenberg_add_object_fit_polyfill( $scripts ) {
748 did_action( 'init' ) && $scripts->add_inline_script(
749 'wp-polyfill',
750 wp_get_script_polyfill(
751 $scripts,
752 array(
753 '"objectFit" in document.documentElement.style' => 'object-fit-polyfill',
754 )
755 )
756 );
757 }
758 add_action( 'wp_default_scripts', 'gutenberg_add_object_fit_polyfill', 20 );
759