Make WordPress Core


Ignore:
Timestamp:
08/18/2016 07:38:18 PM (10 years ago)
Author:
wonderboymusic
Message:

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

See #37699.

File:
1 edited

Legend:

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

    r38125 r38279  
    13201320
    13211321        private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
     1322
     1323        /**
     1324         * @since 4.7.0
     1325         * @access protected
     1326         * @var wpdb
     1327         */
     1328        protected $db;
    13221329
    13231330        /**
    … …  
    21152122         * @since 3.7.0
    21162123         *
    2117          * @global wpdb $wpdb WordPress database abstraction object.
    2118          *
    21192124         * @param array $q Query variables.
    21202125         * @return string WHERE clause.
    21212126         */
    21222127        protected function parse_search( &$q ) {
    2123                 global $wpdb;
    2124 
    21252128                $search = '';
    21262129
    … …  
    21622165
    21632166                        if ( $n && $include ) {
    2164                                 $like = '%' . $wpdb->esc_like( $term ) . '%';
    2165                                 $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
    2166                         }
    2167 
    2168                         $like = $n . $wpdb->esc_like( $term ) . $n;
    2169                         $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
     2167                                $like = '%' . $this->db->esc_like( $term ) . '%';
     2168                                $q['search_orderby_title'][] = $this->db->prepare( "{$this->db->posts}.post_title LIKE %s", $like );
     2169                        }
     2170
     2171                        $like = $n . $this->db->esc_like( $term ) . $n;
     2172                        $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like );
    21702173                        $searchand = ' AND ';
    21712174                }
    … …  
    21732176                if ( ! empty( $search ) ) {
    21742177                        $search = " AND ({$search}) ";
    2175                         if ( ! is_user_logged_in() )
    2176                                 $search .= " AND ($wpdb->posts.post_password = '') ";
     2178                        if ( ! is_user_logged_in() ) {
     2179                                $search .= " AND ({$this->db->posts}.post_password = '') ";
     2180                        }
    21772181                }
    21782182
    … …  
    22572261         * Generate SQL for the ORDER BY condition based on passed search terms.
    22582262         *
    2259          * @global wpdb $wpdb WordPress database abstraction object.
    2260          *
    22612263         * @param array $q Query variables.
    22622264         * @return string ORDER BY clause.
    22632265         */
    22642266        protected function parse_search_order( &$q ) {
    2265                 global $wpdb;
    2266 
    22672267                if ( $q['search_terms_count'] > 1 ) {
    22682268                        $num_terms = count( $q['search_orderby_title'] );
    … …  
    22712271                        $like = '';
    22722272                        if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
    2273                                 $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
     2273                                $like = '%' . $this->db->esc_like( $q['s'] ) . '%';
    22742274                        }
    22752275
    … …  
    22782278                        // sentence match in 'post_title'
    22792279                        if ( $like ) {
    2280                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
     2280                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_title LIKE %s THEN 1 ", $like );
    22812281                        }
    22822282
    … …  
    22932293                        // Sentence match in 'post_content' and 'post_excerpt'.
    22942294                        if ( $like ) {
    2295                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_excerpt LIKE %s THEN 4 ", $like );
    2296                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 5 ", $like );
     2295                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_excerpt LIKE %s THEN 4 ", $like );
     2296                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_content LIKE %s THEN 5 ", $like );
    22972297                        }
    22982298
    … …  
    23152315         * @access protected
    23162316         *
    2317          * @global wpdb $wpdb WordPress database abstraction object.
    2318          *
    23192317         * @param string $orderby Alias for the field to order by.
    23202318         * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
    23212319         */
    23222320        protected function parse_orderby( $orderby ) {
    2323                 global $wpdb;
    2324 
    23252321                // Used to filter values.
    23262322                $allowed_keys = array(
    … …  
    23692365                        case 'menu_order':
    23702366                        case 'comment_count':
    2371                                 $orderby_clause = "$wpdb->posts.{$orderby}";
     2367                                $orderby_clause = "{$this->db->posts}.{$orderby}";
    23722368                                break;
    23732369                        case 'rand':
    … …  
    23942390                                } else {
    23952391                                        // Default: order by post field.
    2396                                         $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
     2392                                        $orderby_clause = "{$this->db->posts}.post_" . sanitize_key( $orderby );
    23972393                                }
    23982394
    … …  
    24812477         * @access public
    24822478         *
    2483          * @global wpdb $wpdb WordPress database abstraction object.
    2484          *
    24852479         * @return array List of posts.
    24862480         */
    24872481        public function get_posts() {
    2488                 global $wpdb;
    2489 
    24902482                $this->parse_query();
    24912483
    … …  
    26232615                switch ( $q['fields'] ) {
    26242616                        case 'ids':
    2625                                 $fields = "$wpdb->posts.ID";
     2617                                $fields = "{$this->db->posts}.ID";
    26262618                                break;
    26272619                        case 'id=>parent':
    2628                                 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
     2620                                $fields = "{$this->db->posts}.ID, {$this->db->posts}.post_parent";
    26292621                                break;
    26302622                        default:
    2631                                 $fields = "$wpdb->posts.*";
    2632                 }
    2633 
    2634                 if ( '' !== $q['menu_order'] )
    2635                         $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
    2636 
     2623                                $fields = "{$this->db->posts}.*";
     2624                }
     2625
     2626                if ( '' !== $q['menu_order'] ) {
     2627                        $where .= " AND {$this->db->posts}.menu_order = " . $q['menu_order'];
     2628                }
    26372629                // The "m" parameter is meant for months but accepts datetimes of varying specificity
    26382630                if ( $q['m'] ) {
    2639                         $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
    2640                         if ( strlen($q['m']) > 5 )
    2641                                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
    2642                         if ( strlen($q['m']) > 7 )
    2643                                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
    2644                         if ( strlen($q['m']) > 9 )
    2645                                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
    2646                         if ( strlen($q['m']) > 11 )
    2647                                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
    2648                         if ( strlen($q['m']) > 13 )
    2649                                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
     2631                        $where .= " AND YEAR({$this->db->posts}.post_date)=" . substr($q['m'], 0, 4);
     2632                        if ( strlen($q['m']) > 5 ) {
     2633                                $where .= " AND MONTH({$this->db->posts}.post_date)=" . substr($q['m'], 4, 2);
     2634                        }
     2635                        if ( strlen($q['m']) > 7 ) {
     2636                                $where .= " AND DAYOFMONTH({$this->db->posts}.post_date)=" . substr($q['m'], 6, 2);
     2637                        }
     2638                        if ( strlen($q['m']) > 9 ) {
     2639                                $where .= " AND HOUR({$this->db->posts}.post_date)=" . substr($q['m'], 8, 2);
     2640                        }
     2641                        if ( strlen($q['m']) > 11 ) {
     2642                                $where .= " AND MINUTE({$this->db->posts}.post_date)=" . substr($q['m'], 10, 2);
     2643                        }
     2644                        if ( strlen($q['m']) > 13 ) {
     2645                                $where .= " AND SECOND({$this->db->posts}.post_date)=" . substr($q['m'], 12, 2);
     2646                        }
    26502647                }
    26512648
    … …  
    27102707
    27112708                if ( '' !== $q['title'] ) {
    2712                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_title = %s", stripslashes( $q['title'] ) );
     2709                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_title = %s", stripslashes( $q['title'] ) );
    27132710                }
    27142711
    … …  
    27162713                if ( '' != $q['name'] ) {
    27172714                        $q['name'] = sanitize_title_for_query( $q['name'] );
    2718                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
     2715                        $where .= " AND {$this->db->posts}.post_name = '" . $q['name'] . "'";
    27192716                } elseif ( '' != $q['pagename'] ) {
    27202717                        if ( isset($this->queried_object_id) ) {
    … …  
    27452742                                $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
    27462743                                $q['name'] = $q['pagename'];
    2747                                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
     2744                                $where .= " AND ({$this->db->posts}.ID = '$reqpage')";
    27482745                                $reqpage_obj = get_post( $reqpage );
    27492746                                if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
    … …  
    27572754                        $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
    27582755                        $q['name'] = $q['attachment'];
    2759                         $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
     2756                        $where .= " AND {$this->db->posts}.post_name = '" . $q['attachment'] . "'";
    27602757                } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
    27612758                        $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
    27622759                        $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
    2763                         $where .= " AND $wpdb->posts.post_name IN ($post_name__in)";
     2760                        $where .= " AND {$this->db->posts}.post_name IN ($post_name__in)";
    27642761                }
    27652762
    … …  
    27702767                // If a post number is specified, load that post
    27712768                if ( $q['p'] ) {
    2772                         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
     2769                        $where .= " AND {$this->db->posts}.ID = " . $q['p'];
    27732770                } elseif ( $q['post__in'] ) {
    27742771                        $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
    2775                         $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
     2772                        $where .= " AND {$this->db->posts}.ID IN ($post__in)";
    27762773                } elseif ( $q['post__not_in'] ) {
    27772774                        $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
    2778                         $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
     2775                        $where .= " AND {$this->db->posts}.ID NOT IN ($post__not_in)";
    27792776                }
    27802777
    27812778                if ( is_numeric( $q['post_parent'] ) ) {
    2782                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
     2779                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_parent = %d ", $q['post_parent'] );
    27832780                } elseif ( $q['post_parent__in'] ) {
    27842781                        $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
    2785                         $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
     2782                        $where .= " AND {$this->db->posts}.post_parent IN ($post_parent__in)";
    27862783                } elseif ( $q['post_parent__not_in'] ) {
    27872784                        $post_parent__not_in = implode( ',',  array_map( 'absint', $q['post_parent__not_in'] ) );
    2788                         $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
     2785                        $where .= " AND {$this->db->posts}.post_parent NOT IN ($post_parent__not_in)";
    27892786                }
    27902787
    … …  
    27922789                        if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
    27932790                                $q['p'] = $q['page_id'];
    2794                                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
     2791                                $where = " AND {$this->db->posts}.ID = " . $q['page_id'];
    27952792                        }
    27962793                }
    … …  
    28172814                        $this->parse_tax_query( $q );
    28182815
    2819                         $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
     2816                        $clauses = $this->tax_query->get_sql( $this->db->posts, 'ID' );
    28202817
    28212818                        $join .= $clauses['join'];
    … …  
    29012898
    29022899                if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    2903                         $groupby = "{$wpdb->posts}.ID";
     2900                        $groupby = "{$this->db->posts}.ID";
    29042901                }
    29052902
    … …  
    29182915                if ( ! empty( $q['author__not_in'] ) ) {
    29192916                        $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
    2920                         $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
     2917                        $where .= " AND {$this->db->posts}.post_author NOT IN ($author__not_in) ";
    29212918                } elseif ( ! empty( $q['author__in'] ) ) {
    29222919                        $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
    2923                         $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
     2920                        $where .= " AND {$this->db->posts}.post_author IN ($author__in) ";
    29242921                }
    29252922
    … …  
    29392936                        if ( $q['author'] )
    29402937                                $q['author'] = $q['author']->ID;
    2941                         $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
     2938                        $whichauthor .= " AND ({$this->db->posts}.post_author = " . absint($q['author']) . ')';
    29422939                }
    29432940
    29442941                // MIME-Type stuff for attachment browsing
    29452942
    2946                 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
    2947                         $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
    2948 
     2943                if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) {
     2944                        $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $this->db->posts );
     2945                }
    29492946                $where .= $search . $whichauthor . $whichmimetype;
    29502947
    29512948                if ( ! empty( $this->meta_query->queries ) ) {
    2952                         $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     2949                        $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this );
    29532950                        $join   .= $clauses['join'];
    29542951                        $where  .= $clauses['where'];
    … …  
    29712968                                $orderby = '';
    29722969                        } else {
    2973                                 $orderby = "$wpdb->posts.post_date " . $q['order'];
     2970                                $orderby = "{$this->db->posts}.post_date " . $q['order'];
    29742971                        }
    29752972                } elseif ( 'none' == $q['orderby'] ) {
    29762973                        $orderby = '';
    29772974                } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
    2978                         $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
     2975                        $orderby = "FIELD( {$this->db->posts}.ID, $post__in )";
    29792976                } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
    2980                         $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
     2977                        $orderby = "FIELD( {$this->db->posts}.post_parent, $post_parent__in )";
    29812978                } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
    2982                         $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
     2979                        $orderby = "FIELD( {$this->db->posts}.post_name, $post_name__in )";
    29832980                } else {
    29842981                        $orderby_array = array();
    … …  
    30123009
    30133010                                if ( empty( $orderby ) ) {
    3014                                         $orderby = "$wpdb->posts.post_date " . $q['order'];
     3011                                        $orderby = "{$this->db->posts}.post_date " . $q['order'];
    30153012                                } elseif ( ! empty( $q['order'] ) ) {
    30163013                                        $orderby .= " {$q['order']}";
    … …  
    30523049
    30533050                if ( isset( $q['post_password'] ) ) {
    3054                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
     3051                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_password = %s", $q['post_password'] );
    30553052                        if ( empty( $q['perm'] ) ) {
    30563053                                $q['perm'] = 'readable';
    30573054                        }
    30583055                } elseif ( isset( $q['has_password'] ) ) {
    3059                         $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
     3056                        $where .= sprintf( " AND {$this->db->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' );
    30603057                }
    30613058
    30623059                if ( ! empty( $q['comment_status'] ) ) {
    3063                         $where .= $wpdb->prepare( " AND $wpdb->posts.comment_status = %s ", $q['comment_status'] );
     3060                        $where .= $this->db->prepare( " AND {$this->db->posts}.comment_status = %s ", $q['comment_status'] );
    30643061                }
    30653062
    30663063                if ( ! empty( $q['ping_status'] ) )  {
    3067                         $where .= $wpdb->prepare( " AND $wpdb->posts.ping_status = %s ", $q['ping_status'] );
     3064                        $where .= $this->db->prepare( " AND {$this->db->posts}.ping_status = %s ", $q['ping_status'] );
    30683065                }
    30693066
    30703067                if ( 'any' == $post_type ) {
    30713068                        $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
    3072                         if ( empty( $in_search_post_types ) )
     3069                        if ( empty( $in_search_post_types ) ) {
    30733070                                $where .= ' AND 1=0 ';
    3074                         else
    3075                                 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
     3071                        } else {
     3072                                $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
     3073                        }
    30763074                } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
    3077                         $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
     3075                        $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $post_type) . "')";
    30783076                } elseif ( ! empty( $post_type ) ) {
    3079                         $where .= " AND $wpdb->posts.post_type = '$post_type'";
     3077                        $where .= " AND {$this->db->posts}.post_type = '$post_type'";
    30803078                        $post_type_object = get_post_type_object ( $post_type );
    30813079                } elseif ( $this->is_attachment ) {
    3082                         $where .= " AND $wpdb->posts.post_type = 'attachment'";
     3080                        $where .= " AND {$this->db->posts}.post_type = 'attachment'";
    30833081                        $post_type_object = get_post_type_object ( 'attachment' );
    30843082                } elseif ( $this->is_page ) {
    3085                         $where .= " AND $wpdb->posts.post_type = 'page'";
     3083                        $where .= " AND {$this->db->posts}.post_type = 'page'";
    30863084                        $post_type_object = get_post_type_object ( 'page' );
    30873085                } else {
    3088                         $where .= " AND $wpdb->posts.post_type = 'post'";
     3086                        $where .= " AND {$this->db->posts}.post_type = 'post'";
    30893087                        $post_type_object = get_post_type_object ( 'post' );
    30903088                }
    … …  
    31153113                                foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
    31163114                                        if ( ! in_array( $status, $q_status ) ) {
    3117                                                 $e_status[] = "$wpdb->posts.post_status <> '$status'";
     3115                                                $e_status[] = "{$this->db->posts}.post_status <> '$status'";
    31183116                                        }
    31193117                                }
    … …  
    31213119                                foreach ( get_post_stati() as $status ) {
    31223120                                        if ( in_array( $status, $q_status ) ) {
    3123                                                 if ( 'private' == $status )
    3124                                                         $p_status[] = "$wpdb->posts.post_status = '$status'";
    3125                                                 else
    3126                                                         $r_status[] = "$wpdb->posts.post_status = '$status'";
     3121                                                if ( 'private' == $status ) {
     3122                                                        $p_status[] = "{$this->db->posts}.post_status = '$status'";
     3123                                                } else {
     3124                                                        $r_status[] = "{$this->db->posts}.post_status = '$status'";
     3125                                                }
    31273126                                        }
    31283127                                }
    … …  
    31383137                        }
    31393138                        if ( !empty($r_status) ) {
    3140                                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
    3141                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
    3142                                 else
     3139                                if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) {
     3140                                        $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
     3141                                } else {
    31433142                                        $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
     3143                                }
    31443144                        }
    31453145                        if ( !empty($p_status) ) {
    3146                                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
    3147                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
    3148                                 else
     3146                                if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) {
     3147                                        $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
     3148                                } else {
    31493149                                        $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
     3150                                }
    31503151                        }
    31513152                        if ( $post_status_join ) {
    3152                                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
    3153                                 foreach ( $statuswheres as $index => $statuswhere )
    3154                                         $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
     3153                                $join .= " LEFT JOIN {$this->db->posts} AS p2 ON ({$this->db->posts}.post_parent = p2.ID) ";
     3154                                foreach ( $statuswheres as $index => $statuswhere ) {
     3155                                        $statuswheres[$index] = "($statuswhere OR ({$this->db->posts}.post_status = 'inherit' AND " . str_replace( $this->db->posts, 'p2', $statuswhere ) . "))";
     3156                                }
    31553157                        }
    31563158                        $where_status = implode( ' OR ', $statuswheres );
    … …  
    31593161                        }
    31603162                } elseif ( !$this->is_singular ) {
    3161                         $where .= " AND ($wpdb->posts.post_status = 'publish'";
     3163                        $where .= " AND ({$this->db->posts}.post_status = 'publish'";
    31623164
    31633165                        // Add public states.
    … …  
    31663168                                if ( 'publish' == $state ) // Publish is hard-coded above.
    31673169                                        continue;
    3168                                 $where .= " OR $wpdb->posts.post_status = '$state'";
     3170                                $where .= " OR {$this->db->posts}.post_status = '$state'";
    31693171                        }
    31703172
    … …  
    31723174                                // Add protected states that should show in the admin all list.
    31733175                                $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
    3174                                 foreach ( (array) $admin_all_states as $state )
    3175                                         $where .= " OR $wpdb->posts.post_status = '$state'";
     3176                                foreach ( (array) $admin_all_states as $state ) {
     3177                                        $where .= " OR {$this->db->posts}.post_status = '$state'";
     3178                                }
    31763179                        }
    31773180
    … …  
    31793182                                // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
    31803183                                $private_states = get_post_stati( array('private' => true) );
    3181                                 foreach ( (array) $private_states as $state )
    3182                                         $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
     3184                                foreach ( (array) $private_states as $state ) {
     3185                                        $where .= current_user_can( $read_private_cap ) ? " OR {$this->db->posts}.post_status = '$state'" : " OR {$this->db->posts}.post_author = $user_id AND {$this->db->posts}.post_status = '$state'";
     3186                                }
    31833187                        }
    31843188
    … …  
    32313235                if ( $this->is_comment_feed && ! $this->is_singular ) {
    32323236                        if ( $this->is_archive || $this->is_search ) {
    3233                                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
     3237                                $cjoin = "JOIN {$this->db->posts} ON ({$this->db->comments}.comment_post_ID = {$this->db->posts}.ID) $join ";
    32343238                                $cwhere = "WHERE comment_approved = '1' $where";
    3235                                 $cgroupby = "$wpdb->comments.comment_id";
     3239                                $cgroupby = "{$this->db->comments}.comment_id";
    32363240                        } else { // Other non singular e.g. front
    3237                                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
     3241                                $cjoin = "JOIN {$this->db->posts} ON ( {$this->db->comments}.comment_post_ID = {$this->db->posts}.ID )";
    32383242                                $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' && post_type = 'attachment' ) ) AND comment_approved = '1'";
    32393243                                $cgroupby = '';
    … …  
    32943298                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
    32953299
    3296                         $comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
     3300                        $comments = (array) $this->db->get_results("SELECT $distinct {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits");
    32973301                        // Convert to WP_Comment
    32983302                        $this->comments = array_map( 'get_comment', $comments );
    … …  
    33063310                        $post_ids = join(',', $post_ids);
    33073311                        $join = '';
    3308                         if ( $post_ids )
    3309                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
    3310                         else
     3312                        if ( $post_ids ) {
     3313                                $where = "AND {$this->db->posts}.ID IN ($post_ids) ";
     3314                        } else {
    33113315                                $where = "AND 0";
     3316                        }
    33123317                }
    33133318
    … …  
    35483553                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    35493554
    3550                 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     3555                $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
    35513556
    35523557                if ( !$q['suppress_filters'] ) {
    … …  
    35823587                if ( 'ids' == $q['fields'] ) {
    35833588                        if ( null === $this->posts ) {
    3584                                 $this->posts = $wpdb->get_col( $this->request );
     3589                                $this->posts = $this->db->get_col( $this->request );
    35853590                        }
    35863591
    … …  
    35943599                if ( 'id=>parent' == $q['fields'] ) {
    35953600                        if ( null === $this->posts ) {
    3596                                 $this->posts = $wpdb->get_results( $this->request );
     3601                                $this->posts = $this->db->get_results( $this->request );
    35973602                        }
    35983603
    … …  
    36123617
    36133618                if ( null === $this->posts ) {
    3614                         $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
     3619                        $split_the_query = ( $old_request == $this->request && "{$this->db->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
    36153620
    36163621                        /**
    … …  
    36313636                                // First get the IDs and then fill in the objects
    36323637
    3633                                 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     3638                                $this->request = "SELECT $found_rows $distinct {$this->db->posts}.ID FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
    36343639
    36353640                                /**
    … …  
    36433648                                $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
    36443649
    3645                                 $ids = $wpdb->get_col( $this->request );
     3650                                $ids = $this->db->get_col( $this->request );
    36463651
    36473652                                if ( $ids ) {
    … …  
    36533658                                }
    36543659                        } else {
    3655                                 $this->posts = $wpdb->get_results( $this->request );
     3660                                $this->posts = $this->db->get_results( $this->request );
    36563661                                $this->set_found_posts( $q, $limits );
    36573662                        }
    … …  
    36933698                        $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
    36943699
    3695                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
    3696                         $comments = $wpdb->get_results($comments_request);
     3700                        $comments_request = "SELECT {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits";
     3701                        $comments = $this->db->get_results($comments_request);
    36973702                        // Convert to WP_Comment
    36983703                        $this->comments = array_map( 'get_comment', $comments );
    … …  
    38373842         * @access private
    38383843         *
    3839          * @global wpdb $wpdb WordPress database abstraction object.
    3840          *
    38413844         * @param array  $q      Query variables.
    38423845         * @param string $limits LIMIT clauses of the query.
    38433846         */
    38443847        private function set_found_posts( $q, $limits ) {
    3845                 global $wpdb;
    3846 
    38473848                // Bail if posts is an empty array. Continue if posts is an empty string,
    38483849                // null, or false to accommodate caching plugins that fill posts later.
    … …  
    38593860                         * @param WP_Query &$this       The WP_Query instance (passed by reference).
    38603861                         */
    3861                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     3862                        $this->found_posts = $this->db->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
    38623863                } else {
    38633864                        $this->found_posts = count( $this->posts );
    … …  
    41574158         * @param string|array $query URL query string or array of vars.
    41584159         */
    4159         public function __construct($query = '') {
    4160                 if ( ! empty($query) ) {
    4161                         $this->query($query);
     4160        public function __construct( $query = '' ) {
     4161                $this->db = $GLOBALS['wpdb'];
     4162
     4163                if ( ! empty( $query ) ) {
     4164                        $this->query( $query );
    41624165                }
    41634166        }
Note: See TracChangeset for help on using the changeset viewer.