3 * Multisite WordPress API
6 * @subpackage Multisite
11 * Gets the network's site and user counts.
14 * @uses get_blog_count()
15 * @uses get_user_count()
17 * @return array Site and user count for the network.
19 function get_sitestats() {
21 'blogs' => get_blog_count(),
22 'users' => get_user_count(),
29 * Get the admin for a domain/path combination.
33 * @param string $sitedomain Optional. Site domain.
34 * @param string $path Optional. Site path.
35 * @return array The network admins
37 function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
41 $site_id = $wpdb->siteid;
43 $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
46 return $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id ), ARRAY_A );
52 * Get one of a user's active blogs
54 * Returns the user's primary blog, if they have one and
55 * it is active. If it's inactive, function returns another
56 * active blog of the user. If none are found, the user
57 * is added as a Subscriber to the Dashboard Blog and that blog
61 * @uses get_blogs_of_user()
62 * @uses add_user_to_blog()
63 * @uses get_blog_details()
65 * @param int $user_id The unique ID of the user
66 * @return object The blog object
68 function get_active_blog_for_user( $user_id ) {
70 $blogs = get_blogs_of_user( $user_id );
71 if ( empty( $blogs ) )
74 if ( !is_multisite() )
75 return $blogs[$wpdb->blogid];
77 $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
78 $first_blog = current($blogs);
79 if ( false !== $primary_blog ) {
80 if ( ! isset( $blogs[ $primary_blog ] ) ) {
81 update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
82 $primary = get_blog_details( $first_blog->userblog_id );
84 $primary = get_blog_details( $primary_blog );
87 //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
88 add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
89 update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
90 $primary = $first_blog;
93 if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
94 $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
96 if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
97 foreach ( (array) $blogs as $blog_id => $blog ) {
98 if ( $blog->site_id != $wpdb->siteid )
100 $details = get_blog_details( $blog_id );
101 if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
103 if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
104 update_user_meta( $user_id, 'primary_blog', $blog_id );
105 if ( !get_user_meta($user_id , 'source_domain', true) )
106 update_user_meta( $user_id, 'source_domain', $blog->domain );
120 * The number of active users in your installation.
122 * The count is cached and updated twice daily. This is not a live count.
128 function get_user_count() {
129 return get_site_option( 'user_count' );
133 * The number of active sites on your installation.
135 * The count is cached and updated twice daily. This is not a live count.
139 * @param int $network_id Deprecated, not supported.
142 function get_blog_count( $network_id = 0 ) {
143 if ( func_num_args() )
144 _deprecated_argument( __FUNCTION__, '3.1' );
146 return get_site_option( 'blog_count' );
150 * Get a blog post from any site on the network.
154 * @param int $blog_id ID of the blog.
155 * @param int $post_id ID of the post you're looking for.
156 * @return WP_Post|null WP_Post on success or null on failure
158 function get_blog_post( $blog_id, $post_id ) {
159 switch_to_blog( $blog_id );
160 $post = get_post( $post_id );
161 restore_current_blog();
167 * Add a user to a blog.
169 * Use the 'add_user_to_blog' action to fire an event when
170 * users are added to a blog.
174 * @param int $blog_id ID of the blog you're adding the user to.
175 * @param int $user_id ID of the user you're adding.
176 * @param string $role The role you want the user to have
179 function add_user_to_blog( $blog_id, $user_id, $role ) {
180 switch_to_blog($blog_id);
182 $user = get_userdata( $user_id );
185 restore_current_blog();
186 return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
189 if ( !get_user_meta($user_id, 'primary_blog', true) ) {
190 update_user_meta($user_id, 'primary_blog', $blog_id);
191 $details = get_blog_details($blog_id);
192 update_user_meta($user_id, 'source_domain', $details->domain);
195 $user->set_role($role);
198 * Fires immediately after a user is added to a site.
202 * @param int $user_id User ID.
203 * @param string $role User role.
204 * @param int $blog_id Blog ID.
206 do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
207 wp_cache_delete( $user_id, 'users' );
208 restore_current_blog();
213 * Remove a user from a blog.
215 * Use the 'remove_user_from_blog' action to fire an event when
216 * users are removed from a blog.
218 * Accepts an optional $reassign parameter, if you want to
219 * reassign the user's blog posts to another user upon removal.
223 * @param int $user_id ID of the user you're removing.
224 * @param int $blog_id ID of the blog you're removing the user from.
225 * @param string $reassign Optional. A user to whom to reassign posts.
228 function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
230 switch_to_blog($blog_id);
231 $user_id = (int) $user_id;
233 * Fires before a user is removed from a site.
237 * @param int $user_id User ID.
238 * @param int $blog_id Blog ID.
240 do_action( 'remove_user_from_blog', $user_id, $blog_id );
242 // If being removed from the primary blog, set a new primary if the user is assigned
243 // to multiple blogs.
244 $primary_blog = get_user_meta($user_id, 'primary_blog', true);
245 if ( $primary_blog == $blog_id ) {
248 $blogs = get_blogs_of_user($user_id);
249 foreach ( (array) $blogs as $blog ) {
250 if ( $blog->userblog_id == $blog_id )
252 $new_id = $blog->userblog_id;
253 $new_domain = $blog->domain;
257 update_user_meta($user_id, 'primary_blog', $new_id);
258 update_user_meta($user_id, 'source_domain', $new_domain);
261 // wp_revoke_user($user_id);
262 $user = get_userdata( $user_id );
264 restore_current_blog();
265 return new WP_Error('user_does_not_exist', __('That user does not exist.'));
268 $user->remove_all_caps();
270 $blogs = get_blogs_of_user($user_id);
271 if ( count($blogs) == 0 ) {
272 update_user_meta($user_id, 'primary_blog', '');
273 update_user_meta($user_id, 'source_domain', '');
276 if ( $reassign != '' ) {
277 $reassign = (int) $reassign;
278 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
279 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $user_id ) );
281 if ( ! empty( $post_ids ) ) {
282 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id ) );
283 array_walk( $post_ids, 'clean_post_cache' );
286 if ( ! empty( $link_ids ) ) {
287 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
288 array_walk( $link_ids, 'clean_bookmark_cache' );
292 restore_current_blog();
298 * Create an empty blog.
301 * @uses install_blog()
303 * @param string $domain The new blog's domain.
304 * @param string $path The new blog's path.
305 * @param string $weblog_title The new blog's title.
306 * @param int $site_id Optional. Defaults to 1.
307 * @return int The ID of the newly created blog
309 function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
313 // Check if the domain has been used already. We should return an error message.
314 if ( domain_exists($domain, $path, $site_id) )
315 return __( '<strong>ERROR</strong>: Site URL already taken.' );
317 // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
318 // Need to get blog_id from wp_blogs, and create new table names.
319 // Must restore table names at the end of function.
321 if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
322 return __( '<strong>ERROR</strong>: problem creating site entry.' );
324 switch_to_blog($blog_id);
325 install_blog($blog_id);
326 restore_current_blog();
332 * Get the permalink for a post on another blog.
336 * @param int $blog_id ID of the source blog.
337 * @param int $post_id ID of the desired post.
338 * @return string The post's permalink
340 function get_blog_permalink( $blog_id, $post_id ) {
341 switch_to_blog( $blog_id );
342 $link = get_permalink( $post_id );
343 restore_current_blog();
349 * Get a blog's numeric ID from its URL.
351 * On a subdirectory installation like example.com/blog1/,
352 * $domain will be the root 'example.com' and $path the
353 * subdirectory '/blog1/'. With subdomains like blog1.example.com,
354 * $domain is 'blog1.example.com' and $path is '/'.
358 * @param string $domain
359 * @param string $path Optional. Not required for subdomain installations.
360 * @return int 0 if no blog found, otherwise the ID of the matching blog
362 function get_blog_id_from_url( $domain, $path = '/' ) {
365 $domain = strtolower( $domain );
366 $path = strtolower( $path );
367 $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
369 if ( $id == -1 ) // blog does not exist
374 $id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
377 wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
381 wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
389 * Checks an email address against a list of banned domains.
391 * This function checks against the Banned Email Domains list
392 * at wp-admin/network/settings.php. The check is only run on
393 * self-registrations; user creation at wp-admin/network/users.php
394 * bypasses this check.
398 * @param string $user_email The email provided by the user at registration.
399 * @return bool Returns true when the email address is banned.
401 function is_email_address_unsafe( $user_email ) {
402 $banned_names = get_site_option( 'banned_email_domains' );
403 if ( $banned_names && ! is_array( $banned_names ) )
404 $banned_names = explode( "\n", $banned_names );
406 $is_email_address_unsafe = false;
408 if ( $banned_names && is_array( $banned_names ) ) {
409 $banned_names = array_map( 'strtolower', $banned_names );
410 $normalized_email = strtolower( $user_email );
412 list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
414 foreach ( $banned_names as $banned_domain ) {
415 if ( ! $banned_domain )
418 if ( $email_domain == $banned_domain ) {
419 $is_email_address_unsafe = true;
423 $dotted_domain = ".$banned_domain";
424 if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
425 $is_email_address_unsafe = true;
432 * Filter whether an email address is unsafe.
436 * @param bool $is_email_address_unsafe Whether the email address is "unsafe". Default false.
437 * @param string $user_email User email address.
439 return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
443 * Processes new user registrations.
445 * Checks the data provided by the user during signup. Verifies
446 * the validity and uniqueness of user names and user email addresses,
447 * and checks email addresses against admin-provided domain
448 * whitelists and blacklists.
450 * The hook 'wpmu_validate_user_signup' provides an easy way
451 * to modify the signup process. The value $result, which is passed
452 * to the hook, contains both the user-provided info and the error
453 * messages created by the function. 'wpmu_validate_user_signup' allows
454 * you to process the data in any way you'd like, and unset the
455 * relevant errors if necessary.
458 * @uses is_email_address_unsafe()
459 * @uses username_exists()
460 * @uses email_exists()
462 * @param string $user_name The login name provided by the user.
463 * @param string $user_email The email provided by the user.
464 * @return array Contains username, email, and error messages.
466 function wpmu_validate_user_signup($user_name, $user_email) {
469 $errors = new WP_Error();
471 $orig_username = $user_name;
472 $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
474 if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
475 $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
476 $user_name = $orig_username;
479 $user_email = sanitize_email( $user_email );
481 if ( empty( $user_name ) )
482 $errors->add('user_name', __( 'Please enter a username.' ) );
484 $illegal_names = get_site_option( 'illegal_names' );
485 if ( is_array( $illegal_names ) == false ) {
486 $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
487 add_site_option( 'illegal_names', $illegal_names );
489 if ( in_array( $user_name, $illegal_names ) == true )
490 $errors->add('user_name', __( 'That username is not allowed.' ) );
492 if ( is_email_address_unsafe( $user_email ) )
493 $errors->add('user_email', __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
495 if ( strlen( $user_name ) < 4 )
496 $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
498 if ( strpos( ' ' . $user_name, '_' ) != false )
499 $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character “_”!' ) );
502 if ( preg_match( '/^[0-9]*$/', $user_name ) )
503 $errors->add('user_name', __('Sorry, usernames must have letters too!'));
505 if ( !is_email( $user_email ) )
506 $errors->add('user_email', __( 'Please enter a valid email address.' ) );
508 $limited_email_domains = get_site_option( 'limited_email_domains' );
509 if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
510 $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
511 if ( in_array( $emaildomain, $limited_email_domains ) == false )
512 $errors->add('user_email', __('Sorry, that email address is not allowed!'));
515 // Check if the username has been used already.
516 if ( username_exists($user_name) )
517 $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
519 // Check if the email address has been used already.
520 if ( email_exists($user_email) )
521 $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
523 // Has someone already signed up for this username?
524 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
525 if ( $signup != null ) {
526 $registered_at = mysql2date('U', $signup->registered);
527 $now = current_time( 'timestamp', true );
528 $diff = $now - $registered_at;
529 // If registered more than two days ago, cancel registration and let this signup go through.
530 if ( $diff > 2 * DAY_IN_SECONDS )
531 $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
533 $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
536 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
537 if ( $signup != null ) {
538 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
539 // If registered more than two days ago, cancel registration and let this signup go through.
540 if ( $diff > 2 * DAY_IN_SECONDS )
541 $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
543 $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
546 $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
549 * Filter the validated user registration details.
551 * This does not allow you to override the username or email of the user during
552 * registration. The values are solely used for validation and error handling.
556 * @param array $result {
557 * The array of user name, email and the error messages.
559 * @type string $user_name Sanitized and unique username.
560 * @type string $orig_username Original username.
561 * @type string $user_email User email address.
562 * @type WP_Error $errors WP_Error object containing any errors found.
565 return apply_filters( 'wpmu_validate_user_signup', $result );
569 * Processes new site registrations.
571 * Checks the data provided by the user during blog signup. Verifies
572 * the validity and uniqueness of blog paths and domains.
574 * This function prevents the current user from registering a new site
575 * with a blogname equivalent to another user's login name. Passing the
576 * $user parameter to the function, where $user is the other user, is
577 * effectively an override of this limitation.
579 * Filter 'wpmu_validate_blog_signup' if you want to modify
580 * the way that WordPress validates new site signups.
583 * @uses domain_exists()
584 * @uses username_exists()
586 * @param string $blogname The blog name provided by the user. Must be unique.
587 * @param string $blog_title The blog title provided by the user.
588 * @return array Contains the new site data and error messages.
590 function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
591 global $wpdb, $domain;
593 $current_site = get_current_site();
594 $base = $current_site->path;
596 $blog_title = strip_tags( $blog_title );
597 $blog_title = substr( $blog_title, 0, 50 );
599 $errors = new WP_Error();
600 $illegal_names = get_site_option( 'illegal_names' );
601 if ( $illegal_names == false ) {
602 $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
603 add_site_option( 'illegal_names', $illegal_names );
607 * On sub dir installs, some names are so illegal, only a filter can
608 * spring them from jail.
610 if ( ! is_subdomain_install() ) {
611 $illegal_names = array_merge(
614 * Filter reserved site names on a sub-directory Multisite install.
618 * @param array $subdirectory_reserved_names Array of reserved names.
620 apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) )
624 if ( empty( $blogname ) )
625 $errors->add('blogname', __( 'Please enter a site name.' ) );
627 if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
628 $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
630 if ( in_array( $blogname, $illegal_names ) == true )
631 $errors->add('blogname', __( 'That name is not allowed.' ) );
633 if ( strlen( $blogname ) < 4 && !is_super_admin() )
634 $errors->add('blogname', __( 'Site name must be at least 4 characters.' ) );
636 if ( strpos( $blogname, '_' ) !== false )
637 $errors->add( 'blogname', __( 'Sorry, site names may not contain the character “_”!' ) );
639 // do not allow users to create a blog that conflicts with a page on the main blog.
640 if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
641 $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
644 if ( preg_match( '/^[0-9]*$/', $blogname ) )
645 $errors->add('blogname', __('Sorry, site names must have letters too!'));
648 * Filter the new site name during registration.
650 * The name is the site's subdomain or the site's subdirectory
651 * path depending on the network settings.
655 * @param string $blogname Site name.
657 $blogname = apply_filters( 'newblogname', $blogname );
659 $blog_title = wp_unslash( $blog_title );
661 if ( empty( $blog_title ) )
662 $errors->add('blog_title', __( 'Please enter a site title.' ) );
664 // Check if the domain/path has been used already.
665 if ( is_subdomain_install() ) {
666 $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
669 $mydomain = "$domain";
670 $path = $base.$blogname.'/';
672 if ( domain_exists($mydomain, $path, $current_site->id) )
673 $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
675 if ( username_exists( $blogname ) ) {
676 if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
677 $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
680 // Has someone already signed up for this domain?
681 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
682 if ( ! empty($signup) ) {
683 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
684 // If registered more than two days ago, cancel registration and let this signup go through.
685 if ( $diff > 2 * DAY_IN_SECONDS )
686 $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
688 $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
691 $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
694 * Filter site details and error messages following registration.
698 * @param array $result {
699 * Array of domain, path, blog name, blog title, user and error messages.
701 * @type string $domain Domain for the site.
702 * @type string $path Path for the site. Used in subdirectory installs.
703 * @type string $blogname The unique site name (slug).
704 * @type string $blog_title Blog title.
705 * @type string $user User email address.
706 * @type WP_Error $errors WP_Error containing any errors found.
709 return apply_filters( 'wpmu_validate_blog_signup', $result );
713 * Record site signup information for future activation.
716 * @uses wpmu_signup_blog_notification()
718 * @param string $domain The requested domain.
719 * @param string $path The requested path.
720 * @param string $title The requested site title.
721 * @param string $user The user's requested login name.
722 * @param string $user_email The user's email address.
723 * @param array $meta By default, contains the requested privacy setting and lang_id.
725 function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
728 $key = substr( md5( time() . rand() . $domain ), 0, 16 );
729 $meta = serialize($meta);
731 $wpdb->insert( $wpdb->signups, array(
735 'user_login' => $user,
736 'user_email' => $user_email,
737 'registered' => current_time('mysql', true),
738 'activation_key' => $key,
742 wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
746 * Record user signup information for future activation.
748 * This function is used when user registration is open but
749 * new site registration is not.
752 * @uses wpmu_signup_user_notification()
754 * @param string $user The user's requested login name.
755 * @param string $user_email The user's email address.
756 * @param array $meta By default, this is an empty array.
758 function wpmu_signup_user( $user, $user_email, $meta = array() ) {
762 $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
763 $user_email = sanitize_email( $user_email );
764 $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
765 $meta = serialize($meta);
767 $wpdb->insert( $wpdb->signups, array(
771 'user_login' => $user,
772 'user_email' => $user_email,
773 'registered' => current_time('mysql', true),
774 'activation_key' => $key,
778 wpmu_signup_user_notification($user, $user_email, $key, $meta);
782 * Notify user of signup success.
784 * This is the notification function used when site registration
787 * Filter 'wpmu_signup_blog_notification' to bypass this function or
788 * replace it with your own notification behavior.
790 * Filter 'wpmu_signup_blog_notification_email' and
791 * 'wpmu_signup_blog_notification_subject' to change the content
792 * and subject line of the email sent to newly registered users.
796 * @param string $domain The new blog domain.
797 * @param string $path The new blog path.
798 * @param string $title The site title.
799 * @param string $user The user's login name.
800 * @param string $user_email The user's email address.
801 * @param string $key The activation key created in wpmu_signup_blog()
802 * @param array $meta By default, contains the requested privacy setting and lang_id.
805 function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_email, $key, $meta = array() ) {
807 * Filter whether to bypass the new site email notification.
811 * @param string|bool $domain Site domain.
812 * @param string $path Site path.
813 * @param string $title Site title.
814 * @param string $user User login name.
815 * @param string $user_email User email address.
816 * @param string $key Activation key created in wpmu_signup_blog().
817 * @param array $meta By default, contains the requested privacy setting and lang_id.
819 if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta ) ) {
823 // Send email with activation link.
824 if ( !is_subdomain_install() || get_current_site()->id != 1 )
825 $activate_url = network_site_url("wp-activate.php?key=$key");
827 $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
829 $activate_url = esc_url($activate_url);
830 $admin_email = get_site_option( 'admin_email' );
831 if ( $admin_email == '' )
832 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
833 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
834 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
837 * Filter the message content of the new blog notification email.
839 * Content should be formatted for transmission via wp_mail().
843 * @param string $content Content of the notification email.
844 * @param string $domain Site domain.
845 * @param string $path Site path.
846 * @param string $title Site title.
847 * @param string $user User login name.
848 * @param string $user_email User email address.
849 * @param string $key Activation key created in wpmu_signup_blog().
850 * @param array $meta By default, contains the requested privacy setting and lang_id.
852 apply_filters( 'wpmu_signup_blog_notification_email',
853 __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your site here:\n\n%s" ),
854 $domain, $path, $title, $user, $user_email, $key, $meta
857 esc_url( "http://{$domain}{$path}" ),
860 // TODO: Don't hard code activation link.
863 * Filter the subject of the new blog notification email.
867 * @param string $subject Subject of the notification email.
868 * @param string $domain Site domain.
869 * @param string $path Site path.
870 * @param string $title Site title.
871 * @param string $user User login name.
872 * @param string $user_email User email address.
873 * @param string $key Activation key created in wpmu_signup_blog().
874 * @param array $meta By default, contains the requested privacy setting and lang_id.
876 apply_filters( 'wpmu_signup_blog_notification_subject',
877 __( '[%1$s] Activate %2$s' ),
878 $domain, $path, $title, $user, $user_email, $key, $meta
881 esc_url( 'http://' . $domain . $path )
883 wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
888 * Notify user of signup success.
890 * This is the notification function used when no new site has
893 * Filter 'wpmu_signup_user_notification' to bypass this function or
894 * replace it with your own notification behavior.
896 * Filter 'wpmu_signup_user_notification_email' and
897 * 'wpmu_signup_user_notification_subject' to change the content
898 * and subject line of the email sent to newly registered users.
902 * @param string $user The user's login name.
903 * @param string $user_email The user's email address.
904 * @param string $key The activation key created in wpmu_signup_user()
905 * @param array $meta By default, an empty array.
908 function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array() ) {
910 * Filter whether to bypass the email notification for new user sign-up.
914 * @param string $user User login name.
915 * @param string $user_email User email address.
916 * @param string $key Activation key created in wpmu_signup_user().
917 * @param array $meta Signup meta data.
919 if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
922 // Send email with activation link.
923 $admin_email = get_site_option( 'admin_email' );
924 if ( $admin_email == '' )
925 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
926 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
927 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
930 * Filter the content of the notification email for new user sign-up.
932 * Content should be formatted for transmission via wp_mail().
936 * @param string $content Content of the notification email.
937 * @param string $user User login name.
938 * @param string $user_email User email address.
939 * @param string $key Activation key created in wpmu_signup_user().
940 * @param array $meta Signup meta data.
942 apply_filters( 'wpmu_signup_user_notification_email',
943 __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
944 $user, $user_email, $key, $meta
946 site_url( "wp-activate.php?key=$key" )
948 // TODO: Don't hard code activation link.
951 * Filter the subject of the notification email of new user signup.
955 * @param string $subject Subject of the notification email.
956 * @param string $user User login name.
957 * @param string $user_email User email address.
958 * @param string $key Activation key created in wpmu_signup_user().
959 * @param array $meta Signup meta data.
961 apply_filters( 'wpmu_signup_user_notification_subject',
962 __( '[%1$s] Activate %2$s' ),
963 $user, $user_email, $key, $meta
968 wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
975 * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
976 * that should happen only when users or sites are self-created (since
977 * those actions are not called when users and sites are created
981 * @uses wp_generate_password()
982 * @uses wpmu_welcome_user_notification()
983 * @uses add_user_to_blog()
984 * @uses wpmu_create_user()
985 * @uses wpmu_create_blog()
986 * @uses wpmu_welcome_notification()
988 * @param string $key The activation key provided to the user.
989 * @return array An array containing information about the activated user and/or blog
991 function wpmu_activate_signup($key) {
994 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
996 if ( empty( $signup ) )
997 return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
999 if ( $signup->active ) {
1000 if ( empty( $signup->domain ) )
1001 return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
1003 return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
1006 $meta = maybe_unserialize($signup->meta);
1007 $password = wp_generate_password( 12, false );
1009 $user_id = username_exists($signup->user_login);
1012 $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
1014 $user_already_exists = true;
1017 return new WP_Error('create_user', __('Could not create user'), $signup);
1019 $now = current_time('mysql', true);
1021 if ( empty($signup->domain) ) {
1022 $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1024 if ( isset( $user_already_exists ) )
1025 return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
1027 wpmu_welcome_user_notification( $user_id, $password, $meta );
1029 * Fires immediately after a new user is activated.
1033 * @param int $user_id User ID.
1034 * @param int $password User password.
1035 * @param array $meta Signup meta data.
1037 do_action( 'wpmu_activate_user', $user_id, $password, $meta );
1038 return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
1041 $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
1043 // TODO: What to do if we create a user but cannot create a blog?
1044 if ( is_wp_error($blog_id) ) {
1045 // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
1046 // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
1047 if ( 'blog_taken' == $blog_id->get_error_code() ) {
1048 $blog_id->add_data( $signup );
1049 $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
1054 $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1055 wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
1057 * Fires immediately after a site is activated.
1061 * @param int $blog_id Blog ID.
1062 * @param int $user_id User ID.
1063 * @param int $password User password.
1064 * @param string $signup_title Site title.
1065 * @param array $meta Signup meta data.
1067 do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
1069 return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
1075 * This function runs when a user self-registers as well as when
1076 * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
1077 * that should affect all new users, but only on Multisite (otherwise
1078 * use 'user_register').
1081 * @uses wp_create_user()
1083 * @param string $user_name The new user's login name.
1084 * @param string $password The new user's password.
1085 * @param string $email The new user's email address.
1086 * @return mixed Returns false on failure, or int $user_id on success
1088 function wpmu_create_user( $user_name, $password, $email ) {
1089 $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
1091 $user_id = wp_create_user( $user_name, $password, $email );
1092 if ( is_wp_error( $user_id ) )
1095 // Newly created users have no roles or caps until they are added to a blog.
1096 delete_user_option( $user_id, 'capabilities' );
1097 delete_user_option( $user_id, 'user_level' );
1100 * Fires immediately after a new user is created.
1104 * @param int $user_id User ID.
1106 do_action( 'wpmu_new_user', $user_id );
1114 * This function runs when a user self-registers a new site as well
1115 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
1116 * for events that should affect all new sites.
1118 * On subdirectory installs, $domain is the same as the main site's
1119 * domain, and the path is the subdirectory name (eg 'example.com'
1120 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
1121 * root domain (eg 'blog1.example.com'), and $path is '/'.
1124 * @uses domain_exists()
1125 * @uses insert_blog()
1126 * @uses wp_install_defaults()
1127 * @uses add_user_to_blog()
1129 * @param string $domain The new site's domain.
1130 * @param string $path The new site's path.
1131 * @param string $title The new site's title.
1132 * @param int $user_id The user ID of the new site's admin.
1133 * @param array $meta Optional. Used to set initial site options.
1134 * @param int $site_id Optional. Only relevant on multi-network installs.
1135 * @return mixed Returns WP_Error object on failure, int $blog_id on success
1137 function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) {
1138 $defaults = array( 'public' => 0 );
1139 $meta = wp_parse_args( $meta, $defaults );
1141 $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
1143 if ( is_subdomain_install() )
1144 $domain = str_replace( '@', '', $domain );
1146 $title = strip_tags( $title );
1147 $user_id = (int) $user_id;
1152 // Check if the domain has been used already. We should return an error message.
1153 if ( domain_exists($domain, $path, $site_id) )
1154 return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
1156 if ( !defined('WP_INSTALLING') )
1157 define( 'WP_INSTALLING', true );
1159 if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
1160 return new WP_Error('insert_blog', __('Could not create site.'));
1162 switch_to_blog($blog_id);
1163 install_blog($blog_id, $title);
1164 wp_install_defaults($user_id);
1166 add_user_to_blog($blog_id, $user_id, 'administrator');
1168 foreach ( $meta as $key => $value ) {
1169 if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
1170 update_blog_status( $blog_id, $key, $value );
1172 update_option( $key, $value );
1175 add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
1176 update_option( 'blog_public', (int) $meta['public'] );
1178 if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
1179 update_user_meta( $user_id, 'primary_blog', $blog_id );
1181 restore_current_blog();
1183 * Fires immediately after a new site is created.
1187 * @param int $blog_id Blog ID.
1188 * @param int $user_id User ID.
1189 * @param string $domain Site domain.
1190 * @param string $path Site path.
1191 * @param int $site_id Site ID. Only relevant on multi-network installs.
1192 * @param array $meta Meta data. Used to set initial site options.
1194 do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
1200 * Notifies the network admin that a new site has been activated.
1202 * Filter 'newblog_notify_siteadmin' to change the content of
1203 * the notification email.
1207 * @param int $blog_id The new site's ID.
1210 function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
1211 if ( get_site_option( 'registrationnotification' ) != 'yes' )
1214 $email = get_site_option( 'admin_email' );
1215 if ( is_email($email) == false )
1218 $options_site_url = esc_url(network_admin_url('settings.php'));
1220 switch_to_blog( $blog_id );
1221 $blogname = get_option( 'blogname' );
1222 $siteurl = site_url();
1223 restore_current_blog();
1225 $msg = sprintf( __( 'New Site: %1$s
1229 Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1231 * Filter the message body of the new site activation email sent
1232 * to the network administrator.
1236 * @param string $msg Email body.
1238 $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
1240 wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
1245 * Notifies the network admin that a new user has been activated.
1247 * Filter 'newuser_notify_siteadmin' to change the content of
1248 * the notification email.
1251 * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
1253 * @param int $user_id The new user's ID.
1256 function newuser_notify_siteadmin( $user_id ) {
1257 if ( get_site_option( 'registrationnotification' ) != 'yes' )
1260 $email = get_site_option( 'admin_email' );
1262 if ( is_email($email) == false )
1265 $user = get_userdata( $user_id );
1267 $options_site_url = esc_url(network_admin_url('settings.php'));
1268 $msg = sprintf(__('New User: %1$s
1271 Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1274 * Filter the message body of the new user activation email sent
1275 * to the network administrator.
1279 * @param string $msg Email body.
1280 * @param WP_User $user WP_User instance of the new user.
1282 $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
1283 wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
1288 * Check whether a blogname is already taken.
1290 * Used during the new site registration process to ensure
1291 * that each blogname is unique.
1295 * @param string $domain The domain to be checked.
1296 * @param string $path The path to be checked.
1297 * @param int $site_id Optional. Relevant only on multi-network installs.
1300 function domain_exists($domain, $path, $site_id = 1) {
1302 $path = trailingslashit( $path );
1303 $result = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
1306 * Filter whether a blogname is taken.
1310 * @param int|null $result The blog_id if the blogname exists, null otherwise.
1311 * @param string $domain Domain to be checked.
1312 * @param string $path Path to be checked.
1313 * @param int $site_id Site ID. Relevant only on multi-network installs.
1315 return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
1319 * Store basic site info in the blogs table.
1321 * This function creates a row in the wp_blogs table and returns
1322 * the new blog's ID. It is the first step in creating a new blog.
1326 * @param string $domain The domain of the new site.
1327 * @param string $path The path of the new site.
1328 * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
1329 * @return int The ID of the new row
1331 function insert_blog($domain, $path, $site_id) {
1334 $path = trailingslashit($path);
1335 $site_id = (int) $site_id;
1337 $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
1341 $blog_id = $wpdb->insert_id;
1342 refresh_blog_details( $blog_id );
1344 wp_maybe_update_network_site_counts();
1350 * Install an empty blog.
1352 * Creates the new blog tables and options. If calling this function
1353 * directly, be sure to use switch_to_blog() first, so that $wpdb
1354 * points to the new blog.
1357 * @uses make_db_current_silent()
1358 * @uses populate_roles()
1360 * @param int $blog_id The value returned by insert_blog().
1361 * @param string $blog_title The title of the new site.
1363 function install_blog( $blog_id, $blog_title = '' ) {
1364 global $wpdb, $wp_roles;
1366 // Cast for security
1367 $blog_id = (int) $blog_id;
1369 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
1371 $suppress = $wpdb->suppress_errors();
1372 if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
1373 die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
1374 $wpdb->suppress_errors( $suppress );
1376 $url = get_blogaddress_by_id( $blog_id );
1378 // Set everything up
1379 make_db_current_silent( 'blog' );
1384 $url = untrailingslashit( $url );
1386 update_option( 'siteurl', $url );
1387 update_option( 'home', $url );
1389 if ( get_site_option( 'ms_files_rewriting' ) )
1390 update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
1392 update_option( 'upload_path', get_blog_option( get_current_site()->blog_id, 'upload_path' ) );
1394 update_option( 'blogname', wp_unslash( $blog_title ) );
1395 update_option( 'admin_email', '' );
1398 $table_prefix = $wpdb->get_blog_prefix();
1399 delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
1400 delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
1404 * Set blog defaults.
1406 * This function creates a row in the wp_blogs table.
1410 * @deprecated Use wp_install_defaults()
1411 * @uses wp_install_defaults()
1413 * @param int $blog_id Ignored in this function.
1414 * @param int $user_id
1416 function install_blog_defaults($blog_id, $user_id) {
1419 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
1421 $suppress = $wpdb->suppress_errors();
1423 wp_install_defaults($user_id);
1425 $wpdb->suppress_errors( $suppress );
1429 * Notify a user that their blog activation has been successful.
1431 * Filter 'wpmu_welcome_notification' to disable or bypass.
1433 * Filter 'update_welcome_email' and 'update_welcome_subject' to
1434 * modify the content and subject line of the notification email.
1438 * @param int $blog_id
1439 * @param int $user_id
1440 * @param string $password
1441 * @param string $title The new blog's title
1442 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1445 function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
1446 $current_site = get_current_site();
1449 * Filter whether to bypass the welcome email after site activation.
1451 * Returning false disables the welcome email.
1455 * @param int|bool $blog_id Blog ID.
1456 * @param int $user_id User ID.
1457 * @param string $password User password.
1458 * @param string $title Site title.
1459 * @param array $meta Signup meta data.
1461 if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
1464 $welcome_email = get_site_option( 'welcome_email' );
1465 if ( $welcome_email == false )
1466 $welcome_email = __( 'Dear User,
1468 Your new SITE_NAME site has been successfully set up at:
1471 You can log in to the administrator account with the following information:
1474 Log in here: BLOG_URLwp-login.php
1476 We hope you enjoy your new site. Thanks!
1478 --The Team @ SITE_NAME' );
1480 $url = get_blogaddress_by_id($blog_id);
1481 $user = get_userdata( $user_id );
1483 $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1484 $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
1485 $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
1486 $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1487 $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1490 * Filter the content of the welcome email after site activation.
1492 * Content should be formatted for transmission via wp_mail().
1496 * @param string $welcome_email Message body of the email.
1497 * @param int $blog_id Blog ID.
1498 * @param int $user_id User ID.
1499 * @param string $password User password.
1500 * @param string $title Site title.
1501 * @param array $meta Signup meta data.
1503 $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
1504 $admin_email = get_site_option( 'admin_email' );
1506 if ( $admin_email == '' )
1507 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1509 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1510 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1511 $message = $welcome_email;
1513 if ( empty( $current_site->site_name ) )
1514 $current_site->site_name = 'WordPress';
1517 * Filter the subject of the welcome email after site activation.
1521 * @param string $subject Subject of the email.
1523 $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_site->site_name, wp_unslash( $title ) ) );
1524 wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
1529 * Notify a user that their account activation has been successful.
1531 * Filter 'wpmu_welcome_user_notification' to disable or bypass.
1533 * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
1534 * modify the content and subject line of the notification email.
1538 * @param int $user_id
1539 * @param string $password
1540 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1543 function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
1544 $current_site = get_current_site();
1547 * Filter whether to bypass the welcome email after user activation.
1549 * Returning false disables the welcome email.
1553 * @param int $user_id User ID.
1554 * @param string $password User password.
1555 * @param array $meta Signup meta data.
1557 if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
1560 $welcome_email = get_site_option( 'welcome_user_email' );
1562 $user = get_userdata( $user_id );
1565 * Filter the content of the welcome email after user activation.
1567 * Content should be formatted for transmission via wp_mail().
1571 * @param type $welcome_email The message body of the account activation success email.
1572 * @param int $user_id User ID.
1573 * @param string $password User password.
1574 * @param array $meta Signup meta data.
1576 $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
1577 $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1578 $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1579 $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1580 $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
1582 $admin_email = get_site_option( 'admin_email' );
1584 if ( $admin_email == '' )
1585 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1587 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1588 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1589 $message = $welcome_email;
1591 if ( empty( $current_site->site_name ) )
1592 $current_site->site_name = 'WordPress';
1595 * Filter the subject of the welcome email after user activation.
1599 * @param string $subject Subject of the email.
1601 $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login) );
1602 wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
1607 * Get the current site info.
1609 * Returns an object containing the 'id', 'domain', 'path', and 'site_name'
1610 * properties of the site being viewed.
1612 * @see wpmu_current_site()
1618 function get_current_site() {
1619 global $current_site;
1620 return $current_site;
1624 * Get a user's most recent post.
1626 * Walks through each of a user's blogs to find the post with
1627 * the most recent post_date_gmt.
1630 * @uses get_blogs_of_user()
1632 * @param int $user_id
1633 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
1635 function get_most_recent_post_of_user( $user_id ) {
1638 $user_blogs = get_blogs_of_user( (int) $user_id );
1639 $most_recent_post = array();
1641 // Walk through each blog and get the most recent post
1642 // published by $user_id
1643 foreach ( (array) $user_blogs as $blog ) {
1644 $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
1645 $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
1647 // Make sure we found a post
1648 if ( isset($recent_post['ID']) ) {
1649 $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
1651 // If this is the first post checked or if this post is
1652 // newer than the current recent post, make it the new
1653 // most recent post.
1654 if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
1655 $most_recent_post = array(
1656 'blog_id' => $blog->userblog_id,
1657 'post_id' => $recent_post['ID'],
1658 'post_date_gmt' => $recent_post['post_date_gmt'],
1659 'post_gmt_ts' => $post_gmt_ts
1665 return $most_recent_post;
1671 * Get the size of a directory.
1673 * A helper function that is used primarily to check whether
1674 * a blog has exceeded its allowed upload space.
1677 * @uses recurse_dirsize()
1679 * @param string $directory
1682 function get_dirsize( $directory ) {
1683 $dirsize = get_transient( 'dirsize_cache' );
1684 if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
1685 return $dirsize[ $directory ][ 'size' ];
1687 if ( false == is_array( $dirsize ) )
1690 $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
1692 set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
1693 return $dirsize[ $directory ][ 'size' ];
1697 * Get the size of a directory recursively.
1699 * Used by get_dirsize() to get a directory's size when it contains
1700 * other directories.
1704 * @param string $directory
1707 function recurse_dirsize( $directory ) {
1710 $directory = untrailingslashit( $directory );
1712 if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
1715 if ($handle = opendir($directory)) {
1716 while(($file = readdir($handle)) !== false) {
1717 $path = $directory.'/'.$file;
1718 if ($file != '.' && $file != '..') {
1719 if (is_file($path)) {
1720 $size += filesize($path);
1721 } elseif (is_dir($path)) {
1722 $handlesize = recurse_dirsize($path);
1723 if ($handlesize > 0)
1724 $size += $handlesize;
1734 * Check an array of MIME types against a whitelist.
1736 * WordPress ships with a set of allowed upload filetypes,
1737 * which is defined in wp-includes/functions.php in
1738 * get_allowed_mime_types(). This function is used to filter
1739 * that list against the filetype whitelist provided by Multisite
1740 * Super Admins at wp-admin/network/settings.php.
1744 * @param array $mimes
1747 function check_upload_mimes( $mimes ) {
1748 $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
1749 foreach ( $site_exts as $ext ) {
1750 foreach ( $mimes as $ext_pattern => $mime ) {
1751 if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
1752 $site_mimes[$ext_pattern] = $mime;
1759 * Update a blog's post count.
1761 * WordPress MS stores a blog's post count as an option so as
1762 * to avoid extraneous COUNTs when a blog's details are fetched
1763 * with get_blog_details(). This function is called when posts
1764 * are published to make sure the count stays current.
1768 function update_posts_count( $deprecated = '' ) {
1770 update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
1774 * Logs user registrations.
1778 * @param int $blog_id
1779 * @param int $user_id
1781 function wpmu_log_new_registrations( $blog_id, $user_id ) {
1783 $user = get_userdata( (int) $user_id );
1785 $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
1789 * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
1793 * @see term_id_filter
1795 * @param int $term_id An ID for a term on the current blog.
1796 * @return int An ID from the global terms table mapped from $term_id.
1798 function global_terms( $term_id, $deprecated = '' ) {
1800 static $global_terms_recurse = null;
1802 if ( !global_terms_enabled() )
1805 // prevent a race condition
1806 $recurse_start = false;
1807 if ( $global_terms_recurse === null ) {
1808 $recurse_start = true;
1809 $global_terms_recurse = 1;
1810 } elseif ( 10 < $global_terms_recurse++ ) {
1814 $term_id = intval( $term_id );
1815 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
1817 $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
1818 if ( $global_id == null ) {
1819 $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
1820 if ( null == $used_global_id ) {
1821 $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1822 $global_id = $wpdb->insert_id;
1823 if ( empty( $global_id ) )
1826 $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
1827 $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
1828 $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
1829 $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1830 $global_id = $wpdb->insert_id;
1832 } elseif ( $global_id != $term_id ) {
1833 $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
1834 if ( null != $local_id )
1835 $local_id = global_terms( $local_id );
1836 if ( 10 < $global_terms_recurse )
1837 $global_id = $term_id;
1840 if ( $global_id != $term_id ) {
1841 if ( get_option( 'default_category' ) == $term_id )
1842 update_option( 'default_category', $global_id );
1844 $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
1845 $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
1846 $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
1848 clean_term_cache($term_id);
1850 if( $recurse_start )
1851 $global_terms_recurse = null;
1857 * Ensure that the current site's domain is listed in the allowed redirect host list.
1859 * @see wp_validate_redirect()
1862 * @return array The current site's domain
1864 function redirect_this_site( $deprecated = '' ) {
1865 return array( get_current_site()->domain );
1869 * Check whether an upload is too big.
1873 * @param array $upload
1874 * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
1876 function upload_is_file_too_big( $upload ) {
1877 if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
1880 if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
1881 return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
1887 * Add a nonce field to the signup page.
1890 * @uses wp_nonce_field()
1892 function signup_nonce_fields() {
1894 echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
1895 wp_nonce_field('signup_form_' . $id, '_signup_form', false);
1899 * Process the signup nonce created in signup_nonce_fields().
1902 * @uses wp_create_nonce()
1904 * @param array $result
1907 function signup_nonce_check( $result ) {
1908 if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
1911 if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
1912 wp_die( __( 'Please try again.' ) );
1918 * Correct 404 redirects when NOBLOGREDIRECT is defined.
1922 function maybe_redirect_404() {
1924 * Filter the redirect URL for 404s on the main site.
1926 * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
1930 * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
1932 if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
1933 if ( $destination == '%siteurl%' )
1934 $destination = network_home_url();
1935 wp_redirect( $destination );
1941 * Add a new user to a blog by visiting /newbloguser/username/.
1943 * This will only work when the user's details are saved as an option
1944 * keyed as 'new_user_x', where 'x' is the username of the user to be
1945 * added, as when a user is invited through the regular WP Add User interface.
1948 * @uses add_existing_user_to_blog()
1950 function maybe_add_existing_user_to_blog() {
1951 if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
1954 $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
1955 $key = array_pop( $parts );
1958 $key = array_pop( $parts );
1960 $details = get_option( 'new_user_' . $key );
1961 if ( !empty( $details ) )
1962 delete_option( 'new_user_' . $key );
1964 if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
1965 wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
1967 wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress › Success' ) );
1971 * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
1974 * @uses add_user_to_blog()
1976 * @param array $details
1978 function add_existing_user_to_blog( $details = false ) {
1981 if ( is_array( $details ) ) {
1982 $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
1984 * Fires immediately after an existing user is added to a site.
1988 * @param int $user_id User ID.
1989 * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
1991 do_action( 'added_existing_user', $details['user_id'], $result );
1997 * Add a newly created user to the appropriate blog
1999 * To add a user in general, use add_user_to_blog(). This function
2000 * is specifically hooked into the wpmu_activate_user action.
2003 * @see add_user_to_blog()
2005 * @param int $user_id
2006 * @param mixed $password Ignored.
2007 * @param array $meta
2009 function add_new_user_to_blog( $user_id, $password, $meta ) {
2010 if ( !empty( $meta[ 'add_to_blog' ] ) ) {
2011 $blog_id = $meta[ 'add_to_blog' ];
2012 $role = $meta[ 'new_role' ];
2013 remove_user_from_blog($user_id, get_current_site()->blog_id); // remove user from main blog.
2014 add_user_to_blog( $blog_id, $user_id, $role );
2015 update_user_meta( $user_id, 'primary_blog', $blog_id );
2020 * Correct From host on outgoing mail to match the site domain
2024 function fix_phpmailer_messageid( $phpmailer ) {
2025 $phpmailer->Hostname = get_current_site()->domain;
2029 * Check to see whether a user is marked as a spammer, based on user login.
2032 * @uses get_user_by()
2034 * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
2035 * or user login name as a string.
2038 function is_user_spammy( $user = null ) {
2039 if ( ! is_a( $user, 'WP_User' ) ) {
2041 $user = get_user_by( 'login', $user );
2043 $user = wp_get_current_user();
2046 return $user && isset( $user->spam ) && 1 == $user->spam;
2050 * Update this blog's 'public' setting in the global blogs table.
2052 * Public blogs have a setting of 1, private blogs are 0.
2055 * @uses update_blog_status()
2057 * @param int $old_value
2058 * @param int $value The new public value
2061 function update_blog_public( $old_value, $value ) {
2062 update_blog_status( get_current_blog_id(), 'public', (int) $value );
2064 add_action('update_option_blog_public', 'update_blog_public', 10, 2);
2067 * Check whether a usermeta key has to do with the current blog.
2070 * @uses wp_get_current_user()
2072 * @param string $key
2073 * @param int $user_id Optional. Defaults to current user.
2074 * @param int $blog_id Optional. Defaults to current blog.
2077 function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
2080 $current_user = wp_get_current_user();
2081 if ( $user_id == 0 )
2082 $user_id = $current_user->ID;
2083 if ( $blog_id == 0 )
2084 $blog_id = $wpdb->blogid;
2086 $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
2088 if ( isset( $current_user->$local_key ) )
2095 * Check whether users can self-register, based on Network settings.
2101 function users_can_register_signup_filter() {
2102 $registration = get_site_option('registration');
2103 if ( $registration == 'all' || $registration == 'user' )
2108 add_filter('option_users_can_register', 'users_can_register_signup_filter');
2111 * Ensure that the welcome message is not empty. Currently unused.
2115 * @param string $text
2118 function welcome_user_msg_filter( $text ) {
2120 remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2121 $text = __( 'Dear User,
2123 Your new account is set up.
2125 You can log in with the following information:
2132 --The Team @ SITE_NAME' );
2133 update_site_option( 'welcome_user_email', $text );
2137 add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2140 * Whether to force SSL on content.
2144 * @param string|bool $force
2145 * @return bool True if forced, false if not forced.
2147 function force_ssl_content( $force = '' ) {
2148 static $forced_content;
2150 if ( '' != $force ) {
2151 $old_forced = $forced_content;
2152 $forced_content = $force;
2156 return $forced_content;
2160 * Formats a URL to use https.
2162 * Useful as a filter.
2167 * @return string URL with https as the scheme
2169 function filter_SSL( $url ) {
2170 if ( ! is_string( $url ) )
2171 return get_bloginfo( 'url' ); // Return home blog url with proper scheme
2173 if ( force_ssl_content() && is_ssl() )
2174 $url = set_url_scheme( $url, 'https' );
2180 * Schedule update of the network-wide counts for the current network.
2184 function wp_schedule_update_network_counts() {
2185 if ( !is_main_site() )
2188 if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
2189 wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
2193 * Update the network-wide counts for the current network.
2197 function wp_update_network_counts() {
2198 wp_update_network_user_counts();
2199 wp_update_network_site_counts();
2203 * Update the count of sites for the current network.
2205 * If enabled through the 'enable_live_network_counts' filter, update the sites count
2206 * on a network when a site is created or its status is updated.
2210 * @uses wp_update_network_site_counts()
2212 function wp_maybe_update_network_site_counts() {
2213 $is_small_network = ! wp_is_large_network( 'sites' );
2216 * Filter whether to update network site or user counts when a new site is created.
2220 * @see wp_is_large_network()
2222 * @param bool $small_network Whether the network is considered small.
2223 * @param string $context Context. Either 'users' or 'sites'.
2225 if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
2228 wp_update_network_site_counts();
2232 * Update the network-wide users count.
2234 * If enabled through the 'enable_live_network_counts' filter, update the users count
2235 * on a network when a user is created or its status is updated.
2239 * @uses wp_update_network_user_counts()
2241 function wp_maybe_update_network_user_counts() {
2242 $is_small_network = ! wp_is_large_network( 'users' );
2244 /** This filter is documented in wp-includes/ms-functions.php */
2245 if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
2248 wp_update_network_user_counts();
2252 * Update the network-wide site count.
2256 function wp_update_network_site_counts() {
2259 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
2260 update_site_option( 'blog_count', $count );
2264 * Update the network-wide user count.
2268 function wp_update_network_user_counts() {
2271 $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
2272 update_site_option( 'user_count', $count );
2276 * Returns the space used by the current blog.
2280 * @return int Used space in megabytes
2282 function get_space_used() {
2284 * Filter the amount of storage space used by the current site.
2288 * @param int|bool $space_used The amount of used space, in megabytes. Default false.
2290 $space_used = apply_filters( 'pre_get_space_used', false );
2291 if ( false === $space_used ) {
2292 $upload_dir = wp_upload_dir();
2293 $space_used = get_dirsize( $upload_dir['basedir'] ) / 1024 / 1024;
2300 * Returns the upload quota for the current blog.
2304 * @return int Quota in megabytes
2306 function get_space_allowed() {
2307 $space_allowed = get_option( 'blog_upload_space' );
2309 if ( ! is_numeric( $space_allowed ) )
2310 $space_allowed = get_site_option( 'blog_upload_space' );
2312 if ( empty( $space_allowed ) || ! is_numeric( $space_allowed ) )
2313 $space_allowed = 100;
2316 * Filter the upload quota for the current site.
2320 * @param int $space_allowed Upload quota in megabytes for the current blog.
2322 return apply_filters( 'get_space_allowed', $space_allowed );
2326 * Determines if there is any upload space left in the current blog's quota.
2330 * @return int of upload space available in bytes
2332 function get_upload_space_available() {
2333 $space_allowed = get_space_allowed() * 1024 * 1024;
2334 if ( get_site_option( 'upload_space_check_disabled' ) )
2335 return $space_allowed;
2337 $space_used = get_space_used() * 1024 * 1024;
2339 if ( ( $space_allowed - $space_used ) <= 0 )
2342 return $space_allowed - $space_used;
2346 * Determines if there is any upload space left in the current blog's quota.
2349 * @return bool True if space is available, false otherwise.
2351 function is_upload_space_available() {
2352 if ( get_site_option( 'upload_space_check_disabled' ) )
2355 return (bool) get_upload_space_available();
2361 * @return int of upload size limit in bytes
2363 function upload_size_limit_filter( $size ) {
2364 $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
2365 if ( get_site_option( 'upload_space_check_disabled' ) )
2366 return min( $size, $fileupload_maxk );
2368 return min( $size, $fileupload_maxk, get_upload_space_available() );
2372 * Whether or not we have a large network.
2374 * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
2375 * Plugins can alter this criteria using the 'wp_is_large_network' filter.
2378 * @param string $using 'sites or 'users'. Default is 'sites'.
2379 * @return bool True if the network meets the criteria for large. False otherwise.
2381 function wp_is_large_network( $using = 'sites' ) {
2382 if ( 'users' == $using ) {
2383 $count = get_user_count();
2385 * Filter whether the network is considered large.
2389 * @param bool $is_large_network Whether the network has more than 10000 users or sites.
2390 * @param string $component The component to count. Accepts 'users', or 'sites'.
2391 * @param int $count The count of items for the component.
2393 return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
2396 $count = get_blog_count();
2397 /** This filter is documented in wp-includes/ms-functions.php */
2398 return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
2403 * Return an array of sites for a network or networks.
2407 * @param array $args {
2408 * Array of default arguments. Optional.
2410 * @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
2411 * from all networks. Defaults to current network ID.
2412 * @type int $public Retrieve public or non-public sites. Default null, for any.
2413 * @type int $archived Retrieve archived or non-archived sites. Default null, for any.
2414 * @type int $mature Retrieve mature or non-mature sites. Default null, for any.
2415 * @type int $spam Retrieve spam or non-spam sites. Default null, for any.
2416 * @type int $deleted Retrieve deleted or non-deleted sites. Default null, for any.
2417 * @type int $limit Number of sites to limit the query to. Default 100.
2418 * @type int $offset Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
2420 * @return array An empty array if the install is considered "large" via wp_is_large_network(). Otherwise,
2421 * an associative array of site data arrays, each containing the site (network) ID, blog ID,
2422 * site domain and path, dates registered and modified, and the language ID. Also, boolean
2423 * values for whether the site is public, archived, mature, spam, and/or deleted.
2425 function wp_get_sites( $args = array() ) {
2428 if ( wp_is_large_network() )
2432 'network_id' => $wpdb->siteid,
2442 $args = wp_parse_args( $args, $defaults );
2444 $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
2446 if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
2447 $network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
2448 $query .= "AND site_id IN ($network_ids) ";
2451 if ( isset( $args['public'] ) )
2452 $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
2454 if ( isset( $args['archived'] ) )
2455 $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
2457 if ( isset( $args['mature'] ) )
2458 $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
2460 if ( isset( $args['spam'] ) )
2461 $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
2463 if ( isset( $args['deleted'] ) )
2464 $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
2466 if ( isset( $args['limit'] ) && $args['limit'] ) {
2467 if ( isset( $args['offset'] ) && $args['offset'] )
2468 $query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
2470 $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
2473 $site_results = $wpdb->get_results( $query, ARRAY_A );
2475 return $site_results;