Opened 10 years ago
Closed 9 years ago
#32362 closed defect (bug) (fixed)
Comments count: wp_count_comments() should not count spam toward totals
Reported by: | afercia | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | 4.2 |
Component: | Comments | Keywords: | needs-unit-tests has-patch |
Focuses: | Cc: |
Description
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:
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":
Now click on the "undo" link: I should get again "40 items" and instead I get "54", the pagination will show 3 pages too:
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.
Attachments (2)
Change History (16)
#1
follow-up:
↓ 3
@
10 years ago
- Keywords needs-patch needs-unit-tests added
- Milestone changed from Awaiting Review to Future Release
This ticket was mentioned in Slack in #core by afercia. View the logs.
10 years ago
#3
in reply to:
↑ 1
@
9 years ago
Replying to boonebgorges:
So I'd lean toward introducing an
'all'
parameter, and then using it as appropriate in core.
Thanks @boonebgorges :) I could make a patch but definitely can't do unit test. Would greatly appreciate some devs eyes on this.
This ticket was mentioned in Slack in #core by afercia. View the logs.
9 years ago
@
9 years ago
Problem is $total count is 0 and in '_wp_ajax_delete_comment_response' we have check ( ! $total ) )
#6
@
9 years ago
@boonebgorges
total_comments = approved + awaiting_moderation
all = spam + approved + awaiting_moderation
is above equation right for comment count?
#7
@
9 years ago
"all" should be just approved + moderated, that's the whole point of this ticket :)
#8
@
9 years ago
@boonebgorges @afercia
Patch with introducing an 'all' parameter and used it in _wp_ajax_delete_comment_response
.
#9
@
9 years ago
- Milestone changed from Future Release to 4.4
- Owner set to wonderboymusic
- Status changed from new to assigned
#10
@
9 years ago
Thanks @dipesh.kakadiya, can you give me steps to reproduce the bug? I can't seem to get the wrong numbers to show up...
#11
@
9 years ago
@wonderboymusic
- Go to comment page
- From list of comment make any comment as spam
- Then top right side total numer will decrease ( working perfectly)
- After click on
undo
link comment is revert back from spam but on right corner total comment count is not increase.
For more description you can check ticket description.
#12
@
9 years ago
I can't reproduce:
https://wordpress.slack.com/files/wonderboymusic/F0AHGD75J/32362.mov
Can you check the database of the comment you are spamming, and tell me if it has multiple values for _wp_trash_meta_status
?
I poked around the plugin repo a little bit to see what people are doing with
wp_count_comments()
. In many cases, they're not looking at 'total_comments' at all. In many other cases, excluding 'spam' from that count would probably fix bugs in the plugins. That being said, I don't think it's worth breaking backward compatibility for such a widely used function, even if the current functionality is kinda odd.So I'd lean toward introducing an
'all'
parameter, and then using it as appropriate in core.