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

Update 'Add template' screen to prefer template_name label instead of singular_name #60367

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
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
116 changes: 77 additions & 39 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ const usePublicTaxonomies = () => {
}, [ taxonomies ] );
};

function usePostTypeNeedsUniqueIdentifier( publicPostTypes ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
export function usePostTypeArchiveMenuItems() {
const publicPostTypes = usePublicPostTypes();
const postTypesWithArchives = useMemo(
() => publicPostTypes?.filter( ( postType ) => postType.has_archive ),
[ publicPostTypes ]
);
const existingTemplates = useExistingTemplates();
// We need to keep track of naming conflicts. If a conflict
// occurs, we need to add slug.
const postTypeLabels = useMemo(
() =>
publicPostTypes?.reduce( ( accumulator, { labels } ) => {
Expand All @@ -111,25 +119,13 @@ function usePostTypeNeedsUniqueIdentifier( publicPostTypes ) {
}, {} ),
[ publicPostTypes ]
);
return useCallback(
const needsUniqueIdentifier = useCallback(
( { labels, slug } ) => {
const singularName = labels.singular_name.toLowerCase();
return postTypeLabels[ singularName ] > 1 && singularName !== slug;
},
[ postTypeLabels ]
);
}

export function usePostTypeArchiveMenuItems() {
const publicPostTypes = usePublicPostTypes();
const postTypesWithArchives = useMemo(
() => publicPostTypes?.filter( ( postType ) => postType.has_archive ),
[ publicPostTypes ]
);
const existingTemplates = useExistingTemplates();
const needsUniqueIdentifier = usePostTypeNeedsUniqueIdentifier(
postTypesWithArchives
);
return useMemo(
() =>
postTypesWithArchives
Expand Down Expand Up @@ -184,8 +180,30 @@ export const usePostTypeMenuItems = ( onClickMenuItem ) => {
const publicPostTypes = usePublicPostTypes();
const existingTemplates = useExistingTemplates();
const defaultTemplateTypes = useDefaultTemplateTypes();
const needsUniqueIdentifier =
usePostTypeNeedsUniqueIdentifier( publicPostTypes );
// We need to keep track of naming conflicts. If a conflict
// occurs, we need to add slug.
const templateLabels = useMemo(
() =>
publicPostTypes?.reduce( ( accumulator, { labels } ) => {
const templateName = (
labels.template_name || labels.singular_name
).toLowerCase();
accumulator[ templateName ] =
( accumulator[ templateName ] || 0 ) + 1;
return accumulator;
}, {} ),
[ publicPostTypes ]
);
const needsUniqueIdentifier = useCallback(
( { labels, slug } ) => {
const templateName = (
labels.template_name || labels.singular_name
).toLowerCase();
return templateLabels[ templateName ] > 1 && templateName !== slug;
},
[ templateLabels ]
);

// `page`is a special case in template hierarchy.
const templatePrefixes = useMemo(
() =>
Expand Down Expand Up @@ -216,18 +234,27 @@ export const usePostTypeMenuItems = ( onClickMenuItem ) => {
const hasGeneralTemplate =
existingTemplateSlugs?.includes( generalTemplateSlug );
const _needsUniqueIdentifier = needsUniqueIdentifier( postType );
let menuItemTitle = sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Single item: %s' ),
labels.singular_name
);
if ( _needsUniqueIdentifier ) {
menuItemTitle = sprintf(
// translators: %1s: Name of the post type e.g: "Post"; %2s: Slug of the post type e.g: "book".
__( 'Single item: %1$s (%2$s)' ),
labels.singular_name,
slug
let menuItemTitle =
labels.template_name ||
sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Single item: %s' ),
labels.singular_name
);
if ( _needsUniqueIdentifier ) {
menuItemTitle = labels.template_name
? sprintf(
// translators: %1s: Name of the template e.g: "Single Item: Post"; %2s: Slug of the post type e.g: "book".
__( '%1$s (%2$s)' ),
labels.template_name,
slug
)
: sprintf(
// translators: %1s: Name of the post type e.g: "Post"; %2s: Slug of the post type e.g: "book".
__( 'Single item: %1$s (%2$s)' ),
labels.singular_name,
slug
);
}
const menuItem = defaultTemplateType
? {
Expand Down Expand Up @@ -337,9 +364,11 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
// occurs, we need to add slug.
const taxonomyLabels = publicTaxonomies?.reduce(
( accumulator, { labels } ) => {
const singularName = labels.singular_name.toLowerCase();
accumulator[ singularName ] =
( accumulator[ singularName ] || 0 ) + 1;
const templateName = (
labels.template_name || labels.singular_name
).toLowerCase();
accumulator[ templateName ] =
( accumulator[ templateName ] || 0 ) + 1;
return accumulator;
},
{}
Expand All @@ -348,8 +377,10 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
if ( [ 'category', 'post_tag' ].includes( slug ) ) {
return false;
}
const singularName = labels.singular_name.toLowerCase();
return taxonomyLabels[ singularName ] > 1 && singularName !== slug;
const templateName = (
labels.template_name || labels.singular_name
).toLowerCase();
return taxonomyLabels[ templateName ] > 1 && templateName !== slug;
};
const taxonomiesInfo = useEntitiesInfo( 'taxonomy', templatePrefixes );
const existingTemplateSlugs = ( existingTemplates || [] ).map(
Expand All @@ -371,14 +402,21 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
labels,
slug
);
let menuItemTitle = labels.singular_name;
let menuItemTitle = labels.template_name || labels.singular_name;
if ( _needsUniqueIdentifier ) {
menuItemTitle = sprintf(
// translators: %1s: Name of the taxonomy e.g: "Category"; %2s: Slug of the taxonomy e.g: "product_cat".
__( '%1$s (%2$s)' ),
labels.singular_name,
slug
);
menuItemTitle = labels.template_name
? sprintf(
// translators: %1s: Name of the template e.g: "Products by Category"; %2s: Slug of the taxonomy e.g: "product_cat".
__( '%1$s (%2$s)' ),
labels.template_name,
slug
)
: sprintf(
// translators: %1s: Name of the taxonomy e.g: "Category"; %2s: Slug of the taxonomy e.g: "product_cat".
__( '%1$s (%2$s)' ),
labels.singular_name,
slug
);
}
const menuItem = defaultTemplateType
? {
Expand Down
Loading