Make WordPress Core


Ignore:
Timestamp:
02/13/2014 11:06:12 PM (10 years ago)
Author:
nacin
Message:

Multisite: Add get_network_by_path() and wp_get_network() to begin cleanup of multisite load.

Tries to get network detection under control by simplifying wpmu_current_site(). It now also pops off each subdomain to find a more general match. Adds unit tests for get_network_by_path() and a new network factory for unit tests.

Much of this is likely to change in 3.9 as more of ms-load.php and ms-settings.php gets hacked to bits.

props jeremyfelt.
see #27003.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-load.php

    r27076 r27178  
    135135
    136136/**
     137 * Retrieve a network object by its domain and path.
     138 *
     139 * @since 3.9.0
     140 *
     141 * @param string $domain Domain to check.
     142 * @param string $path   Path to check.
     143 * @return object|bool Network object if successful. False when no network is found.
     144 */
     145function get_network_by_path( $domain, $path ) {
     146    global $wpdb;
     147
     148    $network_id = false;
     149
     150    $domains = $exact_domains = array( $domain );
     151    $pieces = explode( '.', $domain );
     152
     153    // It's possible one domain to search is 'com', but it might as well
     154    // be 'localhost' or some other locally mapped domain.
     155    while ( array_shift( $pieces ) ) {
     156        if ( $pieces ) {
     157            $domains[] = implode( '.', $pieces );
     158        }
     159    }
     160
     161    if ( '/' !== $path ) {
     162        $paths = array( '/', $path );
     163    } else {
     164        $paths = array( '/' );
     165    }
     166
     167    $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
     168    $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     169
     170    $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
     171        WHERE domain IN ($search_domains) AND path IN ($paths)
     172        ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     173
     174    /*
     175     * Domains are sorted by length of domain, then by length of path.
     176     * The domain must match for the path to be considered. Otherwise,
     177     * a network with the path of / will suffice.
     178     */
     179    $found = false;
     180    foreach ( $networks as $network ) {
     181        if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
     182            if ( $network->path === $path ) {
     183                $found = true;
     184                break;
     185            }
     186        }
     187        if ( $network->path === '/' ) {
     188            $found = true;
     189            break;
     190        }
     191    }
     192
     193    if ( $found ) {
     194        $network = wp_get_network( $network );
     195
     196        return $network;
     197    }
     198
     199    return false;
     200}
     201
     202/**
     203 * Retrieve an object containing information about the requested network.
     204 *
     205 * @since 3.9.0
     206 *
     207 * @param int $network_id The network's DB row or ID.
     208 * @return mixed Object containing network information if found, false if not.
     209 */
     210function wp_get_network( $network ) {
     211    global $wpdb;
     212
     213    if ( ! is_object( $network ) ) {
     214        $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
     215        if ( ! $network ) {
     216            return false;
     217        }
     218    }
     219
     220    return $network;
     221}
     222
     223/**
    137224 * Sets current_site object.
    138225 *
     
    142229 */
    143230function wpmu_current_site() {
    144     global $wpdb, $current_site, $domain, $path, $sites, $cookie_domain;
     231    global $wpdb, $current_site, $domain, $path;
    145232
    146233    if ( empty( $current_site ) )
    147234        $current_site = new stdClass;
    148235
     236    // 1. If constants are defined, that's our network.
    149237    if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    150238        $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
     
    155243        elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    156244            $current_site->blog_id = BLOGID_CURRENT_SITE;
    157         if ( DOMAIN_CURRENT_SITE == $domain )
    158             $current_site->cookie_domain = $cookie_domain;
    159         elseif ( substr( $current_site->domain, 0, 4 ) == 'www.' )
    160             $current_site->cookie_domain = substr( $current_site->domain, 4 );
    161         else
    162             $current_site->cookie_domain = $current_site->domain;
    163 
    164         wp_load_core_site_options( $current_site->id );
    165 
    166         return $current_site;
    167     }
    168 
    169     $current_site = wp_cache_get( 'current_site', 'site-options' );
    170     if ( $current_site )
    171         return $current_site;
    172 
    173     $sites = $wpdb->get_results( "SELECT * FROM $wpdb->site" ); // usually only one site
    174     if ( 1 == count( $sites ) ) {
    175         $current_site = $sites[0];
    176         wp_load_core_site_options( $current_site->id );
    177         $path = $current_site->path;
    178         $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $current_site->domain, $current_site->path ) );
    179         $current_site = get_current_site_name( $current_site );
    180         if ( substr( $current_site->domain, 0, 4 ) == 'www.' )
    181             $current_site->cookie_domain = substr( $current_site->domain, 4 );
    182         wp_cache_set( 'current_site', $current_site, 'site-options' );
    183         return $current_site;
    184     }
    185     $path = substr( $_SERVER[ 'REQUEST_URI' ], 0, 1 + strpos( $_SERVER[ 'REQUEST_URI' ], '/', 1 ) );
    186 
    187     if ( $domain == $cookie_domain )
    188         $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $domain, $path ) );
    189     else
    190         $current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = %s ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain, $path ) );
    191 
    192     if ( ! $current_site ) {
    193         if ( $domain == $cookie_domain )
    194             $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $domain ) );
    195         else
    196             $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain IN ( %s, %s ) AND path = '/' ORDER BY CHAR_LENGTH( domain ) DESC LIMIT 1", $domain, $cookie_domain ) );
    197     }
    198 
    199     if ( $current_site ) {
    200         $path = $current_site->path;
    201         $current_site->cookie_domain = $cookie_domain;
    202         return $current_site;
    203     }
    204 
    205     if ( is_subdomain_install() ) {
    206         $sitedomain = substr( $domain, 1 + strpos( $domain, '.' ) );
    207         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
    208         if ( $current_site ) {
    209             $current_site->cookie_domain = $current_site->domain;
    210             return $current_site;
    211         }
    212 
    213         $current_site = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->site WHERE domain = %s AND path='/'", $sitedomain) );
    214     }
    215 
    216     if ( $current_site || defined( 'WP_INSTALLING' ) ) {
    217         $path = '/';
    218         return $current_site;
    219     }
    220 
    221     // Still no dice.
    222     wp_load_translations_early();
    223 
    224     if ( 1 == count( $sites ) )
    225         wp_die( sprintf( __( 'That site does not exist. Please try <a href="%s">%s</a>.' ), 'http://' . $sites[0]->domain . $sites[0]->path ) );
    226     else
    227         wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
     245
     246    // 2. Pull the network from cache, if possible.
     247    } elseif ( ! $current_site = wp_cache_get( 'current_site', 'site-options' ) ) {
     248
     249        // 3. See if they have only one network.
     250        $networks = $wpdb->get_col( "SELECT id FROM $wpdb->site LIMIT 2" );
     251
     252        if ( count( $networks ) <= 1 ) {
     253            $current_site = wp_get_network( $networks[0]->id );
     254
     255            $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id
     256                FROM $wpdb->blogs WHERE domain = %s AND path = %s",
     257                $current_site->domain, $current_site->path ) );
     258
     259            wp_cache_set( 'current_site', 'site-options' );
     260
     261        // 4. Multiple networks are in play. Determine which via domain and path.
     262        } else {
     263            // Find the first path segment.
     264            $path = substr( $_SERVER['REQUEST_URI'], 0, 1 + strpos( $_SERVER['REQUEST_URI'], '/', 1 ) );
     265            $current_site = get_network_by_path( $domain, $path );
     266
     267            // Option 1. We did not find anything.
     268            if ( ! $current_site ) {
     269                wp_load_translations_early();
     270                wp_die( __( 'No site defined on this host. If you are the owner of this site, please check <a href="http://codex.wordpress.org/Debugging_a_WordPress_Network">Debugging a WordPress Network</a> for help.' ) );
     271            }
     272        }
     273    }
     274
     275    // Option 2. We found something. Load up site meta and return.
     276    wp_load_core_site_options();
     277    $current_site = get_current_site_name( $current_site );
     278    return $current_site;
    228279}
    229280
Note: See TracChangeset for help on using the changeset viewer.