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 if ( defined('VHOST') && VHOST == 'yes' )
29 * Returns array of network plugin files to be included in global scope.
31 * The default directory is wp-content/plugins. To change the default directory
32 * manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
37 * @return array Files to include.
39 function wp_get_active_network_plugins() {
40 $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
41 if ( empty( $active_plugins ) )
45 $active_plugins = array_keys( $active_plugins );
46 sort( $active_plugins );
48 foreach ( $active_plugins as $plugin ) {
49 if ( ! validate_file( $plugin ) // $plugin must validate as file
50 && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
51 && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
53 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
59 * Checks status of current blog.
61 * Checks if the blog is deleted, inactive, archived, or spammed.
63 * Dies with a default message if the blog does not pass the check.
65 * To change the default message when a blog does not pass the check,
66 * use the wp-content/blog-deleted.php, blog-inactive.php and
67 * blog-suspended.php drop-ins.
71 * @return bool|string Returns true on success, or drop-in file to include.
73 function ms_site_check() {
74 $blog = get_blog_details();
77 * Filter checking the status of the current blog.
81 * @param bool null Whether to skip the blog status check. Default null.
83 $check = apply_filters( 'ms_site_check', null );
84 if ( null !== $check )
87 // Allow super admins to see blocked sites
88 if ( is_super_admin() )
91 if ( '1' == $blog->deleted ) {
92 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
93 return WP_CONTENT_DIR . '/blog-deleted.php';
95 wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
98 if ( '2' == $blog->deleted ) {
99 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) )
100 return WP_CONTENT_DIR . '/blog-inactive.php';
102 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 ) ) ) );
105 if ( $blog->archived == '1' || $blog->spam == '1' ) {
106 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
107 return WP_CONTENT_DIR . '/blog-suspended.php';
109 wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
116 * Retrieve a network object by its domain and path.
120 * @param string $domain Domain to check.
121 * @param string $path Path to check.
122 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
123 * @return object|bool Network object if successful. False when no network is found.
125 function get_network_by_path( $domain, $path, $segments = null ) {
128 $domains = array( $domain );
129 $pieces = explode( '.', $domain );
132 * It's possible one domain to search is 'com', but it might as well
133 * be 'localhost' or some other locally mapped domain.
135 while ( array_shift( $pieces ) ) {
137 $domains[] = implode( '.', $pieces );
142 * If we've gotten to this function during normal execution, there is
143 * more than one network installed. At this point, who knows how many
144 * we have. Attempt to optimize for the situation where networks are
145 * only domains, thus meaning paths never need to be considered.
147 * This is a very basic optimization; anything further could have drawbacks
148 * depending on the setup, so this is best done per-install.
151 if ( wp_using_ext_object_cache() ) {
152 $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
153 if ( false === $using_paths ) {
154 $using_paths = (bool) $wpdb->get_var( "SELECT id FROM $wpdb->site WHERE path <> '/' LIMIT 1" );
155 wp_cache_add( 'networks_have_paths', (int) $using_paths, 'site-options' );
160 if ( $using_paths ) {
161 $path_segments = array_filter( explode( '/', trim( $path, "/" ) ) );
164 * Filter the number of path segments to consider when searching for a site.
168 * @param int|null $segments The number of path segments to consider. WordPress by default looks at
169 * one path segment. The function default of null only makes sense when you
170 * know the requested path should match a network.
171 * @param string $domain The requested domain.
172 * @param string $path The requested path, in full.
174 $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
176 if ( null !== $segments && count($path_segments ) > $segments ) {
177 $path_segments = array_slice( $path_segments, 0, $segments );
180 while ( count( $path_segments ) ) {
181 $paths[] = '/' . implode( '/', $path_segments ) . '/';
182 array_pop( $path_segments );
189 * Determine a network by its domain and path.
191 * This allows one to short-circuit the default logic, perhaps by
192 * replacing it with a routine that is more optimal for your setup.
194 * Return null to avoid the short-circuit. Return false if no network
195 * can be found at the requested domain and path. Otherwise, return
196 * an object from wp_get_network().
200 * @param null|bool|object $network Network value to return by path.
201 * @param string $domain The requested domain.
202 * @param string $path The requested path, in full.
203 * @param int|null $segments The suggested number of paths to consult.
204 * Default null, meaning the entire path was to be consulted.
205 * @param array $paths The paths to search for, based on $path and $segments.
207 $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
208 if ( null !== $pre ) {
212 // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
213 // We already have paths covered. What about how far domains should be drilled down (including www)?
215 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
217 if ( ! $using_paths ) {
218 $network = $wpdb->get_row( "SELECT id, domain, path FROM $wpdb->site
219 WHERE domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1" );
221 return wp_get_network( $network );
226 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
227 $networks = $wpdb->get_results( "SELECT id, domain, path FROM $wpdb->site
228 WHERE domain IN ($search_domains) AND path IN ($search_paths)
229 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC" );
233 * Domains are sorted by length of domain, then by length of path.
234 * The domain must match for the path to be considered. Otherwise,
235 * a network with the path of / will suffice.
238 foreach ( $networks as $network ) {
239 if ( $network->domain === $domain || "www.$network->domain" === $domain ) {
240 if ( in_array( $network->path, $paths, true ) ) {
245 if ( $network->path === '/' ) {
252 return wp_get_network( $network );
259 * Retrieve an object containing information about the requested network.
263 * @param object|int $network The network's database row or ID.
264 * @return object|bool Object containing network information if found, false if not.
266 function wp_get_network( $network ) {
269 if ( ! is_object( $network ) ) {
270 $network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->site WHERE id = %d", $network ) );
280 * Retrieve a site object by its domain and path.
284 * @param string $domain Domain to check.
285 * @param string $path Path to check.
286 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
287 * @return object|bool Site object if successful. False when no site is found.
289 function get_site_by_path( $domain, $path, $segments = null ) {
292 $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
295 * Filter the number of path segments to consider when searching for a site.
299 * @param int|null $segments The number of path segments to consider. WordPress by default looks at
300 * one path segment following the network path. The function default of
301 * null only makes sense when you know the requested path should match a site.
302 * @param string $domain The requested domain.
303 * @param string $path The requested path, in full.
305 $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
307 if ( null !== $segments && count( $path_segments ) > $segments ) {
308 $path_segments = array_slice( $path_segments, 0, $segments );
311 while ( count( $path_segments ) ) {
312 $paths[] = '/' . implode( '/', $path_segments ) . '/';
313 array_pop( $path_segments );
319 * Determine a site by its domain and path.
321 * This allows one to short-circuit the default logic, perhaps by
322 * replacing it with a routine that is more optimal for your setup.
324 * Return null to avoid the short-circuit. Return false if no site
325 * can be found at the requested domain and path. Otherwise, return
330 * @param null|bool|object $site Site value to return by path.
331 * @param string $domain The requested domain.
332 * @param string $path The requested path, in full.
333 * @param int|null $segments The suggested number of paths to consult.
334 * Default null, meaning the entire path was to be consulted.
335 * @param array $paths The paths to search for, based on $path and $segments.
337 $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
338 if ( null !== $pre ) {
344 * get_blog_details(), caching, etc. Consider alternative optimization routes,
345 * perhaps as an opt-in for plugins, rather than using the pre_* filter.
346 * For example: The segments filter can expand or ignore paths.
347 * If persistent caching is enabled, we could query the DB for a path <> '/'
348 * then cache whether we can just always ignore paths.
351 // Either www or non-www is supported, not both. If a www domain is requested,
352 // query for both to provide the proper redirect.
353 $domains = array( $domain );
354 if ( 'www.' === substr( $domain, 0, 4 ) ) {
355 $domains[] = substr( $domain, 4 );
356 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
359 if ( count( $paths ) > 1 ) {
360 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
363 if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
364 $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" );
365 } elseif ( count( $domains ) > 1 ) {
366 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
367 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
368 $site = $wpdb->get_row( $sql );
369 } elseif ( count( $paths ) > 1 ) {
370 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
371 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
372 $site = $wpdb->get_row( $sql );
374 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
378 // @todo get_blog_details()
386 * Displays a failure message.
388 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
393 function ms_not_installed() {
394 global $wpdb, $domain, $path;
396 wp_load_translations_early();
398 $title = __( 'Error establishing a database connection' );
399 $msg = '<h1>' . $title . '</h1>';
400 if ( ! is_admin() ) {
403 $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
404 $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
405 $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
406 if ( ! $wpdb->get_var( $query ) ) {
407 $msg .= '<p>' . sprintf( __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted <code>%s</code>. You really should look at your database now.' ), $wpdb->site ) . '</p>';
409 $msg .= '<p>' . sprintf( __( '<strong>Could not find site <code>%1$s</code>.</strong> Searched for table <code>%2$s</code> in database <code>%3$s</code>. Is that right?' ), rtrim( $domain . $path, '/' ), $wpdb->blogs, DB_NAME ) . '</p>';
411 $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
412 $msg .= __( 'Read the <a target="_blank" href="http://codex.wordpress.org/Debugging_a_WordPress_Network">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' );
413 $msg .= ' ' . __( 'If you’re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
414 foreach ( $wpdb->tables('global') as $t => $table ) {
415 if ( 'sitecategories' == $t )
417 $msg .= '<li>' . $table . '</li>';
421 wp_die( $msg, $title );
425 * This deprecated function formerly set the site_name property of the $current_site object.
427 * This function simply returns the object, as before.
428 * The bootstrap takes care of setting site_name.
432 * @deprecated 3.9.0 Use get_current_site() instead.
434 * @param object $current_site
437 function get_current_site_name( $current_site ) {
438 _deprecated_function( __FUNCTION__, '3.9', 'get_current_site()' );
439 return $current_site;
443 * This deprecated function managed much of the site and network loading in multisite.
445 * The current bootstrap code is now responsible for parsing the site and network load as
446 * well as setting the global $current_site object.
454 function wpmu_current_site() {
455 global $current_site;
456 _deprecated_function( __FUNCTION__, '3.9' );
457 return $current_site;