WP_Site

Core class used for interacting with a multisite site.

Defined (1)

The class is defined in the following location(s).

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