252 } else { |
252 } else { |
253 $cache_key = 'network:' . $this->id . ':main_site'; |
253 $cache_key = 'network:' . $this->id . ':main_site'; |
254 |
254 |
255 $main_site_id = wp_cache_get( $cache_key, 'site-options' ); |
255 $main_site_id = wp_cache_get( $cache_key, 'site-options' ); |
256 if ( false === $main_site_id ) { |
256 if ( false === $main_site_id ) { |
257 $_sites = get_sites( array( |
257 $_sites = get_sites( |
258 'fields' => 'ids', |
258 array( |
259 'number' => 1, |
259 'fields' => 'ids', |
260 'domain' => $this->domain, |
260 'number' => 1, |
261 'path' => $this->path, |
261 'domain' => $this->domain, |
262 'network_id' => $this->id, |
262 'path' => $this->path, |
263 ) ); |
263 'network_id' => $this->id, |
|
264 ) |
|
265 ); |
264 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; |
266 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; |
265 |
267 |
266 wp_cache_add( $cache_key, $main_site_id, 'site-options' ); |
268 wp_cache_add( $cache_key, $main_site_id, 'site-options' ); |
267 } |
269 } |
268 } |
270 } |
314 * |
316 * |
315 * The intent of this method is to match a network during bootstrap for a |
317 * The intent of this method is to match a network during bootstrap for a |
316 * requested site address. |
318 * requested site address. |
317 * |
319 * |
318 * @since 4.4.0 |
320 * @since 4.4.0 |
319 * @static |
|
320 * |
321 * |
321 * @param string $domain Domain to check. |
322 * @param string $domain Domain to check. |
322 * @param string $path Path to check. |
323 * @param string $path Path to check. |
323 * @param int|null $segments Path segments to use. Defaults to null, or the full path. |
324 * @param int|null $segments Path segments to use. Defaults to null, or the full path. |
324 * @return WP_Network|bool Network object if successful. False when no network is found. |
325 * @return WP_Network|bool Network object if successful. False when no network is found. |
348 */ |
349 */ |
349 $using_paths = true; |
350 $using_paths = true; |
350 if ( wp_using_ext_object_cache() ) { |
351 if ( wp_using_ext_object_cache() ) { |
351 $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' ); |
352 $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' ); |
352 if ( false === $using_paths ) { |
353 if ( false === $using_paths ) { |
353 $using_paths = get_networks( array( |
354 $using_paths = get_networks( |
354 'number' => 1, |
355 array( |
355 'count' => true, |
356 'number' => 1, |
356 'path__not_in' => '/', |
357 'count' => true, |
357 ) ); |
358 'path__not_in' => '/', |
358 wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' ); |
359 ) |
|
360 ); |
|
361 wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' ); |
359 } |
362 } |
360 } |
363 } |
361 |
364 |
362 $paths = array(); |
365 $paths = array(); |
363 if ( $using_paths ) { |
366 if ( $using_paths ) { |
398 * can be found at the requested domain and path. Otherwise, return |
401 * can be found at the requested domain and path. Otherwise, return |
399 * an object from wp_get_network(). |
402 * an object from wp_get_network(). |
400 * |
403 * |
401 * @since 3.9.0 |
404 * @since 3.9.0 |
402 * |
405 * |
403 * @param null|bool|object $network Network value to return by path. |
406 * @param null|bool|WP_Network $network Network value to return by path. |
404 * @param string $domain The requested domain. |
407 * @param string $domain The requested domain. |
405 * @param string $path The requested path, in full. |
408 * @param string $path The requested path, in full. |
406 * @param int|null $segments The suggested number of paths to consult. |
409 * @param int|null $segments The suggested number of paths to consult. |
407 * Default null, meaning the entire path was to be consulted. |
410 * Default null, meaning the entire path was to be consulted. |
408 * @param array $paths The paths to search for, based on $path and $segments. |
411 * @param string[] $paths Array of paths to search for, based on `$path` and `$segments`. |
409 */ |
412 */ |
410 $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths ); |
413 $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths ); |
411 if ( null !== $pre ) { |
414 if ( null !== $pre ) { |
412 return $pre; |
415 return $pre; |
413 } |
416 } |
414 |
417 |
415 if ( ! $using_paths ) { |
418 if ( ! $using_paths ) { |
416 $networks = get_networks( array( |
419 $networks = get_networks( |
417 'number' => 1, |
420 array( |
|
421 'number' => 1, |
|
422 'orderby' => array( |
|
423 'domain_length' => 'DESC', |
|
424 ), |
|
425 'domain__in' => $domains, |
|
426 ) |
|
427 ); |
|
428 |
|
429 if ( ! empty( $networks ) ) { |
|
430 return array_shift( $networks ); |
|
431 } |
|
432 |
|
433 return false; |
|
434 } |
|
435 |
|
436 $networks = get_networks( |
|
437 array( |
418 'orderby' => array( |
438 'orderby' => array( |
419 'domain_length' => 'DESC', |
439 'domain_length' => 'DESC', |
|
440 'path_length' => 'DESC', |
420 ), |
441 ), |
421 'domain__in' => $domains, |
442 'domain__in' => $domains, |
422 ) ); |
443 'path__in' => $paths, |
423 |
444 ) |
424 if ( ! empty( $networks ) ) { |
445 ); |
425 return array_shift( $networks ); |
|
426 } |
|
427 |
|
428 return false; |
|
429 } |
|
430 |
|
431 $networks = get_networks( array( |
|
432 'orderby' => array( |
|
433 'domain_length' => 'DESC', |
|
434 'path_length' => 'DESC', |
|
435 ), |
|
436 'domain__in' => $domains, |
|
437 'path__in' => $paths, |
|
438 ) ); |
|
439 |
446 |
440 /* |
447 /* |
441 * Domains are sorted by length of domain, then by length of path. |
448 * Domains are sorted by length of domain, then by length of path. |
442 * The domain must match for the path to be considered. Otherwise, |
449 * The domain must match for the path to be considered. Otherwise, |
443 * a network with the path of / will suffice. |
450 * a network with the path of / will suffice. |