ms-blogs.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. <?php
  2. /**
  3. * Site/blog functions that work with the blogs table and related data.
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since MU (3.0.0)
  8. */
  9. require_once ABSPATH . WPINC . '/ms-site.php';
  10. require_once ABSPATH . WPINC . '/ms-network.php';
  11. /**
  12. * Update the last_updated field for the current site.
  13. *
  14. * @since MU (3.0.0)
  15. */
  16. function wpmu_update_blogs_date() {
  17. $site_id = get_current_blog_id();
  18. update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) );
  19. /**
  20. * Fires after the blog details are updated.
  21. *
  22. * @since MU (3.0.0)
  23. *
  24. * @param int $blog_id Site ID.
  25. */
  26. do_action( 'wpmu_blog_updated', $site_id );
  27. }
  28. /**
  29. * Get a full blog URL, given a blog ID.
  30. *
  31. * @since MU (3.0.0)
  32. *
  33. * @param int $blog_id Blog ID.
  34. * @return string Full URL of the blog if found. Empty string if not.
  35. */
  36. function get_blogaddress_by_id( $blog_id ) {
  37. $bloginfo = get_site( (int) $blog_id );
  38. if ( empty( $bloginfo ) ) {
  39. return '';
  40. }
  41. $scheme = parse_url( $bloginfo->home, PHP_URL_SCHEME );
  42. $scheme = empty( $scheme ) ? 'http' : $scheme;
  43. return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
  44. }
  45. /**
  46. * Get a full blog URL, given a blog name.
  47. *
  48. * @since MU (3.0.0)
  49. *
  50. * @param string $blogname The (subdomain or directory) name
  51. * @return string
  52. */
  53. function get_blogaddress_by_name( $blogname ) {
  54. if ( is_subdomain_install() ) {
  55. if ( 'main' === $blogname ) {
  56. $blogname = 'www';
  57. }
  58. $url = rtrim( network_home_url(), '/' );
  59. if ( ! empty( $blogname ) ) {
  60. $url = preg_replace( '|^([^\.]+://)|', '${1}' . $blogname . '.', $url );
  61. }
  62. } else {
  63. $url = network_home_url( $blogname );
  64. }
  65. return esc_url( $url . '/' );
  66. }
  67. /**
  68. * Retrieves a site's ID given its (subdomain or directory) slug.
  69. *
  70. * @since MU (3.0.0)
  71. * @since 4.7.0 Converted to use `get_sites()`.
  72. *
  73. * @param string $slug A site's slug.
  74. * @return int|null The site ID, or null if no site is found for the given slug.
  75. */
  76. function get_id_from_blogname( $slug ) {
  77. $current_network = get_network();
  78. $slug = trim( $slug, '/' );
  79. if ( is_subdomain_install() ) {
  80. $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
  81. $path = $current_network->path;
  82. } else {
  83. $domain = $current_network->domain;
  84. $path = $current_network->path . $slug . '/';
  85. }
  86. $site_ids = get_sites(
  87. array(
  88. 'number' => 1,
  89. 'fields' => 'ids',
  90. 'domain' => $domain,
  91. 'path' => $path,
  92. 'update_site_meta_cache' => false,
  93. )
  94. );
  95. if ( empty( $site_ids ) ) {
  96. return null;
  97. }
  98. return array_shift( $site_ids );
  99. }
  100. /**
  101. * Retrieve the details for a blog from the blogs table and blog options.
  102. *
  103. * @since MU (3.0.0)
  104. *
  105. * @global wpdb $wpdb WordPress database abstraction object.
  106. *
  107. * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
  108. * If not specified the current blog ID is used.
  109. * @param bool $get_all Whether to retrieve all details or only the details in the blogs table.
  110. * Default is true.
  111. * @return WP_Site|false Blog details on success. False on failure.
  112. */
  113. function get_blog_details( $fields = null, $get_all = true ) {
  114. global $wpdb;
  115. if ( is_array( $fields ) ) {
  116. if ( isset( $fields['blog_id'] ) ) {
  117. $blog_id = $fields['blog_id'];
  118. } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
  119. $key = md5( $fields['domain'] . $fields['path'] );
  120. $blog = wp_cache_get( $key, 'blog-lookup' );
  121. if ( false !== $blog ) {
  122. return $blog;
  123. }
  124. if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
  125. $nowww = substr( $fields['domain'], 4 );
  126. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
  127. } else {
  128. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
  129. }
  130. if ( $blog ) {
  131. wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
  132. $blog_id = $blog->blog_id;
  133. } else {
  134. return false;
  135. }
  136. } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
  137. $key = md5( $fields['domain'] );
  138. $blog = wp_cache_get( $key, 'blog-lookup' );
  139. if ( false !== $blog ) {
  140. return $blog;
  141. }
  142. if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
  143. $nowww = substr( $fields['domain'], 4 );
  144. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
  145. } else {
  146. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
  147. }
  148. if ( $blog ) {
  149. wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
  150. $blog_id = $blog->blog_id;
  151. } else {
  152. return false;
  153. }
  154. } else {
  155. return false;
  156. }
  157. } else {
  158. if ( ! $fields ) {
  159. $blog_id = get_current_blog_id();
  160. } elseif ( ! is_numeric( $fields ) ) {
  161. $blog_id = get_id_from_blogname( $fields );
  162. } else {
  163. $blog_id = $fields;
  164. }
  165. }
  166. $blog_id = (int) $blog_id;
  167. $all = $get_all ? '' : 'short';
  168. $details = wp_cache_get( $blog_id . $all, 'blog-details' );
  169. if ( $details ) {
  170. if ( ! is_object( $details ) ) {
  171. if ( -1 == $details ) {
  172. return false;
  173. } else {
  174. // Clear old pre-serialized objects. Cache clients do better with that.
  175. wp_cache_delete( $blog_id . $all, 'blog-details' );
  176. unset( $details );
  177. }
  178. } else {
  179. return $details;
  180. }
  181. }
  182. // Try the other cache.
  183. if ( $get_all ) {
  184. $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
  185. } else {
  186. $details = wp_cache_get( $blog_id, 'blog-details' );
  187. // If short was requested and full cache is set, we can return.
  188. if ( $details ) {
  189. if ( ! is_object( $details ) ) {
  190. if ( -1 == $details ) {
  191. return false;
  192. } else {
  193. // Clear old pre-serialized objects. Cache clients do better with that.
  194. wp_cache_delete( $blog_id, 'blog-details' );
  195. unset( $details );
  196. }
  197. } else {
  198. return $details;
  199. }
  200. }
  201. }
  202. if ( empty( $details ) ) {
  203. $details = WP_Site::get_instance( $blog_id );
  204. if ( ! $details ) {
  205. // Set the full cache.
  206. wp_cache_set( $blog_id, -1, 'blog-details' );
  207. return false;
  208. }
  209. }
  210. if ( ! $details instanceof WP_Site ) {
  211. $details = new WP_Site( $details );
  212. }
  213. if ( ! $get_all ) {
  214. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  215. return $details;
  216. }
  217. $switched_blog = false;
  218. if ( get_current_blog_id() !== $blog_id ) {
  219. switch_to_blog( $blog_id );
  220. $switched_blog = true;
  221. }
  222. $details->blogname = get_option( 'blogname' );
  223. $details->siteurl = get_option( 'siteurl' );
  224. $details->post_count = get_option( 'post_count' );
  225. $details->home = get_option( 'home' );
  226. if ( $switched_blog ) {
  227. restore_current_blog();
  228. }
  229. /**
  230. * Filters a blog's details.
  231. *
  232. * @since MU (3.0.0)
  233. * @deprecated 4.7.0 Use {@see 'site_details'} instead.
  234. *
  235. * @param WP_Site $details The blog details.
  236. */
  237. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
  238. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  239. $key = md5( $details->domain . $details->path );
  240. wp_cache_set( $key, $details, 'blog-lookup' );
  241. return $details;
  242. }
  243. /**
  244. * Clear the blog details cache.
  245. *
  246. * @since MU (3.0.0)
  247. *
  248. * @param int $blog_id Optional. Blog ID. Defaults to current blog.
  249. */
  250. function refresh_blog_details( $blog_id = 0 ) {
  251. $blog_id = (int) $blog_id;
  252. if ( ! $blog_id ) {
  253. $blog_id = get_current_blog_id();
  254. }
  255. clean_blog_cache( $blog_id );
  256. }
  257. /**
  258. * Update the details for a blog. Updates the blogs table for a given blog ID.
  259. *
  260. * @since MU (3.0.0)
  261. *
  262. * @global wpdb $wpdb WordPress database abstraction object.
  263. *
  264. * @param int $blog_id Blog ID.
  265. * @param array $details Array of details keyed by blogs table field names.
  266. * @return bool True if update succeeds, false otherwise.
  267. */
  268. function update_blog_details( $blog_id, $details = array() ) {
  269. global $wpdb;
  270. if ( empty( $details ) ) {
  271. return false;
  272. }
  273. if ( is_object( $details ) ) {
  274. $details = get_object_vars( $details );
  275. }
  276. $site = wp_update_site( $blog_id, $details );
  277. if ( is_wp_error( $site ) ) {
  278. return false;
  279. }
  280. return true;
  281. }
  282. /**
  283. * Cleans the site details cache for a site.
  284. *
  285. * @since 4.7.4
  286. *
  287. * @param int $site_id Optional. Site ID. Default is the current site ID.
  288. */
  289. function clean_site_details_cache( $site_id = 0 ) {
  290. $site_id = (int) $site_id;
  291. if ( ! $site_id ) {
  292. $site_id = get_current_blog_id();
  293. }
  294. wp_cache_delete( $site_id, 'site-details' );
  295. wp_cache_delete( $site_id, 'blog-details' );
  296. }
  297. /**
  298. * Retrieve option value for a given blog id based on name of option.
  299. *
  300. * If the option does not exist or does not have a value, then the return value
  301. * will be false. This is useful to check whether you need to install an option
  302. * and is commonly used during installation of plugin options and to test
  303. * whether upgrading is required.
  304. *
  305. * If the option was serialized then it will be unserialized when it is returned.
  306. *
  307. * @since MU (3.0.0)
  308. *
  309. * @param int $id A blog ID. Can be null to refer to the current blog.
  310. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  311. * @param mixed $default Optional. Default value to return if the option does not exist.
  312. * @return mixed Value set for the option.
  313. */
  314. function get_blog_option( $id, $option, $default = false ) {
  315. $id = (int) $id;
  316. if ( empty( $id ) ) {
  317. $id = get_current_blog_id();
  318. }
  319. if ( get_current_blog_id() == $id ) {
  320. return get_option( $option, $default );
  321. }
  322. switch_to_blog( $id );
  323. $value = get_option( $option, $default );
  324. restore_current_blog();
  325. /**
  326. * Filters a blog option value.
  327. *
  328. * The dynamic portion of the hook name, `$option`, refers to the blog option name.
  329. *
  330. * @since 3.5.0
  331. *
  332. * @param string $value The option value.
  333. * @param int $id Blog ID.
  334. */
  335. return apply_filters( "blog_option_{$option}", $value, $id );
  336. }
  337. /**
  338. * Add a new option for a given blog ID.
  339. *
  340. * You do not need to serialize values. If the value needs to be serialized, then
  341. * it will be serialized before it is inserted into the database. Remember,
  342. * resources can not be serialized or added as an option.
  343. *
  344. * You can create options without values and then update the values later.
  345. * Existing options will not be updated and checks are performed to ensure that you
  346. * aren't adding a protected WordPress option. Care should be taken to not name
  347. * options the same as the ones which are protected.
  348. *
  349. * @since MU (3.0.0)
  350. *
  351. * @param int $id A blog ID. Can be null to refer to the current blog.
  352. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  353. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  354. * @return bool True if the option was added, false otherwise.
  355. */
  356. function add_blog_option( $id, $option, $value ) {
  357. $id = (int) $id;
  358. if ( empty( $id ) ) {
  359. $id = get_current_blog_id();
  360. }
  361. if ( get_current_blog_id() == $id ) {
  362. return add_option( $option, $value );
  363. }
  364. switch_to_blog( $id );
  365. $return = add_option( $option, $value );
  366. restore_current_blog();
  367. return $return;
  368. }
  369. /**
  370. * Removes option by name for a given blog ID. Prevents removal of protected WordPress options.
  371. *
  372. * @since MU (3.0.0)
  373. *
  374. * @param int $id A blog ID. Can be null to refer to the current blog.
  375. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  376. * @return bool True if the option was deleted, false otherwise.
  377. */
  378. function delete_blog_option( $id, $option ) {
  379. $id = (int) $id;
  380. if ( empty( $id ) ) {
  381. $id = get_current_blog_id();
  382. }
  383. if ( get_current_blog_id() == $id ) {
  384. return delete_option( $option );
  385. }
  386. switch_to_blog( $id );
  387. $return = delete_option( $option );
  388. restore_current_blog();
  389. return $return;
  390. }
  391. /**
  392. * Update an option for a particular blog.
  393. *
  394. * @since MU (3.0.0)
  395. *
  396. * @param int $id The blog ID.
  397. * @param string $option The option key.
  398. * @param mixed $value The option value.
  399. * @param mixed $deprecated Not used.
  400. * @return bool True if the value was updated, false otherwise.
  401. */
  402. function update_blog_option( $id, $option, $value, $deprecated = null ) {
  403. $id = (int) $id;
  404. if ( null !== $deprecated ) {
  405. _deprecated_argument( __FUNCTION__, '3.1.0' );
  406. }
  407. if ( get_current_blog_id() == $id ) {
  408. return update_option( $option, $value );
  409. }
  410. switch_to_blog( $id );
  411. $return = update_option( $option, $value );
  412. restore_current_blog();
  413. return $return;
  414. }
  415. /**
  416. * Switch the current blog.
  417. *
  418. * This function is useful if you need to pull posts, or other information,
  419. * from other blogs. You can switch back afterwards using restore_current_blog().
  420. *
  421. * Things that aren't switched:
  422. * - plugins. See #14941
  423. *
  424. * @see restore_current_blog()
  425. * @since MU (3.0.0)
  426. *
  427. * @global wpdb $wpdb WordPress database abstraction object.
  428. * @global int $blog_id
  429. * @global array $_wp_switched_stack
  430. * @global bool $switched
  431. * @global string $table_prefix
  432. * @global WP_Object_Cache $wp_object_cache
  433. *
  434. * @param int $new_blog_id The ID of the blog to switch to. Default: current blog.
  435. * @param bool $deprecated Not used.
  436. * @return true Always returns true.
  437. */
  438. function switch_to_blog( $new_blog_id, $deprecated = null ) {
  439. global $wpdb;
  440. $prev_blog_id = get_current_blog_id();
  441. if ( empty( $new_blog_id ) ) {
  442. $new_blog_id = $prev_blog_id;
  443. }
  444. $GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
  445. /*
  446. * If we're switching to the same blog id that we're on,
  447. * set the right vars, do the associated actions, but skip
  448. * the extra unnecessary work
  449. */
  450. if ( $new_blog_id == $prev_blog_id ) {
  451. /**
  452. * Fires when the blog is switched.
  453. *
  454. * @since MU (3.0.0)
  455. * @since 5.4.0 The `$context` parameter was added.
  456. *
  457. * @param int $new_blog_id New blog ID.
  458. * @param int $prev_blog_id Previous blog ID.
  459. * @param string $context Additional context. Accepts 'switch' when called from switch_to_blog()
  460. * or 'restore' when called from restore_current_blog().
  461. */
  462. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
  463. $GLOBALS['switched'] = true;
  464. return true;
  465. }
  466. $wpdb->set_blog_id( $new_blog_id );
  467. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  468. $GLOBALS['blog_id'] = $new_blog_id;
  469. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  470. wp_cache_switch_to_blog( $new_blog_id );
  471. } else {
  472. global $wp_object_cache;
  473. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  474. $global_groups = $wp_object_cache->global_groups;
  475. } else {
  476. $global_groups = false;
  477. }
  478. wp_cache_init();
  479. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  480. if ( is_array( $global_groups ) ) {
  481. wp_cache_add_global_groups( $global_groups );
  482. } else {
  483. wp_cache_add_global_groups(
  484. array(
  485. 'blog-details',
  486. 'blog-id-cache',
  487. 'blog-lookup',
  488. 'blog_meta',
  489. 'global-posts',
  490. 'networks',
  491. 'sites',
  492. 'site-details',
  493. 'site-options',
  494. 'site-transient',
  495. 'rss',
  496. 'users',
  497. 'useremail',
  498. 'userlogins',
  499. 'usermeta',
  500. 'user_meta',
  501. 'userslugs',
  502. )
  503. );
  504. }
  505. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  506. }
  507. }
  508. /** This filter is documented in wp-includes/ms-blogs.php */
  509. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
  510. $GLOBALS['switched'] = true;
  511. return true;
  512. }
  513. /**
  514. * Restore the current blog, after calling switch_to_blog().
  515. *
  516. * @see switch_to_blog()
  517. * @since MU (3.0.0)
  518. *
  519. * @global wpdb $wpdb WordPress database abstraction object.
  520. * @global array $_wp_switched_stack
  521. * @global int $blog_id
  522. * @global bool $switched
  523. * @global string $table_prefix
  524. * @global WP_Object_Cache $wp_object_cache
  525. *
  526. * @return bool True on success, false if we're already on the current blog.
  527. */
  528. function restore_current_blog() {
  529. global $wpdb;
  530. if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
  531. return false;
  532. }
  533. $new_blog_id = array_pop( $GLOBALS['_wp_switched_stack'] );
  534. $prev_blog_id = get_current_blog_id();
  535. if ( $new_blog_id == $prev_blog_id ) {
  536. /** This filter is documented in wp-includes/ms-blogs.php */
  537. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
  538. // If we still have items in the switched stack, consider ourselves still 'switched'.
  539. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  540. return true;
  541. }
  542. $wpdb->set_blog_id( $new_blog_id );
  543. $GLOBALS['blog_id'] = $new_blog_id;
  544. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  545. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  546. wp_cache_switch_to_blog( $new_blog_id );
  547. } else {
  548. global $wp_object_cache;
  549. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  550. $global_groups = $wp_object_cache->global_groups;
  551. } else {
  552. $global_groups = false;
  553. }
  554. wp_cache_init();
  555. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  556. if ( is_array( $global_groups ) ) {
  557. wp_cache_add_global_groups( $global_groups );
  558. } else {
  559. wp_cache_add_global_groups(
  560. array(
  561. 'blog-details',
  562. 'blog-id-cache',
  563. 'blog-lookup',
  564. 'blog_meta',
  565. 'global-posts',
  566. 'networks',
  567. 'sites',
  568. 'site-details',
  569. 'site-options',
  570. 'site-transient',
  571. 'rss',
  572. 'users',
  573. 'useremail',
  574. 'userlogins',
  575. 'usermeta',
  576. 'user_meta',
  577. 'userslugs',
  578. )
  579. );
  580. }
  581. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  582. }
  583. }
  584. /** This filter is documented in wp-includes/ms-blogs.php */
  585. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
  586. // If we still have items in the switched stack, consider ourselves still 'switched'.
  587. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  588. return true;
  589. }
  590. /**
  591. * Switches the initialized roles and current user capabilities to another site.
  592. *
  593. * @since 4.9.0
  594. *
  595. * @param int $new_site_id New site ID.
  596. * @param int $old_site_id Old site ID.
  597. */
  598. function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
  599. if ( $new_site_id == $old_site_id ) {
  600. return;
  601. }
  602. if ( ! did_action( 'init' ) ) {
  603. return;
  604. }
  605. wp_roles()->for_site( $new_site_id );
  606. wp_get_current_user()->for_site( $new_site_id );
  607. }
  608. /**
  609. * Determines if switch_to_blog() is in effect
  610. *
  611. * @since 3.5.0
  612. *
  613. * @global array $_wp_switched_stack
  614. *
  615. * @return bool True if switched, false otherwise.
  616. */
  617. function ms_is_switched() {
  618. return ! empty( $GLOBALS['_wp_switched_stack'] );
  619. }
  620. /**
  621. * Check if a particular blog is archived.
  622. *
  623. * @since MU (3.0.0)
  624. *
  625. * @param int $id Blog ID.
  626. * @return string Whether the blog is archived or not.
  627. */
  628. function is_archived( $id ) {
  629. return get_blog_status( $id, 'archived' );
  630. }
  631. /**
  632. * Update the 'archived' status of a particular blog.
  633. *
  634. * @since MU (3.0.0)
  635. *
  636. * @param int $id Blog ID.
  637. * @param string $archived The new status.
  638. * @return string $archived
  639. */
  640. function update_archived( $id, $archived ) {
  641. update_blog_status( $id, 'archived', $archived );
  642. return $archived;
  643. }
  644. /**
  645. * Update a blog details field.
  646. *
  647. * @since MU (3.0.0)
  648. * @since 5.1.0 Use wp_update_site() internally.
  649. *
  650. * @global wpdb $wpdb WordPress database abstraction object.
  651. *
  652. * @param int $blog_id Blog ID.
  653. * @param string $pref Field name.
  654. * @param string $value Field value.
  655. * @param null $deprecated Not used.
  656. * @return string|false $value
  657. */
  658. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  659. global $wpdb;
  660. if ( null !== $deprecated ) {
  661. _deprecated_argument( __FUNCTION__, '3.1.0' );
  662. }
  663. $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  664. if ( ! in_array( $pref, $allowed_field_names, true ) ) {
  665. return $value;
  666. }
  667. $result = wp_update_site(
  668. $blog_id,
  669. array(
  670. $pref => $value,
  671. )
  672. );
  673. if ( is_wp_error( $result ) ) {
  674. return false;
  675. }
  676. return $value;
  677. }
  678. /**
  679. * Get a blog details field.
  680. *
  681. * @since MU (3.0.0)
  682. *
  683. * @global wpdb $wpdb WordPress database abstraction object.
  684. *
  685. * @param int $id Blog ID.
  686. * @param string $pref Field name.
  687. * @return bool|string|null $value
  688. */
  689. function get_blog_status( $id, $pref ) {
  690. global $wpdb;
  691. $details = get_site( $id );
  692. if ( $details ) {
  693. return $details->$pref;
  694. }
  695. return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
  696. }
  697. /**
  698. * Get a list of most recently updated blogs.
  699. *
  700. * @since MU (3.0.0)
  701. *
  702. * @global wpdb $wpdb WordPress database abstraction object.
  703. *
  704. * @param mixed $deprecated Not used.
  705. * @param int $start Optional. Number of blogs to offset the query. Used to build LIMIT clause.
  706. * Can be used for pagination. Default 0.
  707. * @param int $quantity Optional. The maximum number of blogs to retrieve. Default 40.
  708. * @return array The list of blogs.
  709. */
  710. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  711. global $wpdb;
  712. if ( ! empty( $deprecated ) ) {
  713. _deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
  714. }
  715. return $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", get_current_network_id(), $start, $quantity ), ARRAY_A );
  716. }
  717. /**
  718. * Handler for updating the site's last updated date when a post is published or
  719. * an already published post is changed.
  720. *
  721. * @since 3.3.0
  722. *
  723. * @param string $new_status The new post status.
  724. * @param string $old_status The old post status.
  725. * @param WP_Post $post Post object.
  726. */
  727. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  728. $post_type_obj = get_post_type_object( $post->post_type );
  729. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  730. return;
  731. }
  732. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  733. return;
  734. }
  735. // Post was freshly published, published post was saved, or published post was unpublished.
  736. wpmu_update_blogs_date();
  737. }
  738. /**
  739. * Handler for updating the current site's last updated date when a published
  740. * post is deleted.
  741. *
  742. * @since 3.4.0
  743. *
  744. * @param int $post_id Post ID
  745. */
  746. function _update_blog_date_on_post_delete( $post_id ) {
  747. $post = get_post( $post_id );
  748. $post_type_obj = get_post_type_object( $post->post_type );
  749. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  750. return;
  751. }
  752. if ( 'publish' !== $post->post_status ) {
  753. return;
  754. }
  755. wpmu_update_blogs_date();
  756. }
  757. /**
  758. * Handler for updating the current site's posts count when a post is deleted.
  759. *
  760. * @since 4.0.0
  761. *
  762. * @param int $post_id Post ID.
  763. */
  764. function _update_posts_count_on_delete( $post_id ) {
  765. $post = get_post( $post_id );
  766. if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
  767. return;
  768. }
  769. update_posts_count();
  770. }
  771. /**
  772. * Handler for updating the current site's posts count when a post status changes.
  773. *
  774. * @since 4.0.0
  775. * @since 4.9.0 Added the `$post` parameter.
  776. *
  777. * @param string $new_status The status the post is changing to.
  778. * @param string $old_status The status the post is changing from.
  779. * @param WP_Post $post Post object
  780. */
  781. function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
  782. if ( $new_status === $old_status ) {
  783. return;
  784. }
  785. if ( 'post' !== get_post_type( $post ) ) {
  786. return;
  787. }
  788. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  789. return;
  790. }
  791. update_posts_count();
  792. }
  793. /**
  794. * Count number of sites grouped by site status.
  795. *
  796. * @since 5.3.0
  797. *
  798. * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
  799. * @return int[] {
  800. * Numbers of sites grouped by site status.
  801. *
  802. * @type int $all The total number of sites.
  803. * @type int $public The number of public sites.
  804. * @type int $archived The number of archived sites.
  805. * @type int $mature The number of mature sites.
  806. * @type int $spam The number of spam sites.
  807. * @type int $deleted The number of deleted sites.
  808. * }
  809. */
  810. function wp_count_sites( $network_id = null ) {
  811. if ( empty( $network_id ) ) {
  812. $network_id = get_current_network_id();
  813. }
  814. $counts = array();
  815. $args = array(
  816. 'network_id' => $network_id,
  817. 'number' => 1,
  818. 'fields' => 'ids',
  819. 'no_found_rows' => false,
  820. );
  821. $q = new WP_Site_Query( $args );
  822. $counts['all'] = $q->found_sites;
  823. $_args = $args;
  824. $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
  825. foreach ( $statuses as $status ) {
  826. $_args = $args;
  827. $_args[ $status ] = 1;
  828. $q = new WP_Site_Query( $_args );
  829. $counts[ $status ] = $q->found_sites;
  830. }
  831. return $counts;
  832. }