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 5, 2022
1 parent ab33ef6 commit 2b0b236
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
useTaxonomyCategory,
useTaxonomyTag,
useExtraTemplates,
useAuthorTemplate,
} from './utils';
import { useHistory } from '../routes';
import { store as editSiteStore } from '../../store';
Expand Down Expand Up @@ -229,17 +230,21 @@ function useMissingTemplates(
entitiesConfig.tag,
onClickMenuItem
);
const authorMenuItem = useAuthorTemplate( 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 ].forEach( ( menuItem ) => {
[ categoryMenuItem, tagMenuItem, authorMenuItem ].forEach( ( menuItem ) => {
if ( ! menuItem?.length ) {
return;
}
const matchIndex = enhancedMissingDefaultTemplateTypes.findIndex(
( template ) => template.slug === menuItem[ 0 ].slug
( template ) =>
template.slug === menuItem[ 0 ].templateSlug ||
template.slug === menuItem[ 0 ].slug
);
// Some default template types might have been filtered above from
// `missingDefaultTemplates` because they only check for the general
Expand Down
53 changes: 52 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 @@ -80,6 +80,27 @@ const taxonomyBaseConfig = {
),
};
export const entitiesConfig = {
author: {
entityName: 'root',
slug: 'user',
templateSlug: 'author',
recordNamePath: 'name',
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: {
entityName: 'postType',
templatePrefix: 'single-',
Expand Down Expand Up @@ -347,3 +368,33 @@ export const useExtraTemplates = (
);
return extraTemplates;
};

export function useAuthorTemplate( onClickMenuItem ) {
const existingTemplates = useExistingTemplates();
return useMemo( () => {
const authorEntity = entitiesConfig.author;
return [
{
slug: authorEntity.slug,
templateSlug: authorEntity.templateSlug,
title: authorEntity.getTitle(),
icon: authorEntity.getIcon(),
description: authorEntity.getDescription(),
onClick( template ) {
onClickMenuItem( {
...authorEntity,
type: authorEntity.entityName,
config: authorEntity,
hasGeneralTemplate: !! existingTemplates.find(
( { slug } ) => slug === authorEntity.templateSlug
),
template: {
...template,
slug: authorEntity.templateSlug,
},
} );
},
},
];
}, [ existingTemplates, onClickMenuItem ] );
}

0 comments on commit 2b0b236

Please sign in to comment.