Skip to content

Commit 14dc525

Browse files
obenlandpfefferle
andauthored
Comments: Account for custom comment types when calculating comment counts (#1073)
* Comments: Account for custom comment types when calculating comment counts Fixes #1069. * support paging --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent e7fa73c commit 14dc525

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
* `icon` support for `Audio` and `Video` attachments
1313
* Send "new follower" emails
1414
* Send "direct message" emails
15+
* Account for custom comment types when calculating comment counts
1516

1617
### Improved
1718

includes/class-comment.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static function init() {
3030
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
3131
\add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 );
3232
\add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 );
33+
\add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
3334
}
3435

3536
/**
@@ -670,10 +671,6 @@ public static function comment_query( $query ) {
670671
return;
671672
}
672673

673-
if ( isset( $query->query_vars['count'] ) && true === $query->query_vars['count'] ) {
674-
return;
675-
}
676-
677674
// Exclude likes and reposts by the ActivityPub plugin.
678675
$query->query_vars['type__not_in'] = self::get_comment_type_names();
679676
}
@@ -715,4 +712,27 @@ public static function pre_comment_approved( $approved, $commentdata ) {
715712

716713
return $approved;
717714
}
715+
716+
/**
717+
* Filters the comment count to exclude ActivityPub comment types.
718+
*
719+
* @param int|null $new_count The new comment count. Default null.
720+
* @param int $old_count The old comment count.
721+
* @param int $post_id Post ID.
722+
*
723+
* @return int|null The updated comment count, or null to use the default query.
724+
*/
725+
public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) {
726+
if ( null === $new_count ) {
727+
global $wpdb;
728+
729+
$excluded_types = self::get_comment_type_names();
730+
731+
// phpcs:ignore WordPress.DB
732+
$new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ('" . implode( "','", $excluded_types ) . "')", $post_id ) );
733+
734+
}
735+
736+
return $new_count;
737+
}
718738
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ For reasons of data protection, it is not possible to see the followers of other
137137
* Added: `icon` support for `Audio` and `Video` attachments
138138
* Added: Send "new follower" emails
139139
* Added: Send "direct message" emails
140+
* Added: Account for custom comment types when calculating comment counts
140141
* Improved: Email templates for Likes and Reposts
141142
* Improved: Interactions moderation
142143
* Improved: Compatibility with Akismet

tests/includes/class-test-comment.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ public function test_pre_comment_approved() {
234234
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
235235
}
236236

237+
/**
238+
* Test pre_wp_update_comment_count_now.
239+
*
240+
* @covers ::pre_wp_update_comment_count_now
241+
*/
242+
public function test_pre_wp_update_comment_count_now() {
243+
$post_id = self::factory()->post->create();
244+
245+
// Case 1: $new is null, no approved comments of non-ActivityPub types.
246+
$this->assertSame( 0, Comment::pre_wp_update_comment_count_now( null, 0, $post_id ) );
247+
248+
// Case 2: $new is null, approved comments of non-ActivityPub types exist.
249+
self::factory()->comment->create_post_comments( $post_id, 2, array( 'comment_approved' => '1' ) );
250+
$this->assertSame( 2, Comment::pre_wp_update_comment_count_now( null, 0, $post_id ) );
251+
252+
// Case 3: $new is null, mix of ActivityPub and non-ActivityPub approved comments.
253+
self::factory()->comment->create_post_comments(
254+
$post_id,
255+
3,
256+
array(
257+
'comment_approved' => '1',
258+
'comment_type' => 'like',
259+
)
260+
);
261+
self::factory()->comment->create_post_comments( $post_id, 3, array( 'comment_approved' => '1' ) );
262+
$this->assertSame( 5, Comment::pre_wp_update_comment_count_now( null, 0, $post_id ) );
263+
264+
// Case 4: $new is not null, should return $new unmodified.
265+
$this->assertSame( 10, Comment::pre_wp_update_comment_count_now( 10, 0, $post_id ) );
266+
}
267+
237268
/**
238269
* Data provider for test_check_ability_to_federate_comment.
239270
*/

0 commit comments

Comments
 (0)