| 1 |
<?php |
| 2 |
/** |
| 3 |
* Patches resources loaded by the block editor page. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Optimizes the preload paths registered in Core (`edit-form-blocks.php`). |
| 10 |
* |
| 11 |
* @param array $preload_paths Preload paths to be filtered. |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
function gutenberg_optimize_preload_paths( $preload_paths ) { |
| 15 |
// remove preload of the `/` route. |
| 16 |
$root_index = array_search( '/', $preload_paths, true ); |
| 17 |
if ( false !== $root_index ) { |
| 18 |
array_splice( $preload_paths, $root_index, 1 ); |
| 19 |
} |
| 20 |
|
| 21 |
// change `/types` context from `edit` to `view` (requested in `loadPostTypeEntities`). |
| 22 |
$types_index = array_search( '/wp/v2/types?context=edit', $preload_paths, true ); |
| 23 |
if ( false !== $types_index ) { |
| 24 |
$preload_paths[ $types_index ] = '/wp/v2/types?context=view'; |
| 25 |
} |
| 26 |
|
| 27 |
// start preloading `/taxonomies` in `view` context (requested in `loadTaxonomyEntities`). |
| 28 |
$tax_index = array_search( '/wp/v2/taxonomies?per_page=-1&context=edit', $preload_paths, true ); |
| 29 |
if ( false === $tax_index ) { |
| 30 |
array_push( $preload_paths, '/wp/v2/taxonomies?context=view' ); |
| 31 |
} else { |
| 32 |
$preload_paths[ $tax_index ] = '/wp/v2/taxonomies?context=view'; |
| 33 |
} |
| 34 |
|
| 35 |
// start preloading `/settings`. |
| 36 |
$settings_index = array_search( '/wp/v2/settings', $preload_paths, true ); |
| 37 |
if ( false === $settings_index ) { |
| 38 |
array_push( $preload_paths, '/wp/v2/settings' ); |
| 39 |
} |
| 40 |
|
| 41 |
// modify the preload of `/users/me` to match the real request. |
| 42 |
foreach ( $preload_paths as $user_index => $user_path ) { |
| 43 |
if ( is_string( $user_path ) && str_starts_with( $user_path, '/wp/v2/users/me' ) ) { |
| 44 |
$preload_paths[ $user_index ] = '/wp/v2/users/me'; |
| 45 |
break; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $preload_paths; |
| 50 |
} |
| 51 |
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_optimize_preload_paths' ); |
| 52 |
|
| 53 |
/** |
| 54 |
* Disables loading remote block patterns from REST while initializing the editor. |
| 55 |
* Nowadays these loads are done in the `block-patterns/patterns` REST endpoint, and |
| 56 |
* are undesired when initializing the block editor page, both in post and site editor. |
| 57 |
* |
| 58 |
* @param WP_Screen $current_screen WordPress current screen object. |
| 59 |
*/ |
| 60 |
function gutenberg_disable_load_remote_patterns( $current_screen ) { |
| 61 |
$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) ); |
| 62 |
if ( $is_site_editor || $current_screen->is_block_editor() ) { |
| 63 |
add_filter( 'should_load_remote_block_patterns', '__return_false' ); |
| 64 |
} |
| 65 |
} |
| 66 |
add_action( 'current_screen', 'gutenberg_disable_load_remote_patterns' ); |
| 67 |
|