From 242aa97522b8ee0d4e26677964743bf332c00a81 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 11:31:13 +0300 Subject: [PATCH 1/6] Site Editor: preload always required settings --- lib/compat/wordpress-6.6/rest-api.php | 20 +++++++++++++++++++ packages/core-commands/src/hooks.js | 1 + packages/core-data/src/entities.js | 4 +++- .../edit-site/src/components/editor/index.js | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index bf462cd11ca4b4..30070c56925e58 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -29,3 +29,23 @@ function wp_api_template_access_controller( $args, $post_type ) { } } add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 ); + +function gutenberg_block_editor_preload_paths_6_6( $paths ) { + $paths[] = array( '/wp/v2/settings', 'OPTIONS' ); + $paths[] = '/wp/v2/settings'; + $paths[] = array( '/wp/v2/templates', 'OPTIONS' ); + $paths[] = '/wp/v2/types?context=edit'; + $paths[] = '/?_fields=' . urlencode( implode( ',', [ + 'description', + 'gmt_offset', + 'home', + 'name', + 'site_icon', + 'site_icon_url', + 'site_logo', + 'timezone_string', + 'url', + ] ) ); + return $paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10 ); diff --git a/packages/core-commands/src/hooks.js b/packages/core-commands/src/hooks.js index 6d744e3223234d..5a0b4f8c96b81a 100644 --- a/packages/core-commands/src/hooks.js +++ b/packages/core-commands/src/hooks.js @@ -5,6 +5,7 @@ import { store as coreStore } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; export function useIsTemplatesAccessible() { + // This resource is preloaded. return useSelect( ( select ) => select( coreStore ).canUser( 'read', 'templates' ), [] diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index e91744110faf32..5532548f8ed4e3 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -27,6 +27,8 @@ export const rootEntitiesConfig = [ name: '__unstableBase', baseURL: '/', baseURLParams: { + // This resource is preloaded. Please ensure the preload is + // updated when this list changes. _fields: [ 'description', 'gmt_offset', @@ -291,7 +293,7 @@ function makeBlocksSerializable( blocks ) { */ async function loadPostTypeEntities() { const postTypes = await apiFetch( { - path: '/wp/v2/types?context=view', + path: '/wp/v2/types?context=edit', } ); return Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => { const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index d6773e5a24d899..2f0262f9023e31 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -146,6 +146,7 @@ export default function Editor( { isLoading, onClick } ) { isDistractionFree: get( 'core', 'distractionFree' ), showBlockBreadcrumbs: get( 'core', 'showBlockBreadcrumbs' ), showIconLabels: get( 'core', 'showIconLabels' ), + // This resourse is preloaded. postTypeLabel: getPostTypeLabel(), }; }, [] ); From a391589e51fbf0ac6908211e166c9957242b0754 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 11:38:53 +0300 Subject: [PATCH 2/6] Add comments --- packages/core-data/src/entities.js | 2 +- .../sync-state-with-url/use-init-edited-entity-from-url.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 5532548f8ed4e3..cbe6397caf5b10 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -293,7 +293,7 @@ function makeBlocksSerializable( blocks ) { */ async function loadPostTypeEntities() { const postTypes = await apiFetch( { - path: '/wp/v2/types?context=edit', + path: '/wp/v2/types?context=view', } ); return Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => { const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( diff --git a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js index 8cfb0bca716f2f..b6f3832fb8f16a 100644 --- a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js +++ b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js @@ -37,8 +37,11 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { } = useSelect( ( select ) => { const { getSite, getUnstableBase, getEntityRecords } = select( coreDataStore ); + // This resource is preloaded. const siteData = getSite(); + // This resource is preloaded. const base = getUnstableBase(); + // This resource is preloaded. const templates = getEntityRecords( 'postType', TEMPLATE_POST_TYPE, { per_page: -1, } ); From d19a643b93b32fd067487e01bb8d9ad6cd627e4f Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 13:08:59 +0300 Subject: [PATCH 3/6] Use normal site settings for url --- lib/compat/wordpress-6.6/rest-api.php | 11 ----------- packages/edit-site/src/components/editor/index.js | 2 +- .../use-init-edited-entity-from-url.js | 9 +++------ 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 30070c56925e58..830a898e2f4661 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -35,17 +35,6 @@ function gutenberg_block_editor_preload_paths_6_6( $paths ) { $paths[] = '/wp/v2/settings'; $paths[] = array( '/wp/v2/templates', 'OPTIONS' ); $paths[] = '/wp/v2/types?context=edit'; - $paths[] = '/?_fields=' . urlencode( implode( ',', [ - 'description', - 'gmt_offset', - 'home', - 'name', - 'site_icon', - 'site_icon_url', - 'site_logo', - 'timezone_string', - 'url', - ] ) ); return $paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10 ); diff --git a/packages/edit-site/src/components/editor/index.js b/packages/edit-site/src/components/editor/index.js index 2f0262f9023e31..470bb578cbaf1b 100644 --- a/packages/edit-site/src/components/editor/index.js +++ b/packages/edit-site/src/components/editor/index.js @@ -146,7 +146,7 @@ export default function Editor( { isLoading, onClick } ) { isDistractionFree: get( 'core', 'distractionFree' ), showBlockBreadcrumbs: get( 'core', 'showBlockBreadcrumbs' ), showIconLabels: get( 'core', 'showIconLabels' ), - // This resourse is preloaded. + // This resource is preloaded. postTypeLabel: getPostTypeLabel(), }; }, [] ); diff --git a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js index b6f3832fb8f16a..bcbb0744a422d0 100644 --- a/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js +++ b/packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js @@ -35,13 +35,10 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { url, frontPageTemplateId, } = useSelect( ( select ) => { - const { getSite, getUnstableBase, getEntityRecords } = - select( coreDataStore ); + const { getSite, getEntityRecords } = select( coreDataStore ); // This resource is preloaded. const siteData = getSite(); // This resource is preloaded. - const base = getUnstableBase(); - // This resource is preloaded. const templates = getEntityRecords( 'postType', TEMPLATE_POST_TYPE, { per_page: -1, } ); @@ -66,10 +63,10 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { : false; } return { - hasLoadedAllDependencies: !! base && !! siteData, + hasLoadedAllDependencies: !! siteData, homepageId: _homepageId, postsPageId: _postsPageId, - url: base?.home, + url: siteData?.home, frontPageTemplateId: _frontPageTemplateId, }; }, [] ); From 9aa524a20990af224bc11d3e6c601d61fee02126 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 13:17:48 +0300 Subject: [PATCH 4/6] Only add for site editor --- lib/compat/wordpress-6.6/rest-api.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 830a898e2f4661..cce919627fa602 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -30,11 +30,15 @@ function wp_api_template_access_controller( $args, $post_type ) { } add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 ); -function gutenberg_block_editor_preload_paths_6_6( $paths ) { - $paths[] = array( '/wp/v2/settings', 'OPTIONS' ); - $paths[] = '/wp/v2/settings'; - $paths[] = array( '/wp/v2/templates', 'OPTIONS' ); - $paths[] = '/wp/v2/types?context=edit'; +function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { + if ( $context->name === 'core/edit-site' ) { + // When merging back to core, these should be added here: + // https://github.com/WordPress/wordpress-develop/blob/7159243c090e429a7d2a1fd2a9509e323f67a39d/src/wp-admin/site-editor.php#L90-L117 + $paths[] = array( '/wp/v2/settings', 'OPTIONS' ); + $paths[] = '/wp/v2/settings'; + $paths[] = array( '/wp/v2/templates', 'OPTIONS' ); + $paths[] = '/wp/v2/types?context=edit'; + } return $paths; } -add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10 ); +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10, 2 ); From e78ab44cdb771b2de0e0447fffc16d7b373ce651 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 13:20:24 +0300 Subject: [PATCH 5/6] lint --- lib/compat/wordpress-6.6/rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index cce919627fa602..40fdc681934370 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -31,7 +31,7 @@ function wp_api_template_access_controller( $args, $post_type ) { add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 ); function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { - if ( $context->name === 'core/edit-site' ) { + if ( 'core/edit-site' === $context->name ) { // When merging back to core, these should be added here: // https://github.com/WordPress/wordpress-develop/blob/7159243c090e429a7d2a1fd2a9509e323f67a39d/src/wp-admin/site-editor.php#L90-L117 $paths[] = array( '/wp/v2/settings', 'OPTIONS' ); From 14f080de826f92d4b571279d372da6dda8f35482 Mon Sep 17 00:00:00 2001 From: Ella Date: Thu, 25 Apr 2024 13:21:12 +0300 Subject: [PATCH 6/6] Remove old comment --- packages/core-data/src/entities.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index cbe6397caf5b10..e91744110faf32 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -27,8 +27,6 @@ export const rootEntitiesConfig = [ name: '__unstableBase', baseURL: '/', baseURLParams: { - // This resource is preloaded. Please ensure the preload is - // updated when this list changes. _fields: [ 'description', 'gmt_offset',