4 * Site/blog functions that work with the blogs table and related data.
7 * @subpackage Multisite
12 * Update the last_updated field for the current site.
16 * @global wpdb $wpdb WordPress database abstraction object.
18 function wpmu_update_blogs_date() {
21 update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
23 * Fires after the blog details are updated.
27 * @param int $blog_id Site ID.
29 do_action( 'wpmu_blog_updated', $wpdb->blogid );
33 * Get a full blog URL, given a blog id.
37 * @param int $blog_id Blog ID
38 * @return string Full URL of the blog if found. Empty string if not.
40 function get_blogaddress_by_id( $blog_id ) {
41 $bloginfo = get_site( (int) $blog_id );
43 if ( empty( $bloginfo ) ) {
47 $scheme = parse_url( $bloginfo->home, PHP_URL_SCHEME );
48 $scheme = empty( $scheme ) ? 'http' : $scheme;
50 return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
54 * Get a full blog URL, given a blog name.
58 * @param string $blogname The (subdomain or directory) name
61 function get_blogaddress_by_name( $blogname ) {
62 if ( is_subdomain_install() ) {
63 if ( $blogname == 'main' )
65 $url = rtrim( network_home_url(), '/' );
66 if ( !empty( $blogname ) )
67 $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
69 $url = network_home_url( $blogname );
71 return esc_url( $url . '/' );
75 * Retrieves a sites ID given its (subdomain or directory) slug.
78 * @since 4.7.0 Converted to use get_sites().
80 * @param string $slug A site's slug.
81 * @return int|null The site ID, or null if no site is found for the given slug.
83 function get_id_from_blogname( $slug ) {
84 $current_network = get_network();
85 $slug = trim( $slug, '/' );
87 if ( is_subdomain_install() ) {
88 $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
89 $path = $current_network->path;
91 $domain = $current_network->domain;
92 $path = $current_network->path . $slug . '/';
95 $site_ids = get_sites( array(
102 if ( empty( $site_ids ) ) {
106 return array_shift( $site_ids );
110 * Retrieve the details for a blog from the blogs table and blog options.
114 * @global wpdb $wpdb WordPress database abstraction object.
116 * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
117 * If not specified the current blog ID is used.
118 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table.
120 * @return WP_Site|false Blog details on success. False on failure.
122 function get_blog_details( $fields = null, $get_all = true ) {
125 if ( is_array($fields ) ) {
126 if ( isset($fields['blog_id']) ) {
127 $blog_id = $fields['blog_id'];
128 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
129 $key = md5( $fields['domain'] . $fields['path'] );
130 $blog = wp_cache_get($key, 'blog-lookup');
131 if ( false !== $blog )
133 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
134 $nowww = substr( $fields['domain'], 4 );
135 $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'] ) );
137 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
140 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
141 $blog_id = $blog->blog_id;
145 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
146 $key = md5( $fields['domain'] );
147 $blog = wp_cache_get($key, 'blog-lookup');
148 if ( false !== $blog )
150 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
151 $nowww = substr( $fields['domain'], 4 );
152 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
154 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
157 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
158 $blog_id = $blog->blog_id;
167 $blog_id = get_current_blog_id();
168 elseif ( ! is_numeric( $fields ) )
169 $blog_id = get_id_from_blogname( $fields );
174 $blog_id = (int) $blog_id;
176 $all = $get_all == true ? '' : 'short';
177 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
180 if ( ! is_object( $details ) ) {
181 if ( $details == -1 ) {
184 // Clear old pre-serialized objects. Cache clients do better with that.
185 wp_cache_delete( $blog_id . $all, 'blog-details' );
193 // Try the other cache.
195 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
197 $details = wp_cache_get( $blog_id, 'blog-details' );
198 // If short was requested and full cache is set, we can return.
200 if ( ! is_object( $details ) ) {
201 if ( $details == -1 ) {
204 // Clear old pre-serialized objects. Cache clients do better with that.
205 wp_cache_delete( $blog_id, 'blog-details' );
214 if ( empty($details) ) {
215 $details = WP_Site::get_instance( $blog_id );
217 // Set the full cache.
218 wp_cache_set( $blog_id, -1, 'blog-details' );
223 if ( ! $details instanceof WP_Site ) {
224 $details = new WP_Site( $details );
228 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
232 switch_to_blog( $blog_id );
233 $details->blogname = get_option( 'blogname' );
234 $details->siteurl = get_option( 'siteurl' );
235 $details->post_count = get_option( 'post_count' );
236 $details->home = get_option( 'home' );
237 restore_current_blog();
240 * Filters a blog's details.
243 * @deprecated 4.7.0 Use site_details
245 * @param object $details The blog details.
247 $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
249 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
251 $key = md5( $details->domain . $details->path );
252 wp_cache_set( $key, $details, 'blog-lookup' );
258 * Clear the blog details cache.
262 * @param int $blog_id Optional. Blog ID. Defaults to current blog.
264 function refresh_blog_details( $blog_id = 0 ) {
265 $blog_id = (int) $blog_id;
267 $blog_id = get_current_blog_id();
270 $details = get_site( $blog_id );
272 // Make sure clean_blog_cache() gets the blog ID
273 // when the blog has been previously cached as
275 $details = (object) array(
276 'blog_id' => $blog_id,
282 clean_blog_cache( $details );
285 * Fires after the blog details cache is cleared.
289 * @param int $blog_id Blog ID.
291 do_action( 'refresh_blog_details', $blog_id );
295 * Update the details for a blog. Updates the blogs table for a given blog id.
299 * @global wpdb $wpdb WordPress database abstraction object.
301 * @param int $blog_id Blog ID
302 * @param array $details Array of details keyed by blogs table field names.
303 * @return bool True if update succeeds, false otherwise.
305 function update_blog_details( $blog_id, $details = array() ) {
308 if ( empty($details) )
311 if ( is_object($details) )
312 $details = get_object_vars($details);
314 $current_details = get_site( $blog_id );
315 if ( empty($current_details) )
318 $current_details = get_object_vars($current_details);
320 $details = array_merge($current_details, $details);
321 $details['last_updated'] = current_time('mysql', true);
323 $update_details = array();
324 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
325 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
326 if ( 'path' === $field ) {
327 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
330 $update_details[ $field ] = $details[ $field ];
333 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
335 if ( false === $result )
338 // If spam status changed, issue actions.
339 if ( $details['spam'] != $current_details['spam'] ) {
340 if ( $details['spam'] == 1 ) {
342 * Fires when the blog status is changed to 'spam'.
346 * @param int $blog_id Blog ID.
348 do_action( 'make_spam_blog', $blog_id );
351 * Fires when the blog status is changed to 'ham'.
355 * @param int $blog_id Blog ID.
357 do_action( 'make_ham_blog', $blog_id );
361 // If mature status changed, issue actions.
362 if ( $details['mature'] != $current_details['mature'] ) {
363 if ( $details['mature'] == 1 ) {
365 * Fires when the blog status is changed to 'mature'.
369 * @param int $blog_id Blog ID.
371 do_action( 'mature_blog', $blog_id );
374 * Fires when the blog status is changed to 'unmature'.
378 * @param int $blog_id Blog ID.
380 do_action( 'unmature_blog', $blog_id );
384 // If archived status changed, issue actions.
385 if ( $details['archived'] != $current_details['archived'] ) {
386 if ( $details['archived'] == 1 ) {
388 * Fires when the blog status is changed to 'archived'.
392 * @param int $blog_id Blog ID.
394 do_action( 'archive_blog', $blog_id );
397 * Fires when the blog status is changed to 'unarchived'.
401 * @param int $blog_id Blog ID.
403 do_action( 'unarchive_blog', $blog_id );
407 // If deleted status changed, issue actions.
408 if ( $details['deleted'] != $current_details['deleted'] ) {
409 if ( $details['deleted'] == 1 ) {
411 * Fires when the blog status is changed to 'deleted'.
415 * @param int $blog_id Blog ID.
417 do_action( 'make_delete_blog', $blog_id );
420 * Fires when the blog status is changed to 'undeleted'.
424 * @param int $blog_id Blog ID.
426 do_action( 'make_undelete_blog', $blog_id );
430 if ( isset( $details['public'] ) ) {
431 switch_to_blog( $blog_id );
432 update_option( 'blog_public', $details['public'] );
433 restore_current_blog();
436 refresh_blog_details($blog_id);
442 * Clean the blog cache
446 * @param WP_Site $blog The site object to be cleared from cache.
448 function clean_blog_cache( $blog ) {
449 $blog_id = $blog->blog_id;
450 $domain_path_key = md5( $blog->domain . $blog->path );
452 wp_cache_delete( $blog_id, 'sites' );
453 wp_cache_delete( $blog_id, 'site-details' );
454 wp_cache_delete( $blog_id , 'blog-details' );
455 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
456 wp_cache_delete( $domain_path_key, 'blog-lookup' );
457 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
458 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
459 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
462 * Fires immediately after a site has been removed from the object cache.
466 * @param int $id Blog ID.
467 * @param WP_Site $blog Site object.
468 * @param string $domain_path_key md5 hash of domain and path.
470 do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
472 wp_cache_set( 'last_changed', microtime(), 'sites' );
476 * Retrieves site data given a site ID or site object.
478 * Site data will be cached and returned after being passed through a filter.
479 * If the provided site is empty, the current site global will be used.
483 * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
484 * @return WP_Site|null The site object or null if not found.
486 function get_site( $site = null ) {
487 if ( empty( $site ) ) {
488 $site = get_current_blog_id();
491 if ( $site instanceof WP_Site ) {
493 } elseif ( is_object( $site ) ) {
494 $_site = new WP_Site( $site );
496 $_site = WP_Site::get_instance( $site );
504 * Fires after a site is retrieved.
508 * @param WP_Site $_site Site data.
510 $_site = apply_filters( 'get_site', $_site );
516 * Adds any sites from the given ids to the cache that do not already exist in cache.
521 * @see update_site_cache()
522 * @global wpdb $wpdb WordPress database abstraction object.
524 * @param array $ids ID list.
526 function _prime_site_caches( $ids ) {
529 $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
530 if ( ! empty( $non_cached_ids ) ) {
531 $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
533 update_site_cache( $fresh_sites );
538 * Updates sites in cache.
542 * @param array $sites Array of site objects.
544 function update_site_cache( $sites ) {
549 foreach ( $sites as $site ) {
550 wp_cache_add( $site->blog_id, $site, 'sites' );
551 wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
556 * Retrieves a list of sites matching requested arguments.
560 * @see WP_Site_Query::parse_query()
562 * @param string|array $args {
563 * Optional. Array or query string of site query parameters. Default empty.
565 * @type array $site__in Array of site IDs to include. Default empty.
566 * @type array $site__not_in Array of site IDs to exclude. Default empty.
567 * @type bool $count Whether to return a site count (true) or array of site objects.
569 * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query.
571 * @type string $fields Site fields to return. Accepts 'ids' (returns an array of site IDs)
572 * or empty (returns an array of complete site objects). Default empty.
573 * @type int $ID A site ID to only return that site. Default empty.
574 * @type int $number Maximum number of sites to retrieve. Default 100.
575 * @type int $offset Number of sites to offset the query. Used to build LIMIT clause.
577 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
578 * @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'path',
579 * 'network_id', 'last_updated', 'registered', 'domain_length',
580 * 'path_length', 'site__in' and 'network__in'. Also accepts false,
581 * an empty array, or 'none' to disable `ORDER BY` clause.
583 * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
584 * @type int $network_id Limit results to those affiliated with a given network ID. If 0,
585 * include all networks. Default 0.
586 * @type array $network__in Array of network IDs to include affiliated sites for. Default empty.
587 * @type array $network__not_in Array of network IDs to exclude affiliated sites for. Default empty.
588 * @type string $domain Limit results to those affiliated with a given domain. Default empty.
589 * @type array $domain__in Array of domains to include affiliated sites for. Default empty.
590 * @type array $domain__not_in Array of domains to exclude affiliated sites for. Default empty.
591 * @type string $path Limit results to those affiliated with a given path. Default empty.
592 * @type array $path__in Array of paths to include affiliated sites for. Default empty.
593 * @type array $path__not_in Array of paths to exclude affiliated sites for. Default empty.
594 * @type int $public Limit results to public sites. Accepts '1' or '0'. Default empty.
595 * @type int $archived Limit results to archived sites. Accepts '1' or '0'. Default empty.
596 * @type int $mature Limit results to mature sites. Accepts '1' or '0'. Default empty.
597 * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty.
598 * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty.
599 * @type string $search Search term(s) to retrieve matching sites for. Default empty.
600 * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'.
601 * Default empty array.
602 * @type bool $update_site_cache Whether to prime the cache for found sites. Default false.
604 * @return array List of sites.
606 function get_sites( $args = array() ) {
607 $query = new WP_Site_Query();
609 return $query->query( $args );
613 * Retrieve option value for a given blog id based on name of option.
615 * If the option does not exist or does not have a value, then the return value
616 * will be false. This is useful to check whether you need to install an option
617 * and is commonly used during installation of plugin options and to test
618 * whether upgrading is required.
620 * If the option was serialized then it will be unserialized when it is returned.
624 * @param int $id A blog ID. Can be null to refer to the current blog.
625 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
626 * @param mixed $default Optional. Default value to return if the option does not exist.
627 * @return mixed Value set for the option.
629 function get_blog_option( $id, $option, $default = false ) {
633 $id = get_current_blog_id();
635 if ( get_current_blog_id() == $id )
636 return get_option( $option, $default );
638 switch_to_blog( $id );
639 $value = get_option( $option, $default );
640 restore_current_blog();
643 * Filters a blog option value.
645 * The dynamic portion of the hook name, `$option`, refers to the blog option name.
649 * @param string $value The option value.
650 * @param int $id Blog ID.
652 return apply_filters( "blog_option_{$option}", $value, $id );
656 * Add a new option for a given blog id.
658 * You do not need to serialize values. If the value needs to be serialized, then
659 * it will be serialized before it is inserted into the database. Remember,
660 * resources can not be serialized or added as an option.
662 * You can create options without values and then update the values later.
663 * Existing options will not be updated and checks are performed to ensure that you
664 * aren't adding a protected WordPress option. Care should be taken to not name
665 * options the same as the ones which are protected.
669 * @param int $id A blog ID. Can be null to refer to the current blog.
670 * @param string $option Name of option to add. Expected to not be SQL-escaped.
671 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
672 * @return bool False if option was not added and true if option was added.
674 function add_blog_option( $id, $option, $value ) {
678 $id = get_current_blog_id();
680 if ( get_current_blog_id() == $id )
681 return add_option( $option, $value );
683 switch_to_blog( $id );
684 $return = add_option( $option, $value );
685 restore_current_blog();
691 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
695 * @param int $id A blog ID. Can be null to refer to the current blog.
696 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
697 * @return bool True, if option is successfully deleted. False on failure.
699 function delete_blog_option( $id, $option ) {
703 $id = get_current_blog_id();
705 if ( get_current_blog_id() == $id )
706 return delete_option( $option );
708 switch_to_blog( $id );
709 $return = delete_option( $option );
710 restore_current_blog();
716 * Update an option for a particular blog.
720 * @param int $id The blog id.
721 * @param string $option The option key.
722 * @param mixed $value The option value.
723 * @param mixed $deprecated Not used.
724 * @return bool True on success, false on failure.
726 function update_blog_option( $id, $option, $value, $deprecated = null ) {
729 if ( null !== $deprecated )
730 _deprecated_argument( __FUNCTION__, '3.1.0' );
732 if ( get_current_blog_id() == $id )
733 return update_option( $option, $value );
735 switch_to_blog( $id );
736 $return = update_option( $option, $value );
737 restore_current_blog();
739 refresh_blog_details( $id );
745 * Switch the current blog.
747 * This function is useful if you need to pull posts, or other information,
748 * from other blogs. You can switch back afterwards using restore_current_blog().
750 * Things that aren't switched:
751 * - autoloaded options. See #14992
752 * - plugins. See #14941
754 * @see restore_current_blog()
758 * @global int $blog_id
759 * @global array $_wp_switched_stack
760 * @global bool $switched
761 * @global string $table_prefix
762 * @global WP_Object_Cache $wp_object_cache
764 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
765 * @param bool $deprecated Deprecated argument
766 * @return true Always returns True.
768 function switch_to_blog( $new_blog, $deprecated = null ) {
769 global $wpdb, $wp_roles;
771 $blog_id = get_current_blog_id();
772 if ( empty( $new_blog ) ) {
773 $new_blog = $blog_id;
776 $GLOBALS['_wp_switched_stack'][] = $blog_id;
779 * If we're switching to the same blog id that we're on,
780 * set the right vars, do the associated actions, but skip
781 * the extra unnecessary work
783 if ( $new_blog == $blog_id ) {
785 * Fires when the blog is switched.
789 * @param int $new_blog New blog ID.
790 * @param int $new_blog Blog ID.
792 do_action( 'switch_blog', $new_blog, $new_blog );
793 $GLOBALS['switched'] = true;
797 $wpdb->set_blog_id( $new_blog );
798 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
799 $prev_blog_id = $blog_id;
800 $GLOBALS['blog_id'] = $new_blog;
802 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
803 wp_cache_switch_to_blog( $new_blog );
805 global $wp_object_cache;
807 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
808 $global_groups = $wp_object_cache->global_groups;
810 $global_groups = false;
814 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
815 if ( is_array( $global_groups ) ) {
816 wp_cache_add_global_groups( $global_groups );
818 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
820 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
824 if ( did_action( 'init' ) ) {
825 $wp_roles = new WP_Roles();
826 $current_user = wp_get_current_user();
827 $current_user->for_blog( $new_blog );
830 /** This filter is documented in wp-includes/ms-blogs.php */
831 do_action( 'switch_blog', $new_blog, $prev_blog_id );
832 $GLOBALS['switched'] = true;
838 * Restore the current blog, after calling switch_to_blog()
840 * @see switch_to_blog()
844 * @global array $_wp_switched_stack
845 * @global int $blog_id
846 * @global bool $switched
847 * @global string $table_prefix
848 * @global WP_Object_Cache $wp_object_cache
850 * @return bool True on success, false if we're already on the current blog
852 function restore_current_blog() {
853 global $wpdb, $wp_roles;
855 if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
859 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
860 $blog_id = get_current_blog_id();
862 if ( $blog_id == $blog ) {
863 /** This filter is documented in wp-includes/ms-blogs.php */
864 do_action( 'switch_blog', $blog, $blog );
865 // If we still have items in the switched stack, consider ourselves still 'switched'
866 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
870 $wpdb->set_blog_id( $blog );
871 $prev_blog_id = $blog_id;
872 $GLOBALS['blog_id'] = $blog;
873 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
875 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
876 wp_cache_switch_to_blog( $blog );
878 global $wp_object_cache;
880 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
881 $global_groups = $wp_object_cache->global_groups;
883 $global_groups = false;
888 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
889 if ( is_array( $global_groups ) ) {
890 wp_cache_add_global_groups( $global_groups );
892 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
894 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
898 if ( did_action( 'init' ) ) {
899 $wp_roles = new WP_Roles();
900 $current_user = wp_get_current_user();
901 $current_user->for_blog( $blog );
904 /** This filter is documented in wp-includes/ms-blogs.php */
905 do_action( 'switch_blog', $blog, $prev_blog_id );
907 // If we still have items in the switched stack, consider ourselves still 'switched'
908 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
914 * Determines if switch_to_blog() is in effect
918 * @global array $_wp_switched_stack
920 * @return bool True if switched, false otherwise.
922 function ms_is_switched() {
923 return ! empty( $GLOBALS['_wp_switched_stack'] );
927 * Check if a particular blog is archived.
931 * @param int $id The blog id
932 * @return string Whether the blog is archived or not
934 function is_archived( $id ) {
935 return get_blog_status($id, 'archived');
939 * Update the 'archived' status of a particular blog.
943 * @param int $id The blog id
944 * @param string $archived The new status
945 * @return string $archived
947 function update_archived( $id, $archived ) {
948 update_blog_status($id, 'archived', $archived);
953 * Update a blog details field.
957 * @global wpdb $wpdb WordPress database abstraction object.
959 * @param int $blog_id BLog ID
960 * @param string $pref A field name
961 * @param string $value Value for $pref
962 * @param null $deprecated
963 * @return string|false $value
965 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
968 if ( null !== $deprecated )
969 _deprecated_argument( __FUNCTION__, '3.1.0' );
971 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
974 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
976 if ( false === $result )
979 refresh_blog_details( $blog_id );
981 if ( 'spam' == $pref ) {
983 /** This filter is documented in wp-includes/ms-blogs.php */
984 do_action( 'make_spam_blog', $blog_id );
986 /** This filter is documented in wp-includes/ms-blogs.php */
987 do_action( 'make_ham_blog', $blog_id );
989 } elseif ( 'mature' == $pref ) {
991 /** This filter is documented in wp-includes/ms-blogs.php */
992 do_action( 'mature_blog', $blog_id );
994 /** This filter is documented in wp-includes/ms-blogs.php */
995 do_action( 'unmature_blog', $blog_id );
997 } elseif ( 'archived' == $pref ) {
999 /** This filter is documented in wp-includes/ms-blogs.php */
1000 do_action( 'archive_blog', $blog_id );
1002 /** This filter is documented in wp-includes/ms-blogs.php */
1003 do_action( 'unarchive_blog', $blog_id );
1005 } elseif ( 'deleted' == $pref ) {
1006 if ( $value == 1 ) {
1007 /** This filter is documented in wp-includes/ms-blogs.php */
1008 do_action( 'make_delete_blog', $blog_id );
1010 /** This filter is documented in wp-includes/ms-blogs.php */
1011 do_action( 'make_undelete_blog', $blog_id );
1013 } elseif ( 'public' == $pref ) {
1015 * Fires after the current blog's 'public' setting is updated.
1019 * @param int $blog_id Blog ID.
1020 * @param string $value The value of blog status.
1022 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
1029 * Get a blog details field.
1033 * @global wpdb $wpdb WordPress database abstraction object.
1035 * @param int $id The blog id
1036 * @param string $pref A field name
1037 * @return bool|string|null $value
1039 function get_blog_status( $id, $pref ) {
1042 $details = get_site( $id );
1044 return $details->$pref;
1046 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
1050 * Get a list of most recently updated blogs.
1054 * @global wpdb $wpdb WordPress database abstraction object.
1056 * @param mixed $deprecated Not used
1057 * @param int $start The offset
1058 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
1059 * @return array The list of blogs
1061 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
1064 if ( ! empty( $deprecated ) )
1065 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
1067 return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
1071 * Retrieves a list of networks.
1075 * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
1076 * for information on accepted arguments. Default empty array.
1077 * @return int|array List of networks or number of found networks if `$count` argument is true.
1079 function get_networks( $args = array() ) {
1080 $query = new WP_Network_Query();
1082 return $query->query( $args );
1086 * Retrieves network data given a network ID or network object.
1088 * Network data will be cached and returned after being passed through a filter.
1089 * If the provided network is empty, the current network global will be used.
1093 * @global WP_Network $current_site
1095 * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
1096 * @return WP_Network|null The network object or null if not found.
1098 function get_network( $network = null ) {
1099 global $current_site;
1100 if ( empty( $network ) && isset( $current_site ) ) {
1101 $network = $current_site;
1104 if ( $network instanceof WP_Network ) {
1105 $_network = $network;
1106 } elseif ( is_object( $network ) ) {
1107 $_network = new WP_Network( $network );
1109 $_network = WP_Network::get_instance( $network );
1112 if ( ! $_network ) {
1117 * Fires after a network is retrieved.
1121 * @param WP_Network $_network Network data.
1123 $_network = apply_filters( 'get_network', $_network );
1129 * Removes a network from the object cache.
1133 * @param int|array $ids Network ID or an array of network IDs to remove from cache.
1135 function clean_network_cache( $ids ) {
1136 foreach ( (array) $ids as $id ) {
1137 wp_cache_delete( $id, 'networks' );
1140 * Fires immediately after a network has been removed from the object cache.
1144 * @param int $id Network ID.
1146 do_action( 'clean_network_cache', $id );
1149 wp_cache_set( 'last_changed', microtime(), 'networks' );
1153 * Updates the network cache of given networks.
1155 * Will add the networks in $networks to the cache. If network ID already exists
1156 * in the network cache then it will not be updated. The network is added to the
1157 * cache using the network group with the key using the ID of the networks.
1161 * @param array $networks Array of network row objects.
1163 function update_network_cache( $networks ) {
1164 foreach ( (array) $networks as $network ) {
1165 wp_cache_add( $network->id, $network, 'networks' );
1170 * Adds any networks from the given IDs to the cache that do not already exist in cache.
1175 * @see update_network_cache()
1176 * @global wpdb $wpdb WordPress database abstraction object.
1178 * @param array $network_ids Array of network IDs.
1180 function _prime_network_caches( $network_ids ) {
1183 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
1184 if ( !empty( $non_cached_ids ) ) {
1185 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
1187 update_network_cache( $fresh_networks );
1192 * Handler for updating the blog date when a post is published or an already published post is changed.
1196 * @param string $new_status The new post status
1197 * @param string $old_status The old post status
1198 * @param object $post Post object
1200 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
1201 $post_type_obj = get_post_type_object( $post->post_type );
1202 if ( ! $post_type_obj || ! $post_type_obj->public ) {
1206 if ( 'publish' != $new_status && 'publish' != $old_status ) {
1210 // Post was freshly published, published post was saved, or published post was unpublished.
1212 wpmu_update_blogs_date();
1216 * Handler for updating the blog date when a published post is deleted.
1220 * @param int $post_id Post ID
1222 function _update_blog_date_on_post_delete( $post_id ) {
1223 $post = get_post( $post_id );
1225 $post_type_obj = get_post_type_object( $post->post_type );
1226 if ( ! $post_type_obj || ! $post_type_obj->public ) {
1230 if ( 'publish' != $post->post_status ) {
1234 wpmu_update_blogs_date();
1238 * Handler for updating the blog posts count date when a post is deleted.
1242 * @param int $post_id Post ID.
1244 function _update_posts_count_on_delete( $post_id ) {
1245 $post = get_post( $post_id );
1247 if ( ! $post || 'publish' !== $post->post_status ) {
1251 update_posts_count();
1255 * Handler for updating the blog posts count date when a post status changes.
1259 * @param string $new_status The status the post is changing to.
1260 * @param string $old_status The status the post is changing from.
1262 function _update_posts_count_on_transition_post_status( $new_status, $old_status ) {
1263 if ( $new_status === $old_status ) {
1267 if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
1271 update_posts_count();