Skip to content

Commit

Permalink
Use entity details when calling 'canUser' selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 12, 2024
1 parent 6c3caac commit 6c48757
Show file tree
Hide file tree
Showing 31 changed files with 174 additions and 69 deletions.
6 changes: 5 additions & 1 deletion packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ function ReusableBlockEdit( {
getBlockEditingMode: _getBlockEditingMode,
} = select( blockEditorStore );
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
const canEdit = canUser( 'update', 'blocks', ref );
const canEdit = canUser( 'update', {
kind: 'postType',
name: 'wp_block',
id: ref,
} );

// For editing link to the site editor if the theme and user permissions support it.
return {
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/page-list-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import {

function useFrontPageId() {
return useSelect( ( select ) => {
const canReadSettings = select( coreStore ).canUser(
'read',
'settings'
);
const canReadSettings = select( coreStore ).canUser( 'read', {
kind: 'root',
name: 'site',
} );
if ( ! canReadSettings ) {
return undefined;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export default function QueryContent( {
const { postsPerPage } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const { getEntityRecord, canUser } = select( coreStore );
const settingPerPage = canUser( 'read', 'settings' )
const settingPerPage = canUser( 'read', {
kind: 'root',
name: 'site',
} )
? +getEntityRecord( 'root', 'site' )?.posts_per_page
: +getSettings().postsPerPage;
return {
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/site-logo/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ export default function LogoEdit( {
} = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
const _canUserEdit = canUser( 'update', 'settings' );
const _canUserEdit = canUser( 'update', {
kind: 'root',
name: 'site',
} );
const siteSettings = _canUserEdit
? getEditedEntityRecord( 'root', 'site' )
: undefined;
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/site-tagline/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ export default function SiteTaglineEdit( {
const { canUserEdit, tagline } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
const canEdit = canUser( 'update', 'settings' );
const canEdit = canUser( 'update', {
kind: 'root',
name: 'site',
} );
const settings = canEdit ? getEditedEntityRecord( 'root', 'site' ) : {};
const readOnlySettings = getEntityRecord( 'root', '__unstableBase' );

return {
canUserEdit: canUser( 'update', 'settings' ),
canUserEdit: canEdit,
tagline: canEdit
? settings?.description
: readOnlySettings?.description,
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/site-title/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default function SiteTitleEdit( {
const { canUserEdit, title } = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEditedEntityRecord } =
select( coreStore );
const canEdit = canUser( 'update', 'settings' );
const canEdit = canUser( 'update', {
kind: 'root',
name: 'site',
} );
const settings = canEdit ? getEditedEntityRecord( 'root', 'site' ) : {};
const readOnlySettings = getEntityRecord( 'root', '__unstableBase' );

Expand Down
8 changes: 5 additions & 3 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ export default function TemplatePartEdit( {
)
: false;

const _canEditTemplate =
select( coreStore ).canUser( 'create', 'templates' ) ?? false;
const _canEditTemplate = select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template_part',
} );

return {
hasInnerBlocks: getBlockCount( clientId ) > 0,
Expand All @@ -165,7 +167,7 @@ export default function TemplatePartEdit( {
onNavigateToEntityRecord:
getSettings().onNavigateToEntityRecord,
title: entityRecord?.title,
canEditTemplate: _canEditTemplate,
canEditTemplate: !! _canEditTemplate,
};
},
[ templatePartId, attributes.area, clientId ]
Expand Down
13 changes: 8 additions & 5 deletions packages/block-library/src/template-part/edit/inner-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,14 @@ export default function TemplatePartInnerBlocks( {
const { canViewTemplatePart, canEditTemplatePart } = useSelect(
( select ) => {
return {
canViewTemplatePart:
select( coreStore ).canUser( 'read', 'templates' ) ?? false,
canEditTemplatePart:
select( coreStore ).canUser( 'create', 'templates' ) ??
false,
canViewTemplatePart: !! select( coreStore ).canUser( 'read', {
kind: 'postType',
name: 'wp_template',
} ),
canEditTemplatePart: !! select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
},
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ function useSiteEditorBasicNavigationCommands() {
'site-editor.php'
);
const canCreateTemplate = useSelect( ( select ) => {
return select( coreStore ).canUser( 'create', 'templates' );
return select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} );
}, [] );
const isBlockBasedTheme = useIsBlockBasedTheme();
const commands = useMemo( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export default function SidebarBlockEditor( {
const { get } = select( preferencesStore );
return {
hasUploadPermissions:
select( coreStore ).canUser( 'create', 'media' ) ?? true,
select( coreStore ).canUser( 'create', {
kind: 'root',
name: 'media',
} ) ?? true,
isFixedToolbarActive: !! get(
'core/customize-widgets',
'fixedToolbar'
Expand Down
5 changes: 4 additions & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ function Layout( {
const supportsTemplateMode = settings.supportsTemplateMode;
const isViewable =
getPostType( currentPost.postType )?.viewable ?? false;
const canViewTemplate = canUser( 'read', 'templates' );
const canViewTemplate = canUser( 'read', {
kind: 'postType',
name: 'wp_template',
} );

return {
mode: select( editorStore ).getEditorMode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ 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( 'create', 'templates' ) ? patternsUrl : defaultUrl;
return canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} )
? patternsUrl
: defaultUrl;
}, [] );

return (
Expand Down
10 changes: 8 additions & 2 deletions packages/edit-site/src/components/add-new-pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ export default function AddNewPattern() {
addNewTemplatePartLabel: getPostType( TEMPLATE_PART_POST_TYPE )
?.labels?.add_new_item,
// Blocks refers to the wp_block post type, this checks the ability to create a post of that type.
canCreatePattern: canUser( 'create', 'blocks' ),
canCreateTemplatePart: canUser( 'create', 'template-parts' ),
canCreatePattern: canUser( 'create', {
kind: 'postType',
name: PATTERN_TYPES.user,
} ),
canCreateTemplatePart: canUser( 'create', {
kind: 'postType',
name: TEMPLATE_PART_POST_TYPE,
} ),
};
}, [] );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ function FontLibraryModal( {
} ) {
const { collections, setNotice } = useContext( FontLibraryContext );
const canUserCreate = useSelect( ( select ) => {
const { canUser } = select( coreStore );
return canUser( 'create', 'font-families' );
return select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_font_family',
} );
}, [] );

const tabs = [ DEFAULT_TAB ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ function InstalledFonts() {
const { canUser } = select( coreStore );
return (
customFontFamilyId &&
canUser( 'delete', 'font-families', customFontFamilyId )
canUser( 'delete', {
kind: 'postType',
name: 'wp_font_family',
id: customFontFamilyId,
} )
);
},
[ customFontFamilyId ]
Expand Down
9 changes: 4 additions & 5 deletions packages/edit-site/src/components/post-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,14 @@ export default function PostList( { postType } ) {
const { getEntityRecord, getPostType, canUser } =
select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
const postTypeObject = getPostType( postType );
return {
frontPageId: siteSettings?.page_on_front,
postsPageId: siteSettings?.page_for_posts,
labels: getPostType( postType )?.labels,
canCreateRecord: canUser(
'create',
postTypeObject?.rest_base || 'posts'
),
canCreateRecord: canUser( 'create', {
kind: 'postType',
name: postType,
} ),
};
},
[ postType ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export default function WidgetAreasBlockEditorProvider( {
} = useSelect( ( select ) => {
const { canUser, getEntityRecord, getEntityRecords } =
select( coreStore );
const siteSettings = canUser( 'read', 'settings' )
const siteSettings = canUser( 'read', {
kind: 'root',
name: 'site',
} )
? getEntityRecord( 'root', 'site' )
: undefined;
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ function ContentOnlySettingsMenuItems( { clientId, onClose } ) {
);
}
}
const _canEditTemplates = select( coreStore ).canUser(
'create',
'templates'
);
const _canEditTemplates = select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} );
return {
canEditTemplates: _canEditTemplates,
entity: record,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function MaybeCategoryPanel() {
const postType = select( editorStore ).getCurrentPostType();
const { canUser, getEntityRecord, getTaxonomy } = select( coreStore );
const categoriesTaxonomy = getTaxonomy( 'category' );
const defaultCategoryId = canUser( 'read', 'settings' )
const defaultCategoryId = canUser( 'read', {
kind: 'root',
name: 'site',
} )
? getEntityRecord( 'root', 'site' )?.default_category
: undefined;
const defaultCategory = defaultCategoryId
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/components/post-template/block-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export default function BlockThemeControl( { id } ) {

const canCreateTemplate = useSelect(
( select ) =>
select( coreStore ).canUser( 'create', 'templates' ) ?? false
select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
[]
);

if ( ! hasResolved ) {
Expand Down
11 changes: 8 additions & 3 deletions packages/editor/src/components/post-template/classic-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ function PostTemplateToggle( { isOpen, onClick } ) {
return availableTemplates[ templateSlug ];
}
const template =
select( coreStore ).canUser( 'create', 'templates' ) &&
select( editorStore ).getCurrentTemplateId();
select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ) && select( editorStore ).getCurrentTemplateId();
return (
template?.title ||
template?.slug ||
Expand Down Expand Up @@ -78,7 +80,10 @@ function PostTemplateDropdownContent( { onClose } ) {
( select ) => {
const { canUser, getEntityRecords } = select( coreStore );
const editorSettings = select( editorStore ).getEditorSettings();
const canCreateTemplates = canUser( 'create', 'templates' );
const canCreateTemplates = canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} );
const _currentTemplateId =
select( editorStore ).getCurrentTemplateId();
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export default function CreateNewTemplate( { onClick } ) {
const { canCreateTemplates } = useSelect( ( select ) => {
const { canUser } = select( coreStore );
return {
canCreateTemplates: canUser( 'create', 'templates' ),
canCreateTemplates: canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
}, [] );
const [ isCreateModalOpen, setIsCreateModalOpen ] = useState( false );
Expand Down
12 changes: 10 additions & 2 deletions packages/editor/src/components/post-template/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ export default function PostTemplatePanel() {
}

const canCreateTemplates =
select( coreStore ).canUser( 'create', 'templates' ) ?? false;
select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ) ?? false;
return canCreateTemplates;
}, [] );

const canViewTemplates = useSelect( ( select ) => {
return select( coreStore ).canUser( 'read', 'templates' ) ?? false;
return (
select( coreStore ).canUser( 'read', {
kind: 'postType',
name: 'wp_template',
} ) ?? false
);
}, [] );

if ( ( ! isBlockTheme || ! canViewTemplates ) && isVisible ) {
Expand Down
14 changes: 9 additions & 5 deletions packages/editor/src/components/post-trash/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ export default function PostTrashCheck( { children } ) {
const { canTrashPost } = useSelect( ( select ) => {
const { isEditedPostNew, getCurrentPostId, getCurrentPostType } =
select( editorStore );
const { getPostType, canUser } = select( coreStore );
const postType = getPostType( getCurrentPostType() );
const { canUser } = select( coreStore );
const postType = getCurrentPostType();
const postId = getCurrentPostId();
const isNew = isEditedPostNew();
const resource = postType?.rest_base || ''; // eslint-disable-line camelcase
const canUserDelete =
postId && resource ? canUser( 'delete', resource, postId ) : false;
const canUserDelete = !! postId
? canUser( 'delete', {
kind: 'postType',
name: postType,
id: postId,
} )
: false;

return {
canTrashPost: ( ! isNew || postId ) && canUserDelete,
Expand Down
Loading

0 comments on commit 6c48757

Please sign in to comment.