/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. $_site = wp_cache_get( $site_id, 'sites' ); 
  177.  
  178. if ( ! $_site ) { 
  179. $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) ); 
  180.  
  181. if ( empty( $_site ) || is_wp_error( $_site ) ) { 
  182. return false; 
  183.  
  184. wp_cache_add( $site_id, $_site, 'sites' ); 
  185.  
  186. return new WP_Site( $_site ); 
  187.  
  188. /** 
  189. * Creates a new WP_Site object. 
  190. * 
  191. * Will populate object properties from the object provided and assign other 
  192. * default properties based on that information. 
  193. * 
  194. * @since 4.5.0 
  195. * @access public 
  196. * 
  197. * @param WP_Site|object $site A site object. 
  198. */ 
  199. public function __construct( $site ) { 
  200. foreach( get_object_vars( $site ) as $key => $value ) { 
  201. $this->$key = $value; 
  202.  
  203. /** 
  204. * Converts an object to array. 
  205. * 
  206. * @since 4.6.0 
  207. * @access public 
  208. * 
  209. * @return array Object as array. 
  210. */ 
  211. public function to_array() { 
  212. return get_object_vars( $this ); 
  213.  
  214. /** 
  215. * Getter. 
  216. * 
  217. * Allows current multisite naming conventions when getting properties. 
  218. * Allows access to extended site properties. 
  219. * 
  220. * @since 4.6.0 
  221. * @access public 
  222. * 
  223. * @param string $key Property to get. 
  224. * @return mixed Value of the property. Null if not available. 
  225. */ 
  226. public function __get( $key ) { 
  227. switch ( $key ) { 
  228. case 'id': 
  229. return (int) $this->blog_id; 
  230. case 'network_id': 
  231. return (int) $this->site_id; 
  232. case 'blogname': 
  233. case 'siteurl': 
  234. case 'post_count': 
  235. case 'home': 
  236. if ( ! did_action( 'ms_loaded' ) ) { 
  237. return null; 
  238. $details = $this->get_details(); 
  239. return $details->$key; 
  240.  
  241. return null; 
  242.  
  243. /** 
  244. * Isset-er. 
  245. * 
  246. * Allows current multisite naming conventions when checking for properties. 
  247. * Checks for extended site properties. 
  248. * 
  249. * @since 4.6.0 
  250. * @access public 
  251. * 
  252. * @param string $key Property to check if set. 
  253. * @return bool Whether the property is set. 
  254. */ 
  255. public function __isset( $key ) { 
  256. switch ( $key ) { 
  257. case 'id': 
  258. case 'network_id': 
  259. return true; 
  260. case 'blogname': 
  261. case 'siteurl': 
  262. case 'post_count': 
  263. case 'home': 
  264. if ( ! did_action( 'ms_loaded' ) ) { 
  265. return false; 
  266. return true; 
  267.  
  268. return false; 
  269.  
  270. /** 
  271. * Setter. 
  272. * 
  273. * Allows current multisite naming conventions while setting properties. 
  274. * 
  275. * @since 4.6.0 
  276. * @access public 
  277. * 
  278. * @param string $key Property to set. 
  279. * @param mixed $value Value to assign to the property. 
  280. */ 
  281. public function __set( $key, $value ) { 
  282. switch ( $key ) { 
  283. case 'id': 
  284. $this->blog_id = (string) $value; 
  285. break; 
  286. case 'network_id': 
  287. $this->site_id = (string) $value; 
  288. break; 
  289. default: 
  290. $this->$key = $value; 
  291.  
  292. /** 
  293. * Retrieves the details for this site. 
  294. * 
  295. * This method is used internally to lazy-load the extended properties of a site. 
  296. * 
  297. * @since 4.6.0 
  298. * @access private 
  299. * 
  300. * @see WP_Site::__get() 
  301. * 
  302. * @return stdClass A raw site object with all details included. 
  303. */ 
  304. private function get_details() { 
  305. $details = wp_cache_get( $this->blog_id, 'site-details' ); 
  306.  
  307. if ( false === $details ) { 
  308.  
  309. switch_to_blog( $this->blog_id ); 
  310. // Create a raw copy of the object for backwards compatibility with the filter below. 
  311. $details = new stdClass(); 
  312. foreach ( get_object_vars( $this ) as $key => $value ) { 
  313. $details->$key = $value; 
  314. $details->blogname = get_option( 'blogname' ); 
  315. $details->siteurl = get_option( 'siteurl' ); 
  316. $details->post_count = get_option( 'post_count' ); 
  317. $details->home = get_option( 'home' ); 
  318. restore_current_blog(); 
  319.  
  320. $cache_details = true; 
  321. foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $field ) { 
  322. if ( false === $details->$field ) { 
  323. $cache_details = false; 
  324. break; 
  325.  
  326. if ( $cache_details ) { 
  327. wp_cache_set( $this->blog_id, $details, 'site-details' ); 
  328.  
  329. /** This filter is documented in wp-includes/ms-blogs.php */ 
  330. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); 
  331.  
  332. /** 
  333. * Filters a site's extended properties. 
  334. * 
  335. * @since 4.6.0 
  336. * 
  337. * @param stdClass $details The site details. 
  338. */ 
  339. $details = apply_filters( 'site_details', $details ); 
  340.  
  341. return $details; 
.