From a55209f3e716550b7a6420051c1f875b6bcc2521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:27:08 +0100 Subject: [PATCH 1/6] Template field: Disable the field for the front page and the posts page The reading settings assign the template of these pages, so the field can't change it. The control already disabled itself; `isDisabled` now disables the whole field, so every DataForm gets it without overriding the field. Co-Authored-By: Claude Opus 5.5 --- packages/core-fields/CHANGELOG.md | 1 + .../src/post_type_supports/template/edit.tsx | 22 ++------------- .../src/post_type_supports/template/field.php | 4 +-- .../src/post_type_supports/template/field.tsx | 6 +++++ .../template/is-template-switchable.ts | 27 +++++++++++++++++++ .../src/test/post-type-supports.jsdom.test.ts | 3 ++- 6 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 packages/core-fields/src/post_type_supports/template/is-template-switchable.ts diff --git a/packages/core-fields/CHANGELOG.md b/packages/core-fields/CHANGELOG.md index ab486e603529b7..39513fd34e4ce9 100644 --- a/packages/core-fields/CHANGELOG.md +++ b/packages/core-fields/CHANGELOG.md @@ -8,6 +8,7 @@ ### Enhancements +- Template field: Disable the field for the front page and the posts page, whose template comes from the reading settings. - Author fields: Show initials when the author's profile image is unavailable ([#85208](https://github.com/WordPress/gutenberg/pull/85208)). ### Bug Fixes diff --git a/packages/core-fields/src/post_type_supports/template/edit.tsx b/packages/core-fields/src/post_type_supports/template/edit.tsx index 372745a90754ad..ecabfb376986ea 100644 --- a/packages/core-fields/src/post_type_supports/template/edit.tsx +++ b/packages/core-fields/src/post_type_supports/template/edit.tsx @@ -6,7 +6,7 @@ import { useCallback, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { SelectControl } from '@wordpress/ui'; import { getItemTitle } from '../../shared/title/get-item-title'; -import { unlock } from '../../lock-unlock'; +import { isTemplateSwitchable } from './is-template-switchable'; import { useDefaultTemplateLabel, useTemplateFieldMode } from './hooks'; import type { PostWithTemplate } from './types'; @@ -35,30 +35,12 @@ function fromItemValue( itemValue: string | null | undefined ) { return ! itemValue || itemValue === DEFAULT_TEMPLATE_VALUE ? '' : itemValue; } -/** - * Whether the post is the front page or the posts page, which are assigned - * a template by the reading settings rather than by this field. - * - * @param postType The post type. - * @param postId The post id. - * @return Whether the template can be switched. - */ function useCanSwitchTemplate( postType: string, postId: number | string ): boolean { return useSelect( - ( select ) => { - const { getHomePage, getPostsPageId } = unlock( - select( coreStore ) - ); - const singlePostId = String( postId ); - const isPostsPage = getPostsPageId() === singlePostId; - const isFrontPage = - postType === 'page' && getHomePage()?.postId === singlePostId; - - return ! isPostsPage && ! isFrontPage; - }, + ( select ) => isTemplateSwitchable( select, postType, postId ), [ postId, postType ] ); } diff --git a/packages/core-fields/src/post_type_supports/template/field.php b/packages/core-fields/src/post_type_supports/template/field.php index 26791315ac87f8..20a4cd95e500db 100644 --- a/packages/core-fields/src/post_type_supports/template/field.php +++ b/packages/core-fields/src/post_type_supports/template/field.php @@ -1,8 +1,8 @@ [ string ] = { Edit: TemplateEdit, render: TemplateView, + // The front page and the posts page get their template from the + // reading settings, so the field can't change it. + isDisabled: ( { item } ) => + ! isTemplateSwitchable( select, item.type, item.id ), }; diff --git a/packages/core-fields/src/post_type_supports/template/is-template-switchable.ts b/packages/core-fields/src/post_type_supports/template/is-template-switchable.ts new file mode 100644 index 00000000000000..970e019bbfd35d --- /dev/null +++ b/packages/core-fields/src/post_type_supports/template/is-template-switchable.ts @@ -0,0 +1,27 @@ +import { store as coreStore } from '@wordpress/core-data'; +import type { select as selectFn } from '@wordpress/data'; +import { unlock } from '../../lock-unlock'; + +/** + * Whether the template of a post can be switched: the front page and the + * posts page are assigned a template by the reading settings rather than by + * this field. + * + * @param select The `select` function of a registry. + * @param postType The post type. + * @param postId The post id. + * @return Whether the template can be switched. + */ +export function isTemplateSwitchable( + select: typeof selectFn, + postType: string, + postId: number | string +): boolean { + const { getHomePage, getPostsPageId } = unlock( select( coreStore ) ); + const singlePostId = String( postId ); + const isPostsPage = getPostsPageId() === singlePostId; + const isFrontPage = + postType === 'page' && getHomePage()?.postId === singlePostId; + + return ! isPostsPage && ! isFrontPage; +} diff --git a/packages/core-fields/src/test/post-type-supports.jsdom.test.ts b/packages/core-fields/src/test/post-type-supports.jsdom.test.ts index d69f0f01a37d10..c524b928886a4f 100644 --- a/packages/core-fields/src/test/post-type-supports.jsdom.test.ts +++ b/packages/core-fields/src/test/post-type-supports.jsdom.test.ts @@ -229,10 +229,11 @@ describe( 'post_type_supports', () => { } ); } ); - it( 'provides the control and the render of the template', () => { + it( 'provides the control, the render, and the disabled state of the template', () => { expect( postTypeSupports.template ).toEqual( { Edit: expect.any( Function ), render: expect.any( Function ), + isDisabled: expect.any( Function ), } ); } ); From 4476b3d34accc662cc5b17fcce506d5083539615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:27:13 +0100 Subject: [PATCH 2/6] Post list: Rely on the template field to disable itself in quick edit The template field now knows when it can't switch the template, so the quick edit modals no longer make it read-only themselves. Co-Authored-By: Claude Opus 5.5 --- .../components/post-list/quick-edit-modal.tsx | 23 ++----------------- routes/post-list/quick-edit-modal.tsx | 23 ++----------------- 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/packages/edit-site/src/components/post-list/quick-edit-modal.tsx b/packages/edit-site/src/components/post-list/quick-edit-modal.tsx index 45920eb7c83b9b..42ff553f3371e9 100644 --- a/packages/edit-site/src/components/post-list/quick-edit-modal.tsx +++ b/packages/edit-site/src/components/post-list/quick-edit-modal.tsx @@ -40,7 +40,7 @@ export function QuickEditModal( { null ); const [ localEdits, setLocalEdits ] = useState< Partial< Post > >( {} ); - const { record, hasFinishedResolution, canSwitchTemplate } = useSelect( + const { record, hasFinishedResolution } = useSelect( ( select ) => { const { getEditedEntityRecord, @@ -60,24 +60,12 @@ export function QuickEditModal( { postId[ 0 ], ]; - const { getHomePage, getPostsPageId } = unlock( - select( coreDataStore ) - ); - const singlePostId = String( postId[ 0 ] ); - const isPostsPage = - singlePostId !== undefined && getPostsPageId() === singlePostId; - const isFrontPage = - singlePostId !== undefined && - postType === 'page' && - getHomePage()?.postId === singlePostId; - return { record: getEditedEntityRecord( ...args ), hasFinishedResolution: hasFinished( 'getEditedEntityRecord', args ), - canSwitchTemplate: ! isPostsPage && ! isFrontPage, }; }, [ postType, postId, isBulk ] @@ -98,16 +86,9 @@ export function QuickEditModal( { }; } - if ( field.id === 'template' ) { - return { - ...field, - readOnly: ! canSwitchTemplate, - }; - } - return field; } ), - [ _fields, canSwitchTemplate ] + [ _fields ] ); const form = useMemo( (): Form => { diff --git a/routes/post-list/quick-edit-modal.tsx b/routes/post-list/quick-edit-modal.tsx index e6ff8a14f140ba..2cae95e49a2fb2 100644 --- a/routes/post-list/quick-edit-modal.tsx +++ b/routes/post-list/quick-edit-modal.tsx @@ -80,7 +80,7 @@ export function QuickEditModal( { const [ localEdits, setLocalEdits ] = useState< Record< string, any > >( {} ); - const { record, hasFinishedResolution, canSwitchTemplate } = useSelect( + const { record, hasFinishedResolution } = useSelect( ( select ) => { const store = select( coreDataStore ); const { getEditedEntityRecord } = store; @@ -94,24 +94,12 @@ export function QuickEditModal( { const args = [ 'postType', postType, postId[ 0 ] ] as const; - const { getHomePage, getPostsPageId } = unlock( - select( coreDataStore ) - ); - const singlePostId = String( postId[ 0 ] ); - const isPostsPage = - singlePostId !== undefined && getPostsPageId() === singlePostId; - const isFrontPage = - singlePostId !== undefined && - postType === 'page' && - getHomePage()?.postId === singlePostId; - return { record: getEditedEntityRecord( ...args ), hasFinishedResolution: ( store as any ).hasFinishedResolution( 'getEditedEntityRecord', args ), - canSwitchTemplate: ! isPostsPage && ! isFrontPage, }; }, [ postType, postId, isBulk ] @@ -132,13 +120,6 @@ export function QuickEditModal( { ), }; } - - if ( field.id === 'template' ) { - return { - ...field, - readOnly: ! canSwitchTemplate, - }; - } if ( field.id === 'featured_media' && field.Edit ) { return { ...field, @@ -148,7 +129,7 @@ export function QuickEditModal( { return field; } ), - [ _fields, canSwitchTemplate ] + [ _fields ] ); const form = useMemo( (): Form => { From 15742c724fcbe0b3a414d63d2825d12e3708a9d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:18:19 +0100 Subject: [PATCH 3/6] Page actions: Update the site base data after setting a page "Set as homepage" and "Set as posts page" save the `root/site` entity, but `getHomePage()` and `getPostsPageId()` read the pages from `root/__unstableBase`, which the save left untouched. Until the next load, everything built on those selectors kept the previous pages: the template field stayed disabled for the old posts page and editable for the new one. Copy the saved reading settings into the loaded base data once the save succeeds. Co-Authored-By: Claude Opus 5.5 --- .../src/page/set-site-page-modal.tsx | 7 +- .../core-actions/src/page/sync-site-base.ts | 53 +++++++++++++ .../src/test/sync-site-base.jsdom.test.ts | 79 +++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 packages/core-actions/src/page/sync-site-base.ts create mode 100644 packages/core-actions/src/test/sync-site-base.jsdom.test.ts diff --git a/packages/core-actions/src/page/set-site-page-modal.tsx b/packages/core-actions/src/page/set-site-page-modal.tsx index 36352f88932362..761333bfbe966e 100644 --- a/packages/core-actions/src/page/set-site-page-modal.tsx +++ b/packages/core-actions/src/page/set-site-page-modal.tsx @@ -12,6 +12,7 @@ import { store as noticesStore } from '@wordpress/notices'; import { getItemTitle } from '../shared/utils'; import type { BasePost, CoreDataError } from '../shared/types'; import { refetchPages } from './refetch-pages'; +import { syncSiteBase } from './sync-site-base'; export interface SetSitePageModalProps { /** @@ -93,10 +94,11 @@ export function SetSitePageModal( { event.preventDefault(); const previousId = siteSettings?.[ setting ]; + let savedSettings: Record< string, unknown > | undefined; try { // Without `throwOnError`, a failed save resolves with `undefined` // and the success notice would show. - await saveEntityRecord( + savedSettings = await saveEntityRecord( 'root', 'site', { [ setting ]: item.id, show_on_front: 'page' }, @@ -113,6 +115,9 @@ export function SetSitePageModal( { return; } + // `getHomePage()` and `getPostsPageId()` read the base data of the + // site, which the save doesn't update. + syncSiteBase( registry, savedSettings ); createSuccessNotice( successNotice, { type: 'snackbar' } ); closeModal?.(); diff --git a/packages/core-actions/src/page/sync-site-base.ts b/packages/core-actions/src/page/sync-site-base.ts new file mode 100644 index 00000000000000..df10b549922ec8 --- /dev/null +++ b/packages/core-actions/src/page/sync-site-base.ts @@ -0,0 +1,53 @@ +import { store as coreStore } from '@wordpress/core-data'; +import type { DataRegistry } from '@wordpress/data'; + +/** + * The reading settings that the base data of the site mirrors. + */ +const READING_SETTINGS = [ + 'show_on_front', + 'page_on_front', + 'page_for_posts', +] as const; + +/** + * Copies the saved reading settings into the base data of the site. + * + * The homepage and the posts page are saved to the `root/site` entity, while + * `getHomePage()` and `getPostsPageId()` read them from `root/__unstableBase`, + * which saving the settings doesn't update. Without this, whatever depends on + * those selectors, such as the template field, keeps the previous pages until + * the next load. + * + * Base data that isn't loaded yet is left alone: requesting it here fetches + * it, and the response already holds the saved settings. + * + * @param registry The data registry. + * @param savedSettings The site settings the save responded with. + */ +export function syncSiteBase( + registry: DataRegistry, + savedSettings: Record< string, unknown > | undefined +) { + if ( ! savedSettings ) { + return; + } + const base = registry + .select( coreStore ) + .getEntityRecord( 'root', '__unstableBase' ); + if ( ! base ) { + return; + } + const readingSettings: Record< string, unknown > = {}; + for ( const setting of READING_SETTINGS ) { + if ( setting in savedSettings ) { + readingSettings[ setting ] = savedSettings[ setting ]; + } + } + registry + .dispatch( coreStore ) + .receiveEntityRecords( 'root', '__unstableBase', { + ...base, + ...readingSettings, + } ); +} diff --git a/packages/core-actions/src/test/sync-site-base.jsdom.test.ts b/packages/core-actions/src/test/sync-site-base.jsdom.test.ts new file mode 100644 index 00000000000000..5a00f4cf6ecd82 --- /dev/null +++ b/packages/core-actions/src/test/sync-site-base.jsdom.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, it, vi } from 'vitest'; +import { store as coreStore } from '@wordpress/core-data'; +import { createRegistry } from '@wordpress/data'; +import { unlock } from '../lock-unlock'; +import { syncSiteBase } from '../page/sync-site-base'; + +// The resolvers of `core-data` request what isn't in the store: leave those +// requests pending, so the store holds only what the tests receive. +vi.mock( '@wordpress/api-fetch', () => ( { + default: vi.fn( () => new Promise( () => {} ) ), +} ) ); + +function createRegistryWithBase( base?: Record< string, unknown > ) { + const registry = createRegistry(); + registry.register( coreStore ); + if ( base ) { + registry + .dispatch( coreStore ) + .receiveEntityRecords( 'root', '__unstableBase', base ); + } + return registry; +} + +const BASE = { + name: 'Site', + home: 'https://example.org', + show_on_front: 'page', + page_on_front: 2, + page_for_posts: 3, +}; + +describe( 'syncSiteBase', () => { + it( 'copies the saved reading settings into the base data', () => { + const registry = createRegistryWithBase( BASE ); + + syncSiteBase( registry, { + title: 'Site', + show_on_front: 'page', + page_on_front: 2, + page_for_posts: 4, + } ); + + expect( + registry + .select( coreStore ) + .getEntityRecord( 'root', '__unstableBase' ) + ).toEqual( { ...BASE, page_for_posts: 4 } ); + expect( unlock( registry.select( coreStore ) ).getPostsPageId() ).toBe( + '4' + ); + expect( unlock( registry.select( coreStore ) ).getHomePage() ).toEqual( + { postType: 'page', postId: '2' } + ); + } ); + + it( 'leaves base data that is not loaded alone', () => { + const registry = createRegistryWithBase(); + + syncSiteBase( registry, { page_for_posts: 4 } ); + + expect( + registry + .select( coreStore ) + .getEntityRecord( 'root', '__unstableBase' ) + ).toBeUndefined(); + } ); + + it( 'does nothing without saved settings', () => { + const registry = createRegistryWithBase( BASE ); + + syncSiteBase( registry, undefined ); + + expect( + registry + .select( coreStore ) + .getEntityRecord( 'root', '__unstableBase' ) + ).toEqual( BASE ); + } ); +} ); From d96b8df7578db9629da6a861a23bcb0e4ba3d278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:00:56 +0100 Subject: [PATCH 4/6] Template field: Link the changelog entry to its pull request Co-Authored-By: Claude Opus 5.5 --- packages/core-fields/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-fields/CHANGELOG.md b/packages/core-fields/CHANGELOG.md index 39513fd34e4ce9..4e3681513bace8 100644 --- a/packages/core-fields/CHANGELOG.md +++ b/packages/core-fields/CHANGELOG.md @@ -8,7 +8,7 @@ ### Enhancements -- Template field: Disable the field for the front page and the posts page, whose template comes from the reading settings. +- Template field: Disable the field for the front page and the posts page, whose template comes from the reading settings ([#87541](https://github.com/WordPress/gutenberg/pull/87541)). - Author fields: Show initials when the author's profile image is unavailable ([#85208](https://github.com/WordPress/gutenberg/pull/85208)). ### Bug Fixes From 4862e13fb8ade99031f7ca02a5077307fc40e8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:00:57 +0100 Subject: [PATCH 5/6] Tests: Expect a read-only template field for the posts page The reading settings assign the template of the posts page, so the field no longer offers an edit button there. Check the value it shows and that the button is gone. Co-Authored-By: Claude Opus 5.5 --- .../various/template-resolution.spec.js | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/e2e/specs/editor/various/template-resolution.spec.js b/test/e2e/specs/editor/various/template-resolution.spec.js index 93cf378b34d29e..bcfaa610b80aae 100644 --- a/test/e2e/specs/editor/various/template-resolution.spec.js +++ b/test/e2e/specs/editor/various/template-resolution.spec.js @@ -152,9 +152,18 @@ test.describe( 'Template resolution', () => { await updateSiteSettings( { requestUtils, pageId: newPage.id } ); await page.reload(); await openPostSummary( { editor, page } ); + // The reading settings assign the template of the posts page, so + // the field shows it without offering to edit it. + await expect( + summary + .locator( '.dataforms-layouts-panel__field-trigger' ) + .filter( { + has: page.getByText( 'Template', { exact: true } ), + } ) + ).toContainText( 'Index' ); await expect( summary.getByRole( 'button', { name: 'Edit Template' } ) - ).toHaveAccessibleDescription( 'Index' ); + ).toBeHidden(); } ); test( 'Site editor proper template resolution', async ( { @@ -174,9 +183,18 @@ test.describe( 'Template resolution', () => { canvas: 'edit', } ); const summary = await openPostSummary( { editor, page } ); + // The reading settings assign the template of the posts page, so + // the field shows it without offering to edit it. + await expect( + summary + .locator( '.dataforms-layouts-panel__field-trigger' ) + .filter( { + has: page.getByText( 'Template', { exact: true } ), + } ) + ).toContainText( 'Index' ); await expect( summary.getByRole( 'button', { name: 'Edit Template' } ) - ).toHaveAccessibleDescription( 'Index' ); + ).toBeHidden(); } ); } ); } ); From 65d77617a88e0ec84133460ca30016916be696c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:01:05 +0100 Subject: [PATCH 6/6] Page actions: Mock api-fetch through a typed import in the site base test The Vitest conventions require vi.mock( import( ... ) ) in TypeScript tests. Co-Authored-By: Claude Opus 5.5 --- packages/core-actions/src/test/sync-site-base.jsdom.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core-actions/src/test/sync-site-base.jsdom.test.ts b/packages/core-actions/src/test/sync-site-base.jsdom.test.ts index 5a00f4cf6ecd82..fcf76f7a806e11 100644 --- a/packages/core-actions/src/test/sync-site-base.jsdom.test.ts +++ b/packages/core-actions/src/test/sync-site-base.jsdom.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it, vi } from 'vitest'; +import apiFetch from '@wordpress/api-fetch'; import { store as coreStore } from '@wordpress/core-data'; import { createRegistry } from '@wordpress/data'; import { unlock } from '../lock-unlock'; @@ -6,9 +7,8 @@ import { syncSiteBase } from '../page/sync-site-base'; // The resolvers of `core-data` request what isn't in the store: leave those // requests pending, so the store holds only what the tests receive. -vi.mock( '@wordpress/api-fetch', () => ( { - default: vi.fn( () => new Promise( () => {} ) ), -} ) ); +vi.mock( import( '@wordpress/api-fetch' ) ); +vi.mocked( apiFetch ).mockImplementation( () => new Promise( () => {} ) ); function createRegistryWithBase( base?: Record< string, unknown > ) { const registry = createRegistry();