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_blog_details( (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 * Given a blog's (subdomain or directory) slug, retrieve its id.
79 * @global wpdb $wpdb WordPress database abstraction object.
82 * @return int A blog id
84 function get_id_from_blogname( $slug ) {
87 $current_site = get_current_site();
88 $slug = trim( $slug, '/' );
90 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
94 if ( is_subdomain_install() ) {
95 $domain = $slug . '.' . $current_site->domain;
96 $path = $current_site->path;
98 $domain = $current_site->domain;
99 $path = $current_site->path . $slug . '/';
102 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
103 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
108 * Retrieve the details for a blog from the blogs table and blog options.
112 * @global wpdb $wpdb WordPress database abstraction object.
114 * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
115 * If not specified the current blog ID is used.
116 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table.
118 * @return WP_Site|false Blog details on success. False on failure.
120 function get_blog_details( $fields = null, $get_all = true ) {
123 if ( is_array($fields ) ) {
124 if ( isset($fields['blog_id']) ) {
125 $blog_id = $fields['blog_id'];
126 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
127 $key = md5( $fields['domain'] . $fields['path'] );
128 $blog = wp_cache_get($key, 'blog-lookup');
129 if ( false !== $blog )
131 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
132 $nowww = substr( $fields['domain'], 4 );
133 $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'] ) );
135 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
138 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
139 $blog_id = $blog->blog_id;
143 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
144 $key = md5( $fields['domain'] );
145 $blog = wp_cache_get($key, 'blog-lookup');
146 if ( false !== $blog )
148 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
149 $nowww = substr( $fields['domain'], 4 );
150 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
152 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
155 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
156 $blog_id = $blog->blog_id;
165 $blog_id = get_current_blog_id();
166 elseif ( ! is_numeric( $fields ) )
167 $blog_id = get_id_from_blogname( $fields );
172 $blog_id = (int) $blog_id;
174 $all = $get_all == true ? '' : 'short';
175 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
178 if ( ! is_object( $details ) ) {
179 if ( $details == -1 ) {
182 // Clear old pre-serialized objects. Cache clients do better with that.
183 wp_cache_delete( $blog_id . $all, 'blog-details' );
191 // Try the other cache.
193 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
195 $details = wp_cache_get( $blog_id, 'blog-details' );
196 // If short was requested and full cache is set, we can return.
198 if ( ! is_object( $details ) ) {
199 if ( $details == -1 ) {
202 // Clear old pre-serialized objects. Cache clients do better with that.
203 wp_cache_delete( $blog_id, 'blog-details' );
212 if ( empty($details) ) {
213 $details = WP_Site::get_instance( $blog_id );
215 // Set the full cache.
216 wp_cache_set( $blog_id, -1, 'blog-details' );
221 if ( ! $details instanceof WP_Site ) {
222 $details = new WP_Site( $details );
226 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
230 switch_to_blog( $blog_id );
231 $details->blogname = get_option( 'blogname' );
232 $details->siteurl = get_option( 'siteurl' );
233 $details->post_count = get_option( 'post_count' );
234 $details->home = get_option( 'home' );
235 restore_current_blog();
238 * Filters a blog's details.
242 * @param object $details The blog details.
244 $details = apply_filters( 'blog_details', $details );
246 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
248 $key = md5( $details->domain . $details->path );
249 wp_cache_set( $key, $details, 'blog-lookup' );
255 * Clear the blog details cache.
259 * @param int $blog_id Optional. Blog ID. Defaults to current blog.
261 function refresh_blog_details( $blog_id = 0 ) {
262 $blog_id = (int) $blog_id;
264 $blog_id = get_current_blog_id();
267 $details = get_blog_details( $blog_id, false );
269 // Make sure clean_blog_cache() gets the blog ID
270 // when the blog has been previously cached as
272 $details = (object) array(
273 'blog_id' => $blog_id,
279 clean_blog_cache( $details );
282 * Fires after the blog details cache is cleared.
286 * @param int $blog_id Blog ID.
288 do_action( 'refresh_blog_details', $blog_id );
292 * Update the details for a blog. Updates the blogs table for a given blog id.
296 * @global wpdb $wpdb WordPress database abstraction object.
298 * @param int $blog_id Blog ID
299 * @param array $details Array of details keyed by blogs table field names.
300 * @return bool True if update succeeds, false otherwise.
302 function update_blog_details( $blog_id, $details = array() ) {
305 if ( empty($details) )
308 if ( is_object($details) )
309 $details = get_object_vars($details);
311 $current_details = get_blog_details($blog_id, false);
312 if ( empty($current_details) )
315 $current_details = get_object_vars($current_details);
317 $details = array_merge($current_details, $details);
318 $details['last_updated'] = current_time('mysql', true);
320 $update_details = array();
321 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
322 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
323 if ( 'path' === $field ) {
324 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
327 $update_details[ $field ] = $details[ $field ];
330 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
332 if ( false === $result )
335 // If spam status changed, issue actions.
336 if ( $details['spam'] != $current_details['spam'] ) {
337 if ( $details['spam'] == 1 ) {
339 * Fires when the blog status is changed to 'spam'.
343 * @param int $blog_id Blog ID.
345 do_action( 'make_spam_blog', $blog_id );
348 * Fires when the blog status is changed to 'ham'.
352 * @param int $blog_id Blog ID.
354 do_action( 'make_ham_blog', $blog_id );
358 // If mature status changed, issue actions.
359 if ( $details['mature'] != $current_details['mature'] ) {
360 if ( $details['mature'] == 1 ) {
362 * Fires when the blog status is changed to 'mature'.
366 * @param int $blog_id Blog ID.
368 do_action( 'mature_blog', $blog_id );
371 * Fires when the blog status is changed to 'unmature'.
375 * @param int $blog_id Blog ID.
377 do_action( 'unmature_blog', $blog_id );
381 // If archived status changed, issue actions.
382 if ( $details['archived'] != $current_details['archived'] ) {
383 if ( $details['archived'] == 1 ) {
385 * Fires when the blog status is changed to 'archived'.
389 * @param int $blog_id Blog ID.
391 do_action( 'archive_blog', $blog_id );
394 * Fires when the blog status is changed to 'unarchived'.
398 * @param int $blog_id Blog ID.
400 do_action( 'unarchive_blog', $blog_id );
404 // If deleted status changed, issue actions.
405 if ( $details['deleted'] != $current_details['deleted'] ) {
406 if ( $details['deleted'] == 1 ) {
408 * Fires when the blog status is changed to 'deleted'.
412 * @param int $blog_id Blog ID.
414 do_action( 'make_delete_blog', $blog_id );
417 * Fires when the blog status is changed to 'undeleted'.
421 * @param int $blog_id Blog ID.
423 do_action( 'make_undelete_blog', $blog_id );
427 if ( isset( $details['public'] ) ) {
428 switch_to_blog( $blog_id );
429 update_option( 'blog_public', $details['public'] );
430 restore_current_blog();
433 refresh_blog_details($blog_id);
439 * Clean the blog cache
443 * @param WP_Site $blog The blog details as returned from get_blog_details()
445 function clean_blog_cache( $blog ) {
446 $blog_id = $blog->blog_id;
447 $domain_path_key = md5( $blog->domain . $blog->path );
449 wp_cache_delete( $blog_id, 'sites' );
450 wp_cache_delete( $blog_id, 'site-details' );
451 wp_cache_delete( $blog_id , 'blog-details' );
452 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
453 wp_cache_delete( $domain_path_key, 'blog-lookup' );
454 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
455 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
456 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
457 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
460 * Fires immediately after a site has been removed from the object cache.
464 * @param int $id Blog ID.
465 * @param WP_Site $blog Site object.
466 * @param string $domain_path_key md5 hash of domain and path.
468 do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
470 wp_cache_set( 'last_changed', microtime(), 'sites' );
474 * Retrieves site data given a site ID or site object.
476 * Site data will be cached and returned after being passed through a filter.
477 * If the provided site is empty, the current site global will be used.
481 * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
482 * @return WP_Site|null The site object or null if not found.
484 function get_site( $site = null ) {
485 if ( empty( $site ) ) {
486 $site = get_current_blog_id();
489 if ( $site instanceof WP_Site ) {
491 } elseif ( is_object( $site ) ) {
492 $_site = new WP_Site( $site );
494 $_site = WP_Site::get_instance( $site );
502 * Fires after a site is retrieved.
506 * @param WP_Site $_site Site data.
508 $_site = apply_filters( 'get_site', $_site );
514 * Adds any sites from the given ids to the cache that do not already exist in cache.
519 * @see update_site_cache()
520 * @global wpdb $wpdb WordPress database abstraction object.
522 * @param array $ids ID list.
524 function _prime_site_caches( $ids ) {
527 $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
528 if ( ! empty( $non_cached_ids ) ) {
529 $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
531 update_site_cache( $fresh_sites );
536 * Updates sites in cache.
540 * @param array $sites Array of site objects.
542 function update_site_cache( $sites ) {
547 foreach ( $sites as $site ) {
548 wp_cache_add( $site->blog_id, $site, 'sites' );
549 wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
554 * Retrieves a list of sites matching requested arguments.
558 * @see WP_Site_Query::parse_query()
560 * @param string|array $args {
561 * Optional. Array or query string of site query parameters. Default empty.
563 * @type array $site__in Array of site IDs to include. Default empty.
564 * @type array $site__not_in Array of site IDs to exclude. Default empty.
565 * @type bool $count Whether to return a site count (true) or array of site objects.
567 * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query.
569 * @type string $fields Site fields to return. Accepts 'ids' for site IDs only or empty
570 * for all fields. Default empty.
571 * @type int $ID A site ID to only return that site. Default empty.
572 * @type int $number Maximum number of sites to retrieve. Default null (no limit).
573 * @type int $offset Number of sites to offset the query. Used to build LIMIT clause.
575 * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
576 * @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'path',
577 * 'network_id', 'last_updated', 'registered', 'domain_length',
578 * 'path_length', 'site__in' and 'network__in'. Also accepts false,
579 * an empty array, or 'none' to disable `ORDER BY` clause.
581 * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
582 * @type int $network_id Limit results to those affiliated with a given network ID.
583 * Default current network ID.
584 * @type array $network__in Array of network IDs to include affiliated sites for. Default empty.
585 * @type array $network__not_in Array of network IDs to exclude affiliated sites for. Default empty.
586 * @type string $domain Limit results to those affiliated with a given domain.
588 * @type array $domain__in Array of domains to include affiliated sites for. Default empty.
589 * @type array $domain__not_in Array of domains to exclude affiliated sites for. Default empty.
590 * @type string $path Limit results to those affiliated with a given path.
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 bool $update_site_cache Whether to prime the cache for found sites. Default false.
602 * @return array List of sites.
604 function get_sites( $args = array() ) {
605 $query = new WP_Site_Query();
607 return $query->query( $args );
611 * Retrieve option value for a given blog id based on name of option.
613 * If the option does not exist or does not have a value, then the return value
614 * will be false. This is useful to check whether you need to install an option
615 * and is commonly used during installation of plugin options and to test
616 * whether upgrading is required.
618 * If the option was serialized then it will be unserialized when it is returned.
622 * @param int $id A blog ID. Can be null to refer to the current blog.
623 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
624 * @param mixed $default Optional. Default value to return if the option does not exist.
625 * @return mixed Value set for the option.
627 function get_blog_option( $id, $option, $default = false ) {
631 $id = get_current_blog_id();
633 if ( get_current_blog_id() == $id )
634 return get_option( $option, $default );
636 switch_to_blog( $id );
637 $value = get_option( $option, $default );
638 restore_current_blog();
641 * Filters a blog option value.
643 * The dynamic portion of the hook name, `$option`, refers to the blog option name.
647 * @param string $value The option value.
648 * @param int $id Blog ID.
650 return apply_filters( "blog_option_{$option}", $value, $id );
654 * Add a new option for a given blog id.
656 * You do not need to serialize values. If the value needs to be serialized, then
657 * it will be serialized before it is inserted into the database. Remember,
658 * resources can not be serialized or added as an option.
660 * You can create options without values and then update the values later.
661 * Existing options will not be updated and checks are performed to ensure that you
662 * aren't adding a protected WordPress option. Care should be taken to not name
663 * options the same as the ones which are protected.
667 * @param int $id A blog ID. Can be null to refer to the current blog.
668 * @param string $option Name of option to add. Expected to not be SQL-escaped.
669 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
670 * @return bool False if option was not added and true if option was added.
672 function add_blog_option( $id, $option, $value ) {
676 $id = get_current_blog_id();
678 if ( get_current_blog_id() == $id )
679 return add_option( $option, $value );
681 switch_to_blog( $id );
682 $return = add_option( $option, $value );
683 restore_current_blog();
689 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
693 * @param int $id A blog ID. Can be null to refer to the current blog.
694 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
695 * @return bool True, if option is successfully deleted. False on failure.
697 function delete_blog_option( $id, $option ) {
701 $id = get_current_blog_id();
703 if ( get_current_blog_id() == $id )
704 return delete_option( $option );
706 switch_to_blog( $id );
707 $return = delete_option( $option );
708 restore_current_blog();
714 * Update an option for a particular blog.
718 * @param int $id The blog id.
719 * @param string $option The option key.
720 * @param mixed $value The option value.
721 * @param mixed $deprecated Not used.
722 * @return bool True on success, false on failure.
724 function update_blog_option( $id, $option, $value, $deprecated = null ) {
727 if ( null !== $deprecated )
728 _deprecated_argument( __FUNCTION__, '3.1.0' );
730 if ( get_current_blog_id() == $id )
731 return update_option( $option, $value );
733 switch_to_blog( $id );
734 $return = update_option( $option, $value );
735 restore_current_blog();
737 refresh_blog_details( $id );
743 * Switch the current blog.
745 * This function is useful if you need to pull posts, or other information,
746 * from other blogs. You can switch back afterwards using restore_current_blog().
748 * Things that aren't switched:
749 * - autoloaded options. See #14992
750 * - plugins. See #14941
752 * @see restore_current_blog()
756 * @global int $blog_id
757 * @global array $_wp_switched_stack
758 * @global bool $switched
759 * @global string $table_prefix
760 * @global WP_Object_Cache $wp_object_cache
762 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
763 * @param bool $deprecated Deprecated argument
764 * @return true Always returns True.
766 function switch_to_blog( $new_blog, $deprecated = null ) {
769 if ( empty( $new_blog ) )
770 $new_blog = $GLOBALS['blog_id'];
772 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
775 * If we're switching to the same blog id that we're on,
776 * set the right vars, do the associated actions, but skip
777 * the extra unnecessary work
779 if ( $new_blog == $GLOBALS['blog_id'] ) {
781 * Fires when the blog is switched.
785 * @param int $new_blog New blog ID.
786 * @param int $new_blog Blog ID.
788 do_action( 'switch_blog', $new_blog, $new_blog );
789 $GLOBALS['switched'] = true;
793 $wpdb->set_blog_id( $new_blog );
794 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
795 $prev_blog_id = $GLOBALS['blog_id'];
796 $GLOBALS['blog_id'] = $new_blog;
798 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
799 wp_cache_switch_to_blog( $new_blog );
801 global $wp_object_cache;
803 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
804 $global_groups = $wp_object_cache->global_groups;
806 $global_groups = false;
810 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
811 if ( is_array( $global_groups ) ) {
812 wp_cache_add_global_groups( $global_groups );
814 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' ) );
816 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
820 if ( did_action( 'init' ) ) {
821 wp_roles()->reinit();
822 $current_user = wp_get_current_user();
823 $current_user->for_blog( $new_blog );
826 /** This filter is documented in wp-includes/ms-blogs.php */
827 do_action( 'switch_blog', $new_blog, $prev_blog_id );
828 $GLOBALS['switched'] = true;
834 * Restore the current blog, after calling switch_to_blog()
836 * @see switch_to_blog()
840 * @global array $_wp_switched_stack
841 * @global int $blog_id
842 * @global bool $switched
843 * @global string $table_prefix
844 * @global WP_Object_Cache $wp_object_cache
846 * @return bool True on success, false if we're already on the current blog
848 function restore_current_blog() {
851 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
854 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
856 if ( $GLOBALS['blog_id'] == $blog ) {
857 /** This filter is documented in wp-includes/ms-blogs.php */
858 do_action( 'switch_blog', $blog, $blog );
859 // If we still have items in the switched stack, consider ourselves still 'switched'
860 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
864 $wpdb->set_blog_id( $blog );
865 $prev_blog_id = $GLOBALS['blog_id'];
866 $GLOBALS['blog_id'] = $blog;
867 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
869 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
870 wp_cache_switch_to_blog( $blog );
872 global $wp_object_cache;
874 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
875 $global_groups = $wp_object_cache->global_groups;
877 $global_groups = false;
881 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
882 if ( is_array( $global_groups ) ) {
883 wp_cache_add_global_groups( $global_groups );
885 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' ) );
887 wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
891 if ( did_action( 'init' ) ) {
892 wp_roles()->reinit();
893 $current_user = wp_get_current_user();
894 $current_user->for_blog( $blog );
897 /** This filter is documented in wp-includes/ms-blogs.php */
898 do_action( 'switch_blog', $blog, $prev_blog_id );
900 // If we still have items in the switched stack, consider ourselves still 'switched'
901 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
907 * Determines if switch_to_blog() is in effect
911 * @global array $_wp_switched_stack
913 * @return bool True if switched, false otherwise.
915 function ms_is_switched() {
916 return ! empty( $GLOBALS['_wp_switched_stack'] );
920 * Check if a particular blog is archived.
924 * @param int $id The blog id
925 * @return string Whether the blog is archived or not
927 function is_archived( $id ) {
928 return get_blog_status($id, 'archived');
932 * Update the 'archived' status of a particular blog.
936 * @param int $id The blog id
937 * @param string $archived The new status
938 * @return string $archived
940 function update_archived( $id, $archived ) {
941 update_blog_status($id, 'archived', $archived);
946 * Update a blog details field.
950 * @global wpdb $wpdb WordPress database abstraction object.
952 * @param int $blog_id BLog ID
953 * @param string $pref A field name
954 * @param string $value Value for $pref
955 * @param null $deprecated
956 * @return string|false $value
958 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
961 if ( null !== $deprecated )
962 _deprecated_argument( __FUNCTION__, '3.1.0' );
964 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
967 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
969 if ( false === $result )
972 refresh_blog_details( $blog_id );
974 if ( 'spam' == $pref ) {
976 /** This filter is documented in wp-includes/ms-blogs.php */
977 do_action( 'make_spam_blog', $blog_id );
979 /** This filter is documented in wp-includes/ms-blogs.php */
980 do_action( 'make_ham_blog', $blog_id );
982 } elseif ( 'mature' == $pref ) {
984 /** This filter is documented in wp-includes/ms-blogs.php */
985 do_action( 'mature_blog', $blog_id );
987 /** This filter is documented in wp-includes/ms-blogs.php */
988 do_action( 'unmature_blog', $blog_id );
990 } elseif ( 'archived' == $pref ) {
992 /** This filter is documented in wp-includes/ms-blogs.php */
993 do_action( 'archive_blog', $blog_id );
995 /** This filter is documented in wp-includes/ms-blogs.php */
996 do_action( 'unarchive_blog', $blog_id );
998 } elseif ( 'deleted' == $pref ) {
1000 /** This filter is documented in wp-includes/ms-blogs.php */
1001 do_action( 'make_delete_blog', $blog_id );
1003 /** This filter is documented in wp-includes/ms-blogs.php */
1004 do_action( 'make_undelete_blog', $blog_id );
1006 } elseif ( 'public' == $pref ) {
1008 * Fires after the current blog's 'public' setting is updated.
1012 * @param int $blog_id Blog ID.
1013 * @param string $value The value of blog status.
1015 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
1022 * Get a blog details field.
1026 * @global wpdb $wpdb WordPress database abstraction object.
1028 * @param int $id The blog id
1029 * @param string $pref A field name
1030 * @return bool|string|null $value
1032 function get_blog_status( $id, $pref ) {
1035 $details = get_blog_details( $id, false );
1037 return $details->$pref;
1039 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
1043 * Get a list of most recently updated blogs.
1047 * @global wpdb $wpdb WordPress database abstraction object.
1049 * @param mixed $deprecated Not used
1050 * @param int $start The offset
1051 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
1052 * @return array The list of blogs
1054 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
1057 if ( ! empty( $deprecated ) )
1058 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
1060 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 );
1064 * Retrieves a list of networks.
1068 * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
1069 * for information on accepted arguments. Default empty array.
1070 * @return int|array List of networks or number of found networks if `$count` argument is true.
1072 function get_networks( $args = array() ) {
1073 $query = new WP_Network_Query();
1075 return $query->query( $args );
1079 * Retrieves network data given a network ID or network object.
1081 * Network data will be cached and returned after being passed through a filter.
1082 * If the provided network is empty, the current network global will be used.
1086 * @global WP_Network $current_site
1088 * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
1089 * @return WP_Network|null The network object or null if not found.
1091 function get_network( $network = null ) {
1092 global $current_site;
1093 if ( empty( $network ) && isset( $current_site ) ) {
1094 $network = $current_site;
1097 if ( $network instanceof WP_Network ) {
1098 $_network = $network;
1099 } elseif ( is_object( $network ) ) {
1100 $_network = new WP_Network( $network );
1102 $_network = WP_Network::get_instance( $network );
1105 if ( ! $_network ) {
1110 * Fires after a network is retrieved.
1114 * @param WP_Network $_network Network data.
1116 $_network = apply_filters( 'get_network', $_network );
1122 * Removes a network from the object cache.
1126 * @param int|array $ids Network ID or an array of network IDs to remove from cache.
1128 function clean_network_cache( $ids ) {
1129 foreach ( (array) $ids as $id ) {
1130 wp_cache_delete( $id, 'networks' );
1133 * Fires immediately after a network has been removed from the object cache.
1137 * @param int $id Network ID.
1139 do_action( 'clean_network_cache', $id );
1142 wp_cache_set( 'last_changed', microtime(), 'networks' );
1146 * Updates the network cache of given networks.
1148 * Will add the networks in $networks to the cache. If network ID already exists
1149 * in the network cache then it will not be updated. The network is added to the
1150 * cache using the network group with the key using the ID of the networks.
1154 * @param array $networks Array of network row objects.
1156 function update_network_cache( $networks ) {
1157 foreach ( (array) $networks as $network ) {
1158 wp_cache_add( $network->id, $network, 'networks' );
1163 * Adds any networks from the given IDs to the cache that do not already exist in cache.
1168 * @see update_network_cache()
1169 * @global wpdb $wpdb WordPress database abstraction object.
1171 * @param array $network_ids Array of network IDs.
1173 function _prime_network_caches( $network_ids ) {
1176 $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
1177 if ( !empty( $non_cached_ids ) ) {
1178 $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
1180 update_network_cache( $fresh_networks );
1185 * Handler for updating the blog date when a post is published or an already published post is changed.
1189 * @param string $new_status The new post status
1190 * @param string $old_status The old post status
1191 * @param object $post Post object
1193 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
1194 $post_type_obj = get_post_type_object( $post->post_type );
1195 if ( ! $post_type_obj || ! $post_type_obj->public ) {
1199 if ( 'publish' != $new_status && 'publish' != $old_status ) {
1203 // Post was freshly published, published post was saved, or published post was unpublished.
1205 wpmu_update_blogs_date();
1209 * Handler for updating the blog date when a published post is deleted.
1213 * @param int $post_id Post ID
1215 function _update_blog_date_on_post_delete( $post_id ) {
1216 $post = get_post( $post_id );
1218 $post_type_obj = get_post_type_object( $post->post_type );
1219 if ( ! $post_type_obj || ! $post_type_obj->public ) {
1223 if ( 'publish' != $post->post_status ) {
1227 wpmu_update_blogs_date();
1231 * Handler for updating the blog posts count date when a post is deleted.
1235 * @param int $post_id Post ID.
1237 function _update_posts_count_on_delete( $post_id ) {
1238 $post = get_post( $post_id );
1240 if ( ! $post || 'publish' !== $post->post_status ) {
1244 update_posts_count();
1248 * Handler for updating the blog posts count date when a post status changes.
1252 * @param string $new_status The status the post is changing to.
1253 * @param string $old_status The status the post is changing from.
1255 function _update_posts_count_on_transition_post_status( $new_status, $old_status ) {
1256 if ( $new_status === $old_status ) {
1260 if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
1264 update_posts_count();