PageRenderTime 50ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/ms-blogs.php

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