-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Template field: Fix default label and skip wasted front-page lookup #78338
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
Open
ellatrix
wants to merge
1
commit into
trunk
Choose a base branch
from
fix/skip-front-page-lookup-non-pages
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−125
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -59,100 +59,33 @@ export function useTemplateFieldMode( | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| function getTemplateSlugToCheck( | ||
| postType: string, | ||
| slug: string | undefined | ||
| ): string { | ||
| if ( slug ) { | ||
| return postType === 'page' | ||
| ? `${ postType }-${ slug }` | ||
| : `single-${ postType }-${ slug }`; | ||
| } | ||
| return postType === 'page' ? 'page' : `single-${ postType }`; | ||
| } | ||
|
|
||
| const NAME_NOT_FOUND = ''; | ||
|
|
||
| /** | ||
| * Hook that resolves the human-readable label for the default template | ||
| * that would apply to a post, given its type, ID and slug. | ||
| * that would apply to a post, given its type and ID. | ||
| * | ||
| * Delegates the homepage / posts-page / template-hierarchy resolution to | ||
| * the `getTemplateId` private selector in `@wordpress/core-data`, then | ||
| * looks up the resolved template's record for its title. | ||
| * | ||
| * @param postType The post type. | ||
| * @param postId The post ID. | ||
| * @param slug The post slug. | ||
| */ | ||
| export function useDefaultTemplateLabel( | ||
| postType: string | undefined, | ||
| postId: string | number | undefined, | ||
| slug: string | undefined | ||
| postId: string | number | undefined | ||
| ): string { | ||
| return useSelect( | ||
| ( select ) => { | ||
| if ( ! postType || ! postId ) { | ||
| return NAME_NOT_FOUND; | ||
| } | ||
|
|
||
| const postIdStr = String( postId ); | ||
|
Contributor
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. The main difference between the hook and the selector is that the hook would always return the Screen.Recording.2026-05-15.at.5.19.42.PM.mov |
||
|
|
||
| // Check if the current page is the front page. | ||
| const homePage = unlock( select( coreStore ) ).getHomePage(); | ||
| if ( | ||
| postType === 'page' && | ||
| homePage?.postType === 'page' && | ||
| homePage?.postId === postIdStr | ||
| ) { | ||
| const templates = select( | ||
| coreStore | ||
| ).getEntityRecords< WpTemplate >( 'postType', 'wp_template', { | ||
| per_page: -1, | ||
| } ); | ||
| const frontPage = templates?.find( | ||
| ( t ) => t.slug === 'front-page' | ||
| ); | ||
| if ( frontPage ) { | ||
| return getItemTitle( frontPage ); | ||
| } | ||
|
|
||
| // If no front page template is found, fall back to the page template. | ||
| // See @getTemplateId private selector in core-data package. | ||
| } | ||
|
|
||
| // Check if the current page is the posts page. | ||
| const postsPageId = unlock( select( coreStore ) ).getPostsPageId(); | ||
| if ( postType === 'page' && postsPageId === postIdStr ) { | ||
| const templateId = select( coreStore ).getDefaultTemplateId( { | ||
| slug: 'home', | ||
| } ); | ||
| if ( ! templateId ) { | ||
| return NAME_NOT_FOUND; | ||
| } | ||
|
|
||
| const template = select( | ||
| coreStore | ||
| ).getEntityRecord< WpTemplate >( | ||
| 'postType', | ||
| 'wp_template', | ||
| templateId | ||
| ); | ||
| return template ? getItemTitle( template ) : NAME_NOT_FOUND; | ||
| } | ||
|
|
||
| // Check any other case. | ||
| const slugToCheck = getTemplateSlugToCheck( postType, slug ); | ||
| const templateId = select( coreStore ).getDefaultTemplateId( { | ||
| slug: slugToCheck, | ||
| } ); | ||
| const templateId = unlock( select( coreStore ) ).getTemplateId( | ||
| postType, | ||
| postId | ||
| ); | ||
| if ( ! templateId ) { | ||
| return NAME_NOT_FOUND; | ||
| } | ||
|
|
@@ -164,6 +97,6 @@ export function useDefaultTemplateLabel( | |
| ); | ||
| return template ? getItemTitle( template ) : NAME_NOT_FOUND; | ||
| }, | ||
| [ postType, postId, slug ] | ||
| [ postType, postId ] | ||
| ); | ||
| } | ||
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
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.
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.
Changes in this file could be split in a separate PR.