PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/ms-blogs.php

https://bitbucket.org/stratworkouts/wordpress
PHP | 733 lines | 413 code | 99 blank | 221 comment | 139 complexity | 2eae7dbb1d423a642309e7f45c02df29 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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) name, retrieve it's id.
  79. *
  80. * @since MU
  81. *
  82. * @param string $name
  83. * @return int A blog id
  84. */
  85. function get_id_from_blogname( $name ) {
  86. global $wpdb, $current_site;
  87. $blog_id = wp_cache_get( 'get_id_from_blogname_' . $name, 'blog-details' );
  88. if ( $blog_id )
  89. return $blog_id;
  90. if ( is_subdomain_install() ) {
  91. $domain = $name . '.' . $current_site->domain;
  92. $path = $current_site->path;
  93. } else {
  94. $domain = $current_site->domain;
  95. $path = $current_site->path . $name . '/';
  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_' . $name, $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 name, or an array of fields to query against.
  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, $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 ( !is_numeric( $fields ) )
  154. $blog_id = get_id_from_blogname( $fields );
  155. else
  156. $blog_id = $fields;
  157. }
  158. $blog_id = (int) $blog_id;
  159. $all = $get_all == true ? '' : 'short';
  160. $details = wp_cache_get( $blog_id . $all, 'blog-details' );
  161. if ( $details ) {
  162. if ( ! is_object( $details ) ) {
  163. if ( $details == -1 ) {
  164. return false;
  165. } else {
  166. // Clear old pre-serialized objects. Cache clients do better with that.
  167. wp_cache_delete( $blog_id . $all, 'blog-details' );
  168. unset($details);
  169. }
  170. } else {
  171. return $details;
  172. }
  173. }
  174. // Try the other cache.
  175. if ( $get_all ) {
  176. $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
  177. } else {
  178. $details = wp_cache_get( $blog_id, 'blog-details' );
  179. // If short was requested and full cache is set, we can return.
  180. if ( $details ) {
  181. if ( ! is_object( $details ) ) {
  182. if ( $details == -1 ) {
  183. return false;
  184. } else {
  185. // Clear old pre-serialized objects. Cache clients do better with that.
  186. wp_cache_delete( $blog_id, 'blog-details' );
  187. unset($details);
  188. }
  189. } else {
  190. return $details;
  191. }
  192. }
  193. }
  194. if ( empty($details) ) {
  195. $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
  196. if ( ! $details ) {
  197. // Set the full cache.
  198. wp_cache_set( $blog_id, -1, 'blog-details' );
  199. return false;
  200. }
  201. }
  202. if ( ! $get_all ) {
  203. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  204. return $details;
  205. }
  206. $details->blogname = get_blog_option( $blog_id, 'blogname' );
  207. $details->siteurl = get_blog_option( $blog_id, 'siteurl' );
  208. $details->post_count = get_blog_option( $blog_id, 'post_count' );
  209. $details = apply_filters( 'blog_details', $details );
  210. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  211. $key = md5( $details->domain . $details->path );
  212. wp_cache_set( $key, $details, 'blog-lookup' );
  213. return $details;
  214. }
  215. /**
  216. * Clear the blog details cache.
  217. *
  218. * @since MU
  219. *
  220. * @param int $blog_id Blog ID
  221. */
  222. function refresh_blog_details( $blog_id ) {
  223. $blog_id = (int) $blog_id;
  224. $details = get_blog_details( $blog_id, false );
  225. wp_cache_delete( $blog_id , 'blog-details' );
  226. wp_cache_delete( $blog_id . 'short' , 'blog-details' );
  227. wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
  228. wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
  229. wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
  230. do_action( 'refresh_blog_details', $blog_id );
  231. }
  232. /**
  233. * Update the details for a blog. Updates the blogs table for a given blog id.
  234. *
  235. * @since MU
  236. *
  237. * @param int $blog_id Blog ID
  238. * @param array $details Array of details keyed by blogs table field names.
  239. * @return bool True if update succeeds, false otherwise.
  240. */
  241. function update_blog_details( $blog_id, $details = array() ) {
  242. global $wpdb;
  243. if ( empty($details) )
  244. return false;
  245. if ( is_object($details) )
  246. $details = get_object_vars($details);
  247. $current_details = get_blog_details($blog_id, false);
  248. if ( empty($current_details) )
  249. return false;
  250. $current_details = get_object_vars($current_details);
  251. $details = array_merge($current_details, $details);
  252. $details['last_updated'] = current_time('mysql', true);
  253. $update_details = array();
  254. $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
  255. foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
  256. $update_details[$field] = $details[$field];
  257. $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
  258. // If spam status changed, issue actions.
  259. if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
  260. if ( $details[ 'spam' ] == 1 )
  261. do_action( "make_spam_blog", $blog_id );
  262. else
  263. do_action( "make_ham_blog", $blog_id );
  264. }
  265. if ( isset($details[ 'public' ]) )
  266. update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
  267. refresh_blog_details($blog_id);
  268. return true;
  269. }
  270. /**
  271. * Retrieve option value based on setting name and blog_id.
  272. *
  273. * If the option does not exist or does not have a value, then the return value
  274. * will be false. This is useful to check whether you need to install an option
  275. * and is commonly used during installation of plugin options and to test
  276. * whether upgrading is required.
  277. *
  278. * There is a filter called 'blog_option_$option' with the $option being
  279. * replaced with the option name. The filter takes two parameters. $value and
  280. * $blog_id. It returns $value.
  281. * The 'option_$option' filter in get_option() is not called.
  282. *
  283. * @since MU
  284. * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
  285. *
  286. * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog.
  287. * @param string $setting Name of option to retrieve. Should already be SQL-escaped.
  288. * @param string $default (optional) Default value returned if option not found.
  289. * @return mixed Value set for the option.
  290. */
  291. function get_blog_option( $blog_id, $setting, $default = false ) {
  292. global $wpdb;
  293. if ( null === $blog_id )
  294. $blog_id = $wpdb->blogid;
  295. $key = $blog_id . '-' . $setting . '-blog_option';
  296. $value = wp_cache_get( $key, 'site-options' );
  297. if ( $value == null ) {
  298. if ( $blog_id == $wpdb->blogid ) {
  299. $value = get_option( $setting, $default );
  300. $notoptions = wp_cache_get( 'notoptions', 'options' );
  301. if ( isset( $notoptions[$setting] ) ) {
  302. wp_cache_set( $key, 'noop', 'site-options' );
  303. $value = $default;
  304. } elseif ( $value == false ) {
  305. wp_cache_set( $key, 'falsevalue', 'site-options' );
  306. } else {
  307. wp_cache_set( $key, $value, 'site-options' );
  308. }
  309. return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
  310. } else {
  311. $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
  312. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
  313. if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  314. $value = $row->option_value;
  315. if ( $value == false )
  316. wp_cache_set( $key, 'falsevalue', 'site-options' );
  317. else
  318. wp_cache_set( $key, $value, 'site-options' );
  319. } else { // option does not exist, so we must cache its non-existence
  320. wp_cache_set( $key, 'noop', 'site-options' );
  321. $value = $default;
  322. }
  323. }
  324. } elseif ( $value == 'noop' ) {
  325. $value = $default;
  326. } elseif ( $value == 'falsevalue' ) {
  327. $value = false;
  328. }
  329. // If home is not set use siteurl.
  330. if ( 'home' == $setting && '' == $value )
  331. return get_blog_option( $blog_id, 'siteurl' );
  332. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  333. $value = untrailingslashit( $value );
  334. return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
  335. }
  336. /**
  337. * Add an option for a particular blog.
  338. *
  339. * @since MU
  340. *
  341. * @param int $id The blog id
  342. * @param string $key The option key
  343. * @param mixed $value The option value
  344. * @return bool True on success, false on failure.
  345. */
  346. function add_blog_option( $id, $key, $value ) {
  347. $id = (int) $id;
  348. switch_to_blog($id);
  349. $return = add_option( $key, $value );
  350. restore_current_blog();
  351. if ( $return )
  352. wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
  353. return $return;
  354. }
  355. /**
  356. * Delete an option for a particular blog.
  357. *
  358. * @since MU
  359. *
  360. * @param int $id The blog id
  361. * @param string $key The option key
  362. * @return bool True on success, false on failure.
  363. */
  364. function delete_blog_option( $id, $key ) {
  365. $id = (int) $id;
  366. switch_to_blog($id);
  367. $return = delete_option( $key );
  368. restore_current_blog();
  369. if ( $return )
  370. wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
  371. return $return;
  372. }
  373. /**
  374. * Update an option for a particular blog.
  375. *
  376. * @since MU
  377. *
  378. * @param int $id The blog id
  379. * @param string $key The option key
  380. * @param mixed $value The option value
  381. * @return bool True on success, false on failrue.
  382. */
  383. function update_blog_option( $id, $key, $value, $deprecated = null ) {
  384. $id = (int) $id;
  385. if ( null !== $deprecated )
  386. _deprecated_argument( __FUNCTION__, '3.1' );
  387. switch_to_blog($id);
  388. $return = update_option( $key, $value );
  389. restore_current_blog();
  390. refresh_blog_details( $id );
  391. if ( $return )
  392. wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');
  393. return $return;
  394. }
  395. /**
  396. * Switch the current blog.
  397. *
  398. * This function is useful if you need to pull posts, or other information,
  399. * from other blogs. You can switch back afterwards using restore_current_blog().
  400. *
  401. * Things that aren't switched:
  402. * - autoloaded options. See #14992
  403. * - plugins. See #14941
  404. *
  405. * @see restore_current_blog()
  406. * @since MU
  407. *
  408. * @param int $new_blog The id of the blog you want to switch to. Default: current blog
  409. * @param bool $validate Whether to check if $new_blog exists before proceeding
  410. * @return bool True on success, False if the validation failed
  411. */
  412. function switch_to_blog( $new_blog, $validate = false ) {
  413. global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
  414. if ( empty($new_blog) )
  415. $new_blog = $blog_id;
  416. if ( $validate && ! get_blog_details( $new_blog ) )
  417. return false;
  418. if ( empty($switched_stack) )
  419. $switched_stack = array();
  420. $switched_stack[] = $blog_id;
  421. /* If we're switching to the same blog id that we're on,
  422. * set the right vars, do the associated actions, but skip
  423. * the extra unnecessary work */
  424. if ( $blog_id == $new_blog ) {
  425. do_action( 'switch_blog', $blog_id, $blog_id );
  426. $switched = true;
  427. return true;
  428. }
  429. $wpdb->set_blog_id($new_blog);
  430. $table_prefix = $wpdb->prefix;
  431. $prev_blog_id = $blog_id;
  432. $blog_id = $new_blog;
  433. if ( is_object( $wp_roles ) ) {
  434. $wpdb->suppress_errors();
  435. if ( method_exists( $wp_roles ,'_init' ) )
  436. $wp_roles->_init();
  437. elseif ( method_exists( $wp_roles, '__construct' ) )
  438. $wp_roles->__construct();
  439. $wpdb->suppress_errors( false );
  440. }
  441. if ( did_action('init') ) {
  442. $current_user = wp_get_current_user();
  443. if ( is_object( $current_user ) )
  444. $current_user->for_blog( $blog_id );
  445. }
  446. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  447. $global_groups = $wp_object_cache->global_groups;
  448. else
  449. $global_groups = false;
  450. wp_cache_init();
  451. if ( function_exists('wp_cache_add_global_groups') ) {
  452. if ( is_array( $global_groups ) )
  453. wp_cache_add_global_groups( $global_groups );
  454. else
  455. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
  456. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  457. }
  458. do_action('switch_blog', $blog_id, $prev_blog_id);
  459. $switched = true;
  460. return true;
  461. }
  462. /**
  463. * Restore the current blog, after calling switch_to_blog()
  464. *
  465. * @see switch_to_blog()
  466. * @since MU
  467. *
  468. * @return bool True on success, False if we're already on the current blog
  469. */
  470. function restore_current_blog() {
  471. global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
  472. if ( !$switched )
  473. return false;
  474. if ( !is_array( $switched_stack ) )
  475. return false;
  476. $blog = array_pop( $switched_stack );
  477. if ( $blog_id == $blog ) {
  478. do_action( 'switch_blog', $blog, $blog );
  479. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  480. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  481. return true;
  482. }
  483. $wpdb->set_blog_id($blog);
  484. $prev_blog_id = $blog_id;
  485. $blog_id = $blog;
  486. $table_prefix = $wpdb->prefix;
  487. if ( is_object( $wp_roles ) ) {
  488. $wpdb->suppress_errors();
  489. if ( method_exists( $wp_roles ,'_init' ) )
  490. $wp_roles->_init();
  491. elseif ( method_exists( $wp_roles, '__construct' ) )
  492. $wp_roles->__construct();
  493. $wpdb->suppress_errors( false );
  494. }
  495. if ( did_action('init') ) {
  496. $current_user = wp_get_current_user();
  497. if ( is_object( $current_user ) )
  498. $current_user->for_blog( $blog_id );
  499. }
  500. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  501. $global_groups = $wp_object_cache->global_groups;
  502. else
  503. $global_groups = false;
  504. wp_cache_init();
  505. if ( function_exists('wp_cache_add_global_groups') ) {
  506. if ( is_array( $global_groups ) )
  507. wp_cache_add_global_groups( $global_groups );
  508. else
  509. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
  510. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  511. }
  512. do_action('switch_blog', $blog_id, $prev_blog_id);
  513. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  514. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  515. return true;
  516. }
  517. /**
  518. * Check if a particular blog is archived.
  519. *
  520. * @since MU
  521. *
  522. * @param int $id The blog id
  523. * @return string Whether the blog is archived or not
  524. */
  525. function is_archived( $id ) {
  526. return get_blog_status($id, 'archived');
  527. }
  528. /**
  529. * Update the 'archived' status of a particular blog.
  530. *
  531. * @since MU
  532. *
  533. * @param int $id The blog id
  534. * @param string $archived The new status
  535. * @return string $archived
  536. */
  537. function update_archived( $id, $archived ) {
  538. update_blog_status($id, 'archived', $archived);
  539. return $archived;
  540. }
  541. /**
  542. * Update a blog details field.
  543. *
  544. * @since MU
  545. *
  546. * @param int $blog_id BLog ID
  547. * @param string $pref A field name
  548. * @param string $value Value for $pref
  549. * @return string $value
  550. */
  551. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  552. global $wpdb;
  553. if ( null !== $deprecated )
  554. _deprecated_argument( __FUNCTION__, '3.1' );
  555. if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  556. return $value;
  557. $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
  558. refresh_blog_details($blog_id);
  559. if ( 'spam' == $pref )
  560. ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
  561. elseif ( 'mature' == $pref )
  562. ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
  563. elseif ( 'archived' == $pref )
  564. ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
  565. elseif ( 'archived' == $pref )
  566. ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
  567. return $value;
  568. }
  569. /**
  570. * Get a blog details field.
  571. *
  572. * @since MU
  573. *
  574. * @param int $id The blog id
  575. * @param string $pref A field name
  576. * @return bool $value
  577. */
  578. function get_blog_status( $id, $pref ) {
  579. global $wpdb;
  580. $details = get_blog_details( $id, false );
  581. if ( $details )
  582. return $details->$pref;
  583. return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
  584. }
  585. /**
  586. * Get a list of most recently updated blogs.
  587. *
  588. * @since MU
  589. *
  590. * @param mixed $deprecated Not used
  591. * @param int $start The offset
  592. * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
  593. * @return array The list of blogs
  594. */
  595. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  596. global $wpdb;
  597. if ( ! empty( $deprecated ) )
  598. _deprecated_argument( __FUNCTION__, 'MU' ); // never used
  599. 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 );
  600. }
  601. /**
  602. * Handler for updating the blog date when a post is published or an already published post is changed.
  603. *
  604. * @since 3.3.0
  605. *
  606. * @param string $new_status The new post status
  607. * @param string $old_status The old post status
  608. * @param object $post Post object
  609. */
  610. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  611. $post_type_obj = get_post_type_object( $post->post_type );
  612. if ( ! $post_type_obj->public )
  613. return;
  614. if ( 'publish' != $new_status && 'publish' != $old_status )
  615. return;
  616. // Post was freshly published, published post was saved, or published post was unpublished.
  617. wpmu_update_blogs_date();
  618. }
  619. /**
  620. * Handler for updating the blog date when a published post is deleted.
  621. *
  622. * @since 3.4.0
  623. *
  624. * @param int $post_id Post ID
  625. */
  626. function _update_blog_date_on_post_delete( $post_id ) {
  627. $post = get_post( $post_id );
  628. $post_type_obj = get_post_type_object( $post->post_type );
  629. if ( ! $post_type_obj->public )
  630. return;
  631. if ( 'publish' != $post->post_status )
  632. return;
  633. wpmu_update_blogs_date();
  634. }