Skip to content

Commit

Permalink
Site Editor Navigation Commands: Add permission check (WordPress#63798)
Browse files Browse the repository at this point in the history
* Site Editor Navigation Commands: Add permission check

* Replate `"wp_template"` with `templateType`

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: dlh01 <[email protected]>
  • Loading branch information
4 people authored Jul 23, 2024
1 parent acaffc1 commit 90a058f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
12 changes: 0 additions & 12 deletions packages/core-commands/src/hooks.js

This file was deleted.

53 changes: 40 additions & 13 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import { useIsBlockBasedTheme } from './hooks';
import { unlock } from './lock-unlock';
import { orderEntityRecordsBySearch } from './utils/order-entity-records-by-search';

Expand Down Expand Up @@ -51,7 +50,19 @@ function useDebouncedValue( value ) {
const getNavigationCommandLoaderPerPostType = ( postType ) =>
function useNavigationCommandLoader( { search } ) {
const history = useHistory();
const isBlockBasedTheme = useIsBlockBasedTheme();
const { isBlockBasedTheme, canCreateTemplate } = useSelect(
( select ) => {
return {
isBlockBasedTheme:
select( coreStore ).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
},
[]
);
const delayedSearch = useDebouncedValue( search );
const { records, isLoading } = useSelect(
( select ) => {
Expand Down Expand Up @@ -100,6 +111,7 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
};

if (
! canCreateTemplate ||
postType === 'post' ||
( postType === 'page' && ! isBlockBasedTheme )
) {
Expand Down Expand Up @@ -142,7 +154,7 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
},
};
} );
}, [ records, isBlockBasedTheme, history ] );
}, [ canCreateTemplate, records, isBlockBasedTheme, history ] );

return {
commands,
Expand All @@ -153,7 +165,19 @@ const getNavigationCommandLoaderPerPostType = ( postType ) =>
const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
function useNavigationCommandLoader( { search } ) {
const history = useHistory();
const isBlockBasedTheme = useIsBlockBasedTheme();
const { isBlockBasedTheme, canCreateTemplate } = useSelect(
( select ) => {
return {
isBlockBasedTheme:
select( coreStore ).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select( coreStore ).canUser( 'create', {
kind: 'postType',
name: templateType,
} ),
};
},
[]
);
const { records, isLoading } = useSelect( ( select ) => {
const { getEntityRecords } = select( coreStore );
const query = { per_page: -1 };
Expand All @@ -177,8 +201,8 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>

const commands = useMemo( () => {
if (
! isBlockBasedTheme &&
! templateType === 'wp_template_part'
! canCreateTemplate ||
( ! isBlockBasedTheme && ! templateType === 'wp_template_part' )
) {
return [];
}
Expand Down Expand Up @@ -243,7 +267,7 @@ const getNavigationCommandLoaderPerTemplate = ( templateType ) =>
} );
}
return result;
}, [ isBlockBasedTheme, orderedRecords, history ] );
}, [ canCreateTemplate, isBlockBasedTheme, orderedRecords, history ] );

return {
commands,
Expand All @@ -265,13 +289,16 @@ function useSiteEditorBasicNavigationCommands() {
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const canCreateTemplate = useSelect( ( select ) => {
return select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} );
const { isBlockBasedTheme, canCreateTemplate } = useSelect( ( select ) => {
return {
isBlockBasedTheme:
select( coreStore ).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
}, [] );
const isBlockBasedTheme = useIsBlockBasedTheme();
const commands = useMemo( () => {
const result = [];

Expand Down

0 comments on commit 90a058f

Please sign in to comment.