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 * Given a blog's (subdomain or directory) slug, retrieve its id.
64 * @return int A blog id
66 function get_id_from_blogname( $slug ) {
67 global $wpdb, $current_site;
69 $slug = trim( $slug, '/' );
71 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
75 if ( is_subdomain_install() ) {
76 $domain = $slug . '.' . $current_site->domain;
77 $path = $current_site->path;
79 $domain = $current_site->domain;
80 $path = $current_site->path . $slug . '/';
83 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
84 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
89 * Retrieve the details for a blog from the blogs table and blog options.
93 * @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.
94 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
95 * @return object Blog details.
97 function get_blog_details( $fields = null, $get_all = true ) {
100 if ( is_array($fields ) ) {
101 if ( isset($fields['blog_id']) ) {
102 $blog_id = $fields['blog_id'];
103 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
104 $key = md5( $fields['domain'] . $fields['path'] );
105 $blog = wp_cache_get($key, 'blog-lookup');
106 if ( false !== $blog )
108 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
109 $nowww = substr( $fields['domain'], 4 );
110 $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'] ) );
112 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
115 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
116 $blog_id = $blog->blog_id;
120 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
121 $key = md5( $fields['domain'] );
122 $blog = wp_cache_get($key, 'blog-lookup');
123 if ( false !== $blog )
125 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
126 $nowww = substr( $fields['domain'], 4 );
127 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
129 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
132 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
133 $blog_id = $blog->blog_id;
142 $blog_id = get_current_blog_id();
143 elseif ( ! is_numeric( $fields ) )
144 $blog_id = get_id_from_blogname( $fields );
149 $blog_id = (int) $blog_id;
151 $all = $get_all == true ? '' : 'short';
152 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
155 if ( ! is_object( $details ) ) {
156 if ( $details == -1 ) {
159 // Clear old pre-serialized objects. Cache clients do better with that.
160 wp_cache_delete( $blog_id . $all, 'blog-details' );
168 // Try the other cache.
170 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
172 $details = wp_cache_get( $blog_id, 'blog-details' );
173 // If short was requested and full cache is set, we can return.
175 if ( ! is_object( $details ) ) {
176 if ( $details == -1 ) {
179 // Clear old pre-serialized objects. Cache clients do better with that.
180 wp_cache_delete( $blog_id, 'blog-details' );
189 if ( empty($details) ) {
190 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
192 // Set the full cache.
193 wp_cache_set( $blog_id, -1, 'blog-details' );
199 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
203 switch_to_blog( $blog_id );
204 $details->blogname = get_option( 'blogname' );
205 $details->siteurl = get_option( 'siteurl' );
206 $details->post_count = get_option( 'post_count' );
207 restore_current_blog();
209 $details = apply_filters( 'blog_details', $details );
211 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
213 $key = md5( $details->domain . $details->path );
214 wp_cache_set( $key, $details, 'blog-lookup' );
220 * Clear the blog details cache.
224 * @param int $blog_id Blog ID
226 function refresh_blog_details( $blog_id ) {
227 $blog_id = (int) $blog_id;
228 $details = get_blog_details( $blog_id, false );
230 // Make sure clean_blog_cache() gets the blog ID
231 // when the blog has been previously cached as
233 $details = (object) array(
234 'blog_id' => $blog_id,
240 clean_blog_cache( $details );
242 do_action( 'refresh_blog_details', $blog_id );
246 * Update the details for a blog. Updates the blogs table for a given blog id.
250 * @param int $blog_id Blog ID
251 * @param array $details Array of details keyed by blogs table field names.
252 * @return bool True if update succeeds, false otherwise.
254 function update_blog_details( $blog_id, $details = array() ) {
257 if ( empty($details) )
260 if ( is_object($details) )
261 $details = get_object_vars($details);
263 $current_details = get_blog_details($blog_id, false);
264 if ( empty($current_details) )
267 $current_details = get_object_vars($current_details);
269 $details = array_merge($current_details, $details);
270 $details['last_updated'] = current_time('mysql', true);
272 $update_details = array();
273 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
274 foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
275 $update_details[$field] = $details[$field];
277 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
279 if ( false === $result )
282 // If spam status changed, issue actions.
283 if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
284 if ( $details[ 'spam' ] == 1 )
285 do_action( 'make_spam_blog', $blog_id );
287 do_action( 'make_ham_blog', $blog_id );
290 // If mature status changed, issue actions.
291 if ( $details[ 'mature' ] != $current_details[ 'mature' ] ) {
292 if ( $details[ 'mature' ] == 1 )
293 do_action( 'mature_blog', $blog_id );
295 do_action( 'unmature_blog', $blog_id );
298 // If archived status changed, issue actions.
299 if ( $details[ 'archived' ] != $current_details[ 'archived' ] ) {
300 if ( $details[ 'archived' ] == 1 )
301 do_action( 'archive_blog', $blog_id );
303 do_action( 'unarchive_blog', $blog_id );
306 // If deleted status changed, issue actions.
307 if ( $details[ 'deleted' ] != $current_details[ 'deleted' ] ) {
308 if ( $details[ 'deleted' ] == 1 )
309 do_action( 'make_delete_blog', $blog_id );
311 do_action( 'make_undelete_blog', $blog_id );
314 if ( isset( $details[ 'public' ] ) ) {
315 switch_to_blog( $blog_id );
316 update_option( 'blog_public', $details[ 'public' ] );
317 restore_current_blog();
320 refresh_blog_details($blog_id);
326 * Clean the blog cache
330 * @param stdClass $blog The blog details as returned from get_blog_details()
332 function clean_blog_cache( $blog ) {
333 $blog_id = $blog->blog_id;
334 $domain_path_key = md5( $blog->domain . $blog->path );
336 wp_cache_delete( $blog_id , 'blog-details' );
337 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
338 wp_cache_delete( $domain_path_key, 'blog-lookup' );
339 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
340 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
341 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
342 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
346 * Retrieve option value for a given blog id based on name of option.
348 * If the option does not exist or does not have a value, then the return value
349 * will be false. This is useful to check whether you need to install an option
350 * and is commonly used during installation of plugin options and to test
351 * whether upgrading is required.
353 * If the option was serialized then it will be unserialized when it is returned.
357 * @param int $id A blog ID. Can be null to refer to the current blog.
358 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
359 * @param mixed $default Optional. Default value to return if the option does not exist.
360 * @return mixed Value set for the option.
362 function get_blog_option( $id, $option, $default = false ) {
366 $id = get_current_blog_id();
368 if ( get_current_blog_id() == $id )
369 return get_option( $option, $default );
371 switch_to_blog( $id );
372 $value = get_option( $option, $default );
373 restore_current_blog();
375 return apply_filters( 'blog_option_' . $option, $value, $id );
379 * Add a new option for a given blog id.
381 * You do not need to serialize values. If the value needs to be serialized, then
382 * it will be serialized before it is inserted into the database. Remember,
383 * resources can not be serialized or added as an option.
385 * You can create options without values and then update the values later.
386 * Existing options will not be updated and checks are performed to ensure that you
387 * aren't adding a protected WordPress option. Care should be taken to not name
388 * options the same as the ones which are protected.
392 * @param int $id A blog ID. Can be null to refer to the current blog.
393 * @param string $option Name of option to add. Expected to not be SQL-escaped.
394 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
395 * @return bool False if option was not added and true if option was added.
397 function add_blog_option( $id, $option, $value ) {
401 $id = get_current_blog_id();
403 if ( get_current_blog_id() == $id )
404 return add_option( $option, $value );
406 switch_to_blog( $id );
407 $return = add_option( $option, $value );
408 restore_current_blog();
414 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
418 * @param int $id A blog ID. Can be null to refer to the current blog.
419 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
420 * @return bool True, if option is successfully deleted. False on failure.
422 function delete_blog_option( $id, $option ) {
426 $id = get_current_blog_id();
428 if ( get_current_blog_id() == $id )
429 return delete_option( $option );
431 switch_to_blog( $id );
432 $return = delete_option( $option );
433 restore_current_blog();
439 * Update an option for a particular blog.
443 * @param int $id The blog id
444 * @param string $option The option key
445 * @param mixed $value The option value
446 * @return bool True on success, false on failure.
448 function update_blog_option( $id, $option, $value, $deprecated = null ) {
451 if ( null !== $deprecated )
452 _deprecated_argument( __FUNCTION__, '3.1' );
454 if ( get_current_blog_id() == $id )
455 return update_option( $option, $value );
457 switch_to_blog( $id );
458 $return = update_option( $option, $value );
459 restore_current_blog();
461 refresh_blog_details( $id );
467 * Switch the current blog.
469 * This function is useful if you need to pull posts, or other information,
470 * from other blogs. You can switch back afterwards using restore_current_blog().
472 * Things that aren't switched:
473 * - autoloaded options. See #14992
474 * - plugins. See #14941
476 * @see restore_current_blog()
479 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
480 * @param bool $deprecated Deprecated argument
481 * @return bool True on success, false if the validation failed
483 function switch_to_blog( $new_blog, $deprecated = null ) {
484 global $wpdb, $wp_roles;
486 if ( empty( $new_blog ) )
487 $new_blog = $GLOBALS['blog_id'];
489 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
491 /* If we're switching to the same blog id that we're on,
492 * set the right vars, do the associated actions, but skip
493 * the extra unnecessary work */
494 if ( $new_blog == $GLOBALS['blog_id'] ) {
495 do_action( 'switch_blog', $new_blog, $new_blog );
496 $GLOBALS['switched'] = true;
500 $wpdb->set_blog_id( $new_blog );
501 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
502 $prev_blog_id = $GLOBALS['blog_id'];
503 $GLOBALS['blog_id'] = $new_blog;
505 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
506 wp_cache_switch_to_blog( $new_blog );
508 global $wp_object_cache;
510 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
511 $global_groups = $wp_object_cache->global_groups;
513 $global_groups = false;
517 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
518 if ( is_array( $global_groups ) )
519 wp_cache_add_global_groups( $global_groups );
521 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' ) );
522 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
526 if ( did_action( 'init' ) ) {
528 $current_user = wp_get_current_user();
529 $current_user->for_blog( $new_blog );
532 do_action( 'switch_blog', $new_blog, $prev_blog_id );
533 $GLOBALS['switched'] = true;
539 * Restore the current blog, after calling switch_to_blog()
541 * @see switch_to_blog()
544 * @return bool True on success, false if we're already on the current blog
546 function restore_current_blog() {
547 global $wpdb, $wp_roles;
549 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
552 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
554 if ( $GLOBALS['blog_id'] == $blog ) {
555 do_action( 'switch_blog', $blog, $blog );
556 // If we still have items in the switched stack, consider ourselves still 'switched'
557 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
561 $wpdb->set_blog_id( $blog );
562 $prev_blog_id = $GLOBALS['blog_id'];
563 $GLOBALS['blog_id'] = $blog;
564 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
566 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
567 wp_cache_switch_to_blog( $blog );
569 global $wp_object_cache;
571 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
572 $global_groups = $wp_object_cache->global_groups;
574 $global_groups = false;
578 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
579 if ( is_array( $global_groups ) )
580 wp_cache_add_global_groups( $global_groups );
582 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' ) );
583 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
587 if ( did_action( 'init' ) ) {
589 $current_user = wp_get_current_user();
590 $current_user->for_blog( $blog );
593 do_action( 'switch_blog', $blog, $prev_blog_id );
595 // If we still have items in the switched stack, consider ourselves still 'switched'
596 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
602 * Determines if switch_to_blog() is in effect
606 * @return bool True if switched, false otherwise.
608 function ms_is_switched() {
609 return ! empty( $GLOBALS['_wp_switched_stack'] );
613 * Check if a particular blog is archived.
617 * @param int $id The blog id
618 * @return string Whether the blog is archived or not
620 function is_archived( $id ) {
621 return get_blog_status($id, 'archived');
625 * Update the 'archived' status of a particular blog.
629 * @param int $id The blog id
630 * @param string $archived The new status
631 * @return string $archived
633 function update_archived( $id, $archived ) {
634 update_blog_status($id, 'archived', $archived);
639 * Update a blog details field.
643 * @param int $blog_id BLog ID
644 * @param string $pref A field name
645 * @param string $value Value for $pref
646 * @return string $value
648 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
651 if ( null !== $deprecated )
652 _deprecated_argument( __FUNCTION__, '3.1' );
654 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
657 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
659 if ( false === $result )
662 refresh_blog_details( $blog_id );
664 if ( 'spam' == $pref )
665 ( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) : do_action( 'make_ham_blog', $blog_id );
666 elseif ( 'mature' == $pref )
667 ( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
668 elseif ( 'archived' == $pref )
669 ( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
670 elseif ( 'deleted' == $pref )
671 ( $value == 1 ) ? do_action( 'make_delete_blog', $blog_id ) : do_action( 'make_undelete_blog', $blog_id );
672 elseif ( 'public' == $pref )
673 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
679 * Get a blog details field.
683 * @param int $id The blog id
684 * @param string $pref A field name
685 * @return bool $value
687 function get_blog_status( $id, $pref ) {
690 $details = get_blog_details( $id, false );
692 return $details->$pref;
694 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
698 * Get a list of most recently updated blogs.
702 * @param mixed $deprecated Not used
703 * @param int $start The offset
704 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
705 * @return array The list of blogs
707 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
710 if ( ! empty( $deprecated ) )
711 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
713 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 );
717 * Handler for updating the blog date when a post is published or an already published post is changed.
721 * @param string $new_status The new post status
722 * @param string $old_status The old post status
723 * @param object $post Post object
725 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
726 $post_type_obj = get_post_type_object( $post->post_type );
727 if ( ! $post_type_obj->public )
730 if ( 'publish' != $new_status && 'publish' != $old_status )
733 // Post was freshly published, published post was saved, or published post was unpublished.
735 wpmu_update_blogs_date();
739 * Handler for updating the blog date when a published post is deleted.
743 * @param int $post_id Post ID
745 function _update_blog_date_on_post_delete( $post_id ) {
746 $post = get_post( $post_id );
748 $post_type_obj = get_post_type_object( $post->post_type );
749 if ( ! $post_type_obj->public )
752 if ( 'publish' != $post->post_status )
755 wpmu_update_blogs_date();