class-wp-network.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #[AllowDynamicProperties]
  24. class WP_Network {
  25. /**
  26. * Network ID.
  27. *
  28. * @since 4.4.0
  29. * @since 4.6.0 Converted from public to private to explicitly enable more intuitive
  30. * access via magic methods. As part of the access change, the type was
  31. * also changed from `string` to `int`.
  32. * @var int
  33. */
  34. private $id;
  35. /**
  36. * Domain of the network.
  37. *
  38. * @since 4.4.0
  39. * @var string
  40. */
  41. public $domain = '';
  42. /**
  43. * Path of the network.
  44. *
  45. * @since 4.4.0
  46. * @var string
  47. */
  48. public $path = '';
  49. /**
  50. * The ID of the network's main site.
  51. *
  52. * Named "blog" vs. "site" for legacy reasons. A main site is mapped to
  53. * the network when the network is created.
  54. *
  55. * A numeric string, for compatibility reasons.
  56. *
  57. * @since 4.4.0
  58. * @var string
  59. */
  60. private $blog_id = '0';
  61. /**
  62. * Domain used to set cookies for this network.
  63. *
  64. * @since 4.4.0
  65. * @var string
  66. */
  67. public $cookie_domain = '';
  68. /**
  69. * Name of this network.
  70. *
  71. * Named "site" vs. "network" for legacy reasons.
  72. *
  73. * @since 4.4.0
  74. * @var string
  75. */
  76. public $site_name = '';
  77. /**
  78. * Retrieves a network from the database by its ID.
  79. *
  80. * @since 4.4.0
  81. *
  82. * @global wpdb $wpdb WordPress database abstraction object.
  83. *
  84. * @param int $network_id The ID of the network to retrieve.
  85. * @return WP_Network|false The network's object if found. False if not.
  86. */
  87. public static function get_instance( $network_id ) {
  88. global $wpdb;
  89. $network_id = (int) $network_id;
  90. if ( ! $network_id ) {
  91. return false;
  92. }
  93. $_network = wp_cache_get( $network_id, 'networks' );
  94. if ( false === $_network ) {
  95. $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
  96. if ( empty( $_network ) || is_wp_error( $_network ) ) {
  97. $_network = -1;
  98. }
  99. wp_cache_add( $network_id, $_network, 'networks' );
  100. }
  101. if ( is_numeric( $_network ) ) {
  102. return false;
  103. }
  104. return new WP_Network( $_network );
  105. }
  106. /**
  107. * Creates a new WP_Network object.
  108. *
  109. * Will populate object properties from the object provided and assign other
  110. * default properties based on that information.
  111. *
  112. * @since 4.4.0
  113. *
  114. * @param WP_Network|object $network A network object.
  115. */
  116. public function __construct( $network ) {
  117. foreach ( get_object_vars( $network ) as $key => $value ) {
  118. $this->$key = $value;
  119. }
  120. $this->_set_site_name();
  121. $this->_set_cookie_domain();
  122. }
  123. /**
  124. * Getter.
  125. *
  126. * Allows current multisite naming conventions when getting properties.
  127. *
  128. * @since 4.6.0
  129. *
  130. * @param string $key Property to get.
  131. * @return mixed Value of the property. Null if not available.
  132. */
  133. public function __get( $key ) {
  134. switch ( $key ) {
  135. case 'id':
  136. return (int) $this->id;
  137. case 'blog_id':
  138. return (string) $this->get_main_site_id();
  139. case 'site_id':
  140. return $this->get_main_site_id();
  141. }
  142. return null;
  143. }
  144. /**
  145. * Isset-er.
  146. *
  147. * Allows current multisite naming conventions when checking for properties.
  148. *
  149. * @since 4.6.0
  150. *
  151. * @param string $key Property to check if set.
  152. * @return bool Whether the property is set.
  153. */
  154. public function __isset( $key ) {
  155. switch ( $key ) {
  156. case 'id':
  157. case 'blog_id':
  158. case 'site_id':
  159. return true;
  160. }
  161. return false;
  162. }
  163. /**
  164. * Setter.
  165. *
  166. * Allows current multisite naming conventions while setting properties.
  167. *
  168. * @since 4.6.0
  169. *
  170. * @param string $key Property to set.
  171. * @param mixed $value Value to assign to the property.
  172. */
  173. public function __set( $key, $value ) {
  174. switch ( $key ) {
  175. case 'id':
  176. $this->id = (int) $value;
  177. break;
  178. case 'blog_id':
  179. case 'site_id':
  180. $this->blog_id = (string) $value;
  181. break;
  182. default:
  183. $this->$key = $value;
  184. }
  185. }
  186. /**
  187. * Returns the main site ID for the network.
  188. *
  189. * Internal method used by the magic getter for the 'blog_id' and 'site_id'
  190. * properties.
  191. *
  192. * @since 4.9.0
  193. *
  194. * @return int The ID of the main site.
  195. */
  196. private function get_main_site_id() {
  197. /**
  198. * Filters the main site ID.
  199. *
  200. * Returning a positive integer will effectively short-circuit the function.
  201. *
  202. * @since 4.9.0
  203. *
  204. * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
  205. * @param WP_Network $network The network object for which the main site was detected.
  206. */
  207. $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
  208. if ( 0 < $main_site_id ) {
  209. return $main_site_id;
  210. }
  211. if ( 0 < (int) $this->blog_id ) {
  212. return (int) $this->blog_id;
  213. }
  214. if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
  215. || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
  216. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
  217. $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
  218. return (int) $this->blog_id;
  219. }
  220. if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
  221. $this->blog_id = (string) BLOGID_CURRENT_SITE;
  222. return (int) $this->blog_id;
  223. }
  224. }
  225. $site = get_site();
  226. if ( $site->domain === $this->domain && $site->path === $this->path ) {
  227. $main_site_id = (int) $site->id;
  228. } else {
  229. $main_site_id = get_network_option( $this->id, 'main_site' );
  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. update_network_option( $this->id, 'main_site', $main_site_id );
  242. }
  243. }
  244. $this->blog_id = (string) $main_site_id;
  245. return (int) $this->blog_id;
  246. }
  247. /**
  248. * Sets 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. * Sets 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. * Retrieves 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|false 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 = get_networks(
  317. array(
  318. 'number' => 1,
  319. 'count' => true,
  320. 'path__not_in' => '/',
  321. )
  322. );
  323. }
  324. $paths = array();
  325. if ( $using_paths ) {
  326. $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
  327. /**
  328. * Filters the number of path segments to consider when searching for a site.
  329. *
  330. * @since 3.9.0
  331. *
  332. * @param int|null $segments The number of path segments to consider. WordPress by default looks at
  333. * one path segment. The function default of null only makes sense when you
  334. * know the requested path should match a network.
  335. * @param string $domain The requested domain.
  336. * @param string $path The requested path, in full.
  337. */
  338. $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
  339. if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
  340. $path_segments = array_slice( $path_segments, 0, $segments );
  341. }
  342. while ( count( $path_segments ) ) {
  343. $paths[] = '/' . implode( '/', $path_segments ) . '/';
  344. array_pop( $path_segments );
  345. }
  346. $paths[] = '/';
  347. }
  348. /**
  349. * Determines a network by its domain and path.
  350. *
  351. * This allows one to short-circuit the default logic, perhaps by
  352. * replacing it with a routine that is more optimal for your setup.
  353. *
  354. * Return null to avoid the short-circuit. Return false if no network
  355. * can be found at the requested domain and path. Otherwise, return
  356. * an object from wp_get_network().
  357. *
  358. * @since 3.9.0
  359. *
  360. * @param null|false|WP_Network $network Network value to return by path. Default null
  361. * to continue retrieving the network.
  362. * @param string $domain The requested domain.
  363. * @param string $path The requested path, in full.
  364. * @param int|null $segments The suggested number of paths to consult.
  365. * Default null, meaning the entire path was to be consulted.
  366. * @param string[] $paths Array of paths to search for, based on `$path` and `$segments`.
  367. */
  368. $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
  369. if ( null !== $pre ) {
  370. return $pre;
  371. }
  372. if ( ! $using_paths ) {
  373. $networks = get_networks(
  374. array(
  375. 'number' => 1,
  376. 'orderby' => array(
  377. 'domain_length' => 'DESC',
  378. ),
  379. 'domain__in' => $domains,
  380. )
  381. );
  382. if ( ! empty( $networks ) ) {
  383. return array_shift( $networks );
  384. }
  385. return false;
  386. }
  387. $networks = get_networks(
  388. array(
  389. 'orderby' => array(
  390. 'domain_length' => 'DESC',
  391. 'path_length' => 'DESC',
  392. ),
  393. 'domain__in' => $domains,
  394. 'path__in' => $paths,
  395. )
  396. );
  397. /*
  398. * Domains are sorted by length of domain, then by length of path.
  399. * The domain must match for the path to be considered. Otherwise,
  400. * a network with the path of / will suffice.
  401. */
  402. $found = false;
  403. foreach ( $networks as $network ) {
  404. if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
  405. if ( in_array( $network->path, $paths, true ) ) {
  406. $found = true;
  407. break;
  408. }
  409. }
  410. if ( '/' === $network->path ) {
  411. $found = true;
  412. break;
  413. }
  414. }
  415. if ( true === $found ) {
  416. return $network;
  417. }
  418. return false;
  419. }
  420. }