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