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

Fix user capabilities check for the Site Editor #61444

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import { useCommand } from '@wordpress/commands';
import { __ } from '@wordpress/i18n';
import { plus, symbol } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { addQueryArgs, getPath } from '@wordpress/url';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { useIsTemplatesAccessible } from './hooks';
import { unlock } from './lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

export function useAdminNavigationCommands() {
const history = useHistory();
const isTemplatesAccessible = useIsTemplatesAccessible();
const canCreateTemplate = useSelect( ( select ) => {
return select( coreStore ).canUser( 'create', 'templates' );
}, [] );

const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
Expand Down Expand Up @@ -45,10 +48,10 @@ export function useAdminNavigationCommands() {
icon: symbol,
callback: ( { close } ) => {
// The site editor and templates both check whether the user
// can read templates. We can leverage that here and this
// can create templates. We can leverage that here and this
// command links to the classic dashboard manage patterns page
// if the user can't access it.
if ( isTemplatesAccessible ) {
if ( canCreateTemplate ) {
const args = {
path: '/patterns',
};
Expand Down
7 changes: 0 additions & 7 deletions packages/core-commands/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export function useIsTemplatesAccessible() {
return useSelect(
( select ) => select( coreStore ).canUser( 'read', 'templates' ),
[]
);
}

export function useIsBlockBasedTheme() {
return useSelect(
( select ) => select( coreStore ).getCurrentTheme()?.is_block_theme,
Expand Down
10 changes: 6 additions & 4 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { useDebounce } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { useIsTemplatesAccessible, useIsBlockBasedTheme } from './hooks';
import { useIsBlockBasedTheme } from './hooks';
import { unlock } from './lock-unlock';
import { orderEntityRecordsBySearch } from './utils/order-entity-records-by-search';

Expand Down Expand Up @@ -257,12 +257,14 @@ function useSiteEditorBasicNavigationCommands() {
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const isTemplatesAccessible = useIsTemplatesAccessible();
const canCreateTemplate = useSelect( ( select ) => {
return select( coreStore ).canUser( 'create', 'templates' );
}, [] );
const isBlockBasedTheme = useIsBlockBasedTheme();
const commands = useMemo( () => {
const result = [];

if ( ! isTemplatesAccessible || ! isBlockBasedTheme ) {
if ( ! canCreateTemplate || ! isBlockBasedTheme ) {
return result;
}

Expand Down Expand Up @@ -339,7 +341,7 @@ function useSiteEditorBasicNavigationCommands() {
} );

return result;
}, [ history, isSiteEditor, isTemplatesAccessible, isBlockBasedTheme ] );
}, [ history, isSiteEditor, canCreateTemplate, isBlockBasedTheme ] );

return {
commands,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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' ) ? patternsUrl : defaultUrl;
return canUser( 'create', 'templates' ) ? patternsUrl : defaultUrl;
}, [] );

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/patterns/src/components/patterns-manage-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function PatternsManageButton( { clientId } ) {
// The site editor and templates both check whether the user
// has edit_theme_options capabilities. We can leverage that here
// and omit the manage patterns link if the user can't access it.
managePatternsUrl: canUser( 'read', 'templates' )
managePatternsUrl: canUser( 'create', 'templates' )
? addQueryArgs( 'site-editor.php', {
path: '/patterns',
} )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function ReusableBlocksManageButton( { clientId } ) {
// The site editor and templates both check whether the user
// has edit_theme_options capabilities. We can leverage that here
// and omit the manage patterns link if the user can't access it.
managePatternsUrl: canUser( 'read', 'templates' )
managePatternsUrl: canUser( 'create', 'templates' )
? addQueryArgs( 'site-editor.php', {
path: '/patterns',
} )
Expand Down
Loading