From b786d481a9159a18cca79cb5d8056c20b5505be2 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 18 Aug 2016 19:47:15 +0000 Subject: [PATCH] Query: use composition for `$db` in `WP_Date_Query`, removes need to import `global $wpdb` in multiple methods. See #37699. git-svn-id: https://develop.svn.wordpress.org/trunk@38280 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/date.php | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index d1f85f8c0d..1b46e2c5a3 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -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' ); + /** + * @since 4.7.0 + * @access protected + * @var wpdb + */ + protected $db; + /** * Constructor. * @@ -151,6 +158,7 @@ class WP_Date_Query { * 'comment_date', 'comment_date_gmt'. */ public function __construct( $date_query, $default_column = 'post_date' ) { + $this->db = $GLOBALS['wpdb']; if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) { $this->relation = 'OR'; @@ -486,8 +494,6 @@ class WP_Date_Query { * @return string A validated column name value. */ public function validate_column( $column ) { - global $wpdb; - $valid_columns = array( 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', 'comment_date', 'comment_date_gmt', @@ -512,20 +518,20 @@ class WP_Date_Query { } $known_columns = array( - $wpdb->posts => array( + $this->db->posts => array( 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', ), - $wpdb->comments => array( + $this->db->comments => array( 'comment_date', 'comment_date_gmt', ), - $wpdb->users => array( + $this->db->users => array( 'user_registered', ), - $wpdb->blogs => array( + $this->db->blogs => array( 'registered', 'last_updated', ), @@ -717,8 +723,6 @@ class WP_Date_Query { * } */ protected function get_sql_for_clause( $query, $parent_query ) { - global $wpdb; - // The sub-parts of a $where part. $where_parts = array(); @@ -740,12 +744,12 @@ class WP_Date_Query { } // Range queries. - if ( ! empty( $query['after'] ) ) - $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) ); - - if ( ! empty( $query['before'] ) ) - $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); - + if ( ! empty( $query['after'] ) ) { + $where_parts[] = $this->db->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) ); + } + if ( ! empty( $query['before'] ) ) { + $where_parts[] = $this->db->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); + } // Specific value queries. 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. */ public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { - global $wpdb; - // Have to have at least one if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) ) return false; @@ -1013,6 +1015,6 @@ class WP_Date_Query { $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 ); } }