[wp-trac] [WordPress Trac] #19903: wp_count_comments() and get_comments_count() both do SQL queries

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


#19903: wp_count_comments() and get_comments_count() both do SQL queries
--------------------------+-----------------------------
 Reporter:  markjaquith   |       Owner:
     Type:  enhancement   |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  Comments      |     Version:  3.3.1
 Severity:  normal        |  Resolution:
 Keywords:  dev-feedback  |     Focuses:  performance
--------------------------+-----------------------------

Comment (by afercia):

 Came across this ticket while investigating for #32362. About
 `get_comment_count()` would recommend to fix it or deprecate it :) The
 thing is, it can return wrong results and it does that when you have
 "post-trashed" or "trashed" comments. Wondering also no one complained in
 the last 3 years so maybe +1 for deprecating it. Anyway, please let me do
 a quick example, dumping the 2 functions here's what I get in my local
 environment:
 {{{
 wp_count_comments() dump:

 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)
 }

 get_comment_count() dump:

 array(4) {
   ["approved"] => string(2) "34"
   ["awaiting_moderation"] => string(1) "8"
   ["spam"] => string(2) "14"
   ["total_comments"] => int(64)
 }
 }}}

 where `get_comment_count()` returns a wrong value for
 "awaiting_moderation". The "total_comments" is correct but includes also
 "post-trashed" and "trashed", that's inconsistent with what
 `wp_count_comments()` does.

 `get_comment_count()` uses a switch statement which does loose comparison.
 Possible values for 'comment_approved' are: `'0', '1', 'post-trashed',
 'spam', 'trash'` in this order.
 In PHP, [http://php.net/manual/en/types.comparisons.php#types
 .comparisions-loose 0 == 'string' returns true]. Thus, `case 0` will catch
 all the comments that are `'0'`, `'post-trashed'` and `'trash'` and the
 value returned for "awaiting_moderation" will be the last one which
 overwrites the previous ones. Will work only if you don't have post-
 trashed or trashed comments. Not arguing, I can understand this is
 probably for historical reasons since those two statuses were added in a
 second time and maybe something was missed on the road, type-checking
 included ;)

--
Ticket URL: <https://core.trac.wordpress.org/ticket/19903#comment:10>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list