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

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