| 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 |
* Preload bound single-resource GETs the post editor would otherwise |
| 32 |
* fetch post-mount. |
| 33 |
* |
| 34 |
* @param array $paths REST API paths. |
| 35 |
* @param WP_Block_Editor_Context $context Block editor context. |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
function gutenberg_block_editor_preload_paths_post_editor_bound_gets( $paths, $context ) { |
| 39 |
if ( 'core/edit-post' !== $context->name || ! isset( $context->post ) ) { |
| 40 |
return $paths; |
| 41 |
} |
| 42 |
$paths[] = '/wp/v2/templates/lookup?slug=front-page'; |
| 43 |
$paths[] = '/wp/v2/taxonomies?context=edit'; |
| 44 |
$paths[] = array( '/wp/v2/posts', 'OPTIONS' ); |
| 45 |
|
| 46 |
$author_id = (int) get_post_field( 'post_author', $context->post->ID ); |
| 47 |
if ( $author_id ) { |
| 48 |
$paths[] = sprintf( |
| 49 |
'/wp/v2/users/%d?context=view&_fields=id,name', |
| 50 |
$author_id |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return $paths; |
| 55 |
} |
| 56 |
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_post_editor_bound_gets', 10, 2 ); |
| 57 |
|