From 29f21696b456ac41e4107da2da2bdc7c5444fc85 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Jul 2023 12:16:39 +1200 Subject: [PATCH 1/8] update the Manage all my patterns command to redirect to site editor patterns list --- packages/core-commands/package.json | 1 + .../src/admin-navigation-commands.js | 40 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/core-commands/package.json b/packages/core-commands/package.json index 808a0a32e8c52e..c6cc52454b4b41 100644 --- a/packages/core-commands/package.json +++ b/packages/core-commands/package.json @@ -27,6 +27,7 @@ "sideEffects": false, "dependencies": { "@babel/runtime": "^7.16.0", + "@wordpress/block-editor": "file:../block-editor", "@wordpress/commands": "file:../commands", "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index a72b0c7dd5ab9b..5ec5c6739089b6 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -4,8 +4,26 @@ import { useCommand } from '@wordpress/commands'; import { __ } from '@wordpress/i18n'; import { external, plus } from '@wordpress/icons'; +import { useSelect } from '@wordpress/data'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +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 getSettings = useSelect( + ( select ) => select( blockEditorStore ).getSettings + ); + const settings = getSettings(); + const isBlockTheme = settings.__unstableIsBlockBasedTheme; + useCommand( { name: 'core/add-new-post', label: __( 'Add new post' ), @@ -25,8 +43,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'; + callback: ( { close } ) => { + const isSiteEditor = getPath( window.location.href )?.includes( + 'site-editor.php' + ); + // __unstableIsBlockBasedTheme is not defined in the site editor so we need to check the context. + // If not the site editor and not a block based theme then redirect to the old wp-admin patterns page. + if ( ! isSiteEditor && ! isBlockTheme ) { + 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, } ); From e4a799f2644742f34459c21118e83707ea3a8a35 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Jul 2023 12:51:36 +1200 Subject: [PATCH 2/8] Fix icon when in site editor --- .../core-commands/src/admin-navigation-commands.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 5ec5c6739089b6..7e6939d612567a 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -3,7 +3,7 @@ */ 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 blockEditorStore } from '@wordpress/block-editor'; import { addQueryArgs, getPath } from '@wordpress/url'; @@ -23,6 +23,9 @@ export function useAdminNavigationCommands() { ); const settings = getSettings(); const isBlockTheme = settings.__unstableIsBlockBasedTheme; + const isSiteEditor = getPath( window.location.href )?.includes( + 'site-editor.php' + ); useCommand( { name: 'core/add-new-post', @@ -44,9 +47,6 @@ export function useAdminNavigationCommands() { name: 'core/manage-reusable-blocks', label: __( 'Manage all of my patterns' ), callback: ( { close } ) => { - const isSiteEditor = getPath( window.location.href )?.includes( - 'site-editor.php' - ); // __unstableIsBlockBasedTheme is not defined in the site editor so we need to check the context. // If not the site editor and not a block based theme then redirect to the old wp-admin patterns page. if ( ! isSiteEditor && ! isBlockTheme ) { @@ -64,6 +64,6 @@ export function useAdminNavigationCommands() { close(); } }, - icon: external, + icon: isSiteEditor ? symbol : external, } ); } From fce46ab6e2754fdcf831d25b26b72372f7ea684c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Fri, 21 Jul 2023 13:06:13 +1200 Subject: [PATCH 3/8] Commit package.json --- package-lock.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package-lock.json b/package-lock.json index 4326788c959a0f..bc1ffc5bb712b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17498,6 +17498,7 @@ "version": "file:packages/core-commands", "requires": { "@babel/runtime": "^7.16.0", + "@wordpress/block-editor": "file:packages/block-editor", "@wordpress/commands": "file:packages/commands", "@wordpress/core-data": "file:packages/core-data", "@wordpress/data": "file:packages/data", From 33c6cebf8f2a8e8a0cd0725f8d1ff9e5140fe25c Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 24 Jul 2023 10:29:52 +1200 Subject: [PATCH 4/8] check user permissions to access site editor --- .../src/admin-navigation-commands.js | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 7e6939d612567a..825a0ab0194c31 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -5,6 +5,7 @@ import { useCommand } from '@wordpress/commands'; import { __ } from '@wordpress/i18n'; import { external, plus, symbol } from '@wordpress/icons'; import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; import { store as blockEditorStore } from '@wordpress/block-editor'; import { addQueryArgs, getPath } from '@wordpress/url'; import { privateApis as routerPrivateApis } from '@wordpress/router'; @@ -18,11 +19,19 @@ const { useHistory } = unlock( routerPrivateApis ); export function useAdminNavigationCommands() { const history = useHistory(); - const getSettings = useSelect( - ( select ) => select( blockEditorStore ).getSettings - ); - const settings = getSettings(); - const isBlockTheme = settings.__unstableIsBlockBasedTheme; + + const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => { + return { + isBlockTheme: + select( blockEditorStore ).getSettings() + .__unstableIsBlockBasedTheme, + canAccessSiteEditor: select( coreStore ).canUser( + 'read', + 'templates' + ), + }; + }, [] ); + const isSiteEditor = getPath( window.location.href )?.includes( 'site-editor.php' ); @@ -47,9 +56,10 @@ export function useAdminNavigationCommands() { name: 'core/manage-reusable-blocks', label: __( 'Manage all of my patterns' ), callback: ( { close } ) => { - // __unstableIsBlockBasedTheme is not defined in the site editor so we need to check the context. - // If not the site editor and not a block based theme then redirect to the old wp-admin patterns page. - if ( ! isSiteEditor && ! isBlockTheme ) { + if ( + ( ! isSiteEditor && ! isBlockTheme ) || + ! canAccessSiteEditor + ) { document.location.href = 'edit.php?post_type=wp_block'; } else { const args = { From e2aed45e63937706f6c697b029a64929bdc73a56 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 24 Jul 2023 14:52:32 +1200 Subject: [PATCH 5/8] remove block-editor dependency --- package-lock.json | 1 - packages/core-commands/package.json | 1 - packages/core-commands/src/admin-navigation-commands.js | 5 +++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index bc1ffc5bb712b8..4326788c959a0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17498,7 +17498,6 @@ "version": "file:packages/core-commands", "requires": { "@babel/runtime": "^7.16.0", - "@wordpress/block-editor": "file:packages/block-editor", "@wordpress/commands": "file:packages/commands", "@wordpress/core-data": "file:packages/core-data", "@wordpress/data": "file:packages/data", diff --git a/packages/core-commands/package.json b/packages/core-commands/package.json index c6cc52454b4b41..808a0a32e8c52e 100644 --- a/packages/core-commands/package.json +++ b/packages/core-commands/package.json @@ -27,7 +27,6 @@ "sideEffects": false, "dependencies": { "@babel/runtime": "^7.16.0", - "@wordpress/block-editor": "file:../block-editor", "@wordpress/commands": "file:../commands", "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 825a0ab0194c31..6474c1b4f4af74 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -6,7 +6,6 @@ import { __ } from '@wordpress/i18n'; import { external, plus, symbol } from '@wordpress/icons'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; -import { store as blockEditorStore } from '@wordpress/block-editor'; import { addQueryArgs, getPath } from '@wordpress/url'; import { privateApis as routerPrivateApis } from '@wordpress/router'; @@ -23,7 +22,9 @@ export function useAdminNavigationCommands() { const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => { return { isBlockTheme: - select( blockEditorStore ).getSettings() + // Avoid making core-commands dependent on block-editor at this point. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + select( 'core/block-editor' )?.getSettings() .__unstableIsBlockBasedTheme, canAccessSiteEditor: select( coreStore ).canUser( 'read', From cb7472ea65da5fe0c56cd6eb809a2bd7f43e68ee Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 24 Jul 2023 14:55:01 +1200 Subject: [PATCH 6/8] tidy up comment --- packages/core-commands/src/admin-navigation-commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 6474c1b4f4af74..adbf15f7ba322a 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -22,7 +22,7 @@ export function useAdminNavigationCommands() { const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => { return { isBlockTheme: - // Avoid making core-commands dependent on block-editor at this point. + // 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, From e30d3909e5852fb3f3f0150e61e8ce959842ff4a Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 25 Jul 2023 13:16:14 +1200 Subject: [PATCH 7/8] remove the duplicate site editor Open patterns command --- .../src/site-editor-navigation-commands.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/packages/core-commands/src/site-editor-navigation-commands.js b/packages/core-commands/src/site-editor-navigation-commands.js index 3d551619d63b04..fe562b6e441396 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 ] ); From 5ffc6573ba8b6d1519b4275dc9d2dc8d32b62970 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 25 Jul 2023 13:25:59 +1200 Subject: [PATCH 8/8] renamed to Open patterns --- packages/core-commands/src/admin-navigation-commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index adbf15f7ba322a..2a189c1c7404dd 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -55,7 +55,7 @@ export function useAdminNavigationCommands() { } ); useCommand( { name: 'core/manage-reusable-blocks', - label: __( 'Manage all of my patterns' ), + label: __( 'Open patterns' ), callback: ( { close } ) => { if ( ( ! isSiteEditor && ! isBlockTheme ) ||