ms-blogs.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 sites 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 ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  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 ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  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. switch_to_blog( $blog_id );
  218. $details->blogname = get_option( 'blogname' );
  219. $details->siteurl = get_option( 'siteurl' );
  220. $details->post_count = get_option( 'post_count' );
  221. $details->home = get_option( 'home' );
  222. restore_current_blog();
  223. /**
  224. * Filters a blog's details.
  225. *
  226. * @since MU (3.0.0)
  227. * @deprecated 4.7.0 Use site_details
  228. *
  229. * @param object $details The blog details.
  230. */
  231. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
  232. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  233. $key = md5( $details->domain . $details->path );
  234. wp_cache_set( $key, $details, 'blog-lookup' );
  235. return $details;
  236. }
  237. /**
  238. * Clear the blog details cache.
  239. *
  240. * @since MU (3.0.0)
  241. *
  242. * @param int $blog_id Optional. Blog ID. Defaults to current blog.
  243. */
  244. function refresh_blog_details( $blog_id = 0 ) {
  245. $blog_id = (int) $blog_id;
  246. if ( ! $blog_id ) {
  247. $blog_id = get_current_blog_id();
  248. }
  249. clean_blog_cache( $blog_id );
  250. }
  251. /**
  252. * Update the details for a blog. Updates the blogs table for a given blog id.
  253. *
  254. * @since MU (3.0.0)
  255. *
  256. * @global wpdb $wpdb WordPress database abstraction object.
  257. *
  258. * @param int $blog_id Blog ID.
  259. * @param array $details Array of details keyed by blogs table field names.
  260. * @return bool True if update succeeds, false otherwise.
  261. */
  262. function update_blog_details( $blog_id, $details = array() ) {
  263. global $wpdb;
  264. if ( empty( $details ) ) {
  265. return false;
  266. }
  267. if ( is_object( $details ) ) {
  268. $details = get_object_vars( $details );
  269. }
  270. $site = wp_update_site( $blog_id, $details );
  271. if ( is_wp_error( $site ) ) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. /**
  277. * Cleans the site details cache for a site.
  278. *
  279. * @since 4.7.4
  280. *
  281. * @param int $site_id Optional. Site ID. Default is the current site ID.
  282. */
  283. function clean_site_details_cache( $site_id = 0 ) {
  284. $site_id = (int) $site_id;
  285. if ( ! $site_id ) {
  286. $site_id = get_current_blog_id();
  287. }
  288. wp_cache_delete( $site_id, 'site-details' );
  289. wp_cache_delete( $site_id, 'blog-details' );
  290. }
  291. /**
  292. * Retrieve option value for a given blog id based on name of option.
  293. *
  294. * If the option does not exist or does not have a value, then the return value
  295. * will be false. This is useful to check whether you need to install an option
  296. * and is commonly used during installation of plugin options and to test
  297. * whether upgrading is required.
  298. *
  299. * If the option was serialized then it will be unserialized when it is returned.
  300. *
  301. * @since MU (3.0.0)
  302. *
  303. * @param int $id A blog ID. Can be null to refer to the current blog.
  304. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  305. * @param mixed $default Optional. Default value to return if the option does not exist.
  306. * @return mixed Value set for the option.
  307. */
  308. function get_blog_option( $id, $option, $default = false ) {
  309. $id = (int) $id;
  310. if ( empty( $id ) ) {
  311. $id = get_current_blog_id();
  312. }
  313. if ( get_current_blog_id() == $id ) {
  314. return get_option( $option, $default );
  315. }
  316. switch_to_blog( $id );
  317. $value = get_option( $option, $default );
  318. restore_current_blog();
  319. /**
  320. * Filters a blog option value.
  321. *
  322. * The dynamic portion of the hook name, `$option`, refers to the blog option name.
  323. *
  324. * @since 3.5.0
  325. *
  326. * @param string $value The option value.
  327. * @param int $id Blog ID.
  328. */
  329. return apply_filters( "blog_option_{$option}", $value, $id );
  330. }
  331. /**
  332. * Add a new option for a given blog id.
  333. *
  334. * You do not need to serialize values. If the value needs to be serialized, then
  335. * it will be serialized before it is inserted into the database. Remember,
  336. * resources can not be serialized or added as an option.
  337. *
  338. * You can create options without values and then update the values later.
  339. * Existing options will not be updated and checks are performed to ensure that you
  340. * aren't adding a protected WordPress option. Care should be taken to not name
  341. * options the same as the ones which are protected.
  342. *
  343. * @since MU (3.0.0)
  344. *
  345. * @param int $id A blog ID. Can be null to refer to the current blog.
  346. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  347. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  348. * @return bool False if option was not added and true if option was added.
  349. */
  350. function add_blog_option( $id, $option, $value ) {
  351. $id = (int) $id;
  352. if ( empty( $id ) ) {
  353. $id = get_current_blog_id();
  354. }
  355. if ( get_current_blog_id() == $id ) {
  356. return add_option( $option, $value );
  357. }
  358. switch_to_blog( $id );
  359. $return = add_option( $option, $value );
  360. restore_current_blog();
  361. return $return;
  362. }
  363. /**
  364. * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
  365. *
  366. * @since MU (3.0.0)
  367. *
  368. * @param int $id A blog ID. Can be null to refer to the current blog.
  369. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  370. * @return bool True, if option is successfully deleted. False on failure.
  371. */
  372. function delete_blog_option( $id, $option ) {
  373. $id = (int) $id;
  374. if ( empty( $id ) ) {
  375. $id = get_current_blog_id();
  376. }
  377. if ( get_current_blog_id() == $id ) {
  378. return delete_option( $option );
  379. }
  380. switch_to_blog( $id );
  381. $return = delete_option( $option );
  382. restore_current_blog();
  383. return $return;
  384. }
  385. /**
  386. * Update an option for a particular blog.
  387. *
  388. * @since MU (3.0.0)
  389. *
  390. * @param int $id The blog id.
  391. * @param string $option The option key.
  392. * @param mixed $value The option value.
  393. * @param mixed $deprecated Not used.
  394. * @return bool True on success, false on failure.
  395. */
  396. function update_blog_option( $id, $option, $value, $deprecated = null ) {
  397. $id = (int) $id;
  398. if ( null !== $deprecated ) {
  399. _deprecated_argument( __FUNCTION__, '3.1.0' );
  400. }
  401. if ( get_current_blog_id() == $id ) {
  402. return update_option( $option, $value );
  403. }
  404. switch_to_blog( $id );
  405. $return = update_option( $option, $value );
  406. restore_current_blog();
  407. return $return;
  408. }
  409. /**
  410. * Switch the current blog.
  411. *
  412. * This function is useful if you need to pull posts, or other information,
  413. * from other blogs. You can switch back afterwards using restore_current_blog().
  414. *
  415. * Things that aren't switched:
  416. * - plugins. See #14941
  417. *
  418. * @see restore_current_blog()
  419. * @since MU (3.0.0)
  420. *
  421. * @global wpdb $wpdb WordPress database abstraction object.
  422. * @global int $blog_id
  423. * @global array $_wp_switched_stack
  424. * @global bool $switched
  425. * @global string $table_prefix
  426. * @global WP_Object_Cache $wp_object_cache
  427. *
  428. * @param int $new_blog_id The ID of the blog to switch to. Default: current blog.
  429. * @param bool $deprecated Not used.
  430. * @return true Always returns true.
  431. */
  432. function switch_to_blog( $new_blog_id, $deprecated = null ) {
  433. global $wpdb;
  434. $prev_blog_id = get_current_blog_id();
  435. if ( empty( $new_blog_id ) ) {
  436. $new_blog_id = $prev_blog_id;
  437. }
  438. $GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
  439. /*
  440. * If we're switching to the same blog id that we're on,
  441. * set the right vars, do the associated actions, but skip
  442. * the extra unnecessary work
  443. */
  444. if ( $new_blog_id == $prev_blog_id ) {
  445. /**
  446. * Fires when the blog is switched.
  447. *
  448. * @since MU (3.0.0)
  449. *
  450. * @param int $new_blog_id New blog ID.
  451. * @param int $prev_blog_id Previous blog ID.
  452. */
  453. do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
  454. $GLOBALS['switched'] = true;
  455. return true;
  456. }
  457. $wpdb->set_blog_id( $new_blog_id );
  458. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  459. $GLOBALS['blog_id'] = $new_blog_id;
  460. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  461. wp_cache_switch_to_blog( $new_blog_id );
  462. } else {
  463. global $wp_object_cache;
  464. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  465. $global_groups = $wp_object_cache->global_groups;
  466. } else {
  467. $global_groups = false;
  468. }
  469. wp_cache_init();
  470. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  471. if ( is_array( $global_groups ) ) {
  472. wp_cache_add_global_groups( $global_groups );
  473. } else {
  474. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
  475. }
  476. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  477. }
  478. }
  479. /** This filter is documented in wp-includes/ms-blogs.php */
  480. do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
  481. $GLOBALS['switched'] = true;
  482. return true;
  483. }
  484. /**
  485. * Restore the current blog, after calling switch_to_blog().
  486. *
  487. * @see switch_to_blog()
  488. * @since MU (3.0.0)
  489. *
  490. * @global wpdb $wpdb WordPress database abstraction object.
  491. * @global array $_wp_switched_stack
  492. * @global int $blog_id
  493. * @global bool $switched
  494. * @global string $table_prefix
  495. * @global WP_Object_Cache $wp_object_cache
  496. *
  497. * @return bool True on success, false if we're already on the current blog.
  498. */
  499. function restore_current_blog() {
  500. global $wpdb;
  501. if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
  502. return false;
  503. }
  504. $new_blog_id = array_pop( $GLOBALS['_wp_switched_stack'] );
  505. $prev_blog_id = get_current_blog_id();
  506. if ( $new_blog_id == $prev_blog_id ) {
  507. /** This filter is documented in wp-includes/ms-blogs.php */
  508. do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
  509. // If we still have items in the switched stack, consider ourselves still 'switched'
  510. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  511. return true;
  512. }
  513. $wpdb->set_blog_id( $new_blog_id );
  514. $GLOBALS['blog_id'] = $new_blog_id;
  515. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  516. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  517. wp_cache_switch_to_blog( $new_blog_id );
  518. } else {
  519. global $wp_object_cache;
  520. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  521. $global_groups = $wp_object_cache->global_groups;
  522. } else {
  523. $global_groups = false;
  524. }
  525. wp_cache_init();
  526. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  527. if ( is_array( $global_groups ) ) {
  528. wp_cache_add_global_groups( $global_groups );
  529. } else {
  530. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
  531. }
  532. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  533. }
  534. }
  535. /** This filter is documented in wp-includes/ms-blogs.php */
  536. do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
  537. // If we still have items in the switched stack, consider ourselves still 'switched'
  538. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  539. return true;
  540. }
  541. /**
  542. * Switches the initialized roles and current user capabilities to another site.
  543. *
  544. * @since 4.9.0
  545. *
  546. * @param int $new_site_id New site ID.
  547. * @param int $old_site_id Old site ID.
  548. */
  549. function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
  550. if ( $new_site_id == $old_site_id ) {
  551. return;
  552. }
  553. if ( ! did_action( 'init' ) ) {
  554. return;
  555. }
  556. wp_roles()->for_site( $new_site_id );
  557. wp_get_current_user()->for_site( $new_site_id );
  558. }
  559. /**
  560. * Determines if switch_to_blog() is in effect
  561. *
  562. * @since 3.5.0
  563. *
  564. * @global array $_wp_switched_stack
  565. *
  566. * @return bool True if switched, false otherwise.
  567. */
  568. function ms_is_switched() {
  569. return ! empty( $GLOBALS['_wp_switched_stack'] );
  570. }
  571. /**
  572. * Check if a particular blog is archived.
  573. *
  574. * @since MU (3.0.0)
  575. *
  576. * @param int $id Blog ID.
  577. * @return string Whether the blog is archived or not.
  578. */
  579. function is_archived( $id ) {
  580. return get_blog_status( $id, 'archived' );
  581. }
  582. /**
  583. * Update the 'archived' status of a particular blog.
  584. *
  585. * @since MU (3.0.0)
  586. *
  587. * @param int $id Blog ID.
  588. * @param string $archived The new status.
  589. * @return string $archived
  590. */
  591. function update_archived( $id, $archived ) {
  592. update_blog_status( $id, 'archived', $archived );
  593. return $archived;
  594. }
  595. /**
  596. * Update a blog details field.
  597. *
  598. * @since MU (3.0.0)
  599. * @since 5.1.0 Use wp_update_site() internally.
  600. *
  601. * @global wpdb $wpdb WordPress database abstraction object.
  602. *
  603. * @param int $blog_id Blog ID.
  604. * @param string $pref Field name.
  605. * @param string $value Field value.
  606. * @param null $deprecated Not used.
  607. * @return string|false $value
  608. */
  609. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  610. global $wpdb;
  611. if ( null !== $deprecated ) {
  612. _deprecated_argument( __FUNCTION__, '3.1.0' );
  613. }
  614. if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
  615. return $value;
  616. }
  617. $result = wp_update_site(
  618. $blog_id,
  619. array(
  620. $pref => $value,
  621. )
  622. );
  623. if ( is_wp_error( $result ) ) {
  624. return false;
  625. }
  626. return $value;
  627. }
  628. /**
  629. * Get a blog details field.
  630. *
  631. * @since MU (3.0.0)
  632. *
  633. * @global wpdb $wpdb WordPress database abstraction object.
  634. *
  635. * @param int $id Blog ID.
  636. * @param string $pref Field name.
  637. * @return bool|string|null $value
  638. */
  639. function get_blog_status( $id, $pref ) {
  640. global $wpdb;
  641. $details = get_site( $id );
  642. if ( $details ) {
  643. return $details->$pref;
  644. }
  645. return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
  646. }
  647. /**
  648. * Get a list of most recently updated blogs.
  649. *
  650. * @since MU (3.0.0)
  651. *
  652. * @global wpdb $wpdb WordPress database abstraction object.
  653. *
  654. * @param mixed $deprecated Not used.
  655. * @param int $start Optional. Number of blogs to offset the query. Used to build LIMIT clause.
  656. * Can be used for pagination. Default 0.
  657. * @param int $quantity Optional. The maximum number of blogs to retrieve. Default 40.
  658. * @return array The list of blogs.
  659. */
  660. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  661. global $wpdb;
  662. if ( ! empty( $deprecated ) ) {
  663. _deprecated_argument( __FUNCTION__, 'MU' ); // never used
  664. }
  665. 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 );
  666. }
  667. /**
  668. * Handler for updating the site's last updated date when a post is published or
  669. * an already published post is changed.
  670. *
  671. * @since 3.3.0
  672. *
  673. * @param string $new_status The new post status
  674. * @param string $old_status The old post status
  675. * @param object $post Post object
  676. */
  677. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  678. $post_type_obj = get_post_type_object( $post->post_type );
  679. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  680. return;
  681. }
  682. if ( 'publish' != $new_status && 'publish' != $old_status ) {
  683. return;
  684. }
  685. // Post was freshly published, published post was saved, or published post was unpublished.
  686. wpmu_update_blogs_date();
  687. }
  688. /**
  689. * Handler for updating the current site's last updated date when a published
  690. * post is deleted.
  691. *
  692. * @since 3.4.0
  693. *
  694. * @param int $post_id Post ID
  695. */
  696. function _update_blog_date_on_post_delete( $post_id ) {
  697. $post = get_post( $post_id );
  698. $post_type_obj = get_post_type_object( $post->post_type );
  699. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  700. return;
  701. }
  702. if ( 'publish' != $post->post_status ) {
  703. return;
  704. }
  705. wpmu_update_blogs_date();
  706. }
  707. /**
  708. * Handler for updating the current site's posts count when a post is deleted.
  709. *
  710. * @since 4.0.0
  711. *
  712. * @param int $post_id Post ID.
  713. */
  714. function _update_posts_count_on_delete( $post_id ) {
  715. $post = get_post( $post_id );
  716. if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
  717. return;
  718. }
  719. update_posts_count();
  720. }
  721. /**
  722. * Handler for updating the current site's posts count when a post status changes.
  723. *
  724. * @since 4.0.0
  725. * @since 4.9.0 Added the `$post` parameter.
  726. *
  727. * @param string $new_status The status the post is changing to.
  728. * @param string $old_status The status the post is changing from.
  729. * @param WP_Post $post Post object
  730. */
  731. function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
  732. if ( $new_status === $old_status ) {
  733. return;
  734. }
  735. if ( 'post' !== get_post_type( $post ) ) {
  736. return;
  737. }
  738. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  739. return;
  740. }
  741. update_posts_count();
  742. }
  743. /**
  744. * Count number of sites grouped by site status.
  745. *
  746. * @since 5.3.0
  747. *
  748. * @param int $network_id The network to get counts for. Default is the current network id.
  749. * @return array Includes a grand total 'all' and an array of counts indexed by
  750. * status strings: public, archived, mature, spam, deleted.
  751. */
  752. function wp_count_sites( $network_id = null ) {
  753. if ( empty( $network_id ) ) {
  754. $network_id = get_current_network_id();
  755. }
  756. $counts = array();
  757. $args = array(
  758. 'network_id' => $network_id,
  759. 'number' => 1,
  760. 'fields' => 'ids',
  761. 'no_found_rows' => false,
  762. );
  763. $q = new WP_Site_Query( $args );
  764. $counts['all'] = $q->found_sites;
  765. $_args = $args;
  766. $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
  767. foreach ( $statuses as $status ) {
  768. $_args = $args;
  769. $_args[ $status ] = 1;
  770. $q = new WP_Site_Query( $_args );
  771. $counts[ $status ] = $q->found_sites;
  772. }
  773. return $counts;
  774. }