Skip to content

Commit 0ccc287

Browse files
committed
feat: add --all flag to comment delete command
1 parent a538162 commit 0ccc287

File tree

2 files changed

+148
-2
lines changed

2 files changed

+148
-2
lines changed

features/comment.feature

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,80 @@ Feature: Manage WordPress comments
472472
And I run `wp comment unspam {COMMENT_ID} --url=www.example.com`
473473
And I run `wp comment trash {COMMENT_ID} --url=www.example.com`
474474
And I run `wp comment untrash {COMMENT_ID} --url=www.example.com`
475+
476+
Scenario: Delete all comments with --all flag
477+
Given a WP install
478+
And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain`
479+
And save STDOUT as {COMMENT_ID_1}
480+
And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain`
481+
And save STDOUT as {COMMENT_ID_2}
482+
And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 3' --porcelain`
483+
And save STDOUT as {COMMENT_ID_3}
484+
485+
When I run `wp comment list --format=count`
486+
Then STDOUT should be:
487+
"""
488+
4
489+
"""
490+
491+
When I run `wp comment delete --all`
492+
Then STDOUT should be:
493+
"""
494+
Success: Trashed comment 1.
495+
Success: Trashed comment {COMMENT_ID_1}.
496+
Success: Trashed comment {COMMENT_ID_2}.
497+
Success: Trashed comment {COMMENT_ID_3}.
498+
"""
499+
500+
When I run `wp comment list --format=count`
501+
Then STDOUT should be:
502+
"""
503+
0
504+
"""
505+
506+
Scenario: Delete all comments with --all flag and --force
507+
Given a WP install
508+
And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain`
509+
And save STDOUT as {COMMENT_ID_1}
510+
And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain`
511+
And save STDOUT as {COMMENT_ID_2}
512+
513+
When I run `wp comment list --format=count`
514+
Then STDOUT should be:
515+
"""
516+
3
517+
"""
518+
519+
When I run `wp comment delete --all --force`
520+
Then STDOUT should be:
521+
"""
522+
Success: Deleted comment 1.
523+
Success: Deleted comment {COMMENT_ID_1}.
524+
Success: Deleted comment {COMMENT_ID_2}.
525+
"""
526+
527+
When I run `wp comment list --format=count`
528+
Then STDOUT should be:
529+
"""
530+
0
531+
"""
532+
533+
Scenario: Delete all comments when no comments exist
534+
Given a WP install
535+
And I run `wp comment delete $(wp comment list --field=ID) --force`
536+
537+
When I run `wp comment delete --all`
538+
Then STDOUT should be:
539+
"""
540+
Success: No comments deleted.
541+
"""
542+
543+
Scenario: Error when no comment IDs and no --all flag provided
544+
Given a WP install
545+
546+
When I try `wp comment delete`
547+
Then STDERR should be:
548+
"""
549+
Error: Please specify one or more comment IDs, or use --all.
550+
"""
551+
And the return code should be 1

src/Comment_Command.php

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* # Update an existing comment.
1717
* $ wp comment update 123 --comment_author='That Guy'
1818
* Success: Updated comment 123.
19-
*
19+
2020
* # Delete an existing comment.
2121
* $ wp comment delete 1337 --force
2222
* Success: Deleted comment 1337.
@@ -416,9 +416,12 @@ function ( $comment ) {
416416
*
417417
* ## OPTIONS
418418
*
419-
* <id>...
419+
* [<id>...]
420420
* : One or more IDs of comments to delete.
421421
*
422+
* [--all]
423+
* : If set, all comments will be deleted.
424+
*
422425
* [--force]
423426
* : Skip the trash bin.
424427
*
@@ -432,8 +435,22 @@ function ( $comment ) {
432435
* $ wp comment delete 1337 2341 --force
433436
* Success: Deleted comment 1337.
434437
* Success: Deleted comment 2341.
438+
*
439+
* # Delete all comments.
440+
* $ wp comment delete --all --force
441+
* Success: Deleted comment 1337.
442+
* Success: Deleted comment 2341.
443+
* Success: Deleted 2 of 2 comments.
435444
*/
436445
public function delete( $args, $assoc_args ) {
446+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
447+
448+
// Check if comment IDs or --all is passed.
449+
$args = $this->check_optional_args_and_all( $args, $all, 'delete' );
450+
if ( ! $args ) {
451+
return;
452+
}
453+
437454
parent::_delete(
438455
$args,
439456
$assoc_args,
@@ -734,4 +751,56 @@ public function exists( $args ) {
734751
WP_CLI::success( "Comment with ID {$args[0]} exists." );
735752
}
736753
}
754+
755+
/**
756+
* If have optional args ([<id>...]) and an all option, then check have something to do.
757+
*
758+
* @param array $args Passed-in arguments.
759+
* @param bool $all All flag.
760+
* @param string $verb Optional. Verb to use. Defaults to 'delete'.
761+
* @param string $exclude Comma separated list of comment IDs.
762+
* @return array Same as $args if not all, otherwise all comment IDs.
763+
* @throws ExitException If neither comment ID nor --all were provided.
764+
*/
765+
private function check_optional_args_and_all( $args, $all, $verb = 'delete', $exclude = null ) {
766+
if ( $all ) {
767+
$args = $this->get_all_comment_ids();
768+
}
769+
770+
if ( $all && $exclude ) {
771+
$exclude_list = array_map( 'intval', explode( ',', trim( $exclude, ',' ) ) );
772+
$args = array_filter(
773+
$args,
774+
static function ( $id ) use ( $exclude_list ) {
775+
return ! in_array( (int) $id, $exclude_list, true );
776+
}
777+
);
778+
}
779+
780+
if ( empty( $args ) ) {
781+
if ( ! $all ) {
782+
WP_CLI::error( 'Please specify one or more comment IDs, or use --all.' );
783+
}
784+
785+
$past_tense_verb = Utils\past_tense_verb( $verb );
786+
WP_CLI::success( "No comments {$past_tense_verb}." );
787+
}
788+
789+
return $args;
790+
}
791+
792+
/**
793+
* Gets all available comment IDs.
794+
*
795+
* @return array Array of comment IDs.
796+
*/
797+
private function get_all_comment_ids() {
798+
$query = new WP_Comment_Query();
799+
$comments = $query->query( array(
800+
'fields' => 'ids',
801+
'number' => 0, // Get all comments
802+
) );
803+
804+
return $comments;
805+
}
737806
}

0 commit comments

Comments
 (0)