diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index a72b0c7dd5ab9..2a189c1c7404d 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -3,9 +3,40 @@ */ import { useCommand } from '@wordpress/commands'; import { __ } from '@wordpress/i18n'; -import { external, plus } from '@wordpress/icons'; +import { external, plus, symbol } from '@wordpress/icons'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import { addQueryArgs, getPath } from '@wordpress/url'; +import { privateApis as routerPrivateApis } from '@wordpress/router'; + +/** + * Internal dependencies + */ +import { unlock } from './lock-unlock'; + +const { useHistory } = unlock( routerPrivateApis ); export function useAdminNavigationCommands() { + const history = useHistory(); + + const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => { + return { + isBlockTheme: + // To avoid making core-commands dependent on block-editor using store string literal name. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + select( 'core/block-editor' )?.getSettings() + .__unstableIsBlockBasedTheme, + canAccessSiteEditor: select( coreStore ).canUser( + 'read', + 'templates' + ), + }; + }, [] ); + + const isSiteEditor = getPath( window.location.href )?.includes( + 'site-editor.php' + ); + useCommand( { name: 'core/add-new-post', label: __( 'Add new post' ), @@ -24,10 +55,26 @@ export function useAdminNavigationCommands() { } ); useCommand( { name: 'core/manage-reusable-blocks', - label: __( 'Manage all of my patterns' ), - callback: () => { - document.location.href = 'edit.php?post_type=wp_block'; + label: __( 'Open patterns' ), + callback: ( { close } ) => { + if ( + ( ! isSiteEditor && ! isBlockTheme ) || + ! canAccessSiteEditor + ) { + document.location.href = 'edit.php?post_type=wp_block'; + } else { + const args = { + path: '/patterns', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + } }, - icon: external, + icon: isSiteEditor ? symbol : external, } ); } diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index 3d551619d63b0..fe562b6e44139 100644 --- a/packages/core-commands/src/site-editor-navigation-commands.js +++ b/packages/core-commands/src/site-editor-navigation-commands.js @@ -13,7 +13,6 @@ import { symbolFilled, styles, navigation, - symbol, } from '@wordpress/icons'; import { privateApis as routerPrivateApis } from '@wordpress/router'; import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url'; @@ -198,23 +197,6 @@ function useSiteEditorBasicNavigationCommands() { }, } ); - result.push( { - name: 'core/edit-site/open-template-parts', - label: __( 'Open patterns' ), - icon: symbol, - callback: ( { close } ) => { - const args = { - path: '/patterns', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); return result; }, [ history, isSiteEditor ] );