WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/20/2016 10:03:52 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.