WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/19/2014 07:38:16 PM (6 years ago)
Author:
boonebgorges
Message:

Use the comment API rather than direct SQL queries in comments_template().

comments_template() is used by most themes to display a post's comments. It
shows all comments that have been approved, and also shows all pending comments
by the current visitor (as determined by the comment cookies). However, the
comments API previously had no way of querying for "all comments that are
either approved, or are unapproved but written by foo@…". The
workaround was a direct SQL query: uncached, not subject to the same filters as
other comment queries, and just generally icky.

The new include_unapproved parameter for WP_Comment_Query accepts an array
of user IDs or email addresses. Pending comments associated with users in this
array will be included in query results, regardless of the value of the 'status'
parameter. In comments_template(), we leap from direct SQL queries to
get_comments() plus `include_unapproved', striving to put right what once
went wrong.

Props boonebgorges, simonwheatley, hardy101, jesin.
Fixes #19623.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-template.php

    r29908 r29965  
    11131113    $comment_author_url = esc_url($commenter['comment_author_url']);
    11141114
    1115     /** @todo Use API instead of SELECTs. */
    1116     if ( $user_ID) {
    1117         $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));
    1118     } else if ( empty($comment_author) ) {
    1119         $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
    1120     } else {
    1121         $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));
    1122     }
     1115    $comment_args = array(
     1116        'order'   => 'ASC',
     1117        'orderby' => 'comment_date_gmt',
     1118        'status'  => 'approve',
     1119        'post_id' => $post->ID,
     1120    );
     1121
     1122    if ( $user_ID ) {
     1123        $comment_args['include_unapproved'] = array( $user_ID );
     1124    } else if ( ! empty( $comment_author ) ) {
     1125        $comment_args['include_unapproved'] = array( $comment_author_email );
     1126    }
     1127
     1128    $comments = get_comments( $comment_args );
    11231129
    11241130    /**
Note: See TracChangeset for help on using the changeset viewer.