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