Source
File: wp-includes/class-wp-network.php
public static function get_by_path( $domain = '', $path = '', $segments = null ) {
$domains = array( $domain );
$pieces = explode( '.', $domain );
while ( array_shift( $pieces ) ) {
if ( ! empty( $pieces ) ) {
$domains[] = implode( '.', $pieces );
}
}
$using_paths = true;
if ( wp_using_ext_object_cache() ) {
$using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
if ( false === $using_paths ) {
$using_paths = get_networks(
array(
'number' => 1,
'count' => true,
'path__not_in' => '/',
)
);
wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' );
}
}
$paths = array();
if ( $using_paths ) {
$path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
$segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
$path_segments = array_slice( $path_segments, 0, $segments );
}
while ( count( $path_segments ) ) {
$paths[] = '/' . implode( '/', $path_segments ) . '/';
array_pop( $path_segments );
}
$paths[] = '/';
}
$pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
if ( null !== $pre ) {
return $pre;
}
if ( ! $using_paths ) {
$networks = get_networks(
array(
'number' => 1,
'orderby' => array(
'domain_length' => 'DESC',
),
'domain__in' => $domains,
)
);
if ( ! empty( $networks ) ) {
return array_shift( $networks );
}
return false;
}
$networks = get_networks(
array(
'orderby' => array(
'domain_length' => 'DESC',
'path_length' => 'DESC',
),
'domain__in' => $domains,
'path__in' => $paths,
)
);
$found = false;
foreach ( $networks as $network ) {
if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
if ( in_array( $network->path, $paths, true ) ) {
$found = true;
break;
}
}
if ( '/' === $network->path ) {
$found = true;
break;
}
}
if ( true === $found ) {
return $network;
}
return false;
}