@@ 209-291 (lines=83) @@ | ||
206 | * |
|
207 | * @return int|array The list of networks. |
|
208 | */ |
|
209 | public function get_networks() { |
|
210 | $this->parse_query(); |
|
211 | ||
212 | /** |
|
213 | * Fires before networks are retrieved. |
|
214 | * |
|
215 | * @since 4.6.0 |
|
216 | * |
|
217 | * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. |
|
218 | */ |
|
219 | do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
|
220 | ||
221 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
|
222 | $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); |
|
223 | $last_changed = wp_cache_get( 'last_changed', 'networks' ); |
|
224 | if ( ! $last_changed ) { |
|
225 | $last_changed = microtime(); |
|
226 | wp_cache_set( 'last_changed', $last_changed, 'networks' ); |
|
227 | } |
|
228 | ||
229 | $cache_key = "get_network_ids:$key:$last_changed"; |
|
230 | $cache_value = wp_cache_get( $cache_key, 'networks' ); |
|
231 | ||
232 | if ( false === $cache_value ) { |
|
233 | $network_ids = $this->get_network_ids(); |
|
234 | if ( $network_ids ) { |
|
235 | $this->set_found_networks(); |
|
236 | } |
|
237 | ||
238 | $cache_value = array( |
|
239 | 'network_ids' => $network_ids, |
|
240 | 'found_networks' => $this->found_networks, |
|
241 | ); |
|
242 | wp_cache_add( $cache_key, $cache_value, 'networks' ); |
|
243 | } else { |
|
244 | $network_ids = $cache_value['network_ids']; |
|
245 | $this->found_networks = $cache_value['found_networks']; |
|
246 | } |
|
247 | ||
248 | if ( $this->found_networks && $this->query_vars['number'] ) { |
|
249 | $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
|
250 | } |
|
251 | ||
252 | // If querying for a count only, there's nothing more to do. |
|
253 | if ( $this->query_vars['count'] ) { |
|
254 | // $network_ids is actually a count in this case. |
|
255 | return intval( $network_ids ); |
|
256 | } |
|
257 | ||
258 | $network_ids = array_map( 'intval', $network_ids ); |
|
259 | ||
260 | if ( 'ids' == $this->query_vars['fields'] ) { |
|
261 | $this->networks = $network_ids; |
|
262 | return $this->networks; |
|
263 | } |
|
264 | ||
265 | if ( $this->query_vars['update_network_cache'] ) { |
|
266 | _prime_network_caches( $network_ids ); |
|
267 | } |
|
268 | ||
269 | // Fetch full network objects from the primed cache. |
|
270 | $_networks = array(); |
|
271 | foreach ( $network_ids as $network_id ) { |
|
272 | if ( $_network = get_network( $network_id ) ) { |
|
273 | $_networks[] = $_network; |
|
274 | } |
|
275 | } |
|
276 | ||
277 | /** |
|
278 | * Filters the network query results. |
|
279 | * |
|
280 | * @since 4.6.0 |
|
281 | * |
|
282 | * @param array $results An array of networks. |
|
283 | * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. |
|
284 | */ |
|
285 | $_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) ); |
|
286 | ||
287 | // Convert to WP_Network instances |
|
288 | $this->networks = array_map( 'get_network', $_networks ); |
|
289 | ||
290 | return $this->networks; |
|
291 | } |
|
292 | ||
293 | /** |
|
294 | * Used internally to get a list of network IDs matching the query vars. |
@@ 245-329 (lines=85) @@ | ||
242 | * |
|
243 | * @return array|int List of sites, or number of sites when 'count' is passed as a query var. |
|
244 | */ |
|
245 | public function get_sites() { |
|
246 | $this->parse_query(); |
|
247 | ||
248 | /** |
|
249 | * Fires before sites are retrieved. |
|
250 | * |
|
251 | * @since 4.6.0 |
|
252 | * |
|
253 | * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. |
|
254 | */ |
|
255 | do_action_ref_array( 'pre_get_sites', array( &$this ) ); |
|
256 | ||
257 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
|
258 | $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); |
|
259 | $last_changed = wp_cache_get( 'last_changed', 'sites' ); |
|
260 | if ( ! $last_changed ) { |
|
261 | $last_changed = microtime(); |
|
262 | wp_cache_set( 'last_changed', $last_changed, 'sites' ); |
|
263 | } |
|
264 | ||
265 | $cache_key = "get_sites:$key:$last_changed"; |
|
266 | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
|
267 | ||
268 | if ( false === $cache_value ) { |
|
269 | $site_ids = $this->get_site_ids(); |
|
270 | if ( $site_ids ) { |
|
271 | $this->set_found_sites(); |
|
272 | } |
|
273 | ||
274 | $cache_value = array( |
|
275 | 'site_ids' => $site_ids, |
|
276 | 'found_sites' => $this->found_sites, |
|
277 | ); |
|
278 | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
|
279 | } else { |
|
280 | $site_ids = $cache_value['site_ids']; |
|
281 | $this->found_sites = $cache_value['found_sites']; |
|
282 | } |
|
283 | ||
284 | if ( $this->found_sites && $this->query_vars['number'] ) { |
|
285 | $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] ); |
|
286 | } |
|
287 | ||
288 | // If querying for a count only, there's nothing more to do. |
|
289 | if ( $this->query_vars['count'] ) { |
|
290 | // $site_ids is actually a count in this case. |
|
291 | return intval( $site_ids ); |
|
292 | } |
|
293 | ||
294 | $site_ids = array_map( 'intval', $site_ids ); |
|
295 | ||
296 | if ( 'ids' == $this->query_vars['fields'] ) { |
|
297 | $this->sites = $site_ids; |
|
298 | ||
299 | return $this->sites; |
|
300 | } |
|
301 | ||
302 | // Prime site network caches. |
|
303 | if ( $this->query_vars['update_site_cache'] ) { |
|
304 | _prime_site_caches( $site_ids ); |
|
305 | } |
|
306 | ||
307 | // Fetch full site objects from the primed cache. |
|
308 | $_sites = array(); |
|
309 | foreach ( $site_ids as $site_id ) { |
|
310 | if ( $_site = get_site( $site_id ) ) { |
|
311 | $_sites[] = $_site; |
|
312 | } |
|
313 | } |
|
314 | ||
315 | /** |
|
316 | * Filters the site query results. |
|
317 | * |
|
318 | * @since 4.6.0 |
|
319 | * |
|
320 | * @param array $results An array of sites. |
|
321 | * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. |
|
322 | */ |
|
323 | $_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) ); |
|
324 | ||
325 | // Convert to WP_Site instances. |
|
326 | $this->sites = array_map( 'get_site', $_sites ); |
|
327 | ||
328 | return $this->sites; |
|
329 | } |
|
330 | ||
331 | /** |
|
332 | * Used internally to get a list of site IDs matching the query vars. |