From f6fe6ccbee2c0e2d3e3de0d1864889824531cc65 Mon Sep 17 00:00:00 2001 From: Brian Coords Date: Mon, 6 May 2024 15:18:46 -0700 Subject: [PATCH 1/2] moves pattern command to site editor main navigation --- .../src/admin-navigation-commands.js | 43 +------------------ .../src/site-editor-navigation-commands.js | 19 ++++++++ 2 files changed, 20 insertions(+), 42 deletions(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 4de403761f4cc..0ffa7ba7eb628 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -3,26 +3,9 @@ */ import { useCommand } from '@wordpress/commands'; import { __ } from '@wordpress/i18n'; -import { plus, symbol } from '@wordpress/icons'; -import { addQueryArgs, getPath } from '@wordpress/url'; -import { privateApis as routerPrivateApis } from '@wordpress/router'; - -/** - * Internal dependencies - */ -import { useIsTemplatesAccessible } from './hooks'; -import { unlock } from './lock-unlock'; - -const { useHistory } = unlock( routerPrivateApis ); +import { plus } from '@wordpress/icons'; export function useAdminNavigationCommands() { - const history = useHistory(); - const isTemplatesAccessible = useIsTemplatesAccessible(); - - const isSiteEditor = getPath( window.location.href )?.includes( - 'site-editor.php' - ); - useCommand( { name: 'core/add-new-post', label: __( 'Add new post' ), @@ -39,28 +22,4 @@ export function useAdminNavigationCommands() { document.location.href = 'post-new.php?post_type=page'; }, } ); - useCommand( { - name: 'core/manage-reusable-blocks', - label: __( 'Patterns' ), - icon: symbol, - callback: ( { close } ) => { - // The site editor and templates both check whether the user - // can read templates. We can leverage that here and this - // command links to the classic dashboard manage patterns page - // if the user can't access it. - if ( isTemplatesAccessible ) { - const args = { - path: '/patterns', - }; - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = addQueryArgs( 'site-editor.php', args ); - } - close(); - } else { - document.location.href = 'edit.php?post_type=wp_block'; - } - }, - } ); } diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index 695ad00e56720..4ec2e5837f761 100644 --- a/packages/core-commands/src/site-editor-navigation-commands.js +++ b/packages/core-commands/src/site-editor-navigation-commands.js @@ -10,6 +10,7 @@ import { post, page, layout, + symbol, symbolFilled, styles, navigation, @@ -338,6 +339,24 @@ function useSiteEditorBasicNavigationCommands() { }, } ); + result.push( { + name: 'core/manage-reusable-blocks', + label: __( '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, isTemplatesAccessible, isBlockBasedTheme ] ); From 90c26a9d5d56e748daa2d940f1cbd3801d456282 Mon Sep 17 00:00:00 2001 From: Brian Coords Date: Mon, 6 May 2024 20:44:30 -0700 Subject: [PATCH 2/2] updates patterns command to properly display and link for non-block themes and non-admin-level users --- packages/core-commands/src/hooks.js | 7 + .../src/site-editor-navigation-commands.js | 172 +++++++++--------- 2 files changed, 97 insertions(+), 82 deletions(-) diff --git a/packages/core-commands/src/hooks.js b/packages/core-commands/src/hooks.js index 6d744e3223234..30a433a7d84b3 100644 --- a/packages/core-commands/src/hooks.js +++ b/packages/core-commands/src/hooks.js @@ -11,6 +11,13 @@ export function useIsTemplatesAccessible() { ); } +export function useIsTemplatesEditable() { + return useSelect( + ( select ) => select( coreStore ).canUser( 'create', 'templates' ), + [] + ); +} + export function useIsBlockBasedTheme() { return useSelect( ( select ) => select( coreStore ).getCurrentTheme()?.is_block_theme, diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index 4ec2e5837f761..559dc655ceddc 100644 --- a/packages/core-commands/src/site-editor-navigation-commands.js +++ b/packages/core-commands/src/site-editor-navigation-commands.js @@ -22,7 +22,11 @@ import { useDebounce } from '@wordpress/compose'; /** * Internal dependencies */ -import { useIsTemplatesAccessible, useIsBlockBasedTheme } from './hooks'; +import { + useIsTemplatesAccessible, + useIsTemplatesEditable, + useIsBlockBasedTheme, +} from './hooks'; import { unlock } from './lock-unlock'; import { orderEntityRecordsBySearch } from './utils/order-entity-records-by-search'; @@ -259,101 +263,105 @@ function useSiteEditorBasicNavigationCommands() { 'site-editor.php' ); const isTemplatesAccessible = useIsTemplatesAccessible(); + const isTemplatesEditable = useIsTemplatesEditable(); const isBlockBasedTheme = useIsBlockBasedTheme(); const commands = useMemo( () => { const result = []; - if ( ! isTemplatesAccessible || ! isBlockBasedTheme ) { - return result; - } - - result.push( { - name: 'core/edit-site/open-navigation', - label: __( 'Navigation' ), - icon: navigation, - callback: ( { close } ) => { - const args = { - path: '/navigation', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); + if ( isTemplatesAccessible && isBlockBasedTheme ) { + result.push( { + name: 'core/edit-site/open-navigation', + label: __( 'Navigation' ), + icon: navigation, + callback: ( { close } ) => { + const args = { + path: '/navigation', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + }, + } ); - result.push( { - name: 'core/edit-site/open-styles', - label: __( 'Styles' ), - icon: styles, - callback: ( { close } ) => { - const args = { - path: '/wp_global_styles', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); + result.push( { + name: 'core/edit-site/open-styles', + label: __( 'Styles' ), + icon: styles, + callback: ( { close } ) => { + const args = { + path: '/wp_global_styles', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + }, + } ); - result.push( { - name: 'core/edit-site/open-pages', - label: __( 'Pages' ), - icon: page, - callback: ( { close } ) => { - const args = { - path: '/page', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); + result.push( { + name: 'core/edit-site/open-pages', + label: __( 'Pages' ), + icon: page, + callback: ( { close } ) => { + const args = { + path: '/page', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + }, + } ); - result.push( { - name: 'core/edit-site/open-templates', - label: __( 'Templates' ), - icon: layout, - callback: ( { close } ) => { - const args = { - path: '/wp_template', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); - } else { - document.location = targetUrl; - } - close(); - }, - } ); + result.push( { + name: 'core/edit-site/open-templates', + label: __( 'Templates' ), + icon: layout, + callback: ( { close } ) => { + const args = { + path: '/wp_template', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); + }, + } ); + } result.push( { - name: 'core/manage-reusable-blocks', + name: 'core/edit-site/open-patterns', label: __( 'Patterns' ), icon: symbol, callback: ( { close } ) => { - const args = { - path: '/patterns', - }; - const targetUrl = addQueryArgs( 'site-editor.php', args ); - if ( isSiteEditor ) { - history.push( args ); + if ( isTemplatesEditable ) { + const args = { + path: '/patterns', + }; + const targetUrl = addQueryArgs( 'site-editor.php', args ); + if ( isSiteEditor ) { + history.push( args ); + } else { + document.location = targetUrl; + } + close(); } else { - document.location = targetUrl; + // If a user cannot access the site editor + document.location.href = 'edit.php?post_type=wp_block'; } - close(); }, } );