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) name, retrieve it's id.
92 * @return int A blog id
94 function get_id_from_blogname( $name ) {
95 global $wpdb, $current_site;
96 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $name, 'blog-details' );
100 if ( is_subdomain_install() ) {
101 $domain = $name . '.' . $current_site->domain;
102 $path = $current_site->path;
104 $domain = $current_site->domain;
105 $path = $current_site->path . $name . '/';
107 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
108 wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
113 * Retrieve the details for a blog from the blogs table and blog options.
117 * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
118 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
119 * @return object Blog details.
121 function get_blog_details( $fields, $get_all = true ) {
124 if ( is_array($fields ) ) {
125 if ( isset($fields['blog_id']) ) {
126 $blog_id = $fields['blog_id'];
127 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
128 $key = md5( $fields['domain'] . $fields['path'] );
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) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
136 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
139 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
140 $blog_id = $blog->blog_id;
144 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
145 $key = md5( $fields['domain'] );
146 $blog = wp_cache_get($key, 'blog-lookup');
147 if ( false !== $blog )
149 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
150 $nowww = substr( $fields['domain'], 4 );
151 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
153 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
156 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
157 $blog_id = $blog->blog_id;
165 if ( !is_numeric( $fields ) )
166 $blog_id = get_id_from_blogname( $fields );
171 $blog_id = (int) $blog_id;
173 $all = $get_all == true ? '' : 'short';
174 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
177 if ( ! is_object( $details ) ) {
178 if ( $details == -1 ) {
181 // Clear old pre-serialized objects. Cache clients do better with that.
182 wp_cache_delete( $blog_id . $all, 'blog-details' );
190 // Try the other cache.
192 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
194 $details = wp_cache_get( $blog_id, 'blog-details' );
195 // If short was requested and full cache is set, we can return.
197 if ( ! is_object( $details ) ) {
198 if ( $details == -1 ) {
201 // Clear old pre-serialized objects. Cache clients do better with that.
202 wp_cache_delete( $blog_id, 'blog-details' );
211 if ( empty($details) ) {
212 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
214 // Set the full cache.
215 wp_cache_set( $blog_id, -1, 'blog-details' );
221 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
225 $details->blogname = get_blog_option( $blog_id, 'blogname' );
226 $details->siteurl = get_blog_option( $blog_id, 'siteurl' );
227 $details->post_count = get_blog_option( $blog_id, 'post_count' );
229 $details = apply_filters( 'blog_details', $details );
231 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
233 $key = md5( $details->domain . $details->path );
234 wp_cache_set( $key, $details, 'blog-lookup' );
240 * Clear the blog details cache.
244 * @param int $blog_id Blog ID
246 function refresh_blog_details( $blog_id ) {
247 $blog_id = (int) $blog_id;
248 $details = get_blog_details( $blog_id, false );
250 wp_cache_delete( $blog_id , 'blog-details' );
251 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
252 wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
253 wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
254 wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
258 * Update the details for a blog. Updates the blogs table for a given blog id.
262 * @param int $blog_id Blog ID
263 * @param array $details Array of details keyed by blogs table field names.
264 * @return bool True if update succeeds, false otherwise.
266 function update_blog_details( $blog_id, $details = array() ) {
269 if ( empty($details) )
272 if ( is_object($details) )
273 $details = get_object_vars($details);
275 $current_details = get_blog_details($blog_id, false);
276 if ( empty($current_details) )
279 $current_details = get_object_vars($current_details);
281 $details = array_merge($current_details, $details);
282 $details['last_updated'] = current_time('mysql', true);
284 $update_details = array();
285 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
286 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
287 $update_details[$field] = $details[$field];
289 $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
291 // If spam status changed, issue actions.
292 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
293 if ( $details[ 'spam' ] == 1 )
294 do_action( "make_spam_blog", $blog_id );
296 do_action( "make_ham_blog", $blog_id );
299 if ( isset($details[ 'public' ]) )
300 update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
302 refresh_blog_details($blog_id);
308 * Retrieve option value based on setting name and blog_id.
310 * If the option does not exist or does not have a value, then the return value
311 * will be false. This is useful to check whether you need to install an option
312 * and is commonly used during installation of plugin options and to test
313 * whether upgrading is required.
315 * There is a filter called 'blog_option_$option' with the $option being
316 * replaced with the option name. The filter takes two parameters. $value and
317 * $blog_id. It returns $value.
318 * The 'option_$option' filter in get_option() is not called.
321 * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
323 * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog.
324 * @param string $setting Name of option to retrieve. Should already be SQL-escaped.
325 * @param string $default (optional) Default value returned if option not found.
326 * @return mixed Value set for the option.
328 function get_blog_option( $blog_id, $setting, $default = false ) {
331 if ( null === $blog_id )
332 $blog_id = $wpdb->blogid;
334 $key = $blog_id . '-' . $setting . '-blog_option';
335 $value = wp_cache_get( $key, 'site-options' );
336 if ( $value == null ) {
337 if ( $blog_id == $wpdb->blogid ) {
338 $value = get_option( $setting, $default );
339 $notoptions = wp_cache_get( 'notoptions', 'options' );
340 if ( isset( $notoptions[$setting] ) ) {
341 wp_cache_set( $key, 'noop', 'site-options' );
343 } elseif ( $value == false ) {
344 wp_cache_set( $key, 'falsevalue', 'site-options' );
346 wp_cache_set( $key, $value, 'site-options' );
348 return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
350 $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
351 $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
352 if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
353 $value = $row->option_value;
354 if ( $value == false )
355 wp_cache_set( $key, 'falsevalue', 'site-options' );
357 wp_cache_set( $key, $value, 'site-options' );
358 } else { // option does not exist, so we must cache its non-existence
359 wp_cache_set( $key, 'noop', 'site-options' );
363 } elseif ( $value == 'noop' ) {
365 } elseif ( $value == 'falsevalue' ) {
368 // If home is not set use siteurl.
369 if ( 'home' == $setting && '' == $value )
370 return get_blog_option( $blog_id, 'siteurl' );
372 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
373 $value = untrailingslashit( $value );
375 return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
379 * Add an option for a particular blog.
383 * @param int $id The blog id
384 * @param string $key The option key
385 * @param mixed $value The option value
386 * @return bool True on success, false on failure.
388 function add_blog_option( $id, $key, $value ) {
392 $return = add_option( $key, $value );
393 restore_current_blog();
395 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
400 * Delete an option for a particular blog.
404 * @param int $id The blog id
405 * @param string $key The option key
406 * @return bool True on success, false on failure.
408 function delete_blog_option( $id, $key ) {
412 $return = delete_option( $key );
413 restore_current_blog();
415 wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
420 * Update an option for a particular blog.
424 * @param int $id The blog id
425 * @param string $key The option key
426 * @param mixed $value The option value
427 * @return bool True on success, false on failrue.
429 function update_blog_option( $id, $key, $value, $deprecated = null ) {
432 if ( null !== $deprecated )
433 _deprecated_argument( __FUNCTION__, '3.1' );
436 $return = update_option( $key, $value );
437 restore_current_blog();
439 refresh_blog_details( $id );
442 wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');
447 * Switch the current blog.
449 * This function is useful if you need to pull posts, or other information,
450 * from other blogs. You can switch back afterwards using restore_current_blog().
452 * Things that aren't switched:
453 * - autoloaded options. See #14992
454 * - plugins. See #14941
456 * @see restore_current_blog()
459 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
460 * @param bool $validate Whether to check if $new_blog exists before proceeding
461 * @return bool True on success, False if the validation failed
463 function switch_to_blog( $new_blog, $validate = false ) {
464 global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
466 if ( empty($new_blog) )
467 $new_blog = $blog_id;
469 if ( $validate && ! get_blog_details( $new_blog ) )
472 if ( empty($switched_stack) )
473 $switched_stack = array();
475 $switched_stack[] = $blog_id;
477 /* If we're switching to the same blog id that we're on,
478 * set the right vars, do the associated actions, but skip
479 * the extra unnecessary work */
480 if ( $blog_id == $new_blog ) {
481 do_action( 'switch_blog', $blog_id, $blog_id );
486 $wpdb->set_blog_id($new_blog);
487 $table_prefix = $wpdb->prefix;
488 $prev_blog_id = $blog_id;
489 $blog_id = $new_blog;
491 if ( is_object( $wp_roles ) ) {
492 $wpdb->suppress_errors();
493 if ( method_exists( $wp_roles ,'_init' ) )
495 elseif ( method_exists( $wp_roles, '__construct' ) )
496 $wp_roles->__construct();
497 $wpdb->suppress_errors( false );
500 if ( did_action('init') ) {
501 $current_user = wp_get_current_user();
502 if ( is_object( $current_user ) )
503 $current_user->for_blog( $blog_id );
506 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
507 $global_groups = $wp_object_cache->global_groups;
509 $global_groups = false;
512 if ( function_exists('wp_cache_add_global_groups') ) {
513 if ( is_array( $global_groups ) )
514 wp_cache_add_global_groups( $global_groups );
516 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
517 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
520 do_action('switch_blog', $blog_id, $prev_blog_id);
526 * Restore the current blog, after calling switch_to_blog()
528 * @see switch_to_blog()
531 * @return bool True on success, False if we're already on the current blog
533 function restore_current_blog() {
534 global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
539 if ( !is_array( $switched_stack ) )
542 $blog = array_pop( $switched_stack );
543 if ( $blog_id == $blog ) {
544 do_action( 'switch_blog', $blog, $blog );
545 /* If we still have items in the switched stack, consider ourselves still 'switched' */
546 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
550 $wpdb->set_blog_id($blog);
551 $prev_blog_id = $blog_id;
553 $table_prefix = $wpdb->prefix;
555 if ( is_object( $wp_roles ) ) {
556 $wpdb->suppress_errors();
557 if ( method_exists( $wp_roles ,'_init' ) )
559 elseif ( method_exists( $wp_roles, '__construct' ) )
560 $wp_roles->__construct();
561 $wpdb->suppress_errors( false );
564 if ( did_action('init') ) {
565 $current_user = wp_get_current_user();
566 if ( is_object( $current_user ) )
567 $current_user->for_blog( $blog_id );
570 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
571 $global_groups = $wp_object_cache->global_groups;
573 $global_groups = false;
576 if ( function_exists('wp_cache_add_global_groups') ) {
577 if ( is_array( $global_groups ) )
578 wp_cache_add_global_groups( $global_groups );
580 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
581 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
584 do_action('switch_blog', $blog_id, $prev_blog_id);
586 /* If we still have items in the switched stack, consider ourselves still 'switched' */
587 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
592 * Check if a particular blog is archived.
596 * @param int $id The blog id
597 * @return string Whether the blog is archived or not
599 function is_archived( $id ) {
600 return get_blog_status($id, 'archived');
604 * Update the 'archived' status of a particular blog.
608 * @param int $id The blog id
609 * @param string $archived The new status
610 * @return string $archived
612 function update_archived( $id, $archived ) {
613 update_blog_status($id, 'archived', $archived);
618 * Update a blog details field.
622 * @param int $blog_id BLog ID
623 * @param string $pref A field name
624 * @param string $value Value for $pref
625 * @return string $value
627 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
630 if ( null !== $deprecated )
631 _deprecated_argument( __FUNCTION__, '3.1' );
633 if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
636 $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
638 refresh_blog_details($blog_id);
640 if ( 'spam' == $pref )
641 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
642 elseif ( 'mature' == $pref )
643 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
644 elseif ( 'archived' == $pref )
645 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
646 elseif ( 'archived' == $pref )
647 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
653 * Get a blog details field.
657 * @param int $id The blog id
658 * @param string $pref A field name
659 * @return bool $value
661 function get_blog_status( $id, $pref ) {
664 $details = get_blog_details( $id, false );
666 return $details->$pref;
668 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
672 * Get a list of most recently updated blogs.
676 * @param mixed $deprecated Not used
677 * @param int $start The offset
678 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
679 * @return array The list of blogs
681 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
684 if ( ! empty( $deprecated ) )
685 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
687 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 );
691 * Handler for updating the blog date when a post is published or an already published post is changed.
695 * @param string $new_status The new post status
696 * @param string $old_status The old post status
697 * @param object $post Post object
699 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
700 $post_type_obj = get_post_type_object( $post->post_type );
701 if ( ! $post_type_obj->public )
704 if ( 'publish' != $new_status && 'publish' != $old_status )
707 // Post was freshly published, published post was saved, or published post was unpublished.
709 wpmu_update_blogs_date();