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

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

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