~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-includes/ms-settings.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:50:12 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: barry.price@canonical.com-20160817045012-qfui81zhqnqv2ba9
Merge WP4.6 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 * @since 3.0.0
11
11
 */
12
12
 
 
13
/**
 
14
 * Objects representing the current network and current site.
 
15
 *
 
16
 * These may be populated through a custom `sunrise.php`. If not, then this
 
17
 * file will attempt to populate them based on the current request.
 
18
 *
 
19
 * @global WP_Network $current_site The current network.
 
20
 * @global object     $current_blog The current site.
 
21
 * @since 3.0.0
 
22
 */
 
23
global $current_site, $current_blog;
 
24
 
13
25
/** WP_Network class */
14
26
require_once( ABSPATH . WPINC . '/class-wp-network.php' );
15
27
 
29
41
/** Check for and define SUBDOMAIN_INSTALL and the deprecated VHOST constant. */
30
42
ms_subdomain_constants();
31
43
 
 
44
// This block will process a request if the current network or current site objects
 
45
// have not been populated in the global scope through something like `sunrise.php`.
32
46
if ( !isset( $current_site ) || !isset( $current_blog ) ) {
33
47
 
34
 
        // Given the domain and path, let's try to identify the network and site.
35
 
        // Usually, it's easier to query the site first, which declares its network.
36
 
        // In limited situations, though, we either can or must find the network first.
37
 
 
38
48
        $domain = strtolower( stripslashes( $_SERVER['HTTP_HOST'] ) );
39
49
        if ( substr( $domain, -3 ) == ':80' ) {
40
50
                $domain = substr( $domain, 0, -3 );
50
60
        }
51
61
        list( $path ) = explode( '?', $path );
52
62
 
53
 
        // If the network is defined in wp-config.php, we can simply use that.
54
 
        if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
55
 
                $current_site = new stdClass;
56
 
                $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
57
 
                $current_site->domain = DOMAIN_CURRENT_SITE;
58
 
                $current_site->path = PATH_CURRENT_SITE;
59
 
                if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
60
 
                        $current_site->blog_id = BLOG_ID_CURRENT_SITE;
61
 
                } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
62
 
                        $current_site->blog_id = BLOGID_CURRENT_SITE;
63
 
                }
64
 
 
65
 
                if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
66
 
                        $current_blog = get_site_by_path( $domain, $path );
67
 
                } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
68
 
                        // If the current network has a path and also matches the domain and path of the request,
69
 
                        // we need to look for a site using the first path segment following the network's path.
70
 
                        $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
71
 
                } else {
72
 
                        // Otherwise, use the first path segment (as usual).
73
 
                        $current_blog = get_site_by_path( $domain, $path, 1 );
74
 
                }
75
 
 
76
 
        } elseif ( ! is_subdomain_install() ) {
77
 
                /*
78
 
                 * A "subdomain" install can be re-interpreted to mean "can support any domain".
79
 
                 * If we're not dealing with one of these installs, then the important part is determining
80
 
                 * the network first, because we need the network's path to identify any sites.
81
 
                 */
82
 
                if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
83
 
                        // Are there even two networks installed?
84
 
                        $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
85
 
                        if ( 1 === $wpdb->num_rows ) {
86
 
                                $current_site = new WP_Network( $one_network );
87
 
                                wp_cache_add( 'current_network', $current_site, 'site-options' );
88
 
                        } elseif ( 0 === $wpdb->num_rows ) {
89
 
                                ms_not_installed( $domain, $path );
90
 
                        }
91
 
                }
92
 
                if ( empty( $current_site ) ) {
93
 
                        $current_site = WP_Network::get_by_path( $domain, $path, 1 );
94
 
                }
95
 
 
96
 
                if ( empty( $current_site ) ) {
97
 
                        /**
98
 
                         * Fires when a network cannot be found based on the requested domain and path.
99
 
                         *
100
 
                         * At the time of this action, the only recourse is to redirect somewhere
101
 
                         * and exit. If you want to declare a particular network, do so earlier.
102
 
                         *
103
 
                         * @since 4.4.0
104
 
                         *
105
 
                         * @param string $domain       The domain used to search for a network.
106
 
                         * @param string $path         The path used to search for a path.
107
 
                         */
108
 
                        do_action( 'ms_network_not_found', $domain, $path );
109
 
 
110
 
                        ms_not_installed( $domain, $path );
111
 
                } elseif ( $path === $current_site->path ) {
112
 
                        $current_blog = get_site_by_path( $domain, $path );
113
 
                } else {
114
 
                        // Search the network path + one more path segment (on top of the network path).
115
 
                        $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
116
 
                }
 
