From a079755c9ef86e6a46d15beaae46db2eee127d77 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 16 Mar 2026 15:47:36 +0200 Subject: [PATCH 1/4] Prolerly resolve `getTemplateId` for hybrid themes --- packages/core-data/src/private-selectors.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 76873c9a3ed1a5..f9bdf6520cf2a3 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -201,11 +201,6 @@ export const getPostsPageId = createRegistrySelector( ( select ) => () => { export const getTemplateId = createRegistrySelector( ( select ) => ( state, postType, postId ) => { const homepage = unlock( select( STORE_NAME ) ).getHomePage(); - - if ( ! homepage ) { - return; - } - // For the front page, we always use the front page template if existing. if ( postType === 'page' && From 646736a43f2ed90ca0c27ff4f0bb6f8295397bb5 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 17 Mar 2026 09:30:54 +0200 Subject: [PATCH 2/4] updates --- packages/core-data/src/private-selectors.ts | 22 +++++++++++++++++---- packages/core-data/src/resolvers.js | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index f9bdf6520cf2a3..65d02253c24289 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -14,6 +14,8 @@ import logEntityDeprecation from './utils/log-entity-deprecation'; type EntityRecordKey = string | number; +const EMPTY_OBJECT = {}; + /** * Returns the previous edit from the current undo offset * for the entity records edits history, if any. @@ -170,11 +172,18 @@ export const getHomePage = createRegistrySelector( ( select ) => ).getDefaultTemplateId( { slug: 'front-page', } ); - // Still resolving getDefaultTemplateId. - if ( ! frontPageTemplateId ) { - return null; + if ( frontPageTemplateId ) { + return { + postType: 'wp_template', + postId: frontPageTemplateId, + }; } - return { postType: 'wp_template', postId: frontPageTemplateId }; + // Resolution is finished and no front-page template exists. + if ( frontPageTemplateId === '' ) { + return EMPTY_OBJECT; + } + // Still resolving getDefaultTemplateId. + return null; }, ( state ) => [ // Even though getDefaultTemplateId.shouldInvalidate returns true when root/site changes, @@ -201,6 +210,11 @@ export const getPostsPageId = createRegistrySelector( ( select ) => () => { export const getTemplateId = createRegistrySelector( ( select ) => ( state, postType, postId ) => { const homepage = unlock( select( STORE_NAME ) ).getHomePage(); + + if ( ! homepage ) { + return; + } + // For the front page, we always use the front page template if existing. if ( postType === 'page' && diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index a08e25cb18b4e5..d4fc908b8520a7 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -992,10 +992,10 @@ export const getDefaultTemplateId = ? template?.wp_id || template?.id : template?.id; // Endpoint may return an empty object if no template is found. + dispatch.receiveDefaultTemplateId( query, id || '' ); if ( id ) { template.id = id; registry.batch( () => { - dispatch.receiveDefaultTemplateId( query, id ); dispatch.receiveEntityRecords( 'postType', template.type, From 8f3a63e34b13061f6479cd7c1b4bb2847b9a0a2a Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 17 Mar 2026 10:27:19 +0200 Subject: [PATCH 3/4] remove unused utils --- packages/fields/src/fields/template/utils.ts | 119 ------------------- 1 file changed, 119 deletions(-) delete mode 100644 packages/fields/src/fields/template/utils.ts diff --git a/packages/fields/src/fields/template/utils.ts b/packages/fields/src/fields/template/utils.ts deleted file mode 100644 index 59ef294703ebb8..00000000000000 --- a/packages/fields/src/fields/template/utils.ts +++ /dev/null @@ -1,119 +0,0 @@ -/** - * WordPress dependencies - */ -import { store as coreStore } from '@wordpress/core-data'; -import type { WpTemplate } from '@wordpress/core-data'; -import { __ } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import { getItemTitle } from '../../actions/utils'; -import { unlock } from '../../lock-unlock'; - -/** - * Compute the template slug to look up in the template hierarchy. - * - * In `draft` status we might not have a slug available, so we use the - * `single` post type template slug (e.g. page, single-post, - * single-product, etc.). Pages do not need the `single` prefix to be - * prioritised through template hierarchy. - * - * @param postType The post type. - * @param slug The post slug. - */ -export function getTemplateSlugToCheck( - postType: string, - slug: string | undefined -): string { - if ( slug ) { - return postType === 'page' - ? `${ postType }-${ slug }` - : `single-${ postType }-${ slug }`; - } - return postType === 'page' ? 'page' : `single-${ postType }`; -} - -/** - * Resolve the human-readable label for the default template that would - * apply to a post, given its type, ID and slug. Meant to be called inside a - * `useSelect` callback (receives `select` as its first argument). - * - * @param select The `select` function from a `useSelect` callback. - * @param postType The post type. - * @param postId The post ID. - * @param slug The post slug. - */ -export function getDefaultTemplateLabel( - select: ( store: typeof coreStore ) => any, - postType: string | undefined, - postId: string | number | undefined, - slug: string | undefined -): string { - if ( ! postType || ! postId ) { - return __( 'Default template' ); - } - - // For the front page, we always use the front page template if existing. - const homePage = unlock( select( coreStore ) ).getHomePage(); - if ( - postType === 'page' && - homePage?.postType === 'page' && - homePage?.postId === String( postId ) - ) { - // The /lookup endpoint cannot currently handle a lookup - // when a page is set as the front page, so specifically in - // that case, we want to check if there is a front page - // template, and instead of falling back to the home - // template, we want to fall back to the page template. - const templates = select( coreStore ).getEntityRecords( - 'postType', - 'wp_template', - { per_page: -1 } - ); - const frontPage = templates?.find( - ( t: WpTemplate ) => t.slug === 'front-page' - ); - if ( frontPage ) { - return getItemTitle( frontPage ); - } - // If no front page template is found, continue with the - // logic below (fetching the page template). - } - - const postsPageId = unlock( select( coreStore ) ).getPostsPageId(); - // Check if the current page is the posts page. - if ( postType === 'page' && postsPageId === String( postId ) ) { - const templateId = select( coreStore ).getDefaultTemplateId( { - slug: 'home', - } ); - if ( templateId ) { - const template = select( coreStore ).getEntityRecord( - 'postType', - 'wp_template', - templateId - ); - if ( template ) { - return getItemTitle( template ); - } - } - return __( 'Default template' ); - } - - const slugToCheck = getTemplateSlugToCheck( postType, slug ); - const templateId = select( coreStore ).getDefaultTemplateId( { - slug: slugToCheck, - } ); - - if ( ! templateId ) { - return __( 'Default template' ); - } - - const template = select( coreStore ).getEntityRecord( - 'postType', - 'wp_template', - templateId - ); - - return template ? getItemTitle( template ) : __( 'Default template' ); -} From 08982f1759b6f5a3d659d4300a02faa9300050a0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 23 Mar 2026 09:58:36 +0200 Subject: [PATCH 4/4] keep dispatches in on batch --- packages/core-data/src/resolvers.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index d4fc908b8520a7..1abd7d7e669b30 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -991,11 +991,12 @@ export const getDefaultTemplateId = const id = window?.__experimentalTemplateActivate ? template?.wp_id || template?.id : template?.id; - // Endpoint may return an empty object if no template is found. - dispatch.receiveDefaultTemplateId( query, id || '' ); - if ( id ) { - template.id = id; - registry.batch( () => { + + registry.batch( () => { + dispatch.receiveDefaultTemplateId( query, id || '' ); + // Endpoint may return an empty object if no template is found. + if ( id ) { + template.id = id; dispatch.receiveEntityRecords( 'postType', template.type, @@ -1007,8 +1008,8 @@ export const getDefaultTemplateId = template.type, id, ] ); - } ); - } + } + } ); }; getDefaultTemplateId.shouldInvalidate = ( action ) => {