Make WordPress Core

Ticket #35791: 35791-cachefound-2.diff

File 35791-cachefound-2.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                        if ( $site_ids ) {
     264                                $this->set_found_sites();
     265                        }
     266                        $cache_value = array(
     267                                'site_ids'      => $site_ids,
     268                                'found_sites'   => $this->found_sites,
     269                                'max_num_pages' => $this->max_num_pages
     270                        );
     271                        wp_cache_add( $cache_key, $cache_value, 'sites' );
     272                } else {
     273                        $site_ids            = $cache_value['site_ids'];
     274                        $this->found_sites   = $cache_value['found_sites'];
     275                        $this->max_num_pages = $cache_value['max_num_pages'];
    265276                }
    266277
    267278                // If querying for a count only, there's nothing more to do.
     
    274285
    275286                $this->site_count = count( $this->sites );
    276287
    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 
    292288                if ( 'ids' == $this->query_vars['fields'] ) {
    293289                        $this->sites = $site_ids;
    294290
     
    572568        }
    573569
    574570        /**
     571         * Set up the amount of found sites and the number of pages (if limit clause was used)
     572         * for the current query.
     573         *
     574         * @since 4.6.0
     575         * @access private
     576         *
     577         * @global wpdb $wpdb WordPress database abstraction object.
     578         *
     579         */
     580        private function set_found_sites() {
     581                global $wpdb;
     582
     583                if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     584                        /**
     585                         * Filters the query used to retrieve found site count.
     586                         *
     587                         * @since 4.6.0
     588                         *
     589                         * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
     590                         * @param WP_Site_Query $site_query The `WP_Site_Query` instance (passed by reference).
     591                         */
     592                        $found_sites_query = apply_filters_ref_array( 'found_sites_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     593
     594                        $this->found_sites   = (int) $wpdb->get_var( $found_sites_query );
     595                        $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
     596                }
     597        }
     598
     599        /**
    575600         * Used internally to generate an SQL string for searching across multiple columns.
    576601         *
    577602         * @since 4.6.0