Ticket #18536: 18536.4.diff

File 18536.4.diff, 4.1 KB (added by scribu, 16 months ago)

introduce set_found_posts()

  • wp-includes/functions.php

    diff --git wp-includes/functions.php wp-includes/functions.php
    index e7c8d44..a65898b 100644
    function wp_allowed_protocols() { 
    36543654 
    36553655        return $protocols; 
    36563656} 
     3657 
     3658/** 
     3659 * Retrieve ids that are not already present in the cache 
     3660 * 
     3661 * @since 3.4.0 
     3662 * 
     3663 * @param array $object_ids ID list 
     3664 * @param string $cache_key The cache bucket to check against 
     3665 * 
     3666 * @return array 
     3667 */ 
     3668function _get_non_cached_ids( $object_ids, $cache_key ) { 
     3669        $clean = array(); 
     3670        foreach ( $object_ids as $id ) { 
     3671                $id = (int) $id; 
     3672                if ( !wp_cache_get( $id, $cache_key ) ) { 
     3673                        $clean[] = $id; 
     3674                } 
     3675        } 
     3676 
     3677        return $clean; 
     3678} 
     3679 
  • wp-includes/pluggable.php

    diff --git wp-includes/pluggable.php wp-includes/pluggable.php
    index 030e453..dd5c126 100644
    if ( !function_exists('cache_users') ) : 
    139139function cache_users( $user_ids ) { 
    140140        global $wpdb; 
    141141 
    142         $clean = array(); 
    143         foreach ( $user_ids as $id ) { 
    144                 $id = (int) $id; 
    145                 if ( !wp_cache_get( $id, 'users' ) ) { 
    146                         $clean[] = $id; 
    147                 } 
    148         } 
     142        $clean = _get_non_cached_ids( $user_ids, 'users' ); 
    149143 
    150144        if ( empty( $clean ) ) 
    151145                return; 
  • wp-includes/query.php

    diff --git wp-includes/query.php wp-includes/query.php
    index bc6edea..a6ab0bf 100644
    class WP_Query { 
    19511951                $fields = ''; 
    19521952                $post_status_join = false; 
    19531953                $page = 1; 
     1954                $expand_ids = false; 
    19541955 
    19551956                if ( isset( $q['caller_get_posts'] ) ) { 
    19561957                        _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) ); 
    class WP_Query { 
    25972598                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 
    25982599                } 
    25992600 
     2601                if ( "$wpdb->posts.*" == $fields ) { 
     2602                        $fields = "$wpdb->posts.ID"; 
     2603                        $expand_ids = true; 
     2604                } 
     2605 
    26002606                if ( ! empty($groupby) ) 
    26012607                        $groupby = 'GROUP BY ' . $groupby; 
    26022608                if ( !empty( $orderby ) ) 
    class WP_Query { 
    26262632                        return $r; 
    26272633                } 
    26282634 
    2629                 $this->posts = $wpdb->get_results($this->request); 
     2635                if ( $expand_ids ) { 
     2636                        $ids = $wpdb->get_col( $this->request ); 
     2637 
     2638                        if ( $ids ) { 
     2639                                $this->set_found_posts( $q, $limits ); 
     2640 
     2641                                $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); 
     2642 
     2643                                if ( !empty( $non_cached_ids ) ) { 
     2644                                        $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) ); 
     2645 
     2646                                        update_post_caches($fresh_posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']); 
     2647                                } 
     2648 
     2649                                $this->posts = array_map( 'get_post', $ids ); 
     2650                        } else { 
     2651                                $this->found_posts = $this->max_num_pages = 0; 
     2652                                $this->posts = array(); 
     2653                        } 
     2654                } else { 
     2655                        $this->posts = $wpdb->get_results( $this->request ); 
     2656                        $this->set_found_posts( $q, $limits ); 
     2657                } 
    26302658 
    26312659                // Raw results filter. Prior to status checks. 
    26322660                if ( !$q['suppress_filters'] ) 
    class WP_Query { 
    26452673                        $this->comment_count = count($this->comments); 
    26462674                } 
    26472675 
    2648                 if ( !$q['no_found_rows'] && !empty($limits) ) { 
    2649                         $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 
    2650                         $this->found_posts = $wpdb->get_var( $found_posts_query ); 
    2651                         $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 
    2652                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 
    2653                 } 
    2654  
    26552676                // Check post status to determine if post should be displayed. 
    26562677                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { 
    26572678                        $status = get_post_status($this->posts[0]); 
    class WP_Query { 
    27542775                return $this->posts; 
    27552776        } 
    27562777 
     2778        function set_found_posts( $q, $limits ) { 
     2779                global $wpdb; 
     2780 
     2781                if ( $q['no_found_rows'] || empty( $limits ) ) 
     2782                        return; 
     2783 
     2784                $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', $this ) ); 
     2785                $this->found_posts = $wpdb->get_var( $found_posts_query ); 
     2786                $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, $this ) ); 
     2787                $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 
     2788        } 
     2789 
    27572790        /** 
    27582791         * Set up the next post and iterate current post index. 
    27592792         *