Skip to content

Commit f1abd8b

Browse files
ntsekourasjorgefilipecostaMamaduka
authored andcommitted
Fix static posts page setting resolved template (#60608)
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
1 parent 9ad8c97 commit f1abd8b

File tree

4 files changed

+174
-47
lines changed

4 files changed

+174
-47
lines changed

packages/e2e-test-utils-playwright/src/request-utils/site-settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type SiteSettings = {
1919
posts_per_page: number;
2020
default_ping_status: 'open' | 'closed';
2121
default_comment_status: 'open' | 'closed';
22+
show_on_front: 'posts' | 'page';
23+
page_on_front: number;
24+
page_for_posts: number;
2225
};
2326

2427
/**

packages/edit-post/src/store/selectors.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -577,36 +577,55 @@ export function areMetaBoxesInitialized( state ) {
577577
*/
578578
export const getEditedPostTemplate = createRegistrySelector(
579579
( select ) => () => {
580+
const {
581+
id: postId,
582+
type: postType,
583+
slug,
584+
} = select( editorStore ).getCurrentPost();
585+
const { getSite, getEditedEntityRecord, getEntityRecords } =
586+
select( coreStore );
587+
const siteSettings = getSite();
588+
// First check if the current page is set as the posts page.
589+
const isPostsPage = +postId === siteSettings?.page_for_posts;
590+
if ( isPostsPage ) {
591+
const defaultTemplateId = select( coreStore ).getDefaultTemplateId(
592+
{ slug: 'home' }
593+
);
594+
return getEditedEntityRecord(
595+
'postType',
596+
'wp_template',
597+
defaultTemplateId
598+
);
599+
}
580600
const currentTemplate =
581601
select( editorStore ).getEditedPostAttribute( 'template' );
582602
if ( currentTemplate ) {
583-
const templateWithSameSlug = select( coreStore )
584-
.getEntityRecords( 'postType', 'wp_template', { per_page: -1 } )
585-
?.find( ( template ) => template.slug === currentTemplate );
603+
const templateWithSameSlug = getEntityRecords(
604+
'postType',
605+
'wp_template',
606+
{ per_page: -1 }
607+
)?.find( ( template ) => template.slug === currentTemplate );
586608
if ( ! templateWithSameSlug ) {
587609
return templateWithSameSlug;
588610
}
589-
return select( coreStore ).getEditedEntityRecord(
611+
return getEditedEntityRecord(
590612
'postType',
591613
'wp_template',
592614
templateWithSameSlug.id
593615
);
594616
}
595-
596-
const post = select( editorStore ).getCurrentPost();
597617
let slugToCheck;
598618
// In `draft` status we might not have a slug available, so we use the `single`
599619
// post type templates slug(ex page, single-post, single-product etc..).
600620
// Pages do not need the `single` prefix in the slug to be prioritized
601621
// through template hierarchy.
602-
if ( post.slug ) {
622+
if ( slug ) {
603623
slugToCheck =
604-
post.type === 'page'
605-
? `${ post.type }-${ post.slug }`
606-
: `single-${ post.type }-${ post.slug }`;
624+
postType === 'page'
625+
? `${ postType }-${ slug }`
626+
: `single-${ postType }-${ slug }`;
607627
} else {
608-
slugToCheck =
609-
post.type === 'page' ? 'page' : `single-${ post.type }`;
628+
slugToCheck = postType === 'page' ? 'page' : `single-${ postType }`;
610629
}
611630
const defaultTemplateId = select( coreStore ).getDefaultTemplateId( {
612631
slug: slugToCheck,

packages/edit-site/src/components/sync-state-with-url/use-init-edited-entity-from-url.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,48 @@ const postTypesWithoutParentTemplate = [
2828
];
2929

3030
function useResolveEditedEntityAndContext( { path, postId, postType } ) {
31-
const { hasLoadedAllDependencies, homepageId, url, frontPageTemplateId } =
32-
useSelect( ( select ) => {
33-
const { getSite, getUnstableBase, getEntityRecords } =
34-
select( coreDataStore );
35-
const siteData = getSite();
36-
const base = getUnstableBase();
37-
const templates = getEntityRecords(
38-
'postType',
39-
TEMPLATE_POST_TYPE,
40-
{
41-
per_page: -1,
42-
}
31+
const {
32+
hasLoadedAllDependencies,
33+
homepageId,
34+
postsPageId,
35+
url,
36+
frontPageTemplateId,
37+
} = useSelect( ( select ) => {
38+
const { getSite, getUnstableBase, getEntityRecords } =
39+
select( coreDataStore );
40+
const siteData = getSite();
41+
const base = getUnstableBase();
42+
const templates = getEntityRecords( 'postType', TEMPLATE_POST_TYPE, {
43+
per_page: -1,
44+
} );
45+
const _homepageId =
46+
siteData?.show_on_front === 'page' &&
47+
[ 'number', 'string' ].includes( typeof siteData.page_on_front ) &&
48+
!! +siteData.page_on_front // We also need to check if it's not zero(`0`).
49+
? siteData.page_on_front.toString()
50+
: null;
51+
const _postsPageId =
52+
siteData?.show_on_front === 'page' &&
53+
[ 'number', 'string' ].includes( typeof siteData.page_for_posts )
54+
? siteData.page_for_posts.toString()
55+
: null;
56+
let _frontPageTemplateId;
57+
if ( templates ) {
58+
const frontPageTemplate = templates.find(
59+
( t ) => t.slug === 'front-page'
4360
);
44-
let _frontPateTemplateId;
45-
if ( templates ) {
46-
const frontPageTemplate = templates.find(
47-
( t ) => t.slug === 'front-page'
48-
);
49-
_frontPateTemplateId = frontPageTemplate
50-
? frontPageTemplate.id
51-
: false;
52-
}
53-
54-
return {
55-
hasLoadedAllDependencies: !! base && !! siteData,
56-
homepageId:
57-
siteData?.show_on_front === 'page' &&
58-
[ 'number', 'string' ].includes(
59-
typeof siteData.page_on_front
60-
)
61-
? siteData.page_on_front.toString()
62-
: null,
63-
url: base?.home,
64-
frontPageTemplateId: _frontPateTemplateId,
65-
};
66-
}, [] );
61+
_frontPageTemplateId = frontPageTemplate
62+
? frontPageTemplate.id
63+
: false;
64+
}
65+
return {
66+
hasLoadedAllDependencies: !! base && !! siteData,
67+
homepageId: _homepageId,
68+
postsPageId: _postsPageId,
69+
url: base?.home,
70+
frontPageTemplateId: _frontPageTemplateId,
71+
};
72+
}, [] );
6773

6874
/**
6975
* 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 } ) {
114120
if ( ! editedEntity ) {
115121
return undefined;
116122
}
123+
// Check if the current page is the posts page.
124+
if (
125+
postTypeToResolve === 'page' &&
126+
postsPageId === postIdToResolve
127+
) {
128+
return __experimentalGetTemplateForLink( editedEntity.link )
129+
?.id;
130+
}
117131
// First see if the post/page has an assigned template and fetch it.
118132
const currentTemplateSlug = editedEntity.template;
119133
if ( currentTemplateSlug ) {
@@ -177,6 +191,7 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) {
177191
},
178192
[
179193
homepageId,
194+
postsPageId,
180195
hasLoadedAllDependencies,
181196
url,
182197
postId,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
5+
6+
async function updateSiteSettings( { pageId, requestUtils } ) {
7+
return requestUtils.updateSiteSettings( {
8+
show_on_front: 'page',
9+
page_on_front: 0,
10+
page_for_posts: pageId,
11+
} );
12+
}
13+
14+
test.describe( 'Template resolution', () => {
15+
test.beforeAll( async ( { requestUtils } ) => {
16+
await requestUtils.activateTheme( 'emptytheme' );
17+
} );
18+
test.afterEach( async ( { requestUtils } ) => {
19+
await Promise.all( [
20+
requestUtils.deleteAllPages(),
21+
requestUtils.updateSiteSettings( {
22+
show_on_front: 'posts',
23+
page_on_front: 0,
24+
page_for_posts: 0,
25+
} ),
26+
] );
27+
} );
28+
test.afterAll( async ( { requestUtils } ) => {
29+
await requestUtils.activateTheme( 'twentytwentyone' );
30+
} );
31+
test( 'Site editor proper front page template resolution when we have only set posts page in settings', async ( {
32+
page,
33+
admin,
34+
requestUtils,
35+
} ) => {
36+
const newPage = await requestUtils.createPage( {
37+
title: 'Posts Page',
38+
status: 'publish',
39+
} );
40+
await updateSiteSettings( { requestUtils, pageId: newPage.id } );
41+
await admin.visitSiteEditor();
42+
await expect( page.locator( '.edit-site-canvas-loader' ) ).toHaveCount(
43+
0
44+
);
45+
} );
46+
test.describe( '`page_for_posts` setting', () => {
47+
test( 'Post editor proper template resolution', async ( {
48+
page,
49+
admin,
50+
editor,
51+
requestUtils,
52+
} ) => {
53+
const newPage = await requestUtils.createPage( {
54+
title: 'Posts Page',
55+
status: 'publish',
56+
} );
57+
await admin.editPost( newPage.id );
58+
await editor.openDocumentSettingsSidebar();
59+
await expect(
60+
page.getByRole( 'button', { name: 'Template options' } )
61+
).toHaveText( 'Single Entries' );
62+
await updateSiteSettings( { requestUtils, pageId: newPage.id } );
63+
await page.reload();
64+
await expect(
65+
page.getByRole( 'button', { name: 'Template options' } )
66+
).toHaveText( 'Index' );
67+
} );
68+
test( 'Site editor proper template resolution', async ( {
69+
page,
70+
editor,
71+
admin,
72+
requestUtils,
73+
} ) => {
74+
const newPage = await requestUtils.createPage( {
75+
title: 'Posts Page',
76+
status: 'publish',
77+
} );
78+
await updateSiteSettings( { requestUtils, pageId: newPage.id } );
79+
await admin.visitSiteEditor( {
80+
postId: newPage.id,
81+
postType: 'page',
82+
canvas: 'edit',
83+
} );
84+
await editor.openDocumentSettingsSidebar();
85+
await expect(
86+
page.getByRole( 'button', { name: 'Template options' } )
87+
).toHaveText( 'Index' );
88+
} );
89+
} );
90+
} );

0 commit comments

Comments
 (0)