Skip to content

Commit 3ad7a07

Browse files
committed
Fix logic and tests
1 parent 96d8c79 commit 3ad7a07

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

features/post.feature

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Feature: Manage WordPress posts
1212
Then STDOUT should be a number
1313
And save STDOUT as {CUSTOM_POST_ID}
1414

15-
When I run `wp post exists {POST_ID}`
15+
When I run `wp post exists {CUSTOM_POST_ID}`
1616
Then STDOUT should be:
1717
"""
18-
Success: Post with ID {POST_ID} exists.
18+
Success: Post with ID {CUSTOM_POST_ID} exists.
1919
"""
2020
And the return code should be 0
2121

@@ -35,6 +35,12 @@ Feature: Manage WordPress posts
3535
Success: Trashed post {POST_ID}.
3636
"""
3737

38+
When I run the previous command again
39+
Then STDOUT should be:
40+
"""
41+
Success: Deleted post {POST_ID}.
42+
"""
43+
3844
When I try `wp post delete {CUSTOM_POST_ID}`
3945
Then STDERR should be:
4046
"""
@@ -48,12 +54,6 @@ Feature: Manage WordPress posts
4854
Success: Deleted post {CUSTOM_POST_ID}.
4955
"""
5056

51-
When I run the previous command again
52-
Then STDOUT should be:
53-
"""
54-
Success: Deleted post {POST_ID}.
55-
"""
56-
5757
When I try the previous command again
5858
Then the return code should be 1
5959

src/Post_Command.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -447,29 +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 );
453+
parent::_delete( $args, $assoc_args, [ $this, 'delete_callback' ] );
454+
}
458455

459-
if ( !$assoc_args['force'] && ( $post_type !== 'post' && $post_type !== 'page' ) ) {
460-
return array( 'error', "Posts of type '$post_type' do not support being sent to trash.\nPlease use the --force flag to skip trash and delete them permanently." );
461-
}
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+
}
462475

463-
$r = wp_delete_post( $post_id, $assoc_args['force'] );
476+
if ( ! wp_delete_post( $post_id, $assoc_args['force'] ) ) {
477+
return [ 'error', "Failed deleting post {$post_id}." ];
478+
}
464479

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

468-
return array( 'success', "$action post $post_id." );
469-
} else {
470-
return array( 'error', "Failed deleting post $post_id." );
471-
}
472-
} );
482+
return [ 'success', "{$action} post {$post_id}." ];
473483
}
474484

475485
/**

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)