diff --git a/features/comment.feature b/features/comment.feature index b5d18e9e7..005c8b0f8 100644 --- a/features/comment.feature +++ b/features/comment.feature @@ -57,6 +57,7 @@ Feature: Manage WordPress comments """ Success: Trashed comment 3. Success: Trashed comment 4. + Success: Deleted 2 comments. """ When I run `wp comment delete 3` @@ -478,6 +479,190 @@ Feature: Manage WordPress comments 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 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}. + """ + + 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 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}. + """ + + When I run `wp comment list --format=count` + Then STDOUT should be: + """ + 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` + + When I run `wp comment delete --all` + Then STDOUT should be: + """ + 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: 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 try `wp comment delete 99999 {COMMENT_ID_1} {COMMENT_ID_2}` + 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 + + 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 + Scenario: List more comments than are loaded from the database at a time Given a WP install And I run `wp comment generate --count=1200 --post_id=1` diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 3331e04ae..908b1a8e0 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -647,9 +647,12 @@ private function query_comments_in_chunks( $query_args, $need_url ) { * * ## OPTIONS * - * ... + * [...] * : One or more IDs of comments to delete. * + * [--all] + * : If set, all comments will be deleted. This command might be slow on a site with lots of comments. + * * [--force] * : Skip the trash bin. * @@ -663,25 +666,65 @@ private function query_comments_in_chunks( $query_args, $need_url ) { * $ 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 comments. */ public function delete( $args, $assoc_args ) { - parent::_delete( - $args, - $assoc_args, - function ( $comment_id, $assoc_args ) { - $force = (bool) Utils\get_flag_value( $assoc_args, 'force' ); + $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' ); + if ( ! $args ) { + return; + } - $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}." ]; - } + $defer_comment_counting = wp_defer_comment_counting(); + if ( $all ) { + wp_defer_comment_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}." ]; + $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}." ]; + $this->success_or_failure( $response ); + ++$successfully_deleted; } - ); + } + + if ( $all ) { + wp_defer_comment_counting( $defer_comment_counting ); + } + + if ( 0 === $status ) { + if ( $total > 1 ) { + WP_CLI::success( "Deleted {$successfully_deleted} comments." ); + } + } else { + $error_count = $total - $successfully_deleted; + WP_CLI::error( "Failed deleting {$error_count} comments." ); + } + + exit( $status ); } private function call( $args, $status, $success, $failure ) { @@ -965,4 +1008,49 @@ 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'. + * @return array Same as $args if not all, otherwise all comment IDs. + */ + private function check_optional_args_and_all( $args, $all, $verb = 'delete' ) { + if ( $all ) { + $args = $this->get_all_comment_ids(); + } + + 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 list> + */ + private function get_all_comment_ids(): array { + $query = new WP_Comment_Query(); + + /** @var list> $comments */ + $comments = $query->query( + array( + 'fields' => 'ids', + 'number' => 0, // Get all comments + 'status' => 'any', // Include spam and trashed comments. + ) + ); + + return $comments; + } }