Make WordPress Core


Ignore:
Timestamp:
08/18/2016 07:47:15 PM (8 years ago)
Author:
wonderboymusic
Message:

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

See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/date.php

    r37518 r38280  
    6262     */
    6363    public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' );
     64
     65    /**
     66     * @since 4.7.0
     67     * @access protected
     68     * @var wpdb
     69     */
     70    protected $db;
    6471
    6572    /**
     
    152159     */
    153160    public function __construct( $date_query, $default_column = 'post_date' ) {
     161        $this->db = $GLOBALS['wpdb'];
    154162
    155163        if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
     
    487495     */
    488496    public function validate_column( $column ) {
    489         global $wpdb;
    490 
    491497        $valid_columns = array(
    492498            'post_date', 'post_date_gmt', 'post_modified',
     
    513519
    514520            $known_columns = array(
    515                 $wpdb->posts => array(
     521                $this->db->posts => array(
    516522                    'post_date',
    517523                    'post_date_gmt',
     
    519525                    'post_modified_gmt',
    520526                ),
    521                 $wpdb->comments => array(
     527                $this->db->comments => array(
    522528                    'comment_date',
    523529                    'comment_date_gmt',
    524530                ),
    525                 $wpdb->users => array(
     531                $this->db->users => array(
    526532                    'user_registered',
    527533                ),
    528                 $wpdb->blogs => array(
     534                $this->db->blogs => array(
    529535                    'registered',
    530536                    'last_updated',
     
    718724     */
    719725    protected function get_sql_for_clause( $query, $parent_query ) {
    720         global $wpdb;
    721 
    722726        // The sub-parts of a $where part.
    723727        $where_parts = array();
     
    741745
    742746        // Range queries.
    743         if ( ! empty( $query['after'] ) )
    744             $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
    745 
    746         if ( ! empty( $query['before'] ) )
    747             $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
    748 
     747        if ( ! empty( $query['after'] ) ) {
     748            $where_parts[] = $this->db->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
     749        }
     750        if ( ! empty( $query['before'] ) ) {
     751            $where_parts[] = $this->db->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
     752        }
    749753        // Specific value queries.
    750754
     
    959963     */
    960964    public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) {
    961         global $wpdb;
    962 
    963965        // Have to have at least one
    964966        if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) )
     
    10141016        }
    10151017
    1016         return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
     1018        return $this->db->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
    10171019    }
    10181020}
Note: See TracChangeset for help on using the changeset viewer.