mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +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:
@@ -95,6 +95,13 @@ class WP_Site_Query {
|
||||
*/
|
||||
public $max_num_pages = 0;
|
||||
|
||||
/**
|
||||
* @since 4.7.0
|
||||
* @access protected
|
||||
* @var wpdb
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Sets up the site query, based on the query vars passed.
|
||||
*
|
||||
@@ -147,6 +154,8 @@ class WP_Site_Query {
|
||||
* }
|
||||
*/
|
||||
public function __construct( $query = '' ) {
|
||||
$this->db = $GLOBALS['wpdb'];
|
||||
|
||||
$this->query_var_defaults = array(
|
||||
'fields' => '',
|
||||
'ID' => '',
|
||||
@@ -325,13 +334,9 @@ class WP_Site_Query {
|
||||
* @since 4.6.0
|
||||
* @access protected
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
|
||||
*/
|
||||
protected function get_site_ids() {
|
||||
global $wpdb;
|
||||
|
||||
$order = $this->parse_order( $this->query_vars['order'] );
|
||||
|
||||
// Disable ORDER BY with 'none', an empty array, or boolean false.
|
||||
@@ -395,7 +400,7 @@ class WP_Site_Query {
|
||||
// Parse site IDs for an IN clause.
|
||||
$site_id = absint( $this->query_vars['ID'] );
|
||||
if ( ! empty( $site_id ) ) {
|
||||
$this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
|
||||
$this->sql_clauses['where']['ID'] = $this->db->prepare( 'blog_id = %d', $site_id );
|
||||
}
|
||||
|
||||
// Parse site IDs for an IN clause.
|
||||
@@ -411,7 +416,7 @@ class WP_Site_Query {
|
||||
$network_id = absint( $this->query_vars['network_id'] );
|
||||
|
||||
if ( ! empty( $network_id ) ) {
|
||||
$this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
|
||||
$this->sql_clauses['where']['network_id'] = $this->db->prepare( 'site_id = %d', $network_id );
|
||||
}
|
||||
|
||||
// Parse site network IDs for an IN clause.
|
||||
@@ -425,56 +430,56 @@ class WP_Site_Query {
|
||||
}
|
||||
|
||||
if ( ! empty( $this->query_vars['domain'] ) ) {
|
||||
$this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
|
||||
$this->sql_clauses['where']['domain'] = $this->db->prepare( 'domain = %s', $this->query_vars['domain'] );
|
||||
}
|
||||
|
||||
// Parse site domain for an IN clause.
|
||||
if ( is_array( $this->query_vars['domain__in'] ) ) {
|
||||
$this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
|
||||
$this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
|
||||
}
|
||||
|
||||
// Parse site domain for a NOT IN clause.
|
||||
if ( is_array( $this->query_vars['domain__not_in'] ) ) {
|
||||
$this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
|
||||
$this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
|
||||
}
|
||||
|
||||
if ( ! empty( $this->query_vars['path'] ) ) {
|
||||
$this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
|
||||
$this->sql_clauses['where']['path'] = $this->db->prepare( 'path = %s', $this->query_vars['path'] );
|
||||
}
|
||||
|
||||
// Parse site path for an IN clause.
|
||||
if ( is_array( $this->query_vars['path__in'] ) ) {
|
||||
$this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
|
||||
$this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__in'] ) ) . "' )";
|
||||
}
|
||||
|
||||
// Parse site path for a NOT IN clause.
|
||||
if ( is_array( $this->query_vars['path__not_in'] ) ) {
|
||||
$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
|
||||
$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
|
||||
}
|
||||
|
||||
if ( is_numeric( $this->query_vars['archived'] ) ) {
|
||||
$archived = absint( $this->query_vars['archived'] );
|
||||
$this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived );
|
||||
$this->sql_clauses['where']['archived'] = $this->db->prepare( "archived = %d ", $archived );
|
||||
}
|
||||
|
||||
if ( is_numeric( $this->query_vars['mature'] ) ) {
|
||||
$mature = absint( $this->query_vars['mature'] );
|
||||
$this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature );
|
||||
$this->sql_clauses['where']['mature'] = $this->db->prepare( "mature = %d ", $mature );
|
||||
}
|
||||
|
||||
if ( is_numeric( $this->query_vars['spam'] ) ) {
|
||||
$spam = absint( $this->query_vars['spam'] );
|
||||
$this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam );
|
||||
$this->sql_clauses['where']['spam'] = $this->db->prepare( "spam = %d ", $spam );
|
||||
}
|
||||
|
||||
if ( is_numeric( $this->query_vars['deleted'] ) ) {
|
||||
$deleted = absint( $this->query_vars['deleted'] );
|
||||
$this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted );
|
||||
$this->sql_clauses['where']['deleted'] = $this->db->prepare( "deleted = %d ", $deleted );
|
||||
}
|
||||
|
||||
if ( is_numeric( $this->query_vars['public'] ) ) {
|
||||
$public = absint( $this->query_vars['public'] );
|
||||
$this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
|
||||
$this->sql_clauses['where']['public'] = $this->db->prepare( "public = %d ", $public );
|
||||
}
|
||||
|
||||
// Falsey search strings are ignored.
|
||||
@@ -550,7 +555,7 @@ class WP_Site_Query {
|
||||
}
|
||||
|
||||
$this->sql_clauses['select'] = "SELECT $found_rows $fields";
|
||||
$this->sql_clauses['from'] = "FROM $wpdb->blogs $join";
|
||||
$this->sql_clauses['from'] = "FROM {$this->db->blogs} $join";
|
||||
$this->sql_clauses['groupby'] = $groupby;
|
||||
$this->sql_clauses['orderby'] = $orderby;
|
||||
$this->sql_clauses['limits'] = $limits;
|
||||
@@ -558,10 +563,10 @@ class WP_Site_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 ) );
|
||||
}
|
||||
|
||||
$site_ids = $wpdb->get_col( $this->request );
|
||||
$site_ids = $this->db->get_col( $this->request );
|
||||
|
||||
return array_map( 'intval', $site_ids );
|
||||
}
|
||||
@@ -572,12 +577,8 @@ class WP_Site_Query {
|
||||
*
|
||||
* @since 4.6.0
|
||||
* @access private
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*/
|
||||
private function set_found_sites() {
|
||||
global $wpdb;
|
||||
|
||||
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
|
||||
/**
|
||||
* Filters the query used to retrieve found site count.
|
||||
@@ -589,7 +590,7 @@ class WP_Site_Query {
|
||||
*/
|
||||
$found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
|
||||
|
||||
$this->found_sites = (int) $wpdb->get_var( $found_sites_query );
|
||||
$this->found_sites = (int) $this->db->get_var( $found_sites_query );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -599,24 +600,20 @@ class WP_Site_Query {
|
||||
* @since 4.6.0
|
||||
* @access protected
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param string $string Search string.
|
||||
* @param array $columns Columns to search.
|
||||
* @return string Search SQL.
|
||||
*/
|
||||
protected function get_search_sql( $string, $columns ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( false !== strpos( $string, '*' ) ) {
|
||||
$like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
|
||||
$like = '%' . implode( '%', array_map( array( $this->db, 'esc_like' ), explode( '*', $string ) ) ) . '%';
|
||||
} else {
|
||||
$like = '%' . $wpdb->esc_like( $string ) . '%';
|
||||
$like = '%' . $this->db->esc_like( $string ) . '%';
|
||||
}
|
||||
|
||||
$searches = array();
|
||||
foreach ( $columns as $column ) {
|
||||
$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
|
||||
$searches[] = $this->db->prepare( "$column LIKE %s", $like );
|
||||
}
|
||||
|
||||
return '(' . implode( ' OR ', $searches ) . ')';
|
||||
@@ -628,24 +625,20 @@ class WP_Site_Query {
|
||||
* @since 4.6.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;
|
||||
|
||||
$parsed = false;
|
||||
|
||||
switch ( $orderby ) {
|
||||
case 'site__in':
|
||||
$site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
|
||||
$parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
|
||||
$parsed = "FIELD( {$this->db->blogs}.blog_id, $site__in )";
|
||||
break;
|
||||
case 'network__in':
|
||||
$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
|
||||
$parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
|
||||
$parsed = "FIELD( {$this->db->blogs}.site_id, $network__in )";
|
||||
break;
|
||||
case 'domain':
|
||||
case 'last_updated':
|
||||
|
||||
Reference in New Issue
Block a user