WordPress.org

Make WordPress Core

Ticket #19623: comment-template-api.diff

File comment-template-api.diff, 2.9 KB (added by hardy101, 3 years ago)

Use get_comments() API instead of SQL in comment-template.php

  • wp-includes/comment-template.php

     
    883883 
    884884       /** @todo Use API instead of SELECTs. */ 
    885885       if ( $user_ID) { 
    886                $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID)); 
     886               $comments = get_comments( array('post_id' => $post->ID, 'unapproved_comment_author_user_id' => $user_ID, 'order' => 'ASC') ); 
    887887       } else if ( empty($comment_author) ) { 
    888888               $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') ); 
    889889       } else { 
    890                $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email)); 
     890               $comments = get_comments( array('post_id' => $post->ID, 'unapproved_comment_author_data' => array( 'comment_author' => wp_specialchars_decode($comment_author,ENT_QUOTES), 'comment_author_email' => $comment_author_email ), 'order' => 'ASC') ); 
    891891       } 
    892892 
    893893       // keep $comments for legacy's sake 
  • wp-includes/comment.php

     
    252252                       $approved = "comment_approved = 'spam'"; 
    253253               elseif ( 'trash' == $status ) 
    254254                       $approved = "comment_approved = 'trash'"; 
     255               elseif ( 0 < $unapproved_comment_author_user_id ) //show unapproved comments to the current user if they're the author 
     256                       $approved = sprintf( "( ( user_id = %d AND comment_approved = '0' ) OR comment_approved = '1' )", $unapproved_comment_author_user_id ); 
     257               elseif ( is_array($unapproved_comment_author_data) && ! empty($unapproved_comment_author_data) && ! empty( $unapproved_comment_author_data['comment_author'] ) && ! empty( $unapproved_comment_author_data['comment_author_email'] ) ) //use the cookie system for current commenter 
     258                       $approved = sprintf( "( ( comment_author = '%1s' AND comment_author_email = '%2s' AND comment_approved = '0' ) OR comment_approved = '1' )", $unapproved_comment_author_data['comment_author'], $unapproved_comment_author_data['comment_author_email'] ); 
    255259               else 
    256260                       $approved = "( comment_approved = '0' OR comment_approved = '1' )";