Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Display trash button only if user has delete capability #15131

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/editor/src/components/post-trash/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import { withSelect } from '@wordpress/data';

function PostTrashCheck( { isNew, postId, children } ) {
if ( isNew || ! postId ) {
function PostTrashCheck( { canUserTrash, isNew, postId, children } ) {
if ( ! canUserTrash || isNew || ! postId ) {
return null;
}

Expand All @@ -13,8 +13,12 @@ function PostTrashCheck( { isNew, postId, children } ) {

export default withSelect( ( select ) => {
const { isEditedPostNew, getCurrentPostId } = select( 'core/editor' );
const { canUser } = select( 'core' );
const postId = getCurrentPostId();

return {
isNew: isEditedPostNew(),
postId: getCurrentPostId(),
canUserTrash: !! postId && canUser( 'delete', 'posts', postId ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the postType be hard-coded here? I'm seeing a 404 from the request that the resolver makes when trying to edit a reusable block. That might also be the cause of the test failures.

IIRC, the rest_base from the postType is the value that should be used.

Copy link
Member Author

@aduth aduth Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the postType be hard-coded here? I'm seeing a 404 from the request that the resolver makes when trying to edit a reusable block. That might also be the cause of the test failures.

IIRC, the rest_base from the postType is the value that should be used.

Yep, I think you're correct on both accounts (shouldn't be hard-coded, should leverage rest_base).

postId,
};
} )( PostTrashCheck );
56 changes: 28 additions & 28 deletions packages/editor/src/components/post-trash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { withDispatch } from '@wordpress/data';

function PostTrash( { isNew, postId, postType, ...props } ) {
if ( isNew || ! postId ) {
return null;
}

const onClick = () => props.trashPost( postId, postType );
/**
* Internal dependencies
*/
import PostTrashCheck from './check';

function PostTrash( { trashPost } ) {
return (
<Button className="editor-post-trash button-link-delete" onClick={ onClick } isDefault isLarge>
{ __( 'Move to Trash' ) }
</Button>
<PostTrashCheck>
<Button
onClick={ trashPost }
isDefault
isLarge
className="editor-post-trash button-link-delete"
>
{ __( 'Move to Trash' ) }
</Button>
</PostTrashCheck>
);
}

export default compose( [
withSelect( ( select ) => {
const {
isEditedPostNew,
getCurrentPostId,
getCurrentPostType,
} = select( 'core/editor' );
return {
isNew: isEditedPostNew(),
postId: getCurrentPostId(),
postType: getCurrentPostType(),
};
} ),
withDispatch( ( dispatch ) => ( {
trashPost: dispatch( 'core/editor' ).trashPost,
} ) ),
] )( PostTrash );
export default withDispatch( ( dispatch, ownProps, registry ) => {
const { trashPost } = dispatch( 'core/editor' );

return {
trashPost() {
const { postId } = ownProps;
const { getCurrentPostType } = registry.select( 'core/editor' );
const postType = getCurrentPostType();
trashPost( postId, postType );
},
};
} )( PostTrash );