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.


git-svn-id: https://develop.svn.wordpress.org/trunk@37477 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt
2016-05-20 22:03:52 +00:00
parent c7ff79df64
commit 381930974c
5 changed files with 1166 additions and 1 deletions
+41
View File
@@ -519,6 +519,47 @@ function get_site( &$site = null, $output = OBJECT ) {
return $_site;
}
/**
* Adds any sites from the given ids to the cache that do not already exist in cache.
*
* @since 4.6.0
* @access private
*
* @see update_site_cache()
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $ids ID list.
*/
function _prime_site_caches( $ids ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
update_site_cache( $fresh_sites );
}
}
/**
* Updates sites in cache.
*
* @since 4.6.0
*
* @param array $sites Array of site objects, passed by reference.
*/
function update_site_cache( &$sites ) {
if ( ! $sites ) {
return;
}
foreach ( $sites as $site ) {
wp_cache_add( $site->blog_id, $site, 'sites' );
wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
}
}
/**
* Retrieve option value for a given blog id based on name of option.
*