Skip to content

Commit

Permalink
Add: Author nicename template creation ability. Take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jul 19, 2022
1 parent 110c23d commit 7568a5f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 19 deletions.
43 changes: 24 additions & 19 deletions packages/edit-site/src/components/add-new-template/new-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
useDefaultTemplateTypes,
useTaxonomiesMenuItems,
usePostTypeMenuItems,
useAuthorMenuItem,
} from './utils';
import AddCustomGenericTemplateModal from './add-custom-generic-template-modal';
import { useHistory } from '../routes';
Expand Down Expand Up @@ -243,26 +244,30 @@ function useMissingTemplates(
useTaxonomiesMenuItems( onClickMenuItem );
const { defaultPostTypesMenuItems, postTypesMenuItems } =
usePostTypeMenuItems( onClickMenuItem );
[ ...defaultTaxonomiesMenuItems, ...defaultPostTypesMenuItems ].forEach(
( menuItem ) => {
if ( ! menuItem ) {
return;
}
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(
( template ) => template.slug === menuItem.slug
);
// Some default template types might have been filtered above from
// `missingDefaultTemplates` because they only check for the general
// template. So here we either replace or append the item, augmented
// with the check if it has available specific item to create a
// template for.
if ( matchIndex > -1 ) {
enhancedMissingDefaultTemplateTypes[ matchIndex ] = menuItem;
} else {
enhancedMissingDefaultTemplateTypes.push( menuItem );
}

const authorMenuItem = useAuthorMenuItem( onClickMenuItem );
[
...defaultTaxonomiesMenuItems,
...defaultPostTypesMenuItems,
authorMenuItem,
].forEach( ( menuItem ) => {
if ( ! menuItem ) {
return;
}
);
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(
( template ) => template.slug === menuItem.slug
);
// Some default template types might have been filtered above from
// `missingDefaultTemplates` because they only check for the general
// template. So here we either replace or append the item, augmented
// with the check if it has available specific item to create a
// template for.
if ( matchIndex > -1 ) {
enhancedMissingDefaultTemplateTypes[ matchIndex ] = menuItem;
} else {
enhancedMissingDefaultTemplateTypes.push( menuItem );
}
} );
// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
enhancedMissingDefaultTemplateTypes?.sort( ( template1, template2 ) => {
return (
Expand Down
56 changes: 56 additions & 0 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,62 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
return taxonomiesMenuItems;
};

const AUTHOR_TEMPLATE_PREFIX = { user: 'author' };
export function useAuthorMenuItem( onClickMenuItem ) {
const existingTemplates = useExistingTemplates();
const defaultTemplateTypes = useDefaultTemplateTypes();
const authorInfo = useEntitiesInfo( 'root', AUTHOR_TEMPLATE_PREFIX );
const authorMenuItem = defaultTemplateTypes?.find(
( { slug } ) => slug === 'author'
);
if ( authorMenuItem && authorInfo.user?.hasEntities ) {
authorMenuItem.onClick = ( template ) => {
onClickMenuItem( {
type: 'root',
slug: 'user',
config: {
queryArgs: ( { search } ) => {
return {
_fields: 'id,name,slug,link',
orderBy: search ? 'name' : 'count',
exclude: authorInfo.user.existingEntitiesIds,
who: 'authors',
};
},
getSpecificTemplate: ( suggestion ) => {
const title = sprintf(
// translators: %s: Represents the name of an author e.g: "Jorge".
__( 'Author: %s' ),
suggestion.name
);
const description = sprintf(
// translators: %s: Represents the name of an author e.g: "Jorge".
__( 'Template for Author: %s' ),
suggestion.name
);
return {
title,
description,
slug: `author-${ suggestion.slug }`,
};
},
},
labels: {
singular_name: __( 'Author' ),
search_items: __( 'Search Authors' ),
not_found: __( 'No authors found.' ),
all_items: __( 'All Authors' ),
},
hasGeneralTemplate: !! existingTemplates.find(
( { slug } ) => slug === 'author'
),
template,
} );
};
}
return authorMenuItem;
}

/**
* Helper hook that filters all the existing templates by the given
* object with the entity's slug as key and the template prefix as value.
Expand Down

0 comments on commit 7568a5f

Please sign in to comment.