]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-site.php
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-site.php
1 <?php
2 /**
3  * Site API: WP_Site class
4  *
5  * @package WordPress
6  * @subpackage Multisite
7  * @since 4.5.0
8  */
9
10 /**
11  * Core class used for interacting with a multisite site.
12  *
13  * This class is used during load to populate the `$current_blog` global and
14  * setup the current site.
15  *
16  * @since 4.5.0
17  *
18  * @property int    $id
19  * @property int    $network_id
20  * @property string $blogname
21  * @property string $siteurl
22  * @property int    $post_count
23  * @property string $home
24  */
25 final class WP_Site {
26
27         /**
28          * Site ID.
29          *
30          * A numeric string, for compatibility reasons.
31          *
32          * @since 4.5.0
33          * @access public
34          * @var string
35          */
36         public $blog_id;
37
38         /**
39          * Domain of the site.
40          *
41          * @since 4.5.0
42          * @access public
43          * @var string
44          */
45         public $domain = '';
46
47         /**
48          * Path of the site.
49          *
50          * @since 4.5.0
51          * @access public
52          * @var string
53          */
54         public $path = '';
55
56         /**
57          * The ID of the site's parent network.
58          *
59          * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
60          * its network.
61          *
62          * A numeric string, for compatibility reasons.
63          *
64          * @since 4.5.0
65          * @access public
66          * @var string
67          */
68         public $site_id = '0';
69
70         /**
71          * The date on which the site was created or registered.
72          *
73          * @since 4.5.0
74          * @access public
75          * @var string Date in MySQL's datetime format.
76          */
77         public $registered = '0000-00-00 00:00:00';
78
79         /**
80          * The date and time on which site settings were last updated.
81          *
82          * @since 4.5.0
83          * @access public
84          * @var string Date in MySQL's datetime format.
85          */
86         public $last_updated = '0000-00-00 00:00:00';
87
88         /**
89          * Whether the site should be treated as public.
90          *
91          * A numeric string, for compatibility reasons.
92          *
93          * @since 4.5.0
94          * @access public
95          * @var string
96          */
97         public $public = '1';
98
99         /**
100          * Whether the site should be treated as archived.
101          *
102          * A numeric string, for compatibility reasons.
103          *
104          * @since 4.5.0
105          * @access public
106          * @var string
107          */
108         public $archived = '0';
109
110         /**
111          * Whether the site should be treated as mature.
112          *
113          * Handling for this does not exist throughout WordPress core, but custom
114          * implementations exist that require the property to be present.
115          *
116          * A numeric string, for compatibility reasons.
117          *
118          * @since 4.5.0
119          * @access public
120          * @var string
121          */
122         public $mature = '0';
123
124         /**
125          * Whether the site should be treated as spam.
126          *
127          * A numeric string, for compatibility reasons.
128          *
129          * @since 4.5.0
130          * @access public
131          * @var string
132          */
133         public $spam = '0';
134
135         /**
136          * Whether the site should be treated as deleted.
137          *
138          * A numeric string, for compatibility reasons.
139          *
140          * @since 4.5.0
141          * @access public
142          * @var string
143          */
144         public $deleted = '0';
145
146         /**
147          * The language pack associated with this site.
148          *
149          * A numeric string, for compatibility reasons.
150          *
151          * @since 4.5.0
152          * @access public
153          * @var string
154          */
155         public $lang_id = '0';
156
157         /**
158          * Retrieves a site from the database by its ID.
159          *
160          * @static
161          * @since 4.5.0
162          * @access public
163          *
164          * @global wpdb $wpdb WordPress database abstraction object.
165          *
166          * @param int $site_id The ID of the site to retrieve.
167          * @return WP_Site|false The site's object if found. False if not.
168          */
169         public static function get_instance( $site_id ) {
170                 global $wpdb;
171
172                 $site_id = (int) $site_id;
173                 if ( ! $site_id ) {
174                         return false;
175                 }
176
177                 $_site = wp_cache_get( $site_id, 'sites' );
178
179                 if ( ! $_site ) {
180                         $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
181
182                         if ( empty( $_site ) || is_wp_error( $_site ) ) {
183                                 return false;
184                         }
185
186                         wp_cache_add( $site_id, $_site, 'sites' );
187                 }
188
189                 return new WP_Site( $_site );
190         }
191
192         /**
193          * Creates a new WP_Site object.
194          *
195          * Will populate object properties from the object provided and assign other
196          * default properties based on that information.
197          *
198          * @since 4.5.0
199          * @access public
200          *
201          * @param WP_Site|object $site A site object.
202          */
203         public function __construct( $site ) {
204                 foreach( get_object_vars( $site ) as $key => $value ) {
205                         $this->$key = $value;
206                 }
207         }
208
209         /**
210          * Converts an object to array.
211          *
212          * @since 4.6.0
213          * @access public
214          *
215          * @return array Object as array.
216          */
217         public function to_array() {
218                 return get_object_vars( $this );
219         }
220
221         /**
222          * Getter.
223          *
224          * Allows current multisite naming conventions when getting properties.
225          * Allows access to extended site properties.
226          *
227          * @since 4.6.0
228          * @access public
229          *
230          * @param string $key Property to get.
231          * @return mixed Value of the property. Null if not available.
232          */
233         public function __get( $key ) {
234                 switch ( $key ) {
235                         case 'id':
236                                 return (int) $this->blog_id;
237                         case 'network_id':
238                                 return (int) $this->site_id;
239                         case 'blogname':
240                         case 'siteurl':
241                         case 'post_count':
242                         case 'home':
243                                 if ( ! did_action( 'ms_loaded' ) ) {
244                                         return null;
245                                 }
246                                 $details = $this->get_details();
247                                 return $details->$key;
248                 }
249
250                 return null;
251         }
252
253         /**
254          * Isset-er.
255          *
256          * Allows current multisite naming conventions when checking for properties.
257          * Checks for extended site properties.
258          *
259          * @since 4.6.0
260          * @access public
261          *
262          * @param string $key Property to check if set.
263          * @return bool Whether the property is set.
264          */
265         public function __isset( $key ) {
266                 switch ( $key ) {
267                         case 'id':
268                         case 'network_id':
269                                 return true;
270                         case 'blogname':
271                         case 'siteurl':
272                         case 'post_count':
273                         case 'home':
274                                 if ( ! did_action( 'ms_loaded' ) ) {
275                                         return false;
276                                 }
277                                 return true;
278                 }
279
280                 return false;
281         }
282
283         /**
284          * Setter.
285          *
286          * Allows current multisite naming conventions while setting properties.
287          *
288          * @since 4.6.0
289          * @access public
290          *
291          * @param string $key   Property to set.
292          * @param mixed  $value Value to assign to the property.
293          */
294         public function __set( $key, $value ) {
295                 switch ( $key ) {
296                         case 'id':
297                                 $this->blog_id = (string) $value;
298                                 break;
299                         case 'network_id':
300                                 $this->site_id = (string) $value;
301                                 break;
302                         default:
303                                 $this->$key = $value;
304                 }
305         }
306
307         /**
308          * Retrieves the details for this site.
309          *
310          * This method is used internally to lazy-load the extended properties of a site.
311          *
312          * @since 4.6.0
313          * @access private
314          *
315          * @see WP_Site::__get()
316          *
317          * @return stdClass A raw site object with all details included.
318          */
319         private function get_details() {
320                 $details = wp_cache_get( $this->blog_id, 'site-details' );
321
322                 if ( false === $details ) {
323
324                         switch_to_blog( $this->blog_id );
325                         // Create a raw copy of the object for backwards compatibility with the filter below.
326                         $details = new stdClass();
327                         foreach ( get_object_vars( $this ) as $key => $value ) {
328                                 $details->$key = $value;
329                         }
330                         $details->blogname   = get_option( 'blogname' );
331                         $details->siteurl    = get_option( 'siteurl' );
332                         $details->post_count = get_option( 'post_count' );
333                         $details->home       = get_option( 'home' );
334                         restore_current_blog();
335
336                         $cache_details = true;
337                         foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) {
338                                 if ( false === $details->$field ) {
339                                         $cache_details = false;
340                                         break;
341                                 }
342                         }
343
344                         if ( $cache_details ) {
345                                 wp_cache_set( $this->blog_id, $details, 'site-details' );
346                         }
347                 }
348
349                 /** This filter is documented in wp-includes/ms-blogs.php */
350                 $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
351
352                 /**
353                  * Filters a site's extended properties.
354                  *
355                  * @since 4.6.0
356                  *
357                  * @param stdClass $details The site details.
358                  */
359                 $details = apply_filters( 'site_details', $details );
360
361                 return $details;
362         }
363 }