WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/02/2014 10:24:50 PM (6 years ago)
Author:
nacin
Message:

Introduce get_site_by_path() and further rewrite the site detection process for multisite.

This is the first big step to supporting arbitrary domains and paths. In this new approach, sites are detected first where possible, then the network is inferred. Allows filtering for arbitrary path segments, smooths out some weirdness, and removes various restrictions. A sunrise plugin could do much of its work by adding filters, if those are even needed.

see #27003.

File:
1 edited

Legend:

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

    r27275 r27359  
    116116 * Sets current site name.
    117117 *
     118 * @todo deprecate
     119 *
    118120 * @access private
    119121 * @since 3.0.0
     
    139141 * @since 3.9.0
    140142 *
    141  * @param string $domain Domain to check.
    142  * @param string $path   Path to check.
     143 * @param string $domain   Domain to check.
     144 * @param string $path     Path to check.
     145 * @param int    $segments Path segments to use. Defaults to null, or the full path.
    143146 * @return object|bool Network object if successful. False when no network is found.
    144147 */
    145 function get_network_by_path( $domain, $path ) {
     148function get_network_by_path( $domain, $path, $segments = null ) {
    146149    global $wpdb;
    147 
    148     $network_id = false;
    149150
    150151    $domains = $exact_domains = array( $domain );
     
    159160    }
    160161
    161     if ( '/' !== $path ) {
    162         $paths = array( '/', $path );
     162    /*
     163     * If we've gotten to this function during normal execution, there is
     164     * more than one network installed. At this point, who knows how many
     165     * we have. Attempt to optimize for the situation where networks are
     166     * only domains, thus meaning paths never need to be considered.
     167     *
     168     * This is a very basic optimization; anything further could have drawbacks
     169     * depending on the setup, so this is best done per-install.
     170     */
     171    $using_paths = true;
     172    if ( wp_using_ext_object_cache() ) {
     173        $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
     174        if ( false === $using_paths ) {
     175            $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
     176            wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options'  );
     177        }
     178    }
     179
     180    $paths = array();
     181    if ( $using_paths ) {
     182        $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
     183
     184        /**
     185         * Filter the number of path segments to consider when searching for a site.
     186         *
     187         * @since 3.9.0
     188         *
     189         * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     190         *                         one path segment. The function default of null only makes sense when you
     191         *                         know the requested path should match a network.
     192         * @param string $domain   The requested domain.
     193         * @param string $path     The requested path, in full.
     194         */
     195        $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
     196
     197        if ( null !== $segments && count($path_segments ) > $segments ) {
     198            $path_segments = array_slice( $path_segments, 0, $segments );
     199        }
     200
     201        while ( count( $path_segments ) ) {
     202            $paths[] = '/' . implode( '/', $path_segments ) . '/';
     203            array_pop( $path_segments );
     204        }
     205
     206        $paths[] = '/';
     207    }
     208
     209    /**
     210     * Determine a network by its domain and path.
     211     *
     212     * This allows one to short-circuit the default logic, perhaps by
     213     * replacing it with a routine that is more optimal for your setup.
     214     *
     215     * Return null to avoid the short-circuit. Return false if no network
     216     * can be found at the requested domain and path. Otherwise, return
     217     * an object from wp_get_network().
     218     *
     219     * @since 3.9.0
     220     *
     221     * @param string $domain   The requested domain.
     222     * @param string $path     The requested path, in full.
     223     * @param mixed  $segments The suggested number of paths to consult.
     224     *                         Default null, meaning the entire path was to be consulted.
     225     * @param array  $paths    The paths to search for, based on $path and $segments.
     226     */
     227    $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
     228    if ( null !== $pre ) {
     229        return $pre;
     230    }
     231
     232    // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
     233    // We already have paths covered. What about how far domains should be drilled down (including www)?
     234
     235    $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
     236
     237    if ( ! $using_paths ) {
     238        $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
     239            WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
     240        if ( $network ) {
     241            return wp_get_network( $network );
     242        }
     243        return false;
     244
    163245    } 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" );
     246        $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     247        $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
     248            WHERE domain IN ($search_domains) AND path IN ($search_paths)
     249            ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
     250    }
    173251
    174252    /*
     
    180258    foreach ( $networks as $network ) {
    181259        if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
    182             if ( $network->path === $path ) {
     260            if ( in_array( $network->path, $paths, true ) ) {
    183261                $found = true;
    184262                break;
     
    192270
    193271    if ( $found ) {
    194         $network = wp_get_network( $network );
    195 
    196         return $network;
     272        return wp_get_network( $network );
    197273    }
    198274
     
    222298
    223299/**
    224  * Sets current_site object.
    225  *
    226  * @access private
    227  * @since 3.0.0
    228  * @return object $current_site object
     300 * @todo deprecate
    229301 */
    230302function wpmu_current_site() {
    231     global $wpdb, $current_site, $domain, $path;
    232 
    233     if ( empty( $current_site ) )
    234         $current_site = new stdClass;
    235 
    236     // 1. If constants are defined, that's our network.
    237     if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
    238         $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
    239         $current_site->domain = DOMAIN_CURRENT_SITE;
    240         $current_site->path   = $path = PATH_CURRENT_SITE;
    241         if ( defined( 'BLOG_ID_CURRENT_SITE' ) )
    242             $current_site->blog_id = BLOG_ID_CURRENT_SITE;
    243         elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) // deprecated.
    244             $current_site->blog_id = BLOGID_CURRENT_SITE;
    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] );
    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;
     303}
     304
     305/**
     306 * Retrieve a site object by its domain and path.
     307 *
     308 * @since 3.9.0
     309 *
     310 * @param string $domain   Domain to check.
     311 * @param string $path     Path to check.
     312 * @param int    $segments Path segments to use. Defaults to null, or the full path.
     313 * @return object|bool Site object if successful. False when no site is found.
     314 */
     315function get_site_by_path( $domain, $path, $segments = null ) {
     316    global $wpdb;
     317
     318    $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
     319
     320    /**
     321     * Filter the number of path segments to consider when searching for a site.
     322     *
     323     * @since 3.9.0
     324     *
     325
     326     * @param mixed  $segments The number of path segments to consider. WordPress by default looks at
     327     *                         one path segment following the network path. The function default of
     328     *                         null only makes sense when you know the requested path should match a site.
     329     * @param string $domain   The requested domain.
     330     * @param string $path     The requested path, in full.
     331     */
     332    $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
     333
     334    if ( null !== $segments && count($path_segments ) > $segments ) {
     335        $path_segments = array_slice( $path_segments, 0, $segments );
     336    }
     337
     338    while ( count( $path_segments ) ) {
     339        $paths[] = '/' . implode( '/', $path_segments ) . '/';
     340        array_pop( $path_segments );
     341    }
     342
     343    $paths[] = '/';
     344
     345    /**
     346     * Determine a site by its domain and path.
     347     *
     348     * This allows one to short-circuit the default logic, perhaps by
     349     * replacing it with a routine that is more optimal for your setup.
     350     *
     351     * Return null to avoid the short-circuit. Return false if no site
     352     * can be found at the requested domain and path. Otherwise, return
     353     * a site object.
     354     *
     355     * @since 3.9.0
     356     *
     357     * @param string $domain   The requested domain.
     358     * @param string $path     The requested path, in full.
     359     * @param mixed  $segments The suggested number of paths to consult.
     360     *                         Default null, meaning the entire path was to be consulted.
     361     * @param array  $paths    The paths to search for, based on $path and $segments.
     362     */
     363    $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
     364    if ( null !== $pre ) {
     365        return $pre;
     366    }
     367
     368    // @todo
     369    // get_blog_details(), caching, etc. Consider alternative optimization routes,
     370    // perhaps as an opt-in for plugins, rather than using the pre_* filter.
     371    // For example: The segments filter can expand or ignore paths.
     372    // If persistent caching is enabled, we could query the DB for a path <> '/'
     373    // then cache whether we can just always ignore paths.
     374
     375    if ( count( $paths ) > 1 ) {
     376        $paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
     377        $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs
     378            WHERE domain = %s AND path IN ($paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1", $domain ) );
     379    } else {
     380        $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, $paths[0] ) );
     381    }
     382
     383    if ( $site ) {
     384        // @todo get_blog_details()
     385        return $site;
     386    }
     387
     388    return false;
    279389}
    280390
Note: See TracChangeset for help on using the changeset viewer.