Eliminate use of `extract()` in `WP_Comment_Query::query()`.

See #22400.

Built from https://develop.svn.wordpress.org/trunk@28458


git-svn-id: http://core.svn.wordpress.org/trunk@28285 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2014-05-16 19:33:14 +00:00
parent 14ba67c38d
commit 4f57a231c5
1 changed files with 66 additions and 46 deletions

View File

@ -277,7 +277,7 @@ class WP_Comment_Query {
extract( $this->query_vars, EXTR_SKIP ); extract( $this->query_vars, EXTR_SKIP );
// $args can be whatever, only use the args defined in defaults to compute the key // $args can be whatever, only use the args defined in defaults to compute the key
$key = md5( serialize( compact(array_keys($defaults)) ) ); $key = md5( serialize( compact( array_keys( $defaults ) ) ) );
$last_changed = wp_cache_get( 'last_changed', 'comment' ); $last_changed = wp_cache_get( 'last_changed', 'comment' );
if ( ! $last_changed ) { if ( ! $last_changed ) {
$last_changed = microtime(); $last_changed = microtime();
@ -285,24 +285,27 @@ class WP_Comment_Query {
} }
$cache_key = "get_comments:$key:$last_changed"; $cache_key = "get_comments:$key:$last_changed";
if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
return $cache; return $cache;
}
$post_id = absint($post_id); $status = $this->query_vars['status'];
if ( 'hold' == $status ) {
if ( 'hold' == $status )
$approved = "comment_approved = '0'"; $approved = "comment_approved = '0'";
elseif ( 'approve' == $status ) } elseif ( 'approve' == $status ) {
$approved = "comment_approved = '1'"; $approved = "comment_approved = '1'";
elseif ( ! empty( $status ) && 'all' != $status ) } elseif ( ! empty( $status ) && 'all' != $status ) {
$approved = $wpdb->prepare( "comment_approved = %s", $status ); $approved = $wpdb->prepare( "comment_approved = %s", $status );
else } else {
$approved = "( comment_approved = '0' OR comment_approved = '1' )"; $approved = "( comment_approved = '0' OR comment_approved = '1' )";
}
$order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
$order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC'; if ( ! empty( $this->query_vars['orderby'] ) ) {
$ordersby = is_array( $this->query_vars['orderby'] ) ?
$this->query_vars['orderby'] :
preg_split( '/[,\s]/', $this->query_vars['orderby'] );
if ( ! empty( $orderby ) ) {
$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
$allowed_keys = array( $allowed_keys = array(
'comment_agent', 'comment_agent',
'comment_approved', 'comment_approved',
@ -338,52 +341,68 @@ class WP_Comment_Query {
$orderby = 'comment_date_gmt'; $orderby = 'comment_date_gmt';
} }
$number = absint($number); $number = absint( $this->query_vars['number'] );
$offset = absint($offset); $offset = absint( $this->query_vars['offset'] );
if ( !empty($number) ) { if ( ! empty( $number ) ) {
if ( $offset ) if ( $offset ) {
$limits = 'LIMIT ' . $offset . ',' . $number; $limits = 'LIMIT ' . $offset . ',' . $number;
else } else {
$limits = 'LIMIT ' . $number; $limits = 'LIMIT ' . $number;
}
} else { } else {
$limits = ''; $limits = '';
} }
if ( $count ) if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)'; $fields = 'COUNT(*)';
else } else {
$fields = '*'; $fields = '*';
}
$join = ''; $join = '';
$where = $approved; $where = $approved;
if ( ! empty($post_id) ) $post_id = absint( $this->query_vars['post_id'] );
if ( ! empty( $post_id ) ) {
$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id ); $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
if ( '' !== $author_email ) }
$where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
if ( '' !== $karma ) if ( '' !== $this->query_vars['author_email'] ) {
$where .= $wpdb->prepare( ' AND comment_karma = %d', $karma ); $where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] );
if ( 'comment' == $type ) { }
if ( '' !== $this->query_vars['karma'] ) {
$where .= $wpdb->prepare( ' AND comment_karma = %d', $this->query_vars['karma'] );
}
if ( 'comment' == $this->query_vars['type'] ) {
$where .= " AND comment_type = ''"; $where .= " AND comment_type = ''";
} elseif( 'pings' == $type ) { } elseif( 'pings' == $this->query_vars['type'] ) {
$where .= ' AND comment_type IN ("pingback", "trackback")'; $where .= ' AND comment_type IN ("pingback", "trackback")';
} elseif ( ! empty( $type ) ) { } elseif ( ! empty( $this->query_vars['type'] ) ) {
$where .= $wpdb->prepare( ' AND comment_type = %s', $type ); $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
}
if ( '' !== $parent )
$where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
if ( is_array( $user_id ) ) {
$where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $user_id ) ) . ')';
} elseif ( '' !== $user_id ) {
$where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
} }
if ( '' !== $search ) if ( '' !== $this->query_vars['parent'] ) {
$where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) ); $where .= $wpdb->prepare( ' AND comment_parent = %d', $this->query_vars['parent'] );
}
if ( is_array( $this->query_vars['user_id'] ) ) {
$where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
} elseif ( '' !== $this->query_vars['user_id'] ) {
$where .= $wpdb->prepare( ' AND user_id = %d', $this->query_vars['user_id'] );
}
if ( '' !== $this->query_vars['search'] ) {
$where .= $this->get_search_sql(
$this->query_vars['search'],
array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
);
}
$plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
$post_fields = array_filter( $plucked );
$post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
if ( ! empty( $post_fields ) ) { if ( ! empty( $post_fields ) ) {
$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
foreach( $post_fields as $field_name => $field_value ) foreach( $post_fields as $field_name => $field_value )
@ -397,6 +416,7 @@ class WP_Comment_Query {
$groupby = "{$wpdb->comments}.comment_ID"; $groupby = "{$wpdb->comments}.comment_ID";
} }
$date_query = $this->query_vars['date_query'];
if ( ! empty( $date_query ) && is_array( $date_query ) ) { if ( ! empty( $date_query ) && is_array( $date_query ) ) {
$date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
$where .= $date_query_object->get_sql(); $where .= $date_query_object->get_sql();
@ -415,24 +435,24 @@ class WP_Comment_Query {
foreach ( $pieces as $piece ) foreach ( $pieces as $piece )
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
if ( $groupby ) if ( $groupby ) {
$groupby = 'GROUP BY ' . $groupby; $groupby = 'GROUP BY ' . $groupby;
}
$query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
if ( $count ) if ( $this->query_vars['count'] ) {
return $wpdb->get_var( $query ); return $wpdb->get_var( $query );
}
$comments = $wpdb->get_results( $query ); $results = $wpdb->get_results( $query );
/** /**
* Filter the comment query results. * Filter the comment query results.
* *
* @since 3.1.0 * @since 3.1.0
* *
* @param array $comments An array of comments. * @param array $results An array of comments.
* @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
*/ */
$comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) ); $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
wp_cache_add( $cache_key, $comments, 'comment' ); wp_cache_add( $cache_key, $comments, 'comment' );