Make WordPress Core

Ticket #42252: 42252.2.diff

File 42252.2.diff, 3.6 KB (added by spacedmonkey, 6 years ago)
  • src/wp-includes/class-wp-site-query.php

     
    229229         *                   or the number of sites when 'count' is passed as a query var.
    230230         */
    231231        public function get_sites() {
     232                $query_vars = $this->query_vars;
    232233                $this->parse_query();
    233234
    234235                /**
     
    240241                 */
    241242                do_action_ref_array( 'pre_get_sites', array( &$this ) );
    242243
    243                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    244                 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
     244                $cache_key = $this->get_cache_key( $query_vars );
    245245
    246                 // Ignore the $fields argument as the queried result will be the same regardless.
    247                 unset( $_args['fields'] );
    248 
    249                 $key = md5( serialize( $_args ) );
    250                 $last_changed = wp_cache_get_last_changed( 'sites' );
    251 
    252                 $cache_key = "get_sites:$key:$last_changed";
    253246                $cache_value = wp_cache_get( $cache_key, 'sites' );
    254247
    255248                if ( false === $cache_value ) {
     
    315308                return $this->sites;
    316309        }
    317310
     311
     312        protected function get_cache_key( $query_vars = '' ) {
     313                if ( empty( $query_vars ) ) {
     314                        $query_vars = $this->query_vars;
     315                }
     316
     317                $query_vars_key = array_keys( $query_vars );
     318                $search         = array( 'domain', 'path', 'network_id', 'number' );
     319                $diff           = array_diff( $query_vars_key, $search );
     320
     321                if ( $query_vars['number'] == 1 && count( $diff ) == 0 ) {
     322                        $_args = array();
     323                        array_pop( $search );
     324                        foreach ( $search as $element ) {
     325                                if ( empty( $query_vars[ $element ] ) ) {
     326                                        break;
     327                                }
     328                                $_args[] = $query_vars[ $element ];
     329                        }
     330                        $key       = md5( serialize( $_args ) );
     331                        $cache_key = "get_sites:$key";
     332                } else {
     333                        // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     334                        $_args = wp_array_slice_assoc( $query_vars, array_keys( $this->query_var_defaults ) );
     335
     336                        // Ignore the $fields argument as the queried result will be the same regardless.
     337                        unset( $_args['fields'] );
     338
     339                        // Ignore the $update_site_cache as it does not affect the query.
     340                        unset( $_args['update_site_cache'] );
     341                        $key          = md5( serialize( ksort( $_args ) ) );
     342                        $last_changed = wp_cache_get_last_changed( 'sites' );
     343                        $cache_key    = "get_sites:$key:$last_changed";
     344                }
     345
     346                return $cache_key;
     347        }
     348
    318349        /**
    319350         * Used internally to get a list of site IDs matching the query vars.
    320351         *
  • src/wp-includes/ms-blogs.php

     
    444444
    445445                // Make sure a WP_Site object exists even when the site has been deleted.
    446446                $blog = new WP_Site( (object) array(
    447                         'blog_id' => $blog_id,
    448                         'domain'  => null,
    449                         'path'    => null,
     447                        'blog_id'    => $blog_id,
     448                        'domain'     => null,
     449                        'path'       => null,
     450                        'network_id' => get_current_network_id()
    450451                ) );
    451452        }
    452453
     
    462463        wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
    463464        wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
    464465
     466
     467        $list  = array( 'domain', 'path', 'network_id' );
     468        $_args = array();
     469        foreach ( $list as $element ) {
     470                $_args[]   = $blog->$element;
     471                $key       = md5( serialize( $_args ) );
     472                $cache_key = "get_sites:$key";
     473                wp_cache_delete( $cache_key, 'sites' );
     474        }
     475
    465476        /**
    466477         * Fires immediately after a site has been removed from the object cache.
    467478         *