Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/core-actions/src/page/set-site-page-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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' },
Expand All @@ -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 );

@oandregal oandregal Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modal is used by the two actions: "set as homepage", "set as posts page" that change the site setting. syncSiteBase updates and syncs the corresponding setting immediately when it changes (previously, it required reloading the page).

createSuccessNotice( successNotice, { type: 'snackbar' } );
closeModal?.();

Expand Down
53 changes: 53 additions & 0 deletions packages/core-actions/src/page/sync-site-base.ts
Original file line number Diff line number Diff line change
@@ -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,
} );
}
79 changes: 79 additions & 0 deletions packages/core-actions/src/test/sync-site-base.jsdom.test.ts
Original file line number Diff line number Diff line change
@@ -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 );
} );
} );
1 change: 1 addition & 0 deletions packages/core-fields/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 2 additions & 20 deletions packages/core-fields/src/post_type_supports/template/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 ]
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* The template that renders a post, for every post type but the design
* ones. Its JavaScript parts (`Edit` and `render`) are in `field.tsx`, next
* to this file.
* ones. Its JavaScript parts (`Edit`, `render`, and `isDisabled`) are in
* `field.tsx`, next to this file.
*
* Whether a post can be assigned a template depends on the theme and on the
* post resolving to one, which only the client knows: both the control and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { select } from '@wordpress/data';
import type { FieldsScriptParts } from '../../types';
import { isTemplateSwitchable } from './is-template-switchable';
import TemplateEdit from './edit';
import TemplateView from './view';
import type { PostWithTemplate } from './types';
Expand All @@ -10,4 +12,8 @@ export const fieldExtensions: FieldsScriptParts< PostWithTemplate >[ 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 ),
};
Original file line number Diff line number Diff line change
@@ -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(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This centralizes the logic that was across QuickEdit and the field's Edit.

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
} );
} );

Expand Down
23 changes: 2 additions & 21 deletions packages/edit-site/src/components/post-list/quick-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function QuickEditModal( {
null
);
const [ localEdits, setLocalEdits ] = useState< Partial< Post > >( {} );
const { record, hasFinishedResolution, canSwitchTemplate } = useSelect(
const { record, hasFinishedResolution } = useSelect(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes here are about removing field-specific logic from the screen. It should live in the field itself (see). This is replaced by making the field itself disabled when it cannot switch templates. As a result, we have the same logic working for quickedit and inspector (fixes #87540).

( select ) => {
const {
getEditedEntityRecord,
Expand All @@ -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 ]
Expand All @@ -98,16 +86,9 @@ export function QuickEditModal( {
};
}

if ( field.id === 'template' ) {
return {
...field,
readOnly: ! canSwitchTemplate,
};
}

return field;
} ),
[ _fields, canSwitchTemplate ]
[ _fields ]
);

const form = useMemo( (): Form => {
Expand Down
23 changes: 2 additions & 21 deletions routes/post-list/quick-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ]
Expand All @@ -132,13 +120,6 @@ export function QuickEditModal( {
),
};
}

if ( field.id === 'template' ) {
return {
...field,
readOnly: ! canSwitchTemplate,
};
}
if ( field.id === 'featured_media' && field.Edit ) {
return {
...field,
Expand All @@ -148,7 +129,7 @@ export function QuickEditModal( {

return field;
} ),
[ _fields, canSwitchTemplate ]
[ _fields ]
);

const form = useMemo( (): Form => {
Expand Down
Loading
Loading