4 * Site/blog functions that work with the blogs table and related data.
7 * @subpackage Multisite
12 * Update the last_updated field for the current blog.
16 function wpmu_update_blogs_date() {
19 update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
21 * Fires after the blog details are updated.
25 * @param int $blog_id Blog ID.
27 do_action( 'wpmu_blog_updated', $wpdb->blogid );
31 * Get a full blog URL, given a blog id.
35 * @param int $blog_id Blog ID
38 function get_blogaddress_by_id( $blog_id ) {
39 $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
40 return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
44 * Get a full blog URL, given a blog name.
48 * @param string $blogname The (subdomain or directory) name
51 function get_blogaddress_by_name( $blogname ) {
52 if ( is_subdomain_install() ) {
53 if ( $blogname == 'main' )
55 $url = rtrim( network_home_url(), '/' );
56 if ( !empty( $blogname ) )
57 $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
59 $url = network_home_url( $blogname );
61 return esc_url( $url . '/' );
65 * Given a blog's (subdomain or directory) slug, retrieve its id.
70 * @return int A blog id
72 function get_id_from_blogname( $slug ) {
75 $current_site = get_current_site();
76 $slug = trim( $slug, '/' );
78 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
82 if ( is_subdomain_install() ) {
83 $domain = $slug . '.' . $current_site->domain;
84 $path = $current_site->path;
86 $domain = $current_site->domain;
87 $path = $current_site->path . $slug . '/';
90 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
91 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
96 * Retrieve the details for a blog from the blogs table and blog options.
100 * @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.
101 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
102 * @return object Blog details.
104 function get_blog_details( $fields = null, $get_all = true ) {
107 if ( is_array($fields ) ) {
108 if ( isset($fields['blog_id']) ) {
109 $blog_id = $fields['blog_id'];
110 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
111 $key = md5( $fields['domain'] . $fields['path'] );
112 $blog = wp_cache_get($key, 'blog-lookup');
113 if ( false !== $blog )
115 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
116 $nowww = substr( $fields['domain'], 4 );
117 $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'] ) );
119 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
122 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
123 $blog_id = $blog->blog_id;
127 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
128 $key = md5( $fields['domain'] );
129 $blog = wp_cache_get($key, 'blog-lookup');
130 if ( false !== $blog )
132 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
133 $nowww = substr( $fields['domain'], 4 );
134 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
136 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
139 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
140 $blog_id = $blog->blog_id;
149 $blog_id = get_current_blog_id();
150 elseif ( ! is_numeric( $fields ) )
151 $blog_id = get_id_from_blogname( $fields );
156 $blog_id = (int) $blog_id;
158 $all = $get_all == true ? '' : 'short';
159 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
162 if ( ! is_object( $details ) ) {
163 if ( $details == -1 ) {
166 // Clear old pre-serialized objects. Cache clients do better with that.
167 wp_cache_delete( $blog_id . $all, 'blog-details' );
175 // Try the other cache.
177 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
179 $details = wp_cache_get( $blog_id, 'blog-details' );
180 // If short was requested and full cache is set, we can return.
182 if ( ! is_object( $details ) ) {
183 if ( $details == -1 ) {
186 // Clear old pre-serialized objects. Cache clients do better with that.
187 wp_cache_delete( $blog_id, 'blog-details' );
196 if ( empty($details) ) {
197 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
199 // Set the full cache.
200 wp_cache_set( $blog_id, -1, 'blog-details' );
206 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
210 switch_to_blog( $blog_id );
211 $details->blogname = get_option( 'blogname' );
212 $details->siteurl = get_option( 'siteurl' );
213 $details->post_count = get_option( 'post_count' );
214 restore_current_blog();
217 * Filter a blog's details.
221 * @param object $details The blog details.
223 $details = apply_filters( 'blog_details', $details );
225 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
227 $key = md5( $details->domain . $details->path );
228 wp_cache_set( $key, $details, 'blog-lookup' );
234 * Clear the blog details cache.
238 * @param int $blog_id Optional. Blog ID. Defaults to current blog.
240 function refresh_blog_details( $blog_id = 0 ) {
241 $blog_id = (int) $blog_id;
243 $blog_id = get_current_blog_id();
246 $details = get_blog_details( $blog_id, false );
248 // Make sure clean_blog_cache() gets the blog ID
249 // when the blog has been previously cached as
251 $details = (object) array(
252 'blog_id' => $blog_id,
258 clean_blog_cache( $details );
261 * Fires after the blog details cache is cleared.
265 * @param int $blog_id Blog ID.
267 do_action( 'refresh_blog_details', $blog_id );
271 * Update the details for a blog. Updates the blogs table for a given blog id.
275 * @param int $blog_id Blog ID
276 * @param array $details Array of details keyed by blogs table field names.
277 * @return bool True if update succeeds, false otherwise.
279 function update_blog_details( $blog_id, $details = array() ) {
282 if ( empty($details) )
285 if ( is_object($details) )
286 $details = get_object_vars($details);
288 $current_details = get_blog_details($blog_id, false);
289 if ( empty($current_details) )
292 $current_details = get_object_vars($current_details);
294 $details = array_merge($current_details, $details);
295 $details['last_updated'] = current_time('mysql', true);
297 $update_details = array();
298 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
299 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
300 $update_details[$field] = $details[$field];
302 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
304 if ( false === $result )
307 // If spam status changed, issue actions.
308 if ( $details['spam'] != $current_details['spam'] ) {
309 if ( $details['spam'] == 1 ) {
311 * Fires when the blog status is changed to 'spam'.
315 * @param int $blog_id Blog ID.
317 do_action( 'make_spam_blog', $blog_id );
320 * Fires when the blog status is changed to 'ham'.
324 * @param int $blog_id Blog ID.
326 do_action( 'make_ham_blog', $blog_id );
330 // If mature status changed, issue actions.
331 if ( $details['mature'] != $current_details['mature'] ) {
332 if ( $details['mature'] == 1 ) {
334 * Fires when the blog status is changed to 'mature'.
338 * @param int $blog_id Blog ID.
340 do_action( 'mature_blog', $blog_id );
343 * Fires when the blog status is changed to 'unmature'.
347 * @param int $blog_id Blog ID.
349 do_action( 'unmature_blog', $blog_id );
353 // If archived status changed, issue actions.
354 if ( $details['archived'] != $current_details['archived'] ) {
355 if ( $details['archived'] == 1 ) {
357 * Fires when the blog status is changed to 'archived'.
361 * @param int $blog_id Blog ID.
363 do_action( 'archive_blog', $blog_id );
366 * Fires when the blog status is changed to 'unarchived'.
370 * @param int $blog_id Blog ID.
372 do_action( 'unarchive_blog', $blog_id );
376 // If deleted status changed, issue actions.
377 if ( $details['deleted'] != $current_details['deleted'] ) {
378 if ( $details['deleted'] == 1 ) {
380 * Fires when the blog status is changed to 'deleted'.
384 * @param int $blog_id Blog ID.
386 do_action( 'make_delete_blog', $blog_id );
389 * Fires when the blog status is changed to 'undeleted'.
393 * @param int $blog_id Blog ID.
395 do_action( 'make_undelete_blog', $blog_id );
399 if ( isset( $details['public'] ) ) {
400 switch_to_blog( $blog_id );
401 update_option( 'blog_public', $details['public'] );
402 restore_current_blog();
405 refresh_blog_details($blog_id);
411 * Clean the blog cache
415 * @param stdClass $blog The blog details as returned from get_blog_details()
417 function clean_blog_cache( $blog ) {
418 $blog_id = $blog->blog_id;
419 $domain_path_key = md5( $blog->domain . $blog->path );
421 wp_cache_delete( $blog_id , 'blog-details' );
422 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
423 wp_cache_delete( $domain_path_key, 'blog-lookup' );
424 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
425 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
426 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
427 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
431 * Retrieve option value for a given blog id based on name of option.
433 * If the option does not exist or does not have a value, then the return value
434 * will be false. This is useful to check whether you need to install an option
435 * and is commonly used during installation of plugin options and to test
436 * whether upgrading is required.
438 * If the option was serialized then it will be unserialized when it is returned.
442 * @param int $id A blog ID. Can be null to refer to the current blog.
443 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
444 * @param mixed $default Optional. Default value to return if the option does not exist.
445 * @return mixed Value set for the option.
447 function get_blog_option( $id, $option, $default = false ) {
451 $id = get_current_blog_id();
453 if ( get_current_blog_id() == $id )
454 return get_option( $option, $default );
456 switch_to_blog( $id );
457 $value = get_option( $option, $default );
458 restore_current_blog();
461 * Filter a blog option value.
463 * The dynamic portion of the hook name, $option, refers to the blog option name.
467 * @param string $value The option value.
468 * @param int $id Blog ID.
470 return apply_filters( "blog_option_{$option}", $value, $id );
474 * Add a new option for a given blog id.
476 * You do not need to serialize values. If the value needs to be serialized, then
477 * it will be serialized before it is inserted into the database. Remember,
478 * resources can not be serialized or added as an option.
480 * You can create options without values and then update the values later.
481 * Existing options will not be updated and checks are performed to ensure that you
482 * aren't adding a protected WordPress option. Care should be taken to not name
483 * options the same as the ones which are protected.
487 * @param int $id A blog ID. Can be null to refer to the current blog.
488 * @param string $option Name of option to add. Expected to not be SQL-escaped.
489 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
490 * @return bool False if option was not added and true if option was added.
492 function add_blog_option( $id, $option, $value ) {
496 $id = get_current_blog_id();
498 if ( get_current_blog_id() == $id )
499 return add_option( $option, $value );
501 switch_to_blog( $id );
502 $return = add_option( $option, $value );
503 restore_current_blog();
509 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
513 * @param int $id A blog ID. Can be null to refer to the current blog.
514 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
515 * @return bool True, if option is successfully deleted. False on failure.
517 function delete_blog_option( $id, $option ) {
521 $id = get_current_blog_id();
523 if ( get_current_blog_id() == $id )
524 return delete_option( $option );
526 switch_to_blog( $id );
527 $return = delete_option( $option );
528 restore_current_blog();
534 * Update an option for a particular blog.
538 * @param int $id The blog id
539 * @param string $option The option key
540 * @param mixed $value The option value
541 * @return bool True on success, false on failure.
543 function update_blog_option( $id, $option, $value, $deprecated = null ) {
546 if ( null !== $deprecated )
547 _deprecated_argument( __FUNCTION__, '3.1' );
549 if ( get_current_blog_id() == $id )
550 return update_option( $option, $value );
552 switch_to_blog( $id );
553 $return = update_option( $option, $value );
554 restore_current_blog();
556 refresh_blog_details( $id );
562 * Switch the current blog.
564 * This function is useful if you need to pull posts, or other information,
565 * from other blogs. You can switch back afterwards using restore_current_blog().
567 * Things that aren't switched:
568 * - autoloaded options. See #14992
569 * - plugins. See #14941
571 * @see restore_current_blog()
574 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
575 * @param bool $deprecated Deprecated argument
576 * @return bool Always returns True.
578 function switch_to_blog( $new_blog, $deprecated = null ) {
579 global $wpdb, $wp_roles;
581 if ( empty( $new_blog ) )
582 $new_blog = $GLOBALS['blog_id'];
584 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
587 * If we're switching to the same blog id that we're on,
588 * set the right vars, do the associated actions, but skip
589 * the extra unnecessary work
591 if ( $new_blog == $GLOBALS['blog_id'] ) {
593 * Fires when the blog is switched.
597 * @param int $new_blog New blog ID.
598 * @param int $new_blog Blog ID.
600 do_action( 'switch_blog', $new_blog, $new_blog );
601 $GLOBALS['switched'] = true;
605 $wpdb->set_blog_id( $new_blog );
606 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
607 $prev_blog_id = $GLOBALS['blog_id'];
608 $GLOBALS['blog_id'] = $new_blog;
610 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
611 wp_cache_switch_to_blog( $new_blog );
613 global $wp_object_cache;
615 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
616 $global_groups = $wp_object_cache->global_groups;
618 $global_groups = false;
622 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
623 if ( is_array( $global_groups ) )
624 wp_cache_add_global_groups( $global_groups );
626 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' ) );
627 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
631 if ( did_action( 'init' ) ) {
633 $current_user = wp_get_current_user();
634 $current_user->for_blog( $new_blog );
637 /** This filter is documented in wp-includes/ms-blogs.php */
638 do_action( 'switch_blog', $new_blog, $prev_blog_id );
639 $GLOBALS['switched'] = true;
645 * Restore the current blog, after calling switch_to_blog()
647 * @see switch_to_blog()
650 * @return bool True on success, false if we're already on the current blog
652 function restore_current_blog() {
653 global $wpdb, $wp_roles;
655 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
658 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
660 if ( $GLOBALS['blog_id'] == $blog ) {
661 /** This filter is documented in wp-includes/ms-blogs.php */
662 do_action( 'switch_blog', $blog, $blog );
663 // If we still have items in the switched stack, consider ourselves still 'switched'
664 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
668 $wpdb->set_blog_id( $blog );
669 $prev_blog_id = $GLOBALS['blog_id'];
670 $GLOBALS['blog_id'] = $blog;
671 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
673 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
674 wp_cache_switch_to_blog( $blog );
676 global $wp_object_cache;
678 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
679 $global_groups = $wp_object_cache->global_groups;
681 $global_groups = false;
685 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
686 if ( is_array( $global_groups ) )
687 wp_cache_add_global_groups( $global_groups );
689 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' ) );
690 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
694 if ( did_action( 'init' ) ) {
696 $current_user = wp_get_current_user();
697 $current_user->for_blog( $blog );
700 /** This filter is documented in wp-includes/ms-blogs.php */
701 do_action( 'switch_blog', $blog, $prev_blog_id );
703 // If we still have items in the switched stack, consider ourselves still 'switched'
704 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
710 * Determines if switch_to_blog() is in effect
714 * @return bool True if switched, false otherwise.
716 function ms_is_switched() {
717 return ! empty( $GLOBALS['_wp_switched_stack'] );
721 * Check if a particular blog is archived.
725 * @param int $id The blog id
726 * @return string Whether the blog is archived or not
728 function is_archived( $id ) {
729 return get_blog_status($id, 'archived');
733 * Update the 'archived' status of a particular blog.
737 * @param int $id The blog id
738 * @param string $archived The new status
739 * @return string $archived
741 function update_archived( $id, $archived ) {
742 update_blog_status($id, 'archived', $archived);
747 * Update a blog details field.
751 * @param int $blog_id BLog ID
752 * @param string $pref A field name
753 * @param string $value Value for $pref
754 * @return string $value
756 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
759 if ( null !== $deprecated )
760 _deprecated_argument( __FUNCTION__, '3.1' );
762 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
765 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
767 if ( false === $result )
770 refresh_blog_details( $blog_id );
772 if ( 'spam' == $pref ) {
774 /** This filter is documented in wp-includes/ms-blogs.php */
775 do_action( 'make_spam_blog', $blog_id );
777 /** This filter is documented in wp-includes/ms-blogs.php */
778 do_action( 'make_ham_blog', $blog_id );
780 } elseif ( 'mature' == $pref ) {
782 /** This filter is documented in wp-includes/ms-blogs.php */
783 do_action( 'mature_blog', $blog_id );
785 /** This filter is documented in wp-includes/ms-blogs.php */
786 do_action( 'unmature_blog', $blog_id );
788 } elseif ( 'archived' == $pref ) {
790 /** This filter is documented in wp-includes/ms-blogs.php */
791 do_action( 'archive_blog', $blog_id );
793 /** This filter is documented in wp-includes/ms-blogs.php */
794 do_action( 'unarchive_blog', $blog_id );
796 } elseif ( 'deleted' == $pref ) {
798 /** This filter is documented in wp-includes/ms-blogs.php */
799 do_action( 'make_delete_blog', $blog_id );
801 /** This filter is documented in wp-includes/ms-blogs.php */
802 do_action( 'make_undelete_blog', $blog_id );
804 } elseif ( 'public' == $pref ) {
806 * Fires after the current blog's 'public' setting is updated.
810 * @param int $blog_id Blog ID.
811 * @param string $value The value of blog status.
813 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
820 * Get a blog details field.
824 * @param int $id The blog id
825 * @param string $pref A field name
826 * @return bool $value
828 function get_blog_status( $id, $pref ) {
831 $details = get_blog_details( $id, false );
833 return $details->$pref;
835 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
839 * Get a list of most recently updated blogs.
843 * @param mixed $deprecated Not used
844 * @param int $start The offset
845 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
846 * @return array The list of blogs
848 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
851 if ( ! empty( $deprecated ) )
852 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
854 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 );
858 * Handler for updating the blog date when a post is published or an already published post is changed.
862 * @param string $new_status The new post status
863 * @param string $old_status The old post status
864 * @param object $post Post object
866 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
867 $post_type_obj = get_post_type_object( $post->post_type );
868 if ( ! $post_type_obj || ! $post_type_obj->public ) {
872 if ( 'publish' != $new_status && 'publish' != $old_status ) {
876 // Post was freshly published, published post was saved, or published post was unpublished.
878 wpmu_update_blogs_date();
882 * Handler for updating the blog date when a published post is deleted.
886 * @param int $post_id Post ID
888 function _update_blog_date_on_post_delete( $post_id ) {
889 $post = get_post( $post_id );
891 $post_type_obj = get_post_type_object( $post->post_type );
892 if ( ! $post_type_obj || ! $post_type_obj->public ) {
896 if ( 'publish' != $post->post_status ) {
900 wpmu_update_blogs_date();
904 * Handler for updating the blog posts count date when a post is deleted.
908 * @param int $post_id Post ID.
910 function _update_posts_count_on_delete( $post_id ) {
911 $post = get_post( $post_id );
913 if ( ! $post || 'publish' !== $post->post_status ) {
917 update_posts_count();
921 * Handler for updating the blog posts count date when a post status changes.
925 * @param string $new_status The status the post is changing to.
926 * @param string $old_status The status the post is changing from.
928 function _update_posts_count_on_transition_post_status( $new_status, $old_status ) {
929 if ( $new_status === $old_status ) {
933 if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
937 update_posts_count();