wp/wp-includes/ms-blogs.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     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 	if ( is_subdomain_install() ) {
       
    47 		if ( $blogname == 'main' )
       
    48 			$blogname = 'www';
       
    49 		$url = rtrim( network_home_url(), '/' );
       
    50 		if ( !empty( $blogname ) )
       
    51 			$url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
       
    52 	} else {
       
    53 		$url = network_home_url( $blogname );
       
    54 	}
       
    55 	return esc_url( $url . '/' );
       
    56 }
       
    57 
       
    58 /**
       
    59  * Given a blog's (subdomain or directory) slug, retrieve its id.
       
    60  *
       
    61  * @since MU
       
    62  *
       
    63  * @param string $slug
       
    64  * @return int A blog id
       
    65  */
       
    66 function get_id_from_blogname( $slug ) {
       
    67 	global $wpdb, $current_site;
       
    68 
       
    69 	$slug = trim( $slug, '/' );
       
    70 
       
    71 	$blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
       
    72 	if ( $blog_id )
       
    73 		return $blog_id;
       
    74 
       
    75 	if ( is_subdomain_install() ) {
       
    76 		$domain = $slug . '.' . $current_site->domain;
       
    77 		$path = $current_site->path;
       
    78 	} else {
       
    79 		$domain = $current_site->domain;
       
    80 		$path = $current_site->path . $slug . '/';
       
    81 	}
       
    82 
       
    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' );
       
    85 	return $blog_id;
       
    86 }
       
    87 
       
    88 /**
       
    89  * Retrieve the details for a blog from the blogs table and blog options.
       
    90  *
       
    91  * @since MU
       
    92  *
       
    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.
       
    96  */
       
    97 function get_blog_details( $fields = null, $get_all = true ) {
       
    98 	global $wpdb;
       
    99 
       
   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 )
       
   107 				return $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'] ) );
       
   111 			} else {
       
   112 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
       
   113 			}
       
   114 			if ( $blog ) {
       
   115 				wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
       
   116 				$blog_id = $blog->blog_id;
       
   117 			} else {
       
   118 				return false;
       
   119 			}
       
   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 )
       
   124 				return $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'] ) );
       
   128 			} else {
       
   129 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
       
   130 			}
       
   131 			if ( $blog ) {
       
   132 				wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
       
   133 				$blog_id = $blog->blog_id;
       
   134 			} else {
       
   135 				return false;
       
   136 			}
       
   137 		} else {
       
   138 			return false;
       
   139 		}
       
   140 	} else {
       
   141 		if ( ! $fields )
       
   142 			$blog_id = get_current_blog_id();
       
   143 		elseif ( ! is_numeric( $fields ) )
       
   144 			$blog_id = get_id_from_blogname( $fields );
       
   145 		else
       
   146 			$blog_id = $fields;
       
   147 	}
       
   148 
       
   149 	$blog_id = (int) $blog_id;
       
   150 
       
   151 	$all = $get_all == true ? '' : 'short';
       
   152 	$details = wp_cache_get( $blog_id . $all, 'blog-details' );
       
   153 
       
   154 	if ( $details ) {
       
   155 		if ( ! is_object( $details ) ) {
       
   156 			if ( $details == -1 ) {
       
   157 				return false;
       
   158 			} else {
       
   159 				// Clear old pre-serialized objects. Cache clients do better with that.
       
   160 				wp_cache_delete( $blog_id . $all, 'blog-details' );
       
   161 				unset($details);
       
   162 			}
       
   163 		} else {
       
   164 			return $details;
       
   165 		}
       
   166 	}
       
   167 
       
   168 	// Try the other cache.
       
   169 	if ( $get_all ) {
       
   170 		$details = wp_cache_get( $blog_id . 'short', 'blog-details' );
       
   171 	} else {
       
   172 		$details = wp_cache_get( $blog_id, 'blog-details' );
       
   173 		// If short was requested and full cache is set, we can return.
       
   174 		if ( $details ) {
       
   175 			if ( ! is_object( $details ) ) {
       
   176 				if ( $details == -1 ) {
       
   177 					return false;
       
   178 				} else {
       
   179 					// Clear old pre-serialized objects. Cache clients do better with that.
       
   180 					wp_cache_delete( $blog_id, 'blog-details' );
       
   181 					unset($details);
       
   182 				}
       
   183 			} else {
       
   184 				return $details;
       
   185 			}
       
   186 		}
       
   187 	}
       
   188 
       
   189 	if ( empty($details) ) {
       
   190 		$details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
       
   191 		if ( ! $details ) {
       
   192 			// Set the full cache.
       
   193 			wp_cache_set( $blog_id, -1, 'blog-details' );
       
   194 			return false;
       
   195 		}
       
   196 	}
       
   197 
       
   198 	if ( ! $get_all ) {
       
   199 		wp_cache_set( $blog_id . $all, $details, 'blog-details' );
       
   200 		return $details;
       
   201 	}
       
   202 
       
   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();
       
   208 
       
   209 	$details = apply_filters( 'blog_details', $details );
       
   210 
       
   211 	wp_cache_set( $blog_id . $all, $details, 'blog-details' );
       
   212 
       
   213 	$key = md5( $details->domain . $details->path );
       
   214 	wp_cache_set( $key, $details, 'blog-lookup' );
       
   215 
       
   216 	return $details;
       
   217 }
       
   218 
       
   219 /**
       
   220  * Clear the blog details cache.
       
   221  *
       
   222  * @since MU
       
   223  *
       
   224  * @param int $blog_id Blog ID
       
   225  */
       
   226 function refresh_blog_details( $blog_id ) {
       
   227 	$blog_id = (int) $blog_id;
       
   228 	$details = get_blog_details( $blog_id, false );
       
   229 	if ( ! $details ) {
       
   230 		// Make sure clean_blog_cache() gets the blog ID
       
   231 		// when the blog has been previously cached as
       
   232 		// non-existent.
       
   233 		$details = (object) array(
       
   234 			'blog_id' => $blog_id,
       
   235 			'domain' => null,
       
   236 			'path' => null
       
   237 		);
       
   238 	}
       
   239 
       
   240 	clean_blog_cache( $details );
       
   241 
       
   242 	do_action( 'refresh_blog_details', $blog_id );
       
   243 }
       
   244 
       
   245 /**
       
   246  * Update the details for a blog. Updates the blogs table for a given blog id.
       
   247  *
       
   248  * @since MU
       
   249  *
       
   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.
       
   253  */
       
   254 function update_blog_details( $blog_id, $details = array() ) {
       
   255 	global $wpdb;
       
   256 
       
   257 	if ( empty($details) )
       
   258 		return false;
       
   259 
       
   260 	if ( is_object($details) )
       
   261 		$details = get_object_vars($details);
       
   262 
       
   263 	$current_details = get_blog_details($blog_id, false);
       
   264 	if ( empty($current_details) )
       
   265 		return false;
       
   266 
       
   267 	$current_details = get_object_vars($current_details);
       
   268 
       
   269 	$details = array_merge($current_details, $details);
       
   270 	$details['last_updated'] = current_time('mysql', true);
       
   271 
       
   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];
       
   276 
       
   277 	$result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
       
   278 
       
   279 	if ( false === $result )
       
   280 		return false;
       
   281 
       
   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 );
       
   286 		else
       
   287 			do_action( 'make_ham_blog', $blog_id );
       
   288 	}
       
   289 
       
   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 );
       
   294 		else
       
   295 			do_action( 'unmature_blog', $blog_id );
       
   296 	}
       
   297 
       
   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 );
       
   302 		else
       
   303 			do_action( 'unarchive_blog', $blog_id );
       
   304 	}
       
   305 
       
   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 );
       
   310 		else
       
   311 			do_action( 'make_undelete_blog', $blog_id );
       
   312 	}
       
   313 
       
   314 	if ( isset( $details[ 'public' ] ) ) {
       
   315 		switch_to_blog( $blog_id );
       
   316 		update_option( 'blog_public', $details[ 'public' ] );
       
   317 		restore_current_blog();
       
   318 	}
       
   319 
       
   320 	refresh_blog_details($blog_id);
       
   321 
       
   322 	return true;
       
   323 }
       
   324 
       
   325 /**
       
   326  * Clean the blog cache
       
   327  *
       
   328  * @since 3.5.0
       
   329  *
       
   330  * @param stdClass $blog The blog details as returned from get_blog_details()
       
   331  */
       
   332 function clean_blog_cache( $blog ) {
       
   333 	$blog_id = $blog->blog_id;
       
   334 	$domain_path_key = md5( $blog->domain . $blog->path );
       
   335 
       
   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' );
       
   343 }
       
   344 
       
   345 /**
       
   346  * Retrieve option value for a given blog id based on name of option.
       
   347  *
       
   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.
       
   352  *
       
   353  * If the option was serialized then it will be unserialized when it is returned.
       
   354  *
       
   355  * @since MU
       
   356  *
       
   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.
       
   361  */
       
   362 function get_blog_option( $id, $option, $default = false ) {
       
   363 	$id = (int) $id;
       
   364 
       
   365 	if ( empty( $id ) )
       
   366 		$id = get_current_blog_id();
       
   367 
       
   368 	if ( get_current_blog_id() == $id )
       
   369 		return get_option( $option, $default );
       
   370 
       
   371 	switch_to_blog( $id );
       
   372 	$value = get_option( $option, $default );
       
   373 	restore_current_blog();
       
   374 
       
   375 	return apply_filters( 'blog_option_' . $option, $value, $id );
       
   376 }
       
   377 
       
   378 /**
       
   379  * Add a new option for a given blog id.
       
   380  *
       
   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.
       
   384  *
       
   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.
       
   389  *
       
   390  * @since MU
       
   391  *
       
   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.
       
   396  */
       
   397 function add_blog_option( $id, $option, $value ) {
       
   398 	$id = (int) $id;
       
   399 
       
   400 	if ( empty( $id ) )
       
   401 		$id = get_current_blog_id();
       
   402 
       
   403 	if ( get_current_blog_id() == $id )
       
   404 		return add_option( $option, $value );
       
   405 
       
   406 	switch_to_blog( $id );
       
   407 	$return = add_option( $option, $value );
       
   408 	restore_current_blog();
       
   409 
       
   410 	return $return;
       
   411 }
       
   412 
       
   413 /**
       
   414  * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
       
   415  *
       
   416  * @since MU
       
   417  *
       
   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.
       
   421  */
       
   422 function delete_blog_option( $id, $option ) {
       
   423 	$id = (int) $id;
       
   424 
       
   425 	if ( empty( $id ) )
       
   426 		$id = get_current_blog_id();
       
   427 
       
   428 	if ( get_current_blog_id() == $id )
       
   429 		return delete_option( $option );
       
   430 
       
   431 	switch_to_blog( $id );
       
   432 	$return = delete_option( $option );
       
   433 	restore_current_blog();
       
   434 
       
   435 	return $return;
       
   436 }
       
   437 
       
   438 /**
       
   439  * Update an option for a particular blog.
       
   440  *
       
   441  * @since MU
       
   442  *
       
   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.
       
   447  */
       
   448 function update_blog_option( $id, $option, $value, $deprecated = null ) {
       
   449 	$id = (int) $id;
       
   450 
       
   451 	if ( null !== $deprecated  )
       
   452 		_deprecated_argument( __FUNCTION__, '3.1' );
       
   453 
       
   454 	if ( get_current_blog_id() == $id )
       
   455 		return update_option( $option, $value );
       
   456 
       
   457 	switch_to_blog( $id );
       
   458 	$return = update_option( $option, $value );
       
   459 	restore_current_blog();
       
   460 
       
   461 	refresh_blog_details( $id );
       
   462 
       
   463 	return $return;
       
   464 }
       
   465 
       
   466 /**
       
   467  * Switch the current blog.
       
   468  *
       
   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().
       
   471  *
       
   472  * Things that aren't switched:
       
   473  *  - autoloaded options. See #14992
       
   474  *  - plugins. See #14941
       
   475  *
       
   476  * @see restore_current_blog()
       
   477  * @since MU
       
   478  *
       
   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
       
   482  */
       
   483 function switch_to_blog( $new_blog, $deprecated = null ) {
       
   484 	global $wpdb, $wp_roles;
       
   485 
       
   486 	if ( empty( $new_blog ) )
       
   487 		$new_blog = $GLOBALS['blog_id'];
       
   488 
       
   489 	$GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
       
   490 
       
   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;
       
   497 		return true;
       
   498 	}
       
   499 
       
   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;
       
   504 
       
   505 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
       
   506 		wp_cache_switch_to_blog( $new_blog );
       
   507 	} else {
       
   508 		global $wp_object_cache;
       
   509 
       
   510 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
       
   511 			$global_groups = $wp_object_cache->global_groups;
       
   512 		else
       
   513 			$global_groups = false;
       
   514 
       
   515 		wp_cache_init();
       
   516 
       
   517 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
       
   518 			if ( is_array( $global_groups ) )
       
   519 				wp_cache_add_global_groups( $global_groups );
       
   520 			else
       
   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' ) );
       
   523 		}
       
   524 	}
       
   525 
       
   526 	if ( did_action( 'init' ) ) {
       
   527 		$wp_roles->reinit();
       
   528 		$current_user = wp_get_current_user();
       
   529 		$current_user->for_blog( $new_blog );
       
   530 	}
       
   531 
       
   532 	do_action( 'switch_blog', $new_blog, $prev_blog_id );
       
   533 	$GLOBALS['switched'] = true;
       
   534 
       
   535 	return true;
       
   536 }
       
   537 
       
   538 /**
       
   539  * Restore the current blog, after calling switch_to_blog()
       
   540  *
       
   541  * @see switch_to_blog()
       
   542  * @since MU
       
   543  *
       
   544  * @return bool True on success, false if we're already on the current blog
       
   545  */
       
   546 function restore_current_blog() {
       
   547 	global $wpdb, $wp_roles;
       
   548 
       
   549 	if ( empty( $GLOBALS['_wp_switched_stack'] ) )
       
   550 		return false;
       
   551 
       
   552 	$blog = array_pop( $GLOBALS['_wp_switched_stack'] );
       
   553 
       
   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'] );
       
   558 		return true;
       
   559 	}
       
   560 
       
   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();
       
   565 
       
   566 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
       
   567 		wp_cache_switch_to_blog( $blog );
       
   568 	} else {
       
   569 		global $wp_object_cache;
       
   570 
       
   571 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
       
   572 			$global_groups = $wp_object_cache->global_groups;
       
   573 		else
       
   574 			$global_groups = false;
       
   575 
       
   576 		wp_cache_init();
       
   577 
       
   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', ' blog-id-cache' ) );
       
   583 			wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
       
   584 		}
       
   585 	}
       
   586 
       
   587 	if ( did_action( 'init' ) ) {
       
   588 		$wp_roles->reinit();
       
   589 		$current_user = wp_get_current_user();
       
   590 		$current_user->for_blog( $blog );
       
   591 	}
       
   592 
       
   593 	do_action( 'switch_blog', $blog, $prev_blog_id );
       
   594 
       
   595 	// If we still have items in the switched stack, consider ourselves still 'switched'
       
   596 	$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
       
   597 
       
   598 	return true;
       
   599 }
       
   600 
       
   601 /**
       
   602  * Determines if switch_to_blog() is in effect
       
   603  *
       
   604  * @since 3.5.0
       
   605  *
       
   606  * @return bool True if switched, false otherwise.
       
   607  */
       
   608 function ms_is_switched() {
       
   609 	return ! empty( $GLOBALS['_wp_switched_stack'] );
       
   610 }
       
   611 
       
   612 /**
       
   613  * Check if a particular blog is archived.
       
   614  *
       
   615  * @since MU
       
   616  *
       
   617  * @param int $id The blog id
       
   618  * @return string Whether the blog is archived or not
       
   619  */
       
   620 function is_archived( $id ) {
       
   621 	return get_blog_status($id, 'archived');
       
   622 }
       
   623 
       
   624 /**
       
   625  * Update the 'archived' status of a particular blog.
       
   626  *
       
   627  * @since MU
       
   628  *
       
   629  * @param int $id The blog id
       
   630  * @param string $archived The new status
       
   631  * @return string $archived
       
   632  */
       
   633 function update_archived( $id, $archived ) {
       
   634 	update_blog_status($id, 'archived', $archived);
       
   635 	return $archived;
       
   636 }
       
   637 
       
   638 /**
       
   639  * Update a blog details field.
       
   640  *
       
   641  * @since MU
       
   642  *
       
   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
       
   647  */
       
   648 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
       
   649 	global $wpdb;
       
   650 
       
   651 	if ( null !== $deprecated  )
       
   652 		_deprecated_argument( __FUNCTION__, '3.1' );
       
   653 
       
   654 	if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
       
   655 		return $value;
       
   656 
       
   657 	$result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
       
   658 
       
   659 	if ( false === $result )
       
   660 		return false;
       
   661 
       
   662 	refresh_blog_details( $blog_id );
       
   663 
       
   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().
       
   674 
       
   675 	return $value;
       
   676 }
       
   677 
       
   678 /**
       
   679  * Get a blog details field.
       
   680  *
       
   681  * @since MU
       
   682  *
       
   683  * @param int $id The blog id
       
   684  * @param string $pref A field name
       
   685  * @return bool $value
       
   686  */
       
   687 function get_blog_status( $id, $pref ) {
       
   688 	global $wpdb;
       
   689 
       
   690 	$details = get_blog_details( $id, false );
       
   691 	if ( $details )
       
   692 		return $details->$pref;
       
   693 
       
   694 	return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
       
   695 }
       
   696 
       
   697 /**
       
   698  * Get a list of most recently updated blogs.
       
   699  *
       
   700  * @since MU
       
   701  *
       
   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
       
   706  */
       
   707 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
       
   708 	global $wpdb;
       
   709 
       
   710 	if ( ! empty( $deprecated ) )
       
   711 		_deprecated_argument( __FUNCTION__, 'MU' ); // never used
       
   712 
       
   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 );
       
   714 }
       
   715 
       
   716 /**
       
   717  * Handler for updating the blog date when a post is published or an already published post is changed.
       
   718  *
       
   719  * @since 3.3.0
       
   720  *
       
   721  * @param string $new_status The new post status
       
   722  * @param string $old_status The old post status
       
   723  * @param object $post Post object
       
   724  */
       
   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 )
       
   728 		return;
       
   729 
       
   730 	if ( 'publish' != $new_status && 'publish' != $old_status )
       
   731 		return;
       
   732 
       
   733 	// Post was freshly published, published post was saved, or published post was unpublished.
       
   734 
       
   735 	wpmu_update_blogs_date();
       
   736 }
       
   737 
       
   738 /**
       
   739  * Handler for updating the blog date when a published post is deleted.
       
   740  *
       
   741  * @since 3.4.0
       
   742  *
       
   743  * @param int $post_id Post ID
       
   744  */
       
   745 function _update_blog_date_on_post_delete( $post_id ) {
       
   746 	$post = get_post( $post_id );
       
   747 
       
   748 	$post_type_obj = get_post_type_object( $post->post_type );
       
   749 	if ( ! $post_type_obj->public )
       
   750 		return;
       
   751 
       
   752 	if ( 'publish' != $post->post_status )
       
   753 		return;
       
   754 
       
   755 	wpmu_update_blogs_date();
       
   756 }
       
   757