class-wp-site.php 7.1 KB

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