[wp-trac] [WordPress Trac] #35192: Comments_clauses filter

WordPress Trac noreply at wordpress.org
Thu Jan 7 08:26:07 UTC 2016


#35192: Comments_clauses filter
--------------------------+--------------------
 Reporter:  firebird75    |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  4.4.2
Component:  Comments      |     Version:  4.4
 Severity:  normal        |  Resolution:
 Keywords:  2nd-opinion   |     Focuses:
--------------------------+--------------------

Comment (by firebird75):

 The code is below. It works fine with WP 4.4 with the hacks I have put but
 it isn't clean as it will throw an SQL error in the fill_descendants query
 (because of the unability to filter there).

 So first, let me tell you that I really appreciate the fact that you are
 looking into this ticket so quickly!

 Then, if you want my honest feedback, I believe we need to keep it as
 simple as possible. The way the comments display code has been modified in
 WP 4.4 isn't really simple. I had a lot of fun to understand the code
 logic :)

 Your patch will probably work but it makes it even more complex to
 understand :)

 I am wondering if it is really a good idea to have a quick fix put in WP
 4.4.2 now. I have a workaround that works. It isn't good and clean but it
 works. Maybe we should take the time to have a more complete code revisit
 in WP 4.5 rather than having multiple changes that will puzzle a lot of
 plugin authors...

 My suggestion would be to have one and only one query for comments
 display. Having multiple queries has several drawbacks :
 - more load on the server
 - more risks to miss something for plugin authors and never notice this
 (the problem here only showed for nested comments which isn't the majority
 of sites right now)
 - more complex queries to create via filters

 Then if you still want to keep the current code logic with a separate
 query for descendants, then we probably need a second filter there as
 well...


 {{{#!php
 <?php
 function my_get_comments_clauses($clauses)
 {
         global $settings, $current_user;

         // WP 4.4 fix (fields is now comment_ID rather than * which makes
 it ambiguous if you have some joins)
         if ($clauses['fields'] !== '*')
                 $clauses['fields'] = 'wp1.comment_ID';

         $clauses['where'] =
 str_replace('comment_approved','wp1.comment_approved',$clauses['where']);
         $clauses['where'] =
 str_replace('comment_post_ID','wp1.comment_post_ID',$clauses['where']);
         $clauses['where'] =
 str_replace($wpdb->posts.'.post_status','wp3.post_status',$clauses['where']);

         // WP 4.4 hack (remove the parent filter to display everything now
 rather than relying on descendant)
         $clauses['where'] = str_replace('AND comment_parent =
 0','',$clauses['where']);

         $clauses['orderby'] =
 str_replace($wpdb->comments.'.comment_date_gmt','wp1.comment_date_gmt',$clauses['orderby']);
         $clauses['orderby'] =
 str_replace($wpdb->comments.'.comment_ID','wp1.comment_ID',$clauses['orderby']);

         $clauses['join'] = " AS wp1 LEFT JOIN ".$wpdb->posts." AS wp3 ON
 wp1.comment_post_id = wp3.ID ";

         $clauses['where'] =
 str_replace('user_id','wp1.user_id',$clauses['where']);
         $clauses['join'] .= " LEFT JOIN ".$wpdb->usermeta." as wp4 ON
 wp3.post_author = wp4.user_id ";
         $clauses['join'] .= " LEFT JOIN ".$wpdb->usermeta." as wp5 ON
 wp5.user_id = ".$current_user->ID." ";
         $where = " OR (wp4.meta_key = '".$settings['company_profile']."'
 AND wp5.meta_key = '".$settings['company_profile']."' AND wp4.meta_value =
 wp5.meta_value) ";
         $clauses['groupby'] = "wp1.comment_ID";

         $clauses['where'] .= " AND wp3.post_status = 'publish' AND
 (wp3.post_author = ".$current_user->ID." ".$where." OR wp3.post_type !=
 'my_custom_type') AND NOT EXISTS (SELECT * FROM ".$wpdb->commentmeta." AS
 wp2 WHERE wp1.comment_ID = wp2.comment_id AND wp2.meta_key =
 'internal_update' AND wp2.meta_value = 1) ";

         return $clauses;
 }
 }}}

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


More information about the wp-trac mailing list