PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/ms-blogs.php

https://gitlab.com/Gashler/dp
PHP | 783 lines | 420 code | 118 blank | 245 comment | 130 complexity | f07a63133cde94eb6a0b6c8fbce4c517 MD5 | raw file
  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
  8. */
  9. /**
  10. * Update the last_updated field for the current blog.
  11. *
  12. * @since MU
  13. */
  14. function wpmu_update_blogs_date() {
  15. global $wpdb;
  16. update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
  17. do_action( 'wpmu_blog_updated', $wpdb->blogid );
  18. }
  19. /**
  20. * Get a full blog URL, given a blog id.
  21. *
  22. * @since MU
  23. *
  24. * @param int $blog_id Blog ID
  25. * @return string
  26. */
  27. function get_blogaddress_by_id( $blog_id ) {
  28. $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
  29. return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
  30. }
  31. /**
  32. * Get a full blog URL, given a blog name.
  33. *
  34. * @since MU
  35. *
  36. * @param string $blogname The (subdomain or directory) name
  37. * @return string
  38. */
  39. function get_blogaddress_by_name( $blogname ) {
  40. if ( is_subdomain_install() ) {
  41. if ( $blogname == 'main' )
  42. $blogname = 'www';
  43. $url = rtrim( network_home_url(), '/' );
  44. if ( !empty( $blogname ) )
  45. $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
  46. } else {
  47. $url = network_home_url( $blogname );
  48. }
  49. return esc_url( $url . '/' );
  50. }
  51. /**
  52. * Get a full blog URL, given a domain and a path.
  53. *
  54. * @since MU
  55. *
  56. * @param string $domain
  57. * @param string $path
  58. * @return string
  59. */
  60. function get_blogaddress_by_domain( $domain, $path ) {
  61. if ( is_subdomain_install() ) {
  62. $url = "http://" . $domain.$path;
  63. } else {
  64. if ( $domain != $_SERVER['HTTP_HOST'] ) {
  65. $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
  66. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
  67. // we're not installing the main blog
  68. if ( $blogname != 'www.' )
  69. $url .= $blogname . '/';
  70. } else { // main blog
  71. $url = 'http://' . $domain . $path;
  72. }
  73. }
  74. return esc_url( $url );
  75. }
  76. /**
  77. * Given a blog's (subdomain or directory) slug, retrieve its id.
  78. *
  79. * @since MU
  80. *
  81. * @param string $slug
  82. * @return int A blog id
  83. */
  84. function get_id_from_blogname( $slug ) {
  85. global $wpdb, $current_site;
  86. $slug = trim( $slug, '/' );
  87. $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
  88. if ( $blog_id )
  89. return $blog_id;
  90. if ( is_subdomain_install() ) {
  91. $domain = $slug . '.' . $current_site->domain;
  92. $path = $current_site->path;
  93. } else {
  94. $domain = $current_site->domain;
  95. $path = $current_site->path . $slug . '/';
  96. }
  97. $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
  98. wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
  99. return $blog_id;
  100. }
  101. /**
  102. * Retrieve the details for a blog from the blogs table and blog options.
  103. *
  104. * @since MU
  105. *
  106. * @param int|string|array $fields A blog ID, a blog slug, or an array of fields to query against. Optional. If not specified the current blog ID is used.
  107. * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
  108. * @return object Blog details.
  109. */
  110. function get_blog_details( $fields = null, $get_all = true ) {
  111. global $wpdb;
  112. if ( is_array($fields ) ) {
  113. if ( isset($fields['blog_id']) ) {
  114. $blog_id = $fields['blog_id'];
  115. } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
  116. $key = md5( $fields['domain'] . $fields['path'] );
  117. $blog = wp_cache_get($key, 'blog-lookup');
  118. if ( false !== $blog )
  119. return $blog;
  120. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  121. $nowww = substr( $fields['domain'], 4 );
  122. $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'] ) );
  123. } else {
  124. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
  125. }
  126. if ( $blog ) {
  127. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  128. $blog_id = $blog->blog_id;
  129. } else {
  130. return false;
  131. }
  132. } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
  133. $key = md5( $fields['domain'] );
  134. $blog = wp_cache_get($key, 'blog-lookup');
  135. if ( false !== $blog )
  136. return $blog;
  137. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  138. $nowww = substr( $fields['domain'], 4 );
  139. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
  140. } else {
  141. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
  142. }
  143. if ( $blog ) {
  144. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  145. $blog_id = $blog->blog_id;
  146. } else {
  147. return false;
  148. }
  149. } else {
  150. return false;
  151. }
  152. } else {
  153. if ( ! $fields )
  154. $blog_id = get_current_blog_id();
  155. elseif ( ! is_numeric( $fields ) )
  156. $blog_id = get_id_from_blogname( $fields );
  157. else
  158. $blog_id = $fields;
  159. }
  160. $blog_id = (int) $blog_id;
  161. $all = $get_all == true ? '' : 'short';
  162. $details = wp_cache_get( $blog_id . $all, 'blog-details' );
  163. if ( $details ) {
  164. if ( ! is_object( $details ) ) {
  165. if ( $details == -1 ) {
  166. return false;
  167. } else {
  168. // Clear old pre-serialized objects. Cache clients do better with that.
  169. wp_cache_delete( $blog_id . $all, 'blog-details' );
  170. unset($details);
  171. }
  172. } else {
  173. return $details;
  174. }
  175. }
  176. // Try the other cache.
  177. if ( $get_all ) {
  178. $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
  179. } else {
  180. $details = wp_cache_get( $blog_id, 'blog-details' );
  181. // If short was requested and full cache is set, we can return.
  182. if ( $details ) {
  183. if ( ! is_object( $details ) ) {
  184. if ( $details == -1 ) {
  185. return false;
  186. } else {
  187. // Clear old pre-serialized objects. Cache clients do better with that.
  188. wp_cache_delete( $blog_id, 'blog-details' );
  189. unset($details);
  190. }
  191. } else {
  192. return $details;
  193. }
  194. }
  195. }
  196. if ( empty($details) ) {
  197. $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
  198. if ( ! $details ) {
  199. // Set the full cache.
  200. wp_cache_set( $blog_id, -1, 'blog-details' );
  201. return false;
  202. }
  203. }
  204. if ( ! $get_all ) {
  205. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  206. return $details;
  207. }
  208. switch_to_blog( $blog_id );
  209. $details->blogname = get_option( 'blogname' );
  210. $details->siteurl = get_option( 'siteurl' );
  211. $details->post_count = get_option( 'post_count' );
  212. restore_current_blog();
  213. $details = apply_filters( 'blog_details', $details );
  214. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  215. $key = md5( $details->domain . $details->path );
  216. wp_cache_set( $key, $details, 'blog-lookup' );
  217. return $details;
  218. }
  219. /**
  220. * Clear the blog details cache.
  221. *
  222. * @since MU
  223. *
  224. * @param int $blog_id Blog ID
  225. */
  226. function refresh_blog_details( $blog_id ) {
  227. $blog_id = (int) $blog_id;
  228. $details = get_blog_details( $blog_id, false );
  229. if ( ! $details ) {
  230. // Make sure clean_blog_cache() gets the blog ID
  231. // when the blog has been previously cached as
  232. // non-existent.
  233. $details = (object) array(
  234. 'blog_id' => $blog_id,
  235. 'domain' => null,
  236. 'path' => null
  237. );
  238. }
  239. clean_blog_cache( $details );
  240. do_action( 'refresh_blog_details', $blog_id );
  241. }
  242. /**
  243. * Update the details for a blog. Updates the blogs table for a given blog id.
  244. *
  245. * @since MU
  246. *
  247. * @param int $blog_id Blog ID
  248. * @param array $details Array of details keyed by blogs table field names.
  249. * @return bool True if update succeeds, false otherwise.
  250. */
  251. function update_blog_details( $blog_id, $details = array() ) {
  252. global $wpdb;
  253. if ( empty($details) )
  254. return false;
  255. if ( is_object($details) )
  256. $details = get_object_vars($details);
  257. $current_details = get_blog_details($blog_id, false);
  258. if ( empty($current_details) )
  259. return false;
  260. $current_details = get_object_vars($current_details);
  261. $details = array_merge($current_details, $details);
  262. $details['last_updated'] = current_time('mysql', true);
  263. $update_details = array();
  264. $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
  265. foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
  266. $update_details[$field] = $details[$field];
  267. $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
  268. if ( false === $result )
  269. return false;
  270. // If spam status changed, issue actions.
  271. if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
  272. if ( $details[ 'spam' ] == 1 )
  273. do_action( 'make_spam_blog', $blog_id );
  274. else
  275. do_action( 'make_ham_blog', $blog_id );
  276. }
  277. // If mature status changed, issue actions.
  278. if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) {
  279. if ( $details[ 'mature' ] == 1 )
  280. do_action( 'mature_blog', $blog_id );
  281. else
  282. do_action( 'unmature_blog', $blog_id );
  283. }
  284. // If archived status changed, issue actions.
  285. if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) {
  286. if ( $details[ 'archived' ] == 1 )
  287. do_action( 'archive_blog', $blog_id );
  288. else
  289. do_action( 'unarchive_blog', $blog_id );
  290. }
  291. // If deleted status changed, issue actions.
  292. if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) {
  293. if ( $details[ 'deleted' ] == 1 )
  294. do_action( 'make_delete_blog', $blog_id );
  295. else
  296. do_action( 'make_undelete_blog', $blog_id );
  297. }
  298. if ( isset( $details[ 'public' ] ) ) {
  299. switch_to_blog( $blog_id );
  300. update_option( 'blog_public', $details[ 'public' ] );
  301. restore_current_blog();
  302. }
  303. refresh_blog_details($blog_id);
  304. return true;
  305. }
  306. /**
  307. * Clean the blog cache
  308. *
  309. * @since 3.5.0
  310. *
  311. * @param stdClass $blog The blog details as returned from get_blog_details()
  312. */
  313. function clean_blog_cache( $blog ) {
  314. $blog_id = $blog->blog_id;
  315. $domain_path_key = md5( $blog->domain . $blog->path );
  316. wp_cache_delete( $blog_id , 'blog-details' );
  317. wp_cache_delete( $blog_id . 'short' , 'blog-details' );
  318. wp_cache_delete( $domain_path_key, 'blog-lookup' );
  319. wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
  320. wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
  321. wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
  322. wp_cache_delete( $domain_path_key, 'blog-id-cache' );
  323. }
  324. /**
  325. * Retrieve option value for a given blog id based on name of option.
  326. *
  327. * If the option does not exist or does not have a value, then the return value
  328. * will be false. This is useful to check whether you need to install an option
  329. * and is commonly used during installation of plugin options and to test
  330. * whether upgrading is required.
  331. *
  332. * If the option was serialized then it will be unserialized when it is returned.
  333. *
  334. * @since MU
  335. *
  336. * @param int $id A blog ID. Can be null to refer to the current blog.
  337. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  338. * @param mixed $default Optional. Default value to return if the option does not exist.
  339. * @return mixed Value set for the option.
  340. */
  341. function get_blog_option( $id, $option, $default = false ) {
  342. $id = (int) $id;
  343. if ( empty( $id ) )
  344. $id = get_current_blog_id();
  345. if ( get_current_blog_id() == $id )
  346. return get_option( $option, $default );
  347. switch_to_blog( $id );
  348. $value = get_option( $option, $default );
  349. restore_current_blog();
  350. return apply_filters( 'blog_option_' . $option, $value, $id );
  351. }
  352. /**
  353. * Add a new option for a given blog id.
  354. *
  355. * You do not need to serialize values. If the value needs to be serialized, then
  356. * it will be serialized before it is inserted into the database. Remember,
  357. * resources can not be serialized or added as an option.
  358. *
  359. * You can create options without values and then update the values later.
  360. * Existing options will not be updated and checks are performed to ensure that you
  361. * aren't adding a protected WordPress option. Care should be taken to not name
  362. * options the same as the ones which are protected.
  363. *
  364. * @since MU
  365. *
  366. * @param int $id A blog ID. Can be null to refer to the current blog.
  367. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  368. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  369. * @return bool False if option was not added and true if option was added.
  370. */
  371. function add_blog_option( $id, $option, $value ) {
  372. $id = (int) $id;
  373. if ( empty( $id ) )
  374. $id = get_current_blog_id();
  375. if ( get_current_blog_id() == $id )
  376. return add_option( $option, $value );
  377. switch_to_blog( $id );
  378. $return = add_option( $option, $value );
  379. restore_current_blog();
  380. return $return;
  381. }
  382. /**
  383. * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
  384. *
  385. * @since MU
  386. *
  387. * @param int $id A blog ID. Can be null to refer to the current blog.
  388. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  389. * @return bool True, if option is successfully deleted. False on failure.
  390. */
  391. function delete_blog_option( $id, $option ) {
  392. $id = (int) $id;
  393. if ( empty( $id ) )
  394. $id = get_current_blog_id();
  395. if ( get_current_blog_id() == $id )
  396. return delete_option( $option );
  397. switch_to_blog( $id );
  398. $return = delete_option( $option );
  399. restore_current_blog();
  400. return $return;
  401. }
  402. /**
  403. * Update an option for a particular blog.
  404. *
  405. * @since MU
  406. *
  407. * @param int $id The blog id
  408. * @param string $option The option key
  409. * @param mixed $value The option value
  410. * @return bool True on success, false on failure.
  411. */
  412. function update_blog_option( $id, $option, $value, $deprecated = null ) {
  413. $id = (int) $id;
  414. if ( null !== $deprecated )
  415. _deprecated_argument( __FUNCTION__, '3.1' );
  416. if ( get_current_blog_id() == $id )
  417. return update_option( $option, $value );
  418. switch_to_blog( $id );
  419. $return = update_option( $option, $value );
  420. restore_current_blog();
  421. refresh_blog_details( $id );
  422. return $return;
  423. }
  424. /**
  425. * Switch the current blog.
  426. *
  427. * This function is useful if you need to pull posts, or other information,
  428. * from other blogs. You can switch back afterwards using restore_current_blog().
  429. *
  430. * Things that aren't switched:
  431. * - autoloaded options. See #14992
  432. * - plugins. See #14941
  433. *
  434. * @see restore_current_blog()
  435. * @since MU
  436. *
  437. * @param int $new_blog The id of the blog you want to switch to. Default: current blog
  438. * @param bool $deprecated Deprecated argument
  439. * @return bool True on success, false if the validation failed
  440. */
  441. function switch_to_blog( $new_blog, $deprecated = null ) {
  442. global $wpdb, $wp_roles;
  443. if ( empty( $new_blog ) )
  444. $new_blog = $GLOBALS['blog_id'];
  445. $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
  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. if ( $new_blog == $GLOBALS['blog_id'] ) {
  450. do_action( 'switch_blog', $new_blog, $new_blog );
  451. $GLOBALS['switched'] = true;
  452. return true;
  453. }
  454. $wpdb->set_blog_id( $new_blog );
  455. $GLOBALS['table_prefix'] = $wpdb->prefix;
  456. $prev_blog_id = $GLOBALS['blog_id'];
  457. $GLOBALS['blog_id'] = $new_blog;
  458. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  459. wp_cache_switch_to_blog( $new_blog );
  460. } else {
  461. global $wp_object_cache;
  462. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  463. $global_groups = $wp_object_cache->global_groups;
  464. else
  465. $global_groups = false;
  466. wp_cache_init();
  467. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  468. if ( is_array( $global_groups ) )
  469. wp_cache_add_global_groups( $global_groups );
  470. else
  471. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
  472. wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
  473. }
  474. }
  475. if ( did_action( 'init' ) ) {
  476. $wp_roles->reinit();
  477. $current_user = wp_get_current_user();
  478. $current_user->for_blog( $new_blog );
  479. }
  480. do_action( 'switch_blog', $new_blog, $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
  489. *
  490. * @return bool True on success, false if we're already on the current blog
  491. */
  492. function restore_current_blog() {
  493. global $wpdb, $wp_roles;
  494. if ( empty( $GLOBALS['_wp_switched_stack'] ) )
  495. return false;
  496. $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
  497. if ( $GLOBALS['blog_id'] == $blog ) {
  498. do_action( 'switch_blog', $blog, $blog );
  499. // If we still have items in the switched stack, consider ourselves still 'switched'
  500. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  501. return true;
  502. }
  503. $wpdb->set_blog_id( $blog );
  504. $prev_blog_id = $GLOBALS['blog_id'];
  505. $GLOBALS['blog_id'] = $blog;
  506. $GLOBALS['table_prefix'] = $wpdb->prefix;
  507. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  508. wp_cache_switch_to_blog( $blog );
  509. } else {
  510. global $wp_object_cache;
  511. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  512. $global_groups = $wp_object_cache->global_groups;
  513. else
  514. $global_groups = false;
  515. wp_cache_init();
  516. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  517. if ( is_array( $global_groups ) )
  518. wp_cache_add_global_groups( $global_groups );
  519. else
  520. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', ' blog-id-cache' ) );
  521. wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
  522. }
  523. }
  524. if ( did_action( 'init' ) ) {
  525. $wp_roles->reinit();
  526. $current_user = wp_get_current_user();
  527. $current_user->for_blog( $blog );
  528. }
  529. do_action( 'switch_blog', $blog, $prev_blog_id );
  530. // If we still have items in the switched stack, consider ourselves still 'switched'
  531. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  532. return true;
  533. }
  534. /**
  535. * Determines if switch_to_blog() is in effect
  536. *
  537. * @since 3.5.0
  538. *
  539. * @return bool True if switched, false otherwise.
  540. */
  541. function ms_is_switched() {
  542. return ! empty( $GLOBALS['_wp_switched_stack'] );
  543. }
  544. /**
  545. * Check if a particular blog is archived.
  546. *
  547. * @since MU
  548. *
  549. * @param int $id The blog id
  550. * @return string Whether the blog is archived or not
  551. */
  552. function is_archived( $id ) {
  553. return get_blog_status($id, 'archived');
  554. }
  555. /**
  556. * Update the 'archived' status of a particular blog.
  557. *
  558. * @since MU
  559. *
  560. * @param int $id The blog id
  561. * @param string $archived The new status
  562. * @return string $archived
  563. */
  564. function update_archived( $id, $archived ) {
  565. update_blog_status($id, 'archived', $archived);
  566. return $archived;
  567. }
  568. /**
  569. * Update a blog details field.
  570. *
  571. * @since MU
  572. *
  573. * @param int $blog_id BLog ID
  574. * @param string $pref A field name
  575. * @param string $value Value for $pref
  576. * @return string $value
  577. */
  578. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  579. global $wpdb;
  580. if ( null !== $deprecated )
  581. _deprecated_argument( __FUNCTION__, '3.1' );
  582. if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  583. return $value;
  584. $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
  585. if ( false === $result )
  586. return false;
  587. refresh_blog_details( $blog_id );
  588. if ( 'spam' == $pref )
  589. ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
  590. elseif ( 'mature' == $pref )
  591. ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
  592. elseif ( 'archived' == $pref )
  593. ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
  594. elseif ( 'deleted' == $pref )
  595. ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id );
  596. elseif ( 'public' == $pref )
  597. do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
  598. return $value;
  599. }
  600. /**
  601. * Get a blog details field.
  602. *
  603. * @since MU
  604. *
  605. * @param int $id The blog id
  606. * @param string $pref A field name
  607. * @return bool $value
  608. */
  609. function get_blog_status( $id, $pref ) {
  610. global $wpdb;
  611. $details = get_blog_details( $id, false );
  612. if ( $details )
  613. return $details->$pref;
  614. return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
  615. }
  616. /**
  617. * Get a list of most recently updated blogs.
  618. *
  619. * @since MU
  620. *
  621. * @param mixed $deprecated Not used
  622. * @param int $start The offset
  623. * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
  624. * @return array The list of blogs
  625. */
  626. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  627. global $wpdb;
  628. if ( ! empty( $deprecated ) )
  629. _deprecated_argument( __FUNCTION__, 'MU' ); // never used
  630. 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", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
  631. }
  632. /**
  633. * Handler for updating the blog date when a post is published or an already published post is changed.
  634. *
  635. * @since 3.3.0
  636. *
  637. * @param string $new_status The new post status
  638. * @param string $old_status The old post status
  639. * @param object $post Post object
  640. */
  641. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  642. $post_type_obj = get_post_type_object( $post->post_type );
  643. if ( ! $post_type_obj->public )
  644. return;
  645. if ( 'publish' != $new_status && 'publish' != $old_status )
  646. return;
  647. // Post was freshly published, published post was saved, or published post was unpublished.
  648. wpmu_update_blogs_date();
  649. }
  650. /**
  651. * Handler for updating the blog date when a published post is deleted.
  652. *
  653. * @since 3.4.0
  654. *
  655. * @param int $post_id Post ID
  656. */
  657. function _update_blog_date_on_post_delete( $post_id ) {
  658. $post = get_post( $post_id );
  659. $post_type_obj = get_post_type_object( $post->post_type );
  660. if ( ! $post_type_obj->public )
  661. return;
  662. if ( 'publish' != $post->post_status )
  663. return;
  664. wpmu_update_blogs_date();
  665. }