From 6e6910fa909193af847dee497d99100c1eecccdd Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 18 Jan 2021 14:45:04 +0100 Subject: [PATCH 01/13] Remove general templates from posts --- .../navigation-panel/constants.js | 5 ++- .../navigation-panel/menus/templates-posts.js | 43 +++++-------------- 2 files changed, 13 insertions(+), 35 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js index bf88a77aa4a335..b8b89daed6dac2 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js @@ -5,15 +5,16 @@ import { __ } from '@wordpress/i18n'; export const TEMPLATES_GENERAL = [ 'front-page', + 'home', 'archive', 'singular', + 'single', + 'single-post', 'index', 'search', '404', ]; -export const TEMPLATES_POSTS = [ 'home', 'single', 'single-post' ]; - export const TEMPLATES_NEW_OPTIONS = [ 'front-page', 'single-post', diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js index 02225ee5bfb247..30cf49bdb68ec1 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js @@ -6,26 +6,16 @@ import { map } from 'lodash'; /** * WordPress dependencies */ -import { - __experimentalNavigationGroup as NavigationGroup, - __experimentalNavigationMenu as NavigationMenu, -} from '@wordpress/components'; -import { __, _x } from '@wordpress/i18n'; +import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import TemplateNavigationItem from '../template-navigation-item'; -import { - MENU_TEMPLATES, - MENU_TEMPLATES_POSTS, - TEMPLATES_POSTS, -} from '../constants'; +import { MENU_TEMPLATES, MENU_TEMPLATES_POSTS } from '../constants'; export default function TemplatesPostsMenu( { templates } ) { - const generalTemplates = - templates?.filter( ( { slug } ) => TEMPLATES_POSTS.includes( slug ) ) ?? - []; const specificTemplates = templates?.filter( ( { slug } ) => slug.startsWith( 'post-' ) ) ?? []; @@ -34,27 +24,14 @@ export default function TemplatesPostsMenu( { templates } ) { menu={ MENU_TEMPLATES_POSTS } title={ __( 'Posts' ) } parentMenu={ MENU_TEMPLATES } - isEmpty={ - generalTemplates.length === 0 && specificTemplates.length === 0 - } + isEmpty={ specificTemplates.length === 0 } > - - { map( specificTemplates, ( template ) => ( - - ) ) } - - - - { map( generalTemplates, ( template ) => ( - - ) ) } - + { map( specificTemplates, ( template ) => ( + + ) ) } ); } From 09e4d386bb1ae8120e5e64be310ac2ce85e9694e Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 18 Jan 2021 14:45:15 +0100 Subject: [PATCH 02/13] Remove general templates from pages --- .../navigation-panel/menus/templates-pages.js | 30 ++++++------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js index d4816219a02399..363c86fd18575b 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js @@ -6,11 +6,8 @@ import { map } from 'lodash'; /** * WordPress dependencies */ -import { - __experimentalNavigationGroup as NavigationGroup, - __experimentalNavigationMenu as NavigationMenu, -} from '@wordpress/components'; -import { __, _x } from '@wordpress/i18n'; +import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies @@ -19,7 +16,6 @@ import TemplateNavigationItem from '../template-navigation-item'; import { MENU_TEMPLATES, MENU_TEMPLATES_PAGES } from '../constants'; export default function TemplatesPagesMenu( { templates } ) { - const defaultTemplate = templates?.find( ( { slug } ) => slug === 'page' ); const specificTemplates = templates?.filter( ( { slug } ) => slug.startsWith( 'page-' ) ) ?? []; @@ -28,22 +24,14 @@ export default function TemplatesPagesMenu( { templates } ) { menu={ MENU_TEMPLATES_PAGES } title={ __( 'Pages' ) } parentMenu={ MENU_TEMPLATES } - isEmpty={ ! defaultTemplate && specificTemplates.length === 0 } + isEmpty={ specificTemplates.length === 0 } > - - { map( specificTemplates, ( template ) => ( - - ) ) } - - - { defaultTemplate && ( - - - - ) } + { map( specificTemplates, ( template ) => ( + + ) ) } ); } From 2bbd5700626fb47463c49b4578c78747432642a8 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 8 Feb 2021 14:57:23 +0100 Subject: [PATCH 03/13] First try --- .../navigation-panel/constants.js | 32 +++-- .../navigation-panel/menus/templates-all.js | 33 ----- .../navigation-panel/menus/templates-posts.js | 37 ----- .../{templates-pages.js => templates-sub.js} | 23 ++-- .../navigation-panel/menus/templates.js | 128 ++++++++++++++---- .../navigation-panel/template-hierarchy.js | 43 ++++++ 6 files changed, 185 insertions(+), 111 deletions(-) delete mode 100644 packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-all.js delete mode 100644 packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js rename packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/{templates-pages.js => templates-sub.js} (50%) create mode 100644 packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js index b8b89daed6dac2..aecc9f6acb1683 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js @@ -3,16 +3,31 @@ */ import { __ } from '@wordpress/i18n'; -export const TEMPLATES_GENERAL = [ - 'front-page', - 'home', - 'archive', +export const TEMPLATES_PRIMARY = [ + 'index', 'singular', + 'archive', 'single', - 'single-post', - 'index', - 'search', + 'page', + 'home', '404', + 'search', +]; + +export const TEMPLATES_SECONDARY = [ + 'author', + 'category', + 'taxonomy', + 'date', + 'tag', + 'attachment', + 'single-post', + 'front-page', +]; + +export const TEMPLATES_TOP_LEVEL = [ + ...TEMPLATES_PRIMARY, + TEMPLATES_SECONDARY, ]; export const TEMPLATES_NEW_OPTIONS = [ @@ -31,9 +46,10 @@ export const MENU_CONTENT_PAGES = 'content-pages'; export const MENU_CONTENT_POSTS = 'content-posts'; export const MENU_TEMPLATE_PARTS = 'template-parts'; export const MENU_TEMPLATES = 'templates'; -export const MENU_TEMPLATES_ALL = 'templates-all'; +export const MENU_TEMPLATES_GENERAL = 'templates-general'; export const MENU_TEMPLATES_PAGES = 'templates-pages'; export const MENU_TEMPLATES_POSTS = 'templates-posts'; +export const MENU_TEMPLATES_UNUSED = 'templates-unused'; export const SEARCH_DEBOUNCE_IN_MS = 75; diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-all.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-all.js deleted file mode 100644 index 934a35bd0e0f5c..00000000000000 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-all.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * External dependencies - */ -import { map } from 'lodash'; - -/** - * WordPress dependencies - */ -import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import TemplateNavigationItem from '../template-navigation-item'; -import { MENU_TEMPLATES, MENU_TEMPLATES_ALL } from '../constants'; - -export default function TemplatesAllMenu( { templates } ) { - return ( - - { map( templates, ( template ) => ( - - ) ) } - - ); -} diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js deleted file mode 100644 index 30cf49bdb68ec1..00000000000000 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-posts.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * External dependencies - */ -import { map } from 'lodash'; - -/** - * WordPress dependencies - */ -import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import TemplateNavigationItem from '../template-navigation-item'; -import { MENU_TEMPLATES, MENU_TEMPLATES_POSTS } from '../constants'; - -export default function TemplatesPostsMenu( { templates } ) { - const specificTemplates = - templates?.filter( ( { slug } ) => slug.startsWith( 'post-' ) ) ?? []; - - return ( - - { map( specificTemplates, ( template ) => ( - - ) ) } - - ); -} diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-sub.js similarity index 50% rename from packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js rename to packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-sub.js index 363c86fd18575b..9d6322c25a4f81 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-pages.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates-sub.js @@ -7,26 +7,31 @@ import { map } from 'lodash'; * WordPress dependencies */ import { __experimentalNavigationMenu as NavigationMenu } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; +import { useMemo } from '@wordpress/element'; /** * Internal dependencies */ import TemplateNavigationItem from '../template-navigation-item'; -import { MENU_TEMPLATES, MENU_TEMPLATES_PAGES } from '../constants'; +import { MENU_TEMPLATES } from '../constants'; -export default function TemplatesPagesMenu( { templates } ) { - const specificTemplates = - templates?.filter( ( { slug } ) => slug.startsWith( 'page-' ) ) ?? []; +export default function TemplatesSubMenu( { menu, title, templates } ) { + const templatesFiltered = useMemo( + () => + templates + ?.filter( ( { location } ) => location === menu ) + ?.map( ( { template } ) => template ) ?? [], + [ menu, templates ] + ); return ( - { map( specificTemplates, ( template ) => ( + { map( templatesFiltered, ( template ) => ( slug ); + return templates.filter( ( { slug } ) => + isTemplateSuperseded( slug, existingSlugs ) + ); +} + +function getTemplatesLocation( templates ) { + return templates.reduce( ( obj, template ) => { + obj[ template.slug ] = getTemplateLocation( template ); + return obj; + }, {} ); +} export default function TemplatesMenu() { const [ search, setSearch ] = useState( '' ); @@ -43,10 +80,30 @@ export default function TemplatesMenu() { select( 'core' ).getEntityRecords( 'postType', 'wp_template' ), [] ); + const templatesWithLocation = useMemo( () => { + if ( ! templates ) { + return null; + } - const generalTemplates = templates?.filter( ( { slug } ) => - TEMPLATES_GENERAL.includes( slug ) - ); + const unusedTemplates = getUnusedTemplates( templates ); + const templateLocations = getTemplatesLocation( templates ); + + return templates.map( ( template ) => { + return { + template, + location: unusedTemplates.some( + ( t ) => t.slug === template.slug + ) + ? MENU_TEMPLATES_UNUSED + : templateLocations[ template.slug ], + }; + } ); + }, [ templates ] ); + + const topLevelTemplates = + templatesWithLocation + ?.filter( ( { location } ) => location === MENU_TEMPLATES ) + ?.map( ( { template } ) => template ) ?? []; return ( + { map( topLevelTemplates, ( template ) => ( + + ) ) } + - { map( generalTemplates, ( template ) => ( - - ) ) } ) } @@ -91,9 +154,26 @@ export default function TemplatesMenu() { ) } - - - + + + + ); } diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js new file mode 100644 index 00000000000000..5795b1d4ad1e51 --- /dev/null +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js @@ -0,0 +1,43 @@ +/** + * Maps template slugs to their parents. + */ +const TEMPLATE_PARENTS = { + index: null, + archive: 'index', + + singular: 'index', + single: 'singular', + page: 'singular', + + home: 'index', + 404: 'index', + search: 'index', +}; + +/** + * Maps template slugs to their children template slugs. + */ +const TEMPLATE_DEPS = Object.keys( TEMPLATE_PARENTS ).reduce( + ( obj, template ) => { + const parent = TEMPLATE_PARENTS[ template ]; + if ( ! obj[ parent ] ) { + obj[ parent ] = []; + } + obj[ parent ].push( template ); + + return obj; + }, + {} +); + +export function isTemplateSuperseded( slug, existingSlugs ) { + return TEMPLATE_DEPS[ slug ] + ? TEMPLATE_DEPS[ slug ].every( + ( dep ) => + isTemplateSuperseded( dep, existingSlugs ) || + !! existingSlugs.find( + ( existingSlug ) => existingSlug === dep + ) + ) + : ! existingSlugs.find( ( existingSlug ) => existingSlug === slug ); +} From d9e466d1bb665226798b5129204dafd3f4a4baeb Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 10 Feb 2021 12:18:29 +0100 Subject: [PATCH 04/13] Fix secondary templates not showing up on top level --- .../components/navigation-sidebar/navigation-panel/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js index aecc9f6acb1683..27ac193df31a66 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js @@ -27,7 +27,7 @@ export const TEMPLATES_SECONDARY = [ export const TEMPLATES_TOP_LEVEL = [ ...TEMPLATES_PRIMARY, - TEMPLATES_SECONDARY, + ...TEMPLATES_SECONDARY, ]; export const TEMPLATES_NEW_OPTIONS = [ From 046d32bd33f5b522d85ddc145885a5027b74d0c1 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 10 Feb 2021 12:18:46 +0100 Subject: [PATCH 05/13] Add front-page --- .../navigation-sidebar/navigation-panel/template-hierarchy.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js index 5795b1d4ad1e51..1cd33c405200e3 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js @@ -12,6 +12,8 @@ const TEMPLATE_PARENTS = { home: 'index', 404: 'index', search: 'index', + + 'front-page': 'home', }; /** From 6200c6efbb77c2f1fc0970bc2de824de0fcae080 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 10 Feb 2021 12:19:00 +0100 Subject: [PATCH 06/13] Add home to unused templates if appropriate --- .../navigation-panel/menus/templates.js | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 22cffd11b586a1..8a2f9a5c8840a0 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -13,6 +13,7 @@ import { import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { useState, useCallback, useMemo } from '@wordpress/element'; +import { store as coreStore } from '@wordpress/core-data'; /** * Internal dependencies @@ -55,11 +56,27 @@ function getTemplateLocation( template ) { return MENU_TEMPLATES_GENERAL; } -function getUnusedTemplates( templates ) { - const existingSlugs = templates.map( ( { slug } ) => slug ); - return templates.filter( ( { slug } ) => - isTemplateSuperseded( slug, existingSlugs ) +function getUnusedTemplates( templates, showOnFront ) { + const unusedTemplates = []; + const templateSlugs = templates.map( ( { slug } ) => slug ); + + // `home` template is unused if it is superseded by `front-page` + // or show on front is set to show a page rather than blog posts. + const homeTemplateExists = templateSlugs.includes( 'home' ); + if ( homeTemplateExists && showOnFront !== 'posts' ) { + unusedTemplates.push( + templates.find( ( { slug } ) => slug === 'home' ) + ); + } + + const usedTemplateSlugs = templateSlugs.filter( + ( slug ) => ! unusedTemplates.find( ( template ) => slug === template ) + ); + const supersededTemplates = templates.filter( ( { slug } ) => + isTemplateSuperseded( slug, usedTemplateSlugs ) ); + + return [ ...supersededTemplates, ...unusedTemplates ]; } function getTemplatesLocation( templates ) { @@ -75,17 +92,21 @@ export default function TemplatesMenu() { setSearch( value ); } ); - const templates = useSelect( - ( select ) => - select( 'core' ).getEntityRecords( 'postType', 'wp_template' ), - [] - ); + const { templates, showOnFront } = useSelect( ( select ) => { + const { getEntityRecords, getEditedEntityRecord } = select( coreStore ); + const _templates = getEntityRecords( 'postType', 'wp_template', { + per_page: -1, + } ); + const _showOnFront = getEditedEntityRecord( 'root', 'site' ) + .show_on_front; + return { templates: _templates, showOnFront: _showOnFront }; + }, [] ); const templatesWithLocation = useMemo( () => { if ( ! templates ) { return null; } - const unusedTemplates = getUnusedTemplates( templates ); + const unusedTemplates = getUnusedTemplates( templates, showOnFront ); const templateLocations = getTemplatesLocation( templates ); return templates.map( ( template ) => { From 66ea2e1ac7494e9e8c51d81ccd397deb9fd249a9 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 17 Feb 2021 15:36:36 +0100 Subject: [PATCH 07/13] Simplify isTemplateSuperseded --- .../navigation-panel/template-hierarchy.js | 53 +++++-------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js index 1cd33c405200e3..aeec8f1bb7f1f6 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js @@ -1,45 +1,16 @@ -/** - * Maps template slugs to their parents. - */ -const TEMPLATE_PARENTS = { - index: null, - archive: 'index', - - singular: 'index', - single: 'singular', - page: 'singular', - - home: 'index', - 404: 'index', - search: 'index', - - 'front-page': 'home', +const TEMPLATE_OVERRIDES = { + singular: [ 'single', 'page' ], + index: [ 'archive', '404', 'search', 'singular' ], }; -/** - * Maps template slugs to their children template slugs. - */ -const TEMPLATE_DEPS = Object.keys( TEMPLATE_PARENTS ).reduce( - ( obj, template ) => { - const parent = TEMPLATE_PARENTS[ template ]; - if ( ! obj[ parent ] ) { - obj[ parent ] = []; - } - obj[ parent ].push( template ); - - return obj; - }, - {} -); - export function isTemplateSuperseded( slug, existingSlugs ) { - return TEMPLATE_DEPS[ slug ] - ? TEMPLATE_DEPS[ slug ].every( - ( dep ) => - isTemplateSuperseded( dep, existingSlugs ) || - !! existingSlugs.find( - ( existingSlug ) => existingSlug === dep - ) - ) - : ! existingSlugs.find( ( existingSlug ) => existingSlug === slug ); + if ( ! TEMPLATE_OVERRIDES[ slug ] ) { + return false; + } + + return TEMPLATE_OVERRIDES[ slug ].every( + ( overrideSlug ) => + existingSlugs.includes( overrideSlug ) || + isTemplateSuperseded( overrideSlug, existingSlugs ) + ); } From 2779c318101b11301ad9815cbff3e6d200c04eee Mon Sep 17 00:00:00 2001 From: David Szabo Date: Wed, 17 Feb 2021 15:58:10 +0100 Subject: [PATCH 08/13] Refactor --- .../navigation-panel/menus/templates.js | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 8a2f9a5c8840a0..6ef9db8a1fe906 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { map } from 'lodash'; +import { map, find, some } from 'lodash'; /** * WordPress dependencies @@ -58,20 +58,18 @@ function getTemplateLocation( template ) { function getUnusedTemplates( templates, showOnFront ) { const unusedTemplates = []; - const templateSlugs = templates.map( ( { slug } ) => slug ); // `home` template is unused if it is superseded by `front-page` // or show on front is set to show a page rather than blog posts. - const homeTemplateExists = templateSlugs.includes( 'home' ); + const homeTemplateExists = some( templates, { slug: 'home' } ); if ( homeTemplateExists && showOnFront !== 'posts' ) { - unusedTemplates.push( - templates.find( ( { slug } ) => slug === 'home' ) - ); + unusedTemplates.push( find( templates, { slug: 'home' } ) ); } - const usedTemplateSlugs = templateSlugs.filter( - ( slug ) => ! unusedTemplates.find( ( template ) => slug === template ) + const usedTemplates = templates.filter( + ( template ) => ! unusedTemplates.includes( template ) ); + const usedTemplateSlugs = map( usedTemplates, 'slug' ); const supersededTemplates = templates.filter( ( { slug } ) => isTemplateSuperseded( slug, usedTemplateSlugs ) ); @@ -79,7 +77,7 @@ function getUnusedTemplates( templates, showOnFront ) { return [ ...supersededTemplates, ...unusedTemplates ]; } -function getTemplatesLocation( templates ) { +function getTemplatesLocationMap( templates ) { return templates.reduce( ( obj, template ) => { obj[ template.slug ] = getTemplateLocation( template ); return obj; @@ -94,37 +92,37 @@ export default function TemplatesMenu() { const { templates, showOnFront } = useSelect( ( select ) => { const { getEntityRecords, getEditedEntityRecord } = select( coreStore ); - const _templates = getEntityRecords( 'postType', 'wp_template', { - per_page: -1, - } ); - const _showOnFront = getEditedEntityRecord( 'root', 'site' ) - .show_on_front; - return { templates: _templates, showOnFront: _showOnFront }; + return { + templates: getEntityRecords( 'postType', 'wp_template', { + per_page: -1, + } ), + showOnFront: getEditedEntityRecord( 'root', 'site' ).show_on_front, + }; }, [] ); + const templatesWithLocation = useMemo( () => { if ( ! templates ) { return null; } const unusedTemplates = getUnusedTemplates( templates, showOnFront ); - const templateLocations = getTemplatesLocation( templates ); - - return templates.map( ( template ) => { - return { - template, - location: unusedTemplates.some( - ( t ) => t.slug === template.slug - ) - ? MENU_TEMPLATES_UNUSED - : templateLocations[ template.slug ], - }; - } ); + const templateLocations = getTemplatesLocationMap( templates ); + + return templates.map( ( template ) => ( { + template, + location: find( unusedTemplates, { slug: template.slug } ) + ? MENU_TEMPLATES_UNUSED + : templateLocations[ template.slug ], + } ) ); }, [ templates ] ); - const topLevelTemplates = - templatesWithLocation - ?.filter( ( { location } ) => location === MENU_TEMPLATES ) - ?.map( ( { template } ) => template ) ?? []; + const topLevelTemplates = useMemo( + () => + templatesWithLocation + ?.filter( ( { location } ) => location === MENU_TEMPLATES ) + ?.map( ( { template } ) => template ) ?? [], + [ templatesWithLocation ] + ); return ( Date: Wed, 17 Feb 2021 16:28:12 +0100 Subject: [PATCH 09/13] Update e2e test --- .../specs/experiments/multi-entity-editing.test.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js index 2d61fa19729e1b..7e1b127b0be670 100644 --- a/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js +++ b/packages/e2e-tests/specs/experiments/multi-entity-editing.test.js @@ -204,8 +204,11 @@ describe( 'Multi-entity editor states', () => { '.wp-block-template-part .block-editor-block-list__layout' ); - // Our custom template shows up in the " templates > all" menu; let's use it. - await clickTemplateItem( [ 'Templates', 'All' ], templateName ); + // Our custom template shows up in the "Templates > General" menu; let's use it. + await clickTemplateItem( + [ 'Templates', 'General templates' ], + templateName + ); await page.waitForXPath( `//h1[contains(@class, "edit-site-document-actions__title") and contains(text(), '${ templateName }')]` ); From 1b63d8eff3c99c12909ca9f9af4423eb88b550b4 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Tue, 23 Feb 2021 19:18:31 +0100 Subject: [PATCH 10/13] Add dash --- .../navigation-sidebar/navigation-panel/menus/templates.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 6ef9db8a1fe906..91894eb516a3a0 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -44,7 +44,7 @@ function getTemplateLocation( template ) { slug.startsWith( 'post-' ) || slug.startsWith( 'author-' ) || slug.startsWith( 'single-post-' ) || - slug.startsWith( 'tag' ) + slug.startsWith( 'tag-' ) ) { return MENU_TEMPLATES_POSTS; } From b8698647772aff3eac6d4e74904148b7ac06746e Mon Sep 17 00:00:00 2001 From: David Szabo Date: Tue, 23 Feb 2021 19:19:14 +0100 Subject: [PATCH 11/13] Move page-home to general --- .../navigation-sidebar/navigation-panel/menus/templates.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 91894eb516a3a0..36a497e0284066 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -40,6 +40,10 @@ function getTemplateLocation( template ) { return MENU_TEMPLATES; } + if ( slug === 'page-home' ) { + return MENU_TEMPLATES_GENERAL; + } + if ( slug.startsWith( 'post-' ) || slug.startsWith( 'author-' ) || From a850392925c2a3bb3772063fb6bb92688c1d621a Mon Sep 17 00:00:00 2001 From: David Szabo Date: Thu, 4 Mar 2021 08:45:37 +0100 Subject: [PATCH 12/13] Determine whether home and index is superseded correctly --- .../navigation-panel/menus/templates.js | 16 +++------------- .../navigation-panel/template-hierarchy.js | 13 ++++++++++--- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 36a497e0284066..30e3214f133218 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { map, find, some } from 'lodash'; +import { map, find } from 'lodash'; /** * WordPress dependencies @@ -63,19 +63,9 @@ function getTemplateLocation( template ) { function getUnusedTemplates( templates, showOnFront ) { const unusedTemplates = []; - // `home` template is unused if it is superseded by `front-page` - // or show on front is set to show a page rather than blog posts. - const homeTemplateExists = some( templates, { slug: 'home' } ); - if ( homeTemplateExists && showOnFront !== 'posts' ) { - unusedTemplates.push( find( templates, { slug: 'home' } ) ); - } - - const usedTemplates = templates.filter( - ( template ) => ! unusedTemplates.includes( template ) - ); - const usedTemplateSlugs = map( usedTemplates, 'slug' ); + const templateSlugs = map( templates, 'slug' ); const supersededTemplates = templates.filter( ( { slug } ) => - isTemplateSuperseded( slug, usedTemplateSlugs ) + isTemplateSuperseded( slug, templateSlugs, showOnFront ) ); return [ ...supersededTemplates, ...unusedTemplates ]; diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js index aeec8f1bb7f1f6..29dba28b9b5aad 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/template-hierarchy.js @@ -1,16 +1,23 @@ const TEMPLATE_OVERRIDES = { singular: [ 'single', 'page' ], - index: [ 'archive', '404', 'search', 'singular' ], + index: [ 'archive', '404', 'search', 'singular', 'home' ], + home: [ 'front-page' ], }; -export function isTemplateSuperseded( slug, existingSlugs ) { +export function isTemplateSuperseded( slug, existingSlugs, showOnFront ) { if ( ! TEMPLATE_OVERRIDES[ slug ] ) { return false; } + // `home` template is unused if it is superseded by `front-page` + // or "show on front" is set to show a page rather than blog posts. + if ( slug === 'home' && showOnFront !== 'posts' ) { + return true; + } + return TEMPLATE_OVERRIDES[ slug ].every( ( overrideSlug ) => existingSlugs.includes( overrideSlug ) || - isTemplateSuperseded( overrideSlug, existingSlugs ) + isTemplateSuperseded( overrideSlug, existingSlugs, showOnFront ) ); } From e463015df97c0ce55727c228b3a983f256d6547d Mon Sep 17 00:00:00 2001 From: David Szabo Date: Thu, 4 Mar 2021 09:01:41 +0100 Subject: [PATCH 13/13] Use constants --- .../navigation-panel/constants.js | 11 +++++++++ .../navigation-panel/menus/templates.js | 24 ++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js index 27ac193df31a66..ea6c799bed779c 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/constants.js @@ -30,6 +30,17 @@ export const TEMPLATES_TOP_LEVEL = [ ...TEMPLATES_SECONDARY, ]; +export const TEMPLATES_GENERAL = [ 'page-home' ]; + +export const TEMPLATES_POSTS_PREFIXES = [ + 'post-', + 'author-', + 'single-post-', + 'tag-', +]; + +export const TEMPLATES_PAGES_PREFIXES = [ 'page-' ]; + export const TEMPLATES_NEW_OPTIONS = [ 'front-page', 'single-post', diff --git a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js index 30e3214f133218..8cd861e8c6e035 100644 --- a/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js +++ b/packages/edit-site/src/components/navigation-sidebar/navigation-panel/menus/templates.js @@ -25,6 +25,9 @@ import { MENU_TEMPLATES_PAGES, MENU_TEMPLATES_POSTS, MENU_TEMPLATES_UNUSED, + TEMPLATES_GENERAL, + TEMPLATES_PAGES_PREFIXES, + TEMPLATES_POSTS_PREFIXES, TEMPLATES_TOP_LEVEL, } from '../constants'; import NewTemplateDropdown from '../new-template-dropdown'; @@ -36,24 +39,27 @@ import { isTemplateSuperseded } from '../template-hierarchy'; function getTemplateLocation( template ) { const { slug } = template; - if ( TEMPLATES_TOP_LEVEL.includes( slug ) ) { + const isTopLevelTemplate = TEMPLATES_TOP_LEVEL.includes( slug ); + if ( isTopLevelTemplate ) { return MENU_TEMPLATES; } - if ( slug === 'page-home' ) { + const isGeneralTemplate = TEMPLATES_GENERAL.includes( slug ); + if ( isGeneralTemplate ) { return MENU_TEMPLATES_GENERAL; } - if ( - slug.startsWith( 'post-' ) || - slug.startsWith( 'author-' ) || - slug.startsWith( 'single-post-' ) || - slug.startsWith( 'tag-' ) - ) { + const isPostsTemplate = TEMPLATES_POSTS_PREFIXES.some( ( prefix ) => + slug.startsWith( prefix ) + ); + if ( isPostsTemplate ) { return MENU_TEMPLATES_POSTS; } - if ( slug.startsWith( 'page-' ) ) { + const isPagesTemplate = TEMPLATES_PAGES_PREFIXES.some( ( prefix ) => + slug.startsWith( prefix ) + ); + if ( isPagesTemplate ) { return MENU_TEMPLATES_PAGES; }