-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fix static posts page setting resolved template #60608
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,42 +28,49 @@ const postTypesWithoutParentTemplate = [ | |
]; | ||
|
||
function useResolveEditedEntityAndContext( { path, postId, postType } ) { | ||
const { hasLoadedAllDependencies, homepageId, url, frontPageTemplateId } = | ||
useSelect( ( select ) => { | ||
const { getSite, getUnstableBase, getEntityRecords } = | ||
select( coreDataStore ); | ||
const siteData = getSite(); | ||
const base = getUnstableBase(); | ||
const templates = getEntityRecords( | ||
'postType', | ||
TEMPLATE_POST_TYPE, | ||
{ | ||
per_page: -1, | ||
} | ||
const { | ||
hasLoadedAllDependencies, | ||
homepageId, | ||
postsPageId, | ||
url, | ||
frontPageTemplateId, | ||
} = useSelect( ( select ) => { | ||
const { getSite, getUnstableBase, getEntityRecords } = | ||
select( coreDataStore ); | ||
const siteData = getSite(); | ||
const base = getUnstableBase(); | ||
const templates = getEntityRecords( 'postType', TEMPLATE_POST_TYPE, { | ||
per_page: -1, | ||
} ); | ||
const _homepageId = | ||
siteData?.show_on_front === 'page' && | ||
[ 'number', 'string' ].includes( typeof siteData.page_on_front ) && | ||
!! +siteData.page_on_front // We also need to check if it's not zero(`0`). | ||
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 is the fix for the |
||
? siteData.page_on_front.toString() | ||
: null; | ||
const _postsPageId = | ||
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. Shouldn't we also check page_for_posts for zero? 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. It doesn't matter for |
||
siteData?.show_on_front === 'page' && | ||
[ 'number', 'string' ].includes( typeof siteData.page_for_posts ) && | ||
!! +siteData.page_for_posts // We also need to check if it's not zero(`0`). | ||
? siteData.page_for_posts.toString() | ||
: null; | ||
let _frontPageTemplateId; | ||
if ( templates ) { | ||
const frontPageTemplate = templates.find( | ||
( t ) => t.slug === 'front-page' | ||
); | ||
let _frontPateTemplateId; | ||
if ( templates ) { | ||
const frontPageTemplate = templates.find( | ||
( t ) => t.slug === 'front-page' | ||
); | ||
_frontPateTemplateId = frontPageTemplate | ||
? frontPageTemplate.id | ||
: false; | ||
} | ||
|
||
return { | ||
hasLoadedAllDependencies: !! base && !! siteData, | ||
homepageId: | ||
siteData?.show_on_front === 'page' && | ||
[ 'number', 'string' ].includes( | ||
typeof siteData.page_on_front | ||
) | ||
? siteData.page_on_front.toString() | ||
: null, | ||
url: base?.home, | ||
frontPageTemplateId: _frontPateTemplateId, | ||
}; | ||
}, [] ); | ||
_frontPageTemplateId = frontPageTemplate | ||
? frontPageTemplate.id | ||
: false; | ||
} | ||
return { | ||
hasLoadedAllDependencies: !! base && !! siteData, | ||
homepageId: _homepageId, | ||
postsPageId: _postsPageId, | ||
url: base?.home, | ||
frontPageTemplateId: _frontPageTemplateId, | ||
}; | ||
}, [] ); | ||
|
||
/** | ||
* This is a hook that recreates the logic to resolve a template for a given WordPress postID postTypeId | ||
|
@@ -114,6 +121,14 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { | |
if ( ! editedEntity ) { | ||
return undefined; | ||
} | ||
// Check if the current page is the posts page. | ||
if ( | ||
postTypeToResolve === 'page' && | ||
postsPageId === postIdToResolve | ||
) { | ||
return __experimentalGetTemplateForLink( editedEntity.link ) | ||
?.id; | ||
} | ||
// First see if the post/page has an assigned template and fetch it. | ||
const currentTemplateSlug = editedEntity.template; | ||
if ( currentTemplateSlug ) { | ||
|
@@ -177,6 +192,7 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { | |
}, | ||
[ | ||
homepageId, | ||
postsPageId, | ||
hasLoadedAllDependencies, | ||
url, | ||
postId, | ||
|
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 is the main change. Rest of the changes are due to the added object deconstruction..
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.
The +postId is to make sure postId is an integer, in which cases it is not an integer could we not safely assume it is always an integer like we are assuming page_for_posts is an integer?
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.
I'm not really sure what is the flow that could result in a string and I left that untouched for now. ). Probably some cases for comparing post entities ids and the site settings - I think I had come across something like that in the past.