class-wp-site.php 7.3 KB

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