From 9be7d88eb738013dfe1a9a6634ac96f5f2fd5cba Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 14 Feb 2024 10:20:57 +0545 Subject: [PATCH 1/3] Fix comment recount output --- src/Comment_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Comment_Command.php b/src/Comment_Command.php index aa2846ca..2b7172c2 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -672,12 +672,12 @@ public function count( $args, $assoc_args ) { */ public function recount( $args ) { foreach ( $args as $id ) { - wp_update_comment_count( $id ); $post = get_post( $id ); if ( $post ) { + wp_update_comment_count( $id ); WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." ); } else { - WP_CLI::warning( "Post {$post->ID} doesn't exist." ); + WP_CLI::warning( "Post {$id} doesn't exist." ); } } } From e380ef2104fc8f9fc7b77e916ea2032c668d0e1a Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 14 Feb 2024 10:26:02 +0545 Subject: [PATCH 2/3] Add behat test for invalid id for comment recount --- features/comment-recount.feature | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/features/comment-recount.feature b/features/comment-recount.feature index 7208b97b..332fbe9e 100644 --- a/features/comment-recount.feature +++ b/features/comment-recount.feature @@ -23,3 +23,9 @@ Feature: Recount comments on a post """ Updated post 1 comment count to 3. """ + + When I run `wp comment recount 99999999` + Then STDOUT should be: + """ + Warning: Post 99999999 doesn't exist. + """ From 0ddfaaa7cabb01c2d50bf00319722c96c3c5fa54 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 14 Feb 2024 10:45:41 +0545 Subject: [PATCH 3/3] Fix condition for comment count check --- features/comment-recount.feature | 4 ++-- src/Comment_Command.php | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/features/comment-recount.feature b/features/comment-recount.feature index 332fbe9e..538f235c 100644 --- a/features/comment-recount.feature +++ b/features/comment-recount.feature @@ -24,8 +24,8 @@ Feature: Recount comments on a post Updated post 1 comment count to 3. """ - When I run `wp comment recount 99999999` - Then STDOUT should be: + When I try `wp comment recount 99999999` + Then STDERR should be: """ Warning: Post 99999999 doesn't exist. """ diff --git a/src/Comment_Command.php b/src/Comment_Command.php index 2b7172c2..1ff6b361 100644 --- a/src/Comment_Command.php +++ b/src/Comment_Command.php @@ -672,9 +672,8 @@ public function count( $args, $assoc_args ) { */ public function recount( $args ) { foreach ( $args as $id ) { - $post = get_post( $id ); - if ( $post ) { - wp_update_comment_count( $id ); + if ( wp_update_comment_count( $id ) ) { + $post = get_post( $id ); WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." ); } else { WP_CLI::warning( "Post {$id} doesn't exist." );