-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 all commits
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,48 @@ 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.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 +120,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 +191,7 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) { | |
}, | ||
[ | ||
homepageId, | ||
postsPageId, | ||
hasLoadedAllDependencies, | ||
url, | ||
postId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
||
async function updateSiteSettings( { pageId, requestUtils } ) { | ||
return requestUtils.updateSiteSettings( { | ||
show_on_front: 'page', | ||
page_on_front: 0, | ||
page_for_posts: pageId, | ||
} ); | ||
} | ||
|
||
test.describe( 'Template resolution', () => { | ||
test.beforeAll( async ( { requestUtils } ) => { | ||
await requestUtils.activateTheme( 'emptytheme' ); | ||
} ); | ||
test.afterEach( async ( { requestUtils } ) => { | ||
await Promise.all( [ | ||
requestUtils.deleteAllPages(), | ||
requestUtils.updateSiteSettings( { | ||
show_on_front: 'posts', | ||
page_on_front: 0, | ||
page_for_posts: 0, | ||
} ), | ||
] ); | ||
} ); | ||
test.afterAll( async ( { requestUtils } ) => { | ||
await requestUtils.activateTheme( 'twentytwentyone' ); | ||
} ); | ||
test( 'Site editor proper front page template resolution when we have only set posts page in settings', async ( { | ||
page, | ||
admin, | ||
requestUtils, | ||
} ) => { | ||
const newPage = await requestUtils.createPage( { | ||
title: 'Posts Page', | ||
status: 'publish', | ||
} ); | ||
await updateSiteSettings( { requestUtils, pageId: newPage.id } ); | ||
await admin.visitSiteEditor(); | ||
await expect( page.locator( '.edit-site-canvas-loader' ) ).toHaveCount( | ||
0 | ||
); | ||
} ); | ||
test.describe( '`page_for_posts` setting', () => { | ||
test( 'Post editor proper template resolution', async ( { | ||
page, | ||
admin, | ||
editor, | ||
requestUtils, | ||
} ) => { | ||
const newPage = await requestUtils.createPage( { | ||
title: 'Posts Page', | ||
status: 'publish', | ||
} ); | ||
await admin.editPost( newPage.id ); | ||
await editor.openDocumentSettingsSidebar(); | ||
await expect( | ||
page.getByRole( 'button', { name: 'Template options' } ) | ||
).toHaveText( 'Single Entries' ); | ||
await updateSiteSettings( { requestUtils, pageId: newPage.id } ); | ||
await page.reload(); | ||
await expect( | ||
page.getByRole( 'button', { name: 'Template options' } ) | ||
).toHaveText( 'Index' ); | ||
} ); | ||
test( 'Site editor proper template resolution', async ( { | ||
page, | ||
editor, | ||
admin, | ||
requestUtils, | ||
} ) => { | ||
const newPage = await requestUtils.createPage( { | ||
title: 'Posts Page', | ||
status: 'publish', | ||
} ); | ||
await updateSiteSettings( { requestUtils, pageId: newPage.id } ); | ||
await admin.visitSiteEditor( { | ||
postId: newPage.id, | ||
postType: 'page', | ||
canvas: 'edit', | ||
} ); | ||
await editor.openDocumentSettingsSidebar(); | ||
await expect( | ||
page.getByRole( 'button', { name: 'Template options' } ) | ||
).toHaveText( 'Index' ); | ||
} ); | ||
} ); | ||
} ); |
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.