Skip to content

Commit

Permalink
Fix user capabilities check for the Site Editor (#61444)
Browse files Browse the repository at this point in the history
* Fix user Capabilities check for the Site Editor

* Remove internal hook

Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: bacoords <bacoords@git.wordpress.org>
  • Loading branch information
6 people authored May 8, 2024
1 parent 60d2269 commit 990214e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
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

0 comments on commit 990214e

Please sign in to comment.