| 1 |
<?php |
| 2 |
/** |
| 3 |
* Preload paths for client-side media processing. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Filters the block editor preload paths to include media processing fields. |
| 10 |
* |
| 11 |
* The packages/core-data/src/entities.js file requests additional fields |
| 12 |
* (image_sizes, image_size_threshold) on the root endpoint that are not |
| 13 |
* included in WordPress Core's default preload paths. This filter ensures |
| 14 |
* the preloaded URL matches exactly what the JavaScript requests. |
| 15 |
* |
| 16 |
* @since 20.1.0 |
| 17 |
* |
| 18 |
* @param array $paths REST API paths to preload. |
| 19 |
* @return array Filtered preload paths. |
| 20 |
*/ |
| 21 |
function gutenberg_block_editor_preload_paths_root_fields( $paths ) { |
| 22 |
// Complete list of fields expected by packages/core-data/src/entities.js. |
| 23 |
// This must match exactly for preloading to work (same fields, same order). |
| 24 |
// @see packages/core-data/src/entities.js rootEntitiesConfig.__unstableBase |
| 25 |
$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'; |
| 26 |
|
| 27 |
foreach ( $paths as $key => $path ) { |
| 28 |
if ( is_string( $path ) && str_starts_with( $path, '/?_fields=' ) ) { |
| 29 |
// Replace with the complete fields list to ensure exact match. |
| 30 |
$paths[ $key ] = '/?_fields=' . $root_fields; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
return $paths; |
| 36 |
} |
| 37 |
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_root_fields' ); |
| 38 |
|