Skip to content
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

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,36 +562,56 @@ export function areMetaBoxesInitialized( state ) {
*/
export const getEditedPostTemplate = createRegistrySelector(
( select ) => () => {
const currentTemplate =
select( editorStore ).getEditedPostAttribute( 'template' );
const {
id: postId,
type: postType,
slug,
template: currentTemplate,
} = select( editorStore ).getCurrentPost();
const { getSite, getEditedEntityRecord, getEntityRecords } =
select( coreStore );
const siteSettings = getSite();
// First check if the current page is set as the posts page.
const isPostsPage = +postId === siteSettings?.page_for_posts;
if ( isPostsPage ) {
Copy link
Contributor Author

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..

Copy link
Member

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?

Copy link
Contributor Author

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.

const defaultTemplateId = select( coreStore ).getDefaultTemplateId(
{
slug: 'front-page',
}
);
return getEditedEntityRecord(
'postType',
'wp_template',
defaultTemplateId
);
}
if ( currentTemplate ) {
const templateWithSameSlug = select( coreStore )
.getEntityRecords( 'postType', 'wp_template', { per_page: -1 } )
?.find( ( template ) => template.slug === currentTemplate );
const templateWithSameSlug = getEntityRecords(
'postType',
'wp_template',
{ per_page: -1 }
)?.find( ( template ) => template.slug === currentTemplate );
if ( ! templateWithSameSlug ) {
return templateWithSameSlug;
}
return select( coreStore ).getEditedEntityRecord(
return getEditedEntityRecord(
'postType',
'wp_template',
templateWithSameSlug.id
);
}

const post = select( editorStore ).getCurrentPost();
let slugToCheck;
// In `draft` status we might not have a slug available, so we use the `single`
// post type templates slug(ex page, single-post, single-product etc..).
// Pages do not need the `single` prefix in the slug to be prioritized
// through template hierarchy.
if ( post.slug ) {
if ( slug ) {
slugToCheck =
post.type === 'page'
? `${ post.type }-${ post.slug }`
: `single-${ post.type }-${ post.slug }`;
postType === 'page'
? `${ postType }-${ slug }`
: `single-${ postType }-${ slug }`;
} else {
slugToCheck =
post.type === 'page' ? 'page' : `single-${ post.type }`;
slugToCheck = postType === 'page' ? 'page' : `single-${ postType }`;
}
const defaultTemplateId = select( coreStore ).getDefaultTemplateId( {
slug: slugToCheck,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for the infinite loading in site editor.

? siteData.page_on_front.toString()
: null;
const _postsPageId =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also check page_for_posts for zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter for page_for_posts because we don't use the value in a logical comparison(if block).

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
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -177,6 +192,7 @@ function useResolveEditedEntityAndContext( { path, postId, postType } ) {
},
[
homepageId,
postsPageId,
hasLoadedAllDependencies,
url,
postId,
Expand Down
Loading