Keine Beschreibung

ms-blogs.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 ( '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( 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' ) );
  484. }
  485. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  486. }
  487. }
  488. /** This filter is documented in wp-includes/ms-blogs.php */
  489. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
  490. $GLOBALS['switched'] = true;
  491. return true;
  492. }
  493. /**
  494. * Restore the current blog, after calling switch_to_blog().
  495. *
  496. * @see switch_to_blog()
  497. * @since MU (3.0.0)
  498. *
  499. * @global wpdb $wpdb WordPress database abstraction object.
  500. * @global array $_wp_switched_stack
  501. * @global int $blog_id
  502. * @global bool $switched
  503. * @global string $table_prefix
  504. * @global WP_Object_Cache $wp_object_cache
  505. *
  506. * @return bool True on success, false if we're already on the current blog.
  507. */
  508. function restore_current_blog() {
  509. global $wpdb;
  510. if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
  511. return false;
  512. }
  513. $new_blog_id = array_pop( $GLOBALS['_wp_switched_stack'] );
  514. $prev_blog_id = get_current_blog_id();
  515. if ( $new_blog_id == $prev_blog_id ) {
  516. /** This filter is documented in wp-includes/ms-blogs.php */
  517. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
  518. // If we still have items in the switched stack, consider ourselves still 'switched'.
  519. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  520. return true;
  521. }
  522. $wpdb->set_blog_id( $new_blog_id );
  523. $GLOBALS['blog_id'] = $new_blog_id;
  524. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  525. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  526. wp_cache_switch_to_blog( $new_blog_id );
  527. } else {
  528. global $wp_object_cache;
  529. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  530. $global_groups = $wp_object_cache->global_groups;
  531. } else {
  532. $global_groups = false;
  533. }
  534. wp_cache_init();
  535. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  536. if ( is_array( $global_groups ) ) {
  537. wp_cache_add_global_groups( $global_groups );
  538. } else {
  539. 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' ) );
  540. }
  541. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  542. }
  543. }
  544. /** This filter is documented in wp-includes/ms-blogs.php */
  545. do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
  546. // If we still have items in the switched stack, consider ourselves still 'switched'.
  547. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  548. return true;
  549. }
  550. /**
  551. * Switches the initialized roles and current user capabilities to another site.
  552. *
  553. * @since 4.9.0
  554. *
  555. * @param int $new_site_id New site ID.
  556. * @param int $old_site_id Old site ID.
  557. */
  558. function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
  559. if ( $new_site_id == $old_site_id ) {
  560. return;
  561. }
  562. if ( ! did_action( 'init' ) ) {
  563. return;
  564. }
  565. wp_roles()->for_site( $new_site_id );
  566. wp_get_current_user()->for_site( $new_site_id );
  567. }
  568. /**
  569. * Determines if switch_to_blog() is in effect
  570. *
  571. * @since 3.5.0
  572. *
  573. * @global array $_wp_switched_stack
  574. *
  575. * @return bool True if switched, false otherwise.
  576. */
  577. function ms_is_switched() {
  578. return ! empty( $GLOBALS['_wp_switched_stack'] );
  579. }
  580. /**
  581. * Check if a particular blog is archived.
  582. *
  583. * @since MU (3.0.0)
  584. *
  585. * @param int $id Blog ID.
  586. * @return string Whether the blog is archived or not.
  587. */
  588. function is_archived( $id ) {
  589. return get_blog_status( $id, 'archived' );
  590. }
  591. /**
  592. * Update the 'archived' status of a particular blog.
  593. *
  594. * @since MU (3.0.0)
  595. *
  596. * @param int $id Blog ID.
  597. * @param string $archived The new status.
  598. * @return string $archived
  599. */
  600. function update_archived( $id, $archived ) {
  601. update_blog_status( $id, 'archived', $archived );
  602. return $archived;
  603. }
  604. /**
  605. * Update a blog details field.
  606. *
  607. * @since MU (3.0.0)
  608. * @since 5.1.0 Use wp_update_site() internally.
  609. *
  610. * @global wpdb $wpdb WordPress database abstraction object.
  611. *
  612. * @param int $blog_id Blog ID.
  613. * @param string $pref Field name.
  614. * @param string $value Field value.
  615. * @param null $deprecated Not used.
  616. * @return string|false $value
  617. */
  618. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  619. global $wpdb;
  620. if ( null !== $deprecated ) {
  621. _deprecated_argument( __FUNCTION__, '3.1.0' );
  622. }
  623. $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  624. if ( ! in_array( $pref, $allowed_field_names, true ) ) {
  625. return $value;
  626. }
  627. $result = wp_update_site(
  628. $blog_id,
  629. array(
  630. $pref => $value,
  631. )
  632. );
  633. if ( is_wp_error( $result ) ) {
  634. return false;
  635. }
  636. return $value;
  637. }
  638. /**
  639. * Get a blog details field.
  640. *
  641. * @since MU (3.0.0)
  642. *
  643. * @global wpdb $wpdb WordPress database abstraction object.
  644. *
  645. * @param int $id Blog ID.
  646. * @param string $pref Field name.
  647. * @return bool|string|null $value
  648. */
  649. function get_blog_status( $id, $pref ) {
  650. global $wpdb;
  651. $details = get_site( $id );
  652. if ( $details ) {
  653. return $details->$pref;
  654. }
  655. return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
  656. }
  657. /**
  658. * Get a list of most recently updated blogs.
  659. *
  660. * @since MU (3.0.0)
  661. *
  662. * @global wpdb $wpdb WordPress database abstraction object.
  663. *
  664. * @param mixed $deprecated Not used.
  665. * @param int $start Optional. Number of blogs to offset the query. Used to build LIMIT clause.
  666. * Can be used for pagination. Default 0.
  667. * @param int $quantity Optional. The maximum number of blogs to retrieve. Default 40.
  668. * @return array The list of blogs.
  669. */
  670. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  671. global $wpdb;
  672. if ( ! empty( $deprecated ) ) {
  673. _deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
  674. }
  675. 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 );
  676. }
  677. /**
  678. * Handler for updating the site's last updated date when a post is published or
  679. * an already published post is changed.
  680. *
  681. * @since 3.3.0
  682. *
  683. * @param string $new_status The new post status.
  684. * @param string $old_status The old post status.
  685. * @param WP_Post $post Post object.
  686. */
  687. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  688. $post_type_obj = get_post_type_object( $post->post_type );
  689. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  690. return;
  691. }
  692. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  693. return;
  694. }
  695. // Post was freshly published, published post was saved, or published post was unpublished.
  696. wpmu_update_blogs_date();
  697. }
  698. /**
  699. * Handler for updating the current site's last updated date when a published
  700. * post is deleted.
  701. *
  702. * @since 3.4.0
  703. *
  704. * @param int $post_id Post ID
  705. */
  706. function _update_blog_date_on_post_delete( $post_id ) {
  707. $post = get_post( $post_id );
  708. $post_type_obj = get_post_type_object( $post->post_type );
  709. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  710. return;
  711. }
  712. if ( 'publish' !== $post->post_status ) {
  713. return;
  714. }
  715. wpmu_update_blogs_date();
  716. }
  717. /**
  718. * Handler for updating the current site's posts count when a post is deleted.
  719. *
  720. * @since 4.0.0
  721. *
  722. * @param int $post_id Post ID.
  723. */
  724. function _update_posts_count_on_delete( $post_id ) {
  725. $post = get_post( $post_id );
  726. if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
  727. return;
  728. }
  729. update_posts_count();
  730. }
  731. /**
  732. * Handler for updating the current site's posts count when a post status changes.
  733. *
  734. * @since 4.0.0
  735. * @since 4.9.0 Added the `$post` parameter.
  736. *
  737. * @param string $new_status The status the post is changing to.
  738. * @param string $old_status The status the post is changing from.
  739. * @param WP_Post $post Post object
  740. */
  741. function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
  742. if ( $new_status === $old_status ) {
  743. return;
  744. }
  745. if ( 'post' !== get_post_type( $post ) ) {
  746. return;
  747. }
  748. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  749. return;
  750. }
  751. update_posts_count();
  752. }
  753. /**
  754. * Count number of sites grouped by site status.
  755. *
  756. * @since 5.3.0
  757. *
  758. * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
  759. * @return int[] {
  760. * Numbers of sites grouped by site status.
  761. *
  762. * @type int $all The total number of sites.
  763. * @type int $public The number of public sites.
  764. * @type int $archived The number of archived sites.
  765. * @type int $mature The number of mature sites.
  766. * @type int $spam The number of spam sites.
  767. * @type int $deleted The number of deleted sites.
  768. * }
  769. */
  770. function wp_count_sites( $network_id = null ) {
  771. if ( empty( $network_id ) ) {
  772. $network_id = get_current_network_id();
  773. }
  774. $counts = array();
  775. $args = array(
  776. 'network_id' => $network_id,
  777. 'number' => 1,
  778. 'fields' => 'ids',
  779. 'no_found_rows' => false,
  780. );
  781. $q = new WP_Site_Query( $args );
  782. $counts['all'] = $q->found_sites;
  783. $_args = $args;
  784. $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
  785. foreach ( $statuses as $status ) {
  786. $_args = $args;
  787. $_args[ $status ] = 1;
  788. $q = new WP_Site_Query( $_args );
  789. $counts[ $status ] = $q->found_sites;
  790. }
  791. return $counts;
  792. }