3 * Site API: WP_Site class
6 * @subpackage Multisite
11 * Core class used for interacting with a multisite site.
13 * This class is used during load to populate the `$current_blog` global and
14 * setup the current site.
19 * @property int $network_id
20 * @property string $blogname
21 * @property string $siteurl
22 * @property int $post_count
23 * @property string $home
30 * A numeric string, for compatibility reasons.
57 * The ID of the site's parent network.
59 * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
62 * A numeric string, for compatibility reasons.
68 public $site_id = '0';
71 * The date on which the site was created or registered.
75 * @var string Date in MySQL's datetime format.
77 public $registered = '0000-00-00 00:00:00';
80 * The date and time on which site settings were last updated.
84 * @var string Date in MySQL's datetime format.
86 public $last_updated = '0000-00-00 00:00:00';
89 * Whether the site should be treated as public.
91 * A numeric string, for compatibility reasons.
100 * Whether the site should be treated as archived.
102 * A numeric string, for compatibility reasons.
108 public $archived = '0';
111 * Whether the site should be treated as mature.
113 * Handling for this does not exist throughout WordPress core, but custom
114 * implementations exist that require the property to be present.
116 * A numeric string, for compatibility reasons.
122 public $mature = '0';
125 * Whether the site should be treated as spam.
127 * A numeric string, for compatibility reasons.
136 * Whether the site should be treated as deleted.
138 * A numeric string, for compatibility reasons.
144 public $deleted = '0';
147 * The language pack associated with this site.
149 * A numeric string, for compatibility reasons.
155 public $lang_id = '0';
158 * Retrieves a site from the database by its ID.
164 * @global wpdb $wpdb WordPress database abstraction object.
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.
169 public static function get_instance( $site_id ) {
172 $site_id = (int) $site_id;
177 $_site = wp_cache_get( $site_id, 'sites' );
180 $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
182 if ( empty( $_site ) || is_wp_error( $_site ) ) {
186 wp_cache_add( $site_id, $_site, 'sites' );
189 return new WP_Site( $_site );
193 * Creates a new WP_Site object.
195 * Will populate object properties from the object provided and assign other
196 * default properties based on that information.
201 * @param WP_Site|object $site A site object.
203 public function __construct( $site ) {
204 foreach( get_object_vars( $site ) as $key => $value ) {
205 $this->$key = $value;
210 * Converts an object to array.
215 * @return array Object as array.
217 public function to_array() {
218 return get_object_vars( $this );
224 * Allows current multisite naming conventions when getting properties.
225 * Allows access to extended site properties.
230 * @param string $key Property to get.
231 * @return mixed Value of the property. Null if not available.
233 public function __get( $key ) {
236 return (int) $this->blog_id;
238 return (int) $this->site_id;
243 if ( ! did_action( 'ms_loaded' ) ) {
246 $details = $this->get_details();
247 return $details->$key;
256 * Allows current multisite naming conventions when checking for properties.
257 * Checks for extended site properties.
262 * @param string $key Property to check if set.
263 * @return bool Whether the property is set.
265 public function __isset( $key ) {
274 if ( ! did_action( 'ms_loaded' ) ) {
286 * Allows current multisite naming conventions while setting properties.
291 * @param string $key Property to set.
292 * @param mixed $value Value to assign to the property.
294 public function __set( $key, $value ) {
297 $this->blog_id = (string) $value;
300 $this->site_id = (string) $value;
303 $this->$key = $value;
308 * Retrieves the details for this site.
310 * This method is used internally to lazy-load the extended properties of a site.
315 * @see WP_Site::__get()
317 * @return stdClass A raw site object with all details included.
319 private function get_details() {
320 $details = wp_cache_get( $this->blog_id, 'site-details' );
322 if ( false === $details ) {
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;
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();
336 $cache_details = true;
337 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) {
338 if ( false === $details->$field ) {
339 $cache_details = false;
344 if ( $cache_details ) {
345 wp_cache_set( $this->blog_id, $details, 'site-details' );
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' );
353 * Filters a site's extended properties.
357 * @param stdClass $details The site details.
359 $details = apply_filters( 'site_details', $details );