Make WordPress Core

Ticket #21434: 21434.diff

File 21434.diff, 6.2 KB (added by ryan, 13 years ago)

First draft, untested

  • wp-includes/ms-blogs.php

     
    487487                        $current_user->for_blog( $blog_id );
    488488        }
    489489
    490         if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    491                 $global_groups = $wp_object_cache->global_groups;
    492         else
    493                 $global_groups = false;
    494 
    495         wp_cache_init();
    496         if ( function_exists('wp_cache_add_global_groups') ) {
    497                 if ( is_array( $global_groups ) )
    498                         wp_cache_add_global_groups( $global_groups );
     490        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
     491                wp_cache_switch_to_blog( $blog_id );
     492        } else {
     493                if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
     494                        $global_groups = $wp_object_cache->global_groups;
    499495                else
    500                         wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
    501                 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     496                        $global_groups = false;
     497       
     498                wp_cache_init();
     499                if ( function_exists('wp_cache_add_global_groups') ) {
     500                        if ( is_array( $global_groups ) )
     501                                wp_cache_add_global_groups( $global_groups );
     502                        else
     503                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
     504                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     505                }
    502506        }
    503507
    504508        do_action('switch_blog', $blog_id, $prev_blog_id);
     
    551555                        $current_user->for_blog( $blog_id );
    552556        }
    553557
    554         if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    555                 $global_groups = $wp_object_cache->global_groups;
    556         else
    557                 $global_groups = false;
    558 
    559         wp_cache_init();
    560         if ( function_exists('wp_cache_add_global_groups') ) {
    561                 if ( is_array( $global_groups ) )
    562                         wp_cache_add_global_groups( $global_groups );
     558        if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
     559                wp_cache_switch_to_blog( $blog_id );
     560        } else {
     561                if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
     562                        $global_groups = $wp_object_cache->global_groups;
    563563                else
    564                         wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
    565                 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     564                        $global_groups = false;
     565       
     566                wp_cache_init();
     567                if ( function_exists('wp_cache_add_global_groups') ) {
     568                        if ( is_array( $global_groups ) )
     569                                wp_cache_add_global_groups( $global_groups );
     570                        else
     571                                wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
     572                        wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
     573                }
    566574        }
    567575
    568576        do_action('switch_blog', $blog_id, $prev_blog_id);
  • wp-includes/cache.php

     
    180180}
    181181
    182182/**
     183 * Switch the interal blog id.
     184 *
     185 * This changes the blog id used to create keys in blog specific groups.
     186 *
     187 * @param int $blog_id Blog ID
     188 */
     189function wp_cache_switch_to_blog( $blog_id ) {
     190        global $wp_object_cache;
     191
     192        return $wp_object_cache->switch_to_blog( $blog_id );
     193}
     194
     195/**
    183196 * Adds a group or set of groups to the list of global groups.
    184197 *
    185198 * @since 2.6.0
     
    292305                if ( empty( $group ) )
    293306                        $group = 'default';
    294307
     308                $key = $this->key( $id, $group );
     309
    295310                if ( $this->_exists($key, $group) )
    296311                        return false;
    297312
     
    325340        function decr( $key, $offset = 1, $group = 'default' ) {
    326341                if ( empty( $group ) )
    327342                        $group = 'default';
    328                
     343
     344                $key = $this->key( $id, $group );
     345
    329346                if ( ! $this->_exists( $key, $group ) )
    330347                        return false;
    331348
     
    361378                if ( empty( $group ) )
    362379                        $group = 'default';
    363380
     381                $key = $this->key( $id, $group );
     382
    364383                if ( ! $force && ! $this->_exists( $key, $group ) )
    365384                        return false;
    366385
     
    402421                if ( empty( $group ) )
    403422                        $group = 'default';
    404423
     424                $key = $this->key( $id, $group );
     425
    405426                if ( $this->_exists( $key, $group ) ) {
    406427                        $found = true;
    407428                        $this->cache_hits += 1;
     
    430451                if ( empty( $group ) )
    431452                        $group = 'default';
    432453
     454                $key = $this->key( $id, $group );
     455
    433456                if ( ! $this->_exists( $key, $group ) )
    434457                        return false;
    435458
     
    447470        }
    448471
    449472        /**
     473         * Create a key from a key and group
     474         *
     475         * @since 3.5.0
     476         *
     477         * @param string $key A cache key
     478         * @param string $group A group
     479         * @return string A key for an individual cache bucket
     480         */
     481        function key( $key, $group ) { 
     482                if ( empty( $group ) )
     483                        $group = 'default';
     484
     485                if ( false !== array_search( $group, $this->global_groups ) )
     486                        $prefix = '';
     487                else
     488                        $prefix = $this->blog_prefix;
     489
     490                return preg_replace('/\s+/', '', "$prefix$group:$key");
     491        }
     492
     493        /**
    450494         * Replace the contents in the cache, if contents already exist
    451495         *
    452496         * @since 2.0.0
     
    462506                if ( empty( $group ) )
    463507                        $group = 'default';
    464508
     509                $key = $this->key( $id, $group );
     510
    465511                if ( ! $this->_exists( $key, $group ) )
    466512                        return false;
    467513
     
    505551                if ( empty( $group ) )
    506552                        $group = 'default';
    507553
     554                $key = $this->key( $id, $group );
     555
    508556                if ( is_object($data) )
    509557                        $data = clone $data;
    510558
     
    533581        }
    534582
    535583        /**
     584         * Switch the interal blog id.
     585         *
     586         * This changes the blog id used to create keys in blog specific groups.
     587         *
     588         * @param int $blog_id Blog ID
     589         */
     590        function switch_to_blog( $blog_id ) {
     591                $this->blog_prefix = is_multisite() ? get_current_blog_id() . ':' : '';
     592        }
     593
     594        /**
    536595         * Utility function to determine whether a key exists in the cache.
    537596         *
    538597         * @since 3.4.0
     
    550609         * @return null|WP_Object_Cache If cache is disabled, returns null.
    551610         */
    552611        function __construct() {
     612                $this->blog_prefix = is_multisite() ? get_current_blog_id() . ':' : '';
     613
    553614                /**
    554615                 * @todo This should be moved to the PHP4 style constructor, PHP5
    555616                 * already calls __destruct()