Skip to content

Commit 6683aff

Browse files
t-hamanostokesmanMamadukacarolinan
authored
Site Editor: Prevent access to the Design/Styles screen from classic themes without StyleBook support (#69377)
* Router: Add context parameter to useMatch * Change `context` prop to `matchResolverArgs` * Site Editor: Prevent access to the Design/Styles screen from classic themes without StyleBook support * Update warning message Co-authored-by: Mitchell Austin <mr.fye@oneandthesame.net> * Remove redundant logic * Update comment * Avoid redundant selectors * SiteHubMobile: Fix back path --------- Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: stokesman <presstoke@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org>
1 parent a3c7d38 commit 6683aff

File tree

8 files changed

+164
-52
lines changed

8 files changed

+164
-52
lines changed

packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export default function SidebarNavigationScreenPatterns( { backPath } ) {
120120
description={ __(
121121
'Manage what patterns are available when editing the site.'
122122
) }
123+
isRoot={ ! backPath }
123124
backPath={ backPath }
124125
content={
125126
<>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
import { Notice, __experimentalSpacer as Spacer } from '@wordpress/components';
6+
7+
export default function SidebarNavigationScreenUnsupported() {
8+
return (
9+
<Spacer padding={ 3 }>
10+
<Notice status="warning" isDismissible={ false }>
11+
{ __(
12+
'The theme you are currently using does not support this screen.'
13+
) }
14+
</Notice>
15+
</Spacer>
16+
);
17+
}

packages/edit-site/src/components/site-editor-routes/home.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22
* Internal dependencies
33
*/
44
import SidebarNavigationScreenMain from '../sidebar-navigation-screen-main';
5+
import SidebarNavigationScreenUnsupported from '../sidebar-navigation-screen-unsupported';
56
import Editor from '../editor';
7+
import { isClassicThemeWithStyleBookSupport } from './utils';
68

79
export const homeRoute = {
810
name: 'home',
911
path: '/',
1012
areas: {
11-
sidebar: <SidebarNavigationScreenMain />,
12-
preview: <Editor isHomeRoute />,
13-
mobile: <SidebarNavigationScreenMain />,
13+
sidebar( { siteData } ) {
14+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
15+
return isBlockTheme ||
16+
isClassicThemeWithStyleBookSupport( siteData ) ? (
17+
<SidebarNavigationScreenMain />
18+
) : (
19+
<SidebarNavigationScreenUnsupported />
20+
);
21+
},
22+
preview( { siteData } ) {
23+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
24+
return isBlockTheme ||
25+
isClassicThemeWithStyleBookSupport( siteData ) ? (
26+
<Editor isHomeRoute />
27+
) : undefined;
28+
},
29+
mobile( { siteData } ) {
30+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
31+
return isBlockTheme ||
32+
isClassicThemeWithStyleBookSupport( siteData ) ? (
33+
<SidebarNavigationScreenMain />
34+
) : (
35+
<SidebarNavigationScreenUnsupported />
36+
);
37+
},
1438
},
1539
};

packages/edit-site/src/components/site-editor-routes/pattern-item.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
*/
44
import Editor from '../editor';
55
import SidebarNavigationScreenPatterns from '../sidebar-navigation-screen-patterns';
6+
import { isClassicThemeWithStyleBookSupport } from './utils';
67

78
export const patternItemRoute = {
89
name: 'pattern-item',
910
path: '/wp_block/:postId',
1011
areas: {
11-
sidebar: <SidebarNavigationScreenPatterns backPath="/" />,
12+
sidebar( { siteData } ) {
13+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
14+
const backPath =
15+
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
16+
? '/'
17+
: undefined;
18+
return <SidebarNavigationScreenPatterns backPath={ backPath } />;
19+
},
1220
mobile: <Editor />,
1321
preview: <Editor />,
1422
},
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
/**
2-
* WordPress dependencies
3-
*/
4-
import { privateApis as routerPrivateApis } from '@wordpress/router';
5-
61
/**
72
* Internal dependencies
83
*/
94
import SidebarNavigationScreenPatterns from '../sidebar-navigation-screen-patterns';
105
import PagePatterns from '../page-patterns';
11-
import { unlock } from '../../lock-unlock';
12-
13-
const { useLocation } = unlock( routerPrivateApis );
14-
15-
function MobilePatternsView() {
16-
const { query = {} } = useLocation();
17-
const { categoryId } = query;
18-
19-
return !! categoryId ? (
20-
<PagePatterns />
21-
) : (
22-
<SidebarNavigationScreenPatterns backPath="/" />
23-
);
24-
}
6+
import { isClassicThemeWithStyleBookSupport } from './utils';
257

268
export const patternsRoute = {
279
name: 'patterns',
2810
path: '/pattern',
2911
areas: {
30-
sidebar: <SidebarNavigationScreenPatterns backPath="/" />,
12+
sidebar( { siteData } ) {
13+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
14+
const backPath =
15+
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
16+
? '/'
17+
: undefined;
18+
return <SidebarNavigationScreenPatterns backPath={ backPath } />;
19+
},
3120
content: <PagePatterns />,
32-
mobile: <MobilePatternsView />,
21+
mobile( { siteData, query } ) {
22+
const { categoryId } = query;
23+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
24+
const backPath =
25+
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
26+
? '/'
27+
: undefined;
28+
return !! categoryId ? (
29+
<PagePatterns />
30+
) : (
31+
<SidebarNavigationScreenPatterns backPath={ backPath } />
32+
);
33+
},
3334
},
3435
};

packages/edit-site/src/components/site-editor-routes/stylebook.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,36 @@ import { __ } from '@wordpress/i18n';
77
* Internal dependencies
88
*/
99
import SidebarNavigationScreen from '../sidebar-navigation-screen';
10+
import SidebarNavigationScreenUnsupported from '../sidebar-navigation-screen-unsupported';
1011
import { StyleBookPreview } from '../style-book';
12+
import { isClassicThemeWithStyleBookSupport } from './utils';
1113

1214
export const stylebookRoute = {
1315
name: 'stylebook',
1416
path: '/stylebook',
1517
areas: {
16-
sidebar: (
17-
<SidebarNavigationScreen
18-
title={ __( 'Styles' ) }
19-
backPath="/"
20-
description={ __(
21-
`Preview your website's visual identity: colors, typography, and blocks.`
22-
) }
23-
/>
24-
),
25-
preview: <StyleBookPreview isStatic />,
26-
mobile: <StyleBookPreview isStatic />,
18+
sidebar( { siteData } ) {
19+
return isClassicThemeWithStyleBookSupport( siteData ) ? (
20+
<SidebarNavigationScreen
21+
title={ __( 'Styles' ) }
22+
backPath="/"
23+
description={ __(
24+
`Preview your website's visual identity: colors, typography, and blocks.`
25+
) }
26+
/>
27+
) : (
28+
<SidebarNavigationScreenUnsupported />
29+
);
30+
},
31+
preview( { siteData } ) {
32+
return isClassicThemeWithStyleBookSupport( siteData ) ? (
33+
<StyleBookPreview isStatic />
34+
) : undefined;
35+
},
36+
mobile( { siteData } ) {
37+
return isClassicThemeWithStyleBookSupport( siteData ) ? (
38+
<StyleBookPreview isStatic />
39+
) : undefined;
40+
},
2741
},
2842
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Check if the classic theme supports the stylebook.
3+
*
4+
* @param {Object} siteData - The site data provided by the site editor route area resolvers.
5+
* @return {boolean} True if the stylebook is supported, false otherwise.
6+
*/
7+
export function isClassicThemeWithStyleBookSupport( siteData ) {
8+
const isBlockTheme = siteData.currentTheme?.is_block_theme;
9+
const supportsEditorStyles =
10+
siteData.currentTheme?.theme_supports[ 'editor-styles' ];
11+
// This is a temp solution until the has_theme_json value is available for the current theme.
12+
const hasThemeJson = siteData.editorSettings?.supportsLayout;
13+
return ! isBlockTheme && ( supportsEditorStyles || hasThemeJson );
14+
}

packages/edit-site/src/components/site-hub/index.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,65 @@ export const SiteHubMobile = memo(
121121
const history = useHistory();
122122
const { navigate } = useContext( SidebarNavigationContext );
123123

124-
const { dashboardLink, homeUrl, siteTitle } = useSelect( ( select ) => {
124+
const {
125+
dashboardLink,
126+
homeUrl,
127+
siteTitle,
128+
isBlockTheme,
129+
isClassicThemeWithStyleBookSupport,
130+
} = useSelect( ( select ) => {
125131
const { getSettings } = unlock( select( editSiteStore ) );
126-
const { getEntityRecord } = select( coreStore );
132+
const { getEntityRecord, getCurrentTheme } = select( coreStore );
127133
const _site = getEntityRecord( 'root', 'site' );
134+
const currentTheme = getCurrentTheme();
135+
const settings = getSettings();
136+
const supportsEditorStyles =
137+
currentTheme.theme_supports[ 'editor-styles' ];
138+
// This is a temp solution until the has_theme_json value is available for the current theme.
139+
const hasThemeJson = settings.supportsLayout;
140+
128141
return {
129-
dashboardLink: getSettings().__experimentalDashboardLink,
142+
dashboardLink: settings.__experimentalDashboardLink,
130143
homeUrl: getEntityRecord( 'root', '__unstableBase' )?.home,
131144
siteTitle:
132145
! _site?.title && !! _site?.url
133146
? filterURLForDisplay( _site?.url )
134147
: _site?.title,
148+
isBlockTheme: currentTheme?.is_block_theme,
149+
isClassicThemeWithStyleBookSupport:
150+
! currentTheme?.is_block_theme &&
151+
( supportsEditorStyles || hasThemeJson ),
135152
};
136153
}, [] );
137154
const { open: openCommandCenter } = useDispatch( commandsStore );
138-
const isRoot = path === '/';
155+
156+
let backPath;
157+
158+
// If the current path is not the root page, find a page to back to.
159+
if ( path !== '/' ) {
160+
if ( isBlockTheme || isClassicThemeWithStyleBookSupport ) {
161+
// If the current theme is a block theme or a classic theme that supports StyleBook,
162+
// back to the Design screen.
163+
backPath = '/';
164+
} else if ( path !== '/pattern' ) {
165+
// If the current theme is a classic theme that does not support StyleBook,
166+
// back to the Patterns page.
167+
backPath = '/pattern';
168+
}
169+
}
170+
171+
const backButtonProps = {
172+
href: !! backPath ? undefined : dashboardLink,
173+
label: !! backPath
174+
? __( 'Go to Site Editor' )
175+
: __( 'Go to the Dashboard' ),
176+
onClick: !! backPath
177+
? () => {
178+
history.navigate( backPath );
179+
navigate( 'back' );
180+
}
181+
: undefined,
182+
};
139183

140184
return (
141185
<div className="edit-site-site-hub">
@@ -156,18 +200,7 @@ export const SiteHubMobile = memo(
156200
transform: 'scale(0.5)',
157201
borderRadius: 4,
158202
} }
159-
{ ...( isRoot
160-
? {
161-
href: dashboardLink,
162-
label: __( 'Go to the Dashboard' ),
163-
}
164-
: {
165-
onClick: () => {
166-
history.navigate( '/' );
167-
navigate( 'back' );
168-
},
169-
label: __( 'Go to Site Editor' ),
170-
} ) }
203+
{ ...backButtonProps }
171204
>
172205
<SiteIcon className="edit-site-layout__view-mode-toggle-icon" />
173206
</Button>

0 commit comments

Comments
 (0)