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 do_action( 'wpmu_blog_updated', $wpdb->blogid );
25 * Get a full blog URL, given a blog id.
29 * @param int $blog_id Blog ID
32 function get_blogaddress_by_id( $blog_id ) {
33 $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
34 return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
38 * Get a full blog URL, given a blog name.
42 * @param string $blogname The (subdomain or directory) name
45 function get_blogaddress_by_name( $blogname ) {
48 if ( is_subdomain_install() ) {
49 if ( $blogname == 'main' )
51 $url = rtrim( network_home_url(), '/' );
52 if ( !empty( $blogname ) )
53 $url = preg_replace( '|^([^\.]+://)|', '$1' . $blogname . '.', $url );
55 $url = network_home_url( $blogname );
57 return esc_url( $url . '/' );
61 * Get a full blog URL, given a domain and a path.
65 * @param string $domain
69 function get_blogaddress_by_domain( $domain, $path ) {
70 if ( is_subdomain_install() ) {
71 $url = "http://" . $domain.$path;
73 if ( $domain != $_SERVER['HTTP_HOST'] ) {
74 $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
75 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
76 // we're not installing the main blog
77 if ( $blogname != 'www.' )
78 $url .= $blogname . '/';
80 $url = 'http://' . $domain . $path;
83 return esc_url( $url );
87 * Given a blog's (subdomain or directory) slug, retrieve it's id.
92 * @return int A blog id
94 function get_id_from_blogname( $slug ) {
95 global $wpdb, $current_site;
97 $slug = trim( $slug, '/' );
99 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
103 if ( is_subdomain_install() ) {
104 $domain = $slug . '.' . $current_site->domain;
105 $path = $current_site->path;
107 $domain = $current_site->domain;
108 $path = $current_site->path . $slug . '/';
111 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
112 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
117 * Retrieve the details for a blog from the blogs table and blog options.
121 * @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.
122 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
123 * @return object Blog details.
125 function get_blog_details( $fields = null, $get_all = true ) {
128 if ( is_array($fields ) ) {
129 if ( isset($fields['blog_id']) ) {
130 $blog_id = $fields['blog_id'];
131 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
132 $key = md5( $fields['domain'] . $fields['path'] );
133 $blog = wp_cache_get($key, 'blog-lookup');
134 if ( false !== $blog )
136 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
137 $nowww = substr( $fields['domain'], 4 );
138 $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'] ) );
140 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
143 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
144 $blog_id = $blog->blog_id;
148 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
149 $key = md5( $fields['domain'] );
150 $blog = wp_cache_get($key, 'blog-lookup');
151 if ( false !== $blog )
153 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
154 $nowww = substr( $fields['domain'], 4 );
155 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
157 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
160 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
161 $blog_id = $blog->blog_id;
170 $blog_id = get_current_blog_id();
171 elseif ( ! is_numeric( $fields ) )
172 $blog_id = get_id_from_blogname( $fields );
177 $blog_id = (int) $blog_id;
179 $all = $get_all == true ? '' : 'short';
180 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
183 if ( ! is_object( $details ) ) {
184 if ( $details == -1 ) {
187 // Clear old pre-serialized objects. Cache clients do better with that.
188 wp_cache_delete( $blog_id . $all, 'blog-details' );
196 // Try the other cache.
198 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
200 $details = wp_cache_get( $blog_id, 'blog-details' );
201 // If short was requested and full cache is set, we can return.
203 if ( ! is_object( $details ) ) {
204 if ( $details == -1 ) {
207 // Clear old pre-serialized objects. Cache clients do better with that.
208 wp_cache_delete( $blog_id, 'blog-details' );
217 if ( empty($details) ) {
218 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
220 // Set the full cache.
221 wp_cache_set( $blog_id, -1, 'blog-details' );
227 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
231 switch_to_blog( $blog_id );
232 $details->blogname = get_option( 'blogname' );
233 $details->siteurl = get_option( 'siteurl' );
234 $details->post_count = get_option( 'post_count' );
235 restore_current_blog();
237 $details = apply_filters( 'blog_details', $details );
239 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
241 $key = md5( $details->domain . $details->path );
242 wp_cache_set( $key, $details, 'blog-lookup' );
248 * Clear the blog details cache.
252 * @param int $blog_id Blog ID
254 function refresh_blog_details( $blog_id ) {
255 $blog_id = (int) $blog_id;
256 $details = get_blog_details( $blog_id, false );
258 clean_blog_cache( $details );
260 do_action( 'refresh_blog_details', $blog_id );
264 * Update the details for a blog. Updates the blogs table for a given blog id.
268 * @param int $blog_id Blog ID
269 * @param array $details Array of details keyed by blogs table field names.
270 * @return bool True if update succeeds, false otherwise.
272 function update_blog_details( $blog_id, $details = array() ) {
275 if ( empty($details) )
278 if ( is_object($details) )
279 $details = get_object_vars($details);
281 $current_details = get_blog_details($blog_id, false);
282 if ( empty($current_details) )
285 $current_details = get_object_vars($current_details);
287 $details = array_merge($current_details, $details);
288 $details['last_updated'] = current_time('mysql', true);
290 $update_details = array();
291 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
292 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
293 $update_details[$field] = $details[$field];
295 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
297 if ( false === $result )
300 // If spam status changed, issue actions.
301 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
302 if ( $details[ 'spam' ] == 1 )
303 do_action( 'make_spam_blog', $blog_id );
305 do_action( 'make_ham_blog', $blog_id );
308 // If mature status changed, issue actions.
309 if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) {
310 if ( $details[ 'mature' ] == 1 )
311 do_action( 'mature_blog', $blog_id );
313 do_action( 'unmature_blog', $blog_id );
316 // If archived status changed, issue actions.
317 if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) {
318 if ( $details[ 'archived' ] == 1 )
319 do_action( 'archive_blog', $blog_id );
321 do_action( 'unarchive_blog', $blog_id );
324 // If deleted status changed, issue actions.
325 if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) {
326 if ( $details[ 'deleted' ] == 1 )
327 do_action( 'make_delete_blog', $blog_id );
329 do_action( 'make_undelete_blog', $blog_id );
332 if ( isset( $details[ 'public' ] ) ) {
333 switch_to_blog( $blog_id );
334 update_option( 'blog_public', $details[ 'public' ] );
335 restore_current_blog();
338 refresh_blog_details($blog_id);
344 * Clean the blog cache
348 * @param stdClass $blog The blog details as returned from get_blog_details()
350 function clean_blog_cache( $blog ) {
351 $blog_id = $blog->blog_id;
352 $domain_path_key = md5( $blog->domain . $blog->path );
354 wp_cache_delete( $blog_id , 'blog-details' );
355 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
356 wp_cache_delete( $domain_path_key, 'blog-lookup' );
357 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
358 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
359 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
360 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
364 * Retrieve option value for a given blog id based on name of option.
366 * If the option does not exist or does not have a value, then the return value
367 * will be false. This is useful to check whether you need to install an option
368 * and is commonly used during installation of plugin options and to test
369 * whether upgrading is required.
371 * If the option was serialized then it will be unserialized when it is returned.
375 * @param int $id A blog ID. Can be null to refer to the current blog.
376 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
377 * @param mixed $default Optional. Default value to return if the option does not exist.
378 * @return mixed Value set for the option.
380 function get_blog_option( $id, $option, $default = false ) {
384 $id = get_current_blog_id();
386 if ( get_current_blog_id() == $id )
387 return get_option( $option, $default );
389 switch_to_blog( $id );
390 $value = get_option( $option, $default );
391 restore_current_blog();
393 return apply_filters( 'blog_option_' . $option, $value, $id );
397 * Add a new option for a given blog id.
399 * You do not need to serialize values. If the value needs to be serialized, then
400 * it will be serialized before it is inserted into the database. Remember,
401 * resources can not be serialized or added as an option.
403 * You can create options without values and then update the values later.
404 * Existing options will not be updated and checks are performed to ensure that you
405 * aren't adding a protected WordPress option. Care should be taken to not name
406 * options the same as the ones which are protected.
410 * @param int $id A blog ID. Can be null to refer to the current blog.
411 * @param string $option Name of option to add. Expected to not be SQL-escaped.
412 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
413 * @return bool False if option was not added and true if option was added.
415 function add_blog_option( $id, $option, $value ) {
419 $id = get_current_blog_id();
421 if ( get_current_blog_id() == $id )
422 return add_option( $option, $value );
424 switch_to_blog( $id );
425 $return = add_option( $option, $value );
426 restore_current_blog();
432 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
436 * @param int $id A blog ID. Can be null to refer to the current blog.
437 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
438 * @return bool True, if option is successfully deleted. False on failure.
440 function delete_blog_option( $id, $option ) {
444 $id = get_current_blog_id();
446 if ( get_current_blog_id() == $id )
447 return delete_option( $option );
449 switch_to_blog( $id );
450 $return = delete_option( $option );
451 restore_current_blog();
457 * Update an option for a particular blog.
461 * @param int $id The blog id
462 * @param string $option The option key
463 * @param mixed $value The option value
464 * @return bool True on success, false on failrue.
466 function update_blog_option( $id, $option, $value, $deprecated = null ) {
469 if ( null !== $deprecated )
470 _deprecated_argument( __FUNCTION__, '3.1' );
472 if ( get_current_blog_id() == $id )
473 return update_option( $option, $value );
475 switch_to_blog( $id );
476 $return = update_option( $option, $value );
477 restore_current_blog();
479 refresh_blog_details( $id );
485 * Switch the current blog.
487 * This function is useful if you need to pull posts, or other information,
488 * from other blogs. You can switch back afterwards using restore_current_blog().
490 * Things that aren't switched:
491 * - autoloaded options. See #14992
492 * - plugins. See #14941
494 * @see restore_current_blog()
497 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
498 * @param bool $deprecated Deprecated argument
499 * @return bool True on success, false if the validation failed
501 function switch_to_blog( $new_blog, $deprecated = null ) {
502 global $wpdb, $wp_roles;
504 if ( empty( $new_blog ) )
505 $new_blog = $GLOBALS['blog_id'];
507 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
509 /* If we're switching to the same blog id that we're on,
510 * set the right vars, do the associated actions, but skip
511 * the extra unnecessary work */
512 if ( $new_blog == $GLOBALS['blog_id'] ) {
513 do_action( 'switch_blog', $new_blog, $new_blog );
514 $GLOBALS['switched'] = true;
518 $wpdb->set_blog_id( $new_blog );
519 $GLOBALS['table_prefix'] = $wpdb->prefix;
520 $prev_blog_id = $GLOBALS['blog_id'];
521 $GLOBALS['blog_id'] = $new_blog;
523 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
524 wp_cache_switch_to_blog( $new_blog );
526 global $wp_object_cache;
528 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
529 $global_groups = $wp_object_cache->global_groups;
531 $global_groups = false;
535 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
536 if ( is_array( $global_groups ) )
537 wp_cache_add_global_groups( $global_groups );
539 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' ) );
540 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
544 if ( did_action( 'init' ) ) {
546 $current_user = wp_get_current_user();
547 $current_user->for_blog( $new_blog );
550 do_action( 'switch_blog', $new_blog, $prev_blog_id );
551 $GLOBALS['switched'] = true;
557 * Restore the current blog, after calling switch_to_blog()
559 * @see switch_to_blog()
562 * @return bool True on success, false if we're already on the current blog
564 function restore_current_blog() {
565 global $wpdb, $wp_roles;
567 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
570 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
572 if ( $GLOBALS['blog_id'] == $blog ) {
573 do_action( 'switch_blog', $blog, $blog );
574 // If we still have items in the switched stack, consider ourselves still 'switched'
575 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
579 $wpdb->set_blog_id( $blog );
580 $prev_blog_id = $GLOBALS['blog_id'];
581 $GLOBALS['blog_id'] = $blog;
582 $GLOBALS['table_prefix'] = $wpdb->prefix;
584 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
585 wp_cache_switch_to_blog( $blog );
587 global $wp_object_cache;
589 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
590 $global_groups = $wp_object_cache->global_groups;
592 $global_groups = false;
596 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
597 if ( is_array( $global_groups ) )
598 wp_cache_add_global_groups( $global_groups );
600 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' ) );
601 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
605 if ( did_action( 'init' ) ) {
607 $current_user = wp_get_current_user();
608 $current_user->for_blog( $blog );
611 do_action( 'switch_blog', $blog, $prev_blog_id );
613 // If we still have items in the switched stack, consider ourselves still 'switched'
614 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
620 * Determines if switch_to_blog() is in effect
624 * @return bool True if switched, false otherwise.
626 function ms_is_switched() {
627 return ! empty( $GLOBALS['_wp_switched_stack'] );
631 * Check if a particular blog is archived.
635 * @param int $id The blog id
636 * @return string Whether the blog is archived or not
638 function is_archived( $id ) {
639 return get_blog_status($id, 'archived');
643 * Update the 'archived' status of a particular blog.
647 * @param int $id The blog id
648 * @param string $archived The new status
649 * @return string $archived
651 function update_archived( $id, $archived ) {
652 update_blog_status($id, 'archived', $archived);
657 * Update a blog details field.
661 * @param int $blog_id BLog ID
662 * @param string $pref A field name
663 * @param string $value Value for $pref
664 * @return string $value
666 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
669 if ( null !== $deprecated )
670 _deprecated_argument( __FUNCTION__, '3.1' );
672 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
675 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
677 if ( false === $result )
680 refresh_blog_details( $blog_id );
682 if ( 'spam' == $pref )
683 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
684 elseif ( 'mature' == $pref )
685 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
686 elseif ( 'archived' == $pref )
687 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
688 elseif ( 'deleted' == $pref )
689 ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id );
695 * Get a blog details field.
699 * @param int $id The blog id
700 * @param string $pref A field name
701 * @return bool $value
703 function get_blog_status( $id, $pref ) {
706 $details = get_blog_details( $id, false );
708 return $details->$pref;
710 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
714 * Get a list of most recently updated blogs.
718 * @param mixed $deprecated Not used
719 * @param int $start The offset
720 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
721 * @return array The list of blogs
723 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
726 if ( ! empty( $deprecated ) )
727 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
729 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 );
733 * Handler for updating the blog date when a post is published or an already published post is changed.
737 * @param string $new_status The new post status
738 * @param string $old_status The old post status
739 * @param object $post Post object
741 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
742 $post_type_obj = get_post_type_object( $post->post_type );
743 if ( ! $post_type_obj->public )
746 if ( 'publish' != $new_status && 'publish' != $old_status )
749 // Post was freshly published, published post was saved, or published post was unpublished.
751 wpmu_update_blogs_date();
755 * Handler for updating the blog date when a published post is deleted.
759 * @param int $post_id Post ID
761 function _update_blog_date_on_post_delete( $post_id ) {
762 $post = get_post( $post_id );
764 $post_type_obj = get_post_type_object( $post->post_type );
765 if ( ! $post_type_obj->public )
768 if ( 'publish' != $post->post_status )
771 wpmu_update_blogs_date();