Skip to content
Merged
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
20 changes: 15 additions & 5 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,29 @@ Feature: Manage WordPress posts
Success: Deleted post {POST_ID}.
"""

When I try `wp post delete {CUSTOM_POST_ID}`
Then STDERR should be:
When I run `wp post delete {CUSTOM_POST_ID}`
Then STDOUT should be:
"""
Warning: Posts of type 'test' do not support being sent to trash.
Please use the --force flag to skip trash and delete them permanently.
Success: Trashed post {CUSTOM_POST_ID}.
"""

When I run `wp post delete {CUSTOM_POST_ID} --force`
When I run the previous command again
Then STDOUT should be:
"""
Success: Deleted post {CUSTOM_POST_ID}.
"""

Scenario: Force-deleting a custom post type post skips trash
When I run `wp post create --post_title='Test CPT post' --post_type='book' --porcelain`
Then STDOUT should be a number
And save STDOUT as {BOOK_POST_ID}

When I run `wp post delete {BOOK_POST_ID} --force`
Then STDOUT should be:
"""
Success: Deleted post {BOOK_POST_ID}.
"""

When I try the previous command again
Then the return code should be 1

Expand Down
27 changes: 14 additions & 13 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,24 @@ protected function delete_callback( $post_id, $assoc_args ) {
$status = get_post_status( $post_id );
$post_type = get_post_type( $post_id );

if ( ! $assoc_args['force']
&& 'trash' !== $status
&& ( 'post' !== $post_type && 'page' !== $post_type ) ) {
return [
'error',
"Posts of type '{$post_type}' do not support being sent to trash.\n"
. 'Please use the --force flag to skip trash and delete them permanently.',
];
}
$force_delete = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type;

if ( ! wp_delete_post( $post_id, $assoc_args['force'] ) ) {
return [ 'error', "Failed deleting post {$post_id}." ];
if ( $force_delete || ! EMPTY_TRASH_DAYS ) {
if ( ! wp_delete_post( $post_id, true ) ) {
return [ 'error', "Failed deleting post {$post_id}." ];
}

return [ 'success', "Deleted post {$post_id}." ];
}

$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
// Use wp_trash_post() directly because wp_delete_post() only auto-trashes
// 'post' and 'page' types, permanently deleting all other post types even
// when $force_delete is false. wp_trash_post() works for all post types.
if ( wp_trash_post( $post_id ) ) {
return [ 'success', "Trashed post {$post_id}." ];
}

return [ 'success', "{$action} post {$post_id}." ];
return [ 'error', "Failed trashing post {$post_id}." ];
}

/**
Expand Down