diff --git a/lib/compat/wordpress-6.2/site-editor.php b/lib/compat/wordpress-6.2/site-editor.php index b6246e49c6d113..de81680313727d 100644 --- a/lib/compat/wordpress-6.2/site-editor.php +++ b/lib/compat/wordpress-6.2/site-editor.php @@ -6,19 +6,30 @@ */ /** - * Remove home template setting for WP 6.2. + * Remove home template setting and add filtered extraTemplatesTypes setting for WP 6.2. * * @param array $settings Existing block editor settings. * @param WP_Block_Editor_Context $context The current block editor context. * @return array */ -function gutenberg_site_editor_unset_homepage_setting( $settings, $context ) { +function gutenberg_site_editor_update_settings( $settings, $context ) { if ( isset( $context->post ) ) { return $settings; } unset( $settings['__unstableHomeTemplate'] ); + /** + * Filters the list of extra templates. + * + * Template types added in this filter will show up under the `Add new` menu in the Site Editor. + * + * @since 6.2.0 + * + * @param array $extra_template_types An array of template types, formatted as [ slug, title, description ] ]. + */ + $settings['extraTemplateTypes'] = apply_filters( 'extra_template_types', array() ); + return $settings; } -add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_unset_homepage_setting', 10, 2 ); +add_filter( 'block_editor_settings_all', 'gutenberg_site_editor_update_settings', 10, 2 ); diff --git a/packages/edit-site/src/components/add-new-template/new-template.js b/packages/edit-site/src/components/add-new-template/new-template.js index ccd5448e4d81db..a48422943bd04e 100644 --- a/packages/edit-site/src/components/add-new-template/new-template.js +++ b/packages/edit-site/src/components/add-new-template/new-template.js @@ -43,6 +43,7 @@ import { usePostTypeMenuItems, useAuthorMenuItem, usePostTypeArchiveMenuItems, + useExtraTemplateTypes, } from './utils'; import AddCustomGenericTemplateModal from './add-custom-generic-template-modal'; import TemplateActionsLoadingScreen from './template-actions-loading-screen'; @@ -204,6 +205,7 @@ export default function NewTemplate( { slug, onClick, icon, + isWPSuggestion = false, } = template; return ( onClick ? onClick( template ) - : createTemplate( template ) + : createTemplate( + template, + isWPSuggestion + ) } > { title } @@ -280,6 +285,13 @@ function useMissingTemplates( DEFAULT_TEMPLATE_SLUGS.includes( template.slug ) && ! existingTemplateSlugs.includes( template.slug ) ); + const extraTemplateTypes = ( useExtraTemplateTypes() || [] ).map( + ( template ) => { + template.isWPSuggestion = false; + return template; + } + ); + const onClickMenuItem = ( _entityForSuggestions ) => { setShowCustomTemplateModal( true ); setEntityForSuggestions( _entityForSuggestions ); @@ -330,5 +342,15 @@ function useMissingTemplates( ...postTypesMenuItems, ...taxonomiesMenuItems, ]; - return missingTemplates; + + const missingTemplateSlugs = ( missingTemplates || [] ).map( + ( { slug } ) => slug + ); + const missingExtraTemplateTypes = ( extraTemplateTypes || [] ).filter( + ( template ) => + ! missingTemplateSlugs.includes( template.slug ) && + ! existingTemplateSlugs.includes( template.slug ) + ); + + return [ ...missingTemplates, ...missingExtraTemplateTypes ]; } diff --git a/packages/edit-site/src/components/add-new-template/utils.js b/packages/edit-site/src/components/add-new-template/utils.js index 7309f2ff711ca9..7a61fcbfc0da68 100644 --- a/packages/edit-site/src/components/add-new-template/utils.js +++ b/packages/edit-site/src/components/add-new-template/utils.js @@ -59,6 +59,11 @@ export const useDefaultTemplateTypes = () => { [] ); }; +export const useExtraTemplateTypes = () => { + return useSelect( ( select ) => { + return select( editorStore ).__experimentalGetExtraTemplateTypes(); + }, [] ); +}; const usePublicPostTypes = () => { const postTypes = useSelect( diff --git a/packages/edit-site/src/index.js b/packages/edit-site/src/index.js index d2cbb2f4aa04d2..26a6fecc3a95cd 100644 --- a/packages/edit-site/src/index.js +++ b/packages/edit-site/src/index.js @@ -85,6 +85,7 @@ export function reinitializeEditor( target, settings ) { dispatch( editorStore ).updateEditorSettings( { defaultTemplateTypes: settings.defaultTemplateTypes, defaultTemplatePartAreas: settings.defaultTemplatePartAreas, + extraTemplateTypes: settings.extraTemplateTypes, } ); } diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index da5b4700eb9fc8..3ff3cc2a892ca6 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -1549,6 +1549,17 @@ export function __experimentalGetDefaultTemplateTypes( state ) { return getEditorSettings( state )?.defaultTemplateTypes; } +/** + * Returns the extra template slugs. + * + * @param {Object} state Global application state. + * + * @return {Array} The template slugs. + */ +export function __experimentalGetExtraTemplateTypes( state ) { + return getEditorSettings( state )?.extraTemplateTypes; +} + /** * Returns the default template part areas. *