Multisite: Use WP_Network_Query in WP_Network::get_by_path().

An additional unit test has been introduced to verify the method works properly when using an external object cache.

Props spacedmonkey, jeremyfelt.
Fixes #37217.

Built from https://develop.svn.wordpress.org/trunk@40102


git-svn-id: http://core.svn.wordpress.org/trunk@40039 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2017-02-22 10:42:45 +00:00
parent b6ce4e2830
commit 55df308b4d
2 changed files with 25 additions and 27 deletions

View File

@ -267,8 +267,6 @@ class WP_Network {
* @return WP_Network|bool Network object if successful. False when no network is found.
*/
public static function get_by_path( $domain = '', $path = '', $segments = null ) {
global $wpdb;
$domains = array( $domain );
$pieces = explode( '.', $domain );
@ -295,7 +293,11 @@ class WP_Network {
if ( wp_using_ext_object_cache() ) {
$using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
if ( false === $using_paths ) {
$using_paths = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} WHERE path <> '/' LIMIT 1" );
$using_paths = get_networks( array(
'number' => 1,
'count' => true,
'path__not_in' => '/',
) );
wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' );
}
}
@ -353,35 +355,31 @@ class WP_Network {
return $pre;
}
// @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
// We already have paths covered. What about how far domains should be drilled down (including www)?
$search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
if ( ! $using_paths ) {
$network = $wpdb->get_row( "
SELECT * FROM {$wpdb->site}
WHERE domain IN ({$search_domains})
ORDER BY CHAR_LENGTH(domain)
DESC LIMIT 1
" );
$networks = get_networks( array(
'number' => 1,
'orderby' => array(
'domain_length' => 'DESC',
),
'domain__in' => $domains,
) );
if ( ! empty( $network ) && ! is_wp_error( $network ) ) {
return new WP_Network( $network );
if ( ! empty( $networks ) ) {
return array_shift( $networks );
}
return false;
} else {
$search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
$networks = $wpdb->get_results( "
SELECT * FROM {$wpdb->site}
WHERE domain IN ({$search_domains})
AND path IN ({$search_paths})
ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC
" );
}
$networks = get_networks( array(
'orderby' => array(
'domain_length' => 'DESC',
'path_length' => 'DESC',
),
'domain__in' => $domains,
'path__in' => $paths,
) );
/*
* Domains are sorted by length of domain, then by length of path.
* The domain must match for the path to be considered. Otherwise,
@ -402,7 +400,7 @@ class WP_Network {
}
if ( true === $found ) {
return new WP_Network( $network );
return $network;
}
return false;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8-alpha-40101';
$wp_version = '4.8-alpha-40102';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.