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 Blog ID
240 function refresh_blog_details( $blog_id ) {
241 $blog_id = (int) $blog_id;
242 $details = get_blog_details( $blog_id, false );
244 // Make sure clean_blog_cache() gets the blog ID
245 // when the blog has been previously cached as
247 $details = (object) array(
248 'blog_id' => $blog_id,
254 clean_blog_cache( $details );
257 * Fires after the blog details cache is cleared.
261 * @param int $blog_id Blog ID.
263 do_action( 'refresh_blog_details', $blog_id );
267 * Update the details for a blog. Updates the blogs table for a given blog id.
271 * @param int $blog_id Blog ID
272 * @param array $details Array of details keyed by blogs table field names.
273 * @return bool True if update succeeds, false otherwise.
275 function update_blog_details( $blog_id, $details = array() ) {
278 if ( empty($details) )
281 if ( is_object($details) )
282 $details = get_object_vars($details);
284 $current_details = get_blog_details($blog_id, false);
285 if ( empty($current_details) )
288 $current_details = get_object_vars($current_details);
290 $details = array_merge($current_details, $details);
291 $details['last_updated'] = current_time('mysql', true);
293 $update_details = array();
294 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
295 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
296 $update_details[$field] = $details[$field];
298 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
300 if ( false === $result )
303 // If spam status changed, issue actions.
304 if ( $details['spam'] != $current_details['spam'] ) {
305 if ( $details['spam'] == 1 ) {
307 * Fires when the blog status is changed to 'spam'.
311 * @param int $blog_id Blog ID.
313 do_action( 'make_spam_blog', $blog_id );
316 * Fires when the blog status is changed to 'ham'.
320 * @param int $blog_id Blog ID.
322 do_action( 'make_ham_blog', $blog_id );
326 // If mature status changed, issue actions.
327 if ( $details['mature'] != $current_details['mature'] ) {
328 if ( $details['mature'] == 1 ) {
330 * Fires when the blog status is changed to 'mature'.
334 * @param int $blog_id Blog ID.
336 do_action( 'mature_blog', $blog_id );
339 * Fires when the blog status is changed to 'unmature'.
343 * @param int $blog_id Blog ID.
345 do_action( 'unmature_blog', $blog_id );
349 // If archived status changed, issue actions.
350 if ( $details['archived'] != $current_details['archived'] ) {
351 if ( $details['archived'] == 1 ) {
353 * Fires when the blog status is changed to 'archived'.
357 * @param int $blog_id Blog ID.
359 do_action( 'archive_blog', $blog_id );
362 * Fires when the blog status is changed to 'unarchived'.
366 * @param int $blog_id Blog ID.
368 do_action( 'unarchive_blog', $blog_id );
372 // If deleted status changed, issue actions.
373 if ( $details['deleted'] != $current_details['deleted'] ) {
374 if ( $details['deleted'] == 1 ) {
376 * Fires when the blog status is changed to 'deleted'.
380 * @param int $blog_id Blog ID.
382 do_action( 'make_delete_blog', $blog_id );
385 * Fires when the blog status is changed to 'undeleted'.
389 * @param int $blog_id Blog ID.
391 do_action( 'make_undelete_blog', $blog_id );
395 if ( isset( $details['public'] ) ) {
396 switch_to_blog( $blog_id );
397 update_option( 'blog_public', $details['public'] );
398 restore_current_blog();
401 refresh_blog_details($blog_id);
407 * Clean the blog cache
411 * @param stdClass $blog The blog details as returned from get_blog_details()
413 function clean_blog_cache( $blog ) {
414 $blog_id = $blog->blog_id;
415 $domain_path_key = md5( $blog->domain . $blog->path );
417 wp_cache_delete( $blog_id , 'blog-details' );
418 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
419 wp_cache_delete( $domain_path_key, 'blog-lookup' );
420 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
421 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
422 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
423 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
427 * Retrieve option value for a given blog id based on name of option.
429 * If the option does not exist or does not have a value, then the return value
430 * will be false. This is useful to check whether you need to install an option
431 * and is commonly used during installation of plugin options and to test
432 * whether upgrading is required.
434 * If the option was serialized then it will be unserialized when it is returned.
438 * @param int $id A blog ID. Can be null to refer to the current blog.
439 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
440 * @param mixed $default Optional. Default value to return if the option does not exist.
441 * @return mixed Value set for the option.
443 function get_blog_option( $id, $option, $default = false ) {
447 $id = get_current_blog_id();
449 if ( get_current_blog_id() == $id )
450 return get_option( $option, $default );
452 switch_to_blog( $id );
453 $value = get_option( $option, $default );
454 restore_current_blog();
457 * Filter a blog option value.
459 * The dynamic portion of the hook name, $option, refers to the blog option name.
463 * @param string $value The option value.
464 * @param int $id Blog ID.
466 return apply_filters( "blog_option_{$option}", $value, $id );
470 * Add a new option for a given blog id.
472 * You do not need to serialize values. If the value needs to be serialized, then
473 * it will be serialized before it is inserted into the database. Remember,
474 * resources can not be serialized or added as an option.
476 * You can create options without values and then update the values later.
477 * Existing options will not be updated and checks are performed to ensure that you
478 * aren't adding a protected WordPress option. Care should be taken to not name
479 * options the same as the ones which are protected.
483 * @param int $id A blog ID. Can be null to refer to the current blog.
484 * @param string $option Name of option to add. Expected to not be SQL-escaped.
485 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
486 * @return bool False if option was not added and true if option was added.
488 function add_blog_option( $id, $option, $value ) {
492 $id = get_current_blog_id();
494 if ( get_current_blog_id() == $id )
495 return add_option( $option, $value );
497 switch_to_blog( $id );
498 $return = add_option( $option, $value );
499 restore_current_blog();
505 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
509 * @param int $id A blog ID. Can be null to refer to the current blog.
510 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
511 * @return bool True, if option is successfully deleted. False on failure.
513 function delete_blog_option( $id, $option ) {
517 $id = get_current_blog_id();
519 if ( get_current_blog_id() == $id )
520 return delete_option( $option );
522 switch_to_blog( $id );
523 $return = delete_option( $option );
524 restore_current_blog();
530 * Update an option for a particular blog.
534 * @param int $id The blog id
535 * @param string $option The option key
536 * @param mixed $value The option value
537 * @return bool True on success, false on failure.
539 function update_blog_option( $id, $option, $value, $deprecated = null ) {
542 if ( null !== $deprecated )
543 _deprecated_argument( __FUNCTION__, '3.1' );
545 if ( get_current_blog_id() == $id )
546 return update_option( $option, $value );
548 switch_to_blog( $id );
549 $return = update_option( $option, $value );
550 restore_current_blog();
552 refresh_blog_details( $id );
558 * Switch the current blog.
560 * This function is useful if you need to pull posts, or other information,
561 * from other blogs. You can switch back afterwards using restore_current_blog().
563 * Things that aren't switched:
564 * - autoloaded options. See #14992
565 * - plugins. See #14941
567 * @see restore_current_blog()
570 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
571 * @param bool $deprecated Deprecated argument
572 * @return bool Always returns True.
574 function switch_to_blog( $new_blog, $deprecated = null ) {
575 global $wpdb, $wp_roles;
577 if ( empty( $new_blog ) )
578 $new_blog = $GLOBALS['blog_id'];
580 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
583 * If we're switching to the same blog id that we're on,
584 * set the right vars, do the associated actions, but skip
585 * the extra unnecessary work
587 if ( $new_blog == $GLOBALS['blog_id'] ) {
589 * Fires when the blog is switched.
593 * @param int $new_blog New blog ID.
594 * @param int $new_blog Blog ID.
596 do_action( 'switch_blog', $new_blog, $new_blog );
597 $GLOBALS['switched'] = true;
601 $wpdb->set_blog_id( $new_blog );
602 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
603 $prev_blog_id = $GLOBALS['blog_id'];
604 $GLOBALS['blog_id'] = $new_blog;
606 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
607 wp_cache_switch_to_blog( $new_blog );
609 global $wp_object_cache;
611 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
612 $global_groups = $wp_object_cache->global_groups;
614 $global_groups = false;
618 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
619 if ( is_array( $global_groups ) )
620 wp_cache_add_global_groups( $global_groups );
622 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' ) );
623 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
627 if ( did_action( 'init' ) ) {
629 $current_user = wp_get_current_user();
630 $current_user->for_blog( $new_blog );
633 /** This filter is documented in wp-includes/ms-blogs.php */
634 do_action( 'switch_blog', $new_blog, $prev_blog_id );
635 $GLOBALS['switched'] = true;
641 * Restore the current blog, after calling switch_to_blog()
643 * @see switch_to_blog()
646 * @return bool True on success, false if we're already on the current blog
648 function restore_current_blog() {
649 global $wpdb, $wp_roles;
651 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
654 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
656 if ( $GLOBALS['blog_id'] == $blog ) {
657 /** This filter is documented in wp-includes/ms-blogs.php */
658 do_action( 'switch_blog', $blog, $blog );
659 // If we still have items in the switched stack, consider ourselves still 'switched'
660 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
664 $wpdb->set_blog_id( $blog );
665 $prev_blog_id = $GLOBALS['blog_id'];
666 $GLOBALS['blog_id'] = $blog;
667 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
669 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
670 wp_cache_switch_to_blog( $blog );
672 global $wp_object_cache;
674 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
675 $global_groups = $wp_object_cache->global_groups;
677 $global_groups = false;
681 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
682 if ( is_array( $global_groups ) )
683 wp_cache_add_global_groups( $global_groups );
685 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' ) );
686 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
690 if ( did_action( 'init' ) ) {
692 $current_user = wp_get_current_user();
693 $current_user->for_blog( $blog );
696 /** This filter is documented in wp-includes/ms-blogs.php */
697 do_action( 'switch_blog', $blog, $prev_blog_id );
699 // If we still have items in the switched stack, consider ourselves still 'switched'
700 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
706 * Determines if switch_to_blog() is in effect
710 * @return bool True if switched, false otherwise.
712 function ms_is_switched() {
713 return ! empty( $GLOBALS['_wp_switched_stack'] );
717 * Check if a particular blog is archived.
721 * @param int $id The blog id
722 * @return string Whether the blog is archived or not
724 function is_archived( $id ) {
725 return get_blog_status($id, 'archived');
729 * Update the 'archived' status of a particular blog.
733 * @param int $id The blog id
734 * @param string $archived The new status
735 * @return string $archived
737 function update_archived( $id, $archived ) {
738 update_blog_status($id, 'archived', $archived);
743 * Update a blog details field.
747 * @param int $blog_id BLog ID
748 * @param string $pref A field name
749 * @param string $value Value for $pref
750 * @return string $value
752 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
755 if ( null !== $deprecated )
756 _deprecated_argument( __FUNCTION__, '3.1' );
758 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
761 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
763 if ( false === $result )
766 refresh_blog_details( $blog_id );
768 if ( 'spam' == $pref ) {
770 /** This filter is documented in wp-includes/ms-blogs.php */
771 do_action( 'make_spam_blog', $blog_id );
773 /** This filter is documented in wp-includes/ms-blogs.php */
774 do_action( 'make_ham_blog', $blog_id );
776 } elseif ( 'mature' == $pref ) {
778 /** This filter is documented in wp-includes/ms-blogs.php */
779 do_action( 'mature_blog', $blog_id );
781 /** This filter is documented in wp-includes/ms-blogs.php */
782 do_action( 'unmature_blog', $blog_id );
784 } elseif ( 'archived' == $pref ) {
786 /** This filter is documented in wp-includes/ms-blogs.php */
787 do_action( 'archive_blog', $blog_id );
789 /** This filter is documented in wp-includes/ms-blogs.php */
790 do_action( 'unarchive_blog', $blog_id );
792 } elseif ( 'deleted' == $pref ) {
794 /** This filter is documented in wp-includes/ms-blogs.php */
795 do_action( 'make_delete_blog', $blog_id );
797 /** This filter is documented in wp-includes/ms-blogs.php */
798 do_action( 'make_undelete_blog', $blog_id );
800 } elseif ( 'public' == $pref ) {
802 * Fires after the current blog's 'public' setting is updated.
806 * @param int $blog_id Blog ID.
807 * @param string $value The value of blog status.
809 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
816 * Get a blog details field.
820 * @param int $id The blog id
821 * @param string $pref A field name
822 * @return bool $value
824 function get_blog_status( $id, $pref ) {
827 $details = get_blog_details( $id, false );
829 return $details->$pref;
831 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
835 * Get a list of most recently updated blogs.
839 * @param mixed $deprecated Not used
840 * @param int $start The offset
841 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
842 * @return array The list of blogs
844 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
847 if ( ! empty( $deprecated ) )
848 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
850 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 );
854 * Handler for updating the blog date when a post is published or an already published post is changed.
858 * @param string $new_status The new post status
859 * @param string $old_status The old post status
860 * @param object $post Post object
862 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
863 $post_type_obj = get_post_type_object( $post->post_type );
864 if ( ! $post_type_obj->public )
867 if ( 'publish' != $new_status && 'publish' != $old_status )
870 // Post was freshly published, published post was saved, or published post was unpublished.
872 wpmu_update_blogs_date();
876 * Handler for updating the blog date when a published post is deleted.
880 * @param int $post_id Post ID
882 function _update_blog_date_on_post_delete( $post_id ) {
883 $post = get_post( $post_id );
885 $post_type_obj = get_post_type_object( $post->post_type );
886 if ( ! $post_type_obj->public )
889 if ( 'publish' != $post->post_status )
892 wpmu_update_blogs_date();