mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +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:
@@ -105,6 +105,13 @@ class WP_Meta_Query {
|
||||
*/
|
||||
protected $has_or_relation = false;
|
||||
|
||||
/**
|
||||
* @since 4.7.0
|
||||
* @access protected
|
||||
* @var wpdb
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@@ -137,8 +144,11 @@ class WP_Meta_Query {
|
||||
* }
|
||||
*/
|
||||
public function __construct( $meta_query = false ) {
|
||||
if ( !$meta_query )
|
||||
$this->db = $GLOBALS['wpdb'];
|
||||
|
||||
if ( ! $meta_query ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
|
||||
$this->relation = 'OR';
|
||||
@@ -484,8 +494,6 @@ class WP_Meta_Query {
|
||||
* @since 4.1.0
|
||||
* @access public
|
||||
*
|
||||
* @global wpdb $wpdb WordPress database abstraction object.
|
||||
*
|
||||
* @param array $clause Query clause, passed by reference.
|
||||
* @param array $parent_query Parent query array.
|
||||
* @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
|
||||
@@ -498,8 +506,6 @@ class WP_Meta_Query {
|
||||
* }
|
||||
*/
|
||||
public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
$sql_chunks = array(
|
||||
'where' => array(),
|
||||
'join' => array(),
|
||||
@@ -537,7 +543,7 @@ class WP_Meta_Query {
|
||||
if ( 'NOT EXISTS' === $meta_compare ) {
|
||||
$join .= " LEFT JOIN $this->meta_table";
|
||||
$join .= $i ? " AS $alias" : '';
|
||||
$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
|
||||
$join .= $this->db->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
|
||||
|
||||
// All other JOIN clauses.
|
||||
} else {
|
||||
@@ -581,7 +587,7 @@ class WP_Meta_Query {
|
||||
if ( 'NOT EXISTS' === $meta_compare ) {
|
||||
$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
|
||||
} else {
|
||||
$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
|
||||
$sql_chunks['where'][] = $this->db->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,25 +607,25 @@ class WP_Meta_Query {
|
||||
case 'IN' :
|
||||
case 'NOT IN' :
|
||||
$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
|
||||
$where = $wpdb->prepare( $meta_compare_string, $meta_value );
|
||||
$where = $this->db->prepare( $meta_compare_string, $meta_value );
|
||||
break;
|
||||
|
||||
case 'BETWEEN' :
|
||||
case 'NOT BETWEEN' :
|
||||
$meta_value = array_slice( $meta_value, 0, 2 );
|
||||
$where = $wpdb->prepare( '%s AND %s', $meta_value );
|
||||
$where = $this->db->prepare( '%s AND %s', $meta_value );
|
||||
break;
|
||||
|
||||
case 'LIKE' :
|
||||
case 'NOT LIKE' :
|
||||
$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
$meta_value = '%' . $this->db->esc_like( $meta_value ) . '%';
|
||||
$where = $this->db->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
||||
// EXISTS with a value is interpreted as '='.
|
||||
case 'EXISTS' :
|
||||
$meta_compare = '=';
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
$where = $this->db->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
||||
// 'value' is ignored for NOT EXISTS.
|
||||
@@ -628,7 +634,7 @@ class WP_Meta_Query {
|
||||
break;
|
||||
|
||||
default :
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
$where = $this->db->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user