PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-site.php

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