]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-network.php
WordPress 4.4.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-network.php
1 <?php
2 /**
3  * Network API: WP_Network class
4  *
5  * @package WordPress
6  * @subpackage Multisite
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used for interacting with a multisite network.
12  *
13  * This class is used during load to populate the `$current_site` global and
14  * setup the current network.
15  *
16  * This class is most useful in WordPress multi-network installations where the
17  * ability to interact with any network of sites is required.
18  *
19  * @since 4.4.0
20  */
21 class WP_Network {
22
23         /**
24          * Network ID.
25          *
26          * @since 4.4.0
27          * @access public
28          * @var int
29          */
30         public $id;
31
32         /**
33          * Domain of the network.
34          *
35          * @since 4.4.0
36          * @access public
37          * @var string
38          */
39         public $domain = '';
40
41         /**
42          * Path of the network.
43          *
44          * @since 4.4.0
45          * @access public
46          * @var string
47          */
48         public $path = '';
49
50         /**
51          * The ID of the network's main site.
52          *
53          * Named "blog" vs. "site" for legacy reasons. A main site is mapped to
54          * the network when the network is created.
55          *
56          * @since 4.4.0
57          * @access public
58          * @var int
59          */
60         public $blog_id = 0;
61
62         /**
63          * Domain used to set cookies for this network.
64          *
65          * @since 4.4.0
66          * @access public
67          * @var int
68          */
69         public $cookie_domain = '';
70
71         /**
72          * Name of this network.
73          *
74          * Named "site" vs. "network" for legacy reasons.
75          *
76          * @since 4.4.0
77          * @access public
78          * @var string
79          */
80         public $site_name = '';
81
82         /**
83          * Retrieve a network from the database by its ID.
84          *
85          * @since 4.4.0
86          * @access public
87          *
88          * @global wpdb $wpdb WordPress database abstraction object.
89          *
90          * @param int $network_id The ID of the network to retrieve.
91          * @return WP_Network|bool The network's object if found. False if not.
92          */
93         public static function get_instance( $network_id ) {
94                 global $wpdb;
95
96                 $network_id = (int) $network_id;
97                 if ( ! $network_id ) {
98                         return false;
99                 }
100
101                 $_network = wp_cache_get( $network_id, 'networks' );
102
103                 if ( ! $_network ) {
104                         $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
105
106                         if ( empty( $_network ) || is_wp_error( $_network ) ) {
107                                 return false;
108                         }
109
110                         wp_cache_add( $network_id, $_network, 'networks' );
111                 }
112
113                 return new WP_Network( $_network );
114         }
115
116         /**
117          * Create a new WP_Network object.
118          *
119          * Will populate object properties from the object provided and assign other
120          * default properties based on that information.
121          *
122          * @since 4.4.0
123          * @access public
124          *
125          * @param WP_Network|object $network A network object.
126          */
127         public function __construct( $network ) {
128                 foreach( get_object_vars( $network ) as $key => $value ) {
129                         $this->$key = $value;
130                 }
131
132                 $this->_set_site_name();
133                 $this->_set_cookie_domain();
134         }
135
136         /**
137          * Set the site name assigned to the network if one has not been populated.
138          *
139          * @since 4.4.0
140          * @access private
141          */
142         private function _set_site_name() {
143                 if ( ! empty( $this->site_name ) ) {
144                         return;
145                 }
146
147                 $default = ucfirst( $this->domain );
148                 $this->site_name = get_network_option( $this->id, 'site_name', $default );
149         }
150
151         /**
152          * Set the cookie domain based on the network domain if one has
153          * not been populated.
154          *
155          * @todo What if the domain of the network doesn't match the current site?
156          *
157          * @since 4.4.0
158          * @access private
159          */
160         private function _set_cookie_domain() {
161                 if ( ! empty( $this->cookie_domain ) ) {
162                         return;
163                 }
164
165                 $this->cookie_domain = $this->domain;
166                 if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
167                         $this->cookie_domain = substr( $this->cookie_domain, 4 );
168                 }
169         }
170
171         /**
172          * Retrieve the closest matching network for a domain and path.
173          *
174          * This will not necessarily return an exact match for a domain and path. Instead, it
175          * breaks the domain and path into pieces that are then used to match the closest
176          * possibility from a query.
177          *
178          * The intent of this method is to match a network during bootstrap for a
179          * requested site address.
180          *
181          * @since 4.4.0
182          * @access public
183          * @static
184          *
185          * @param string   $domain   Domain to check.
186          * @param string   $path     Path to check.
187          * @param int|null $segments Path segments to use. Defaults to null, or the full path.
188          * @return WP_Network|bool Network object if successful. False when no network is found.
189          */
190         public static function get_by_path( $domain = '', $path = '', $segments = null ) {
191                 global $wpdb;
192
193                 $domains = array( $domain );
194                 $pieces  = explode( '.', $domain );
195
196                 /*
197                  * It's possible one domain to search is 'com', but it might as well
198                  * be 'localhost' or some other locally mapped domain.
199                  */
200                 while ( array_shift( $pieces ) ) {
201                         if ( ! empty( $pieces ) ) {
202                                 $domains[] = implode( '.', $pieces );
203                         }
204                 }
205
206                 /*
207                  * If we've gotten to this function during normal execution, there is
208                  * more than one network installed. At this point, who knows how many
209                  * we have. Attempt to optimize for the situation where networks are
210                  * only domains, thus meaning paths never need to be considered.
211                  *
212                  * This is a very basic optimization; anything further could have
213                  * drawbacks depending on the setup, so this is best done per-install.
214                  */
215                 $using_paths = true;
216                 if ( wp_using_ext_object_cache() ) {
217                         $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
218                         if ( false === $using_paths ) {
219                                 $using_paths = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->site} WHERE path <> '/' LIMIT 1" );
220                                 wp_cache_add( 'networks_have_paths', $using_paths, 'site-options'  );
221                         }
222                 }
223
224                 $paths = array();
225                 if ( $using_paths ) {
226                         $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
227
228                         /**
229                          * Filter the number of path segments to consider when searching for a site.
230                          *
231                          * @since 3.9.0
232                          *
233                          * @param int|null $segments The number of path segments to consider. WordPress by default looks at
234                          *                           one path segment. The function default of null only makes sense when you
235                          *                           know the requested path should match a network.
236                          * @param string   $domain   The requested domain.
237                          * @param string   $path     The requested path, in full.
238                          */
239                         $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
240
241                         if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
242                                 $path_segments = array_slice( $path_segments, 0, $segments );
243                         }
244
245                         while ( count( $path_segments ) ) {
246                                 $paths[] = '/' . implode( '/', $path_segments ) . '/';
247                                 array_pop( $path_segments );
248                         }
249
250                         $paths[] = '/';
251                 }
252
253                 /**
254                  * Determine a network by its domain and path.
255                  *
256                  * This allows one to short-circuit the default logic, perhaps by
257                  * replacing it with a routine that is more optimal for your setup.
258                  *
259                  * Return null to avoid the short-circuit. Return false if no network
260                  * can be found at the requested domain and path. Otherwise, return
261                  * an object from wp_get_network().
262                  *
263                  * @since 3.9.0
264                  *
265                  * @param null|bool|object $network  Network value to return by path.
266                  * @param string           $domain   The requested domain.
267                  * @param string           $path     The requested path, in full.
268                  * @param int|null         $segments The suggested number of paths to consult.
269                  *                                   Default null, meaning the entire path was to be consulted.
270                  * @param array            $paths    The paths to search for, based on $path and $segments.
271                  */
272                 $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
273                 if ( null !== $pre ) {
274                         return $pre;
275                 }
276
277                 // @todo Consider additional optimization routes, perhaps as an opt-in for plugins.
278                 // We already have paths covered. What about how far domains should be drilled down (including www)?
279
280                 $search_domains = "'" . implode( "', '", $wpdb->_escape( $domains ) ) . "'";
281
282                 if ( ! $using_paths ) {
283                         $network = $wpdb->get_row( "
284                                 SELECT * FROM {$wpdb->site}
285                                 WHERE domain IN ({$search_domains})
286                                 ORDER BY CHAR_LENGTH(domain)
287                                 DESC LIMIT 1
288                         " );
289
290                         if ( ! empty( $network ) && ! is_wp_error( $network ) ) {
291                                 return new WP_Network( $network );
292                         }
293
294                         return false;
295
296                 } else {
297                         $search_paths = "'" . implode( "', '", $wpdb->_escape( $paths ) ) . "'";
298                         $networks = $wpdb->get_results( "
299                                 SELECT * FROM {$wpdb->site}
300                                 WHERE domain IN ({$search_domains})
301                                 AND path IN ({$search_paths})
302                                 ORDER BY CHAR_LENGTH(domain) DESC, CHAR_LENGTH(path) DESC
303                         " );
304                 }
305
306                 /*
307                  * Domains are sorted by length of domain, then by length of path.
308                  * The domain must match for the path to be considered. Otherwise,
309                  * a network with the path of / will suffice.
310                  */
311                 $found = false;
312                 foreach ( $networks as $network ) {
313                         if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
314                                 if ( in_array( $network->path, $paths, true ) ) {
315                                         $found = true;
316                                         break;
317                                 }
318                         }
319                         if ( $network->path === '/' ) {
320                                 $found = true;
321                                 break;
322                         }
323                 }
324
325                 if ( true === $found ) {
326                         return new WP_Network( $network );
327                 }
328
329                 return false;
330         }
331 }