Make WordPress Core

Ticket #10964: query.php_2012-01-05.patch

File query.php_2012-01-05.patch, 11.6 KB (added by asannad, 11 years ago)
  • query.php

     
    19491949                $join = '';
    19501950                $search = '';
    19511951                $groupby = '';
    1952                 $fields = '';
     1952                $fields = "$wpdb->posts.ID";
    19531953                $post_status_join = false;
    19541954                $page = 1;
     1955                $orderby = '';
     1956                $quick_fields = "$wpdb->posts.*";
    19551957
    19561958                if ( isset( $q['caller_get_posts'] ) ) {
    19571959                        _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
     
    20332035                switch ( $q['fields'] ) {
    20342036                        case 'ids':
    20352037                                $fields = "$wpdb->posts.ID";
     2038                                $quick_fields = "$wpdb->posts.ID";
    20362039                                break;
    20372040                        case 'id=>parent':
    20382041                                $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
     2042                                $quick_fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
    20392043                                break;
    20402044                        default:
    2041                                 $fields = "$wpdb->posts.*";
     2045                                $fields = "$wpdb->posts.ID";
     2046                                $quick_fields = "$wpdb->posts.*";
    20422047                }
    20432048
    20442049                // If a month is specified in the querystring, load that month
     
    25642569
    25652570                $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
    25662571
     2572                // Set up quick_* defaults from the standard placeholders, before filtering
     2573                $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' );
     2574                $quick_distinct = $distinct;
     2575                $quick_join = $join;
     2576                $quick_where = $where;
     2577                $quick_groupby = $groupby;
     2578               
    25672579                // Apply post-paging filters on where and join.  Only plugins that
    25682580                // manipulate paging queries should use these hooks.
    25692581                if ( !$q['suppress_filters'] ) {
    2570                         $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
    2571                         $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ) );
    2572                         $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
     2582                        $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this, false ) );
     2583                        $quick_distinct = apply_filters_ref_array( 'posts_distinct', array( $quick_distinct, &$this, true ) );
     2584
     2585                        $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this, false ) );
     2586                        $quick_fields = apply_filters_ref_array( 'posts_fields', array( $quick_fields, &$this, true ) );
     2587
     2588                        $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this, false ) );
     2589                        $quick_join = apply_filters_ref_array( 'posts_join_paged', array( $quick_join, &$this, true ) );
     2590
     2591                        $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this, false ) );
     2592                        $quick_where = apply_filters_ref_array( 'posts_where_paged', array( $quick_where, &$this, true ) );
     2593
     2594                        $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this, false ) );
     2595                        $quick_groupby = apply_filters_ref_array( 'posts_groupby', array( $quick_groupby, &$this, true ) );
     2596
    25732597                        $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ) );
    2574                         $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
     2598
    25752599                        $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) );
    2576                         $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) );
    2577 
     2600                       
    25782601                        // Filter all clauses at once, for convenience
    25792602                        $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
    25802603                        foreach ( $pieces as $piece )
    25812604                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
     2605
     2606                        // And again for the quick clauses
     2607                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) );
     2608                        foreach ( $quick_pieces as $piece )
     2609                            $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    25822610                }
    25832611
    25842612                // Announce current selection parameters.  For use by caching plugins.
     
    25862614
    25872615                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
    25882616                if ( !$q['suppress_filters'] ) {
    2589                         $where          = apply_filters_ref_array( 'posts_where_request',               array( $where, &$this ) );
    2590                         $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ) );
    2591                         $join           = apply_filters_ref_array( 'posts_join_request',                array( $join, &$this ) );
     2617                       
     2618                        $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this, false ) );
     2619                        $quick_distinct = apply_filters_ref_array( 'posts_distinct_request', array( $quick_distinct, &$this, true ) );
     2620
     2621                        $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this, false ) );
     2622                        $quick_fields = apply_filters_ref_array( 'posts_fields_request', array( $quick_fields, &$this, true ) );
     2623
     2624                        $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this, false ) );
     2625                        $quick_join = apply_filters_ref_array( 'posts_join_request', array( $quick_join, &$this, true ) );
     2626
     2627                        $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this, false ) );
     2628                        $quick_where = apply_filters_ref_array( 'posts_where_request', array( $quick_where, &$this, true ) );
     2629
     2630                        $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this, false ) );
     2631                        $quick_groupby = apply_filters_ref_array( 'posts_groupby_request', array( $quick_groupby, &$this, true ) );
     2632
    25922633                        $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ) );
    2593                         $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
    2594                         $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) );
    25952634                        $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) );
    25962635
    25972636                        // Filter all clauses at once, for convenience
    25982637                        $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
    25992638                        foreach ( $pieces as $piece )
    26002639                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
     2640
     2641                        // And again for the quick clauses
     2642                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) );
     2643                        foreach ( $quick_pieces as $piece )
     2644                            $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    26012645                }
    26022646
    26032647                if ( ! empty($groupby) )
    26042648                        $groupby = 'GROUP BY ' . $groupby;
     2649                if ( ! empty($quick_groupby) )
     2650                        $quick_groupby = 'GROUP BY ' . $quick_groupby;
    26052651                if ( !empty( $orderby ) )
    26062652                        $orderby = 'ORDER BY ' . $orderby;
    26072653
     
    26092655                if ( !$q['no_found_rows'] && !empty($limits) )
    26102656                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    26112657
    2612                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2613                 if ( !$q['suppress_filters'] )
    2614                         $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
     2658                if ( empty($limits) ) {
     2659                        // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2660                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2661                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
    26152662
    2616                 if ( 'ids' == $q['fields'] ) {
    2617                         $this->posts = $wpdb->get_col($this->request);
     2663                        if ( 'ids' == $q['fields'] ) {
     2664                            $this->posts = $wpdb->get_col($this->request);
     2665                            return $this->posts;
     2666                        }
    26182667
    2619                         return $this->posts;
    2620                 }
     2668                        if ( 'id=>parent' == $q['fields'] ) {
     2669                            $this->posts = $wpdb->get_results($this->request);
    26212670
    2622                 if ( 'id=>parent' == $q['fields'] ) {
    2623                         $this->posts = $wpdb->get_results($this->request);
    2624 
    2625                         $r = array();
    2626                         foreach ( $this->posts as $post )
     2671                            $r = array();
     2672                            foreach ( $this->posts as $post )
    26272673                                $r[ $post->ID ] = $post->post_parent;
    26282674
    2629                         return $r;
     2675                            return $r;
     2676                        }
     2677
     2678                        if ( !$q['suppress_filters'] ) {
     2679                                $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this, false ) );
     2680                                $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
     2681                        }
     2682
     2683                        $this->posts = $wpdb->get_results($this->quick_request);
     2684
     2685                        $this->found_posts = count($this->posts);
     2686                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2687                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2688                } else {
     2689
     2690                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2691                        if ( !$q['suppress_filters'] )
     2692                                $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this, false ) );
     2693
     2694                        $post_ids = $wpdb->get_col($this->request);
     2695
     2696                        if ( !$post_ids ) {
     2697                                $this->found_posts = 0;
     2698                                $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2699                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2700
     2701                                $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2702                                if ( !$q['suppress_filters'] )
     2703                                        $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
     2704
     2705                                $this->posts = array(); // no point in querying, since there are no posts
     2706                        } else {
     2707                                $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     2708                                $this->found_posts = $wpdb->get_var( $found_posts_query );
     2709                                $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2710                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2711
     2712                                $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1 = 1 $quick_where AND $wpdb->posts.ID IN ( " . implode(',', $post_ids) . " ) $quick_groupby ORDER BY FIELD( $wpdb->posts.ID, " . implode(',', $post_ids) . ") ";
     2713
     2714                                if ( !$q['suppress_filters'] )
     2715                                        $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
     2716
     2717                                $this->posts = $wpdb->get_results($this->quick_request);
     2718                        }
    26302719                }
    26312720
    2632                 $this->posts = $wpdb->get_results($this->request);
    2633 
    26342721                // Raw results filter.  Prior to status checks.
    26352722                if ( !$q['suppress_filters'] )
    26362723                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
     
    26472734                        $this->comments = $wpdb->get_results($comments_request);
    26482735                        $this->comment_count = count($this->comments);
    26492736                }
    2650 
    2651                 if ( !$q['no_found_rows'] && !empty($limits) ) {
    2652                         $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
    2653                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2654                         $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    2655                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2656                 }
    2657 
     2737               
    26582738                // Check post status to determine if post should be displayed.
    26592739                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    26602740                        $status = get_post_status($this->posts[0]);