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
21 changes: 19 additions & 2 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ Feature: Manage WordPress posts
Then STDOUT should be a number
And save STDOUT as {POST_ID}

When I run `wp post exists {POST_ID}`
When I run `wp post create --post_title='Test post' --post_type="test" --porcelain`
Then STDOUT should be a number
And save STDOUT as {CUSTOM_POST_ID}

When I run `wp post exists {CUSTOM_POST_ID}`
Then STDOUT should be:
"""
Success: Post with ID {POST_ID} exists.
Success: Post with ID {CUSTOM_POST_ID} exists.
"""
And the return code should be 0

Expand All @@ -37,6 +41,19 @@ Feature: Manage WordPress posts
Success: Deleted post {POST_ID}.
"""

When I try `wp post delete {CUSTOM_POST_ID}`
Then STDERR 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.
"""

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

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

Expand Down
43 changes: 29 additions & 14 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,24 +447,39 @@ public function get( $args, $assoc_args ) {
* Success: Deleted post 1294.
*/
public function delete( $args, $assoc_args ) {
$defaults = array(
'force' => false,
);
$defaults = [ 'force' => false ];
$assoc_args = array_merge( $defaults, $assoc_args );

parent::_delete( $args, $assoc_args, function ( $post_id, $assoc_args ) {
$status = get_post_status( $post_id );
$post_type = get_post_type( $post_id );
$r = wp_delete_post( $post_id, $assoc_args['force'] );
parent::_delete( $args, $assoc_args, [ $this, 'delete_callback' ] );
}

/**
* Callback used to delete a post.
*
* @param $post_id
* @param $assoc_args
* @return array
*/
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']
&& ( $post_type !== 'post' && $post_type !== 'page' ) ) {
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.',
];
}

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

if ( $r ) {
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';

return array( 'success', "$action post $post_id." );
} else {
return array( 'error', "Failed deleting post $post_id." );
}
} );
return [ 'success', "{$action} post {$post_id}." ];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/WP_CLI/CommandWithDBObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected static function process_csv_arguments_to_arrays( $assoc_args ) {
*
* @param array $args Collection of one or more object ids to delete.
* @param array $assoc_args Any arguments needed for the callback function.
* @param string $callback Function used to delete object.
* @param callable $callback Function used to delete object.
*/
protected function _delete( $args, $assoc_args, $callback ) {
$status = 0;
Expand Down