Ticket #37699: 37699-query.diff
File 37699-query.diff, 26.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/query.php
1321 1321 private $compat_methods = array( 'init_query_flags', 'parse_tax_query' ); 1322 1322 1323 1323 /** 1324 * @since 4.7.0 1325 * @access protected 1326 * @var wpdb 1327 */ 1328 protected $db; 1329 1330 /** 1324 1331 * Resets query flags to false. 1325 1332 * 1326 1333 * The query flags are what page info WordPress was able to figure out. … … 2114 2121 * 2115 2122 * @since 3.7.0 2116 2123 * 2117 * @global wpdb $wpdb WordPress database abstraction object.2118 *2119 2124 * @param array $q Query variables. 2120 2125 * @return string WHERE clause. 2121 2126 */ 2122 2127 protected function parse_search( &$q ) { 2123 global $wpdb;2124 2125 2128 $search = ''; 2126 2129 2127 2130 // added slashes screw with quote grouping when done early, so done later … … 2161 2164 } 2162 2165 2163 2166 if ( $n && $include ) { 2164 $like = '%' . $ wpdb->esc_like( $term ) . '%';2165 $q['search_orderby_title'][] = $ wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );2167 $like = '%' . $this->db->esc_like( $term ) . '%'; 2168 $q['search_orderby_title'][] = $this->db->prepare( "{$this->db->posts}.post_title LIKE %s", $like ); 2166 2169 } 2167 2170 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 );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 ); 2170 2173 $searchand = ' AND '; 2171 2174 } 2172 2175 … … 2173 2176 if ( ! empty( $search ) ) { 2174 2177 $search = " AND ({$search}) "; 2175 2178 if ( ! is_user_logged_in() ) 2176 $search .= " AND ( $wpdb->posts.post_password = '') ";2179 $search .= " AND ({$this->db->posts}.post_password = '') "; 2177 2180 } 2178 2181 2179 2182 return $search; … … 2256 2259 /** 2257 2260 * Generate SQL for the ORDER BY condition based on passed search terms. 2258 2261 * 2259 * @global wpdb $wpdb WordPress database abstraction object.2260 *2261 2262 * @param array $q Query variables. 2262 2263 * @return string ORDER BY clause. 2263 2264 */ 2264 2265 protected function parse_search_order( &$q ) { 2265 global $wpdb;2266 2267 2266 if ( $q['search_terms_count'] > 1 ) { 2268 2267 $num_terms = count( $q['search_orderby_title'] ); 2269 2268 … … 2270 2269 // If the search terms contain negative queries, don't bother ordering by sentence matches. 2271 2270 $like = ''; 2272 2271 if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) { 2273 $like = '%' . $ wpdb->esc_like( $q['s'] ) . '%';2272 $like = '%' . $this->db->esc_like( $q['s'] ) . '%'; 2274 2273 } 2275 2274 2276 2275 $search_orderby = ''; … … 2277 2276 2278 2277 // sentence match in 'post_title' 2279 2278 if ( $like ) { 2280 $search_orderby .= $ wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );2279 $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_title LIKE %s THEN 1 ", $like ); 2281 2280 } 2282 2281 2283 2282 // sanity limit, sort as sentence when more than 6 terms … … 2292 2291 2293 2292 // Sentence match in 'post_content' and 'post_excerpt'. 2294 2293 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 );2294 $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_excerpt LIKE %s THEN 4 ", $like ); 2295 $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_content LIKE %s THEN 5 ", $like ); 2297 2296 } 2298 2297 2299 2298 if ( $search_orderby ) { … … 2314 2313 * @since 4.0.0 2315 2314 * @access protected 2316 2315 * 2317 * @global wpdb $wpdb WordPress database abstraction object.2318 *2319 2316 * @param string $orderby Alias for the field to order by. 2320 2317 * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise. 2321 2318 */ 2322 2319 protected function parse_orderby( $orderby ) { 2323 global $wpdb;2324 2325 2320 // Used to filter values. 2326 2321 $allowed_keys = array( 2327 2322 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified', … … 2368 2363 case 'ID': 2369 2364 case 'menu_order': 2370 2365 case 'comment_count': 2371 $orderby_clause = " $wpdb->posts.{$orderby}";2366 $orderby_clause = "{$this->db->posts}.{$orderby}"; 2372 2367 break; 2373 2368 case 'rand': 2374 2369 $orderby_clause = 'RAND()'; … … 2393 2388 $orderby_clause = $orderby; 2394 2389 } else { 2395 2390 // Default: order by post field. 2396 $orderby_clause = " $wpdb->posts.post_" . sanitize_key( $orderby );2391 $orderby_clause = "{$this->db->posts}.post_" . sanitize_key( $orderby ); 2397 2392 } 2398 2393 2399 2394 break; … … 2480 2475 * @since 1.5.0 2481 2476 * @access public 2482 2477 * 2483 * @global wpdb $wpdb WordPress database abstraction object.2484 *2485 2478 * @return array List of posts. 2486 2479 */ 2487 2480 public function get_posts() { 2488 global $wpdb;2489 2490 2481 $this->parse_query(); 2491 2482 2492 2483 /** … … 2622 2613 2623 2614 switch ( $q['fields'] ) { 2624 2615 case 'ids': 2625 $fields = " $wpdb->posts.ID";2616 $fields = "{$this->db->posts}.ID"; 2626 2617 break; 2627 2618 case 'id=>parent': 2628 $fields = " $wpdb->posts.ID, $wpdb->posts.post_parent";2619 $fields = "{$this->db->posts}.ID, {$this->db->posts}.post_parent"; 2629 2620 break; 2630 2621 default: 2631 $fields = " $wpdb->posts.*";2622 $fields = "{$this->db->posts}.*"; 2632 2623 } 2633 2624 2634 2625 if ( '' !== $q['menu_order'] ) 2635 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];2626 $where .= " AND {$this->db->posts}.menu_order = " . $q['menu_order']; 2636 2627 2637 2628 // The "m" parameter is meant for months but accepts datetimes of varying specificity 2638 2629 if ( $q['m'] ) { 2639 $where .= " AND YEAR( $wpdb->posts.post_date)=" . substr($q['m'], 0, 4);2630 $where .= " AND YEAR({$this->db->posts}.post_date)=" . substr($q['m'], 0, 4); 2640 2631 if ( strlen($q['m']) > 5 ) 2641 $where .= " AND MONTH( $wpdb->posts.post_date)=" . substr($q['m'], 4, 2);2632 $where .= " AND MONTH({$this->db->posts}.post_date)=" . substr($q['m'], 4, 2); 2642 2633 if ( strlen($q['m']) > 7 ) 2643 $where .= " AND DAYOFMONTH( $wpdb->posts.post_date)=" . substr($q['m'], 6, 2);2634 $where .= " AND DAYOFMONTH({$this->db->posts}.post_date)=" . substr($q['m'], 6, 2); 2644 2635 if ( strlen($q['m']) > 9 ) 2645 $where .= " AND HOUR( $wpdb->posts.post_date)=" . substr($q['m'], 8, 2);2636 $where .= " AND HOUR({$this->db->posts}.post_date)=" . substr($q['m'], 8, 2); 2646 2637 if ( strlen($q['m']) > 11 ) 2647 $where .= " AND MINUTE( $wpdb->posts.post_date)=" . substr($q['m'], 10, 2);2638 $where .= " AND MINUTE({$this->db->posts}.post_date)=" . substr($q['m'], 10, 2); 2648 2639 if ( strlen($q['m']) > 13 ) 2649 $where .= " AND SECOND( $wpdb->posts.post_date)=" . substr($q['m'], 12, 2);2640 $where .= " AND SECOND({$this->db->posts}.post_date)=" . substr($q['m'], 12, 2); 2650 2641 } 2651 2642 2652 2643 // Handle the other individual date parameters … … 2709 2700 } 2710 2701 2711 2702 if ( '' !== $q['title'] ) { 2712 $where .= $ wpdb->prepare( " AND $wpdb->posts.post_title = %s", stripslashes( $q['title'] ) );2703 $where .= $this->db->prepare( " AND {$this->db->posts}.post_title = %s", stripslashes( $q['title'] ) ); 2713 2704 } 2714 2705 2715 2706 // Parameters related to 'post_name'. 2716 2707 if ( '' != $q['name'] ) { 2717 2708 $q['name'] = sanitize_title_for_query( $q['name'] ); 2718 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";2709 $where .= " AND {$this->db->posts}.post_name = '" . $q['name'] . "'"; 2719 2710 } elseif ( '' != $q['pagename'] ) { 2720 2711 if ( isset($this->queried_object_id) ) { 2721 2712 $reqpage = $this->queried_object_id; … … 2744 2735 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) { 2745 2736 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); 2746 2737 $q['name'] = $q['pagename']; 2747 $where .= " AND ( $wpdb->posts.ID = '$reqpage')";2738 $where .= " AND ({$this->db->posts}.ID = '$reqpage')"; 2748 2739 $reqpage_obj = get_post( $reqpage ); 2749 2740 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) { 2750 2741 $this->is_attachment = true; … … 2756 2747 } elseif ( '' != $q['attachment'] ) { 2757 2748 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); 2758 2749 $q['name'] = $q['attachment']; 2759 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";2750 $where .= " AND {$this->db->posts}.post_name = '" . $q['attachment'] . "'"; 2760 2751 } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { 2761 2752 $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); 2762 2753 $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; 2763 $where .= " AND $wpdb->posts.post_name IN ($post_name__in)";2754 $where .= " AND {$this->db->posts}.post_name IN ($post_name__in)"; 2764 2755 } 2765 2756 2766 2757 // If an attachment is requested by number, let it supersede any post number. … … 2769 2760 2770 2761 // If a post number is specified, load that post 2771 2762 if ( $q['p'] ) { 2772 $where .= " AND {$ wpdb->posts}.ID = " . $q['p'];2763 $where .= " AND {$this->db->posts}.ID = " . $q['p']; 2773 2764 } elseif ( $q['post__in'] ) { 2774 2765 $post__in = implode(',', array_map( 'absint', $q['post__in'] )); 2775 $where .= " AND {$ wpdb->posts}.ID IN ($post__in)";2766 $where .= " AND {$this->db->posts}.ID IN ($post__in)"; 2776 2767 } elseif ( $q['post__not_in'] ) { 2777 2768 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] )); 2778 $where .= " AND {$ wpdb->posts}.ID NOT IN ($post__not_in)";2769 $where .= " AND {$this->db->posts}.ID NOT IN ($post__not_in)"; 2779 2770 } 2780 2771 2781 2772 if ( is_numeric( $q['post_parent'] ) ) { 2782 $where .= $ wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );2773 $where .= $this->db->prepare( " AND {$this->db->posts}.post_parent = %d ", $q['post_parent'] ); 2783 2774 } elseif ( $q['post_parent__in'] ) { 2784 2775 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); 2785 $where .= " AND {$ wpdb->posts}.post_parent IN ($post_parent__in)";2776 $where .= " AND {$this->db->posts}.post_parent IN ($post_parent__in)"; 2786 2777 } elseif ( $q['post_parent__not_in'] ) { 2787 2778 $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)";2779 $where .= " AND {$this->db->posts}.post_parent NOT IN ($post_parent__not_in)"; 2789 2780 } 2790 2781 2791 2782 if ( $q['page_id'] ) { 2792 2783 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) { 2793 2784 $q['p'] = $q['page_id']; 2794 $where = " AND {$ wpdb->posts}.ID = " . $q['page_id'];2785 $where = " AND {$this->db->posts}.ID = " . $q['page_id']; 2795 2786 } 2796 2787 } 2797 2788 … … 2816 2807 if ( !$this->is_singular ) { 2817 2808 $this->parse_tax_query( $q ); 2818 2809 2819 $clauses = $this->tax_query->get_sql( $ wpdb->posts, 'ID' );2810 $clauses = $this->tax_query->get_sql( $this->db->posts, 'ID' ); 2820 2811 2821 2812 $join .= $clauses['join']; 2822 2813 $where .= $clauses['where']; … … 2900 2891 } 2901 2892 2902 2893 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { 2903 $groupby = "{$ wpdb->posts}.ID";2894 $groupby = "{$this->db->posts}.ID"; 2904 2895 } 2905 2896 2906 2897 // Author/user stuff … … 2917 2908 2918 2909 if ( ! empty( $q['author__not_in'] ) ) { 2919 2910 $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) ";2911 $where .= " AND {$this->db->posts}.post_author NOT IN ($author__not_in) "; 2921 2912 } elseif ( ! empty( $q['author__in'] ) ) { 2922 2913 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); 2923 $where .= " AND {$ wpdb->posts}.post_author IN ($author__in) ";2914 $where .= " AND {$this->db->posts}.post_author IN ($author__in) "; 2924 2915 } 2925 2916 2926 2917 // Author stuff for nice URLs … … 2938 2929 $q['author'] = get_user_by('slug', $q['author_name']); 2939 2930 if ( $q['author'] ) 2940 2931 $q['author'] = $q['author']->ID; 2941 $whichauthor .= " AND ( $wpdb->posts.post_author = " . absint($q['author']) . ')';2932 $whichauthor .= " AND ({$this->db->posts}.post_author = " . absint($q['author']) . ')'; 2942 2933 } 2943 2934 2944 2935 // MIME-Type stuff for attachment browsing 2945 2936 2946 2937 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) 2947 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $ wpdb->posts );2938 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $this->db->posts ); 2948 2939 2949 2940 $where .= $search . $whichauthor . $whichmimetype; 2950 2941 2951 2942 if ( ! empty( $this->meta_query->queries ) ) { 2952 $clauses = $this->meta_query->get_sql( 'post', $ wpdb->posts, 'ID', $this );2943 $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this ); 2953 2944 $join .= $clauses['join']; 2954 2945 $where .= $clauses['where']; 2955 2946 } … … 2970 2961 if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { 2971 2962 $orderby = ''; 2972 2963 } else { 2973 $orderby = " $wpdb->posts.post_date " . $q['order'];2964 $orderby = "{$this->db->posts}.post_date " . $q['order']; 2974 2965 } 2975 2966 } elseif ( 'none' == $q['orderby'] ) { 2976 2967 $orderby = ''; 2977 2968 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) { 2978 $orderby = "FIELD( {$ wpdb->posts}.ID, $post__in )";2969 $orderby = "FIELD( {$this->db->posts}.ID, $post__in )"; 2979 2970 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) { 2980 $orderby = "FIELD( {$ wpdb->posts}.post_parent, $post_parent__in )";2971 $orderby = "FIELD( {$this->db->posts}.post_parent, $post_parent__in )"; 2981 2972 } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) { 2982 $orderby = "FIELD( {$ wpdb->posts}.post_name, $post_name__in )";2973 $orderby = "FIELD( {$this->db->posts}.post_name, $post_name__in )"; 2983 2974 } else { 2984 2975 $orderby_array = array(); 2985 2976 if ( is_array( $q['orderby'] ) ) { … … 3011 3002 $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); 3012 3003 3013 3004 if ( empty( $orderby ) ) { 3014 $orderby = " $wpdb->posts.post_date " . $q['order'];3005 $orderby = "{$this->db->posts}.post_date " . $q['order']; 3015 3006 } elseif ( ! empty( $q['order'] ) ) { 3016 3007 $orderby .= " {$q['order']}"; 3017 3008 } … … 3051 3042 } 3052 3043 3053 3044 if ( isset( $q['post_password'] ) ) { 3054 $where .= $ wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );3045 $where .= $this->db->prepare( " AND {$this->db->posts}.post_password = %s", $q['post_password'] ); 3055 3046 if ( empty( $q['perm'] ) ) { 3056 3047 $q['perm'] = 'readable'; 3057 3048 } 3058 3049 } elseif ( isset( $q['has_password'] ) ) { 3059 $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );3050 $where .= sprintf( " AND {$this->db->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); 3060 3051 } 3061 3052 3062 3053 if ( ! empty( $q['comment_status'] ) ) { 3063 $where .= $ wpdb->prepare( " AND $wpdb->posts.comment_status = %s ", $q['comment_status'] );3054 $where .= $this->db->prepare( " AND {$this->db->posts}.comment_status = %s ", $q['comment_status'] ); 3064 3055 } 3065 3056 3066 3057 if ( ! empty( $q['ping_status'] ) ) { 3067 $where .= $ wpdb->prepare( " AND $wpdb->posts.ping_status = %s ", $q['ping_status'] );3058 $where .= $this->db->prepare( " AND {$this->db->posts}.ping_status = %s ", $q['ping_status'] ); 3068 3059 } 3069 3060 3070 3061 if ( 'any' == $post_type ) { … … 3072 3063 if ( empty( $in_search_post_types ) ) 3073 3064 $where .= ' AND 1=0 '; 3074 3065 else 3075 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";3066 $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $in_search_post_types ) . "')"; 3076 3067 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) { 3077 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";3068 $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $post_type) . "')"; 3078 3069 } elseif ( ! empty( $post_type ) ) { 3079 $where .= " AND $wpdb->posts.post_type = '$post_type'";3070 $where .= " AND {$this->db->posts}.post_type = '$post_type'"; 3080 3071 $post_type_object = get_post_type_object ( $post_type ); 3081 3072 } elseif ( $this->is_attachment ) { 3082 $where .= " AND $wpdb->posts.post_type = 'attachment'";3073 $where .= " AND {$this->db->posts}.post_type = 'attachment'"; 3083 3074 $post_type_object = get_post_type_object ( 'attachment' ); 3084 3075 } elseif ( $this->is_page ) { 3085 $where .= " AND $wpdb->posts.post_type = 'page'";3076 $where .= " AND {$this->db->posts}.post_type = 'page'"; 3086 3077 $post_type_object = get_post_type_object ( 'page' ); 3087 3078 } else { 3088 $where .= " AND $wpdb->posts.post_type = 'post'";3079 $where .= " AND {$this->db->posts}.post_type = 'post'"; 3089 3080 $post_type_object = get_post_type_object ( 'post' ); 3090 3081 } 3091 3082 … … 3114 3105 if ( in_array( 'any', $q_status ) ) { 3115 3106 foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { 3116 3107 if ( ! in_array( $status, $q_status ) ) { 3117 $e_status[] = " $wpdb->posts.post_status <> '$status'";3108 $e_status[] = "{$this->db->posts}.post_status <> '$status'"; 3118 3109 } 3119 3110 } 3120 3111 } else { … … 3121 3112 foreach ( get_post_stati() as $status ) { 3122 3113 if ( in_array( $status, $q_status ) ) { 3123 3114 if ( 'private' == $status ) 3124 $p_status[] = " $wpdb->posts.post_status = '$status'";3115 $p_status[] = "{$this->db->posts}.post_status = '$status'"; 3125 3116 else 3126 $r_status[] = " $wpdb->posts.post_status = '$status'";3117 $r_status[] = "{$this->db->posts}.post_status = '$status'"; 3127 3118 } 3128 3119 } 3129 3120 } … … 3138 3129 } 3139 3130 if ( !empty($r_status) ) { 3140 3131 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 ) . "))";3132 $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))"; 3142 3133 else 3143 3134 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; 3144 3135 } 3145 3136 if ( !empty($p_status) ) { 3146 3137 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 ) . "))";3138 $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))"; 3148 3139 else 3149 3140 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; 3150 3141 } 3151 3142 if ( $post_status_join ) { 3152 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";3143 $join .= " LEFT JOIN {$this->db->posts} AS p2 ON ({$this->db->posts}.post_parent = p2.ID) "; 3153 3144 foreach ( $statuswheres as $index => $statuswhere ) 3154 $statuswheres[$index] = "($statuswhere OR ( $wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";3145 $statuswheres[$index] = "($statuswhere OR ({$this->db->posts}.post_status = 'inherit' AND " . str_replace( $this->db->posts, 'p2', $statuswhere ) . "))"; 3155 3146 } 3156 3147 $where_status = implode( ' OR ', $statuswheres ); 3157 3148 if ( ! empty( $where_status ) ) { … … 3158 3149 $where .= " AND ($where_status)"; 3159 3150 } 3160 3151 } elseif ( !$this->is_singular ) { 3161 $where .= " AND ( $wpdb->posts.post_status = 'publish'";3152 $where .= " AND ({$this->db->posts}.post_status = 'publish'"; 3162 3153 3163 3154 // Add public states. 3164 3155 $public_states = get_post_stati( array('public' => true) ); … … 3165 3156 foreach ( (array) $public_states as $state ) { 3166 3157 if ( 'publish' == $state ) // Publish is hard-coded above. 3167 3158 continue; 3168 $where .= " OR $wpdb->posts.post_status = '$state'";3159 $where .= " OR {$this->db->posts}.post_status = '$state'"; 3169 3160 } 3170 3161 3171 3162 if ( $this->is_admin ) { … … 3172 3163 // Add protected states that should show in the admin all list. 3173 3164 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) ); 3174 3165 foreach ( (array) $admin_all_states as $state ) 3175 $where .= " OR $wpdb->posts.post_status = '$state'";3166 $where .= " OR {$this->db->posts}.post_status = '$state'"; 3176 3167 } 3177 3168 3178 3169 if ( is_user_logged_in() ) { … … 3179 3170 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. 3180 3171 $private_states = get_post_stati( array('private' => true) ); 3181 3172 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'";3173 $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'"; 3183 3174 } 3184 3175 3185 3176 $where .= ')'; … … 3230 3221 // Comments feeds 3231 3222 if ( $this->is_comment_feed && ! $this->is_singular ) { 3232 3223 if ( $this->is_archive || $this->is_search ) { 3233 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";3224 $cjoin = "JOIN {$this->db->posts} ON ({$this->db->comments}.comment_post_ID = {$this->db->posts}.ID) $join "; 3234 3225 $cwhere = "WHERE comment_approved = '1' $where"; 3235 $cgroupby = " $wpdb->comments.comment_id";3226 $cgroupby = "{$this->db->comments}.comment_id"; 3236 3227 } else { // Other non singular e.g. front 3237 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";3228 $cjoin = "JOIN {$this->db->posts} ON ( {$this->db->comments}.comment_post_ID = {$this->db->posts}.ID )"; 3238 3229 $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' && post_type = 'attachment' ) ) AND comment_approved = '1'"; 3239 3230 $cgroupby = ''; 3240 3231 } … … 3293 3284 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 3294 3285 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 3295 3286 3296 $comments = (array) $ wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments$cjoin $cwhere $cgroupby $corderby $climits");3287 $comments = (array) $this->db->get_results("SELECT $distinct {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits"); 3297 3288 // Convert to WP_Comment 3298 3289 $this->comments = array_map( 'get_comment', $comments ); 3299 3290 $this->comment_count = count($this->comments); … … 3306 3297 $post_ids = join(',', $post_ids); 3307 3298 $join = ''; 3308 3299 if ( $post_ids ) 3309 $where = "AND $wpdb->posts.ID IN ($post_ids) ";3300 $where = "AND {$this->db->posts}.ID IN ($post_ids) "; 3310 3301 else 3311 3302 $where = "AND 0"; 3312 3303 } … … 3547 3538 if ( !$q['no_found_rows'] && !empty($limits) ) 3548 3539 $found_rows = 'SQL_CALC_FOUND_ROWS'; 3549 3540 3550 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts$join WHERE 1=1 $where $groupby $orderby $limits";3541 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; 3551 3542 3552 3543 if ( !$q['suppress_filters'] ) { 3553 3544 /** … … 3581 3572 3582 3573 if ( 'ids' == $q['fields'] ) { 3583 3574 if ( null === $this->posts ) { 3584 $this->posts = $ wpdb->get_col( $this->request );3575 $this->posts = $this->db->get_col( $this->request ); 3585 3576 } 3586 3577 3587 3578 $this->posts = array_map( 'intval', $this->posts ); … … 3593 3584 3594 3585 if ( 'id=>parent' == $q['fields'] ) { 3595 3586 if ( null === $this->posts ) { 3596 $this->posts = $ wpdb->get_results( $this->request );3587 $this->posts = $this->db->get_results( $this->request ); 3597 3588 } 3598 3589 3599 3590 $this->post_count = count( $this->posts ); … … 3611 3602 } 3612 3603 3613 3604 if ( null === $this->posts ) { 3614 $split_the_query = ( $old_request == $this->request && " $wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );3605 $split_the_query = ( $old_request == $this->request && "{$this->db->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 ); 3615 3606 3616 3607 /** 3617 3608 * Filters whether to split the query. … … 3630 3621 if ( $split_the_query ) { 3631 3622 // First get the IDs and then fill in the objects 3632 3623 3633 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts$join WHERE 1=1 $where $groupby $orderby $limits";3624 $this->request = "SELECT $found_rows $distinct {$this->db->posts}.ID FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; 3634 3625 3635 3626 /** 3636 3627 * Filters the Post IDs SQL request before sending. … … 3642 3633 */ 3643 3634 $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); 3644 3635 3645 $ids = $ wpdb->get_col( $this->request );3636 $ids = $this->db->get_col( $this->request ); 3646 3637 3647 3638 if ( $ids ) { 3648 3639 $this->posts = $ids; … … 3652 3643 $this->posts = array(); 3653 3644 } 3654 3645 } else { 3655 $this->posts = $ wpdb->get_results( $this->request );3646 $this->posts = $this->db->get_results( $this->request ); 3656 3647 $this->set_found_posts( $q, $limits ); 3657 3648 } 3658 3649 } … … 3692 3683 /** This filter is documented in wp-includes/query.php */ 3693 3684 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 3694 3685 3695 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";3696 $comments = $ wpdb->get_results($comments_request);3686 $comments_request = "SELECT {$this->db->comments}.* FROM $this->db->comments $cjoin $cwhere $cgroupby $corderby $climits"; 3687 $comments = $this->db->get_results($comments_request); 3697 3688 // Convert to WP_Comment 3698 3689 $this->comments = array_map( 'get_comment', $comments ); 3699 3690 $this->comment_count = count($this->comments); … … 3836 3827 * @since 3.5.0 3837 3828 * @access private 3838 3829 * 3839 * @global wpdb $wpdb WordPress database abstraction object.3840 *3841 3830 * @param array $q Query variables. 3842 3831 * @param string $limits LIMIT clauses of the query. 3843 3832 */ 3844 3833 private function set_found_posts( $q, $limits ) { 3845 global $wpdb;3846 3847 3834 // Bail if posts is an empty array. Continue if posts is an empty string, 3848 3835 // null, or false to accommodate caching plugins that fill posts later. 3849 3836 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) … … 3858 3845 * @param string $found_posts The query to run to find the found posts. 3859 3846 * @param WP_Query &$this The WP_Query instance (passed by reference). 3860 3847 */ 3861 $this->found_posts = $ wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );3848 $this->found_posts = $this->db->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) ); 3862 3849 } else { 3863 3850 $this->found_posts = count( $this->posts ); 3864 3851 } … … 4156 4143 * 4157 4144 * @param string|array $query URL query string or array of vars. 4158 4145 */ 4159 public function __construct($query = '') { 4160 if ( ! empty($query) ) { 4161 $this->query($query); 4146 public function __construct( $query = '' ) { 4147 $this->db = $GLOBALS['wpdb']; 4148 4149 if ( ! empty( $query ) ) { 4150 $this->query( $query ); 4162 4151 } 4163 4152 } 4164 4153