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..fcf76f7a806e11 --- /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 apiFetch from '@wordpress/api-fetch'; +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( import( '@wordpress/api-fetch' ) ); +vi.mocked( apiFetch ).mockImplementation( () => 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 ); + } ); +} ); diff --git a/packages/core-fields/CHANGELOG.md b/packages/core-fields/CHANGELOG.md index ab486e603529b7..4e3681513bace8 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 ([#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 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 ), } ); } ); 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 => { 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(); } ); } ); } );