Skip to content

Commit

Permalink
Site Editor: Prevent access to the Design/Styles screen from classic …
Browse files Browse the repository at this point in the history
…themes without StyleBook support
  • Loading branch information
t-hamano committed Mar 2, 2025
1 parent 00766d0 commit a2305d3
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ function CategoriesGroup( {
);
}

export default function SidebarNavigationScreenPatterns( { backPath } ) {
export default function SidebarNavigationScreenPatterns( {
isRoot,
backPath,
} ) {
const {
query: { postType = 'wp_block', categoryId },
} = useLocation();
Expand All @@ -120,6 +123,7 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) {
description={ __(
'Manage what patterns are available when editing the site.'
) }
isRoot={ isRoot }
backPath={ backPath }
content={
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Notice, __experimentalSpacer as Spacer } from '@wordpress/components';

export default function SidebarNavigationScreenUnsupported() {
return (
<Spacer padding={ 3 }>
<Notice status="warning" isDismissible={ false }>
{ __(
'The theme you are currently using does not support current screen.'
) }
</Notice>
</Spacer>
);
}
30 changes: 27 additions & 3 deletions packages/edit-site/src/components/site-editor-routes/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,38 @@
* Internal dependencies
*/
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main';
import SidebarNavigationScreenUnsupported from '../sidebar-navigation-screen-unsupported';
import Editor from '../editor';
import { isClassicThemeStyleBookSupported } from './utils';

export const homeRoute = {
name: 'home',
path: '/',
areas: {
sidebar: <SidebarNavigationScreenMain />,
preview: <Editor isHomeRoute />,
mobile: <SidebarNavigationScreenMain />,
sidebar( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
return isBlockTheme ||
isClassicThemeStyleBookSupported( siteData ) ? (
<SidebarNavigationScreenMain />
) : (
<SidebarNavigationScreenUnsupported />
);
},
preview( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
return isBlockTheme ||
isClassicThemeStyleBookSupported( siteData ) ? (
<Editor isHomeRoute />
) : undefined;
},
mobile( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
return isBlockTheme ||
isClassicThemeStyleBookSupported( siteData ) ? (
<SidebarNavigationScreenMain />
) : (
<SidebarNavigationScreenUnsupported />
);
},
},
};
51 changes: 30 additions & 21 deletions packages/edit-site/src/components/site-editor-routes/patterns.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
/**
* WordPress dependencies
*/
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import SidebarNavigationScreenPatterns from '../sidebar-navigation-screen-patterns';
import PagePatterns from '../page-patterns';
import { unlock } from '../../lock-unlock';

const { useLocation } = unlock( routerPrivateApis );

function MobilePatternsView() {
const { query = {} } = useLocation();
const { categoryId } = query;

return !! categoryId ? (
<PagePatterns />
) : (
<SidebarNavigationScreenPatterns backPath="/" />
);
}
import { isClassicThemeStyleBookSupported } from './utils';

export const patternsRoute = {
name: 'patterns',
path: '/pattern',
areas: {
sidebar: <SidebarNavigationScreenPatterns backPath="/" />,
sidebar( { siteData } ) {
const backPath = isClassicThemeStyleBookSupported( siteData )
? undefined
: '/';
const isRoot = isClassicThemeStyleBookSupported( siteData );

return (
<SidebarNavigationScreenPatterns
backPath={ backPath }
isRoot={ isRoot }
/>
);
},
content: <PagePatterns />,
mobile: <MobilePatternsView />,
mobile( { siteData, query } ) {
const { categoryId } = query;
const backPath = isClassicThemeStyleBookSupported( siteData )
? undefined
: '/';
const isRoot = isClassicThemeStyleBookSupported( siteData );

return !! categoryId ? (
<PagePatterns />
) : (
<SidebarNavigationScreenPatterns
backPath={ backPath }
isRoot={ isRoot }
/>
);
},
},
};
36 changes: 25 additions & 11 deletions packages/edit-site/src/components/site-editor-routes/stylebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,36 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import SidebarNavigationScreenUnsupported from '../sidebar-navigation-screen-unsupported';
import { StyleBookPreview } from '../style-book';
import { isClassicThemeStyleBookSupported } from './utils';

export const stylebookRoute = {
name: 'stylebook',
path: '/stylebook',
areas: {
sidebar: (
<SidebarNavigationScreen
title={ __( 'Styles' ) }
backPath="/"
description={ __(
`Preview your website's visual identity: colors, typography, and blocks.`
) }
/>
),
preview: <StyleBookPreview isStatic />,
mobile: <StyleBookPreview isStatic />,
sidebar( { siteData } ) {
return isClassicThemeStyleBookSupported( siteData ) ? (
<SidebarNavigationScreen
title={ __( 'Styles' ) }
backPath="/"
description={ __(
`Preview your website's visual identity: colors, typography, and blocks.`
) }
/>
) : (
<SidebarNavigationScreenUnsupported />
);
},
preview( { siteData } ) {
return isClassicThemeStyleBookSupported( siteData ) ? (
<StyleBookPreview isStatic />
) : undefined;
},
mobile( { siteData } ) {
return isClassicThemeStyleBookSupported( siteData ) ? (
<StyleBookPreview isStatic />
) : undefined;
},
},
};
14 changes: 14 additions & 0 deletions packages/edit-site/src/components/site-editor-routes/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Check if the classic theme supports the stylebook.
*
* @param {Object} siteData - The site data provided by the site editor route area resolvers.
* @return {boolean} True if the stylebook is supported, false otherwise.
*/
export function isClassicThemeStyleBookSupported( siteData ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
const supportsEditorStyles =
siteData.currentTheme?.theme_supports[ 'editor-styles' ];
// supportsLayout is equivalent to the `wp_theme_has_theme_json()` PHP function.
const hasThemeJson = siteData.editorSettings?.supportsLayout;
return ! isBlockTheme && ( supportsEditorStyles || hasThemeJson );
}
45 changes: 30 additions & 15 deletions packages/edit-site/src/components/site-hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,47 @@ export const SiteHubMobile = memo(
const history = useHistory();
const { navigate } = useContext( SidebarNavigationContext );

const { dashboardLink, homeUrl, siteTitle } = useSelect( ( select ) => {
const {
dashboardLink,
homeUrl,
siteTitle,
isClassicThemeStyleBookSupported,
} = useSelect( ( select ) => {
const { getSettings } = unlock( select( editSiteStore ) );
const { getEntityRecord } = select( coreStore );
const { getEntityRecord, getCurrentTheme } = select( coreStore );
const _site = getEntityRecord( 'root', 'site' );
const isBlockTheme = getCurrentTheme().currentTheme?.is_block_theme;
const supportsEditorStyles =
getCurrentTheme().theme_supports[ 'editor-styles' ];
// supportsLayout is equivalent to the `wp_theme_has_theme_json()` PHP function.
const hasThemeJson = getSettings().supportsLayout;

return {
dashboardLink: getSettings().__experimentalDashboardLink,
homeUrl: getEntityRecord( 'root', '__unstableBase' )?.home,
siteTitle:
! _site?.title && !! _site?.url
? filterURLForDisplay( _site?.url )
: _site?.title,
isClassicThemeStyleBookSupported:
! isBlockTheme && ( supportsEditorStyles || hasThemeJson ),
};
}, [] );
const { open: openCommandCenter } = useDispatch( commandsStore );
const isRoot = path === '/';
const isRoot = path === '/' || ! isClassicThemeStyleBookSupported;

const backButtonProps = {
href: isRoot ? dashboardLink : undefined,
label: isRoot
? __( 'Go to the Dashboard' )
: __( 'Go to Site Editor' ),
onClick: isRoot
? undefined
: () => {
history.navigate( '/' );
navigate( 'back' );
},
};

return (
<div className="edit-site-site-hub">
Expand All @@ -156,18 +182,7 @@ export const SiteHubMobile = memo(
transform: 'scale(0.5)',
borderRadius: 4,
} }
{ ...( isRoot
? {
href: dashboardLink,
label: __( 'Go to the Dashboard' ),
}
: {
onClick: () => {
history.navigate( '/' );
navigate( 'back' );
},
label: __( 'Go to Site Editor' ),
} ) }
{ ...backButtonProps }
>
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
</Button>
Expand Down

0 comments on commit a2305d3

Please sign in to comment.