Make WordPress Core


Ignore:
Timestamp:
10/19/2014 07:38:16 PM (10 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.php

    r29935 r29965  
    260260     * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
    261261     *              'post_author__not_in', 'author__in', 'author__not_in',
    262      *              'post__in', and 'post__not_in' to $query_vars.
     262     *              'post__in', 'post__not_in', and 'include_unapproved' to $query_vars.
    263263     *
    264264     * @param string|array $query_vars
     
    272272            'author__in' => '',
    273273            'author__not_in' => '',
     274            'include_unapproved' => '',
    274275            'fields' => '',
    275276            'ID' => '',
     
    334335        }
    335336
     337        // Assemble clauses related to 'comment_approved'.
     338        $approved_clauses = array();
    336339        $status = $this->query_vars['status'];
    337340        if ( 'hold' == $status ) {
    338             $approved = "comment_approved = '0'";
     341            $approved_clauses[] = "comment_approved = '0'";
    339342        } elseif ( 'approve' == $status ) {
    340             $approved = "comment_approved = '1'";
     343            $approved_clauses[] = "comment_approved = '1'";
    341344        } elseif ( ! empty( $status ) && 'all' != $status ) {
    342             $approved = $wpdb->prepare( "comment_approved = %s", $status );
     345            $approved_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
    343346        } else {
    344             $approved = "( comment_approved = '0' OR comment_approved = '1' )";
    345         }
     347            $approved_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
     348        }
     349
     350        // User IDs or emails whose unapproved comments are included, regardless of $status.
     351        if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
     352            $include_unapproved = $this->query_vars['include_unapproved'];
     353
     354            // Accepts arrays or comma-separated strings.
     355            if ( ! is_array( $include_unapproved ) ) {
     356                $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
     357            }
     358
     359            $unapproved_ids = $unapproved_emails = array();
     360            foreach ( $include_unapproved as $unapproved_identifier ) {
     361                // Numeric values are assumed to be user ids.
     362                if ( is_numeric( $unapproved_identifier ) ) {
     363                    $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
     364
     365                // Otherwise we match against email addresses.
     366                } else {
     367                    $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     368                }
     369            }
     370        }
     371
     372        // Collapse comment_approved clauses into a single OR-separated clause.
     373        if ( 1 === count( $approved_clauses ) ) {
     374            $approved = $approved_clauses[0];
     375        } else {
     376            $approved = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
     377        }
     378
    346379        $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
    347380
Note: See TracChangeset for help on using the changeset viewer.