Repository navigation
Template field: Disable the field for the front page and the posts page #87541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a55209f
Template field: Disable the field for the front page and the posts page
oandregal 4476b3d
Post list: Rely on the template field to disable itself in quick edit
oandregal 15742c7
Page actions: Update the site base data after setting a page
oandregal d96b8df
Template field: Link the changelog entry to its pull request
oandregal 4862e13
Tests: Expect a read-only template field for the posts page
oandregal 65d7761
Page actions: Mock api-fetch through a typed import in the site base …
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
79
packages/core-actions/src/test/sync-site-base.jsdom.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/core-fields/src/post_type_supports/template/field.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/core-fields/src/post_type_supports/template/is-template-switchable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ export function QuickEditModal( { | |
| null | ||
| ); | ||
| const [ localEdits, setLocalEdits ] = useState< Partial< Post > >( {} ); | ||
| const { record, hasFinishedResolution, canSwitchTemplate } = useSelect( | ||
| const { record, hasFinishedResolution } = useSelect( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ( 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 => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
syncSiteBaseupdates and syncs the corresponding setting immediately when it changes (previously, it required reloading the page).