From 267921d9d0257f3d201773e7127e23be7b00e0f4 Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Thu, 12 Dec 2024 13:24:06 +0530 Subject: [PATCH] Adding command for manage template --- .../src/site-editor-navigation-commands.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index c1b12a84d4d61..02fa6ac2fd5c2 100644 --- a/packages/core-commands/src/site-editor-navigation-commands.js +++ b/packages/core-commands/src/site-editor-navigation-commands.js @@ -406,6 +406,54 @@ const getSiteEditorBasicNavigationCommands = () => isLoading: false, }; }; +const useManageTemplateCommand = () => { + const history = useHistory(); + const isSiteEditor = getPath( window.location.href )?.includes( + 'site-editor.php' + ); + const { isBlockBasedTheme, canCreateTemplate } = useSelect( + ( select ) => ( { + isBlockBasedTheme: + select( coreStore ).getCurrentTheme()?.is_block_theme, + canCreateTemplate: select( coreStore ).canUser( 'create', { + kind: 'postType', + name: 'wp_template', + } ), + } ), + [] + ); + + const commands = useMemo( () => { + if ( ! canCreateTemplate || ! isBlockBasedTheme ) { + return []; + } + + return [ + { + name: 'core/edit-site/open-templates', + label: __( 'Manage Templates' ), + icon: layout, + callback: ( { close } ) => { + const args = { + postType: 'wp_template', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.navigate( '/template' ); + } else { + document.location = targetUrl; + } + close(); + }, + }, + ]; + }, [ canCreateTemplate, isBlockBasedTheme, history, isSiteEditor ] ); + + return { + commands, + isLoading: false, + }; +}; export function useSiteEditorNavigationCommands() { useCommandLoader( { @@ -429,4 +477,10 @@ export function useSiteEditorNavigationCommands() { hook: getSiteEditorBasicNavigationCommands(), context: 'site-editor', } ); + useCommandLoader( { + name: 'core/edit-site/open-templates', + /* eslint-disable-next-line react-compiler/react-compiler */ + hook: useManageTemplateCommand, + context: 'entity-edit', + } ); }