63
        $bootstrap_result = ms_load_current_site_and_network( $domain, $path, is_subdomain_install() );
 
64
 
 
65
        if ( true === $bootstrap_result ) {
 
66
                // `$current_blog` and `$current_site are now populated.
 
67
        } elseif ( false === $bootstrap_result ) {
 
68
                ms_not_installed( $domain, $path );
117
69
        } else {
118
 
                // Find the site by the domain and at most the first path segment.
119
 
                $current_blog = get_site_by_path( $domain, $path, 1 );
120
 
                if ( $current_blog ) {
121
 
                        $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
122
 
                } else {
123
 
                        // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
124
 
                        $current_site = WP_Network::get_by_path( $domain, $path, 1 );
125
 
                }
126
 
        }
127
 
 
128
 
        // The network declared by the site trumps any constants.
129
 
        if ( $current_blog && $current_blog->site_id != $current_site->id ) {
130
 
                $current_site = WP_Network::get_instance( $current_blog->site_id );
131
 
        }
132
 
 
133
 
        // No network has been found, bail.
134
 
        if ( empty( $current_site ) ) {
135
 
                /** This action is documented in wp-includes/ms-settings.php */
136
 
                do_action( 'ms_network_not_found', $domain, $path );
137
 
 
138
 
                ms_not_installed( $domain, $path );
139
 
        }
140
 
 
141
 
        // During activation of a new subdomain, the requested site does not yet exist.
142
 
        if ( empty( $current_blog ) && wp_installing() ) {
143
 
                $current_blog = new stdClass;
144
 
                $current_blog->blog_id = $blog_id = 1;
145
 
                $current_blog->public = 1;
146
 
        }
147
 
 
148
 
        // No site has been found, bail.
149
 
        if ( empty( $current_blog ) ) {
150
 
                // We're going to redirect to the network URL, with some possible modifications.
151
 
                $scheme = is_ssl() ? 'https' : 'http';
152
 
                $destination = "$scheme://{$current_site->domain}{$current_site->path}";
153
 
 
154
 
                /**
155
 
                 * Fires when a network can be determined but a site cannot.
156
 
                 *
157
 
                 * At the time of this action, the only recourse is to redirect somewhere
158
 
                 * and exit. If you want to declare a particular site, do so earlier.
159
 
                 *
160
 
                 * @since 3.9.0
161
 
                 *
162
 
                 * @param object $current_site The network that had been determined.
163
 
                 * @param string $domain       The domain used to search for a site.
164
 
                 * @param string $path         The path used to search for a site.
165
 
                 */
166
 
                do_action( 'ms_site_not_found', $current_site, $domain, $path );
167
 
 
168
 
                if ( is_subdomain_install() && ! defined( 'NOBLOGREDIRECT' ) ) {
169
 
                        // For a "subdomain" install, redirect to the signup form specifically.
170
 
                        $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
171
 
                } elseif ( is_subdomain_install() ) {
172
 
                        // For a "subdomain" install, the NOBLOGREDIRECT constant
173
 
                        // can be used to avoid a redirect to the signup form.
174
 
                        // Using the ms_site_not_found action is preferred to the constant.
175
 
                        if ( '%siteurl%' !== NOBLOGREDIRECT ) {
176
 
                                $destination = NOBLOGREDIRECT;
177
 
                        }
178
 
                } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
179
 
                        /*
180
 
                         * If the domain we were searching for matches the network's domain,
181
 
                         * it's no use redirecting back to ourselves -- it'll cause a loop.
182
 
                         * As we couldn't find a site, we're simply not installed.
183
 
                         */
184
 
                        ms_not_installed( $domain, $path );
185
 
                }
186
 
 
187
 
                header( 'Location: ' . $destination );
 
70
                header( 'Location: ' . $bootstrap_result );
188
71
                exit;
189
72
        }
190
 
 
191
 
        // Figure out the current network's main site.
192
 
        if ( empty( $current_site->blog_id ) ) {
193
 
                if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
194
 
                        $current_site->blog_id = $current_blog->blog_id;
195
 
                } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
196
 
                        $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
197
 
                                $current_site->domain, $current_site->path ) );
198
 
                        wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
199
 
                }
200
 
        }
 
73
        unset( $bootstrap_result );
201
74
 
202
75
        $blog_id = $current_blog->blog_id;
203
76
        $public  = $current_blog->public;
231
104
 
232
105
// Define upload directory constants
233
106
ms_upload_constants();
 
107
 
 
108
/**
 
109
 * Fires after the current site and network have been detected and loaded
 
110
 * in multisite's bootstrap.
 
111
 *
 
112
 * @since 4.6.0
 
113
 */
 
114
do_action( 'ms_loaded' );