Skip to content

Commit

Permalink
Move optimization filter to lib/compat
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Mar 8, 2022
1 parent c15bddb commit 9a94170
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
52 changes: 52 additions & 0 deletions lib/compat/wordpress-6.0/edit-form-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Patches preload paths for post editor.
*
* @package gutenberg
*/

/**
* Optimizes the preload paths registered in Core (`edit-form-blocks.php`).
*
* @param array $preload_paths Preload paths to be filtered.
* @return array
*/
function optimize_preload_paths( $preload_paths ) {
// remove preload of the `/` route.
$root_index = array_search( '/', $preload_paths, true );
if ( false !== $root_index ) {
array_splice( $preload_paths, $root_index, 1 );
}

// change `/types` context from `edit` to `view` (requested in `loadPostTypeEntities`).
$types_index = array_search( '/wp/v2/types?context=edit', $preload_paths, true );
if ( false !== $types_index ) {
$preload_paths[ $types_index ] = '/wp/v2/types?context=view';
}

// start preloading `/taxonomies` in `view` context (requested in `loadTaxonomyEntities`).
$tax_index = array_search( '/wp/v2/taxonomies?per_page=-1&context=edit', $preload_paths, true );
if ( false === $tax_index ) {
array_push( $preload_paths, '/wp/v2/taxonomies?context=view' );
} else {
$preload_paths[ $tax_index ] = '/wp/v2/taxonomies?context=view';
}

// start preloading `/settings`.
$settings_index = array_search( '/wp/v2/settings', $preload_paths, true );
if ( false === $settings_index ) {
array_push( $preload_paths, '/wp/v2/settings' );
}

// modify the preload of `/users/me` to match the real request.
foreach ( $preload_paths as $user_index => $user_path ) {
if ( 0 === strpos( $user_path, '/wp/v2/users/me' ) ) {
$preload_paths[ $user_index ] = '/wp/v2/users/me';
break;
}
}

return $preload_paths;
}

add_filter( 'block_editor_rest_api_preload_paths', 'optimize_preload_paths' );
46 changes: 0 additions & 46 deletions lib/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,49 +148,3 @@ function register_site_logo_to_rest_index( $response ) {
}

add_filter( 'rest_index', 'register_site_logo_to_rest_index' );

/**
* Optimizes the preload paths registered in Core (`edit-form-blocks.php`).
*
* @param array $preload_paths Preload paths to be filtered.
* @return array
*/
function optimize_preload_paths( $preload_paths ) {
// remove preload of the `/` route.
$root_index = array_search( '/', $preload_paths, true );
if ( false !== $root_index ) {
array_splice( $preload_paths, $root_index, 1 );
}

// change `/types` context from `edit` to `view` (requested in `loadPostTypeEntities`).
$types_index = array_search( '/wp/v2/types?context=edit', $preload_paths, true );
if ( false !== $types_index ) {
$preload_paths[$types_index] = '/wp/v2/types?context=view';
}

// start preloading `/taxonomies` in `view` context (requested in `loadTaxonomyEntities`).
$tax_index = array_search( '/wp/v2/taxonomies?per_page=-1&context=edit', $preload_paths, true );
if ( false === $tax_index ) {
array_push( $preload_paths, '/wp/v2/taxonomies?context=view' );
} else {
$preload_paths[$tax_index] = '/wp/v2/taxonomies?context=view';
}

// start preloading `/settings`.
$settings_index = array_search( '/wp/v2/settings', $preload_paths, true );
if ( false === $settings_index ) {
array_push( $preload_paths, '/wp/v2/settings' );
}

// modify the preload of `/users/me` to match the real request.
foreach ( $preload_paths as $user_index => $user_path ) {
if ( 0 === strpos( $user_path, '/wp/v2/users/me' ) ) {
$preload_paths[$user_index] = '/wp/v2/users/me';
break;
}
}

return $preload_paths;
}

add_filter( 'block_editor_rest_api_preload_paths', 'optimize_preload_paths' );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider.php';
require __DIR__ . '/compat/wordpress-6.0/class-wp-webfonts-provider-local.php';
require __DIR__ . '/compat/wordpress-6.0/webfonts.php';
require __DIR__ . '/compat/wordpress-6.0/edit-form-blocks.php';
require __DIR__ . '/compat/experimental/blocks.php';

require __DIR__ . '/blocks.php';
Expand Down

0 comments on commit 9a94170

Please sign in to comment.