From 56286f94ddea8392ea3e8b768867e6f578b77dc3 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Mon, 11 Sep 2023 15:58:41 +0800 Subject: [PATCH 1/2] Allow accessing the patterns screen when the classic theme supports 'block-template-parts' --- .../core-commands/src/admin-navigation-commands.js | 12 ++++++++++-- packages/core-commands/src/hooks.js | 8 ++++++++ packages/edit-post/src/plugins/index.js | 7 +++++-- .../src/components/patterns-manage-button.js | 7 +++++-- .../reusable-blocks-manage-button.js | 7 +++++-- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index 0ba4df82920d9a..664d7c53def230 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -10,7 +10,11 @@ import { privateApis as routerPrivateApis } from '@wordpress/router'; /** * Internal dependencies */ -import { useIsTemplatesAccessible, useIsBlockBasedTheme } from './hooks'; +import { + useIsTemplatesAccessible, + useIsBlockBasedTheme, + useSupportsBlockTemplateParts, +} from './hooks'; import { unlock } from './lock-unlock'; const { useHistory } = unlock( routerPrivateApis ); @@ -19,6 +23,7 @@ export function useAdminNavigationCommands() { const history = useHistory(); const isTemplatesAccessible = useIsTemplatesAccessible(); const isBlockBasedTheme = useIsBlockBasedTheme(); + const supportsBlockTemplateParts = useSupportsBlockTemplateParts(); const isSiteEditor = getPath( window.location.href )?.includes( 'site-editor.php' @@ -45,7 +50,10 @@ export function useAdminNavigationCommands() { label: __( 'Patterns' ), icon: symbol, callback: ( { close } ) => { - if ( isTemplatesAccessible && isBlockBasedTheme ) { + if ( + isTemplatesAccessible && + ( isBlockBasedTheme || supportsBlockTemplateParts ) + ) { const args = { path: '/patterns', }; diff --git a/packages/core-commands/src/hooks.js b/packages/core-commands/src/hooks.js index 6d744e3223234d..aa76b08db3ea8c 100644 --- a/packages/core-commands/src/hooks.js +++ b/packages/core-commands/src/hooks.js @@ -17,3 +17,11 @@ export function useIsBlockBasedTheme() { [] ); } + +export function useSupportsBlockTemplateParts() { + return useSelect( + ( select ) => + !! select( coreStore ).getThemeSupports()[ 'block-template-parts' ], + [] + ); +} diff --git a/packages/edit-post/src/plugins/index.js b/packages/edit-post/src/plugins/index.js index aa663659acbdc1..3bded4fecd6c34 100644 --- a/packages/edit-post/src/plugins/index.js +++ b/packages/edit-post/src/plugins/index.js @@ -20,10 +20,12 @@ import WelcomeGuideMenuItem from './welcome-guide-menu-item'; function ManagePatternsMenuItem() { const url = useSelect( ( select ) => { - const { canUser } = select( coreStore ); + const { canUser, getThemeSupports } = select( coreStore ); const { getEditorSettings } = select( editorStore ); const isBlockTheme = getEditorSettings().__unstableIsBlockBasedTheme; + const supportsBlockTemplateParts = + !! getThemeSupports()[ 'block-template-parts' ]; const defaultUrl = addQueryArgs( 'edit.php', { post_type: 'wp_block', } ); @@ -34,7 +36,8 @@ function ManagePatternsMenuItem() { // The site editor and templates both check whether the user has // edit_theme_options capabilities. We can leverage that here and not // display the manage patterns link if the user can't access it. - return canUser( 'read', 'templates' ) && isBlockTheme + return canUser( 'read', 'templates' ) && + ( isBlockTheme || supportsBlockTemplateParts ) ? patternsUrl : defaultUrl; }, [] ); diff --git a/packages/patterns/src/components/patterns-manage-button.js b/packages/patterns/src/components/patterns-manage-button.js index bfa36521a28c1f..6281ee1db512b1 100644 --- a/packages/patterns/src/components/patterns-manage-button.js +++ b/packages/patterns/src/components/patterns-manage-button.js @@ -20,9 +20,11 @@ function PatternsManageButton( { clientId } ) { ( select ) => { const { getBlock, canRemoveBlock, getBlockCount, getSettings } = select( blockEditorStore ); - const { canUser } = select( coreStore ); + const { canUser, getThemeSupports } = select( coreStore ); const reusableBlock = getBlock( clientId ); const isBlockTheme = getSettings().__unstableIsBlockBasedTheme; + const supportsBlockTemplateParts = + !! getThemeSupports()[ 'block-template-parts' ]; return { canRemove: canRemoveBlock( clientId ), @@ -39,7 +41,8 @@ function PatternsManageButton( { clientId } ) { // has edit_theme_options capabilities. We can leverage that here // and omit the manage patterns link if the user can't access it. managePatternsUrl: - isBlockTheme && canUser( 'read', 'templates' ) + ( isBlockTheme || supportsBlockTemplateParts ) && + canUser( 'read', 'templates' ) ? addQueryArgs( 'site-editor.php', { path: '/patterns', } ) diff --git a/packages/reusable-blocks/src/components/reusable-blocks-menu-items/reusable-blocks-manage-button.js b/packages/reusable-blocks/src/components/reusable-blocks-menu-items/reusable-blocks-manage-button.js index 6ca19269d40f64..8eb889c7289678 100644 --- a/packages/reusable-blocks/src/components/reusable-blocks-menu-items/reusable-blocks-manage-button.js +++ b/packages/reusable-blocks/src/components/reusable-blocks-menu-items/reusable-blocks-manage-button.js @@ -20,9 +20,11 @@ function ReusableBlocksManageButton( { clientId } ) { ( select ) => { const { getBlock, canRemoveBlock, getBlockCount, getSettings } = select( blockEditorStore ); - const { canUser } = select( coreStore ); + const { canUser, getThemeSupports } = select( coreStore ); const reusableBlock = getBlock( clientId ); const isBlockTheme = getSettings().__unstableIsBlockBasedTheme; + const supportsBlockTemplateParts = + !! getThemeSupports()[ 'block-template-parts' ]; return { canRemove: canRemoveBlock( clientId ), @@ -39,7 +41,8 @@ function ReusableBlocksManageButton( { clientId } ) { // has edit_theme_options capabilities. We can leverage that here // and omit the manage patterns link if the user can't access it. managePatternsUrl: - isBlockTheme && canUser( 'read', 'templates' ) + ( isBlockTheme || supportsBlockTemplateParts ) && + canUser( 'read', 'templates' ) ? addQueryArgs( 'site-editor.php', { path: '/patterns', } ) From c4983562ec91313a41ce495e0fa2d63f93839546 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Tue, 12 Sep 2023 10:17:37 +0800 Subject: [PATCH 2/2] Forbid access to other site eidtor pages --- .../sidebar-navigation-screen-patterns/index.js | 7 +++++++ .../sidebar-navigation-screen-templates-browse/index.js | 9 --------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js index 3b6a6a8110f562..21bdee00af72de 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js @@ -14,6 +14,8 @@ import { getTemplatePartIcon } from '@wordpress/editor'; import { __, sprintf } from '@wordpress/i18n'; import { getQueryArgs } from '@wordpress/url'; import { file, starFilled, lockSmall } from '@wordpress/icons'; +import { store as coreStore } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -105,6 +107,10 @@ export default function SidebarNavigationScreenPatterns() { useTemplatePartAreas(); const { patternCategories, hasPatterns } = usePatternCategories(); const { myPatterns } = useMyPatterns(); + const isBlockBasedTheme = useSelect( + ( select ) => !! select( coreStore ).getCurrentTheme()?.is_block_theme, + [] + ); const templatePartsLink = useLink( { path: '/wp_template_part/all' } ); const footer = ! isMobileViewport ? ( @@ -124,6 +130,7 @@ export default function SidebarNavigationScreenPatterns() { return ( { - const settings = select( editSiteStore ).getSettings(); - - return !! settings.supportsTemplatePartsMode; - }, [] ); - return (