al 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'. $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( 'blog-details', 'blog-id-cache', 'blog-lookup', 'blog_meta', 'global-posts', 'networks', 'network-queries', 'sites', 'site-details', 'site-options', 'site-queries', 'site-transient', 'theme_files', 'rss', 'users', 'user-queries', 'user_meta', 'useremail', 'userlogins', 'userslugs', ) ); } wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); } } /** 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; } /** * Switches the initialized roles and current user capabilities to another site. * * @since 4.9.0 * * @param int $new_site_id New site ID. * @param int $old_site_id Old site ID. */ function wp_switch_roles_and_user( $new_site_id, $old_site_id ) { if ( $new_site_id == $old_site_id ) { return; } if ( ! did_action( 'init' ) ) { return; } wp_roles()->for_site( $new_site_id ); wp_get_current_user()->for_site( $new_site_id ); } /** * Determines if switch_to_blog() is in effect. * * @since 3.5.0 * * @global array $_wp_switched_stack * * @return bool True if switched, false otherwise. */ function ms_is_switched() { return ! empty( $GLOBALS['_wp_switched_stack'] ); } /** * Checks if a particular blog is archived. * * @since MU (3.0.0) * * @param int $id Blog ID. * @return string Whether the blog is archived or not. */ function is_archived( $id ) { return get_blog_status( $id, 'archived' ); } /** * Updates the 'archived' status of a particular blog. * * @since MU (3.0.0) * * @param int $id Blog ID. * @param string $archived The new status. * @return string $archived */ function update_archived( $id, $archived ) { update_blog_status( $id, 'archived', $archived ); return $archived; } /** * Updates a blog details field. * * @since MU (3.0.0) * @since 5.1.0 Use wp_update_site() internally. * * @global wpdb $wpdb WordPress database abstraction object. * * @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 ) { global $wpdb; if ( null !== $deprecated ) { _deprecated_argument( __FUNCTION__, '3.1.0' ); } $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; } $result = wp_update_site( $blog_id, array( $pref => $value, ) ); if ( is_wp_error( $result ) ) { return false; } return $value; } /** * Gets a blog details field. * * @since MU (3.0.0) * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $id Blog ID. * @param string $pref Field name. * @return bool|string|null $value */ function get_blog_status( $id, $pref ) { global $wpdb; $details = get_site( $id ); if ( $details ) { return $details->$pref; } return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) ); } /** * Gets a list of most recently updated blogs. * * @since MU (3.0.0) * * @global wpdb $wpdb WordPress database abstraction object. * * @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. } 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 ); } /** * Handler for updating the site's last updated date when a post is published or * an already published post is changed. * * @since 3.3.0 * * @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 ); if ( ! $post_type_obj || ! $post_type_obj->public ) { return; } if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } // Post was freshly published, published post was saved, or published post was unpublished. wpmu_update_blogs_date(); } /** * Handler for updating the current site's last updated date when a published * post is deleted. * * @since 3.4.0 * * @param int $post_id Post ID */ function _update_blog_date_on_post_delete( $post_id ) { $post = get_post( $post_id ); $post_type_obj = get_post_type_object( $post->post_type ); if ( ! $post_type_obj || ! $post_type_obj->public ) { return; } if ( 'publish' !== $post->post_status ) { return; } wpmu_update_blogs_date(); } /** * Handler for updating the current site's posts count when a post is deleted. * * @since 4.0.0 * @since 6.2.0 Added the `$post` parameter. * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ function _update_posts_count_on_delete( $post_id, $post ) { if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) { return; } update_posts_count(); } /** * Handler for updating the current site's posts count when a post status changes. * * @since 4.0.0 * @since 4.9.0 Added the `$post` parameter. * * @param string $new_status The status the post is changing to. * @param string $old_status The status the post is changing from. * @param WP_Post $post Post object */ function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) { if ( $new_status === $old_status ) { return; } if ( 'post' !== get_post_type( $post ) ) { return; } if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } update_posts_count(); } /** * Counts 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; }