WordPress.org

Make WordPress Core

Changeset 37894


Ignore:
Timestamp:
06/28/2016 09:26:48 PM (3 years ago)
Author:
jeremyfelt
Message:

Multisite: Introduce WP_Network_Query.

Provides a consistent way to query $wpdb->site for WP_Network objects based on domain, path, network ID, and (main) site ID.

Introduces and uses update_network_cache() and _prime_network_caches() to maintain a cached list of WP_Network objects for use in multiple queries.

Props flixos90.
See #32504.

Location:
trunk
Files:
2 added
2 edited

Legend:

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

    r37893 r37894  
    11161116
    11171117/**
     1118 * Removes a network from the object cache.
     1119 *
     1120 * @since 4.6.0
     1121 *
     1122 * @param int|array $ids Network ID or an array of network IDs to remove from cache.
     1123 */
     1124function clean_network_cache( $ids ) {
     1125    foreach ( (array) $ids as $id ) {
     1126        wp_cache_delete( $id, 'networks' );
     1127
     1128        /**
     1129         * Fires immediately after a network has been removed from the object cache.
     1130         *
     1131         * @since 4.6.0
     1132         *
     1133         * @param int $id Network ID.
     1134         */
     1135        do_action( 'clean_network_cache', $id );
     1136    }
     1137
     1138    wp_cache_set( 'last_changed', microtime(), 'networks' );
     1139}
     1140
     1141/**
     1142 * Updates the network cache of given networks.
     1143 *
     1144 * Will add the networks in $networks to the cache. If network ID already exists
     1145 * in the network cache then it will not be updated. The network is added to the
     1146 * cache using the network group with the key using the ID of the networks.
     1147 *
     1148 * @since 4.6.0
     1149 *
     1150 * @param array $networks Array of network row objects.
     1151 */
     1152function update_network_cache( $networks ) {
     1153    foreach ( (array) $networks as $network ) {
     1154        wp_cache_add( $network->id, $network, 'networks' );
     1155    }
     1156}
     1157
     1158/**
     1159 * Adds any networks from the given IDs to the cache that do not already exist in cache.
     1160 *
     1161 * @since 4.6.0
     1162 * @access private
     1163 *
     1164 * @see update_network_cache()
     1165 * @global wpdb $wpdb WordPress database abstraction object.
     1166 *
     1167 * @param array $network_ids Array of network IDs.
     1168 */
     1169function _prime_network_caches( $network_ids ) {
     1170    global $wpdb;
     1171
     1172    $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
     1173    if ( !empty( $non_cached_ids ) ) {
     1174        $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
     1175
     1176        update_network_cache( $fresh_networks );
     1177    }
     1178}
     1179
     1180/**
    11181181 * Handler for updating the blog date when a post is published or an already published post is changed.
    11191182 *
  • trunk/src/wp-settings.php

    r37890 r37894  
    116116if ( is_multisite() ) {
    117117    require( ABSPATH . WPINC . '/class-wp-site-query.php' );
     118    require( ABSPATH . WPINC . '/class-wp-network-query.php' );
    118119    require( ABSPATH . WPINC . '/ms-blogs.php' );
    119120    require( ABSPATH . WPINC . '/ms-settings.php' );
Note: See TracChangeset for help on using the changeset viewer.