142 /** |
143 /** |
143 * Parses arguments passed to the network query with default query parameters. |
144 * Parses arguments passed to the network query with default query parameters. |
144 * |
145 * |
145 * @since 4.6.0 |
146 * @since 4.6.0 |
146 * |
147 * |
147 * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct() |
148 * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct() for accepted arguments. |
148 */ |
149 */ |
149 public function parse_query( $query = '' ) { |
150 public function parse_query( $query = '' ) { |
150 if ( empty( $query ) ) { |
151 if ( empty( $query ) ) { |
151 $query = $this->query_vars; |
152 $query = $this->query_vars; |
152 } |
153 } |
247 |
248 |
248 $key = md5( serialize( $_args ) ); |
249 $key = md5( serialize( $_args ) ); |
249 $last_changed = wp_cache_get_last_changed( 'networks' ); |
250 $last_changed = wp_cache_get_last_changed( 'networks' ); |
250 |
251 |
251 $cache_key = "get_network_ids:$key:$last_changed"; |
252 $cache_key = "get_network_ids:$key:$last_changed"; |
252 $cache_value = wp_cache_get( $cache_key, 'networks' ); |
253 $cache_value = wp_cache_get( $cache_key, 'network-queries' ); |
253 |
254 |
254 if ( false === $cache_value ) { |
255 if ( false === $cache_value ) { |
255 $network_ids = $this->get_network_ids(); |
256 $network_ids = $this->get_network_ids(); |
256 if ( $network_ids ) { |
257 if ( $network_ids ) { |
257 $this->set_found_networks(); |
258 $this->set_found_networks(); |
259 |
260 |
260 $cache_value = array( |
261 $cache_value = array( |
261 'network_ids' => $network_ids, |
262 'network_ids' => $network_ids, |
262 'found_networks' => $this->found_networks, |
263 'found_networks' => $this->found_networks, |
263 ); |
264 ); |
264 wp_cache_add( $cache_key, $cache_value, 'networks' ); |
265 wp_cache_add( $cache_key, $cache_value, 'network-queries' ); |
265 } else { |
266 } else { |
266 $network_ids = $cache_value['network_ids']; |
267 $network_ids = $cache_value['network_ids']; |
267 $this->found_networks = $cache_value['found_networks']; |
268 $this->found_networks = $cache_value['found_networks']; |
268 } |
269 } |
269 |
270 |
270 if ( $this->found_networks && $this->query_vars['number'] ) { |
271 if ( $this->found_networks && $this->query_vars['number'] ) { |
271 $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
272 $this->max_num_pages = (int) ceil( $this->found_networks / $this->query_vars['number'] ); |
272 } |
273 } |
273 |
274 |
274 // If querying for a count only, there's nothing more to do. |
275 // If querying for a count only, there's nothing more to do. |
275 if ( $this->query_vars['count'] ) { |
276 if ( $this->query_vars['count'] ) { |
276 // $network_ids is actually a count in this case. |
277 // $network_ids is actually a count in this case. |
436 |
437 |
437 $where = implode( ' AND ', $this->sql_clauses['where'] ); |
438 $where = implode( ' AND ', $this->sql_clauses['where'] ); |
438 |
439 |
439 $groupby = ''; |
440 $groupby = ''; |
440 |
441 |
441 $clauses = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); |
442 $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); |
442 |
443 |
443 /** |
444 /** |
444 * Filters the network query clauses. |
445 * Filters the network query clauses. |
445 * |
446 * |
446 * @since 4.6.0 |
447 * @since 4.6.0 |
447 * |
448 * |
448 * @param string[] $clauses An associative array of network query clauses. |
449 * @param string[] $clauses { |
|
450 * Associative array of the clauses for the query. |
|
451 * |
|
452 * @type string $fields The SELECT clause of the query. |
|
453 * @type string $join The JOIN clause of the query. |
|
454 * @type string $where The WHERE clause of the query. |
|
455 * @type string $orderby The ORDER BY clause of the query. |
|
456 * @type string $limits The LIMIT clause of the query. |
|
457 * @type string $groupby The GROUP BY clause of the query. |
|
458 * } |
449 * @param WP_Network_Query $query Current instance of WP_Network_Query (passed by reference). |
459 * @param WP_Network_Query $query Current instance of WP_Network_Query (passed by reference). |
450 */ |
460 */ |
451 $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $clauses ), &$this ) ); |
461 $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) ); |
452 |
462 |
453 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; |
463 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; |
454 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; |
464 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; |
455 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; |
465 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; |
456 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; |
466 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; |
478 $this->sql_clauses['from'] = "FROM $wpdb->site $join"; |
488 $this->sql_clauses['from'] = "FROM $wpdb->site $join"; |
479 $this->sql_clauses['groupby'] = $groupby; |
489 $this->sql_clauses['groupby'] = $groupby; |
480 $this->sql_clauses['orderby'] = $orderby; |
490 $this->sql_clauses['orderby'] = $orderby; |
481 $this->sql_clauses['limits'] = $limits; |
491 $this->sql_clauses['limits'] = $limits; |
482 |
492 |
483 $this->request = " |
493 // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841. |
484 {$this->sql_clauses['select']} |
494 $this->request = |
485 {$this->sql_clauses['from']} |
495 "{$this->sql_clauses['select']} |
486 {$where} |
496 {$this->sql_clauses['from']} |
487 {$this->sql_clauses['groupby']} |
497 {$where} |
488 {$this->sql_clauses['orderby']} |
498 {$this->sql_clauses['groupby']} |
489 {$this->sql_clauses['limits']} |
499 {$this->sql_clauses['orderby']} |
490 "; |
500 {$this->sql_clauses['limits']}"; |
491 |
501 |
492 if ( $this->query_vars['count'] ) { |
502 if ( $this->query_vars['count'] ) { |
493 return (int) $wpdb->get_var( $this->request ); |
503 return (int) $wpdb->get_var( $this->request ); |
494 } |
504 } |
495 |
505 |