[wp-trac] [WordPress Trac] #32362: Comments count: wp_count_comments() should not count spam toward totals

WordPress Trac noreply at wordpress.org
Tue May 12 17:05:05 UTC 2015


#32362: Comments count: wp_count_comments() should not count spam toward totals
--------------------------+-----------------------------
 Reporter:  afercia       |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Comments      |    Version:  4.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Or at least shouldn't be used to update the "displaying items" count the
 way it is now. Quickly discussed with @boone on Slack, going to provide an
 example, trying to do my best. In the Comments screen, the default view
 shows just the approved + pending comments. Say `wp_count_comments()`
 returns the following result (that's the comments count in my local
 environment):
 {{{
 object(stdClass)#339 (6) {
   ["moderated"] => string(1) "6"
   ["approved"] => string(2) "34"
   ["post-trashed"] => string(1) "2"
   ["spam"] => string(2) "14"
   ["trash"] => string(1) "8"
   ["total_comments"] => int(54)
 }
 }}}

 and the "per page" option is 20, the default. The initial view will show
 these numbers, notice the "40 items" count displayed on the right:

 [[Image(https://cldup.com/B4cGksm5u1.png)]]

 Now trash a comment, all the counts will be updated via JavaScript
 avoiding to call `wp_count_comments()` and the highlighted count will
 correctly show "39":

 [[Image(https://cldup.com/SYnC3PZjLJ.png)]]

 Now click on the "undo" link: I should get again "40 items" and instead I
 get "54", the pagination will show 3 pages too:

 [[Image(https://cldup.com/E7m-6vlGQk.png)]]


 Checking `_wp_ajax_delete_comment_response()` see
 https://core.trac.wordpress.org/browser/tags/4.2/src/wp-admin/includes
 /ajax-actions.php#L353 I can see two cases where `wp_count_comments()` is
 called and returns the 'total_comments' count, '''which is not the number
 we need''' here. We need approved + pending while 'total_comments'
 includes spam too. The two cases are:
 - when you're on a "page break" and this is our example case: 40/20 = 0
 - randomly when a generated number between 1 and the `$per_page` value
 will be 1
 the second one looks a bit arbitrary to me and doesn't exactly reflects
 what the comment in the code say ("and about 1 other time per page").
 Anyways, there's always a chance you get the wrong number and that could
 happen on your first click or after several ones.

 Fixing `wp_count_comments()` to exclude "spam" from `'total_comments'`
 would be trivial but I'm not sure if this is used somewhere else or could
 affect plugins.

 Maybe, `wp_count_comments()` should return also a `'all'` count in
 addition to the existing ones ?

 Please also notice that when you're in the "All" view, meaning:
 `/wp-admin/edit-comments.php?comment_status=all`
 things are a bit different since "all" doesn't exist as comment count type
 and thus `_wp_ajax_delete_comment_response` will use the decremented value
 `$total += $delta`. Introducing a "all" count could probably make this a
 bit more consistent.

 Related: #17275, #11200 see also #19903

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32362>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list