wp/wp-includes/ms-blogs.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     6  * @package WordPress
     6  * @package WordPress
     7  * @subpackage Multisite
     7  * @subpackage Multisite
     8  * @since MU (3.0.0)
     8  * @since MU (3.0.0)
     9  */
     9  */
    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  * Update the last_updated field for the current site.
    16  *
    16  *
    17  * @since MU (3.0.0)
    17  * @since MU (3.0.0)
    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  * Get a full blog URL, given a blog 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 Blog ID.
    39  * @return string Full URL of the blog if found. Empty string if not.
    39  * @return string Full URL of the blog 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 
   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 ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
   140 			if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
   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 ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
   158 			if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
   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 			}
   236 	if ( ! $get_all ) {
   236 	if ( ! $get_all ) {
   237 		wp_cache_set( $blog_id . $all, $details, 'blog-details' );
   237 		wp_cache_set( $blog_id . $all, $details, 'blog-details' );
   238 		return $details;
   238 		return $details;
   239 	}
   239 	}
   240 
   240 
   241 	switch_to_blog( $blog_id );
   241 	$switched_blog = false;
       
   242 
       
   243 	if ( get_current_blog_id() !== $blog_id ) {
       
   244 		switch_to_blog( $blog_id );
       
   245 		$switched_blog = true;
       
   246 	}
       
   247 
   242 	$details->blogname   = get_option( 'blogname' );
   248 	$details->blogname   = get_option( 'blogname' );
   243 	$details->siteurl    = get_option( 'siteurl' );
   249 	$details->siteurl    = get_option( 'siteurl' );
   244 	$details->post_count = get_option( 'post_count' );
   250 	$details->post_count = get_option( 'post_count' );
   245 	$details->home       = get_option( 'home' );
   251 	$details->home       = get_option( 'home' );
   246 	restore_current_blog();
   252 
       
   253 	if ( $switched_blog ) {
       
   254 		restore_current_blog();
       
   255 	}
   247 
   256 
   248 	/**
   257 	/**
   249 	 * Filters a blog's details.
   258 	 * Filters a blog's details.
   250 	 *
   259 	 *
   251 	 * @since MU (3.0.0)
   260 	 * @since MU (3.0.0)
   252 	 * @deprecated 4.7.0 Use site_details
   261 	 * @deprecated 4.7.0 Use {@see 'site_details'} instead.
   253 	 *
   262 	 *
   254 	 * @param object $details The blog details.
   263 	 * @param object $details The blog details.
   255 	 */
   264 	 */
   256 	$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
   265 	$details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
   257 
   266 
   278 
   287 
   279 	clean_blog_cache( $blog_id );
   288 	clean_blog_cache( $blog_id );
   280 }
   289 }
   281 
   290 
   282 /**
   291 /**
   283  * Update the details for a blog. Updates the blogs table for a given blog id.
   292  * Update the details for a blog. Updates the blogs table for a given blog ID.
   284  *
   293  *
   285  * @since MU (3.0.0)
   294  * @since MU (3.0.0)
   286  *
   295  *
   287  * @global wpdb $wpdb WordPress database abstraction object.
   296  * @global wpdb $wpdb WordPress database abstraction object.
   288  *
   297  *
   289  * @param int   $blog_id Blog ID
   298  * @param int   $blog_id Blog ID.
   290  * @param array $details Array of details keyed by blogs table field names.
   299  * @param array $details Array of details keyed by blogs table field names.
   291  * @return bool True if update succeeds, false otherwise.
   300  * @return bool True if update succeeds, false otherwise.
   292  */
   301  */
   293 function update_blog_details( $blog_id, $details = array() ) {
   302 function update_blog_details( $blog_id, $details = array() ) {
   294 	global $wpdb;
   303 	global $wpdb;
   371 	 */
   380 	 */
   372 	return apply_filters( "blog_option_{$option}", $value, $id );
   381 	return apply_filters( "blog_option_{$option}", $value, $id );
   373 }
   382 }
   374 
   383 
   375 /**
   384 /**
   376  * Add a new option for a given blog id.
   385  * Add a new option for a given blog ID.
   377  *
   386  *
   378  * You do not need to serialize values. If the value needs to be serialized, then
   387  * You do not need to serialize values. If the value needs to be serialized, then
   379  * it will be serialized before it is inserted into the database. Remember,
   388  * it will be serialized before it is inserted into the database. Remember,
   380  * resources can not be serialized or added as an option.
   389  * resources can not be serialized or added as an option.
   381  *
   390  *
   387  * @since MU (3.0.0)
   396  * @since MU (3.0.0)
   388  *
   397  *
   389  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   398  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   390  * @param string $option Name of option to add. Expected to not be SQL-escaped.
   399  * @param string $option Name of option to add. Expected to not be SQL-escaped.
   391  * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
   400  * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
   392  * @return bool False if option was not added and true if option was added.
   401  * @return bool True if the option was added, false otherwise.
   393  */
   402  */
   394 function add_blog_option( $id, $option, $value ) {
   403 function add_blog_option( $id, $option, $value ) {
   395 	$id = (int) $id;
   404 	$id = (int) $id;
   396 
   405 
   397 	if ( empty( $id ) ) {
   406 	if ( empty( $id ) ) {
   408 
   417 
   409 	return $return;
   418 	return $return;
   410 }
   419 }
   411 
   420 
   412 /**
   421 /**
   413  * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
   422  * Removes option by name for a given blog ID. Prevents removal of protected WordPress options.
   414  *
   423  *
   415  * @since MU (3.0.0)
   424  * @since MU (3.0.0)
   416  *
   425  *
   417  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   426  * @param int    $id     A blog ID. Can be null to refer to the current blog.
   418  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   427  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   419  * @return bool True, if option is successfully deleted. False on failure.
   428  * @return bool True if the option was deleted, false otherwise.
   420  */
   429  */
   421 function delete_blog_option( $id, $option ) {
   430 function delete_blog_option( $id, $option ) {
   422 	$id = (int) $id;
   431 	$id = (int) $id;
   423 
   432 
   424 	if ( empty( $id ) ) {
   433 	if ( empty( $id ) ) {
   439 /**
   448 /**
   440  * Update an option for a particular blog.
   449  * Update an option for a particular blog.
   441  *
   450  *
   442  * @since MU (3.0.0)
   451  * @since MU (3.0.0)
   443  *
   452  *
   444  * @param int    $id         The blog id.
   453  * @param int    $id         The blog ID.
   445  * @param string $option     The option key.
   454  * @param string $option     The option key.
   446  * @param mixed  $value      The option value.
   455  * @param mixed  $value      The option value.
   447  * @param mixed  $deprecated Not used.
   456  * @param mixed  $deprecated Not used.
   448  * @return bool True on success, false on failure.
   457  * @return bool True if the value was updated, false otherwise.
   449  */
   458  */
   450 function update_blog_option( $id, $option, $value, $deprecated = null ) {
   459 function update_blog_option( $id, $option, $value, $deprecated = null ) {
   451 	$id = (int) $id;
   460 	$id = (int) $id;
   452 
   461 
   453 	if ( null !== $deprecated ) {
   462 	if ( null !== $deprecated ) {
   475  *  - plugins. See #14941
   484  *  - plugins. See #14941
   476  *
   485  *
   477  * @see restore_current_blog()
   486  * @see restore_current_blog()
   478  * @since MU (3.0.0)
   487  * @since MU (3.0.0)
   479  *
   488  *
   480  * @global wpdb            $wpdb
   489  * @global wpdb            $wpdb               WordPress database abstraction object.
   481  * @global int             $blog_id
   490  * @global int             $blog_id
   482  * @global array           $_wp_switched_stack
   491  * @global array           $_wp_switched_stack
   483  * @global bool            $switched
   492  * @global bool            $switched
   484  * @global string          $table_prefix
   493  * @global string          $table_prefix
   485  * @global WP_Object_Cache $wp_object_cache
   494  * @global WP_Object_Cache $wp_object_cache
   486  *
   495  *
   487  * @param int  $new_blog   The id of the blog you want to switch to. Default: current blog
   496  * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
   488  * @param bool $deprecated Deprecated argument
   497  * @param bool $deprecated  Not used.
   489  * @return true Always returns True.
   498  * @return true Always returns true.
   490  */
   499  */
   491 function switch_to_blog( $new_blog, $deprecated = null ) {
   500 function switch_to_blog( $new_blog_id, $deprecated = null ) {
   492 	global $wpdb;
   501 	global $wpdb;
   493 
   502 
   494 	$blog_id = get_current_blog_id();
   503 	$prev_blog_id = get_current_blog_id();
   495 	if ( empty( $new_blog ) ) {
   504 	if ( empty( $new_blog_id ) ) {
   496 		$new_blog = $blog_id;
   505 		$new_blog_id = $prev_blog_id;
   497 	}
   506 	}
   498 
   507 
   499 	$GLOBALS['_wp_switched_stack'][] = $blog_id;
   508 	$GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
   500 
   509 
   501 	/*
   510 	/*
   502 	 * If we're switching to the same blog id that we're on,
   511 	 * If we're switching to the same blog id that we're on,
   503 	 * set the right vars, do the associated actions, but skip
   512 	 * set the right vars, do the associated actions, but skip
   504 	 * the extra unnecessary work
   513 	 * the extra unnecessary work
   505 	 */
   514 	 */
   506 	if ( $new_blog == $blog_id ) {
   515 	if ( $new_blog_id == $prev_blog_id ) {
   507 		/**
   516 		/**
   508 		 * Fires when the blog is switched.
   517 		 * Fires when the blog is switched.
   509 		 *
   518 		 *
   510 		 * @since MU (3.0.0)
   519 		 * @since MU (3.0.0)
       
   520 		 * @since 5.4.0 The `$context` parameter was added.
   511 		 *
   521 		 *
   512 		 * @param int $new_blog New blog ID.
   522 		 * @param int    $new_blog_id  New blog ID.
   513 		 * @param int $new_blog Blog ID.
   523 		 * @param int    $prev_blog_id Previous blog ID.
       
   524 		 * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
       
   525 		 *                             or 'restore' when called from restore_current_blog().
   514 		 */
   526 		 */
   515 		do_action( 'switch_blog', $new_blog, $new_blog );
   527 		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
       
   528 
   516 		$GLOBALS['switched'] = true;
   529 		$GLOBALS['switched'] = true;
       
   530 
   517 		return true;
   531 		return true;
   518 	}
   532 	}
   519 
   533 
   520 	$wpdb->set_blog_id( $new_blog );
   534 	$wpdb->set_blog_id( $new_blog_id );
   521 	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
   535 	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
   522 	$prev_blog_id            = $blog_id;
   536 	$GLOBALS['blog_id']      = $new_blog_id;
   523 	$GLOBALS['blog_id']      = $new_blog;
       
   524 
   537 
   525 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
   538 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
   526 		wp_cache_switch_to_blog( $new_blog );
   539 		wp_cache_switch_to_blog( $new_blog_id );
   527 	} else {
   540 	} else {
   528 		global $wp_object_cache;
   541 		global $wp_object_cache;
   529 
   542 
   530 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
   543 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
   531 			$global_groups = $wp_object_cache->global_groups;
   544 			$global_groups = $wp_object_cache->global_groups;
   532 		} else {
   545 		} else {
   533 			$global_groups = false;
   546 			$global_groups = false;
   534 		}
   547 		}
       
   548 
   535 		wp_cache_init();
   549 		wp_cache_init();
   536 
   550 
   537 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   551 		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
   538 			if ( is_array( $global_groups ) ) {
   552 			if ( is_array( $global_groups ) ) {
   539 				wp_cache_add_global_groups( $global_groups );
   553 				wp_cache_add_global_groups( $global_groups );
   540 			} else {
   554 			} else {
   541 				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' ) );
   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' ) );
   542 			}
   556 			}
       
   557 
   543 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   558 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   544 		}
   559 		}
   545 	}
   560 	}
   546 
   561 
   547 	/** This filter is documented in wp-includes/ms-blogs.php */
   562 	/** This filter is documented in wp-includes/ms-blogs.php */
   548 	do_action( 'switch_blog', $new_blog, $prev_blog_id );
   563 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
       
   564 
   549 	$GLOBALS['switched'] = true;
   565 	$GLOBALS['switched'] = true;
   550 
   566 
   551 	return true;
   567 	return true;
   552 }
   568 }
   553 
   569 
   554 /**
   570 /**
   555  * Restore the current blog, after calling switch_to_blog()
   571  * Restore the current blog, after calling switch_to_blog().
   556  *
   572  *
   557  * @see switch_to_blog()
   573  * @see switch_to_blog()
   558  * @since MU (3.0.0)
   574  * @since MU (3.0.0)
   559  *
   575  *
   560  * @global wpdb            $wpdb
   576  * @global wpdb            $wpdb               WordPress database abstraction object.
   561  * @global array           $_wp_switched_stack
   577  * @global array           $_wp_switched_stack
   562  * @global int             $blog_id
   578  * @global int             $blog_id
   563  * @global bool            $switched
   579  * @global bool            $switched
   564  * @global string          $table_prefix
   580  * @global string          $table_prefix
   565  * @global WP_Object_Cache $wp_object_cache
   581  * @global WP_Object_Cache $wp_object_cache
   566  *
   582  *
   567  * @return bool True on success, false if we're already on the current blog
   583  * @return bool True on success, false if we're already on the current blog.
   568  */
   584  */
   569 function restore_current_blog() {
   585 function restore_current_blog() {
   570 	global $wpdb;
   586 	global $wpdb;
   571 
   587 
   572 	if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
   588 	if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
   573 		return false;
   589 		return false;
   574 	}
   590 	}
   575 
   591 
   576 	$blog    = array_pop( $GLOBALS['_wp_switched_stack'] );
   592 	$new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
   577 	$blog_id = get_current_blog_id();
   593 	$prev_blog_id = get_current_blog_id();
   578 
   594 
   579 	if ( $blog_id == $blog ) {
   595 	if ( $new_blog_id == $prev_blog_id ) {
   580 		/** This filter is documented in wp-includes/ms-blogs.php */
   596 		/** This filter is documented in wp-includes/ms-blogs.php */
   581 		do_action( 'switch_blog', $blog, $blog );
   597 		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   582 		// If we still have items in the switched stack, consider ourselves still 'switched'
   598 
       
   599 		// If we still have items in the switched stack, consider ourselves still 'switched'.
   583 		$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
   600 		$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
       
   601 
   584 		return true;
   602 		return true;
   585 	}
   603 	}
   586 
   604 
   587 	$wpdb->set_blog_id( $blog );
   605 	$wpdb->set_blog_id( $new_blog_id );
   588 	$prev_blog_id            = $blog_id;
   606 	$GLOBALS['blog_id']      = $new_blog_id;
   589 	$GLOBALS['blog_id']      = $blog;
       
   590 	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
   607 	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
   591 
   608 
   592 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
   609 	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
   593 		wp_cache_switch_to_blog( $blog );
   610 		wp_cache_switch_to_blog( $new_blog_id );
   594 	} else {
   611 	} else {
   595 		global $wp_object_cache;
   612 		global $wp_object_cache;
   596 
   613 
   597 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
   614 		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
   598 			$global_groups = $wp_object_cache->global_groups;
   615 			$global_groups = $wp_object_cache->global_groups;
   606 			if ( is_array( $global_groups ) ) {
   623 			if ( is_array( $global_groups ) ) {
   607 				wp_cache_add_global_groups( $global_groups );
   624 				wp_cache_add_global_groups( $global_groups );
   608 			} else {
   625 			} else {
   609 				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' ) );
   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' ) );
   610 			}
   627 			}
       
   628 
   611 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   629 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
   612 		}
   630 		}
   613 	}
   631 	}
   614 
   632 
   615 	/** This filter is documented in wp-includes/ms-blogs.php */
   633 	/** This filter is documented in wp-includes/ms-blogs.php */
   616 	do_action( 'switch_blog', $blog, $prev_blog_id );
   634 	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
   617 
   635 
   618 	// If we still have items in the switched stack, consider ourselves still 'switched'
   636 	// If we still have items in the switched stack, consider ourselves still 'switched'.
   619 	$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
   637 	$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
   620 
   638 
   621 	return true;
   639 	return true;
   622 }
   640 }
   623 
   641 
   658 /**
   676 /**
   659  * Check if a particular blog is archived.
   677  * Check if a particular blog is archived.
   660  *
   678  *
   661  * @since MU (3.0.0)
   679  * @since MU (3.0.0)
   662  *
   680  *
   663  * @param int $id The blog id
   681  * @param int $id Blog ID.
   664  * @return string Whether the blog is archived or not
   682  * @return string Whether the blog is archived or not.
   665  */
   683  */
   666 function is_archived( $id ) {
   684 function is_archived( $id ) {
   667 	return get_blog_status( $id, 'archived' );
   685 	return get_blog_status( $id, 'archived' );
   668 }
   686 }
   669 
   687 
   670 /**
   688 /**
   671  * Update the 'archived' status of a particular blog.
   689  * Update the 'archived' status of a particular blog.
   672  *
   690  *
   673  * @since MU (3.0.0)
   691  * @since MU (3.0.0)
   674  *
   692  *
   675  * @param int    $id       The blog id
   693  * @param int    $id       Blog ID.
   676  * @param string $archived The new status
   694  * @param string $archived The new status.
   677  * @return string $archived
   695  * @return string $archived
   678  */
   696  */
   679 function update_archived( $id, $archived ) {
   697 function update_archived( $id, $archived ) {
   680 	update_blog_status( $id, 'archived', $archived );
   698 	update_blog_status( $id, 'archived', $archived );
   681 	return $archived;
   699 	return $archived;
   687  * @since MU (3.0.0)
   705  * @since MU (3.0.0)
   688  * @since 5.1.0 Use wp_update_site() internally.
   706  * @since 5.1.0 Use wp_update_site() internally.
   689  *
   707  *
   690  * @global wpdb $wpdb WordPress database abstraction object.
   708  * @global wpdb $wpdb WordPress database abstraction object.
   691  *
   709  *
   692  * @param int    $blog_id BLog ID
   710  * @param int    $blog_id    Blog ID.
   693  * @param string $pref    A field name
   711  * @param string $pref       Field name.
   694  * @param string $value   Value for $pref
   712  * @param string $value      Field value.
   695  * @param null   $deprecated
   713  * @param null   $deprecated Not used.
   696  * @return string|false $value
   714  * @return string|false $value
   697  */
   715  */
   698 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
   716 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
   699 	global $wpdb;
   717 	global $wpdb;
   700 
   718 
   701 	if ( null !== $deprecated ) {
   719 	if ( null !== $deprecated ) {
   702 		_deprecated_argument( __FUNCTION__, '3.1.0' );
   720 		_deprecated_argument( __FUNCTION__, '3.1.0' );
   703 	}
   721 	}
   704 
   722 
   705 	if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
   723 	$allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
       
   724 
       
   725 	if ( ! in_array( $pref, $allowed_field_names, true ) ) {
   706 		return $value;
   726 		return $value;
   707 	}
   727 	}
   708 
   728 
   709 	$result = wp_update_site(
   729 	$result = wp_update_site(
   710 		$blog_id,
   730 		$blog_id,
   725  *
   745  *
   726  * @since MU (3.0.0)
   746  * @since MU (3.0.0)
   727  *
   747  *
   728  * @global wpdb $wpdb WordPress database abstraction object.
   748  * @global wpdb $wpdb WordPress database abstraction object.
   729  *
   749  *
   730  * @param int    $id   The blog id
   750  * @param int    $id   Blog ID.
   731  * @param string $pref A field name
   751  * @param string $pref Field name.
   732  * @return bool|string|null $value
   752  * @return bool|string|null $value
   733  */
   753  */
   734 function get_blog_status( $id, $pref ) {
   754 function get_blog_status( $id, $pref ) {
   735 	global $wpdb;
   755 	global $wpdb;
   736 
   756 
   747  *
   767  *
   748  * @since MU (3.0.0)
   768  * @since MU (3.0.0)
   749  *
   769  *
   750  * @global wpdb $wpdb WordPress database abstraction object.
   770  * @global wpdb $wpdb WordPress database abstraction object.
   751  *
   771  *
   752  * @param mixed $deprecated Not used
   772  * @param mixed $deprecated Not used.
   753  * @param int   $start      The offset
   773  * @param int   $start      Optional. Number of blogs to offset the query. Used to build LIMIT clause.
   754  * @param int   $quantity   The maximum number of blogs to retrieve. Default is 40.
   774  *                          Can be used for pagination. Default 0.
   755  * @return array The list of blogs
   775  * @param int   $quantity   Optional. The maximum number of blogs to retrieve. Default 40.
       
   776  * @return array The list of blogs.
   756  */
   777  */
   757 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
   778 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
   758 	global $wpdb;
   779 	global $wpdb;
   759 
   780 
   760 	if ( ! empty( $deprecated ) ) {
   781 	if ( ! empty( $deprecated ) ) {
   761 		_deprecated_argument( __FUNCTION__, 'MU' ); // never used
   782 		_deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
   762 	}
   783 	}
   763 
   784 
   764 	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", get_current_network_id(), $start, $quantity ), ARRAY_A );
   785 	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", get_current_network_id(), $start, $quantity ), ARRAY_A );
   765 }
   786 }
   766 
   787 
   768  * Handler for updating the site's last updated date when a post is published or
   789  * Handler for updating the site's last updated date when a post is published or
   769  * an already published post is changed.
   790  * an already published post is changed.
   770  *
   791  *
   771  * @since 3.3.0
   792  * @since 3.3.0
   772  *
   793  *
   773  * @param string $new_status The new post status
   794  * @param string  $new_status The new post status.
   774  * @param string $old_status The old post status
   795  * @param string  $old_status The old post status.
   775  * @param object $post       Post object
   796  * @param WP_Post $post       Post object.
   776  */
   797  */
   777 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
   798 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
   778 	$post_type_obj = get_post_type_object( $post->post_type );
   799 	$post_type_obj = get_post_type_object( $post->post_type );
   779 	if ( ! $post_type_obj || ! $post_type_obj->public ) {
   800 	if ( ! $post_type_obj || ! $post_type_obj->public ) {
   780 		return;
   801 		return;
   781 	}
   802 	}
   782 
   803 
   783 	if ( 'publish' != $new_status && 'publish' != $old_status ) {
   804 	if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
   784 		return;
   805 		return;
   785 	}
   806 	}
   786 
   807 
   787 	// Post was freshly published, published post was saved, or published post was unpublished.
   808 	// Post was freshly published, published post was saved, or published post was unpublished.
   788 
   809 
   803 	$post_type_obj = get_post_type_object( $post->post_type );
   824 	$post_type_obj = get_post_type_object( $post->post_type );
   804 	if ( ! $post_type_obj || ! $post_type_obj->public ) {
   825 	if ( ! $post_type_obj || ! $post_type_obj->public ) {
   805 		return;
   826 		return;
   806 	}
   827 	}
   807 
   828 
   808 	if ( 'publish' != $post->post_status ) {
   829 	if ( 'publish' !== $post->post_status ) {
   809 		return;
   830 		return;
   810 	}
   831 	}
   811 
   832 
   812 	wpmu_update_blogs_date();
   833 	wpmu_update_blogs_date();
   813 }
   834 }
   852 		return;
   873 		return;
   853 	}
   874 	}
   854 
   875 
   855 	update_posts_count();
   876 	update_posts_count();
   856 }
   877 }
       
   878 
       
   879 /**
       
   880  * Count number of sites grouped by site status.
       
   881  *
       
   882  * @since 5.3.0
       
   883  *
       
   884  * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
       
   885  * @return int[] {
       
   886  *     Numbers of sites grouped by site status.
       
   887  *
       
   888  *     @type int $all      The total number of sites.
       
   889  *     @type int $public   The number of public sites.
       
   890  *     @type int $archived The number of archived sites.
       
   891  *     @type int $mature   The number of mature sites.
       
   892  *     @type int $spam     The number of spam sites.
       
   893  *     @type int $deleted  The number of deleted sites.
       
   894  * }
       
   895  */
       
   896 function wp_count_sites( $network_id = null ) {
       
   897 	if ( empty( $network_id ) ) {
       
   898 		$network_id = get_current_network_id();
       
   899 	}
       
   900 
       
   901 	$counts = array();
       
   902 	$args   = array(
       
   903 		'network_id'    => $network_id,
       
   904 		'number'        => 1,
       
   905 		'fields'        => 'ids',
       
   906 		'no_found_rows' => false,
       
   907 	);
       
   908 
       
   909 	$q             = new WP_Site_Query( $args );
       
   910 	$counts['all'] = $q->found_sites;
       
   911 
       
   912 	$_args    = $args;
       
   913 	$statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
       
   914 
       
   915 	foreach ( $statuses as $status ) {
       
   916 		$_args            = $args;
       
   917 		$_args[ $status ] = 1;
       
   918 
       
   919 		$q                 = new WP_Site_Query( $_args );
       
   920 		$counts[ $status ] = $q->found_sites;
       
   921 	}
       
   922 
       
   923 	return $counts;
       
   924 }