Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site Editor: Prevent access to the Design/Styles screen from classic themes without StyleBook support #69377

Open
wants to merge 3 commits into
base: router/add-context
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { isClassicThemeWithStyleBookSupport } 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 ||
isClassicThemeWithStyleBookSupport( siteData ) ? (
<SidebarNavigationScreenMain />
) : (
<SidebarNavigationScreenUnsupported />
);
},
preview( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
return isBlockTheme ||
isClassicThemeWithStyleBookSupport( siteData ) ? (
<Editor isHomeRoute />
) : undefined;
},
mobile( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
return isBlockTheme ||
isClassicThemeWithStyleBookSupport( siteData ) ? (
<SidebarNavigationScreenMain />
) : (
<SidebarNavigationScreenUnsupported />
);
},
},
};
59 changes: 38 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,51 @@
/**
* 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 { isClassicThemeWithStyleBookSupport } from './utils';

export const patternsRoute = {
name: 'patterns',
path: '/pattern',
areas: {
sidebar: <SidebarNavigationScreenPatterns backPath="/" />,
sidebar( { siteData } ) {
const isBlockTheme = siteData.currentTheme?.is_block_theme;
const backPath =
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
? '/'
: undefined;
const isRoot = ! (
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
);

return (
<SidebarNavigationScreenPatterns
backPath={ backPath }
isRoot={ isRoot }
/>
);
},
content: <PagePatterns />,
mobile: <MobilePatternsView />,
mobile( { siteData, query } ) {
const { categoryId } = query;
const isBlockTheme = siteData.currentTheme?.is_block_theme;
const backPath =
isBlockTheme || isClassicThemeWithStyleBookSupport( siteData )
? '/'
: undefined;
const isRoot = ! (
isBlockTheme || isClassicThemeWithStyleBookSupport( 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 { isClassicThemeWithStyleBookSupport } 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 isClassicThemeWithStyleBookSupport( siteData ) ? (
<SidebarNavigationScreen
title={ __( 'Styles' ) }
backPath="/"
description={ __(
`Preview your website's visual identity: colors, typography, and blocks.`
) }
/>
) : (
<SidebarNavigationScreenUnsupported />
);
},
preview( { siteData } ) {
return isClassicThemeWithStyleBookSupport( siteData ) ? (
<StyleBookPreview isStatic />
) : undefined;
},
mobile( { siteData } ) {
return isClassicThemeWithStyleBookSupport( 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 isClassicThemeWithStyleBookSupport( 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,
isClassicThemeWithStyleBookSupport,
} = 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,
isClassicThemeWithStyleBookSupport:
! isBlockTheme && ( supportsEditorStyles || hasThemeJson ),
};
}, [] );
const { open: openCommandCenter } = useDispatch( commandsStore );
const isRoot = path === '/';
const isRoot = path === '/' || ! isClassicThemeWithStyleBookSupport;

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
Loading