89 * @param string|array $query { |
89 * @param string|array $query { |
90 * Optional. Array or query string of network query parameters. Default empty. |
90 * Optional. Array or query string of network query parameters. Default empty. |
91 * |
91 * |
92 * @type array $network__in Array of network IDs to include. Default empty. |
92 * @type array $network__in Array of network IDs to include. Default empty. |
93 * @type array $network__not_in Array of network IDs to exclude. Default empty. |
93 * @type array $network__not_in Array of network IDs to exclude. Default empty. |
94 * @type bool $count Whether to return a network count (true) or array of network objects. |
94 * @type bool $count Whether to return a network count (true) or array of network objects. |
95 * Default false. |
95 * Default false. |
96 * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) |
96 * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) |
97 * or empty (returns an array of complete network objects). Default empty. |
97 * or empty (returns an array of complete network objects). Default empty. |
98 * @type int $number Maximum number of networks to retrieve. Default empty (no limit). |
98 * @type int $number Maximum number of networks to retrieve. Default empty (no limit). |
99 * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. |
99 * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. |
100 * Default 0. |
100 * Default 0. |
101 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. |
101 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. |
102 * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path', |
102 * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path', |
103 * 'domain_length', 'path_length' and 'network__in'. Also accepts false, |
103 * 'domain_length', 'path_length' and 'network__in'. Also accepts false, |
104 * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. |
104 * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. |
105 * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. |
105 * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. |
106 * @type string $domain Limit results to those affiliated with a given domain. Default empty. |
106 * @type string $domain Limit results to those affiliated with a given domain. Default empty. |
107 * @type array $domain__in Array of domains to include affiliated networks for. Default empty. |
107 * @type array $domain__in Array of domains to include affiliated networks for. Default empty. |
108 * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. |
108 * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. |
109 * @type string $path Limit results to those affiliated with a given path. Default empty. |
109 * @type string $path Limit results to those affiliated with a given path. Default empty. |
110 * @type array $path__in Array of paths to include affiliated networks for. Default empty. |
110 * @type array $path__in Array of paths to include affiliated networks for. Default empty. |
111 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. |
111 * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. |
112 * @type string $search Search term(s) to retrieve matching networks for. Default empty. |
112 * @type string $search Search term(s) to retrieve matching networks for. Default empty. |
113 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. |
113 * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. |
114 * } |
114 * } |
115 */ |
115 */ |
116 public function __construct( $query = '' ) { |
116 public function __construct( $query = '' ) { |
117 $this->query_var_defaults = array( |
117 $this->query_var_defaults = array( |
196 * |
195 * |
197 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference). |
196 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference). |
198 */ |
197 */ |
199 do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
198 do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
200 |
199 |
201 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
200 $network_ids = null; |
202 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
201 |
203 |
202 /** |
204 // Ignore the $fields argument as the queried result will be the same regardless. |
203 * Filter the sites array before the query takes place. |
205 unset( $_args['fields'] ); |
204 * |
206 |
205 * Return a non-null value to bypass WordPress's default site queries. |
207 $key = md5( serialize( $_args ) ); |
206 * |
208 $last_changed = wp_cache_get_last_changed( 'networks' ); |
207 * |
209 |
208 * @since 5.2.0 |
210 $cache_key = "get_network_ids:$key:$last_changed"; |
209 * |
211 $cache_value = wp_cache_get( $cache_key, 'networks' ); |
210 * @param array|null $site_ids Return an array of site data to short-circuit WP's site query, |
212 |
211 * or null to allow WP to run its normal queries. |
213 if ( false === $cache_value ) { |
212 * @param WP_Network_Query $this The WP_Network_Query instance, passed by reference. |
214 $network_ids = $this->get_network_ids(); |
213 */ |
215 if ( $network_ids ) { |
214 $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) ); |
216 $this->set_found_networks(); |
215 |
|
216 if ( null === $network_ids ) { |
|
217 |
|
218 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
|
219 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
|
220 |
|
221 // Ignore the $fields argument as the queried result will be the same regardless. |
|
222 unset( $_args['fields'] ); |
|
223 |
|
224 $key = md5( serialize( $_args ) ); |
|
225 $last_changed = wp_cache_get_last_changed( 'networks' ); |
|
226 |
|
227 $cache_key = "get_network_ids:$key:$last_changed"; |
|
228 $cache_value = wp_cache_get( $cache_key, 'networks' ); |
|
229 |
|
230 if ( false === $cache_value ) { |
|
231 $network_ids = $this->get_network_ids(); |
|
232 if ( $network_ids ) { |
|
233 $this->set_found_networks(); |
|
234 } |
|
235 |
|
236 $cache_value = array( |
|
237 'network_ids' => $network_ids, |
|
238 'found_networks' => $this->found_networks, |
|
239 ); |
|
240 wp_cache_add( $cache_key, $cache_value, 'networks' ); |
|
241 } else { |
|
242 $network_ids = $cache_value['network_ids']; |
|
243 $this->found_networks = $cache_value['found_networks']; |
217 } |
244 } |
218 |
|
219 $cache_value = array( |
|
220 'network_ids' => $network_ids, |
|
221 'found_networks' => $this->found_networks, |
|
222 ); |
|
223 wp_cache_add( $cache_key, $cache_value, 'networks' ); |
|
224 } else { |
|
225 $network_ids = $cache_value['network_ids']; |
|
226 $this->found_networks = $cache_value['found_networks']; |
|
227 } |
245 } |
228 |
246 |
229 if ( $this->found_networks && $this->query_vars['number'] ) { |
247 if ( $this->found_networks && $this->query_vars['number'] ) { |
230 $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
248 $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
231 } |
249 } |
391 |
410 |
392 $join = ''; |
411 $join = ''; |
393 |
412 |
394 $where = implode( ' AND ', $this->sql_clauses['where'] ); |
413 $where = implode( ' AND ', $this->sql_clauses['where'] ); |
395 |
414 |
|
415 $groupby = ''; |
|
416 |
396 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); |
417 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); |
397 |
418 |
398 /** |
419 /** |
399 * Filters the network query clauses. |
420 * Filters the network query clauses. |
400 * |
421 * |
401 * @since 4.6.0 |
422 * @since 4.6.0 |
402 * |
423 * |
403 * @param array $pieces A compacted array of network query clauses. |
424 * @param string[] $pieces An associative array of network query clauses. |
404 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference). |
425 * @param WP_Network_Query $this Current instance of WP_Network_Query (passed by reference). |
405 */ |
426 */ |
406 $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) ); |
427 $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) ); |
407 |
428 |
408 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; |
429 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; |
409 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; |
430 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; |
410 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; |
431 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; |
411 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; |
432 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; |
412 $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; |
433 $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; |
413 $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; |
434 $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; |
414 |
435 |
415 if ( $where ) { |
436 if ( $where ) { |
416 $where = 'WHERE ' . $where; |
437 $where = 'WHERE ' . $where; |
417 } |
438 } |
517 ); |
538 ); |
518 |
539 |
519 $parsed = false; |
540 $parsed = false; |
520 if ( $orderby == 'network__in' ) { |
541 if ( $orderby == 'network__in' ) { |
521 $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) ); |
542 $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) ); |
522 $parsed = "FIELD( {$wpdb->site}.id, $network__in )"; |
543 $parsed = "FIELD( {$wpdb->site}.id, $network__in )"; |
523 } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) { |
544 } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) { |
524 $field = substr( $orderby, 0, -7 ); |
545 $field = substr( $orderby, 0, -7 ); |
525 $parsed = "CHAR_LENGTH($wpdb->site.$field)"; |
546 $parsed = "CHAR_LENGTH($wpdb->site.$field)"; |
526 } elseif ( in_array( $orderby, $allowed_keys ) ) { |
547 } elseif ( in_array( $orderby, $allowed_keys ) ) { |
527 $parsed = "$wpdb->site.$orderby"; |
548 $parsed = "$wpdb->site.$orderby"; |
528 } |
549 } |
529 |
550 |