167 * Sets up the WordPress query for retrieving networks. |
167 * Sets up the WordPress query for retrieving networks. |
168 * |
168 * |
169 * @since 4.6.0 |
169 * @since 4.6.0 |
170 * |
170 * |
171 * @param string|array $query Array or URL query string of parameters. |
171 * @param string|array $query Array or URL query string of parameters. |
172 * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids', |
172 * @return array|int List of WP_Network objects, a list of network IDs when 'fields' is set to 'ids', |
173 * or the number of networks when 'count' is passed as a query var. |
173 * or the number of networks when 'count' is passed as a query var. |
174 */ |
174 */ |
175 public function query( $query ) { |
175 public function query( $query ) { |
176 $this->query_vars = wp_parse_args( $query ); |
176 $this->query_vars = wp_parse_args( $query ); |
177 return $this->get_networks(); |
177 return $this->get_networks(); |
195 * |
195 * |
196 * @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). |
197 */ |
197 */ |
198 do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
198 do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
199 |
199 |
200 $network_ids = null; |
200 $network_data = null; |
201 |
201 |
202 /** |
202 /** |
203 * Filter the sites array before the query takes place. |
203 * Filter the network data before the query takes place. |
204 * |
204 * |
205 * Return a non-null value to bypass WordPress's default site queries. |
205 * Return a non-null value to bypass WordPress's default network queries. |
206 * |
206 * |
|
207 * The expected return type from this filter depends on the value passed in the request query_vars. |
|
208 * When `$this->query_vars['count']` is set, the filter should return the network count as an int. |
|
209 * When `'ids' === $this->query_vars['fields']`, the filter should return an array of network IDs. |
|
210 * Otherwise the filter should return an array of WP_Network objects. |
207 * |
211 * |
208 * @since 5.2.0 |
212 * @since 5.2.0 |
209 * |
213 * |
210 * @param array|null $site_ids Return an array of site data to short-circuit WP's site query, |
214 * @param array|null $network_data Return an array of network data to short-circuit WP's network query, |
211 * or null to allow WP to run its normal queries. |
215 * the network count as an integer if `$this->query_vars['count']` is set, |
212 * @param WP_Network_Query $this The WP_Network_Query instance, passed by reference. |
216 * or null to allow WP to run its normal queries. |
|
217 * @param WP_Network_Query $this The WP_Network_Query instance, passed by reference. |
213 */ |
218 */ |
214 $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) ); |
219 $network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) ); |
215 |
220 |
216 if ( null === $network_ids ) { |
221 if ( null !== $network_data ) { |
217 |
222 return $network_data; |
218 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
223 } |
219 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
224 |
220 |
225 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
221 // Ignore the $fields argument as the queried result will be the same regardless. |
226 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
222 unset( $_args['fields'] ); |
227 |
223 |
228 // Ignore the $fields argument as the queried result will be the same regardless. |
224 $key = md5( serialize( $_args ) ); |
229 unset( $_args['fields'] ); |
225 $last_changed = wp_cache_get_last_changed( 'networks' ); |
230 |
226 |
231 $key = md5( serialize( $_args ) ); |
227 $cache_key = "get_network_ids:$key:$last_changed"; |
232 $last_changed = wp_cache_get_last_changed( 'networks' ); |
228 $cache_value = wp_cache_get( $cache_key, 'networks' ); |
233 |
229 |
234 $cache_key = "get_network_ids:$key:$last_changed"; |
230 if ( false === $cache_value ) { |
235 $cache_value = wp_cache_get( $cache_key, 'networks' ); |
231 $network_ids = $this->get_network_ids(); |
236 |
232 if ( $network_ids ) { |
237 if ( false === $cache_value ) { |
233 $this->set_found_networks(); |
238 $network_ids = $this->get_network_ids(); |
234 } |
239 if ( $network_ids ) { |
235 |
240 $this->set_found_networks(); |
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']; |
|
244 } |
241 } |
|
242 |
|
243 $cache_value = array( |
|
244 'network_ids' => $network_ids, |
|
245 'found_networks' => $this->found_networks, |
|
246 ); |
|
247 wp_cache_add( $cache_key, $cache_value, 'networks' ); |
|
248 } else { |
|
249 $network_ids = $cache_value['network_ids']; |
|
250 $this->found_networks = $cache_value['found_networks']; |
245 } |
251 } |
246 |
252 |
247 if ( $this->found_networks && $this->query_vars['number'] ) { |
253 if ( $this->found_networks && $this->query_vars['number'] ) { |
248 $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
254 $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); |
249 } |
255 } |
536 'domain', |
542 'domain', |
537 'path', |
543 'path', |
538 ); |
544 ); |
539 |
545 |
540 $parsed = false; |
546 $parsed = false; |
541 if ( $orderby == 'network__in' ) { |
547 if ( 'network__in' === $orderby ) { |
542 $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) ); |
548 $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) ); |
543 $parsed = "FIELD( {$wpdb->site}.id, $network__in )"; |
549 $parsed = "FIELD( {$wpdb->site}.id, $network__in )"; |
544 } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) { |
550 } elseif ( 'domain_length' === $orderby || 'path_length' === $orderby ) { |
545 $field = substr( $orderby, 0, -7 ); |
551 $field = substr( $orderby, 0, -7 ); |
546 $parsed = "CHAR_LENGTH($wpdb->site.$field)"; |
552 $parsed = "CHAR_LENGTH($wpdb->site.$field)"; |
547 } elseif ( in_array( $orderby, $allowed_keys ) ) { |
553 } elseif ( in_array( $orderby, $allowed_keys, true ) ) { |
548 $parsed = "$wpdb->site.$orderby"; |
554 $parsed = "$wpdb->site.$orderby"; |
549 } |
555 } |
550 |
556 |
551 return $parsed; |
557 return $parsed; |
552 } |
558 } |