PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/ms-blogs.php

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