diff --git a/packages/editor/src/components/commands/index.js b/packages/editor/src/components/commands/index.js index d495dcaaef337..5cfa4ba3f45a9 100644 --- a/packages/editor/src/components/commands/index.js +++ b/packages/editor/src/components/commands/index.js @@ -18,6 +18,7 @@ import { layout, rotateRight, rotateLeft, + category, } from '@wordpress/icons'; import { useCommandLoader } from '@wordpress/commands'; import { store as preferencesStore } from '@wordpress/preferences'; @@ -307,6 +308,55 @@ const getEditorCommandLoader = () => }; }; +const getSiteEditorCategoryCommands = () => + function useSiteEditorCategoryCommands() { + // Fetch the current template + const templateSlug = useSelect( ( select ) => { + const { getCurrentPost } = select( editorStore ); + const currentTemplate = getCurrentPost(); + return currentTemplate?.type === 'wp_template' + ? currentTemplate.slug + : null; + }, [] ); + + const commands = []; + + // Check if we are in the Site Editor and editing a category-related template + const isSiteEditor = getPath( window.location.href )?.includes( + 'site-editor.php' + ); + if ( isSiteEditor && templateSlug ) { + const isCategoryTemplate = + templateSlug.includes( 'category' ) || + templateSlug.includes( 'tag' ) || + templateSlug.includes( 'archive' ); + + if ( isCategoryTemplate ) { + const taxonomy = templateSlug.includes( 'tag' ) + ? 'post_tag' + : 'category'; + const label = + taxonomy === 'post_tag' + ? __( 'Manage Tags' ) + : __( 'Manage Categories' ); + + commands.push( { + name: 'core/manage-categories', + label, + icon: category, + callback: ( { close } ) => { + close(); + // Redirect to the categories management page + window.location.href = `${ window.location.origin }/wp-admin/edit-tags.php?taxonomy=${ taxonomy }`; + }, + isDefault: true, // Ensure it shows up by default + } ); + } + } + + return { isLoading: false, commands }; + }; + const getEditedEntityContextualCommands = () => function useEditedEntityContextualCommands() { const { postType } = useSelect( ( select ) => { @@ -473,6 +523,12 @@ export default function useCommands() { hook: getEditorCommandLoader(), } ); + useCommandLoader( { + name: 'core/editor/category-commands', + hook: getSiteEditorCategoryCommands(), + context: 'entity-edit', + } ); + useCommandLoader( { name: 'core/editor/contextual-commands', hook: getEditedEntityContextualCommands(),