PluginProbe
Gutenberg / 23.3.2
Gutenberg v23.3.2
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 / compat / wordpress-7.0 / preload.php

preload.php in Gutenberg 23.3.2, at lib/compat/wordpress-7.0/preload.php

88 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Preload necessary resources for the editors.
5 *
6 * @param array $paths REST API paths to preload.
7 * @param WP_Block_Editor_Context $context Current block editor context
8 *
9 * @return array Filtered preload paths.
10 */
11 function gutenberg_block_editor_preload_paths_6_9( $paths, $context ) {
12 if ( 'core/edit-site' === $context->name ) {
13 // Only prefetch for the root. If we preload it for all pages and it's not used
14 // it won't be possible to invalidate.
15 // To do: perhaps purge all preloaded paths when client side navigating.
16 if ( isset( $_GET['p'] ) && '/' !== $_GET['p'] ) {
17 $paths = array_filter(
18 $paths,
19 static function ( $path ) {
20 return '/wp/v2/templates/lookup?slug=front-page' !== $path && '/wp/v2/templates/lookup?slug=home' !== $path;
21 }
22 );
23 }
24 }
25
26 return $paths;
27 }
28 add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_9', 10, 2 );
29
30 /**
31 * Filters the block editor preload paths to include media processing fields.
32 *
33 * The packages/core-data/src/entities.js file requests additional fields
34 * (image_sizes, image_size_threshold) on the root endpoint that are not
35 * included in WordPress Core's default preload paths. This filter ensures
36 * the preloaded URL matches exactly what the JavaScript requests.
37 *
38 * @since 20.1.0
39 *
40 * @param array $paths REST API paths to preload.
41 * @return array Filtered preload paths.
42 */
43 function gutenberg_block_editor_preload_paths_root_fields( $paths ) {
44 // Complete list of fields expected by packages/core-data/src/entities.js.
45 // This must match exactly for preloading to work (same fields, same order).
46 // @see packages/core-data/src/entities.js rootEntitiesConfig.__unstableBase
47 $root_fields = 'description,gmt_offset,home,image_sizes,image_size_threshold,name,site_icon,site_icon_url,site_logo,timezone_string,url,page_for_posts,page_on_front,show_on_front';
48
49 foreach ( $paths as $key => $path ) {
50 if ( is_string( $path ) && str_starts_with( $path, '/?_fields=' ) ) {
51 // Replace with the complete fields list to ensure exact match.
52 $paths[ $key ] = '/?_fields=' . $root_fields;
53 break;
54 }
55 }
56
57 return $paths;
58 }
59 add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_root_fields' );
60
61 /**
62 * Preload bound single-resource GETs the post editor would otherwise
63 * fetch post-mount.
64 *
65 * @param array $paths REST API paths.
66 * @param WP_Block_Editor_Context $context Block editor context.
67 * @return array
68 */
69 function gutenberg_block_editor_preload_paths_post_editor_bound_gets( $paths, $context ) {
70 if ( 'core/edit-post' !== $context->name || ! isset( $context->post ) ) {
71 return $paths;
72 }
73 $paths[] = '/wp/v2/templates/lookup?slug=front-page';
74 $paths[] = '/wp/v2/taxonomies?context=edit';
75 $paths[] = array( '/wp/v2/posts', 'OPTIONS' );
76
77 $author_id = (int) get_post_field( 'post_author', $context->post->ID );
78 if ( $author_id ) {
79 $paths[] = sprintf(
80 '/wp/v2/users/%d?context=view&_fields=id,name',
81 $author_id
82 );
83 }
84
85 return $paths;
86 }
87 add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_post_editor_bound_gets', 10, 2 );
88