Skip to content

Commit

Permalink
Add: Author nice name template creation ability
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jul 12, 2022
1 parent 0b93618 commit e90d25c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ function useSearchSuggestions( entityForSuggestions, search ) {
orderBy: config.getOrderBy( { search } ),
exclude: postsToExclude,
per_page: search ? 20 : 10,
...( config.additionalQueryParameters
? config.additionalQueryParameters( { search } )
: {} ),
} ),
[ search, config, postsToExclude ]
);
Expand Down
52 changes: 32 additions & 20 deletions packages/edit-site/src/components/add-new-template/new-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
useTaxonomyCategory,
useTaxonomyTag,
useExtraTemplates,
useAuthor,
} from './utils';
import AddCustomGenericTemplateModal from './add-custom-generic-template-modal';
import { useHistory } from '../routes';
Expand Down Expand Up @@ -230,6 +231,7 @@ function useMissingTemplates(
const taxonomies = useTaxonomies();
const categoryTaxonomy = useTaxonomyCategory();
const tagTaxonomy = useTaxonomyTag();
const author = useAuthor();

const existingTemplates = useExistingTemplates();
const defaultTemplateTypes = useDefaultTemplateTypes();
Expand Down Expand Up @@ -263,33 +265,43 @@ function useMissingTemplates(
entitiesConfig.page,
onClickMenuItem
);
const authorMenuItem = useExtraTemplates(
author,
entitiesConfig.author,
onClickMenuItem
);

// We need to replace existing default template types with
// the create specific template functionality. The original
// info (title, description, etc.) is preserved in the
// `useExtraTemplates` hook.
const enhancedMissingDefaultTemplateTypes = [ ...missingDefaultTemplates ];
[ categoryMenuItem, tagMenuItem, pageMenuItem ].forEach( ( menuItem ) => {
if ( ! menuItem?.length ) {
return;
}
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(
( template ) => template.slug === menuItem[ 0 ].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.splice(
matchIndex,
1,
menuItem[ 0 ]
[ categoryMenuItem, tagMenuItem, pageMenuItem, authorMenuItem ].forEach(
( menuItem ) => {
if ( ! menuItem?.length ) {
return;
}
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(
( template ) =>
template.slug === menuItem[ 0 ].templateSlug ||
template.slug === menuItem[ 0 ].slug
);
} else {
enhancedMissingDefaultTemplateTypes.push( menuItem[ 0 ] );
// 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.splice(
matchIndex,
1,
menuItem[ 0 ]
);
} else {
enhancedMissingDefaultTemplateTypes.push( menuItem[ 0 ] );
}
}
} );
);
// Update the sort order to match the DEFAULT_TEMPLATE_SLUGS order.
enhancedMissingDefaultTemplateTypes?.sort( ( template1, template2 ) => {
return (
Expand Down
34 changes: 33 additions & 1 deletion packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { store as editorStore } from '@wordpress/editor';
import { decodeEntities } from '@wordpress/html-entities';
import { useMemo } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { blockMeta, post } from '@wordpress/icons';
import { blockMeta, post, postAuthor } from '@wordpress/icons';

/**
* @typedef IHasNameAndId
Expand Down Expand Up @@ -102,6 +102,26 @@ const postTypeBaseConfig = {
),
};
export const entitiesConfig = {
author: {
entityName: 'root',
slug: 'user',
templateSlug: 'author',
getOrderBy: ( { search } ) => ( search ? 'relevance' : 'name' ),
// `icon` is the `menu_icon` property of a post type. We
// only handle `dashicons` for now, even if the `menu_icon`
// also supports urls and svg as values.
getIcon: () => postAuthor,
getTitle: () => __( 'Author' ),
getDescription: () =>
__( 'Displays latest posts written by a single author.' ),
labels: {
singular_name: __( 'Author' ),
search_items: __( 'Search Authors' ),
not_found: __( 'No authors found.' ),
all_items: __( 'All Authors' ),
},
additionalQueryParameters: () => ( { who: 'authors' } ),
},
postType: {
...postTypeBaseConfig,
templatePrefix: 'single-',
Expand Down Expand Up @@ -302,6 +322,18 @@ const useEntitiesInfo = (
return entitiesInfo;
};

export function useAuthor() {
if ( ! useAuthor.data ) {
useAuthor.data = [
{
slug: 'user',
labels: entitiesConfig.author.labels,
},
];
}
return useAuthor.data;
}

export const useExtraTemplates = (
entities,
entityConfig,
Expand Down

0 comments on commit e90d25c

Please sign in to comment.