Make WordPress Core

Ticket #35791: 35791-cachefound-1.diff

File 35791-cachefound-1.diff, 3.0 KB (added by spacedmonkey, 9 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    256256                        $last_changed = microtime();
    257257                        wp_cache_set( 'last_changed', $last_changed, 'sites' );
    258258                }
    259                 $cache_key = "get_site_ids:$key:$last_changed";
    260 
    261                 $site_ids = wp_cache_get( $cache_key, 'sites' );
    262                 if ( false === $site_ids ) {
     259                $cache_key   = "get_sites:$key:$last_changed";
     260                $cache_value = wp_cache_get( $cache_key, 'sites' );
     261                if ( false === $cache_value ) {
    263262                        $site_ids = $this->get_site_ids();
    264                         wp_cache_add( $cache_key, $site_ids, 'sites' );
     263                        $this->set_found_sites();
     264                        $cache_value = array(
     265                                'site_ids'      => $site_ids,
     266                                'found_sites'   => $this->found_sites,
     267                                'max_num_pages' => $this->max_num_pages
     268                        );
     269                        wp_cache_add( $cache_key, $cache_value, 'sites' );
     270                } else {
     271                        $site_ids            = $cache_value['site_ids'];
     272                        $this->found_sites   = $cache_value['found_sites'];
     273                        $this->max_num_pages = $cache_value['max_num_pages'];
    265274                }
    266275
    267276                // If querying for a count only, there's nothing more to do.
     
    274283
    275284                $this->site_count = count( $this->sites );
    276285
    277                 if ( $site_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    278                         /**
    279                          * Filters the query used to retrieve found site count.
    280                          *
    281                          * @since 4.6.0
    282                          *
    283                          * @param string        $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
    284                          * @param WP_Site_Query $site_query        The `WP_Site_Query` instance.
    285                          */
    286                         $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
    287 
    288                         $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
    289                         $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
    290                 }
    291 
    292286                if ( 'ids' == $this->query_vars['fields'] ) {
    293287                        $this->sites = $site_ids;
    294288
     
    572566        }
    573567
    574568        /**
     569         * Set up the amount of found sites and the number of pages (if limit clause was used)
     570         * for the current query.
     571         *
     572         * @since 4.6.0
     573         * @access private
     574         *
     575         * @global wpdb $wpdb WordPress database abstraction object.
     576         *
     577         */
     578        private function set_found_sites() {
     579                global $wpdb;
     580
     581                if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     582                        /**
     583                         * Filters the query used to retrieve found site count.
     584                         *
     585                         * @since 4.6.0
     586                         *
     587                         * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
     588                         * @param WP_Site_Query $site_query The `WP_Site_Query` instance (passed by reference).
     589                         */
     590                        $found_sites_query = apply_filters_ref_array( 'found_sites_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     591
     592                        $this->found_sites   = (int) $wpdb->get_var( $found_sites_query );
     593                        $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
     594                }
     595        }
     596
     597        /**
    575598         * Used internally to generate an SQL string for searching across multiple columns.
    576599         *
    577600         * @since 4.6.0