Skip to content

Commit

Permalink
Post Actions: Use entity details for capability checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 11, 2024
1 parent 3f27f26 commit 3e8846a
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,28 +230,30 @@ const trashPostAction = {
},
};

function useCanUserEligibilityCheckPostType( capability, resource, action ) {
function useCanUserEligibilityCheckPostType( capability, postType, action ) {
const registry = useRegistry();
return useMemo(
() => ( {
...action,
isEligible( item ) {
return (
action.isEligible( item ) &&
registry
.select( coreStore )
.canUser( capability, resource, item.id )
registry.select( coreStore ).canUser( capability, {
kind: 'postType',
name: postType,
id: item.id,
} )
);
},
} ),
[ action, registry, capability, resource ]
[ action, registry, capability, postType ]
);
}

function useTrashPostAction( resource ) {
function useTrashPostAction( postType ) {
return useCanUserEligibilityCheckPostType(
'delete',
resource,
postType,
trashPostAction
);
}
Expand Down Expand Up @@ -347,10 +349,10 @@ const permanentlyDeletePostAction = {
},
};

function usePermanentlyDeletePostAction( resource ) {
function usePermanentlyDeletePostAction( postType ) {
return useCanUserEligibilityCheckPostType(
'delete',
resource,
postType,
permanentlyDeletePostAction
);
}
Expand Down Expand Up @@ -462,10 +464,10 @@ const restorePostAction = {
},
};

function useRestorePostAction( resource ) {
function useRestorePostAction( postType ) {
return useCanUserEligibilityCheckPostType(
'update',
resource,
postType,
restorePostAction
);
}
Expand Down Expand Up @@ -623,22 +625,21 @@ const renamePostAction = {
},
};

function useRenamePostAction( resource ) {
function useRenamePostAction( postType ) {
return useCanUserEligibilityCheckPostType(
'update',
resource,
postType,
renamePostAction
);
}

const useDuplicatePostAction = ( postType ) => {
const { userCanCreatePost } = useSelect(
const userCanCreatePost = useSelect(
( select ) => {
const { getPostType, canUser } = select( coreStore );
const resource = getPostType( postType )?.rest_base || '';
return {
userCanCreatePost: canUser( 'create', resource ),
};
return select( coreStore ).canUser( 'create', {
kind: 'postType',
name: postType,
} );
},
[ postType ]
);
Expand Down Expand Up @@ -863,32 +864,32 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
defaultActions,
postTypeObject,
userCanCreatePostType,
resource,
cachedCanUserResolvers,
} = useSelect(
( select ) => {
const { getPostType, canUser, getCachedResolvers } =
select( coreStore );
const { getEntityActions } = unlock( select( editorStore ) );
const _postTypeObject = getPostType( postType );
const _resource = _postTypeObject?.rest_base || '';
return {
postTypeObject: _postTypeObject,
defaultActions: getEntityActions( 'postType', postType ),
userCanCreatePostType: canUser( 'create', _resource ),
resource: _resource,
userCanCreatePostType: canUser( 'create', {
kind: 'postType',
name: postType,
} ),
cachedCanUserResolvers: getCachedResolvers()?.canUser,
};
},
[ postType ]
);

const duplicatePostAction = useDuplicatePostAction( postType );
const trashPostActionForPostType = useTrashPostAction( resource );
const trashPostActionForPostType = useTrashPostAction( postType );
const permanentlyDeletePostActionForPostType =
usePermanentlyDeletePostAction( resource );
const renamePostActionForPostType = useRenamePostAction( resource );
const restorePostActionForPostType = useRestorePostAction( resource );
usePermanentlyDeletePostAction( postType );
const renamePostActionForPostType = useRenamePostAction( postType );
const restorePostActionForPostType = useRestorePostAction( postType );
const isTemplateOrTemplatePart = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
Expand Down

0 comments on commit 3e8846a

Please sign in to comment.