]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/ms-load.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-includes / ms-load.php
1 <?php
2 /**
3  * These functions are needed to load Multisite.
4  *
5  * @since 3.0.0
6  *
7  * @package WordPress
8  * @subpackage Multisite
9  */
10
11 /**
12  * Whether a subdomain configuration is enabled.
13  *
14  * @since 3.0.0
15  *
16  * @return bool True if subdomain configuration is enabled, false otherwise.
17  */
18 function is_subdomain_install() {
19         if ( defined('SUBDOMAIN_INSTALL') )
20                 return SUBDOMAIN_INSTALL;
21
22         return ( defined( 'VHOST' ) && VHOST == 'yes' );
23 }
24
25 /**
26  * Returns array of network plugin files to be included in global scope.
27  *
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`.
30  *
31  * @access private
32  * @since 3.1.0
33  *
34  * @return array Files to include.
35  */
36 function wp_get_active_network_plugins() {
37         $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
38         if ( empty( $active_plugins ) )
39                 return array();
40
41         $plugins = array();
42         $active_plugins = array_keys( $active_plugins );
43         sort( $active_plugins );
44
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
49                         )
50                 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
51         }
52         return $plugins;
53 }
54
55 /**
56  * Checks status of current blog.
57  *
58  * Checks if the blog is deleted, inactive, archived, or spammed.
59  *
60  * Dies with a default message if the blog does not pass the check.
61  *
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.
65  *
66  * @since 3.0.0
67  *
68  * @return true|string Returns true on success, or drop-in file to include.
69  */
70 function ms_site_check() {
71         $blog = get_blog_details();
72
73         /**
74          * Filter checking the status of the current blog.
75          *
76          * @since 3.0.0
77          *
78          * @param bool null Whether to skip the blog status check. Default null.
79         */
80         $check = apply_filters( 'ms_site_check', null );
81         if ( null !== $check )
82                 return true;
83
84         // Allow super admins to see blocked sites
85         if ( is_super_admin() )
86                 return true;
87
88         if ( '1' == $blog->deleted ) {
89                 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
90                         return WP_CONTENT_DIR . '/blog-deleted.php';
91                 else
92                         wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
93         }
94
95         if ( '2' == $blog->deleted ) {
96                 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
97                         return WP_CONTENT_DIR . '/blog-inactive.php';
98                 } else {
99                         $admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_current_site()->domain ) );
100                         wp_die(
101                                 /* translators: %s: admin email link */
102                                 sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
103                                         sprintf( '<a href="mailto:%s">%s</a>', $admin_email )
104                                 )
105                         );
106                 }
107         }
108
109         if ( $blog->archived == '1' || $blog->spam == '1' ) {
110                 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
111                         return WP_CONTENT_DIR . '/blog-suspended.php';
112                 else
113                         wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
114         }
115
116         return true;
117 }
118
119 /**
120  * Retrieve the closest matching network for a domain and path.
121  *
122  * @since 3.9.0
123  * @since 4.4.0 Converted to a wrapper for WP_Network::get_by_path()
124  *
125  * @param string   $domain   Domain to check.
126  * @param string   $path     Path to check.
127  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
128  * @return WP_Network|false Network object if successful. False when no network is found.
129  */
130 function get_network_by_path( $domain, $path, $segments = null ) {
131         return WP_Network::get_by_path( $domain, $path, $segments );
132 }
133
134 /**
135  * Retrieve an object containing information about the requested network.
136  *
137  * @since 3.9.0
138  * @since 4.4.0 Converted to leverage WP_Network
139  *
140  * @param object|int $network The network's database row or ID.
141  * @return WP_Network|false Object containing network information if found, false if not.
142  */
143 function wp_get_network( $network ) {
144         if ( ! is_object( $network ) ) {
145                 $network = WP_Network::get_instance( $network );
146         } else {
147                 $network = new WP_Network( $network );
148         }
149
150         return $network;
151 }
152
153 /**
154  * Retrieve a site object by its domain and path.
155  *
156  * @since 3.9.0
157  *
158  * @global wpdb $wpdb WordPress database abstraction object.
159  *
160  * @param string   $domain   Domain to check.
161  * @param string   $path     Path to check.
162  * @param int|null $segments Path segments to use. Defaults to null, or the full path.
163  * @return object|false Site object if successful. False when no site is found.
164  */
165 function get_site_by_path( $domain, $path, $segments = null ) {
166         global $wpdb;
167
168         $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
169
170         /**
171          * Filter the number of path segments to consider when searching for a site.
172          *
173          * @since 3.9.0
174          *
175          * @param int|null $segments The number of path segments to consider. WordPress by default looks at
176          *                           one path segment following the network path. The function default of
177          *                           null only makes sense when you know the requested path should match a site.
178          * @param string   $domain   The requested domain.
179          * @param string   $path     The requested path, in full.
180          */
181         $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
182
183         if ( null !== $segments && count( $path_segments ) > $segments ) {
184                 $path_segments = array_slice( $path_segments, 0, $segments );
185         }
186
187         $paths = array();
188
189         while ( count( $path_segments ) ) {
190                 $paths[] = '/' . implode( '/', $path_segments ) . '/';
191                 array_pop( $path_segments );
192         }
193
194         $paths[] = '/';
195
196         /**
197          * Determine a site by its domain and path.
198          *
199          * This allows one to short-circuit the default logic, perhaps by
200          * replacing it with a routine that is more optimal for your setup.
201          *
202          * Return null to avoid the short-circuit. Return false if no site
203          * can be found at the requested domain and path. Otherwise, return
204          * a site object.
205          *
206          * @since 3.9.0
207          *
208          * @param null|bool|object $site     Site value to return by path.
209          * @param string           $domain   The requested domain.
210          * @param string           $path     The requested path, in full.
211          * @param int|null         $segments The suggested number of paths to consult.
212          *                                   Default null, meaning the entire path was to be consulted.
213          * @param array            $paths    The paths to search for, based on $path and $segments.
214          */
215         $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
216         if ( null !== $pre ) {
217                 return $pre;
218         }
219
220         /*
221          * @todo
222          * get_blog_details(), caching, etc. Consider alternative optimization routes,
223          * perhaps as an opt-in for plugins, rather than using the pre_* filter.
224          * For example: The segments filter can expand or ignore paths.
225          * If persistent caching is enabled, we could query the DB for a path <> '/'
226          * then cache whether we can just always ignore paths.
227          */
228
229         // Either www or non-www is supported, not both. If a www domain is requested,
230         // query for both to provide the proper redirect.
231         $domains = array( $domain );
232         if ( 'www.' === substr( $domain, 0, 4 ) ) {
233                 $domains[] = substr( $domain, 4 );
234                 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
235         }
236
237         if ( count( $paths ) > 1 ) {
238                 $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
239         }
240
241         if ( count( $domains ) > 1 && count( $paths ) > 1 ) {
242                 $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" );
243         } elseif ( count( $domains ) > 1 ) {
244                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE path = %s", $paths[0] );
245                 $sql .= " AND domain IN ($search_domains) ORDER BY CHAR_LENGTH(domain) DESC LIMIT 1";
246                 $site = $wpdb->get_row( $sql );
247         } elseif ( count( $paths ) > 1 ) {
248                 $sql = $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $domains[0] );
249                 $sql .= " AND path IN ($search_paths) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1";
250                 $site = $wpdb->get_row( $sql );
251         } else {
252                 $site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $domains[0], $paths[0] ) );
253         }
254
255         if ( $site ) {
256                 // @todo get_blog_details()
257                 return $site;
258         }
259
260         return false;
261 }
262
263 /**
264  * Displays a failure message.
265  *
266  * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
267  *
268  * @access private
269  * @since 3.0.0
270  * @since 4.4.0 The `$domain` and `$path` parameters were added.
271  *
272  * @global wpdb $wpdb WordPress database abstraction object.
273  *
274  * @param string $domain The requested domain for the error to reference.
275  * @param string $path   The requested path for the error to reference.
276  */
277 function ms_not_installed( $domain, $path ) {
278         global $wpdb;
279
280         if ( ! is_admin() ) {
281                 dead_db();
282         }
283
284         wp_load_translations_early();
285
286         $title = __( 'Error establishing a database connection' );
287
288         $msg  = '<h1>' . $title . '</h1>';
289         $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
290         $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
291         $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
292         if ( ! $wpdb->get_var( $query ) ) {
293                 $msg .= '<p>' . sprintf(
294                         /* translators: %s: table name */
295                         __( '<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.' ),
296                         '<code>' . $wpdb->site . '</code>'
297                 ) . '</p>';
298         } else {
299                 $msg .= '<p>' . sprintf(
300                         /* translators: 1: site url, 2: table name, 3: database name */
301                         __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
302                         '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
303                         '<code>' . $wpdb->blogs . '</code>',
304                         '<code>' . DB_NAME . '</code>'
305                 ) . '</p>';
306         }
307         $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
308         /* translators: %s: Codex URL */
309         $msg .= sprintf( __( 'Read the <a href="%s" target="_blank">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' ),
310                 __( 'https://codex.wordpress.org/Debugging_a_WordPress_Network' )
311         );
312         $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
313         foreach ( $wpdb->tables('global') as $t => $table ) {
314                 if ( 'sitecategories' == $t )
315                         continue;
316                 $msg .= '<li>' . $table . '</li>';
317         }
318         $msg .= '</ul>';
319
320         wp_die( $msg, $title, array( 'response' => 500 ) );
321 }
322
323 /**
324  * This deprecated function formerly set the site_name property of the $current_site object.
325  *
326  * This function simply returns the object, as before.
327  * The bootstrap takes care of setting site_name.
328  *
329  * @access private
330  * @since 3.0.0
331  * @deprecated 3.9.0 Use get_current_site() instead.
332  *
333  * @param object $current_site
334  * @return object
335  */
336 function get_current_site_name( $current_site ) {
337         _deprecated_function( __FUNCTION__, '3.9', 'get_current_site()' );
338         return $current_site;
339 }
340
341 /**
342  * This deprecated function managed much of the site and network loading in multisite.
343  *
344  * The current bootstrap code is now responsible for parsing the site and network load as
345  * well as setting the global $current_site object.
346  *
347  * @access private
348  * @since 3.0.0
349  * @deprecated 3.9.0
350  *
351  * @global object $current_site
352  *
353  * @return object
354  */
355 function wpmu_current_site() {
356         global $current_site;
357         _deprecated_function( __FUNCTION__, '3.9' );
358         return $current_site;
359 }