Skip to content

Commit 459fba6

Browse files
authored
Merge pull request #188 from Mte90/delete
Delete alert in case of custom post type
2 parents 26ce047 + 3ad7a07 commit 459fba6

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

features/post.feature

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ Feature: Manage WordPress posts
88
Then STDOUT should be a number
99
And save STDOUT as {POST_ID}
1010

11-
When I run `wp post exists {POST_ID}`
11+
When I run `wp post create --post_title='Test post' --post_type="test" --porcelain`
12+
Then STDOUT should be a number
13+
And save STDOUT as {CUSTOM_POST_ID}
14+
15+
When I run `wp post exists {CUSTOM_POST_ID}`
1216
Then STDOUT should be:
1317
"""
14-
Success: Post with ID {POST_ID} exists.
18+
Success: Post with ID {CUSTOM_POST_ID} exists.
1519
"""
1620
And the return code should be 0
1721

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

44+
When I try `wp post delete {CUSTOM_POST_ID}`
45+
Then STDERR should be:
46+
"""
47+
Warning: Posts of type 'test' do not support being sent to trash.
48+
Please use the --force flag to skip trash and delete them permanently.
49+
"""
50+
51+
When I run `wp post delete {CUSTOM_POST_ID} --force`
52+
Then STDOUT should be:
53+
"""
54+
Success: Deleted post {CUSTOM_POST_ID}.
55+
"""
56+
4057
When I try the previous command again
4158
Then the return code should be 1
4259

src/Post_Command.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -447,24 +447,39 @@ public function get( $args, $assoc_args ) {
447447
* Success: Deleted post 1294.
448448
*/
449449
public function delete( $args, $assoc_args ) {
450-
$defaults = array(
451-
'force' => false,
452-
);
450+
$defaults = [ 'force' => false ];
453451
$assoc_args = array_merge( $defaults, $assoc_args );
454452

455-
parent::_delete( $args, $assoc_args, function ( $post_id, $assoc_args ) {
456-
$status = get_post_status( $post_id );
457-
$post_type = get_post_type( $post_id );
458-
$r = wp_delete_post( $post_id, $assoc_args['force'] );
453+
parent::_delete( $args, $assoc_args, [ $this, 'delete_callback' ] );
454+
}
455+
456+
/**
457+
* Callback used to delete a post.
458+
*
459+
* @param $post_id
460+
* @param $assoc_args
461+
* @return array
462+
*/
463+
protected function delete_callback( $post_id, $assoc_args ) {
464+
$status = get_post_status( $post_id );
465+
$post_type = get_post_type( $post_id );
466+
467+
if ( ! $assoc_args['force']
468+
&& ( $post_type !== 'post' && $post_type !== 'page' ) ) {
469+
return [
470+
'error',
471+
"Posts of type '{$post_type}' do not support being sent to trash.\n"
472+
. 'Please use the --force flag to skip trash and delete them permanently.',
473+
];
474+
}
475+
476+
if ( ! wp_delete_post( $post_id, $assoc_args['force'] ) ) {
477+
return [ 'error', "Failed deleting post {$post_id}." ];
478+
}
459479

460-
if ( $r ) {
461-
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
480+
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
462481

463-
return array( 'success', "$action post $post_id." );
464-
} else {
465-
return array( 'error', "Failed deleting post $post_id." );
466-
}
467-
} );
482+
return [ 'success', "{$action} post {$post_id}." ];
468483
}
469484

470485
/**

src/WP_CLI/CommandWithDBObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected static function process_csv_arguments_to_arrays( $assoc_args ) {
103103
*
104104
* @param array $args Collection of one or more object ids to delete.
105105
* @param array $assoc_args Any arguments needed for the callback function.
106-
* @param string $callback Function used to delete object.
106+
* @param callable $callback Function used to delete object.
107107
*/
108108
protected function _delete( $args, $assoc_args, $callback ) {
109109
$status = 0;

0 commit comments

Comments
 (0)