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 11, 2024
1 parent 3f27f26 commit fed3d8f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 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',
} );

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

0 comments on commit fed3d8f

Please sign in to comment.