mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +00:00
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@example.com". 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. git-svn-id: https://develop.svn.wordpress.org/trunk@29965 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1112,15 +1112,21 @@ function comments_template( $file = '/comments.php', $separate_comments = false
|
||||
*/
|
||||
$comment_author_url = esc_url($commenter['comment_author_url']);
|
||||
|
||||
/** @todo Use API instead of SELECTs. */
|
||||
if ( $user_ID) {
|
||||
$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));
|
||||
} else if ( empty($comment_author) ) {
|
||||
$comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
|
||||
} else {
|
||||
$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));
|
||||
$comment_args = array(
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'comment_date_gmt',
|
||||
'status' => 'approve',
|
||||
'post_id' => $post->ID,
|
||||
);
|
||||
|
||||
if ( $user_ID ) {
|
||||
$comment_args['include_unapproved'] = array( $user_ID );
|
||||
} else if ( ! empty( $comment_author ) ) {
|
||||
$comment_args['include_unapproved'] = array( $comment_author_email );
|
||||
}
|
||||
|
||||
$comments = get_comments( $comment_args );
|
||||
|
||||
/**
|
||||
* Filter the comments array.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user