3 * These functions are needed to load Multisite.
8 * @subpackage Multisite
12 * Whether a subdomain configuration is enabled.
16 * @return bool True if subdomain configuration is enabled, false otherwise.
18 function is_subdomain_install() {
19 if ( defined('SUBDOMAIN_INSTALL') )
20 return SUBDOMAIN_INSTALL;
22 return ( defined( 'VHOST' ) && VHOST == 'yes' );
26 * Returns array of network plugin files to be included in global scope.
28 * The default directory is wp-content/plugins. To change the default directory
29 * manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
34 * @return array Files to include.
36 function wp_get_active_network_plugins() {
37 $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
38 if ( empty( $active_plugins ) )
42 $active_plugins = array_keys( $active_plugins );
43 sort( $active_plugins );
45 foreach ( $active_plugins as $plugin ) {
46 if ( ! validate_file( $plugin ) // $plugin must validate as file
47 && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
48 && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
50 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
56 * Checks status of current blog.
58 * Checks if the blog is deleted, inactive, archived, or spammed.
60 * Dies with a default message if the blog does not pass the check.
62 * To change the default message when a blog does not pass the check,
63 * use the wp-content/blog-deleted.php, blog-inactive.php and
64 * blog-suspended.php drop-ins.
68 * @return true|string Returns true on success, or drop-in file to include.
70 function ms_site_check() {
71 $blog = get_blog_details();
74 * Filter checking the status of the current blog.
78 * @param bool null Whether to skip the blog status check. Default null.
80 $check = apply_filters( 'ms_site_check', null );
81 if ( null !== $check )
84 // Allow super admins to see blocked sites
85 if ( is_super_admin() )
88 if ( '1' == $blog->deleted ) {
89 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
90 return WP_CONTENT_DIR . '/blog-deleted.php';
92 wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
95 if ( '2' == $blog->deleted ) {
96 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) )
97 return WP_CONTENT_DIR . '/blog-inactive.php';
99 wp_die( sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact <a href="mailto:%1$s">%1$s</a>.' ), str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_current_site()->domain ) ) ) );
102 if ( $blog->archived == '1' || $blog->spam == '1' ) {
103 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
104 return WP_CONTENT_DIR . '/blog-suspended.php';
106 wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
113 * Retrieve a network object by its domain and path.
119 * @param string $domain Domain to check.
120 * @param string $path Path to check.
121 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
122 * @return object|false Network object if successful. False when no network is found.
124 function get_network_by_path( $domain, $path, $segments = null ) {
127 $domains = array( $domain );
128 $pieces = explode( '.', $domain );
131 * It's possible one domain to search is 'com', but it might as well
132 * be 'localhost' or some other locally mapped domain.
134 while ( array_shift( $pieces ) ) {
136 $domains[] = implode( '.', $pieces );
141 * If we've gotten to this function during normal execution, there is
142 * more than one network installed. At this point, who knows how many
143 * we have. Attempt to optimize for the situation where networks are
144 * only domains, thus meaning paths never need to be considered.
146 * This is a very basic optimization; anything further could have drawbacks
147 * depending on the setup, so this is best done per-install.
150 if ( wp_using_ext_object_cache() ) {
151 $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
152 if ( false === $using_paths ) {
153 $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
154 wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options' );
159 if ( $using_paths ) {
160 $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
163 * Filter the number of path segments to consider when searching for a site.
167 * @param int|null $segments The number of path segments to consider. WordPress by default looks at
168 * one path segment. The function default of null only makes sense when you
169 * know the requested path should match a network.
170 * @param string $domain The requested domain.
171 * @param string $path The requested path, in full.
173 $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
175 if ( null !== $segments && count($path_segments ) > $segments ) {
176 $path_segments = array_slice( $path_segments, 0, $segments );
179 while ( count( $path_segments ) ) {
180 $paths[] = '/' . implode( '/', $path_segments ) . '/';
181 array_pop( $path_segments );
188 * Determine a network by its domain and path.
190 * This allows one to short-circuit the default logic, perhaps by
191 * replacing it with a routine that is more optimal for your setup.
193 * Return null to avoid the short-circuit. Return false if no network
194 * can be found at the requested domain and path. Otherwise, return
195 * an object from wp_get_network().
199 * @param null|bool|object $network Network value to return by path.
200 * @param string $domain The requested domain.
201 * @param string $path The requested path, in full.
202 * @param int|null $segments The suggested number of paths to consult.
203 * Default null, meaning the entire path was to be consulted.
204 * @param array $paths The paths to search for, based on $path and $segments.
206 $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
207 if ( null !== $pre ) {
211 // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
212 // We already have paths covered. What about how far domains should be drilled down (including www)?
214 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
216 if ( ! $using_paths ) {
217 $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
218 WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
220 return wp_get_network( $network );
225 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
226 $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
227 WHERE domain IN ($search_domains) AND path IN ($search_paths)
228 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
232 * Domains are sorted by length of domain, then by length of path.
233 * The domain must match for the path to be considered. Otherwise,
234 * a network with the path of / will suffice.
237 foreach ( $networks as $network ) {
238 if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
239 if ( in_array( $network->path, $paths, true ) ) {
244 if ( $network->path === '/' ) {
251 return wp_get_network( $network );
258 * Retrieve an object containing information about the requested network.
264 * @param object|int $network The network's database row or ID.
265 * @return object|false Object containing network information if found, false if not.
267 function wp_get_network( $network ) {
270 if ( ! is_object( $network ) ) {
271 $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
281 * Retrieve a site object by its domain and path.
287 * @param string $domain Domain to check.
288 * @param string $path Path to check.
289 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
290 * @return object|false Site object if successful. False when no site is found.
292 function get_site_by_path( $domain, $path, $segments = null ) {
295 $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
298 * Filter the number of path segments to consider when searching for a site.
302 * @param int|null $segments The number of path segments to consider. WordPress by default looks at
303 * one path segment following the network path. The function default of
304 * null only makes sense when you know the requested path should match a site.
305 * @param string $domain The requested domain.
306 * @param string $path The requested path, in full.
308 $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
310 if ( null !== $segments && count( $path_segments ) > $segments ) {
311 $path_segments = array_slice( $path_segments, 0, $segments );
316 while ( count( $path_segments ) ) {
317 $paths[] = '/' . implode( '/', $path_segments ) . '/';
318 array_pop( $path_segments );
324 * Determine a site by its domain and path.
326 * This allows one to short-circuit the default logic, perhaps by
327 * replacing it with a routine that is more optimal for your setup.
329 * Return null to avoid the short-circuit. Return false if no site
330 * can be found at the requested domain and path. Otherwise, return
335 * @param null|bool|object $site Site value to return by path.
336 * @param string $domain The requested domain.
337 * @param string $path The requested path, in full.
338 * @param int|null $segments The suggested number of paths to consult.
339 * Default null, meaning the entire path was to be consulted.
340 * @param array $paths The paths to search for, based on $path and $segments.
342 $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
343 if ( null !== $pre ) {
349 * get_blog_details(), caching, etc. Consider alternative optimization routes,
350 * perhaps as an opt-in for plugins, rather than using the pre_* filter.
351 * For example: The segments filter can expand or ignore paths.
352 * If persistent caching is enabled, we could query the DB for a path <> '/'
353 * then cache whether we can just always ignore paths.
356 // Either www or non-www is supported, not both. If a www domain is requested,
357 // query for both to provide the proper redirect.
358 $domains = array( $domain );
359 if ( 'www.' === substr( $domain, 0, 4 ) ) {
360 $domains[] = substr( $domain, 4 );
361 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
364 if ( count( $paths ) > 1 ) {
365 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
368 if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
369 $site = $wpdb->get_row( "SELECT * FROM $wpdb->blogs WHERE domain IN ($search_domains) AND path IN ($search_paths) ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC LIMIT 1" );
370 } elseif ( count( $domains ) > 1 ) {
371 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
372 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
373 $site = $wpdb->get_row( $sql );
374 } elseif ( count( $paths ) > 1 ) {
375 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
376 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
377 $site = $wpdb->get_row( $sql );
379 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
383 // @todo get_blog_details()
391 * Displays a failure message.
393 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
399 * @global string $domain
400 * @global string $path
402 function ms_not_installed() {
403 global $wpdb, $domain, $path;
405 if ( ! is_admin() ) {
409 wp_load_translations_early();
411 $title = __( 'Error establishing a database connection' );
413 $msg = '<h1>' . $title . '</h1>';
414 $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
415 $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
416 $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
417 if ( ! $wpdb->get_var( $query ) ) {
418 $msg .= '<p>' . sprintf(
419 /* translators: %s: table name */
420 __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
421 '<code>' . $wpdb->site . '</code>'
424 $msg .= '<p>' . sprintf(
425 /* translators: 1: site url, 2: table name, 3: database name */
426 __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
427 '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
428 '<code>' . $wpdb->blogs . '</code>',
429 '<code>' . DB_NAME . '</code>'
432 $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
433 $msg .= __( 'Read the <a target="_blank" href="https://codex.wordpress.org/Debugging_a_WordPress_Network">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' );
434 $msg .= ' ' . __( 'If you’re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
435 foreach ( $wpdb->tables('global') as $t => $table ) {
436 if ( 'sitecategories' == $t )
438 $msg .= '<li>' . $table . '</li>';
442 wp_die( $msg, $title, array( 'response' => 500 ) );
446 * This deprecated function formerly set the site_name property of the $current_site object.
448 * This function simply returns the object, as before.
449 * The bootstrap takes care of setting site_name.
453 * @deprecated 3.9.0 Use get_current_site() instead.
455 * @param object $current_site
458 function get_current_site_name( $current_site ) {
459 _deprecated_function( __FUNCTION__, '3.9', 'get_current_site()' );
460 return $current_site;
464 * This deprecated function managed much of the site and network loading in multisite.
466 * The current bootstrap code is now responsible for parsing the site and network load as
467 * well as setting the global $current_site object.
473 * @global object $current_site
477 function wpmu_current_site() {
478 global $current_site;
479 _deprecated_function( __FUNCTION__, '3.9' );
480 return $current_site;