Skip to content

Commit

Permalink
Site Editor: Prevent unintended actions on the classic theme (#54422)
Browse files Browse the repository at this point in the history
* Add action and selector to track access to Patterns page

* Add a URL parameter to check if the Patterns page was accessed directly

* Revert lib changes

* Fix critical error in the Post Editor

* Explicitly add `! isBlockBasedTheme` with `isTemplatePartsMode`

* Fix critical error in the Post Editor
  • Loading branch information
t-hamano committed Sep 27, 2023
1 parent a3f4f02 commit c1fba86
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
15 changes: 14 additions & 1 deletion packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useIsTemplatesAccessible, useIsBlockBasedTheme } from './hooks';
import { unlock } from './lock-unlock';
import { orderEntityRecordsBySearch } from './utils/order-entity-records-by-search';

const { useHistory } = unlock( routerPrivateApis );
const { useHistory, useLocation } = unlock( routerPrivateApis );

const icons = {
post,
Expand Down Expand Up @@ -136,6 +136,14 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
function useNavigationCommandLoader( { search } ) {
const history = useHistory();
const location = useLocation();

const isPatternsPage =
location?.params?.path === '/patterns' ||
location?.params?.postType === 'wp_block';
const didAccessPatternsPage =
!! location?.params?.didAccessPatternsPage;

const isBlockBasedTheme = useIsBlockBasedTheme();
const { records, isLoading } = useSelect( ( select ) => {
const { getEntityRecords } = select( coreStore );
Expand Down Expand Up @@ -184,6 +192,11 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
const args = {
postType: templateType,
postId: record.id,
didAccessPatternsPage:
! isBlockBasedTheme &&
( isPatternsPage || didAccessPatternsPage )
? 1
: undefined,
...extraArgs,
};
const targetUrl = addQueryArgs(
Expand Down
11 changes: 4 additions & 7 deletions packages/edit-site/src/components/add-new-pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
privateApis as editPatternsPrivateApis,
store as patternsStore,
} from '@wordpress/patterns';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';

/**
Expand All @@ -19,7 +20,6 @@ import { store as noticesStore } from '@wordpress/notices';
import CreateTemplatePartModal from '../create-template-part-modal';
import SidebarButton from '../sidebar-button';
import { unlock } from '../../lock-unlock';
import { store as editSiteStore } from '../../store';
import {
PATTERN_TYPES,
PATTERN_DEFAULT_CATEGORY,
Expand All @@ -36,9 +36,8 @@ export default function AddNewPattern() {
const [ showPatternModal, setShowPatternModal ] = useState( false );
const [ showTemplatePartModal, setShowTemplatePartModal ] =
useState( false );
const isTemplatePartsMode = useSelect( ( select ) => {
const settings = select( editSiteStore ).getSettings();
return !! settings.supportsTemplatePartsMode;
const isBlockBasedTheme = useSelect( ( select ) => {
return select( coreStore ).getCurrentTheme()?.is_block_theme;
}, [] );
const { createPatternFromFile } = unlock( useDispatch( patternsStore ) );
const { createSuccessNotice, createErrorNotice } =
Expand Down Expand Up @@ -82,9 +81,7 @@ export default function AddNewPattern() {
},
];

// Remove condition when command palette issues are resolved.
// See: https://github.com/WordPress/gutenberg/issues/52154.
if ( ! isTemplatePartsMode ) {
if ( isBlockBasedTheme ) {
controls.push( {
icon: symbolFilled,
onClick: () => setShowTemplatePartModal( true ),
Expand Down
15 changes: 14 additions & 1 deletion packages/edit-site/src/components/page-template-parts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
Expand All @@ -20,8 +21,15 @@ import AddedBy from '../list/added-by';
import TemplateActions from '../template-actions';
import AddNewTemplatePart from './add-new-template-part';
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';
import { unlock } from '../../lock-unlock';

const { useLocation } = unlock( routerPrivateApis );

export default function PageTemplateParts() {
const {
params: { didAccessPatternsPage },
} = useLocation();

const { records: templateParts } = useEntityRecords(
'postType',
TEMPLATE_PART_POST_TYPE,
Expand All @@ -40,8 +48,13 @@ export default function PageTemplateParts() {
params={ {
postId: templatePart.id,
postType: templatePart.type,
didAccessPatternsPage: !! didAccessPatternsPage
? 1
: undefined,
} }
state={ {
backPath: '/wp_template_part/all',
} }
state={ { backPath: '/wp_template_part/all' } }
>
{ decodeEntities(
templatePart.title?.rendered ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { useViewportMatch } from '@wordpress/compose';
import { getTemplatePartIcon } from '@wordpress/editor';
import { __ } from '@wordpress/i18n';
import { getQueryArgs } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { file } from '@wordpress/icons';

/**
Expand All @@ -27,6 +29,7 @@ import {
import { useLink } from '../routes/link';
import usePatternCategories from './use-pattern-categories';
import useTemplatePartAreas from './use-template-part-areas';
import { store as editSiteStore } from '../../store';

function TemplatePartGroup( { areas, currentArea, currentType } ) {
return (
Expand Down Expand Up @@ -93,8 +96,23 @@ export default function SidebarNavigationScreenPatterns() {
const { templatePartAreas, hasTemplateParts, isLoading } =
useTemplatePartAreas();
const { patternCategories, hasPatterns } = usePatternCategories();
const isBlockBasedTheme = useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.is_block_theme,
[]
);
const isTemplatePartsMode = useSelect( ( select ) => {
const settings = select( editSiteStore ).getSettings();
return !! settings.supportsTemplatePartsMode;
}, [] );

const templatePartsLink = useLink( {
path: '/wp_template_part/all',
// If a classic theme that supports template parts accessed
// the Patterns page directly, preserve that state in the URL.
didAccessPatternsPage:
! isBlockBasedTheme && isTemplatePartsMode ? 1 : undefined,
} );

const templatePartsLink = useLink( { path: '/wp_template_part/all' } );
const footer = ! isMobileViewport ? (
<ItemGroup>
<SidebarNavigationItem
Expand All @@ -104,14 +122,17 @@ export default function SidebarNavigationScreenPatterns() {
>
{ __( 'Manage all of my patterns' ) }
</SidebarNavigationItem>
<SidebarNavigationItem withChevron { ...templatePartsLink }>
{ __( 'Manage all template parts' ) }
</SidebarNavigationItem>
{ ( isBlockBasedTheme || isTemplatePartsMode ) && (
<SidebarNavigationItem withChevron { ...templatePartsLink }>
{ __( 'Manage all template parts' ) }
</SidebarNavigationItem>
) }
</ItemGroup>
) : undefined;

return (
<SidebarNavigationScreen
isRoot={ ! isBlockBasedTheme }
title={ __( 'Patterns' ) }
description={ __(
'Manage what patterns are available when editing the site.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { __experimentalUseNavigator as useNavigator } from '@wordpress/components';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
Expand All @@ -14,6 +15,7 @@ import {
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
} from '../../utils/constants';
import { unlock } from '../../lock-unlock';

const config = {
[ TEMPLATE_POST_TYPE ]: {
Expand All @@ -31,20 +33,26 @@ const config = {
},
};

const { useLocation } = unlock( routerPrivateApis );

export default function SidebarNavigationScreenTemplatesBrowse() {
const {
params: { postType },
} = useNavigator();
const {
params: { didAccessPatternsPage },
} = useLocation();

const isTemplatePartsMode = useSelect( ( select ) => {
const settings = select( editSiteStore ).getSettings();

return !! settings.supportsTemplatePartsMode;
return !! select( editSiteStore ).getSettings()
.supportsTemplatePartsMode;
}, [] );

return (
<SidebarNavigationScreen
isRoot={ isTemplatePartsMode }
// If a classic theme that supports template parts has never
// accessed the Patterns page, return to the dashboard.
isRoot={ isTemplatePartsMode && ! didAccessPatternsPage }
title={ config[ postType ].title }
description={ config[ postType ].description }
backPath={ config[ postType ].backPath }
Expand Down

0 comments on commit c1fba86

Please sign in to comment.