web/wp-includes/ms-blogs.php
changeset 194 32102edaa81b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Site/blog functions that work with the blogs table and related data.
       
     5  *
       
     6  * @package WordPress
       
     7  * @subpackage Multisite
       
     8  * @since MU
       
     9  */
       
    10 
       
    11 /**
       
    12  * Update the last_updated field for the current blog.
       
    13  *
       
    14  * @since MU
       
    15  */
       
    16 function wpmu_update_blogs_date() {
       
    17 	global $wpdb;
       
    18 
       
    19 	update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
       
    20 
       
    21 	do_action( 'wpmu_blog_updated', $wpdb->blogid );
       
    22 }
       
    23 
       
    24 /**
       
    25  * Get a full blog URL, given a blog id.
       
    26  *
       
    27  * @since MU
       
    28  *
       
    29  * @param int $blog_id Blog ID
       
    30  * @return string
       
    31  */
       
    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 );
       
    35 }
       
    36 
       
    37 /**
       
    38  * Get a full blog URL, given a blog name.
       
    39  *
       
    40  * @since MU
       
    41  *
       
    42  * @param string $blogname The (subdomain or directory) name
       
    43  * @return string
       
    44  */
       
    45 function get_blogaddress_by_name( $blogname ) {
       
    46 	global $current_site;
       
    47 
       
    48 	if ( is_subdomain_install() ) {
       
    49 		if ( $blogname == 'main' )
       
    50 			$blogname = 'www';
       
    51 		$url = rtrim( network_home_url(), '/' );
       
    52 		if ( !empty( $blogname ) )
       
    53 			$url = preg_replace( '|^([^\.]+://)|', '$1' . $blogname . '.', $url );
       
    54 	} else {
       
    55 		$url = network_home_url( $blogname );
       
    56 	}
       
    57 	return esc_url( $url . '/' );
       
    58 }
       
    59 
       
    60 /**
       
    61  * Get a full blog URL, given a domain and a path.
       
    62  *
       
    63  * @since MU
       
    64  *
       
    65  * @param string $domain
       
    66  * @param string $path
       
    67  * @return string
       
    68  */
       
    69 function get_blogaddress_by_domain( $domain, $path ) {
       
    70 	if ( is_subdomain_install() ) {
       
    71 		$url = "http://" . $domain.$path;
       
    72 	} else {
       
    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 . '/';
       
    79 		} else { // main blog
       
    80 			$url = 'http://' . $domain . $path;
       
    81 		}
       
    82 	}
       
    83 	return esc_url( $url );
       
    84 }
       
    85 
       
    86 /**
       
    87  * Given a blog's (subdomain or directory) name, retrieve it's id.
       
    88  *
       
    89  * @since MU
       
    90  *
       
    91  * @param string $name
       
    92  * @return int A blog id
       
    93  */
       
    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' );
       
    97 	if ( $blog_id )
       
    98 		return $blog_id;
       
    99 
       
   100 	if ( is_subdomain_install() ) {
       
   101 		$domain = $name . '.' . $current_site->domain;
       
   102 		$path = $current_site->path;
       
   103 	} else {
       
   104 		$domain = $current_site->domain;
       
   105 		$path = $current_site->path . $name . '/';
       
   106 	}
       
   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' );
       
   109 	return $blog_id;
       
   110 }
       
   111 
       
   112 /**
       
   113  * Retrieve the details for a blog from the blogs table and blog options.
       
   114  *
       
   115  * @since MU
       
   116  *
       
   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.
       
   120  */
       
   121 function get_blog_details( $fields, $get_all = true ) {
       
   122 	global $wpdb;
       
   123 
       
   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 )
       
   131 				return $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'] ) );
       
   135 			} else {
       
   136 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
       
   137 			}
       
   138 			if ( $blog ) {
       
   139 				wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
       
   140 				$blog_id = $blog->blog_id;
       
   141 			} else {
       
   142 				return false;
       
   143 			}
       
   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 )
       
   148 				return $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'] ) );
       
   152 			} else {
       
   153 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
       
   154 			}
       
   155 			if ( $blog ) {
       
   156 				wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
       
   157 				$blog_id = $blog->blog_id;
       
   158 			} else {
       
   159 				return false;
       
   160 			}
       
   161 		} else {
       
   162 			return false;
       
   163 		}
       
   164 	} else {
       
   165 		if ( !is_numeric( $fields ) )
       
   166 			$blog_id = get_id_from_blogname( $fields );
       
   167 		else
       
   168 			$blog_id = $fields;
       
   169 	}
       
   170 
       
   171 	$blog_id = (int) $blog_id;
       
   172 
       
   173 	$all = $get_all == true ? '' : 'short';
       
   174 	$details = wp_cache_get( $blog_id . $all, 'blog-details' );
       
   175 
       
   176 	if ( $details ) {
       
   177 		if ( ! is_object( $details ) ) {
       
   178 			if ( $details == -1 ) {
       
   179 				return false;
       
   180 			} else {
       
   181 				// Clear old pre-serialized objects. Cache clients do better with that.
       
   182 				wp_cache_delete( $blog_id . $all, 'blog-details' );
       
   183 				unset($details);
       
   184 			}
       
   185 		} else {
       
   186 			return $details;
       
   187 		}
       
   188 	}
       
   189 
       
   190 	// Try the other cache.
       
   191 	if ( $get_all ) {
       
   192 		$details = wp_cache_get( $blog_id . 'short', 'blog-details' );
       
   193 	} else {
       
   194 		$details = wp_cache_get( $blog_id, 'blog-details' );
       
   195 		// If short was requested and full cache is set, we can return.
       
   196 		if ( $details ) {
       
   197 			if ( ! is_object( $details ) ) {
       
   198 				if ( $details == -1 ) {
       
   199 					return false;
       
   200 				} else {
       
   201 					// Clear old pre-serialized objects. Cache clients do better with that.
       
   202 					wp_cache_delete( $blog_id, 'blog-details' );
       
   203 					unset($details);
       
   204 				}
       
   205 			} else {
       
   206 				return $details;
       
   207 			}
       
   208 		}
       
   209 	}
       
   210 
       
   211 	if ( empty($details) ) {
       
   212 		$details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
       
   213 		if ( ! $details ) {
       
   214 			// Set the full cache.
       
   215 			wp_cache_set( $blog_id, -1, 'blog-details' );
       
   216 			return false;
       
   217 		}
       
   218 	}
       
   219 
       
   220 	if ( ! $get_all ) {
       
   221 		wp_cache_set( $blog_id . $all, $details, 'blog-details' );
       
   222 		return $details;
       
   223 	}
       
   224 
       
   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' );
       
   228 
       
   229 	$details = apply_filters( 'blog_details', $details );
       
   230 
       
   231 	wp_cache_set( $blog_id . $all, $details, 'blog-details' );
       
   232 
       
   233 	$key = md5( $details->domain . $details->path );
       
   234 	wp_cache_set( $key, $details, 'blog-lookup' );
       
   235 
       
   236 	return $details;
       
   237 }
       
   238 
       
   239 /**
       
   240  * Clear the blog details cache.
       
   241  *
       
   242  * @since MU
       
   243  *
       
   244  * @param int $blog_id Blog ID
       
   245  */
       
   246 function refresh_blog_details( $blog_id ) {
       
   247 	$blog_id = (int) $blog_id;
       
   248 	$details = get_blog_details( $blog_id, false );
       
   249 
       
   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' );
       
   255 
       
   256 	do_action( 'refresh_blog_details', $blog_id );
       
   257 }
       
   258 
       
   259 /**
       
   260  * Update the details for a blog. Updates the blogs table for a given blog id.
       
   261  *
       
   262  * @since MU
       
   263  *
       
   264  * @param int $blog_id Blog ID
       
   265  * @param array $details Array of details keyed by blogs table field names.
       
   266  * @return bool True if update succeeds, false otherwise.
       
   267  */
       
   268 function update_blog_details( $blog_id, $details = array() ) {
       
   269 	global $wpdb;
       
   270 
       
   271 	if ( empty($details) )
       
   272 		return false;
       
   273 
       
   274 	if ( is_object($details) )
       
   275 		$details = get_object_vars($details);
       
   276 
       
   277 	$current_details = get_blog_details($blog_id, false);
       
   278 	if ( empty($current_details) )
       
   279 		return false;
       
   280 
       
   281 	$current_details = get_object_vars($current_details);
       
   282 
       
   283 	$details = array_merge($current_details, $details);
       
   284 	$details['last_updated'] = current_time('mysql', true);
       
   285 
       
   286 	$update_details = array();
       
   287 	$fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
       
   288 	foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
       
   289 		$update_details[$field] = $details[$field];
       
   290 
       
   291 	$wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
       
   292 
       
   293 	// If spam status changed, issue actions.
       
   294 	if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
       
   295 		if ( $details[ 'spam' ] == 1 )
       
   296 			do_action( "make_spam_blog", $blog_id );
       
   297 		else
       
   298 			do_action( "make_ham_blog", $blog_id );
       
   299 	}
       
   300 
       
   301 	if ( isset($details[ 'public' ]) )
       
   302 		update_blog_option( $blog_id, 'blog_public', $details[ 'public' ] );
       
   303 
       
   304 	refresh_blog_details($blog_id);
       
   305 
       
   306 	return true;
       
   307 }
       
   308 
       
   309 /**
       
   310  * Retrieve option value based on setting name and blog_id.
       
   311  *
       
   312  * If the option does not exist or does not have a value, then the return value
       
   313  * will be false. This is useful to check whether you need to install an option
       
   314  * and is commonly used during installation of plugin options and to test
       
   315  * whether upgrading is required.
       
   316  *
       
   317  * There is a filter called 'blog_option_$option' with the $option being
       
   318  * replaced with the option name. The filter takes two parameters. $value and
       
   319  * $blog_id. It returns $value.
       
   320  * The 'option_$option' filter in get_option() is not called.
       
   321  *
       
   322  * @since MU
       
   323  * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
       
   324  *
       
   325  * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog.
       
   326  * @param string $setting Name of option to retrieve. Should already be SQL-escaped.
       
   327  * @param string $default (optional) Default value returned if option not found.
       
   328  * @return mixed Value set for the option.
       
   329  */
       
   330 function get_blog_option( $blog_id, $setting, $default = false ) {
       
   331 	global $wpdb;
       
   332 
       
   333 	if ( null === $blog_id )
       
   334 		$blog_id = $wpdb->blogid;
       
   335 
       
   336 	$key = $blog_id . '-' . $setting . '-blog_option';
       
   337 	$value = wp_cache_get( $key, 'site-options' );
       
   338 	if ( $value == null ) {
       
   339 		if ( $blog_id == $wpdb->blogid ) {
       
   340 			$value = get_option( $setting, $default );
       
   341 			$notoptions = wp_cache_get( 'notoptions', 'options' );
       
   342 			if ( isset( $notoptions[$setting] ) ) {
       
   343 				wp_cache_set( $key, 'noop', 'site-options' );
       
   344 				$value = $default;
       
   345 			} elseif ( $value == false ) {
       
   346 				wp_cache_set( $key, 'falsevalue', 'site-options' );
       
   347 			} else {
       
   348 				wp_cache_set( $key, $value, 'site-options' );
       
   349 			}
       
   350 			return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
       
   351 		} else {
       
   352 			$blog_prefix = $wpdb->get_blog_prefix( $blog_id );
       
   353 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
       
   354 			if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
       
   355 				$value = $row->option_value;
       
   356 				if ( $value == false )
       
   357 					wp_cache_set( $key, 'falsevalue', 'site-options' );
       
   358 				else
       
   359 					wp_cache_set( $key, $value, 'site-options' );
       
   360 			} else { // option does not exist, so we must cache its non-existence
       
   361 				wp_cache_set( $key, 'noop', 'site-options' );
       
   362 				$value = $default;
       
   363 			}
       
   364 		}
       
   365 	} elseif ( $value == 'noop' ) {
       
   366 		$value = $default;
       
   367 	} elseif ( $value == 'falsevalue' ) {
       
   368 		$value = false;
       
   369 	}
       
   370 	// If home is not set use siteurl.
       
   371 	if ( 'home' == $setting && '' == $value )
       
   372 		return get_blog_option( $blog_id, 'siteurl' );
       
   373 
       
   374 	if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
       
   375 		$value = untrailingslashit( $value );
       
   376 
       
   377 	return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
       
   378 }
       
   379 
       
   380 /**
       
   381  * Add an option for a particular blog.
       
   382  *
       
   383  * @since MU
       
   384  *
       
   385  * @param int $id The blog id
       
   386  * @param string $key The option key
       
   387  * @param mixed $value The option value
       
   388  * @return bool True on success, false on failure.
       
   389  */
       
   390 function add_blog_option( $id, $key, $value ) {
       
   391 	$id = (int) $id;
       
   392 
       
   393 	switch_to_blog($id);
       
   394 	$return = add_option( $key, $value );
       
   395 	restore_current_blog();
       
   396 	if ( $return )
       
   397 		wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
       
   398 	return $return;
       
   399 }
       
   400 
       
   401 /**
       
   402  * Delete an option for a particular blog.
       
   403  *
       
   404  * @since MU
       
   405  *
       
   406  * @param int $id The blog id
       
   407  * @param string $key The option key
       
   408  * @return bool True on success, false on failure.
       
   409  */
       
   410 function delete_blog_option( $id, $key ) {
       
   411 	$id = (int) $id;
       
   412 
       
   413 	switch_to_blog($id);
       
   414 	$return = delete_option( $key );
       
   415 	restore_current_blog();
       
   416 	if ( $return )
       
   417 		wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
       
   418 	return $return;
       
   419 }
       
   420 
       
   421 /**
       
   422  * Update an option for a particular blog.
       
   423  *
       
   424  * @since MU
       
   425  *
       
   426  * @param int $id The blog id
       
   427  * @param string $key The option key
       
   428  * @param mixed $value The option value
       
   429  * @return bool True on success, false on failrue.
       
   430  */
       
   431 function update_blog_option( $id, $key, $value, $deprecated = null ) {
       
   432 	$id = (int) $id;
       
   433 
       
   434 	if ( null !== $deprecated  )
       
   435 		_deprecated_argument( __FUNCTION__, '3.1' );
       
   436 
       
   437 	switch_to_blog($id);
       
   438 	$return = update_option( $key, $value );
       
   439 	restore_current_blog();
       
   440 
       
   441 	refresh_blog_details( $id );
       
   442 
       
   443 	if ( $return )
       
   444 		wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');
       
   445 	return $return;
       
   446 }
       
   447 
       
   448 /**
       
   449  * Switch the current blog.
       
   450  *
       
   451  * This function is useful if you need to pull posts, or other information,
       
   452  * from other blogs. You can switch back afterwards using restore_current_blog().
       
   453  *
       
   454  * Things that aren't switched:
       
   455  *  - autoloaded options. See #14992
       
   456  *  - plugins. See #14941
       
   457  *
       
   458  * @see restore_current_blog()
       
   459  * @since MU
       
   460  *
       
   461  * @param int $new_blog The id of the blog you want to switch to. Default: current blog
       
   462  * @param bool $validate Whether to check if $new_blog exists before proceeding
       
   463  * @return bool	True on success, False if the validation failed
       
   464  */
       
   465 function switch_to_blog( $new_blog, $validate = false ) {
       
   466 	global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
       
   467 
       
   468 	if ( empty($new_blog) )
       
   469 		$new_blog = $blog_id;
       
   470 
       
   471 	if ( $validate && ! get_blog_details( $new_blog ) )
       
   472 		return false;
       
   473 
       
   474 	if ( empty($switched_stack) )
       
   475 		$switched_stack = array();
       
   476 
       
   477 	$switched_stack[] = $blog_id;
       
   478 
       
   479 	/* If we're switching to the same blog id that we're on,
       
   480 	* set the right vars, do the associated actions, but skip
       
   481 	* the extra unnecessary work */
       
   482 	if ( $blog_id == $new_blog ) {
       
   483 		do_action( 'switch_blog', $blog_id, $blog_id );
       
   484 		$switched = true;
       
   485 		return true;
       
   486 	}
       
   487 
       
   488 	$wpdb->set_blog_id($new_blog);
       
   489 	$table_prefix = $wpdb->prefix;
       
   490 	$prev_blog_id = $blog_id;
       
   491 	$blog_id = $new_blog;
       
   492 
       
   493 	if ( is_object( $wp_roles ) ) {
       
   494 		$wpdb->suppress_errors();
       
   495 		if ( method_exists( $wp_roles ,'_init' ) )
       
   496 			$wp_roles->_init();
       
   497 		elseif ( method_exists( $wp_roles, '__construct' ) )
       
   498 			$wp_roles->__construct();
       
   499 		$wpdb->suppress_errors( false );
       
   500 	}
       
   501 
       
   502 	if ( did_action('init') ) {
       
   503 		$current_user = wp_get_current_user();
       
   504 		if ( is_object( $current_user ) )
       
   505 			$current_user->for_blog( $blog_id );
       
   506 	}
       
   507 
       
   508 	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
       
   509 		$global_groups = $wp_object_cache->global_groups;
       
   510 	else
       
   511 		$global_groups = false;
       
   512 
       
   513 	wp_cache_init();
       
   514 	if ( function_exists('wp_cache_add_global_groups') ) {
       
   515 		if ( is_array( $global_groups ) )
       
   516 			wp_cache_add_global_groups( $global_groups );
       
   517 		else
       
   518 			wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
       
   519 		wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
       
   520 	}
       
   521 
       
   522 	do_action('switch_blog', $blog_id, $prev_blog_id);
       
   523 	$switched = true;
       
   524 	return true;
       
   525 }
       
   526 
       
   527 /**
       
   528  * Restore the current blog, after calling switch_to_blog()
       
   529  *
       
   530  * @see switch_to_blog()
       
   531  * @since MU
       
   532  *
       
   533  * @return bool True on success, False if we're already on the current blog
       
   534  */
       
   535 function restore_current_blog() {
       
   536 	global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
       
   537 
       
   538 	if ( !$switched )
       
   539 		return false;
       
   540 
       
   541 	if ( !is_array( $switched_stack ) )
       
   542 		return false;
       
   543 
       
   544 	$blog = array_pop( $switched_stack );
       
   545 	if ( $blog_id == $blog ) {
       
   546 		do_action( 'switch_blog', $blog, $blog );
       
   547 		/* If we still have items in the switched stack, consider ourselves still 'switched' */
       
   548 		$switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
       
   549 		return true;
       
   550 	}
       
   551 
       
   552 	$wpdb->set_blog_id($blog);
       
   553 	$prev_blog_id = $blog_id;
       
   554 	$blog_id = $blog;
       
   555 	$table_prefix = $wpdb->prefix;
       
   556 
       
   557 	if ( is_object( $wp_roles ) ) {
       
   558 		$wpdb->suppress_errors();
       
   559 		if ( method_exists( $wp_roles ,'_init' ) )
       
   560 			$wp_roles->_init();
       
   561 		elseif ( method_exists( $wp_roles, '__construct' ) )
       
   562 			$wp_roles->__construct();
       
   563 		$wpdb->suppress_errors( false );
       
   564 	}
       
   565 
       
   566 	if ( did_action('init') ) {
       
   567 		$current_user = wp_get_current_user();
       
   568 		if ( is_object( $current_user ) )
       
   569 			$current_user->for_blog( $blog_id );
       
   570 	}
       
   571 
       
   572 	if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
       
   573 		$global_groups = $wp_object_cache->global_groups;
       
   574 	else
       
   575 		$global_groups = false;
       
   576 
       
   577 	wp_cache_init();
       
   578 	if ( function_exists('wp_cache_add_global_groups') ) {
       
   579 		if ( is_array( $global_groups ) )
       
   580 			wp_cache_add_global_groups( $global_groups );
       
   581 		else
       
   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' ) );
       
   583 		wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
       
   584 	}
       
   585 
       
   586 	do_action('switch_blog', $blog_id, $prev_blog_id);
       
   587 
       
   588 	/* If we still have items in the switched stack, consider ourselves still 'switched' */
       
   589 	$switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
       
   590 	return true;
       
   591 }
       
   592 
       
   593 /**
       
   594  * Check if a particular blog is archived.
       
   595  *
       
   596  * @since MU
       
   597  *
       
   598  * @param int $id The blog id
       
   599  * @return string Whether the blog is archived or not
       
   600  */
       
   601 function is_archived( $id ) {
       
   602 	return get_blog_status($id, 'archived');
       
   603 }
       
   604 
       
   605 /**
       
   606  * Update the 'archived' status of a particular blog.
       
   607  *
       
   608  * @since MU
       
   609  *
       
   610  * @param int $id The blog id
       
   611  * @param string $archived The new status
       
   612  * @return string $archived
       
   613  */
       
   614 function update_archived( $id, $archived ) {
       
   615 	update_blog_status($id, 'archived', $archived);
       
   616 	return $archived;
       
   617 }
       
   618 
       
   619 /**
       
   620  * Update a blog details field.
       
   621  *
       
   622  * @since MU
       
   623  *
       
   624  * @param int $blog_id BLog ID
       
   625  * @param string $pref A field name
       
   626  * @param string $value Value for $pref
       
   627  * @return string $value
       
   628  */
       
   629 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
       
   630 	global $wpdb;
       
   631 
       
   632 	if ( null !== $deprecated  )
       
   633 		_deprecated_argument( __FUNCTION__, '3.1' );
       
   634 
       
   635 	if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
       
   636 		return $value;
       
   637 
       
   638 	$wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
       
   639 
       
   640 	refresh_blog_details($blog_id);
       
   641 
       
   642 	if ( 'spam' == $pref )
       
   643 		( $value == 1 ) ? do_action( 'make_spam_blog', $blog_id ) :	do_action( 'make_ham_blog', $blog_id );
       
   644 	elseif ( 'mature' == $pref )
       
   645 		( $value == 1 ) ? do_action( 'mature_blog', $blog_id ) : do_action( 'unmature_blog', $blog_id );
       
   646 	elseif ( 'archived' == $pref )
       
   647 		( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
       
   648 	elseif ( 'archived' == $pref )
       
   649 		( $value == 1 ) ? do_action( 'archive_blog', $blog_id ) : do_action( 'unarchive_blog', $blog_id );
       
   650 
       
   651 	return $value;
       
   652 }
       
   653 
       
   654 /**
       
   655  * Get a blog details field.
       
   656  *
       
   657  * @since MU
       
   658  *
       
   659  * @param int $id The blog id
       
   660  * @param string $pref A field name
       
   661  * @return bool $value
       
   662  */
       
   663 function get_blog_status( $id, $pref ) {
       
   664 	global $wpdb;
       
   665 
       
   666 	$details = get_blog_details( $id, false );
       
   667 	if ( $details )
       
   668 		return $details->$pref;
       
   669 
       
   670 	return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
       
   671 }
       
   672 
       
   673 /**
       
   674  * Get a list of most recently updated blogs.
       
   675  *
       
   676  * @since MU
       
   677  *
       
   678  * @param mixed $deprecated Not used
       
   679  * @param int $start The offset
       
   680  * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
       
   681  * @return array The list of blogs
       
   682  */
       
   683 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
       
   684 	global $wpdb;
       
   685 
       
   686 	if ( ! empty( $deprecated ) )
       
   687 		_deprecated_argument( __FUNCTION__, 'MU' ); // never used
       
   688 
       
   689 	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 );
       
   690 }
       
   691 
       
   692 /**
       
   693  * Handler for updating the blog date when a post is published or an already published post is changed.
       
   694  *
       
   695  * @since 3.3.0
       
   696  *
       
   697  * @param string $new_status The new post status
       
   698  * @param string $old_status The old post status
       
   699  * @param object $post Post object
       
   700  */
       
   701 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
       
   702 	$post_type_obj = get_post_type_object( $post->post_type );
       
   703 	if ( ! $post_type_obj->public )
       
   704 		return;
       
   705 
       
   706 	if ( 'publish' != $new_status && 'publish' != $old_status )
       
   707 		return;
       
   708 
       
   709 	// Post was freshly published, published post was saved, or published post was unpublished.
       
   710 
       
   711 	wpmu_update_blogs_date();
       
   712 }
       
   713 
       
   714 /**
       
   715  * Handler for updating the blog date when a published post is deleted.
       
   716  *
       
   717  * @since 3.4.0
       
   718  *
       
   719  * @param int $post_id Post ID
       
   720  */
       
   721 function _update_blog_date_on_post_delete( $post_id ) {
       
   722 	$post = get_post( $post_id );
       
   723 
       
   724 	$post_type_obj = get_post_type_object( $post->post_type );
       
   725 	if ( ! $post_type_obj->public )
       
   726 		return;
       
   727 
       
   728 	if ( 'publish' != $post->post_status )
       
   729 		return;
       
   730 
       
   731 	wpmu_update_blogs_date();
       
   732 }
       
   733