wp/wp-includes/ms-blogs.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    10 
    10 
    11 require_once ABSPATH . WPINC . '/ms-site.php';
    11 require_once ABSPATH . WPINC . '/ms-site.php';
    12 require_once ABSPATH . WPINC . '/ms-network.php';
    12 require_once ABSPATH . WPINC . '/ms-network.php';
    13 
    13 
    14 /**
    14 /**
    15  * Update the last_updated field for the current site.
    15  * Updates the last_updated field for the current site.
    16  *
    16  *
    17  * @since MU (3.0.0)
    17  * @since MU (3.0.0)
    18  */
    18  */
    19 function wpmu_update_blogs_date() {
    19 function wpmu_update_blogs_date() {
    20 	$site_id = get_current_blog_id();
    20 	$site_id = get_current_blog_id();
    29 	 */
    29 	 */
    30 	do_action( 'wpmu_blog_updated', $site_id );
    30 	do_action( 'wpmu_blog_updated', $site_id );
    31 }
    31 }
    32 
    32 
    33 /**
    33 /**
    34  * Get a full blog URL, given a blog ID.
    34  * Gets a full site URL, given a site ID.
    35  *
    35  *
    36  * @since MU (3.0.0)
    36  * @since MU (3.0.0)
    37  *
    37  *
    38  * @param int $blog_id Blog ID.
    38  * @param int $blog_id Site ID.
    39  * @return string Full URL of the blog if found. Empty string if not.
    39  * @return string Full site URL if found. Empty string if not.
    40  */
    40  */
    41 function get_blogaddress_by_id( $blog_id ) {
    41 function get_blogaddress_by_id( $blog_id ) {
    42 	$bloginfo = get_site( (int) $blog_id );
    42 	$bloginfo = get_site( (int) $blog_id );
    43 
    43 
    44 	if ( empty( $bloginfo ) ) {
    44 	if ( empty( $bloginfo ) ) {
    50 
    50 
    51 	return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
    51 	return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
    52 }
    52 }
    53 
    53 
    54 /**
    54 /**
    55  * Get a full blog URL, given a blog name.
    55  * Gets a full site URL, given a site name.
    56  *
    56  *
    57  * @since MU (3.0.0)
    57  * @since MU (3.0.0)
    58  *
    58  *
    59  * @param string $blogname The (subdomain or directory) name
    59  * @param string $blogname Name of the subdomain or directory.
    60  * @return string
    60  * @return string
    61  */
    61  */
    62 function get_blogaddress_by_name( $blogname ) {
    62 function get_blogaddress_by_name( $blogname ) {
    63 	if ( is_subdomain_install() ) {
    63 	if ( is_subdomain_install() ) {
    64 		if ( 'main' === $blogname ) {
    64 		if ( 'main' === $blogname ) {
    73 	}
    73 	}
    74 	return esc_url( $url . '/' );
    74 	return esc_url( $url . '/' );
    75 }
    75 }
    76 
    76 
    77 /**
    77 /**
    78  * Retrieves a sites ID given its (subdomain or directory) slug.
    78  * Retrieves a site's ID given its (subdomain or directory) slug.
    79  *
    79  *
    80  * @since MU (3.0.0)
    80  * @since MU (3.0.0)
    81  * @since 4.7.0 Converted to use `get_sites()`.
    81  * @since 4.7.0 Converted to use `get_sites()`.
    82  *
    82  *
    83  * @param string $slug A site's slug.
    83  * @param string $slug A site's slug.
   111 
   111 
   112 	return array_shift( $site_ids );
   112 	return array_shift( $site_ids );
   113 }
   113 }
   114 
   114 
   115 /**
   115 /**
   116  * Retrieve the details for a blog from the blogs table and blog options.
   116  * Retrieves the details for a blog from the blogs table and blog options.
   117  *
   117  *
   118  * @since MU (3.0.0)
   118  * @since MU (3.0.0)
   119  *
   119  *
   120  * @global wpdb $wpdb WordPress database abstraction object.
   120  * @global wpdb $wpdb WordPress database abstraction object.
   121  *
   121  *
   122  * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
   122  * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
   123  *                                  If not specified the current blog ID is used.
   123  *                                  Defaults to the current blog ID.
   124  * @param bool             $get_all Whether to retrieve all details or only the details in the blogs table.
   124  * @param bool             $get_all Whether to retrieve all details or only the details in the blogs table.
   125  *                                  Default is true.
   125  *                                  Default is true.
   126  * @return WP_Site|false Blog details on success. False on failure.
   126  * @return WP_Site|false Blog details on success. False on failure.
   127  */
   127  */
   128 function get_blog_details( $fields = null, $get_all = true ) {
   128 function get_blog_details( $fields = null, $get_all = true ) {
   135 			$key  = md5( $fields['domain'] . $fields['path'] );
   135 			$key  = md5( $fields['domain'] . $fields['path'] );
   136 			$blog = wp_cache_get( $key, 'blog-lookup' );
   136 			$blog = wp_cache_get( $key, 'blog-lookup' );
   137 			if ( false !== $blog ) {
   137 			if ( false !== $blog ) {
   138 				return $blog;
   138 				return $blog;
   139 			}
   139 			}
   140 			if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
   140 			if ( str_starts_with( $fields['domain'], 'www.' ) ) {
   141 				$nowww = substr( $fields['domain'], 4 );
   141 				$nowww = substr( $fields['domain'], 4 );
   142 				$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'] ) );
   142 				$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'] ) );
   143 			} else {
   143 			} else {
   144 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
   144 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
   145 			}
   145 			}
   153 			$key  = md5( $fields['domain'] );
   153 			$key  = md5( $fields['domain'] );
   154 			$blog = wp_cache_get( $key, 'blog-lookup' );
   154 			$blog = wp_cache_get( $key, 'blog-lookup' );
   155 			if ( false !== $blog ) {
   155 			if ( false !== $blog ) {
   156 				return $blog;
   156 				return $blog;
   157 			}
   157 			}
   158 			if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
   158 			if ( str_starts_with( $fields['domain'], 'www.' ) ) {
   159 				$nowww = substr( $fields['domain'], 4 );
   159 				$nowww = substr( $fields['domain'], 4 );
   160 				$blog  = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
   160 				$blog  = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
   161 			} else {
   161 			} else {
   162 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
   162 				$blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
   163 			}
   163 			}
   185 	$all     = $get_all ? '' : 'short';
   185 	$all     = $get_all ? '' : 'short';
   186 	$details = wp_cache_get( $blog_id . $all, 'blog-details' );
   186 	$details = wp_cache_get( $blog_id . $all, 'blog-details' );
   187 
   187 
   188 	if ( $details ) {
   188 	if ( $details ) {
   189 		if ( ! is_object( $details ) ) {
   189 		if ( ! is_object( $details ) ) {
   190 			if ( -1 == $details ) {
   190 			if ( -1 === $details ) {
   191 				return false;
   191 				return false;
   192 			} else {
   192 			} else {
   193 				// Clear old pre-serialized objects. Cache clients do better with that.
   193 				// Clear old pre-serialized objects. Cache clients do better with that.
   194 				wp_cache_delete( $blog_id . $all, 'blog-details' );
   194 				wp_cache_delete( $blog_id . $all, 'blog-details' );
   195 				unset( $details );
   195 				unset( $details );
   205 	} else {
   205 	} else {
   206 		$details = wp_cache_get( $blog_id, 'blog-details' );
   206 		$details = wp_cache_get( $blog_id, 'blog-details' );
   207 		// If short was requested and full cache is set, we can return.
   207 		// If short was requested and full cache is set, we can return.
   208 		if ( $details ) {
   208 		if ( $details ) {
   209 			if ( ! is_object( $details ) ) {
   209 			if ( ! is_object( $details ) ) {
   210 				if ( -1 == $details ) {
   210 				if ( -1 === $details ) {
   211 					return false;
   211 					return false;
   212 				} else {
   212 				} else {
   213 					// Clear old pre-serialized objects. Cache clients do better with that.
   213 					// Clear old pre-serialized objects. Cache clients do better with that.
   214 					wp_cache_delete( $blog_id, 'blog-details' );
   214 					wp_cache_delete( $blog_id, 'blog-details' );
   215 					unset( $details );
   215 					unset( $details );
   271 
   271 
   272 	return $details;
   272 	return $details;
   273 }
   273 }
   274 
   274 
   275 /**
   275 /**
   276  * Clear the blog details cache.
   276  * Clears the blog details cache.
   277  *
   277  *
   278  * @since MU (3.0.0)
   278  * @since MU (3.0.0)
   279  *
   279  *
   280  * @param int $blog_id Optional. Blog ID. Defaults to current blog.
   280  * @param int $blog_id Optional. Blog ID. Defaults to current blog.
   281  */
   281  */
   287 
   287 
   288 	clean_blog_cache( $blog_id );
   288 	clean_blog_cache( $blog_id );
   289 }
   289 }
   290 
   290 
   291 /**
   291 /**
   292  * Update the details for a blog. Updates the blogs table for a given blog ID.
   292  * Updates the details for a blog and the blogs table for a given blog ID.
   293  *
   293  *
   294  * @since MU (3.0.0)
   294  * @since MU (3.0.0)
   295  *
       
   296  * @global wpdb $wpdb WordPress database abstraction object.
       
   297  *
   295  *
   298  * @param int   $blog_id Blog ID.
   296  * @param int   $blog_id Blog ID.
   299  * @param array $details Array of details keyed by blogs table field names.
   297  * @param array $details Array of details keyed by blogs table field names.
   300  * @return bool True if update succeeds, false otherwise.
   298  * @return bool True if update succeeds, false otherwise.
   301  */
   299  */
   302 function update_blog_details( $blog_id, $details = array() ) {
   300 function update_blog_details( $blog_id, $details = array() ) {
   303 	global $wpdb;
       
   304 
       
   305 	if ( empty( $details ) ) {
   301 	if ( empty( $details ) ) {
   306 		return false;
   302 		return false;
   307 	}
   303 	}
   308 
   304 
   309 	if ( is_object( $details ) ) {
   305 	if ( is_object( $details ) ) {
   335 	wp_cache_delete( $site_id, 'site-details' );
   331 	wp_cache_delete( $site_id, 'site-details' );
   336 	wp_cache_delete( $site_id, 'blog-details' );
   332 	wp_cache_delete( $site_id, 'blog-details' );
   337 }
   333 }
   338 
   334 
   339 /**
   335 /**
   340  * Retrieve option value for a given blog id based on name of option.
   336  * Retrieves option value for a given blog id based on name of option.
   341  *
   337  *
   342  * If the option does not exist or does not have a value, then the return value
   338  * If the option does not exist or does not have a value, then the return value
   343  * will be false. This is useful to check whether you need to install an option
   339  * will be false. This is useful to check whether you need to install an option
   344  * and is commonly used during installation of plugin options and to test
   340  * and is commonly used during installation of plugin options and to test
   345  * whether upgrading is required.
   341  * whether upgrading is required.
   346  *
   342  *
   347  * If the option was serialized then it will be unserialized when it is returned.
   343  * If the option was serialized then it will be unserialized when it is returned.
   348  *
   344  *
   349  * @since MU (3.0.0)
   345  * @since MU (3.0.0)
   350  *
   346  *
   351  * @param int    $id      A blog ID. Can be null to refer to the current blog.
   347  * @param int    $id            A blog ID. Can be null to refer to the current blog.
   352  * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
   348  * @param string $option        Name of option to retrieve. Expected to not be SQL-escaped.
   353  * @param mixed  $default Optional. Default value to return if the option does not exist.
   349  * @param mixed  $default_value Optional. Default value to return if the option does not exist.
   354  * @return mixed Value set for the option.
   350  * @return mixed Value set for the option.
   355  */
   351  */
   356 function get_blog_option( $id, $option, $default = false ) {
   352 function get_blog_option( $id, $option, $default_value = false ) {
   357 	$id = (int) $id;
   353 	$id = (int) $id;
   358 
   354 
   359 	if ( empty( $id ) ) {
   355 	if ( empty( $id ) ) {
   360 		$id = get_current_blog_id();
   356 		$id = get_current_blog_id();
   361 	}
   357 	}
   362 
   358 
   363 	if ( get_current_blog_id() == $id ) {
   359 	if ( get_current_blog_id() === $id ) {
   364 		return get_option( $option, $default );
   360 		return get_option( $option, $default_value );
   365 	}
   361 	}
   366 
   362 
   367 	switch_to_blog( $id );
   363 	switch_to_blog( $id );
   368 	$value = get_option( $option, $default );
   364 	$value = get_option( $option, $default_value );
   369 	restore_current_blog();
   365 	restore_current_blog();
   370 
   366 
   371 	/**
   367 	/**
   372 	 * Filters a blog option value.
   368 	 * Filters a blog option value.
   373 	 *
   369 	 *
   380 	 */
   376 	 */
   381 	return apply_filters( "blog_option_{$option}", $value, $id );
   377 	return apply_filters( "blog_option_{$option}", $value, $id );
   382 }
   378 }
   383 
   379 
   384 /**
   380 /**
   385  * Add a new option for a given blog ID.
   381  * Adds a new option for a given blog ID.
   386  *
   382  *
   387  * You do not need to serialize values. If the value needs to be serialized, then
   383  * You do not need to serialize values. If the value needs to be serialized, then
   388  * it will be serialized before it is inserted into the database. Remember,
   384  * it will be serialized before it is inserted into the database. Remember,
   389  * resources can not be serialized or added as an option.
   385  * resources can not be serialized or added as an option.
   390  *
   386  *
   395  *
   391  *
   396  * @since MU (3.0.0)
   392  * @since MU (3.0.0)
   397  *
   393  *
   398  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   394  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   399  * @param string $option Name of option to add. Expected to not be SQL-escaped.
   395  * @param string $option Name of option to add. Expected to not be SQL-escaped.
   400  * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
   396  * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
   401  * @return bool True if the option was added, false otherwise.
   397  * @return bool True if the option was added, false otherwise.
   402  */
   398  */
   403 function add_blog_option( $id, $option, $value ) {
   399 function add_blog_option( $id, $option, $value ) {
   404 	$id = (int) $id;
   400 	$id = (int) $id;
   405 
   401 
   406 	if ( empty( $id ) ) {
   402 	if ( empty( $id ) ) {
   407 		$id = get_current_blog_id();
   403 		$id = get_current_blog_id();
   408 	}
   404 	}
   409 
   405 
   410 	if ( get_current_blog_id() == $id ) {
   406 	if ( get_current_blog_id() === $id ) {
   411 		return add_option( $option, $value );
   407 		return add_option( $option, $value );
   412 	}
   408 	}
   413 
   409 
   414 	switch_to_blog( $id );
   410 	switch_to_blog( $id );
   415 	$return = add_option( $option, $value );
   411 	$return = add_option( $option, $value );
   417 
   413 
   418 	return $return;
   414 	return $return;
   419 }
   415 }
   420 
   416 
   421 /**
   417 /**
   422  * Removes option by name for a given blog ID. Prevents removal of protected WordPress options.
   418  * Removes an option by name for a given blog ID. Prevents removal of protected WordPress options.
   423  *
   419  *
   424  * @since MU (3.0.0)
   420  * @since MU (3.0.0)
   425  *
   421  *
   426  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   422  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   427  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   423  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   432 
   428 
   433 	if ( empty( $id ) ) {
   429 	if ( empty( $id ) ) {
   434 		$id = get_current_blog_id();
   430 		$id = get_current_blog_id();
   435 	}
   431 	}
   436 
   432 
   437 	if ( get_current_blog_id() == $id ) {
   433 	if ( get_current_blog_id() === $id ) {
   438 		return delete_option( $option );
   434 		return delete_option( $option );
   439 	}
   435 	}
   440 
   436 
   441 	switch_to_blog( $id );
   437 	switch_to_blog( $id );
   442 	$return = delete_option( $option );
   438 	$return = delete_option( $option );
   444 
   440 
   445 	return $return;
   441 	return $return;
   446 }
   442 }
   447 
   443 
   448 /**
   444 /**
   449  * Update an option for a particular blog.
   445  * Updates an option for a particular blog.
   450  *
   446  *
   451  * @since MU (3.0.0)
   447  * @since MU (3.0.0)
   452  *
   448  *
   453  * @param int    $id         The blog ID.
   449  * @param int    $id         The blog ID.
   454  * @param string $option     The option key.
   450  * @param string $option     The option key.
   461 
   457 
   462 	if ( null !== $deprecated ) {
   458 	if ( null !== $deprecated ) {
   463 		_deprecated_argument( __FUNCTION__, '3.1.0' );
   459 		_deprecated_argument( __FUNCTION__, '3.1.0' );
   464 	}
   460 	}
   465 
   461 
   466 	if ( get_current_blog_id() == $id ) {
   462 	if ( get_current_blog_id() === $id ) {
   467 		return update_option( $option, $value );
   463 		return update_option( $option, $value );
   468 	}
   464 	}
   469 
   465 
   470 	switch_to_blog( $id );
   466 	switch_to_blog( $id );
   471 	$return = update_option( $option, $value );
   467 	$return = update_option( $option, $value );
   473 
   469 
   474 	return $return;
   470 	return $return;
   475 }
   471 }
   476 
   472 
   477 /**
   473 /**
   478  * Switch the current blog.
   474  * Switches the current blog.
   479  *
   475  *
   480  * This function is useful if you need to pull posts, or other information,
   476  * This function is useful if you need to pull posts, or other information,
   481  * from other blogs. You can switch back afterwards using restore_current_blog().
   477  * from other blogs. You can switch back afterwards using restore_current_blog().
   482  *
   478  *
   483  * Things that aren't switched:
   479  * PHP code loaded with the originally requested site, such as code from a plugin or theme, does not switch. See #14941.
   484  *  - plugins. See #14941
       
   485  *
   480  *
   486  * @see restore_current_blog()
   481  * @see restore_current_blog()
   487  * @since MU (3.0.0)
   482  * @since MU (3.0.0)
   488  *
   483  *
   489  * @global wpdb            $wpdb               WordPress database abstraction object.
   484  * @global wpdb            $wpdb               WordPress database abstraction object.
   490  * @global int             $blog_id
   485  * @global int             $blog_id
   491  * @global array           $_wp_switched_stack
   486  * @global array           $_wp_switched_stack
   492  * @global bool            $switched
   487  * @global bool            $switched
   493  * @global string          $table_prefix
   488  * @global string          $table_prefix       The database table prefix.
   494  * @global WP_Object_Cache $wp_object_cache
   489  * @global WP_Object_Cache $wp_object_cache
   495  *
   490  *
   496  * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
   491  * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
   497  * @param bool $deprecated  Not used.
   492  * @param bool $deprecated  Not used.
   498  * @return true Always returns true.
   493  * @return true Always returns true.
   510 	/*
   505 	/*
   511 	 * If we're switching to the same blog id that we're on,
   506 	 * If we're switching to the same blog id that we're on,
   512 	 * set the right vars, do the associated actions, but skip
   507 	 * set the right vars, do the associated actions, but skip
   513 	 * the extra unnecessary work
   508 	 * the extra unnecessary work
   514 	 */
   509 	 */
   515 	if ( $new_blog_id == $prev_blog_id ) {
   510 	if ( $new_blog_id === $prev_blog_id ) {
   516 		/**
   511 		/**
   517 		 * Fires when the blog is switched.
   512 		 * Fires when the blog is switched.
   518 		 *
   513 		 *
   519 		 * @since MU (3.0.0)
   514 		 * @since MU (3.0.0)
   520 		 * @since 5.4.0 The `$context` parameter was added.
   515 		 * @since 5.4.0 The `$context` parameter was added.
   550 
   545 
   551 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   546 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   552 			if ( is_array( $global_groups ) ) {
   547 			if ( is_array( $global_groups ) ) {
   553 				wp_cache_add_global_groups( $global_groups );
   548 				wp_cache_add_global_groups( $global_groups );
   554 			} else {
   549 			} else {
   555 				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
   550 				wp_cache_add_global_groups(
       
   551 					array(
       
   552 						'blog-details',
       
   553 						'blog-id-cache',
       
   554 						'blog-lookup',
       
   555 						'blog_meta',
       
   556 						'global-posts',
       
   557 						'networks',
       
   558 						'network-queries',
       
   559 						'sites',
       
   560 						'site-details',
       
   561 						'site-options',
       
   562 						'site-queries',
       
   563 						'site-transient',
       
   564 						'theme_files',
       
   565 						'rss',
       
   566 						'users',
       
   567 						'user-queries',
       
   568 						'user_meta',
       
   569 						'useremail',
       
   570 						'userlogins',
       
   571 						'userslugs',
       
   572 					)
       
   573 				);
   556 			}
   574 			}
   557 
   575 
   558 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   576 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
   559 		}
   577 		}
   560 	}
   578 	}
   561 
   579 
   562 	/** This filter is documented in wp-includes/ms-blogs.php */
   580 	/** This filter is documented in wp-includes/ms-blogs.php */
   563 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
   581 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
   566 
   584 
   567 	return true;
   585 	return true;
   568 }
   586 }
   569 
   587 
   570 /**
   588 /**
   571  * Restore the current blog, after calling switch_to_blog().
   589  * Restores the current blog, after calling switch_to_blog().
   572  *
   590  *
   573  * @see switch_to_blog()
   591  * @see switch_to_blog()
   574  * @since MU (3.0.0)
   592  * @since MU (3.0.0)
   575  *
   593  *
   576  * @global wpdb            $wpdb               WordPress database abstraction object.
   594  * @global wpdb            $wpdb               WordPress database abstraction object.
   577  * @global array           $_wp_switched_stack
   595  * @global array           $_wp_switched_stack
   578  * @global int             $blog_id
   596  * @global int             $blog_id
   579  * @global bool            $switched
   597  * @global bool            $switched
   580  * @global string          $table_prefix
   598  * @global string          $table_prefix       The database table prefix.
   581  * @global WP_Object_Cache $wp_object_cache
   599  * @global WP_Object_Cache $wp_object_cache
   582  *
   600  *
   583  * @return bool True on success, false if we're already on the current blog.
   601  * @return bool True on success, false if we're already on the current blog.
   584  */
   602  */
   585 function restore_current_blog() {
   603 function restore_current_blog() {
   590 	}
   608 	}
   591 
   609 
   592 	$new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
   610 	$new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
   593 	$prev_blog_id = get_current_blog_id();
   611 	$prev_blog_id = get_current_blog_id();
   594 
   612 
   595 	if ( $new_blog_id == $prev_blog_id ) {
   613 	if ( $new_blog_id === $prev_blog_id ) {
   596 		/** This filter is documented in wp-includes/ms-blogs.php */
   614 		/** This filter is documented in wp-includes/ms-blogs.php */
   597 		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   615 		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   598 
   616 
   599 		// If we still have items in the switched stack, consider ourselves still 'switched'.
   617 		// If we still have items in the switched stack, consider ourselves still 'switched'.
   600 		$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
   618 		$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
   621 
   639 
   622 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   640 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   623 			if ( is_array( $global_groups ) ) {
   641 			if ( is_array( $global_groups ) ) {
   624 				wp_cache_add_global_groups( $global_groups );
   642 				wp_cache_add_global_groups( $global_groups );
   625 			} else {
   643 			} else {
   626 				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
   644 				wp_cache_add_global_groups(
       
   645 					array(
       
   646 						'blog-details',
       
   647 						'blog-id-cache',
       
   648 						'blog-lookup',
       
   649 						'blog_meta',
       
   650 						'global-posts',
       
   651 						'networks',
       
   652 						'network-queries',
       
   653 						'sites',
       
   654 						'site-details',
       
   655 						'site-options',
       
   656 						'site-queries',
       
   657 						'site-transient',
       
   658 						'theme_files',
       
   659 						'rss',
       
   660 						'users',
       
   661 						'user-queries',
       
   662 						'user_meta',
       
   663 						'useremail',
       
   664 						'userlogins',
       
   665 						'userslugs',
       
   666 					)
       
   667 				);
   627 			}
   668 			}
   628 
   669 
   629 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   670 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
   630 		}
   671 		}
   631 	}
   672 	}
   632 
   673 
   633 	/** This filter is documented in wp-includes/ms-blogs.php */
   674 	/** This filter is documented in wp-includes/ms-blogs.php */
   634 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   675 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   646  *
   687  *
   647  * @param int $new_site_id New site ID.
   688  * @param int $new_site_id New site ID.
   648  * @param int $old_site_id Old site ID.
   689  * @param int $old_site_id Old site ID.
   649  */
   690  */
   650 function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
   691 function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
   651 	if ( $new_site_id == $old_site_id ) {
   692 	if ( $new_site_id === $old_site_id ) {
   652 		return;
   693 		return;
   653 	}
   694 	}
   654 
   695 
   655 	if ( ! did_action( 'init' ) ) {
   696 	if ( ! did_action( 'init' ) ) {
   656 		return;
   697 		return;
   659 	wp_roles()->for_site( $new_site_id );
   700 	wp_roles()->for_site( $new_site_id );
   660 	wp_get_current_user()->for_site( $new_site_id );
   701 	wp_get_current_user()->for_site( $new_site_id );
   661 }
   702 }
   662 
   703 
   663 /**
   704 /**
   664  * Determines if switch_to_blog() is in effect
   705  * Determines if switch_to_blog() is in effect.
   665  *
   706  *
   666  * @since 3.5.0
   707  * @since 3.5.0
   667  *
   708  *
   668  * @global array $_wp_switched_stack
   709  * @global array $_wp_switched_stack
   669  *
   710  *
   672 function ms_is_switched() {
   713 function ms_is_switched() {
   673 	return ! empty( $GLOBALS['_wp_switched_stack'] );
   714 	return ! empty( $GLOBALS['_wp_switched_stack'] );
   674 }
   715 }
   675 
   716 
   676 /**
   717 /**
   677  * Check if a particular blog is archived.
   718  * Checks if a particular blog is archived.
   678  *
   719  *
   679  * @since MU (3.0.0)
   720  * @since MU (3.0.0)
   680  *
   721  *
   681  * @param int $id Blog ID.
   722  * @param int $id Blog ID.
   682  * @return string Whether the blog is archived or not.
   723  * @return string Whether the blog is archived or not.
   684 function is_archived( $id ) {
   725 function is_archived( $id ) {
   685 	return get_blog_status( $id, 'archived' );
   726 	return get_blog_status( $id, 'archived' );
   686 }
   727 }
   687 
   728 
   688 /**
   729 /**
   689  * Update the 'archived' status of a particular blog.
   730  * Updates the 'archived' status of a particular blog.
   690  *
   731  *
   691  * @since MU (3.0.0)
   732  * @since MU (3.0.0)
   692  *
   733  *
   693  * @param int    $id       Blog ID.
   734  * @param int    $id       Blog ID.
   694  * @param string $archived The new status.
   735  * @param string $archived The new status.
   698 	update_blog_status( $id, 'archived', $archived );
   739 	update_blog_status( $id, 'archived', $archived );
   699 	return $archived;
   740 	return $archived;
   700 }
   741 }
   701 
   742 
   702 /**
   743 /**
   703  * Update a blog details field.
   744  * Updates a blog details field.
   704  *
   745  *
   705  * @since MU (3.0.0)
   746  * @since MU (3.0.0)
   706  * @since 5.1.0 Use wp_update_site() internally.
   747  * @since 5.1.0 Use wp_update_site() internally.
   707  *
   748  *
   708  * @global wpdb $wpdb WordPress database abstraction object.
   749  * @global wpdb $wpdb WordPress database abstraction object.
   739 
   780 
   740 	return $value;
   781 	return $value;
   741 }
   782 }
   742 
   783 
   743 /**
   784 /**
   744  * Get a blog details field.
   785  * Gets a blog details field.
   745  *
   786  *
   746  * @since MU (3.0.0)
   787  * @since MU (3.0.0)
   747  *
   788  *
   748  * @global wpdb $wpdb WordPress database abstraction object.
   789  * @global wpdb $wpdb WordPress database abstraction object.
   749  *
   790  *
   761 
   802 
   762 	return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
   803 	return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
   763 }
   804 }
   764 
   805 
   765 /**
   806 /**
   766  * Get a list of most recently updated blogs.
   807  * Gets a list of most recently updated blogs.
   767  *
   808  *
   768  * @since MU (3.0.0)
   809  * @since MU (3.0.0)
   769  *
   810  *
   770  * @global wpdb $wpdb WordPress database abstraction object.
   811  * @global wpdb $wpdb WordPress database abstraction object.
   771  *
   812  *
   835 
   876 
   836 /**
   877 /**
   837  * Handler for updating the current site's posts count when a post is deleted.
   878  * Handler for updating the current site's posts count when a post is deleted.
   838  *
   879  *
   839  * @since 4.0.0
   880  * @since 4.0.0
   840  *
   881  * @since 6.2.0 Added the `$post` parameter.
   841  * @param int $post_id Post ID.
   882  *
   842  */
   883  * @param int     $post_id Post ID.
   843 function _update_posts_count_on_delete( $post_id ) {
   884  * @param WP_Post $post    Post object.
   844 	$post = get_post( $post_id );
   885  */
   845 
   886 function _update_posts_count_on_delete( $post_id, $post ) {
   846 	if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
   887 	if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
   847 		return;
   888 		return;
   848 	}
   889 	}
   849 
   890 
   850 	update_posts_count();
   891 	update_posts_count();
   875 
   916 
   876 	update_posts_count();
   917 	update_posts_count();
   877 }
   918 }
   878 
   919 
   879 /**
   920 /**
   880  * Count number of sites grouped by site status.
   921  * Counts number of sites grouped by site status.
   881  *
   922  *
   882  * @since 5.3.0
   923  * @since 5.3.0
   883  *
   924  *
   884  * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
   925  * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
   885  * @return int[] {
   926  * @return int[] {