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 // TODO: use update_blog_details
21 $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
22 refresh_blog_details( $wpdb->blogid );
24 do_action( 'wpmu_blog_updated', $wpdb->blogid );
28 * Get a full blog URL, given a blog id.
32 * @param int $blog_id Blog ID
35 function get_blogaddress_by_id( $blog_id ) {
36 $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
37 return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
41 * Get a full blog URL, given a blog name.
45 * @param string $blogname The (subdomain or directory) name
48 function get_blogaddress_by_name( $blogname ) {
51 if ( is_subdomain_install() ) {
52 if ( $blogname == 'main' )
54 $url = rtrim( network_home_url(), '/' );
55 if ( !empty( $blogname ) )
56 $url = preg_replace( '|^([^\.]+://)|', '$1' . $blogname . '.', $url );
58 $url = network_home_url( $blogname );
60 return esc_url( $url . '/' );
64 * Get a full blog URL, given a domain and a path.
68 * @param string $domain
72 function get_blogaddress_by_domain( $domain, $path ) {
73 if ( is_subdomain_install() ) {
74 $url = "http://".$domain.$path;
76 if ( $domain != $_SERVER['HTTP_HOST'] ) {
77 $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
78 $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
79 // we're not installing the main blog
80 if ( $blogname != 'www.' )
81 $url .= $blogname . '/';
83 $url = 'http://' . $domain . $path;
86 return esc_url( $url );
90 * Given a blog's (subdomain or directory) name, retrieve it's id.
95 * @return int A blog id
97 function get_id_from_blogname( $name ) {
98 global $wpdb, $current_site;
99 $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
103 if ( is_subdomain_install() ) {
104 $domain = $name . '.' . $current_site->domain;
105 $path = $current_site->path;
107 $domain = $current_site->domain;
108 $path = $current_site->path . $name . '/';
110 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
111 wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
116 * Retrieve the details for a blog from the blogs table and blog options.
120 * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
121 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
122 * @return object Blog details.
124 function get_blog_details( $fields, $get_all = true ) {
127 if ( is_array($fields ) ) {
128 if ( isset($fields['blog_id']) ) {
129 $blog_id = $fields['blog_id'];
130 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
131 $key = md5( $fields['domain'] . $fields['path'] );
132 $blog = wp_cache_get($key, 'blog-lookup');
133 if ( false !== $blog )
135 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
136 $nowww = substr( $fields['domain'], 4 );
137 $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'] ) );
139 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
142 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
143 $blog_id = $blog->blog_id;
147 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
148 $key = md5( $fields['domain'] );
149 $blog = wp_cache_get($key, 'blog-lookup');
150 if ( false !== $blog )
152 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
153 $nowww = substr( $fields['domain'], 4 );
154 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
156 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
159 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
160 $blog_id = $blog->blog_id;
168 if ( !is_numeric( $fields ) )
169 $blog_id = get_id_from_blogname( $fields );
174 $blog_id = (int) $blog_id;
176 $all = $get_all == true ? '' : 'short';
177 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
180 if ( ! is_object( $details ) ) {
181 if ( $details == -1 ) {
184 // Clear old pre-serialized objects. Cache clients do better with that.
185 wp_cache_delete( $blog_id . $all, 'blog-details' );
193 // Try the other cache.
195 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
197 $details = wp_cache_get( $blog_id, 'blog-details' );
198 // If short was requested and full cache is set, we can return.
200 if ( ! is_object( $details ) ) {
201 if ( $details == -1 ) {
204 // Clear old pre-serialized objects. Cache clients do better with that.
205 wp_cache_delete( $blog_id, 'blog-details' );
214 if ( empty($details) ) {
215 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
217 // Set the full cache.
218 wp_cache_set( $blog_id, -1, 'blog-details' );
224 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
228 $details->blogname = get_blog_option( $blog_id, 'blogname' );
229 $details->siteurl = get_blog_option( $blog_id, 'siteurl' );
230 $details->post_count = get_blog_option( $blog_id, 'post_count' );
232 $details = apply_filters( 'blog_details', $details );
234 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
236 $key = md5( $details->domain . $details->path );
237 wp_cache_set( $key, $details, 'blog-lookup' );
243 * Clear the blog details cache.
247 * @param int $blog_id Blog ID
249 function refresh_blog_details( $blog_id ) {
250 $blog_id = (int) $blog_id;
251 $details = get_blog_details( $blog_id, false );
253 wp_cache_delete( $blog_id , 'blog-details' );
254 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
255 wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
256 wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
257 wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
261 * Update the details for a blog. Updates the blogs table for a given blog id.
265 * @param int $blog_id Blog ID
266 * @param array $details Array of details keyed by blogs table field names.
267 * @return bool True if update succeeds, false otherwise.
269 function update_blog_details( $blog_id, $details = array() ) {
272 if ( empty($details) )
275 if ( is_object($details) )
276 $details = get_object_vars($details);
278 $current_details = get_blog_details($blog_id, false);
279 if ( empty($current_details) )
282 $current_details = get_object_vars($current_details);
284 $details = array_merge($current_details, $details);
285 $details['last_updated'] = current_time('mysql', true);
287 $update_details = array();
288 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
289 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
290 $update_details[$field] = $details[$field];
292 $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
294 // If spam status changed, issue actions.
295 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
296 if ( $details[ 'spam' ] == 1 )
297 do_action( "make_spam_blog", $blog_id );
299 do_action( "make_ham_blog", $blog_id );
302 if ( isset($details[ 'public' ]) )
303 update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
305 refresh_blog_details($blog_id);
311 * Retrieve option value based on setting name and blog_id.
313 * If the option does not exist or does not have a value, then the return value
314 * will be false. This is useful to check whether you need to install an option
315 * and is commonly used during installation of plugin options and to test
316 * whether upgrading is required.
318 * There is a filter called 'blog_option_$option' with the $option being
319 * replaced with the option name. The filter takes two parameters. $value and
320 * $blog_id. It returns $value.
321 * The 'option_$option' filter in get_option() is not called.
324 * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
326 * @param int $blog_id is the id of the blog.
327 * @param string $setting Name of option to retrieve. Should already be SQL-escaped.
328 * @param string $default (optional) Default value returned if option not found.
329 * @return mixed Value set for the option.
331 function get_blog_option( $blog_id, $setting, $default = false ) {
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
387 function add_blog_option( $id, $key, $value ) {
391 add_option( $key, $value );
392 restore_current_blog();
393 wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
397 * Delete an option for a particular blog.
401 * @param int $id The blog id
402 * @param string $key The option key
404 function delete_blog_option( $id, $key ) {
408 delete_option( $key );
409 restore_current_blog();
410 wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
414 * Update an option for a particular blog.
418 * @param int $id The blog id
419 * @param string $key The option key
420 * @param mixed $value The option value
422 function update_blog_option( $id, $key, $value, $deprecated = null ) {
425 if ( null !== $deprecated )
426 _deprecated_argument( __FUNCTION__, '3.1' );
429 update_option( $key, $value );
430 restore_current_blog();
432 refresh_blog_details( $id );
434 wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
438 * Switch the current blog.
440 * This function is useful if you need to pull posts, or other information,
441 * from other blogs. You can switch back afterwards using restore_current_blog().
443 * Things that aren't switched:
444 * - autoloaded options. See #14992
445 * - plugins. See #14941
447 * @see restore_current_blog()
450 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
451 * @param bool $validate Whether to check if $new_blog exists before proceeding
452 * @return bool True on success, False if the validation failed
454 function switch_to_blog( $new_blog, $validate = false ) {
455 global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
457 if ( empty($new_blog) )
458 $new_blog = $blog_id;
460 if ( $validate && ! get_blog_details( $new_blog ) )
463 if ( empty($switched_stack) )
464 $switched_stack = array();
466 $switched_stack[] = $blog_id;
468 /* If we're switching to the same blog id that we're on,
469 * set the right vars, do the associated actions, but skip
470 * the extra unnecessary work */
471 if ( $blog_id == $new_blog ) {
472 do_action( 'switch_blog', $blog_id, $blog_id );
477 $wpdb->set_blog_id($new_blog);
478 $table_prefix = $wpdb->prefix;
479 $prev_blog_id = $blog_id;
480 $blog_id = $new_blog;
482 if ( is_object( $wp_roles ) ) {
483 $wpdb->suppress_errors();
484 if ( method_exists( $wp_roles ,'_init' ) )
486 elseif ( method_exists( $wp_roles, '__construct' ) )
487 $wp_roles->__construct();
488 $wpdb->suppress_errors( false );
491 if ( did_action('init') ) {
492 $current_user = wp_get_current_user();
493 if ( is_object( $current_user ) )
494 $current_user->for_blog( $blog_id );
497 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
498 $global_groups = $wp_object_cache->global_groups;
500 $global_groups = false;
503 if ( function_exists('wp_cache_add_global_groups') ) {
504 if ( is_array( $global_groups ) )
505 wp_cache_add_global_groups( $global_groups );
507 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
508 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
511 do_action('switch_blog', $blog_id, $prev_blog_id);
517 * Restore the current blog, after calling switch_to_blog()
519 * @see switch_to_blog()
522 * @return bool True on success, False if we're already on the current blog
524 function restore_current_blog() {
525 global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
530 if ( !is_array( $switched_stack ) )
533 $blog = array_pop( $switched_stack );
534 if ( $blog_id == $blog ) {
535 do_action( 'switch_blog', $blog, $blog );
536 /* If we still have items in the switched stack, consider ourselves still 'switched' */
537 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
541 $wpdb->set_blog_id($blog);
542 $prev_blog_id = $blog_id;
544 $table_prefix = $wpdb->prefix;
546 if ( is_object( $wp_roles ) ) {
547 $wpdb->suppress_errors();
548 if ( method_exists( $wp_roles ,'_init' ) )
550 elseif ( method_exists( $wp_roles, '__construct' ) )
551 $wp_roles->__construct();
552 $wpdb->suppress_errors( false );
555 if ( did_action('init') ) {
556 $current_user = wp_get_current_user();
557 if ( is_object( $current_user ) )
558 $current_user->for_blog( $blog_id );
561 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
562 $global_groups = $wp_object_cache->global_groups;
564 $global_groups = false;
567 if ( function_exists('wp_cache_add_global_groups') ) {
568 if ( is_array( $global_groups ) )
569 wp_cache_add_global_groups( $global_groups );
571 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
572 wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
575 do_action('switch_blog', $blog_id, $prev_blog_id);
577 /* If we still have items in the switched stack, consider ourselves still 'switched' */
578 $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
583 * Check if a particular blog is archived.
587 * @param int $id The blog id
588 * @return string Whether the blog is archived or not
590 function is_archived( $id ) {
591 return get_blog_status($id, 'archived');
595 * Update the 'archived' status of a particular blog.
599 * @param int $id The blog id
600 * @param string $archived The new status
601 * @return string $archived
603 function update_archived( $id, $archived ) {
604 update_blog_status($id, 'archived', $archived);
609 * Update a blog details field.
613 * @param int $blog_id BLog ID
614 * @param string $pref A field name
615 * @param string $value Value for $pref
616 * @return string $value
618 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
621 if ( null !== $deprecated )
622 _deprecated_argument( __FUNCTION__, '3.1' );
624 if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
627 $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
629 refresh_blog_details($blog_id);
631 if ( 'spam' == $pref )
632 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
633 elseif ( 'mature' == $pref )
634 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
635 elseif ( 'archived' == $pref )
636 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
637 elseif ( 'archived' == $pref )
638 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
644 * Get a blog details field.
648 * @param int $id The blog id
649 * @param string $pref A field name
650 * @return bool $value
652 function get_blog_status( $id, $pref ) {
655 $details = get_blog_details( $id, false );
657 return $details->$pref;
659 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
663 * Get a list of most recently updated blogs.
667 * @param mixed $deprecated Not used
668 * @param int $start The offset
669 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
670 * @return array The list of blogs
672 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
675 if ( ! empty( $deprecated ) )
676 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
678 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 );