mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
Query: add a protected field, $db, (composition, as it were) to WP_*_Query classes to hold the value for the database abstraction, instead of importing the global $wpdb into every method that uses it. Reduces the number of global imports by 32.
See #37699. git-svn-id: https://develop.svn.wordpress.org/trunk@38275 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -141,6 +141,13 @@ class WP_Comment_Query {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.7.0
|
||||
* @access protected
|
||||
* @var wpdb
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@@ -260,6 +267,8 @@ class WP_Comment_Query {
|
||||
* }
|
||||
*/
|
||||
public function __construct( $query = '' ) {
|
||||
$this->db = $GLOBALS['wpdb'];
|
||||
|
||||
$this->query_var_defaults = array(
|
||||
'author_email' => '',
|
||||
'author_url' => '',
|
||||
@@ -363,13 +372,9 @@ class WP_Comment_Query {
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @return int|array List of comments or number of found comments if `$count` argument is true.
|
||||
*/
|
||||
public function get_comments() {
|
||||
global $wpdb;
|
||||
|
||||
$this->parse_query();
|
||||
|
||||
// Parse meta query
|
||||
@@ -388,7 +393,7 @@ class WP_Comment_Query {
|
||||
// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
|
||||
$this->meta_query->parse_query_vars( $this->query_vars );
|
||||
if ( ! empty( $this->meta_query->queries ) ) {
|
||||
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
|
||||
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this );
|
||||
}
|
||||
|
||||
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
|
||||
@@ -480,12 +485,8 @@ class WP_Comment_Query {
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*/
|
||||
protected function get_comment_ids() {
|
||||
global $wpdb;
|
||||
|
||||
// Assemble clauses related to 'comment_approved'.
|
||||
$approved_clauses = array();
|
||||
|
||||
@@ -514,7 +515,7 @@ class WP_Comment_Query {
|
||||
break;
|
||||
|
||||
default :
|
||||
$status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
|
||||
$status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -537,11 +538,11 @@ class WP_Comment_Query {
|
||||
foreach ( $include_unapproved as $unapproved_identifier ) {
|
||||
// Numeric values are assumed to be user ids.
|
||||
if ( is_numeric( $unapproved_identifier ) ) {
|
||||
$approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
|
||||
$approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
|
||||
|
||||
// Otherwise we match against email addresses.
|
||||
} else {
|
||||
$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
|
||||
$approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -600,7 +601,7 @@ class WP_Comment_Query {
|
||||
|
||||
// If no valid clauses were found, order by comment_date_gmt.
|
||||
if ( empty( $orderby_array ) ) {
|
||||
$orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
|
||||
$orderby_array[] = "{$this->db->comments}.comment_date_gmt $order";
|
||||
}
|
||||
|
||||
// To ensure determinate sorting, always include a comment_ID clause.
|
||||
@@ -633,12 +634,12 @@ class WP_Comment_Query {
|
||||
$comment_ID_order = 'DESC';
|
||||
}
|
||||
|
||||
$orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
|
||||
$orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
|
||||
}
|
||||
|
||||
$orderby = implode( ', ', $orderby_array );
|
||||
} else {
|
||||
$orderby = "$wpdb->comments.comment_date_gmt $order";
|
||||
$orderby = "{$this->db->comments}.comment_date_gmt $order";
|
||||
}
|
||||
|
||||
$number = absint( $this->query_vars['number'] );
|
||||
@@ -655,22 +656,22 @@ class WP_Comment_Query {
|
||||
if ( $this->query_vars['count'] ) {
|
||||
$fields = 'COUNT(*)';
|
||||
} else {
|
||||
$fields = "$wpdb->comments.comment_ID";
|
||||
$fields = "{$this->db->comments}.comment_ID";
|
||||
}
|
||||
|
||||
$post_id = absint( $this->query_vars['post_id'] );
|
||||
if ( ! empty( $post_id ) ) {
|
||||
$this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
|
||||
$this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id );
|
||||
}
|
||||
|
||||
// Parse comment IDs for an IN clause.
|
||||
if ( ! empty( $this->query_vars['comment__in'] ) ) {
|
||||
$this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
|
||||
$this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
|
||||
}
|
||||
|
||||
// Parse comment IDs for a NOT IN clause.
|
||||
if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
|
||||
$this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
|
||||
$this->sql_clauses['where']['comment__not_in'] = "{$this->db->comments}.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
|
||||
}
|
||||
|
||||
// Parse comment parent IDs for an IN clause.
|
||||
@@ -694,15 +695,15 @@ class WP_Comment_Query {
|
||||
}
|
||||
|
||||
if ( '' !== $this->query_vars['author_email'] ) {
|
||||
$this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
|
||||
$this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
|
||||
}
|
||||
|
||||
if ( '' !== $this->query_vars['author_url'] ) {
|
||||
$this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
|
||||
$this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
|
||||
}
|
||||
|
||||
if ( '' !== $this->query_vars['karma'] ) {
|
||||
$this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
|
||||
$this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
|
||||
}
|
||||
|
||||
// Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
|
||||
@@ -733,7 +734,7 @@ class WP_Comment_Query {
|
||||
break;
|
||||
|
||||
default:
|
||||
$comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
|
||||
$comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -750,13 +751,13 @@ class WP_Comment_Query {
|
||||
}
|
||||
|
||||
if ( '' !== $parent ) {
|
||||
$this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
|
||||
$this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent );
|
||||
}
|
||||
|
||||
if ( is_array( $this->query_vars['user_id'] ) ) {
|
||||
$this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
|
||||
} elseif ( '' !== $this->query_vars['user_id'] ) {
|
||||
$this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
|
||||
$this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
|
||||
}
|
||||
|
||||
// Falsy search strings are ignored.
|
||||
@@ -780,7 +781,7 @@ class WP_Comment_Query {
|
||||
foreach ( $post_fields as $field_name => $field_value ) {
|
||||
// $field_value may be an array.
|
||||
$esses = array_fill( 0, count( (array) $field_value ), '%s' );
|
||||
$this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
|
||||
$this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -801,7 +802,7 @@ class WP_Comment_Query {
|
||||
$join_posts_table = true;
|
||||
|
||||
$esses = array_fill( 0, count( $q_values ), '%s' );
|
||||
$this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
|
||||
$this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -830,7 +831,7 @@ class WP_Comment_Query {
|
||||
$join = '';
|
||||
|
||||
if ( $join_posts_table ) {
|
||||
$join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
|
||||
$join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
|
||||
}
|
||||
|
||||
if ( ! empty( $this->meta_query_clauses ) ) {
|
||||
@@ -840,7 +841,7 @@ class WP_Comment_Query {
|
||||
$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
|
||||
|
||||
if ( ! $this->query_vars['count'] ) {
|
||||
$groupby = "{$wpdb->comments}.comment_ID";
|
||||
$groupby = "{$this->db->comments}.comment_ID";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -889,7 +890,7 @@ class WP_Comment_Query {
|
||||
}
|
||||
|
||||
$this->sql_clauses['select'] = "SELECT $found_rows $fields";
|
||||
$this->sql_clauses['from'] = "FROM $wpdb->comments $join";
|
||||
$this->sql_clauses['from'] = "FROM {$this->db->comments} $join";
|
||||
$this->sql_clauses['groupby'] = $groupby;
|
||||
$this->sql_clauses['orderby'] = $orderby;
|
||||
$this->sql_clauses['limits'] = $limits;
|
||||
@@ -897,9 +898,9 @@ class WP_Comment_Query {
|
||||
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
|
||||
|
||||
if ( $this->query_vars['count'] ) {
|
||||
return intval( $wpdb->get_var( $this->request ) );
|
||||
return intval( $this->db->get_var( $this->request ) );
|
||||
} else {
|
||||
$comment_ids = $wpdb->get_col( $this->request );
|
||||
$comment_ids = $this->db->get_col( $this->request );
|
||||
return array_map( 'intval', $comment_ids );
|
||||
}
|
||||
}
|
||||
@@ -910,12 +911,8 @@ class WP_Comment_Query {
|
||||
*
|
||||
* @since 4.6.0
|
||||
* @access private
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*/
|
||||
private function set_found_comments() {
|
||||
global $wpdb;
|
||||
|
||||
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
|
||||
/**
|
||||
* Filters the query used to retrieve found comment count.
|
||||
@@ -927,7 +924,7 @@ class WP_Comment_Query {
|
||||
*/
|
||||
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
|
||||
|
||||
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
|
||||
$this->found_comments = (int) $this->db->get_var( $found_comments_query );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -943,8 +940,6 @@ class WP_Comment_Query {
|
||||
* @return array
|
||||
*/
|
||||
protected function fill_descendants( $comments ) {
|
||||
global $wpdb;
|
||||
|
||||
$levels = array(
|
||||
0 => wp_list_pluck( $comments, 'comment_ID' ),
|
||||
);
|
||||
@@ -996,7 +991,7 @@ class WP_Comment_Query {
|
||||
|
||||
if ( $uncached_parent_ids ) {
|
||||
$where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
|
||||
$level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
|
||||
$level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
|
||||
|
||||
// Cache parent-child relationships.
|
||||
$parent_map = array_fill_keys( $uncached_parent_ids, array() );
|
||||
@@ -1067,20 +1062,16 @@ class WP_Comment_Query {
|
||||
* @since 3.1.0
|
||||
* @access protected
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param string $string
|
||||
* @param array $cols
|
||||
* @return string
|
||||
*/
|
||||
protected function get_search_sql( $string, $cols ) {
|
||||
global $wpdb;
|
||||
|
||||
$like = '%' . $wpdb->esc_like( $string ) . '%';
|
||||
$like = '%' . $this->db->esc_like( $string ) . '%';
|
||||
|
||||
$searches = array();
|
||||
foreach ( $cols as $col ) {
|
||||
$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
|
||||
$searches[] = $this->db->prepare( "$col LIKE %s", $like );
|
||||
}
|
||||
|
||||
return ' AND (' . implode(' OR ', $searches) . ')';
|
||||
@@ -1092,14 +1083,10 @@ class WP_Comment_Query {
|
||||
* @since 4.2.0
|
||||
* @access protected
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param string $orderby Alias for the field to order by.
|
||||
* @return string|false Value to used in the ORDER clause. False otherwise.
|
||||
*/
|
||||
protected function parse_orderby( $orderby ) {
|
||||
global $wpdb;
|
||||
|
||||
$allowed_keys = array(
|
||||
'comment_agent',
|
||||
'comment_approved',
|
||||
@@ -1131,19 +1118,19 @@ class WP_Comment_Query {
|
||||
|
||||
$parsed = false;
|
||||
if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
|
||||
$parsed = "$wpdb->commentmeta.meta_value";
|
||||
$parsed = "{$this->db->commentmeta}.meta_value";
|
||||
} elseif ( $orderby == 'meta_value_num' ) {
|
||||
$parsed = "$wpdb->commentmeta.meta_value+0";
|
||||
$parsed = "{$this->db->commentmeta}.meta_value+0";
|
||||
} elseif ( $orderby == 'comment__in' ) {
|
||||
$comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
|
||||
$parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
|
||||
$parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )";
|
||||
} elseif ( in_array( $orderby, $allowed_keys ) ) {
|
||||
|
||||
if ( isset( $meta_query_clauses[ $orderby ] ) ) {
|
||||
$meta_clause = $meta_query_clauses[ $orderby ];
|
||||
$parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
|
||||
} else {
|
||||
$parsed = "$wpdb->comments.$orderby";
|
||||
$parsed = "{$this->db->comments}.$orderby";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user