Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
47cf2c2
Fix: Update the comment count to not include children with unapproved…
hbhalodia Jul 17, 2025
4bef578
Fix: PHPCS feedbacks
hbhalodia Jul 17, 2025
1da3ad1
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Nov 4, 2025
01c7ce9
Add the test case for the updated comment count scenario
hbhalodia Nov 4, 2025
75dbcab
Fix phpcs issue
hbhalodia Nov 4, 2025
158061f
Resolve phpcs multiline issue
hbhalodia Nov 4, 2025
e64afba
Add ticket number to unit test
hbhalodia Nov 4, 2025
1114e45
Merge branch 'trunk' into fix/issue-36409-comment-count
t-hamano Nov 7, 2025
491e833
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Feb 18, 2026
bc41c7e
Address feedbacks
hbhalodia Feb 19, 2026
e4513b8
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Feb 19, 2026
086c901
Fix the tests
hbhalodia Feb 19, 2026
b136e33
Fix test cases
hbhalodia Feb 19, 2026
45d8fd7
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Feb 20, 2026
97a887c
Merge branch 'trunk' into fix/issue-36409-comment-count
westonruter Mar 12, 2026
9129e38
Address the feedbacks on the PR
hbhalodia Mar 13, 2026
6242f4c
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Mar 13, 2026
b6689ff
Fix variable name
hbhalodia Mar 13, 2026
c3cda23
Update to use memomization and reduce time complexity
hbhalodia Mar 13, 2026
e351f6f
Resolve copilot feedbacks
hbhalodia Mar 13, 2026
c97aa78
Cast results to array
hbhalodia Mar 13, 2026
e357266
Merge branch 'trunk' into fix/issue-36409-comment-count
audrasjb Mar 17, 2026
7163df9
Merge branch 'trunk' into fix/issue-36409-comment-count
hbhalodia Mar 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,87 @@ function wp_update_comment_count_now( $post_id ) {
$new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id );

if ( is_null( $new ) ) {
$new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) );
$comments = (array) $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_parent, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_type != 'note'", $post_id ) );
$comments_by_id = array();

// Create a lookup array by comment ID.
foreach ( $comments as $comment ) {
$comments_by_id[ $comment->comment_ID ] = $comment;
}

$comment_count = 0;
$ancestor_approval_cache = array();

foreach ( $comments as $comment ) {
if ( '1' !== $comment->comment_approved ) {
$ancestor_approval_cache[ (int) $comment->comment_ID ] = false;
}
}

foreach ( $comments as $comment ) {

if ( '1' !== $comment->comment_approved ) {
continue;
}

$comment_id = (int) $comment->comment_ID;

// Use cached result if this comment's ancestry was already resolved.
if ( isset( $ancestor_approval_cache[ $comment_id ] ) ) {
if ( $ancestor_approval_cache[ $comment_id ] ) {
++$comment_count;
}
continue;
}

// Walk the ancestor chain, collecting IDs to memoize afterwards.
$chain = array( $comment_id );
$visited = array(
$comment_id => true,
);
$parent_id = (int) $comment->comment_parent;
$has_unapproved = false;

while ( 0 !== $parent_id ) {
if ( isset( $visited[ $parent_id ] ) ) {
$has_unapproved = true;
break;
}

if ( isset( $ancestor_approval_cache[ $parent_id ] ) ) {
if ( ! $ancestor_approval_cache[ $parent_id ] ) {
$has_unapproved = true;
}
break;
}

if ( ! isset( $comments_by_id[ $parent_id ] ) ) {
$has_unapproved = true;
Comment thread
hbhalodia marked this conversation as resolved.
break;
Comment thread
hbhalodia marked this conversation as resolved.
}

$parent_comment = $comments_by_id[ $parent_id ];

if ( '1' !== $parent_comment->comment_approved ) {
$has_unapproved = true;
break;
}

$visited[ $parent_id ] = true;
$chain[] = $parent_id;
$parent_id = (int) $parent_comment->comment_parent;
}

foreach ( $chain as $chain_id ) {
$ancestor_approval_cache[ $chain_id ] = ! $has_unapproved;
}

if ( ! $has_unapproved ) {
++$comment_count;
}
}

$new = $comment_count;
} else {
$new = (int) $new;
}
Expand Down
122 changes: 122 additions & 0 deletions tests/phpunit/tests/comment/wpUpdateCommentCountNow.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,126 @@ public function test_only_approved_regular_comments_are_counted() {
public function _return_100() {
return 100;
}

/**
* Test case where a trashed parent comment causes its child comments to be excluded from the comment count.
*
* @ticket 36409
*/
public function test_trashed_parent_comment_excludes_child_comments_from_count() {
$post_id = self::factory()->post->create();

// Create 2 top-level comments, 2 child comments for the first top-level comment, and a grandchild of that first comment.
$parent_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => 1,
)
);

$child_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $parent_comment_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $parent_comment_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $child_comment_id,
'comment_approved' => 1,
)
);

$this->assertTrue( wp_update_comment_count_now( $post_id ) );
$this->assertSame( '5', get_comments_number( $post_id ) );

wp_update_comment(
array(
'comment_ID' => $parent_comment_id,
'comment_approved' => 'trash',
)
);

$this->assertTrue( wp_update_comment_count_now( $post_id ) );
$this->assertSame( '1', get_comments_number( $post_id ) );
}

/**
* Test case where an unapproved parent comment causes its child comments to be excluded from the comment count.
*
* @ticket 36409
*/
Comment thread
hbhalodia marked this conversation as resolved.
public function test_unapproved_parent_comment_excludes_child_comments_from_count() {
$post_id = self::factory()->post->create();

// Create 2 top-level comments, 2 child comments for the first top-level comment, and a grandchild of that first comment.
$parent_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_approved' => 1,
)
);

$child_comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $parent_comment_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $parent_comment_id,
'comment_approved' => 1,
)
);

self::factory()->comment->create(
array(
'comment_post_ID' => $post_id,
'comment_parent' => $child_comment_id,
'comment_approved' => 1,
)
);

$this->assertTrue( wp_update_comment_count_now( $post_id ) );
$this->assertSame( '5', get_comments_number( $post_id ) );

wp_update_comment(
array(
'comment_ID' => $parent_comment_id,
'comment_approved' => '0',
)
);

$this->assertTrue( wp_update_comment_count_now( $post_id ) );
$this->assertSame( '1', get_comments_number( $post_id ) );
}
}
Loading