Query: use composition for `$db` in `WP_Date_Query`, removes need to import `global $wpdb` in multiple methods.

See #37699.

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


git-svn-id: http://core.svn.wordpress.org/trunk@38221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2016-08-18 19:48:34 +00:00
parent 9b393cea42
commit 3357ca23e6
2 changed files with 20 additions and 18 deletions

View File

@ -62,6 +62,13 @@ class WP_Date_Query {
*/ */
public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' ); public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' );
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -151,6 +158,7 @@ class WP_Date_Query {
* 'comment_date', 'comment_date_gmt'. * 'comment_date', 'comment_date_gmt'.
*/ */
public function __construct( $date_query, $default_column = 'post_date' ) { public function __construct( $date_query, $default_column = 'post_date' ) {
$this->db = $GLOBALS['wpdb'];
if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) { if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
$this->relation = 'OR'; $this->relation = 'OR';
@ -486,8 +494,6 @@ class WP_Date_Query {
* @return string A validated column name value. * @return string A validated column name value.
*/ */
public function validate_column( $column ) { public function validate_column( $column ) {
global $wpdb;
$valid_columns = array( $valid_columns = array(
'post_date', 'post_date_gmt', 'post_modified', 'post_date', 'post_date_gmt', 'post_modified',
'post_modified_gmt', 'comment_date', 'comment_date_gmt', 'post_modified_gmt', 'comment_date', 'comment_date_gmt',
@ -512,20 +518,20 @@ class WP_Date_Query {
} }
$known_columns = array( $known_columns = array(
$wpdb->posts => array( $this->db->posts => array(
'post_date', 'post_date',
'post_date_gmt', 'post_date_gmt',
'post_modified', 'post_modified',
'post_modified_gmt', 'post_modified_gmt',
), ),
$wpdb->comments => array( $this->db->comments => array(
'comment_date', 'comment_date',
'comment_date_gmt', 'comment_date_gmt',
), ),
$wpdb->users => array( $this->db->users => array(
'user_registered', 'user_registered',
), ),
$wpdb->blogs => array( $this->db->blogs => array(
'registered', 'registered',
'last_updated', 'last_updated',
), ),
@ -717,8 +723,6 @@ class WP_Date_Query {
* } * }
*/ */
protected function get_sql_for_clause( $query, $parent_query ) { protected function get_sql_for_clause( $query, $parent_query ) {
global $wpdb;
// The sub-parts of a $where part. // The sub-parts of a $where part.
$where_parts = array(); $where_parts = array();
@ -740,12 +744,12 @@ class WP_Date_Query {
} }
// Range queries. // Range queries.
if ( ! empty( $query['after'] ) ) if ( ! empty( $query['after'] ) ) {
$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) ); $where_parts[] = $this->db->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
}
if ( ! empty( $query['before'] ) ) if ( ! empty( $query['before'] ) ) {
$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); $where_parts[] = $this->db->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
}
// Specific value queries. // Specific value queries.
if ( isset( $query['year'] ) && $value = $this->build_value( $compare, $query['year'] ) ) if ( isset( $query['year'] ) && $value = $this->build_value( $compare, $query['year'] ) )
@ -958,8 +962,6 @@ class WP_Date_Query {
* @return string|false A query part or false on failure. * @return string|false A query part or false on failure.
*/ */
public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) {
global $wpdb;
// Have to have at least one // Have to have at least one
if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) ) if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) )
return false; return false;
@ -1013,6 +1015,6 @@ class WP_Date_Query {
$time .= sprintf( '%02d', $second ); $time .= sprintf( '%02d', $second );
} }
return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time ); return $this->db->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
} }
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38279'; $wp_version = '4.7-alpha-38280';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.