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 ) {
46 if ( is_subdomain_install() ) {
47 if ( $blogname == 'main' )
49 $url = rtrim( network_home_url(), '/' );
50 if ( !empty( $blogname ) )
51 $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
53 $url = network_home_url( $blogname );
55 return esc_url( $url . '/' );
59 * Get a full blog URL, given a domain and a path.
63 * @param string $domain
67 function get_blogaddress_by_domain( $domain, $path ) {
68 if ( is_subdomain_install() ) {
69 $url = "http://" . $domain.$path;
71 if ( $domain != $_SERVER['HTTP_HOST'] ) {
72 $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
73 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
74 // we're not installing the main blog
75 if ( $blogname != 'www.' )
76 $url .= $blogname . '/';
78 $url = 'http://' . $domain . $path;
81 return esc_url( $url );
85 * Given a blog's (subdomain or directory) slug, retrieve its id.
90 * @return int A blog id
92 function get_id_from_blogname( $slug ) {
93 global $wpdb, $current_site;
95 $slug = trim( $slug, '/' );
97 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
101 if ( is_subdomain_install() ) {
102 $domain = $slug . '.' . $current_site->domain;
103 $path = $current_site->path;
105 $domain = $current_site->domain;
106 $path = $current_site->path . $slug . '/';
109 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
110 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
115 * Retrieve the details for a blog from the blogs table and blog options.
119 * @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.
120 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
121 * @return object Blog details.
123 function get_blog_details( $fields = null, $get_all = true ) {
126 if ( is_array($fields ) ) {
127 if ( isset($fields['blog_id']) ) {
128 $blog_id = $fields['blog_id'];
129 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
130 $key = md5( $fields['domain'] . $fields['path'] );
131 $blog = wp_cache_get($key, 'blog-lookup');
132 if ( false !== $blog )
134 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
135 $nowww = substr( $fields['domain'], 4 );
136 $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'] ) );
138 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
141 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
142 $blog_id = $blog->blog_id;
146 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
147 $key = md5( $fields['domain'] );
148 $blog = wp_cache_get($key, 'blog-lookup');
149 if ( false !== $blog )
151 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
152 $nowww = substr( $fields['domain'], 4 );
153 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
155 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
158 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
159 $blog_id = $blog->blog_id;
168 $blog_id = get_current_blog_id();
169 elseif ( ! is_numeric( $fields ) )
170 $blog_id = get_id_from_blogname( $fields );
175 $blog_id = (int) $blog_id;
177 $all = $get_all == true ? '' : 'short';
178 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
181 if ( ! is_object( $details ) ) {
182 if ( $details == -1 ) {
185 // Clear old pre-serialized objects. Cache clients do better with that.
186 wp_cache_delete( $blog_id . $all, 'blog-details' );
194 // Try the other cache.
196 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
198 $details = wp_cache_get( $blog_id, 'blog-details' );
199 // If short was requested and full cache is set, we can return.
201 if ( ! is_object( $details ) ) {
202 if ( $details == -1 ) {
205 // Clear old pre-serialized objects. Cache clients do better with that.
206 wp_cache_delete( $blog_id, 'blog-details' );
215 if ( empty($details) ) {
216 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
218 // Set the full cache.
219 wp_cache_set( $blog_id, -1, 'blog-details' );
225 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
229 switch_to_blog( $blog_id );
230 $details->blogname = get_option( 'blogname' );
231 $details->siteurl = get_option( 'siteurl' );
232 $details->post_count = get_option( 'post_count' );
233 restore_current_blog();
235 $details = apply_filters( 'blog_details', $details );
237 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
239 $key = md5( $details->domain . $details->path );
240 wp_cache_set( $key, $details, 'blog-lookup' );
246 * Clear the blog details cache.
250 * @param int $blog_id Blog ID
252 function refresh_blog_details( $blog_id ) {
253 $blog_id = (int) $blog_id;
254 $details = get_blog_details( $blog_id, false );
256 // Make sure clean_blog_cache() gets the blog ID
257 // when the blog has been previously cached as
259 $details = (object) array(
260 'blog_id' => $blog_id,
266 clean_blog_cache( $details );
268 do_action( 'refresh_blog_details', $blog_id );
272 * Update the details for a blog. Updates the blogs table for a given blog id.
276 * @param int $blog_id Blog ID
277 * @param array $details Array of details keyed by blogs table field names.
278 * @return bool True if update succeeds, false otherwise.
280 function update_blog_details( $blog_id, $details = array() ) {
283 if ( empty($details) )
286 if ( is_object($details) )
287 $details = get_object_vars($details);
289 $current_details = get_blog_details($blog_id, false);
290 if ( empty($current_details) )
293 $current_details = get_object_vars($current_details);
295 $details = array_merge($current_details, $details);
296 $details['last_updated'] = current_time('mysql', true);
298 $update_details = array();
299 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
300 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
301 $update_details[$field] = $details[$field];
303 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
305 if ( false === $result )
308 // If spam status changed, issue actions.
309 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
310 if ( $details[ 'spam' ] == 1 )
311 do_action( 'make_spam_blog', $blog_id );
313 do_action( 'make_ham_blog', $blog_id );
316 // If mature status changed, issue actions.
317 if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) {
318 if ( $details[ 'mature' ] == 1 )
319 do_action( 'mature_blog', $blog_id );
321 do_action( 'unmature_blog', $blog_id );
324 // If archived status changed, issue actions.
325 if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) {
326 if ( $details[ 'archived' ] == 1 )
327 do_action( 'archive_blog', $blog_id );
329 do_action( 'unarchive_blog', $blog_id );
332 // If deleted status changed, issue actions.
333 if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) {
334 if ( $details[ 'deleted' ] == 1 )
335 do_action( 'make_delete_blog', $blog_id );
337 do_action( 'make_undelete_blog', $blog_id );
340 if ( isset( $details[ 'public' ] ) ) {
341 switch_to_blog( $blog_id );
342 update_option( 'blog_public', $details[ 'public' ] );
343 restore_current_blog();
346 refresh_blog_details($blog_id);
352 * Clean the blog cache
356 * @param stdClass $blog The blog details as returned from get_blog_details()
358 function clean_blog_cache( $blog ) {
359 $blog_id = $blog->blog_id;
360 $domain_path_key = md5( $blog->domain . $blog->path );
362 wp_cache_delete( $blog_id , 'blog-details' );
363 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
364 wp_cache_delete( $domain_path_key, 'blog-lookup' );
365 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
366 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
367 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
368 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
372 * Retrieve option value for a given blog id based on name of option.
374 * If the option does not exist or does not have a value, then the return value
375 * will be false. This is useful to check whether you need to install an option
376 * and is commonly used during installation of plugin options and to test
377 * whether upgrading is required.
379 * If the option was serialized then it will be unserialized when it is returned.
383 * @param int $id A blog ID. Can be null to refer to the current blog.
384 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
385 * @param mixed $default Optional. Default value to return if the option does not exist.
386 * @return mixed Value set for the option.
388 function get_blog_option( $id, $option, $default = false ) {
392 $id = get_current_blog_id();
394 if ( get_current_blog_id() == $id )
395 return get_option( $option, $default );
397 switch_to_blog( $id );
398 $value = get_option( $option, $default );
399 restore_current_blog();
401 return apply_filters( 'blog_option_' . $option, $value, $id );
405 * Add a new option for a given blog id.
407 * You do not need to serialize values. If the value needs to be serialized, then
408 * it will be serialized before it is inserted into the database. Remember,
409 * resources can not be serialized or added as an option.
411 * You can create options without values and then update the values later.
412 * Existing options will not be updated and checks are performed to ensure that you
413 * aren't adding a protected WordPress option. Care should be taken to not name
414 * options the same as the ones which are protected.
418 * @param int $id A blog ID. Can be null to refer to the current blog.
419 * @param string $option Name of option to add. Expected to not be SQL-escaped.
420 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
421 * @return bool False if option was not added and true if option was added.
423 function add_blog_option( $id, $option, $value ) {
427 $id = get_current_blog_id();
429 if ( get_current_blog_id() == $id )
430 return add_option( $option, $value );
432 switch_to_blog( $id );
433 $return = add_option( $option, $value );
434 restore_current_blog();
440 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
444 * @param int $id A blog ID. Can be null to refer to the current blog.
445 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
446 * @return bool True, if option is successfully deleted. False on failure.
448 function delete_blog_option( $id, $option ) {
452 $id = get_current_blog_id();
454 if ( get_current_blog_id() == $id )
455 return delete_option( $option );
457 switch_to_blog( $id );
458 $return = delete_option( $option );
459 restore_current_blog();
465 * Update an option for a particular blog.
469 * @param int $id The blog id
470 * @param string $option The option key
471 * @param mixed $value The option value
472 * @return bool True on success, false on failure.
474 function update_blog_option( $id, $option, $value, $deprecated = null ) {
477 if ( null !== $deprecated )
478 _deprecated_argument( __FUNCTION__, '3.1' );
480 if ( get_current_blog_id() == $id )
481 return update_option( $option, $value );
483 switch_to_blog( $id );
484 $return = update_option( $option, $value );
485 restore_current_blog();
487 refresh_blog_details( $id );
493 * Switch the current blog.
495 * This function is useful if you need to pull posts, or other information,
496 * from other blogs. You can switch back afterwards using restore_current_blog().
498 * Things that aren't switched:
499 * - autoloaded options. See #14992
500 * - plugins. See #14941
502 * @see restore_current_blog()
505 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
506 * @param bool $deprecated Deprecated argument
507 * @return bool True on success, false if the validation failed
509 function switch_to_blog( $new_blog, $deprecated = null ) {
510 global $wpdb, $wp_roles;
512 if ( empty( $new_blog ) )
513 $new_blog = $GLOBALS['blog_id'];
515 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
517 /* If we're switching to the same blog id that we're on,
518 * set the right vars, do the associated actions, but skip
519 * the extra unnecessary work */
520 if ( $new_blog == $GLOBALS['blog_id'] ) {
521 do_action( 'switch_blog', $new_blog, $new_blog );
522 $GLOBALS['switched'] = true;
526 $wpdb->set_blog_id( $new_blog );
527 $GLOBALS['table_prefix'] = $wpdb->prefix;
528 $prev_blog_id = $GLOBALS['blog_id'];
529 $GLOBALS['blog_id'] = $new_blog;
531 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
532 wp_cache_switch_to_blog( $new_blog );
534 global $wp_object_cache;
536 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
537 $global_groups = $wp_object_cache->global_groups;
539 $global_groups = false;
543 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
544 if ( is_array( $global_groups ) )
545 wp_cache_add_global_groups( $global_groups );
547 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' ) );
548 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
552 if ( did_action( 'init' ) ) {
554 $current_user = wp_get_current_user();
555 $current_user->for_blog( $new_blog );
558 do_action( 'switch_blog', $new_blog, $prev_blog_id );
559 $GLOBALS['switched'] = true;
565 * Restore the current blog, after calling switch_to_blog()
567 * @see switch_to_blog()
570 * @return bool True on success, false if we're already on the current blog
572 function restore_current_blog() {
573 global $wpdb, $wp_roles;
575 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
578 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
580 if ( $GLOBALS['blog_id'] == $blog ) {
581 do_action( 'switch_blog', $blog, $blog );
582 // If we still have items in the switched stack, consider ourselves still 'switched'
583 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
587 $wpdb->set_blog_id( $blog );
588 $prev_blog_id = $GLOBALS['blog_id'];
589 $GLOBALS['blog_id'] = $blog;
590 $GLOBALS['table_prefix'] = $wpdb->prefix;
592 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
593 wp_cache_switch_to_blog( $blog );
595 global $wp_object_cache;
597 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
598 $global_groups = $wp_object_cache->global_groups;
600 $global_groups = false;
604 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
605 if ( is_array( $global_groups ) )
606 wp_cache_add_global_groups( $global_groups );
608 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' ) );
609 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
613 if ( did_action( 'init' ) ) {
615 $current_user = wp_get_current_user();
616 $current_user->for_blog( $blog );
619 do_action( 'switch_blog', $blog, $prev_blog_id );
621 // If we still have items in the switched stack, consider ourselves still 'switched'
622 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
628 * Determines if switch_to_blog() is in effect
632 * @return bool True if switched, false otherwise.
634 function ms_is_switched() {
635 return ! empty( $GLOBALS['_wp_switched_stack'] );
639 * Check if a particular blog is archived.
643 * @param int $id The blog id
644 * @return string Whether the blog is archived or not
646 function is_archived( $id ) {
647 return get_blog_status($id, 'archived');
651 * Update the 'archived' status of a particular blog.
655 * @param int $id The blog id
656 * @param string $archived The new status
657 * @return string $archived
659 function update_archived( $id, $archived ) {
660 update_blog_status($id, 'archived', $archived);
665 * Update a blog details field.
669 * @param int $blog_id BLog ID
670 * @param string $pref A field name
671 * @param string $value Value for $pref
672 * @return string $value
674 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
677 if ( null !== $deprecated )
678 _deprecated_argument( __FUNCTION__, '3.1' );
680 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
683 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
685 if ( false === $result )
688 refresh_blog_details( $blog_id );
690 if ( 'spam' == $pref )
691 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
692 elseif ( 'mature' == $pref )
693 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
694 elseif ( 'archived' == $pref )
695 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
696 elseif ( 'deleted' == $pref )
697 ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id );
698 elseif ( 'public' == $pref )
699 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
705 * Get a blog details field.
709 * @param int $id The blog id
710 * @param string $pref A field name
711 * @return bool $value
713 function get_blog_status( $id, $pref ) {
716 $details = get_blog_details( $id, false );
718 return $details->$pref;
720 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
724 * Get a list of most recently updated blogs.
728 * @param mixed $deprecated Not used
729 * @param int $start The offset
730 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
731 * @return array The list of blogs
733 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
736 if ( ! empty( $deprecated ) )
737 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
739 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 );
743 * Handler for updating the blog date when a post is published or an already published post is changed.
747 * @param string $new_status The new post status
748 * @param string $old_status The old post status
749 * @param object $post Post object
751 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
752 $post_type_obj = get_post_type_object( $post->post_type );
753 if ( ! $post_type_obj->public )
756 if ( 'publish' != $new_status && 'publish' != $old_status )
759 // Post was freshly published, published post was saved, or published post was unpublished.
761 wpmu_update_blogs_date();
765 * Handler for updating the blog date when a published post is deleted.
769 * @param int $post_id Post ID
771 function _update_blog_date_on_post_delete( $post_id ) {
772 $post = get_post( $post_id );
774 $post_type_obj = get_post_type_object( $post->post_type );
775 if ( ! $post_type_obj->public )
778 if ( 'publish' != $post->post_status )
781 wpmu_update_blogs_date();