class-wp-network.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 ( ! $_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. return false;
  97. }
  98. wp_cache_add( $network_id, $_network, 'networks' );
  99. }
  100. return new WP_Network( $_network );
  101. }
  102. /**
  103. * Create a new WP_Network object.
  104. *
  105. * Will populate object properties from the object provided and assign other
  106. * default properties based on that information.
  107. *
  108. * @since 4.4.0
  109. *
  110. * @param WP_Network|object $network A network object.
  111. */
  112. public function __construct( $network ) {
  113. foreach( get_object_vars( $network ) as $key => $value ) {
  114. $this->$key = $value;
  115. }
  116. $this->_set_site_name();
  117. $this->_set_cookie_domain();
  118. }
  119. /**
  120. * Getter.
  121. *
  122. * Allows current multisite naming conventions when getting properties.
  123. *
  124. * @since 4.6.0
  125. *
  126. * @param string $key Property to get.
  127. * @return mixed Value of the property. Null if not available.
  128. */
  129. public function __get( $key ) {
  130. switch ( $key ) {
  131. case 'id':
  132. return (int) $this->id;
  133. case 'blog_id':
  134. return (string) $this->get_main_site_id();
  135. case 'site_id':
  136. return $this->get_main_site_id();
  137. }
  138. return null;
  139. }
  140. /**
  141. * Isset-er.
  142. *
  143. * Allows current multisite naming conventions when checking for properties.
  144. *
  145. * @since 4.6.0
  146. *
  147. * @param string $key Property to check if set.
  148. * @return bool Whether the property is set.
  149. */
  150. public function __isset( $key ) {
  151. switch ( $key ) {
  152. case 'id':
  153. case 'blog_id':
  154. case 'site_id':
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * Setter.
  161. *
  162. * Allows current multisite naming conventions while setting properties.
  163. *
  164. * @since 4.6.0
  165. *
  166. * @param string $key Property to set.
  167. * @param mixed $value Value to assign to the property.
  168. */
  169. public function __set( $key, $value ) {
  170. switch ( $key ) {
  171. case 'id':
  172. $this->id = (int) $value;
  173. break;
  174. case 'blog_id':
  175. case 'site_id':
  176. $this->blog_id = (string) $value;
  177. break;
  178. default:
  179. $this->$key = $value;
  180. }
  181. }
  182. /**
  183. * Returns the main site ID for the network.
  184. *
  185. * Internal method used by the magic getter for the 'blog_id' and 'site_id'
  186. * properties.
  187. *
  188. * @since 4.9.0
  189. *
  190. * @return int The ID of the main site.
  191. */
  192. private function get_main_site_id() {
  193. /**
  194. * Filters the main site ID.
  195. *
  196. * Returning a positive integer will effectively short-circuit the function.
  197. *
  198. * @since 4.9.0
  199. *
  200. * @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
  201. * @param WP_Network $network The network object for which the main site was detected.
  202. */
  203. $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
  204. if ( 0 < $main_site_id ) {
  205. return $main_site_id;
  206. }
  207. if ( 0 < (int) $this->blog_id ) {
  208. return (int) $this->blog_id;
  209. }
  210. if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_CURRENT_SITE )
  211. || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) {
  212. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
  213. $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
  214. return (int) $this->blog_id;
  215. }
  216. if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
  217. $this->blog_id = (string) BLOGID_CURRENT_SITE;
  218. return (int) $this->blog_id;
  219. }
  220. }
  221. $site = get_site();
  222. if ( $site->domain === $this->domain && $site->path === $this->path ) {
  223. $main_site_id = (int) $site->id;
  224. } else {
  225. $cache_key = 'network:' . $this->id . ':main_site';
  226. $main_site_id = wp_cache_get( $cache_key, 'site-options' );
  227. if ( false === $main_site_id ) {
  228. $_sites = get_sites( array(
  229. 'fields' => 'ids',
  230. 'number' => 1,
  231. 'domain' => $this->domain,
  232. 'path' => $this->path,
  233. 'network_id' => $this->id,
  234. ) );
  235. $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
  236. wp_cache_add( $cache_key, $main_site_id, 'site-options' );
  237. }
  238. }
  239. $this->blog_id = (string) $main_site_id;
  240. return (int) $this->blog_id;
  241. }
  242. /**
  243. * Set the site name assigned to the network if one has not been populated.
  244. *
  245. * @since 4.4.0
  246. */
  247. private function _set_site_name() {
  248. if ( ! empty( $this->site_name ) ) {
  249. return;
  250. }
  251. $default = ucfirst( $this->domain );
  252. $this->site_name = get_network_option( $this->id, 'site_name', $default );
  253. }
  254. /**
  255. * Set the cookie domain based on the network domain if one has
  256. * not been populated.
  257. *
  258. * @todo What if the domain of the network doesn't match the current site?
  259. *
  260. * @since 4.4.0
  261. */
  262. private function _set_cookie_domain() {
  263. if ( ! empty( $this->cookie_domain ) ) {
  264. return;
  265. }
  266. $this->cookie_domain = $this->domain;
  267. if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
  268. $this->cookie_domain = substr( $this->cookie_domain, 4 );
  269. }
  270. }
  271. /**
  272. * Retrieve the closest matching network for a domain and path.
  273. *
  274. * This will not necessarily return an exact match for a domain and path. Instead, it
  275. * breaks the domain and path into pieces that are then used to match the closest
  276. * possibility from a query.
  277. *
  278. * The intent of this method is to match a network during bootstrap for a
  279. * requested site address.
  280. *
  281. * @since 4.4.0
  282. * @static
  283. *
  284. * @param string $domain Domain to check.
  285. * @param string $path Path to check.
  286. * @param int|null $segments Path segments to use. Defaults to null, or the full path.
  287. * @return WP_Network|bool Network object if successful. False when no network is found.
  288. */
  289. public static function get_by_path( $domain = '', $path = '', $segments = null ) {
  290. $domains = array( $domain );
  291. $pieces = explode( '.', $domain );
  292. /*
  293. * It's possible one domain to search is 'com', but it might as well
  294. * be 'localhost' or some other locally mapped domain.
  295. */
  296. while ( array_shift( $pieces ) ) {
  297. if ( ! empty( $pieces ) ) {
  298. $domains[] = implode( '.', $pieces );
  299. }
  300. }
  301. /*
  302. * If we've gotten to this function during normal execution, there is
  303. * more than one network installed. At this point, who knows how many
  304. * we have. Attempt to optimize for the situation where networks are
  305. * only domains, thus meaning paths never need to be considered.
  306. *
  307. * This is a very basic optimization; anything further could have
  308. * drawbacks depending on the setup, so this is best done per-installation.
  309. */
  310. $using_paths = true;
  311. if ( wp_using_ext_object_cache() ) {
  312. $using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
  313. if ( false === $using_paths ) {
  314. $using_paths = get_networks( array(
  315. 'number' => 1,
  316. 'count' => true,
  317. 'path__not_in' => '/',
  318. ) );
  319. wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' );
  320. }
  321. }
  322. $paths = array();
  323. if ( $using_paths ) {
  324. $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
  325. /**
  326. * Filters the number of path segments to consider when searching for a site.
  327. *
  328. * @since 3.9.0
  329. *
  330. * @param int|null $segments The number of path segments to consider. WordPress by default looks at
  331. * one path segment. The function default of null only makes sense when you
  332. * know the requested path should match a network.
  333. * @param string $domain The requested domain.
  334. * @param string $path The requested path, in full.
  335. */
  336. $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
  337. if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
  338. $path_segments = array_slice( $path_segments, 0, $segments );
  339. }
  340. while ( count( $path_segments ) ) {
  341. $paths[] = '/' . implode( '/', $path_segments ) . '/';
  342. array_pop( $path_segments );
  343. }
  344. $paths[] = '/';
  345. }
  346. /**
  347. * Determine a network by its domain and path.
  348. *
  349. * This allows one to short-circuit the default logic, perhaps by
  350. * replacing it with a routine that is more optimal for your setup.
  351. *
  352. * Return null to avoid the short-circuit. Return false if no network
  353. * can be found at the requested domain and path. Otherwise, return
  354. * an object from wp_get_network().
  355. *
  356. * @since 3.9.0
  357. *
  358. * @param null|bool|object $network Network value to return by path.
  359. * @param string $domain The requested domain.
  360. * @param string $path The requested path, in full.
  361. * @param int|null $segments The suggested number of paths to consult.
  362. * Default null, meaning the entire path was to be consulted.
  363. * @param array $paths The paths to search for, based on $path and $segments.
  364. */
  365. $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
  366. if ( null !== $pre ) {
  367. return $pre;
  368. }
  369. if ( ! $using_paths ) {
  370. $networks = get_networks( array(
  371. 'number' => 1,
  372. 'orderby' => array(
  373. 'domain_length' => 'DESC',
  374. ),
  375. 'domain__in' => $domains,
  376. ) );
  377. if ( ! empty( $networks ) ) {
  378. return array_shift( $networks );
  379. }
  380. return false;
  381. }
  382. $networks = get_networks( array(
  383. 'orderby' => array(
  384. 'domain_length' => 'DESC',
  385. 'path_length' => 'DESC',
  386. ),
  387. 'domain__in' => $domains,
  388. 'path__in' => $paths,
  389. ) );
  390. /*
  391. * Domains are sorted by length of domain, then by length of path.
  392. * The domain must match for the path to be considered. Otherwise,
  393. * a network with the path of / will suffice.
  394. */
  395. $found = false;
  396. foreach ( $networks as $network ) {
  397. if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
  398. if ( in_array( $network->path, $paths, true ) ) {
  399. $found = true;
  400. break;
  401. }
  402. }
  403. if ( $network->path === '/' ) {
  404. $found = true;
  405. break;
  406. }
  407. }
  408. if ( true === $found ) {
  409. return $network;
  410. }
  411. return false;
  412. }
  413. }