PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/class-wp-network.php

http://github.com/wordpress/wordpress
PHP | 477 lines | 208 code | 53 blank | 216 comment | 42 complexity | 881927734d881c7963838cd03560bdb1 MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. /**
  3. * Network API: WP_Network class
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used for interacting with a multisite network.
  11. *
  12. * This class is used during load to populate the `$current_site` global and
  13. * setup the current network.
  14. *
  15. * This class is most useful in WordPress multi-network installations where the
  16. * ability to interact with any network of sites is required.
  17. *
  18. * @since 4.4.0
  19. *
  20. * @property int $id
  21. * @property int $site_id
  22. */
  23. class WP_Network {
  24. /**
  25. * Network ID.
  26. *
  27. * @since 4.4.0
  28. * @since 4.6.0 Converted from public to private to explicitly enable more intuitive
  29. * access via magic methods. As part of the access change, the type was
  30. * also changed from `string` to `int`.
  31. * @var int
  32. */
  33. private $id;
  34. /**
  35. * Domain of the network.
  36. *
  37. * @since 4.4.0
  38. * @var string
  39. */
  40. public $domain = '';
  41. /**
  42. * Path of the network.
  43. *
  44. * @since 4.4.0
  45. * @var string
  46. */
  47. public $path = '';
  48. /**
  49. * The ID of the network's main site.
  50. *
  51. * Named "blog" vs. "site" for legacy reasons. A main site is mapped to
  52. * the network when the network is created.
  53. *
  54. * A numeric string, for compatibility reasons.
  55. *
  56. * @since 4.4.0
  57. * @var string
  58. */
  59. private $blog_id = '0';
  60. /**
  61. * Domain used to set cookies for this network.
  62. *
  63. * @since 4.4.0
  64. * @var string
  65. */
  66. public $cookie_domain = '';
  67. /**
  68. * Name of this network.
  69. *
  70. * Named "site" vs. "network" for legacy reasons.
  71. *
  72. * @since 4.4.0
  73. * @var string
  74. */
  75. public $site_name = '';
  76. /**
  77. * Retrieve a network from the database by its ID.
  78. *
  79. * @since 4.4.0
  80. *
  81. * @global wpdb $wpdb WordPress database abstraction object.
  82. *
  83. * @param int $network_id The ID of the network to retrieve.
  84. * @return WP_Network|bool The network's object if found. False if not.
  85. */
  86. public static function get_instance( $network_id ) {
  87. global $wpdb;
  88. $network_id = (int) $network_id;
  89. if ( ! $network_id ) {
  90. return false;
  91. }
  92. $_network = wp_cache_get( $network_id, 'networks' );
  93. if ( false === $_network ) {
  94. $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
  95. if ( empty( $_network ) || is_wp_error( $_network ) ) {
  96. $_network = -1;
  97. }
  98. wp_cache_add( $network_id, $_network, 'networks' );
  99. }
  100. if ( is_numeric( $_network ) ) {
  101. return false;
  102. }
  103. return new WP_Network( $_network );
  104. }
  105. /**
  106. * Create a new WP_Network object.
  107. *
  108. * Will populate object properties from the object provided and assign other
  109. * default properties based on that information.
  110. *
  111. * @since 4.4.0
  112. *
  113. * @param WP_Network|object $network A network object.
  114. */
  115. public function __construct( $network ) {
  116. foreach ( get_object_vars( $network ) as $key => $value ) {
  117. $this->$key = $value;
  118. }
  119. $this->_set_site_name();
  120. $this->_set_cookie_domain();
  121. }
  122. /**
  123. * Getter.
  124. *
  125. * Allows current multisite naming conventions when getting properties.
  126. *
  127. * @since 4.6.0
  128. *
  129. * @param string $key Property to get.
  130. * @return mixed Value of the property. Null if not available.
  131. */
  132. public function __get( $key ) {
  133. switch ( $key ) {
  134. case 'id':
  135. return (int) $this->id;
  136. case 'blog_id':
  137. return (string) $this->get_main_site_id();
  138. case 'site_id':
  139. return $this->get_main_site_id();
  140. }
  141. return null;
  142. }
  143. /**
  144. * Isset-er.
  145. *
  146. * Allows current multisite naming conventions when checking for properties.
  147. *
  148. * @since 4.6.0
  149. *
  150. * @param string $key Property to check if set.
  151. * @return bool Whether the property is set.
  152. */
  153. public function __isset( $key ) {
  154. switch ( $key ) {
  155. case 'id':
  156. case 'blog_id':
  157. case 'site_id':
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Setter.
  164. *
  165. * Allows current multisite naming conventions while setting properties.
  166. *
  167. * @since 4.6.0
  168. *
  169. * @param string $key Property to set.
  170. * @param mixed $value Value to assign to the property.
  171. */
  172. public function __set( $key, $value ) {
  173. switch ( $key ) {
  174. case 'id':
  175. $this->id = (int) $value;
  176. break;
  177. case 'blog_id':
  178. case 'site_id':
  179. $this->blog_id = (string) $value;
  180. break;
  181. default:
  182. $this->$key = $value;
  183. }
  184. }
  185. /**
  186. * Returns the main site ID for the network.
  187. *
  188. * Internal method used by the magic getter for the 'blog_id' and 'site_id'
  189. * properties.
  190. *
  191. * @since 4.9.0
  192. *
  193. * @return int The ID of the main site.
  194. */
  195. private function get_main_site_id() {
  196. /**
  197. * Filters the main site ID.
  198. *
  199. * Returning a positive integer will effectively short-circuit the function.
  200. *
  201. * @since 4.9.0
  202. *
  203. * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
  204. * @param WP_Network $network The network object for which the main site was detected.
  205. */
  206. $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
  207. if ( 0 < $main_site_id ) {
  208. return $main_site_id;
  209. }
  210. if ( 0 < (int) $this->blog_id ) {
  211. return (int) $this->blog_id;
  212. }
  213. if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
  214. || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
  215. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
  216. $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
  217. return (int) $this->blog_id;
  218. }
  219. if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
  220. $this->blog_id = (string) BLOGID_CURRENT_SITE;
  221. return (int) $this->blog_id;
  222. }
  223. }
  224. $site = get_site();
  225. if ( $site->domain === $this->domain && $site->path === $this->path ) {
  226. $main_site_id = (int) $site->id;
  227. } else {
  228. $cache_key = 'network:' . $this->id . ':main_site';
  229. $main_site_id = wp_cache_get( $cache_key, 'site-options' );
  230. if ( false === $main_site_id ) {
  231. $_sites = get_sites(
  232. array(
  233. 'fields' => 'ids',
  234. 'number' => 1,
  235. 'domain' => $this->domain,
  236. 'path' => $this->path,
  237. 'network_id' => $this->id,
  238. )
  239. );
  240. $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
  241. wp_cache_add( $cache_key, $main_site_id, 'site-options' );
  242. }
  243. }
  244. $this->blog_id = (string) $main_site_id;
  245. return (int) $this->blog_id;
  246. }
  247. /**
  248. * Set the site name assigned to the network if one has not been populated.
  249. *
  250. * @since 4.4.0
  251. */
  252. private function _set_site_name() {
  253. if ( ! empty( $this->site_name ) ) {
  254. return;
  255. }
  256. $default = ucfirst( $this->domain );
  257. $this->site_name = get_network_option( $this->id, 'site_name', $default );
  258. }
  259. /**
  260. * Set the cookie domain based on the network domain if one has
  261. * not been populated.
  262. *
  263. * @todo What if the domain of the network doesn't match the current site?
  264. *
  265. * @since 4.4.0
  266. */
  267. private function _set_cookie_domain() {
  268. if ( ! empty( $this->cookie_domain ) ) {
  269. return;
  270. }
  271. $this->cookie_domain = $this->domain;
  272. if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
  273. $this->cookie_domain = substr( $this->cookie_domain, 4 );
  274. }
  275. }
  276. /**
  277. * Retrieve the closest matching network for a domain and path.
  278. *
  279. * This will not necessarily return an exact match for a domain and path. Instead, it
  280. * breaks the domain and path into pieces that are then used to match the closest
  281. * possibility from a query.
  282. *
  283. * The intent of this method is to match a network during bootstrap for a
  284. * requested site address.
  285. *
  286. * @since 4.4.0
  287. *
  288. * @param string $domain Domain to check.
  289. * @param string $path Path to check.
  290. * @param int|null $segments Path segments to use. Defaults to null, or the full path.
  291. * @return WP_Network|bool Network object if successful. False when no network is found.
  292. */
  293. public static function get_by_path( $domain = '', $path = '', $segments = null ) {
  294. $domains = array( $domain );
  295. $pieces = explode( '.', $domain );
  296. /*
  297. * It's possible one domain to search is 'com', but it might as well
  298. * be 'localhost' or some other locally mapped domain.
  299. */
  300. while ( array_shift( $pieces ) ) {
  301. if ( ! empty( $pieces ) ) {
  302. $domains[] = implode( '.', $pieces );
  303. }
  304. }
  305. /*
  306. * If we've gotten to this function during normal execution, there is
  307. * more than one network installed. At this point, who knows how many
  308. * we have. Attempt to optimize for the situation where networks are
  309. * only domains, thus meaning paths never need to be considered.
  310. *
  311. * This is a very basic optimization; anything further could have
  312. * drawbacks depending on the setup, so this is best done per-installation.
  313. */
  314. $using_paths = true;
  315. if ( wp_using_ext_object_cache() ) {
  316. $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
  317. if ( false === $using_paths ) {
  318. $using_paths = get_networks(
  319. array(
  320. 'number' => 1,
  321. 'count' => true,
  322. 'path__not_in' => '/',
  323. )
  324. );
  325. wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' );
  326. }
  327. }
  328. $paths = array();
  329. if ( $using_paths ) {
  330. $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
  331. /**
  332. * Filters the number of path segments to consider when searching for a site.
  333. *
  334. * @since 3.9.0
  335. *
  336. * @param int|null $segments The number of path segments to consider. WordPress by default looks at
  337. * one path segment. The function default of null only makes sense when you
  338. * know the requested path should match a network.
  339. * @param string $domain The requested domain.
  340. * @param string $path The requested path, in full.
  341. */
  342. $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
  343. if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
  344. $path_segments = array_slice( $path_segments, 0, $segments );
  345. }
  346. while ( count( $path_segments ) ) {
  347. $paths[] = '/' . implode( '/', $path_segments ) . '/';
  348. array_pop( $path_segments );
  349. }
  350. $paths[] = '/';
  351. }
  352. /**
  353. * Determine a network by its domain and path.
  354. *
  355. * This allows one to short-circuit the default logic, perhaps by
  356. * replacing it with a routine that is more optimal for your setup.
  357. *
  358. * Return null to avoid the short-circuit. Return false if no network
  359. * can be found at the requested domain and path. Otherwise, return
  360. * an object from wp_get_network().
  361. *
  362. * @since 3.9.0
  363. *
  364. * @param null|bool|WP_Network $network Network value to return by path. Default null
  365. * to continue retrieving the network.
  366. * @param string $domain The requested domain.
  367. * @param string $path The requested path, in full.
  368. * @param int|null $segments The suggested number of paths to consult.
  369. * Default null, meaning the entire path was to be consulted.
  370. * @param string[] $paths Array of paths to search for, based on `$path` and `$segments`.
  371. */
  372. $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
  373. if ( null !== $pre ) {
  374. return $pre;
  375. }
  376. if ( ! $using_paths ) {
  377. $networks = get_networks(
  378. array(
  379. 'number' => 1,
  380. 'orderby' => array(
  381. 'domain_length' => 'DESC',
  382. ),
  383. 'domain__in' => $domains,
  384. )
  385. );
  386. if ( ! empty( $networks ) ) {
  387. return array_shift( $networks );
  388. }
  389. return false;
  390. }
  391. $networks = get_networks(
  392. array(
  393. 'orderby' => array(
  394. 'domain_length' => 'DESC',
  395. 'path_length' => 'DESC',
  396. ),
  397. 'domain__in' => $domains,
  398. 'path__in' => $paths,
  399. )
  400. );
  401. /*
  402. * Domains are sorted by length of domain, then by length of path.
  403. * The domain must match for the path to be considered. Otherwise,
  404. * a network with the path of / will suffice.
  405. */
  406. $found = false;
  407. foreach ( $networks as $network ) {
  408. if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
  409. if ( in_array( $network->path, $paths, true ) ) {
  410. $found = true;
  411. break;
  412. }
  413. }
  414. if ( '/' === $network->path ) {
  415. $found = true;
  416. break;
  417. }
  418. }
  419. if ( true === $found ) {
  420. return $network;
  421. }
  422. return false;
  423. }
  424. }