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
26 * A numeric string, for compatibility reasons.
53 * The ID of the site's parent network.
55 * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
58 * A numeric string, for compatibility reasons.
64 public $site_id = '0';
67 * The date on which the site was created or registered.
71 * @var string Date in MySQL's datetime format.
73 public $registered = '0000-00-00 00:00:00';
76 * The date and time on which site settings were last updated.
80 * @var string Date in MySQL's datetime format.
82 public $last_updated = '0000-00-00 00:00:00';
85 * Whether the site should be treated as public.
87 * A numeric string, for compatibility reasons.
96 * Whether the site should be treated as archived.
98 * A numeric string, for compatibility reasons.
104 public $archived = '0';
107 * Whether the site should be treated as mature.
109 * Handling for this does not exist throughout WordPress core, but custom
110 * implementations exist that require the property to be present.
112 * A numeric string, for compatibility reasons.
118 public $mature = '0';
121 * Whether the site should be treated as spam.
123 * A numeric string, for compatibility reasons.
132 * Whether the site should be treated as deleted.
134 * A numeric string, for compatibility reasons.
140 public $deleted = '0';
143 * The language pack associated with this site.
145 * A numeric string, for compatibility reasons.
151 public $lang_id = '0';
154 * Retrieves a site from the database by its ID.
160 * @global wpdb $wpdb WordPress database abstraction object.
162 * @param int $site_id The ID of the site to retrieve.
163 * @return WP_Site|false The site's object if found. False if not.
165 public static function get_instance( $site_id ) {
168 $site_id = (int) $site_id;
173 $_site = wp_cache_get( $site_id, 'sites' );
176 $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
178 if ( empty( $_site ) || is_wp_error( $_site ) ) {
182 wp_cache_add( $site_id, $_site, 'sites' );
185 return new WP_Site( $_site );
189 * Creates a new WP_Site object.
191 * Will populate object properties from the object provided and assign other
192 * default properties based on that information.
197 * @param WP_Site|object $site A site object.
199 public function __construct( $site ) {
200 foreach( get_object_vars( $site ) as $key => $value ) {
201 $this->$key = $value;
206 * Converts an object to array.
211 * @return array Object as array.
213 public function to_array() {
214 return get_object_vars( $this );
220 * Allows current multisite naming conventions when getting properties.
221 * Allows access to extended site properties.
226 * @param string $key Property to get.
227 * @return mixed Value of the property. Null if not available.
229 public function __get( $key ) {
232 return (int) $this->blog_id;
234 return (int) $this->site_id;
239 if ( ! did_action( 'ms_loaded' ) ) {
242 $details = $this->get_details();
243 return $details->$key;
252 * Allows current multisite naming conventions when checking for properties.
253 * Checks for extended site properties.
258 * @param string $key Property to check if set.
259 * @return bool Whether the property is set.
261 public function __isset( $key ) {
270 if ( ! did_action( 'ms_loaded' ) ) {
282 * Allows current multisite naming conventions while setting properties.
287 * @param string $key Property to set.
288 * @param mixed $value Value to assign to the property.
290 public function __set( $key, $value ) {
293 $this->blog_id = (string) $value;
296 $this->site_id = (string) $value;
299 $this->$key = $value;
304 * Retrieves the details for this site.
306 * This method is used internally to lazy-load the extended properties of a site.
311 * @see WP_Site::__get()
313 * @return object A raw site object with all details included.
315 private function get_details() {
316 $details = wp_cache_get( $this->blog_id, 'site-details' );
318 if ( false === $details ) {
320 switch_to_blog( $this->blog_id );
321 // Create a raw copy of the object for backwards compatibility with the filter below.
322 $details = new stdClass();
323 foreach ( get_object_vars( $this ) as $key => $value ) {
324 $details->$key = $value;
326 $details->blogname = get_option( 'blogname' );
327 $details->siteurl = get_option( 'siteurl' );
328 $details->post_count = get_option( 'post_count' );
329 $details->home = get_option( 'home' );
330 restore_current_blog();
332 $cache_details = true;
333 foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) {
334 if ( false === $details->$field ) {
335 $cache_details = false;
340 if ( $cache_details ) {
341 wp_cache_set( $this->blog_id, $details, 'site-details' );
346 * Filters a site's extended properties.
350 * @param object $details The site details.
352 $details = apply_filters( 'site_details', $details );