wp/wp-includes/ms-network.php
changeset 9 177826044cd9
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     1 <?php
       
     2 /**
       
     3  * Network API
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Multisite
       
     7  * @since 5.1.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Retrieves network data given a network ID or network object.
       
    12  *
       
    13  * Network data will be cached and returned after being passed through a filter.
       
    14  * If the provided network is empty, the current network global will be used.
       
    15  *
       
    16  * @since 4.6.0
       
    17  *
       
    18  * @global WP_Network $current_site
       
    19  *
       
    20  * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
       
    21  * @return WP_Network|null The network object or null if not found.
       
    22  */
       
    23 function get_network( $network = null ) {
       
    24 	global $current_site;
       
    25 	if ( empty( $network ) && isset( $current_site ) ) {
       
    26 		$network = $current_site;
       
    27 	}
       
    28 
       
    29 	if ( $network instanceof WP_Network ) {
       
    30 		$_network = $network;
       
    31 	} elseif ( is_object( $network ) ) {
       
    32 		$_network = new WP_Network( $network );
       
    33 	} else {
       
    34 		$_network = WP_Network::get_instance( $network );
       
    35 	}
       
    36 
       
    37 	if ( ! $_network ) {
       
    38 		return null;
       
    39 	}
       
    40 
       
    41 	/**
       
    42 	 * Fires after a network is retrieved.
       
    43 	 *
       
    44 	 * @since 4.6.0
       
    45 	 *
       
    46 	 * @param WP_Network $_network Network data.
       
    47 	 */
       
    48 	$_network = apply_filters( 'get_network', $_network );
       
    49 
       
    50 	return $_network;
       
    51 }
       
    52 
       
    53 /**
       
    54  * Retrieves a list of networks.
       
    55  *
       
    56  * @since 4.6.0
       
    57  *
       
    58  * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
       
    59  *                           for information on accepted arguments. Default empty array.
       
    60  * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids',
       
    61  *                   or the number of networks when 'count' is passed as a query var.
       
    62  */
       
    63 function get_networks( $args = array() ) {
       
    64 	$query = new WP_Network_Query();
       
    65 
       
    66 	return $query->query( $args );
       
    67 }
       
    68 
       
    69 /**
       
    70  * Removes a network from the object cache.
       
    71  *
       
    72  * @since 4.6.0
       
    73  *
       
    74  * @global bool $_wp_suspend_cache_invalidation
       
    75  *
       
    76  * @param int|array $ids Network ID or an array of network IDs to remove from cache.
       
    77  */
       
    78 function clean_network_cache( $ids ) {
       
    79 	global $_wp_suspend_cache_invalidation;
       
    80 
       
    81 	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
       
    82 		return;
       
    83 	}
       
    84 
       
    85 	foreach ( (array) $ids as $id ) {
       
    86 		wp_cache_delete( $id, 'networks' );
       
    87 
       
    88 		/**
       
    89 		 * Fires immediately after a network has been removed from the object cache.
       
    90 		 *
       
    91 		 * @since 4.6.0
       
    92 		 *
       
    93 		 * @param int $id Network ID.
       
    94 		 */
       
    95 		do_action( 'clean_network_cache', $id );
       
    96 	}
       
    97 
       
    98 	wp_cache_set( 'last_changed', microtime(), 'networks' );
       
    99 }
       
   100 
       
   101 /**
       
   102  * Updates the network cache of given networks.
       
   103  *
       
   104  * Will add the networks in $networks to the cache. If network ID already exists
       
   105  * in the network cache then it will not be updated. The network is added to the
       
   106  * cache using the network group with the key using the ID of the networks.
       
   107  *
       
   108  * @since 4.6.0
       
   109  *
       
   110  * @param array $networks Array of network row objects.
       
   111  */
       
   112 function update_network_cache( $networks ) {
       
   113 	foreach ( (array) $networks as $network ) {
       
   114 		wp_cache_add( $network->id, $network, 'networks' );
       
   115 	}
       
   116 }
       
   117 
       
   118 /**
       
   119  * Adds any networks from the given IDs to the cache that do not already exist in cache.
       
   120  *
       
   121  * @since 4.6.0
       
   122  * @access private
       
   123  *
       
   124  * @see update_network_cache()
       
   125  * @global wpdb $wpdb WordPress database abstraction object.
       
   126  *
       
   127  * @param array $network_ids Array of network IDs.
       
   128  */
       
   129 function _prime_network_caches( $network_ids ) {
       
   130 	global $wpdb;
       
   131 
       
   132 	$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
       
   133 	if ( ! empty( $non_cached_ids ) ) {
       
   134 		$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
       
   135 
       
   136 		update_network_cache( $fresh_networks );
       
   137 	}
       
   138 }