PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0, at lib/client-assets.php

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