From 35300bb6c21899af6775caba4b0a2eae8d2ff0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 30 May 2024 14:11:04 +0200 Subject: [PATCH] Add support for any registered post type --- .../src/components/page-templates/index.js | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/edit-site/src/components/page-templates/index.js b/packages/edit-site/src/components/page-templates/index.js index 132b76e9984058..25d278d6815138 100644 --- a/packages/edit-site/src/components/page-templates/index.js +++ b/packages/edit-site/src/components/page-templates/index.js @@ -185,12 +185,6 @@ function Preview( { item, viewType } ) { ); } -// TODO: how about CPTs added by plugins, etc.? -const POST_TYPES = { - post: __( 'Post' ), - page: __( 'Page' ), -}; - // This maps the template slug to the post types it should be available for. // https://developer.wordpress.org/themes/basics/template-hierarchy/#visual-overview // It only addresses primary and secondary templates, but not tertiary (aka variable) templates. @@ -252,6 +246,23 @@ export default function PageTemplates() { per_page: -1, } ); + const { records: types } = useEntityRecords( 'root', 'postType', { + per_page: -1, + context: 'edit', + } ); + + const registeredPostTypes = useMemo( () => { + const result = + types + ?.filter( ( type ) => type.viewable ) + .map( ( { name, slug } ) => ( { name, slug } ) ) + .reduce( ( acc, current ) => { + acc[ current.slug ] = current.name; + return acc; + }, {} ) || {}; + return result; + }, [ types ] ); + const history = useHistory(); const onSelectionChange = useCallback( ( items ) => { @@ -294,7 +305,7 @@ export default function PageTemplates() { return ( item.post_types || - ( item.is_custom && Object.keys( POST_TYPES ) ) || + ( item.is_custom && Object.keys( registeredPostTypes ) ) || TEMPLATE_TO_POST_TYPE[ item.slug ] || [] ); @@ -370,20 +381,22 @@ export default function PageTemplates() { } if ( - postTypes.length === Object.keys( POST_TYPES ).length + postTypes.length === + Object.keys( registeredPostTypes ).length ) { return __( 'Any' ); } return postTypes .map( - ( postType ) => POST_TYPES[ postType ] || postType + ( postType ) => + registeredPostTypes[ postType ] || postType ) .join( ',' ); }, - elements: Object.keys( POST_TYPES ).map( ( key ) => ( { + elements: Object.keys( registeredPostTypes ).map( ( key ) => ( { value: key, - label: POST_TYPES[ key ], + label: registeredPostTypes[ key ], } ) ), }, { @@ -401,7 +414,7 @@ export default function PageTemplates() { }, }, ], - [ authors, view.type ] + [ authors, view.type, registeredPostTypes, getPostTypesFromItem ] ); const { data, paginationInfo } = useMemo( () => {