Brak opisu

class-wp-site.php 7.3KB

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