diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-includes/ms-blogs.php --- a/wp/wp-includes/ms-blogs.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-includes/ms-blogs.php Tue Dec 15 13:49:49 2020 +0100 @@ -8,8 +8,8 @@ * @since MU (3.0.0) */ -require_once( ABSPATH . WPINC . '/ms-site.php' ); -require_once( ABSPATH . WPINC . '/ms-network.php' ); +require_once ABSPATH . WPINC . '/ms-site.php'; +require_once ABSPATH . WPINC . '/ms-network.php'; /** * Update the last_updated field for the current site. @@ -31,11 +31,11 @@ } /** - * Get a full blog URL, given a blog id. + * Get a full blog URL, given a blog ID. * * @since MU (3.0.0) * - * @param int $blog_id Blog ID + * @param int $blog_id Blog ID. * @return string Full URL of the blog if found. Empty string if not. */ function get_blogaddress_by_id( $blog_id ) { @@ -137,7 +137,7 @@ if ( false !== $blog ) { return $blog; } - if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { + if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { $nowww = substr( $fields['domain'], 4 ); $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'] ) ); } else { @@ -155,7 +155,7 @@ if ( false !== $blog ) { return $blog; } - if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { + if ( 'www.' === substr( $fields['domain'], 0, 4 ) ) { $nowww = substr( $fields['domain'], 4 ); $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); } else { @@ -238,18 +238,27 @@ return $details; } - switch_to_blog( $blog_id ); + $switched_blog = false; + + if ( get_current_blog_id() !== $blog_id ) { + switch_to_blog( $blog_id ); + $switched_blog = true; + } + $details->blogname = get_option( 'blogname' ); $details->siteurl = get_option( 'siteurl' ); $details->post_count = get_option( 'post_count' ); $details->home = get_option( 'home' ); - restore_current_blog(); + + if ( $switched_blog ) { + restore_current_blog(); + } /** * Filters a blog's details. * * @since MU (3.0.0) - * @deprecated 4.7.0 Use site_details + * @deprecated 4.7.0 Use {@see 'site_details'} instead. * * @param object $details The blog details. */ @@ -280,13 +289,13 @@ } /** - * Update the details for a blog. Updates the blogs table for a given blog id. + * Update the details for a blog. Updates the blogs table for a given blog ID. * * @since MU (3.0.0) * * @global wpdb $wpdb WordPress database abstraction object. * - * @param int $blog_id Blog ID + * @param int $blog_id Blog ID. * @param array $details Array of details keyed by blogs table field names. * @return bool True if update succeeds, false otherwise. */ @@ -373,7 +382,7 @@ } /** - * Add a new option for a given blog id. + * Add a new option for a given blog ID. * * You do not need to serialize values. If the value needs to be serialized, then * it will be serialized before it is inserted into the database. Remember, @@ -389,7 +398,7 @@ * @param int $id A blog ID. Can be null to refer to the current blog. * @param string $option Name of option to add. Expected to not be SQL-escaped. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. - * @return bool False if option was not added and true if option was added. + * @return bool True if the option was added, false otherwise. */ function add_blog_option( $id, $option, $value ) { $id = (int) $id; @@ -410,13 +419,13 @@ } /** - * Removes option by name for a given blog id. Prevents removal of protected WordPress options. + * Removes option by name for a given blog ID. Prevents removal of protected WordPress options. * * @since MU (3.0.0) * * @param int $id A blog ID. Can be null to refer to the current blog. * @param string $option Name of option to remove. Expected to not be SQL-escaped. - * @return bool True, if option is successfully deleted. False on failure. + * @return bool True if the option was deleted, false otherwise. */ function delete_blog_option( $id, $option ) { $id = (int) $id; @@ -441,11 +450,11 @@ * * @since MU (3.0.0) * - * @param int $id The blog id. + * @param int $id The blog ID. * @param string $option The option key. * @param mixed $value The option value. * @param mixed $deprecated Not used. - * @return bool True on success, false on failure. + * @return bool True if the value was updated, false otherwise. */ function update_blog_option( $id, $option, $value, $deprecated = null ) { $id = (int) $id; @@ -477,120 +486,57 @@ * @see restore_current_blog() * @since MU (3.0.0) * - * @global wpdb $wpdb + * @global wpdb $wpdb WordPress database abstraction object. * @global int $blog_id * @global array $_wp_switched_stack * @global bool $switched * @global string $table_prefix * @global WP_Object_Cache $wp_object_cache * - * @param int $new_blog The id of the blog you want to switch to. Default: current blog - * @param bool $deprecated Deprecated argument - * @return true Always returns True. + * @param int $new_blog_id The ID of the blog to switch to. Default: current blog. + * @param bool $deprecated Not used. + * @return true Always returns true. */ -function switch_to_blog( $new_blog, $deprecated = null ) { +function switch_to_blog( $new_blog_id, $deprecated = null ) { global $wpdb; - $blog_id = get_current_blog_id(); - if ( empty( $new_blog ) ) { - $new_blog = $blog_id; + $prev_blog_id = get_current_blog_id(); + if ( empty( $new_blog_id ) ) { + $new_blog_id = $prev_blog_id; } - $GLOBALS['_wp_switched_stack'][] = $blog_id; + $GLOBALS['_wp_switched_stack'][] = $prev_blog_id; /* * If we're switching to the same blog id that we're on, * set the right vars, do the associated actions, but skip * the extra unnecessary work */ - if ( $new_blog == $blog_id ) { + if ( $new_blog_id == $prev_blog_id ) { /** * Fires when the blog is switched. * * @since MU (3.0.0) + * @since 5.4.0 The `$context` parameter was added. * - * @param int $new_blog New blog ID. - * @param int $new_blog Blog ID. + * @param int $new_blog_id New blog ID. + * @param int $prev_blog_id Previous blog ID. + * @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() + * or 'restore' when called from restore_current_blog(). */ - do_action( 'switch_blog', $new_blog, $new_blog ); + do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); + $GLOBALS['switched'] = true; + return true; } - $wpdb->set_blog_id( $new_blog ); + $wpdb->set_blog_id( $new_blog_id ); $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); - $prev_blog_id = $blog_id; - $GLOBALS['blog_id'] = $new_blog; + $GLOBALS['blog_id'] = $new_blog_id; if ( function_exists( 'wp_cache_switch_to_blog' ) ) { - wp_cache_switch_to_blog( $new_blog ); - } else { - global $wp_object_cache; - - if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { - $global_groups = $wp_object_cache->global_groups; - } else { - $global_groups = false; - } - wp_cache_init(); - - if ( function_exists( 'wp_cache_add_global_groups' ) ) { - if ( is_array( $global_groups ) ) { - wp_cache_add_global_groups( $global_groups ); - } else { - 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' ) ); - } - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); - } - } - - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'switch_blog', $new_blog, $prev_blog_id ); - $GLOBALS['switched'] = true; - - return true; -} - -/** - * Restore the current blog, after calling switch_to_blog() - * - * @see switch_to_blog() - * @since MU (3.0.0) - * - * @global wpdb $wpdb - * @global array $_wp_switched_stack - * @global int $blog_id - * @global bool $switched - * @global string $table_prefix - * @global WP_Object_Cache $wp_object_cache - * - * @return bool True on success, false if we're already on the current blog - */ -function restore_current_blog() { - global $wpdb; - - if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { - return false; - } - - $blog = array_pop( $GLOBALS['_wp_switched_stack'] ); - $blog_id = get_current_blog_id(); - - if ( $blog_id == $blog ) { - /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'switch_blog', $blog, $blog ); - // If we still have items in the switched stack, consider ourselves still 'switched' - $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); - return true; - } - - $wpdb->set_blog_id( $blog ); - $prev_blog_id = $blog_id; - $GLOBALS['blog_id'] = $blog; - $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); - - if ( function_exists( 'wp_cache_switch_to_blog' ) ) { - wp_cache_switch_to_blog( $blog ); + wp_cache_switch_to_blog( $new_blog_id ); } else { global $wp_object_cache; @@ -608,14 +554,86 @@ } else { 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' ) ); } + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); } } /** This filter is documented in wp-includes/ms-blogs.php */ - do_action( 'switch_blog', $blog, $prev_blog_id ); + do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' ); + + $GLOBALS['switched'] = true; + + return true; +} + +/** + * Restore the current blog, after calling switch_to_blog(). + * + * @see switch_to_blog() + * @since MU (3.0.0) + * + * @global wpdb $wpdb WordPress database abstraction object. + * @global array $_wp_switched_stack + * @global int $blog_id + * @global bool $switched + * @global string $table_prefix + * @global WP_Object_Cache $wp_object_cache + * + * @return bool True on success, false if we're already on the current blog. + */ +function restore_current_blog() { + global $wpdb; + + if ( empty( $GLOBALS['_wp_switched_stack'] ) ) { + return false; + } + + $new_blog_id = array_pop( $GLOBALS['_wp_switched_stack'] ); + $prev_blog_id = get_current_blog_id(); + + if ( $new_blog_id == $prev_blog_id ) { + /** This filter is documented in wp-includes/ms-blogs.php */ + do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); - // If we still have items in the switched stack, consider ourselves still 'switched' + // If we still have items in the switched stack, consider ourselves still 'switched'. + $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); + + return true; + } + + $wpdb->set_blog_id( $new_blog_id ); + $GLOBALS['blog_id'] = $new_blog_id; + $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); + + if ( function_exists( 'wp_cache_switch_to_blog' ) ) { + wp_cache_switch_to_blog( $new_blog_id ); + } else { + global $wp_object_cache; + + if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { + $global_groups = $wp_object_cache->global_groups; + } else { + $global_groups = false; + } + + wp_cache_init(); + + if ( function_exists( 'wp_cache_add_global_groups' ) ) { + if ( is_array( $global_groups ) ) { + wp_cache_add_global_groups( $global_groups ); + } else { + 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' ) ); + } + + wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); + } + } + + /** This filter is documented in wp-includes/ms-blogs.php */ + do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' ); + + // If we still have items in the switched stack, consider ourselves still 'switched'. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] ); return true; @@ -660,8 +678,8 @@ * * @since MU (3.0.0) * - * @param int $id The blog id - * @return string Whether the blog is archived or not + * @param int $id Blog ID. + * @return string Whether the blog is archived or not. */ function is_archived( $id ) { return get_blog_status( $id, 'archived' ); @@ -672,8 +690,8 @@ * * @since MU (3.0.0) * - * @param int $id The blog id - * @param string $archived The new status + * @param int $id Blog ID. + * @param string $archived The new status. * @return string $archived */ function update_archived( $id, $archived ) { @@ -689,10 +707,10 @@ * * @global wpdb $wpdb WordPress database abstraction object. * - * @param int $blog_id BLog ID - * @param string $pref A field name - * @param string $value Value for $pref - * @param null $deprecated + * @param int $blog_id Blog ID. + * @param string $pref Field name. + * @param string $value Field value. + * @param null $deprecated Not used. * @return string|false $value */ function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) { @@ -702,7 +720,9 @@ _deprecated_argument( __FUNCTION__, '3.1.0' ); } - if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) { + $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); + + if ( ! in_array( $pref, $allowed_field_names, true ) ) { return $value; } @@ -727,8 +747,8 @@ * * @global wpdb $wpdb WordPress database abstraction object. * - * @param int $id The blog id - * @param string $pref A field name + * @param int $id Blog ID. + * @param string $pref Field name. * @return bool|string|null $value */ function get_blog_status( $id, $pref ) { @@ -749,16 +769,17 @@ * * @global wpdb $wpdb WordPress database abstraction object. * - * @param mixed $deprecated Not used - * @param int $start The offset - * @param int $quantity The maximum number of blogs to retrieve. Default is 40. - * @return array The list of blogs + * @param mixed $deprecated Not used. + * @param int $start Optional. Number of blogs to offset the query. Used to build LIMIT clause. + * Can be used for pagination. Default 0. + * @param int $quantity Optional. The maximum number of blogs to retrieve. Default 40. + * @return array The list of blogs. */ function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) { global $wpdb; if ( ! empty( $deprecated ) ) { - _deprecated_argument( __FUNCTION__, 'MU' ); // never used + _deprecated_argument( __FUNCTION__, 'MU' ); // Never used. } 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 ); @@ -770,9 +791,9 @@ * * @since 3.3.0 * - * @param string $new_status The new post status - * @param string $old_status The old post status - * @param object $post Post object + * @param string $new_status The new post status. + * @param string $old_status The old post status. + * @param WP_Post $post Post object. */ function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) { $post_type_obj = get_post_type_object( $post->post_type ); @@ -780,7 +801,7 @@ return; } - if ( 'publish' != $new_status && 'publish' != $old_status ) { + if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } @@ -805,7 +826,7 @@ return; } - if ( 'publish' != $post->post_status ) { + if ( 'publish' !== $post->post_status ) { return; } @@ -854,3 +875,50 @@ update_posts_count(); } + +/** + * Count number of sites grouped by site status. + * + * @since 5.3.0 + * + * @param int $network_id Optional. The network to get counts for. Default is the current network ID. + * @return int[] { + * Numbers of sites grouped by site status. + * + * @type int $all The total number of sites. + * @type int $public The number of public sites. + * @type int $archived The number of archived sites. + * @type int $mature The number of mature sites. + * @type int $spam The number of spam sites. + * @type int $deleted The number of deleted sites. + * } + */ +function wp_count_sites( $network_id = null ) { + if ( empty( $network_id ) ) { + $network_id = get_current_network_id(); + } + + $counts = array(); + $args = array( + 'network_id' => $network_id, + 'number' => 1, + 'fields' => 'ids', + 'no_found_rows' => false, + ); + + $q = new WP_Site_Query( $args ); + $counts['all'] = $q->found_sites; + + $_args = $args; + $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); + + foreach ( $statuses as $status ) { + $_args = $args; + $_args[ $status ] = 1; + + $q = new WP_Site_Query( $_args ); + $counts[ $status ] = $q->found_sites; + } + + return $counts; +}