Make WordPress Core

Changeset 37477


Ignore:
Timestamp:
05/20/2016 10:03:52 PM (9 years ago)
Author:
jeremyfelt
Message:

Multisite: Introduce WP_Site_Query

Provides a consistent way to query $wpdb->blogs for WP_Site objects based on domain, path, site ID, network ID, and more.

Introduces and uses update_site_cache() and _prime_site_caches() to maintain a cached list of WP_Site objects for use in multiple queries.

Props spacedmonkey, flixos90, DrewAPicture, jeremyfelt, ocean90.
See #35791.

Location:
trunk
Files:
2 added
3 edited

Legend:

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

    r37342 r37477  
    492492            'post_date', 'post_date_gmt', 'post_modified',
    493493            'post_modified_gmt', 'comment_date', 'comment_date_gmt',
    494             'user_registered',
     494            'user_registered', 'registered', 'last_updated',
    495495        );
    496496
     
    526526                    'user_registered',
    527527                ),
     528                $wpdb->blogs => array(
     529                    'registered',
     530                    'last_updated',
     531                ),
    528532            );
    529533
  • trunk/src/wp-includes/ms-blogs.php

    r37468 r37477  
    521521
    522522/**
     523 * Adds any sites from the given ids to the cache that do not already exist in cache.
     524 *
     525 * @since 4.6.0
     526 * @access private
     527 *
     528 * @see update_site_cache()
     529 *
     530 * @global wpdb $wpdb WordPress database abstraction object.
     531 *
     532 * @param array $ids ID list.
     533 */
     534function _prime_site_caches( $ids ) {
     535    global $wpdb;
     536
     537    $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
     538    if ( ! empty( $non_cached_ids ) ) {
     539        $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
     540
     541        update_site_cache( $fresh_sites );
     542    }
     543}
     544
     545/**
     546 * Updates sites in cache.
     547 *
     548 * @since 4.6.0
     549 *
     550 * @param array $sites Array of site objects, passed by reference.
     551 */
     552function update_site_cache( &$sites ) {
     553    if ( ! $sites ) {
     554        return;
     555    }
     556
     557    foreach ( $sites as $site ) {
     558        wp_cache_add( $site->blog_id, $site, 'sites' );
     559        wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
     560    }
     561}
     562
     563/**
    523564 * Retrieve option value for a given blog id based on name of option.
    524565 *
  • trunk/src/wp-settings.php

    r37428 r37477  
    100100// Initialize multisite if enabled.
    101101if ( is_multisite() ) {
     102    require( ABSPATH . WPINC . '/class-wp-site-query.php' );
    102103    require( ABSPATH . WPINC . '/ms-blogs.php' );
    103104    require( ABSPATH . WPINC . '/ms-settings.php' );
Note: See TracChangeset for help on using the changeset viewer.