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