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 */
23function 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 */
63function 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 */
78function clean_network_cache( $ids ) {
79 global $_wp_suspend_cache_invalidation;
80
81 if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
82 return;
83 }
84
85 $network_ids = (array) $ids;
86 wp_cache_delete_multiple( $network_ids, 'networks' );
87
88 foreach ( $network_ids as $id ) {
89 /**
90 * Fires immediately after a network has been removed from the object cache.
91 *
92 * @since 4.6.0
93 *
94 * @param int $id Network ID.
95 */
96 do_action( 'clean_network_cache', $id );
97 }
98
99 wp_cache_set_last_changed( 'networks' );
100}
101
102/**
103 * Updates the network cache of given networks.
104 *
105 * Will add the networks in $networks to the cache. If network ID already exists
106 * in the network cache then it will not be updated. The network is added to the
107 * cache using the network group with the key using the ID of the networks.
108 *
109 * @since 4.6.0
110 *
111 * @param array $networks Array of network row objects.
112 */
113function update_network_cache( $networks ) {
114 $data = array();
115 foreach ( (array) $networks as $network ) {
116 $data[ $network->id ] = $network;
117 }
118 wp_cache_add_multiple( $data, 'networks' );
119}
120
121/**
122 * Adds any networks from the given IDs to the cache that do not already exist in cache.
123 *
124 * @since 4.6.0
125 * @since 6.1.0 This function is no longer marked as "private".
126 *
127 * @see update_network_cache()
128 * @global wpdb $wpdb WordPress database abstraction object.
129 *
130 * @param array $network_ids Array of network IDs.
131 */
132function _prime_network_caches( $network_ids ) {
133 global $wpdb;
134
135 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
136 if ( ! empty( $non_cached_ids ) ) {
137 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
138
139 update_network_cache( $fresh_networks );
140 }
141}
142