From 0ccc2870253a8798df89b894da6e629abf5f342f Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 15:58:56 -0400 Subject: [PATCH 01/34] feat: add --all flag to comment delete command --- features/comment.feature | 77 ++++++++++++++++++++++++++++++++++++++++ src/Comment_Command.php | 73 +++++++++++++++++++++++++++++++++++-- 2 files changed, 148 insertions(+), 2 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index 290954e02..71a237892 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -472,3 +472,80 @@ Feature: Manage WordPress comments And I run `wp comment unspam {COMMENT_ID} --url=www.example.com` And I run `wp comment trash {COMMENT_ID} --url=www.example.com` And I run `wp comment untrash {COMMENT_ID} --url=www.example.com` + + Scenario: Delete all comments with --all flag + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` + And save STDOUT as {COMMENT_ID_1} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` + And save STDOUT as {COMMENT_ID_2} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 3' --porcelain` + And save STDOUT as {COMMENT_ID_3} + + When I run `wp comment list --format=count` + Then STDOUT should be: + """ + 4 + """ + + When I run `wp comment delete --all` + Then STDOUT should be: + """ + Success: Trashed comment 1. + Success: Trashed comment {COMMENT_ID_1}. + Success: Trashed comment {COMMENT_ID_2}. + Success: Trashed comment {COMMENT_ID_3}. + """ + + When I run `wp comment list --format=count` + Then STDOUT should be: + """ + 0 + """ + + Scenario: Delete all comments with --all flag and --force + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` + And save STDOUT as {COMMENT_ID_1} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` + And save STDOUT as {COMMENT_ID_2} + + When I run `wp comment list --format=count` + Then STDOUT should be: + """ + 3 + """ + + When I run `wp comment delete --all --force` + Then STDOUT should be: + """ + Success: Deleted comment 1. + Success: Deleted comment {COMMENT_ID_1}. + Success: Deleted comment {COMMENT_ID_2}. + """ + + When I run `wp comment list --format=count` + Then STDOUT should be: + """ + 0 + """ + + Scenario: Delete all comments when no comments exist + Given a WP install + And I run `wp comment delete $(wp comment list --field=ID) --force` + + When I run `wp comment delete --all` + Then STDOUT should be: + """ + Success: No comments deleted. + """ + + Scenario: Error when no comment IDs and no --all flag provided + Given a WP install + + When I try `wp comment delete` + Then STDERR should be: + """ + Error: Please specify one or more comment IDs, or use --all. + """ + And the return code should be 1 diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 766e28a54..2a7f01e5a 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -16,7 +16,7 @@ * # Update an existing comment. * $ wp comment update 123 --comment_author='That Guy' * Success: Updated comment 123. - * + * # Delete an existing comment. * $ wp comment delete 1337 --force * Success: Deleted comment 1337. @@ -416,9 +416,12 @@ function ( $comment ) { * * ## OPTIONS * - * ... + * [...] * : One or more IDs of comments to delete. * + * [--all] + * : If set, all comments will be deleted. + * * [--force] * : Skip the trash bin. * @@ -432,8 +435,22 @@ function ( $comment ) { * $ wp comment delete 1337 2341 --force * Success: Deleted comment 1337. * Success: Deleted comment 2341. + * + * # Delete all comments. + * $ wp comment delete --all --force + * Success: Deleted comment 1337. + * Success: Deleted comment 2341. + * Success: Deleted 2 of 2 comments. */ public function delete( $args, $assoc_args ) { + $all = Utils\get_flag_value( $assoc_args, 'all', false ); + + // Check if comment IDs or --all is passed. + $args = $this->check_optional_args_and_all( $args, $all, 'delete' ); + if ( ! $args ) { + return; + } + parent::_delete( $args, $assoc_args, @@ -734,4 +751,56 @@ public function exists( $args ) { WP_CLI::success( "Comment with ID {$args[0]} exists." ); } } + + /** + * If have optional args ([...]) and an all option, then check have something to do. + * + * @param array $args Passed-in arguments. + * @param bool $all All flag. + * @param string $verb Optional. Verb to use. Defaults to 'delete'. + * @param string $exclude Comma separated list of comment IDs. + * @return array Same as $args if not all, otherwise all comment IDs. + * @throws ExitException If neither comment ID nor --all were provided. + */ + private function check_optional_args_and_all( $args, $all, $verb = 'delete', $exclude = null ) { + if ( $all ) { + $args = $this->get_all_comment_ids(); + } + + if ( $all && $exclude ) { + $exclude_list = array_map( 'intval', explode( ',', trim( $exclude, ',' ) ) ); + $args = array_filter( + $args, + static function ( $id ) use ( $exclude_list ) { + return ! in_array( (int) $id, $exclude_list, true ); + } + ); + } + + if ( empty( $args ) ) { + if ( ! $all ) { + WP_CLI::error( 'Please specify one or more comment IDs, or use --all.' ); + } + + $past_tense_verb = Utils\past_tense_verb( $verb ); + WP_CLI::success( "No comments {$past_tense_verb}." ); + } + + return $args; + } + + /** + * Gets all available comment IDs. + * + * @return array Array of comment IDs. + */ + private function get_all_comment_ids() { + $query = new WP_Comment_Query(); + $comments = $query->query( array( + 'fields' => 'ids', + 'number' => 0, // Get all comments + ) ); + + return $comments; + } } From be450396f7da97f5e545f19d41f5868afdb3e0f7 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:01:45 -0400 Subject: [PATCH 02/34] fix: linting errors --- src/Comment_Command.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 2a7f01e5a..d8b89af53 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -795,11 +795,13 @@ static function ( $id ) use ( $exclude_list ) { * @return array Array of comment IDs. */ private function get_all_comment_ids() { - $query = new WP_Comment_Query(); - $comments = $query->query( array( - 'fields' => 'ids', - 'number' => 0, // Get all comments - ) ); + $query = new WP_Comment_Query(); + $comments = $query->query( + array( + 'fields' => 'ids', + 'number' => 0, // Get all comments + ) + ); return $comments; } From 24f1c4c39551df839fa81d9efbf6f0907bbc57e2 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:04:56 -0400 Subject: [PATCH 03/34] fix: phpstan issues --- src/Comment_Command.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index d8b89af53..f150bd874 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -791,10 +791,8 @@ static function ( $id ) use ( $exclude_list ) { /** * Gets all available comment IDs. - * - * @return array Array of comment IDs. */ - private function get_all_comment_ids() { + private function get_all_comment_ids(): array { $query = new WP_Comment_Query(); $comments = $query->query( array( From c4d8775048394dc3103b5503bb5306570c9606bf Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:05:27 -0400 Subject: [PATCH 04/34] fix: phpstan issues --- src/Comment_Command.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index f150bd874..c95d5ae94 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -760,7 +760,6 @@ public function exists( $args ) { * @param string $verb Optional. Verb to use. Defaults to 'delete'. * @param string $exclude Comma separated list of comment IDs. * @return array Same as $args if not all, otherwise all comment IDs. - * @throws ExitException If neither comment ID nor --all were provided. */ private function check_optional_args_and_all( $args, $all, $verb = 'delete', $exclude = null ) { if ( $all ) { From ea5d3140e5760e22adc9d06d8c7c5bcfc07ab594 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:07:30 -0400 Subject: [PATCH 05/34] fix: incorrect return type --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index c95d5ae94..4d45ceb36 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -791,7 +791,7 @@ static function ( $id ) use ( $exclude_list ) { /** * Gets all available comment IDs. */ - private function get_all_comment_ids(): array { + private function get_all_comment_ids(): array|int { $query = new WP_Comment_Query(); $comments = $query->query( array( From 846b801c5a326767c014c22aca400422353920f4 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:09:42 -0400 Subject: [PATCH 06/34] fix: always return array --- src/Comment_Command.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 4d45ceb36..99b60c82d 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -791,7 +791,7 @@ static function ( $id ) use ( $exclude_list ) { /** * Gets all available comment IDs. */ - private function get_all_comment_ids(): array|int { + private function get_all_comment_ids(): array { $query = new WP_Comment_Query(); $comments = $query->query( array( @@ -800,6 +800,10 @@ private function get_all_comment_ids(): array|int { ) ); + if (!is_array($comments)) { + return []; + } + return $comments; } } From 6dbf62de90f01964769470a7dfe058b1a7cf102e Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:10:41 -0400 Subject: [PATCH 07/34] fix: code style --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 99b60c82d..3793fd692 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -800,7 +800,7 @@ private function get_all_comment_ids(): array { ) ); - if (!is_array($comments)) { + if ( ! is_array( $comments ) ) { return []; } From e0f124b63820f7eb3e84cd7756a2ef6375febd1f Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 15 Oct 2025 16:20:24 -0400 Subject: [PATCH 08/34] fix: update test expectations to handle comment deletion order --- features/comment.feature | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index 71a237892..c970e2c9f 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -489,11 +489,20 @@ Feature: Manage WordPress comments """ When I run `wp comment delete --all` - Then STDOUT should be: + Then STDOUT should contain: """ Success: Trashed comment 1. + """ + And STDOUT should contain: + """ Success: Trashed comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ Success: Trashed comment {COMMENT_ID_2}. + """ + And STDOUT should contain: + """ Success: Trashed comment {COMMENT_ID_3}. """ @@ -517,10 +526,16 @@ Feature: Manage WordPress comments """ When I run `wp comment delete --all --force` - Then STDOUT should be: + Then STDOUT should contain: """ Success: Deleted comment 1. + """ + And STDOUT should contain: + """ Success: Deleted comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ Success: Deleted comment {COMMENT_ID_2}. """ From 000c83243813485a44b064c4585edcb02a695e3c Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Thu, 16 Oct 2025 01:46:45 -0400 Subject: [PATCH 09/34] fix: remove $exclude code --- src/Comment_Command.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 3793fd692..1e386aadb 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -758,24 +758,13 @@ public function exists( $args ) { * @param array $args Passed-in arguments. * @param bool $all All flag. * @param string $verb Optional. Verb to use. Defaults to 'delete'. - * @param string $exclude Comma separated list of comment IDs. * @return array Same as $args if not all, otherwise all comment IDs. */ - private function check_optional_args_and_all( $args, $all, $verb = 'delete', $exclude = null ) { + private function check_optional_args_and_all( $args, $all, $verb = 'delete' ) { if ( $all ) { $args = $this->get_all_comment_ids(); } - if ( $all && $exclude ) { - $exclude_list = array_map( 'intval', explode( ',', trim( $exclude, ',' ) ) ); - $args = array_filter( - $args, - static function ( $id ) use ( $exclude_list ) { - return ! in_array( (int) $id, $exclude_list, true ); - } - ); - } - if ( empty( $args ) ) { if ( ! $all ) { WP_CLI::error( 'Please specify one or more comment IDs, or use --all.' ); From 860bb2b99e25060ead12c5e0f13d45379774f8f9 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 16:19:17 -0400 Subject: [PATCH 10/34] fix: defer comment counts --- src/Comment_Command.php | 57 +++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 1e386aadb..fe16dffe9 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -451,23 +451,54 @@ public function delete( $args, $assoc_args ) { return; } - parent::_delete( - $args, - $assoc_args, - function ( $comment_id, $assoc_args ) { - $force = (bool) Utils\get_flag_value( $assoc_args, 'force' ); + $defer_term_counting = Utils\get_flag_value( $assoc_args, 'defer-term-counting', false ); + + if ( $all && ! $defer_term_counting ) { + $assoc_args['defer-term-counting'] = true; + } - $status = wp_get_comment_status( $comment_id ); - $result = wp_delete_comment( $comment_id, $force ); + $status = 0; - if ( ! $result ) { - return [ 'error', "Failed deleting comment {$comment_id}." ]; - } + if ( $defer_term_counting ) { + wp_defer_term_counting( true ); + } - $verb = ( $force || 'trash' === $status ) ? 'Deleted' : 'Trashed'; - return [ 'success', "{$verb} comment {$comment_id}." ]; + $total = count( $args ); + $successfully_deleted = 0; + + $force = (bool) Utils\get_flag_value( $assoc_args, 'force' ); + + foreach ( $args as $comment_id ) { + + $comment_status = wp_get_comment_status( $comment_id ); + $result = wp_delete_comment( $comment_id, $force ); + + if ( ! $result ) { + $response = [ 'error', "Failed deleting comment {$comment_id}." ]; + } else { + $verb = ( $force || 'trash' === $comment_status ) ? 'Deleted' : 'Trashed'; + $response = [ 'success', "{$verb} comment {$comment_id}." ]; + ++$successfully_deleted; } - ); + + $status = $this->success_or_failure( $response ); + if ( $status ) { + ++$successfully_deleted; + } + } + + if ( $defer_term_counting ) { + wp_defer_term_counting( false ); + } + + if ( $status ) { + WP_CLI::success( "Deleted {$successfully_deleted} comments." ); + } else { + $error_count = $total - $successfully_deleted; + WP_CLI::error( "Failed deleting {$error_count} comments." ); + } + + exit( 0 === $status ? 0 : 1 ); } private function call( $args, $status, $success, $failure ) { From 3af6933b9599dfcb2df7b906874fb74999c656e7 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 16:24:11 -0400 Subject: [PATCH 11/34] fix: strict comparison on mixed --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index fe16dffe9..5baa36fd4 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -498,7 +498,7 @@ public function delete( $args, $assoc_args ) { WP_CLI::error( "Failed deleting {$error_count} comments." ); } - exit( 0 === $status ? 0 : 1 ); + exit( 0 == $status ? 0 : 1 ); } private function call( $args, $status, $success, $failure ) { From 3c9ea3c863de7c7495ced13e3eb542a2cf9796ea Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 16:33:18 -0400 Subject: [PATCH 12/34] fix: strict comparison on mixed --- src/Comment_Command.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 5baa36fd4..d589883bb 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -498,7 +498,11 @@ public function delete( $args, $assoc_args ) { WP_CLI::error( "Failed deleting {$error_count} comments." ); } - exit( 0 == $status ? 0 : 1 ); + if ( $status ) { + exit( 0 ); + } else { + exit( 1 ); + } } private function call( $args, $status, $success, $failure ) { From 95e7211612ea1e30c7d3ccd4b36e8f2feb5717a7 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 16:41:50 -0400 Subject: [PATCH 13/34] fix: phpcs comparison --- src/Comment_Command.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index d589883bb..8b604feba 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -475,14 +475,12 @@ public function delete( $args, $assoc_args ) { if ( ! $result ) { $response = [ 'error', "Failed deleting comment {$comment_id}." ]; + $status = $this->success_or_failure( $response ); + // Keep status as 1 (error) if any deletion fails } else { $verb = ( $force || 'trash' === $comment_status ) ? 'Deleted' : 'Trashed'; $response = [ 'success', "{$verb} comment {$comment_id}." ]; - ++$successfully_deleted; - } - - $status = $this->success_or_failure( $response ); - if ( $status ) { + $this->success_or_failure( $response ); ++$successfully_deleted; } } @@ -491,18 +489,14 @@ public function delete( $args, $assoc_args ) { wp_defer_term_counting( false ); } - if ( $status ) { + if ( 0 === $status ) { WP_CLI::success( "Deleted {$successfully_deleted} comments." ); } else { $error_count = $total - $successfully_deleted; WP_CLI::error( "Failed deleting {$error_count} comments." ); } - if ( $status ) { - exit( 0 ); - } else { - exit( 1 ); - } + exit( $status ); } private function call( $args, $status, $success, $failure ) { From 1e396e856f7aa1b674cc14bdb62241fd12779f0c Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 16:55:46 -0400 Subject: [PATCH 14/34] fix: behat tests. Add checks for multiple success lines --- features/comment.feature | 27 +++++++++++++++++++++++++++ src/Comment_Command.php | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/features/comment.feature b/features/comment.feature index c970e2c9f..dc6256f99 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -555,6 +555,33 @@ Feature: Manage WordPress comments Success: No comments deleted. """ + Scenario: Delete multiple comments shows summary message + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` + And save STDOUT as {COMMENT_ID_1} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` + And save STDOUT as {COMMENT_ID_2} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 3' --porcelain` + And save STDOUT as {COMMENT_ID_3} + + When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} {COMMENT_ID_3}` + Then STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_2}. + """ + And STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_3}. + """ + And STDOUT should contain: + """ + Success: Deleted 3 comments. + """ + Scenario: Error when no comment IDs and no --all flag provided Given a WP install diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 8b604feba..99e69da1d 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -490,7 +490,9 @@ public function delete( $args, $assoc_args ) { } if ( 0 === $status ) { - WP_CLI::success( "Deleted {$successfully_deleted} comments." ); + if ( $total > 1 ) { + WP_CLI::success( "Deleted {$successfully_deleted} comments." ); + } } else { $error_count = $total - $successfully_deleted; WP_CLI::error( "Failed deleting {$error_count} comments." ); From a7849ca429de5fce309a13d180c2c375f2ff19e9 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 17:05:39 -0400 Subject: [PATCH 15/34] fix: behat tests. Add checks for multiple success lines --- features/comment.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/features/comment.feature b/features/comment.feature index dc6256f99..4ffbb5252 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -55,6 +55,7 @@ Feature: Manage WordPress comments """ Success: Trashed comment 3. Success: Trashed comment 4. + Success: Deleted 2 comments. """ When I run `wp comment delete 3` From 669caaeb5f324b04b2ba22830e46502886824358 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 19:42:35 -0400 Subject: [PATCH 16/34] test: Delete comments with explicit defer-term-counting flag --- features/comment.feature | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/features/comment.feature b/features/comment.feature index 4ffbb5252..cb36a6bdd 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -583,6 +583,27 @@ Feature: Manage WordPress comments Success: Deleted 3 comments. """ + Scenario: Delete comments with explicit defer-term-counting flag + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` + And save STDOUT as {COMMENT_ID_1} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` + And save STDOUT as {COMMENT_ID_2} + + When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} --defer-term-counting` + Then STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_2}. + """ + And STDOUT should contain: + """ + Success: Deleted 2 comments. + """ + Scenario: Error when no comment IDs and no --all flag provided Given a WP install From 8a3df5b4fa73b6745527e815051a1a561379a137 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 19:46:24 -0400 Subject: [PATCH 17/34] test: Success/Error returns --- features/comment.feature | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/features/comment.feature b/features/comment.feature index cb36a6bdd..ee649ec48 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -604,6 +604,32 @@ Feature: Manage WordPress comments Success: Deleted 2 comments. """ + Scenario: Delete comments with mixed success and failure + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` + And save STDOUT as {COMMENT_ID_1} + And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` + And save STDOUT as {COMMENT_ID_2} + + When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` + Then STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ + Success: Trashed comment {COMMENT_ID_2}. + """ + And STDERR should contain: + """ + Warning: Failed deleting comment 99999. + """ + And STDERR should contain: + """ + Error: Failed deleting 1 comments. + """ + And the return code should be 1 + Scenario: Error when no comment IDs and no --all flag provided Given a WP install From db908895849b57b3d7e690ff037c93e1041b40a9 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 20:04:37 -0400 Subject: [PATCH 18/34] fix: remove --defer-comment-count flag, not referenced in coommand def --- features/comment.feature | 21 --------------------- src/Comment_Command.php | 13 ++++--------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index ee649ec48..4806463f5 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -583,27 +583,6 @@ Feature: Manage WordPress comments Success: Deleted 3 comments. """ - Scenario: Delete comments with explicit defer-term-counting flag - Given a WP install - And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` - And save STDOUT as {COMMENT_ID_1} - And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` - And save STDOUT as {COMMENT_ID_2} - - When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} --defer-term-counting` - Then STDOUT should contain: - """ - Success: Trashed comment {COMMENT_ID_1}. - """ - And STDOUT should contain: - """ - Success: Trashed comment {COMMENT_ID_2}. - """ - And STDOUT should contain: - """ - Success: Deleted 2 comments. - """ - Scenario: Delete comments with mixed success and failure Given a WP install And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 1' --porcelain` diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 99e69da1d..6806ed064 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -451,15 +451,10 @@ public function delete( $args, $assoc_args ) { return; } - $defer_term_counting = Utils\get_flag_value( $assoc_args, 'defer-term-counting', false ); - - if ( $all && ! $defer_term_counting ) { - $assoc_args['defer-term-counting'] = true; - } - $status = 0; - if ( $defer_term_counting ) { + $defer_term_counting = wp_defer_comment_counting(); + if ( $all ) { wp_defer_term_counting( true ); } @@ -485,8 +480,8 @@ public function delete( $args, $assoc_args ) { } } - if ( $defer_term_counting ) { - wp_defer_term_counting( false ); + if ( $all ) { + wp_defer_term_counting( $defer_term_counting ); } if ( 0 === $status ) { From 1c8144ceb8249d48a51ab02b619be103e0c26bf3 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 20:23:46 -0400 Subject: [PATCH 19/34] test: return to STDERR --- features/comment.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/comment.feature b/features/comment.feature index 4806463f5..3a950c748 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -603,7 +603,7 @@ Feature: Manage WordPress comments """ Warning: Failed deleting comment 99999. """ - And STDERR should contain: + And STDOUT should contain: """ Error: Failed deleting 1 comments. """ From 37c0ba617959fe07f113357b71941baf7d0aa1ef Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 20:37:51 -0400 Subject: [PATCH 20/34] test: combined success/warning output to STDOUT --- features/comment.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/comment.feature b/features/comment.feature index 3a950c748..d7111e849 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -599,7 +599,7 @@ Feature: Manage WordPress comments """ Success: Trashed comment {COMMENT_ID_2}. """ - And STDERR should contain: + And STDOUT should contain: """ Warning: Failed deleting comment 99999. """ From 61f74b6bada8df1e600a0b9cbb0f918bf025ec10 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 20:49:05 -0400 Subject: [PATCH 21/34] test: trying to get combined success/fail ouput --- features/comment.feature | 9 --------- 1 file changed, 9 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index d7111e849..b65304161 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -594,17 +594,8 @@ Feature: Manage WordPress comments Then STDOUT should contain: """ Success: Trashed comment {COMMENT_ID_1}. - """ - And STDOUT should contain: - """ Success: Trashed comment {COMMENT_ID_2}. - """ - And STDOUT should contain: - """ Warning: Failed deleting comment 99999. - """ - And STDOUT should contain: - """ Error: Failed deleting 1 comments. """ And the return code should be 1 From d33e0b8da1b5c14b0d8553f507d21007ecdbb050 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 21:08:38 -0400 Subject: [PATCH 22/34] test: add blank line to return --- features/comment.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/features/comment.feature b/features/comment.feature index b65304161..d681103da 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -595,6 +595,7 @@ Feature: Manage WordPress comments """ Success: Trashed comment {COMMENT_ID_1}. Success: Trashed comment {COMMENT_ID_2}. + Warning: Failed deleting comment 99999. Error: Failed deleting 1 comments. """ From fc0e2a20f9d375f2b223b20793ebfe58ff4f2040 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 21:25:24 -0400 Subject: [PATCH 23/34] test: could it be STDERR, now that we have the foramtting fixed? --- features/comment.feature | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index d681103da..cb6f9df20 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -591,11 +591,13 @@ Feature: Manage WordPress comments And save STDOUT as {COMMENT_ID_2} When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` - Then STDOUT should contain: + Then STDOUT should be: """ Success: Trashed comment {COMMENT_ID_1}. Success: Trashed comment {COMMENT_ID_2}. - + """ + And STDERR should be: + """ Warning: Failed deleting comment 99999. Error: Failed deleting 1 comments. """ From fb2d6bec8304306010d434e7079e9323465658fb Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 21:39:02 -0400 Subject: [PATCH 24/34] test: Separates STDOUT and STDERR --- features/comment.feature | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index cb6f9df20..4806463f5 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -591,14 +591,20 @@ Feature: Manage WordPress comments And save STDOUT as {COMMENT_ID_2} When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` - Then STDOUT should be: + Then STDOUT should contain: """ Success: Trashed comment {COMMENT_ID_1}. + """ + And STDOUT should contain: + """ Success: Trashed comment {COMMENT_ID_2}. """ - And STDERR should be: + And STDERR should contain: """ Warning: Failed deleting comment 99999. + """ + And STDERR should contain: + """ Error: Failed deleting 1 comments. """ And the return code should be 1 From d0dd60a2d7c0d0f8a7ea5e1bf41d5c7bb2090308 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 22:44:31 -0400 Subject: [PATCH 25/34] Last try on STDOUT --- features/comment.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index 4806463f5..d7111e849 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -599,11 +599,11 @@ Feature: Manage WordPress comments """ Success: Trashed comment {COMMENT_ID_2}. """ - And STDERR should contain: + And STDOUT should contain: """ Warning: Failed deleting comment 99999. """ - And STDERR should contain: + And STDOUT should contain: """ Error: Failed deleting 1 comments. """ From a8bdff2bd9c812516a314530d5568b297e689078 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Mon, 20 Oct 2025 23:33:27 -0400 Subject: [PATCH 26/34] test: ah, the problem was I try vs I run --- features/comment.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/comment.feature b/features/comment.feature index d7111e849..a1cc7c0b5 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -590,7 +590,7 @@ Feature: Manage WordPress comments And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` And save STDOUT as {COMMENT_ID_2} - When I run `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` + When I try `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` Then STDOUT should contain: """ Success: Trashed comment {COMMENT_ID_1}. @@ -599,11 +599,11 @@ Feature: Manage WordPress comments """ Success: Trashed comment {COMMENT_ID_2}. """ - And STDOUT should contain: + And STDERR should contain: """ Warning: Failed deleting comment 99999. """ - And STDOUT should contain: + And STDERR should contain: """ Error: Failed deleting 1 comments. """ From c26733a0e5f5a19d9b5fb838f34af6f807ab479e Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 28 Oct 2025 17:28:25 -0400 Subject: [PATCH 27/34] revert: comment change --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 6806ed064..1b2257828 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -16,7 +16,7 @@ * # Update an existing comment. * $ wp comment update 123 --comment_author='That Guy' * Success: Updated comment 123. - + * * # Delete an existing comment. * $ wp comment delete 1337 --force * Success: Deleted comment 1337. From 6efbeed4f809773a5606e863bd7eff09e4b5c35f Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 28 Oct 2025 17:31:30 -0400 Subject: [PATCH 28/34] lint: use @var instead of not is_array check --- src/Comment_Command.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 1b2257828..370914017 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -808,6 +808,8 @@ private function check_optional_args_and_all( $args, $all, $verb = 'delete' ) { */ private function get_all_comment_ids(): array { $query = new WP_Comment_Query(); + + /** @var array $comments */ $comments = $query->query( array( 'fields' => 'ids', @@ -815,10 +817,6 @@ private function get_all_comment_ids(): array { ) ); - if ( ! is_array( $comments ) ) { - return []; - } - return $comments; } } From 3c52d46839d2c10c5b47f45133c665d66f063ec9 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 28 Oct 2025 17:33:15 -0400 Subject: [PATCH 29/34] doc: include warning about potential slow command --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 370914017..e185f1774 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -420,7 +420,7 @@ function ( $comment ) { * : One or more IDs of comments to delete. * * [--all] - * : If set, all comments will be deleted. + * : If set, all comments will be deleted. This command might be slow on a site with lots of comments. * * [--force] * : Skip the trash bin. From bc3cc1def71968aa115fcd3ccef7b298b0753ee1 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 28 Oct 2025 17:36:40 -0400 Subject: [PATCH 30/34] lint: remove additional spaces --- src/Comment_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index e185f1774..c34c8b049 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -807,7 +807,7 @@ private function check_optional_args_and_all( $args, $all, $verb = 'delete' ) { * Gets all available comment IDs. */ private function get_all_comment_ids(): array { - $query = new WP_Comment_Query(); + $query = new WP_Comment_Query(); /** @var array $comments */ $comments = $query->query( From 342b555d391cf6b82b80c3debfc31e157086b1d9 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 13:19:49 +0000 Subject: [PATCH 31/34] Defer comment counting, not term counting, in comment delete --all wp_defer_term_counting() was called by mistake, so comment counts were still recalculated after every deletion and the term counting flag was restored to the comment counting state. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- src/Comment_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index c8a60cd95..1812ba97d 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -684,9 +684,9 @@ public function delete( $args, $assoc_args ) { $status = 0; - $defer_term_counting = wp_defer_comment_counting(); + $defer_comment_counting = wp_defer_comment_counting(); if ( $all ) { - wp_defer_term_counting( true ); + wp_defer_comment_counting( true ); } $total = count( $args ); @@ -712,7 +712,7 @@ public function delete( $args, $assoc_args ) { } if ( $all ) { - wp_defer_term_counting( $defer_term_counting ); + wp_defer_comment_counting( $defer_comment_counting ); } if ( 0 === $status ) { From 26c6f8c36dfb8e783d22595ab77f455b940071e2 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 15:24:45 +0000 Subject: [PATCH 32/34] Fix PHPStan error in comment delete --all Narrow the `@var` annotation for the comment ID query result to `list>`, which is a subtype of the query's declared return type, and align the docblock example's summary line with the actual output. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- src/Comment_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 1812ba97d..57177818c 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -671,7 +671,7 @@ private function query_comments_in_chunks( $query_args, $need_url ) { * $ wp comment delete --all --force * Success: Deleted comment 1337. * Success: Deleted comment 2341. - * Success: Deleted 2 of 2 comments. + * Success: Deleted 2 comments. */ public function delete( $args, $assoc_args ) { $all = Utils\get_flag_value( $assoc_args, 'all', false ); @@ -1036,11 +1036,13 @@ private function check_optional_args_and_all( $args, $all, $verb = 'delete' ) { /** * Gets all available comment IDs. + * + * @return list> */ private function get_all_comment_ids(): array { $query = new WP_Comment_Query(); - /** @var array $comments */ + /** @var list> $comments */ $comments = $query->query( array( 'fields' => 'ids', From 4b1fd3c3003f88b8a77930f4a65384d06dc410b1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Nov 2026 11:11:05 +0000 Subject: [PATCH 33/34] Include spam and trashed comments in comment delete --all WP_Comment_Query only returns approved and pending comments by default, so spam and trashed comments were left behind. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- features/comment.feature | 39 +++++++++++++++++++++++++++++++++++++++ src/Comment_Command.php | 3 ++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/features/comment.feature b/features/comment.feature index c321fcf71..3b7010238 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -551,6 +551,45 @@ Feature: Manage WordPress comments 0 """ + Scenario: Delete all comments with --all flag includes spam and trashed comments + Given a WP install + And I run `wp comment create --comment_post_ID=1 --comment_content='Spam comment' --comment_approved=spam --porcelain` + And save STDOUT as {SPAM_ID} + And I run `wp comment create --comment_post_ID=1 --comment_content='Trashed comment' --comment_approved=trash --porcelain` + And save STDOUT as {TRASH_ID} + And I run `wp comment create --comment_post_ID=1 --comment_content='Pending comment' --comment_approved=0 --porcelain` + And save STDOUT as {PENDING_ID} + + When I run `wp comment delete --all --force` + Then STDOUT should contain: + """ + Success: Deleted comment {SPAM_ID}. + """ + And STDOUT should contain: + """ + Success: Deleted comment {TRASH_ID}. + """ + And STDOUT should contain: + """ + Success: Deleted comment {PENDING_ID}. + """ + And STDOUT should contain: + """ + Success: Deleted 4 comments. + """ + + When I run `wp comment list --status=spam --format=count` + Then STDOUT should be: + """ + 0 + """ + + When I run `wp comment list --status=trash --format=count` + Then STDOUT should be: + """ + 0 + """ + Scenario: Delete all comments when no comments exist Given a WP install And I run `wp comment delete $(wp comment list --field=ID) --force` diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 57177818c..908b1a8e0 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -674,7 +674,7 @@ private function query_comments_in_chunks( $query_args, $need_url ) { * Success: Deleted 2 comments. */ public function delete( $args, $assoc_args ) { - $all = Utils\get_flag_value( $assoc_args, 'all', false ); + $all = (bool) Utils\get_flag_value( $assoc_args, 'all', false ); // Check if comment IDs or --all is passed. $args = $this->check_optional_args_and_all( $args, $all, 'delete' ); @@ -1047,6 +1047,7 @@ private function get_all_comment_ids(): array { array( 'fields' => 'ids', 'number' => 0, // Get all comments + 'status' => 'any', // Include spam and trashed comments. ) ); From e7741641a61c50447db10a4df4ce40617c9cbc47 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Nov 2026 11:53:47 +0000 Subject: [PATCH 34/34] Put the failing ID first in the mixed comment delete scenario With the failing ID last, the return code assertion also passed if only the last deletion's result set the exit status. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx --- features/comment.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/comment.feature b/features/comment.feature index 3b7010238..005c8b0f8 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -634,7 +634,7 @@ Feature: Manage WordPress comments And I run `wp comment create --comment_post_ID=1 --comment_content='Comment 2' --porcelain` And save STDOUT as {COMMENT_ID_2} - When I try `wp comment delete {COMMENT_ID_1} {COMMENT_ID_2} 99999` + When I try `wp comment delete 99999 {COMMENT_ID_1} {COMMENT_ID_2}` Then STDOUT should contain: """ Success: Trashed comment {COMMENT_ID_1}.