| 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 |
|