class-wp-site.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * @since 4.5.0
  135. *
  136. * @global wpdb $wpdb WordPress database abstraction object.
  137. *
  138. * @param int $site_id The ID of the site to retrieve.
  139. * @return WP_Site|false The site's object if found. False if not.
  140. */
  141. public static function get_instance( $site_id ) {
  142. global $wpdb;
  143. $site_id = (int) $site_id;
  144. if ( ! $site_id ) {
  145. return false;
  146. }
  147. $_site = wp_cache_get( $site_id, 'sites' );
  148. if ( false === $_site ) {
  149. $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
  150. if ( empty( $_site ) || is_wp_error( $_site ) ) {
  151. $_site = -1;
  152. }
  153. wp_cache_add( $site_id, $_site, 'sites' );
  154. }
  155. if ( is_numeric( $_site ) ) {
  156. return false;
  157. }
  158. return new WP_Site( $_site );
  159. }
  160. /**
  161. * Creates a new WP_Site object.
  162. *
  163. * Will populate object properties from the object provided and assign other
  164. * default properties based on that information.
  165. *
  166. * @since 4.5.0
  167. *
  168. * @param WP_Site|object $site A site object.
  169. */
  170. public function __construct( $site ) {
  171. foreach ( get_object_vars( $site ) as $key => $value ) {
  172. $this->$key = $value;
  173. }
  174. }
  175. /**
  176. * Converts an object to array.
  177. *
  178. * @since 4.6.0
  179. *
  180. * @return array Object as array.
  181. */
  182. public function to_array() {
  183. return get_object_vars( $this );
  184. }
  185. /**
  186. * Getter.
  187. *
  188. * Allows current multisite naming conventions when getting properties.
  189. * Allows access to extended site properties.
  190. *
  191. * @since 4.6.0
  192. *
  193. * @param string $key Property to get.
  194. * @return mixed Value of the property. Null if not available.
  195. */
  196. public function __get( $key ) {
  197. switch ( $key ) {
  198. case 'id':
  199. return (int) $this->blog_id;
  200. case 'network_id':
  201. return (int) $this->site_id;
  202. case 'blogname':
  203. case 'siteurl':
  204. case 'post_count':
  205. case 'home':
  206. default: // Custom properties added by 'site_details' filter.
  207. if ( ! did_action( 'ms_loaded' ) ) {
  208. return null;
  209. }
  210. $details = $this->get_details();
  211. if ( isset( $details->$key ) ) {
  212. return $details->$key;
  213. }
  214. }
  215. return null;
  216. }
  217. /**
  218. * Isset-er.
  219. *
  220. * Allows current multisite naming conventions when checking for properties.
  221. * Checks for extended site properties.
  222. *
  223. * @since 4.6.0
  224. *
  225. * @param string $key Property to check if set.
  226. * @return bool Whether the property is set.
  227. */
  228. public function __isset( $key ) {
  229. switch ( $key ) {
  230. case 'id':
  231. case 'network_id':
  232. return true;
  233. case 'blogname':
  234. case 'siteurl':
  235. case 'post_count':
  236. case 'home':
  237. if ( ! did_action( 'ms_loaded' ) ) {
  238. return false;
  239. }
  240. return true;
  241. default: // Custom properties added by 'site_details' filter.
  242. if ( ! did_action( 'ms_loaded' ) ) {
  243. return false;
  244. }
  245. $details = $this->get_details();
  246. if ( isset( $details->$key ) ) {
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. /**
  253. * Setter.
  254. *
  255. * Allows current multisite naming conventions while setting properties.
  256. *
  257. * @since 4.6.0
  258. *
  259. * @param string $key Property to set.
  260. * @param mixed $value Value to assign to the property.
  261. */
  262. public function __set( $key, $value ) {
  263. switch ( $key ) {
  264. case 'id':
  265. $this->blog_id = (string) $value;
  266. break;
  267. case 'network_id':
  268. $this->site_id = (string) $value;
  269. break;
  270. default:
  271. $this->$key = $value;
  272. }
  273. }
  274. /**
  275. * Retrieves the details for this site.
  276. *
  277. * This method is used internally to lazy-load the extended properties of a site.
  278. *
  279. * @since 4.6.0
  280. *
  281. * @see WP_Site::__get()
  282. *
  283. * @return stdClass A raw site object with all details included.
  284. */
  285. private function get_details() {
  286. $details = wp_cache_get( $this->blog_id, 'site-details' );
  287. if ( false === $details ) {
  288. switch_to_blog( $this->blog_id );
  289. // Create a raw copy of the object for backward compatibility with the filter below.
  290. $details = new stdClass();
  291. foreach ( get_object_vars( $this ) as $key => $value ) {
  292. $details->$key = $value;
  293. }
  294. $details->blogname = get_option( 'blogname' );
  295. $details->siteurl = get_option( 'siteurl' );
  296. $details->post_count = get_option( 'post_count' );
  297. $details->home = get_option( 'home' );
  298. restore_current_blog();
  299. wp_cache_set( $this->blog_id, $details, 'site-details' );
  300. }
  301. /** This filter is documented in wp-includes/ms-blogs.php */
  302. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
  303. /**
  304. * Filters a site's extended properties.
  305. *
  306. * @since 4.6.0
  307. *
  308. * @param stdClass $details The site details.
  309. */
  310. $details = apply_filters( 'site_details', $details );
  311. return $details;
  312. }
  313. }