Make WordPress Core


Ignore:
Timestamp:
06/29/2016 07:31:49 PM (10 years ago)
Author:
jeremyfelt
Message:

Multisite: Lazy load extended WP_Site properties when requested.

In the past, get_blog_details() has been used to retrieve the home, siteurl, blogname, and post_count options for a site. By lazy loading properties in a WP_Site object, we can avoid having to use get_blog_details() and instead provide the properties as needed.

This introduces the global site-details cache group in which standard objects representing the site are stored. This will one day be a replacement for the blog-details cache group that is currently used in get_blog_details().

This relies on the ms_loaded action introduced in [37916] as properties are not available via get_option() until multisite has been fully loaded.

Props flixos90.
Fixes #36935.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-site.php

    r37917 r37918  
    223223         *
    224224         * Allows current multisite naming conventions when getting properties.
     225         * Allows access to extended site properties.
    225226         *
    226227         * @since 4.6.0
     
    240241                        case 'network_id':
    241242                                return (int) $this->site_id;
     243                        case 'blogname':
     244                        case 'siteurl':
     245                        case 'post_count':
     246                        case 'home':
     247                                if ( ! did_action( 'ms_loaded' ) ) {
     248                                        return null;
     249                                }
     250                                $details = $this->get_details();
     251                                return $details->$key;
    242252                }
    243253
     
    249259         *
    250260         * Allows current multisite naming conventions when checking for properties.
     261         * Checks for extended site properties.
    251262         *
    252263         * @since 4.6.0
     
    262273                        case 'site_id':
    263274                        case 'network_id':
     275                                return true;
     276                        case 'blogname':
     277                        case 'siteurl':
     278                        case 'post_count':
     279                        case 'home':
     280                                if ( ! did_action( 'ms_loaded' ) ) {
     281                                        return false;
     282                                }
    264283                                return true;
    265284                }
     
    293312                }
    294313        }
     314
     315        /**
     316         * Retrieve the details for this site.
     317         *
     318         * This method is used internally to lazy-load the extended properties of a site.
     319         *
     320         * @since 4.6.0
     321         * @access private
     322         *
     323         * @see WP_Site::__get()
     324         *
     325         * @return object A raw site object with all details included.
     326         */
     327        private function get_details() {
     328                $details = wp_cache_get( $this->blog_id, 'site-details' );
     329
     330                if ( false === $details ) {
     331
     332                        switch_to_blog( $this->blog_id );
     333                        // Create a raw copy of the object for backwards compatibility with the filter below.
     334                        $details = new stdClass();
     335                        foreach ( get_object_vars( $this ) as $key => $value ) {
     336                                $details->$key = $value;
     337                        }
     338                        $details->blogname   = get_option( 'blogname' );
     339                        $details->siteurl    = get_option( 'siteurl' );
     340                        $details->post_count = get_option( 'post_count' );
     341                        $details->home       = get_option( 'home' );
     342                        restore_current_blog();
     343
     344                        $cache_details = true;
     345                        foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) {
     346                                if ( false === $details->$field ) {
     347                                        $cache_details = false;
     348                                        break;
     349                                }
     350                        }
     351
     352                        if ( $cache_details ) {
     353                                wp_cache_set( $this->blog_id, $details, 'site-details' );
     354                        }
     355                }
     356
     357                /**
     358                 * Filters a site's extended properties.
     359                 *
     360                 * @since 4.6.0
     361                 *
     362                 * @param object $details The site details.
     363                 */
     364                $details = apply_filters( 'site_details', $details );
     365
     366                return $details;
     367        }
    295368}
Note: See TracChangeset for help on using the changeset viewer.