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 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
279 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
282 restore_current_blog();
288 * Create an empty blog.
291 * @uses install_blog()
293 * @param string $domain The new blog's domain.
294 * @param string $path The new blog's path.
295 * @param string $weblog_title The new blog's title.
296 * @param int $site_id Optional. Defaults to 1.
297 * @return int The ID of the newly created blog
299 function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
303 // Check if the domain has been used already. We should return an error message.
304 if ( domain_exists($domain, $path, $site_id) )
305 return __( '<strong>ERROR</strong>: Site URL already taken.' );
307 // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
308 // Need to get blog_id from wp_blogs, and create new table names.
309 // Must restore table names at the end of function.
311 if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
312 return __( '<strong>ERROR</strong>: problem creating site entry.' );
314 switch_to_blog($blog_id);
315 install_blog($blog_id);
316 restore_current_blog();
322 * Get the permalink for a post on another blog.
326 * @param int $blog_id ID of the source blog.
327 * @param int $post_id ID of the desired post.
328 * @return string The post's permalink
330 function get_blog_permalink( $blog_id, $post_id ) {
331 switch_to_blog( $blog_id );
332 $link = get_permalink( $post_id );
333 restore_current_blog();
339 * Get a blog's numeric ID from its URL.
341 * On a subdirectory installation like example.com/blog1/,
342 * $domain will be the root 'example.com' and $path the
343 * subdirectory '/blog1/'. With subdomains like blog1.example.com,
344 * $domain is 'blog1.example.com' and $path is '/'.
348 * @param string $domain
349 * @param string $path Optional. Not required for subdomain installations.
350 * @return int 0 if no blog found, otherwise the ID of the matching blog
352 function get_blog_id_from_url( $domain, $path = '/' ) {
355 $domain = strtolower( $domain );
356 $path = strtolower( $path );
357 $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
359 if ( $id == -1 ) // blog does not exist
364 $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 ) );
367 wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
371 wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
379 * Checks an email address against a list of banned domains.
381 * This function checks against the Banned Email Domains list
382 * at wp-admin/network/settings.php. The check is only run on
383 * self-registrations; user creation at wp-admin/network/users.php
384 * bypasses this check.
388 * @param string $user_email The email provided by the user at registration.
389 * @return bool Returns true when the email address is banned.
391 function is_email_address_unsafe( $user_email ) {
392 $banned_names = get_site_option( 'banned_email_domains' );
393 if ( $banned_names && ! is_array( $banned_names ) )
394 $banned_names = explode( "\n", $banned_names );
396 $is_email_address_unsafe = false;
398 if ( $banned_names && is_array( $banned_names ) ) {
399 $banned_names = array_map( 'strtolower', $banned_names );
400 $normalized_email = strtolower( $user_email );
402 list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
404 foreach ( $banned_names as $banned_domain ) {
405 if ( ! $banned_domain )
408 if ( $email_domain == $banned_domain ) {
409 $is_email_address_unsafe = true;
413 $dotted_domain = ".$banned_domain";
414 if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
415 $is_email_address_unsafe = true;
422 * Filter whether an email address is unsafe.
426 * @param bool $is_email_address_unsafe Whether the email address is "unsafe". Default false.
427 * @param string $user_email User email address.
429 return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
433 * Processes new user registrations.
435 * Checks the data provided by the user during signup. Verifies
436 * the validity and uniqueness of user names and user email addresses,
437 * and checks email addresses against admin-provided domain
438 * whitelists and blacklists.
440 * The hook 'wpmu_validate_user_signup' provides an easy way
441 * to modify the signup process. The value $result, which is passed
442 * to the hook, contains both the user-provided info and the error
443 * messages created by the function. 'wpmu_validate_user_signup' allows
444 * you to process the data in any way you'd like, and unset the
445 * relevant errors if necessary.
448 * @uses is_email_address_unsafe()
449 * @uses username_exists()
450 * @uses email_exists()
452 * @param string $user_name The login name provided by the user.
453 * @param string $user_email The email provided by the user.
454 * @return array Contains username, email, and error messages.
456 function wpmu_validate_user_signup($user_name, $user_email) {
459 $errors = new WP_Error();
461 $orig_username = $user_name;
462 $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
464 if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
465 $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
466 $user_name = $orig_username;
469 $user_email = sanitize_email( $user_email );
471 if ( empty( $user_name ) )
472 $errors->add('user_name', __( 'Please enter a username.' ) );
474 $illegal_names = get_site_option( 'illegal_names' );
475 if ( is_array( $illegal_names ) == false ) {
476 $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
477 add_site_option( 'illegal_names', $illegal_names );
479 if ( in_array( $user_name, $illegal_names ) == true )
480 $errors->add('user_name', __( 'That username is not allowed.' ) );
482 if ( is_email_address_unsafe( $user_email ) )
483 $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.'));
485 if ( strlen( $user_name ) < 4 )
486 $errors->add('user_name', __( 'Username must be at least 4 characters.' ) );
488 if ( strpos( ' ' . $user_name, '_' ) != false )
489 $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character “_”!' ) );
492 if ( preg_match( '/^[0-9]*$/', $user_name ) )
493 $errors->add('user_name', __('Sorry, usernames must have letters too!'));
495 if ( !is_email( $user_email ) )
496 $errors->add('user_email', __( 'Please enter a valid email address.' ) );
498 $limited_email_domains = get_site_option( 'limited_email_domains' );
499 if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
500 $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
501 if ( in_array( $emaildomain, $limited_email_domains ) == false )
502 $errors->add('user_email', __('Sorry, that email address is not allowed!'));
505 // Check if the username has been used already.
506 if ( username_exists($user_name) )
507 $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
509 // Check if the email address has been used already.
510 if ( email_exists($user_email) )
511 $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
513 // Has someone already signed up for this username?
514 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
515 if ( $signup != null ) {
516 $registered_at = mysql2date('U', $signup->registered);
517 $now = current_time( 'timestamp', true );
518 $diff = $now - $registered_at;
519 // If registered more than two days ago, cancel registration and let this signup go through.
520 if ( $diff > 2 * DAY_IN_SECONDS )
521 $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
523 $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
526 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
527 if ( $signup != null ) {
528 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
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_email' => $user_email ) );
533 $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.'));
536 $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
539 * Filter the validated user registration details.
541 * This does not allow you to override the username or email of the user during
542 * registration. The values are solely used for validation and error handling.
546 * @param array $result {
547 * The array of user name, email and the error messages.
549 * @type string $user_name Sanitized and unique username.
550 * @type string $orig_username Original username.
551 * @type string $user_email User email address.
552 * @type WP_Error $errors WP_Error object containing any errors found.
555 return apply_filters( 'wpmu_validate_user_signup', $result );
559 * Processes new site registrations.
561 * Checks the data provided by the user during blog signup. Verifies
562 * the validity and uniqueness of blog paths and domains.
564 * This function prevents the current user from registering a new site
565 * with a blogname equivalent to another user's login name. Passing the
566 * $user parameter to the function, where $user is the other user, is
567 * effectively an override of this limitation.
569 * Filter 'wpmu_validate_blog_signup' if you want to modify
570 * the way that WordPress validates new site signups.
573 * @uses domain_exists()
574 * @uses username_exists()
576 * @param string $blogname The blog name provided by the user. Must be unique.
577 * @param string $blog_title The blog title provided by the user.
578 * @return array Contains the new site data and error messages.
580 function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
581 global $wpdb, $domain;
583 $current_site = get_current_site();
584 $base = $current_site->path;
586 $blog_title = strip_tags( $blog_title );
587 $blog_title = substr( $blog_title, 0, 50 );
589 $errors = new WP_Error();
590 $illegal_names = get_site_option( 'illegal_names' );
591 if ( $illegal_names == false ) {
592 $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
593 add_site_option( 'illegal_names', $illegal_names );
597 * On sub dir installs, some names are so illegal, only a filter can
598 * spring them from jail.
600 if ( ! is_subdomain_install() ) {
601 $illegal_names = array_merge(
604 * Filter reserved site names on a sub-directory Multisite install.
608 * @param array $subdirectory_reserved_names Array of reserved names.
610 apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) )
614 if ( empty( $blogname ) )
615 $errors->add('blogname', __( 'Please enter a site name.' ) );
617 if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
618 $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
620 if ( in_array( $blogname, $illegal_names ) == true )
621 $errors->add('blogname', __( 'That name is not allowed.' ) );
623 if ( strlen( $blogname ) < 4 && !is_super_admin() )
624 $errors->add('blogname', __( 'Site name must be at least 4 characters.' ) );
626 if ( strpos( $blogname, '_' ) !== false )
627 $errors->add( 'blogname', __( 'Sorry, site names may not contain the character “_”!' ) );
629 // do not allow users to create a blog that conflicts with a page on the main blog.
630 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 ) ) )
631 $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
634 if ( preg_match( '/^[0-9]*$/', $blogname ) )
635 $errors->add('blogname', __('Sorry, site names must have letters too!'));
638 * Filter the new site name during registration.
640 * The name is the site's subdomain or the site's subdirectory
641 * path depending on the network settings.
645 * @param string $blogname Site name.
647 $blogname = apply_filters( 'newblogname', $blogname );
649 $blog_title = wp_unslash( $blog_title );
651 if ( empty( $blog_title ) )
652 $errors->add('blog_title', __( 'Please enter a site title.' ) );
654 // Check if the domain/path has been used already.
655 if ( is_subdomain_install() ) {
656 $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
659 $mydomain = "$domain";
660 $path = $base.$blogname.'/';
662 if ( domain_exists($mydomain, $path, $current_site->id) )
663 $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
665 if ( username_exists( $blogname ) ) {
666 if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
667 $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
670 // Has someone already signed up for this domain?
671 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
672 if ( ! empty($signup) ) {
673 $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
674 // If registered more than two days ago, cancel registration and let this signup go through.
675 if ( $diff > 2 * DAY_IN_SECONDS )
676 $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
678 $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
681 $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
684 * Filter site details and error messages following registration.
688 * @param array $result {
689 * Array of domain, path, blog name, blog title, user and error messages.
691 * @type string $domain Domain for the site.
692 * @type string $path Path for the site. Used in subdirectory installs.
693 * @type string $blogname The unique site name (slug).
694 * @type string $blog_title Blog title.
695 * @type string $user User email address.
696 * @type WP_Error $errors WP_Error containing any errors found.
699 return apply_filters( 'wpmu_validate_blog_signup', $result );
703 * Record site signup information for future activation.
706 * @uses wpmu_signup_blog_notification()
708 * @param string $domain The requested domain.
709 * @param string $path The requested path.
710 * @param string $title The requested site title.
711 * @param string $user The user's requested login name.
712 * @param string $user_email The user's email address.
713 * @param array $meta By default, contains the requested privacy setting and lang_id.
715 function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
718 $key = substr( md5( time() . rand() . $domain ), 0, 16 );
719 $meta = serialize($meta);
721 $wpdb->insert( $wpdb->signups, array(
725 'user_login' => $user,
726 'user_email' => $user_email,
727 'registered' => current_time('mysql', true),
728 'activation_key' => $key,
732 wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
736 * Record user signup information for future activation.
738 * This function is used when user registration is open but
739 * new site registration is not.
742 * @uses wpmu_signup_user_notification()
744 * @param string $user The user's requested login name.
745 * @param string $user_email The user's email address.
746 * @param array $meta By default, this is an empty array.
748 function wpmu_signup_user( $user, $user_email, $meta = array() ) {
752 $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
753 $user_email = sanitize_email( $user_email );
754 $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
755 $meta = serialize($meta);
757 $wpdb->insert( $wpdb->signups, array(
761 'user_login' => $user,
762 'user_email' => $user_email,
763 'registered' => current_time('mysql', true),
764 'activation_key' => $key,
768 wpmu_signup_user_notification($user, $user_email, $key, $meta);
772 * Notify user of signup success.
774 * This is the notification function used when site registration
777 * Filter 'wpmu_signup_blog_notification' to bypass this function or
778 * replace it with your own notification behavior.
780 * Filter 'wpmu_signup_blog_notification_email' and
781 * 'wpmu_signup_blog_notification_subject' to change the content
782 * and subject line of the email sent to newly registered users.
786 * @param string $domain The new blog domain.
787 * @param string $path The new blog path.
788 * @param string $title The site title.
789 * @param string $user The user's login name.
790 * @param string $user_email The user's email address.
791 * @param string $key The activation key created in wpmu_signup_blog()
792 * @param array $meta By default, contains the requested privacy setting and lang_id.
795 function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_email, $key, $meta = array() ) {
797 * Filter whether to bypass the new site email notification.
801 * @param string|bool $domain Site domain.
802 * @param string $path Site path.
803 * @param string $title Site title.
804 * @param string $user User login name.
805 * @param string $user_email User email address.
806 * @param string $key Activation key created in wpmu_signup_blog().
807 * @param array $meta By default, contains the requested privacy setting and lang_id.
809 if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta ) ) {
813 // Send email with activation link.
814 if ( !is_subdomain_install() || get_current_site()->id != 1 )
815 $activate_url = network_site_url("wp-activate.php?key=$key");
817 $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
819 $activate_url = esc_url($activate_url);
820 $admin_email = get_site_option( 'admin_email' );
821 if ( $admin_email == '' )
822 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
823 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
824 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
827 * Filter the message content of the new blog notification email.
829 * Content should be formatted for transmission via wp_mail().
833 * @param string $content Content of the notification email.
834 * @param string $domain Site domain.
835 * @param string $path Site path.
836 * @param string $title Site title.
837 * @param string $user User login name.
838 * @param string $user_email User email address.
839 * @param string $key Activation key created in wpmu_signup_blog().
840 * @param array $meta By default, contains the requested privacy setting and lang_id.
842 apply_filters( 'wpmu_signup_blog_notification_email',
843 __( "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" ),
844 $domain, $path, $title, $user, $user_email, $key, $meta
847 esc_url( "http://{$domain}{$path}" ),
850 // TODO: Don't hard code activation link.
853 * Filter the subject of the new blog notification email.
857 * @param string $subject Subject of the notification email.
858 * @param string $domain Site domain.
859 * @param string $path Site path.
860 * @param string $title Site title.
861 * @param string $user User login name.
862 * @param string $user_email User email address.
863 * @param string $key Activation key created in wpmu_signup_blog().
864 * @param array $meta By default, contains the requested privacy setting and lang_id.
866 apply_filters( 'wpmu_signup_blog_notification_subject',
867 __( '[%1$s] Activate %2$s' ),
868 $domain, $path, $title, $user, $user_email, $key, $meta
871 esc_url( 'http://' . $domain . $path )
873 wp_mail($user_email, $subject, $message, $message_headers);
878 * Notify user of signup success.
880 * This is the notification function used when no new site has
883 * Filter 'wpmu_signup_user_notification' to bypass this function or
884 * replace it with your own notification behavior.
886 * Filter 'wpmu_signup_user_notification_email' and
887 * 'wpmu_signup_user_notification_subject' to change the content
888 * and subject line of the email sent to newly registered users.
892 * @param string $user The user's login name.
893 * @param string $user_email The user's email address.
894 * @param string $key The activation key created in wpmu_signup_user()
895 * @param array $meta By default, an empty array.
898 function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array() ) {
900 * Filter whether to bypass the email notification for new user sign-up.
904 * @param string $user User login name.
905 * @param string $user_email User email address.
906 * @param string $key Activation key created in wpmu_signup_user().
907 * @param array $meta Signup meta data.
909 if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
912 // Send email with activation link.
913 $admin_email = get_site_option( 'admin_email' );
914 if ( $admin_email == '' )
915 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
916 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
917 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
920 * Filter the content of the notification email for new user sign-up.
922 * Content should be formatted for transmission via wp_mail().
926 * @param string $content Content of the notification email.
927 * @param string $user User login name.
928 * @param string $user_email User email address.
929 * @param string $key Activation key created in wpmu_signup_user().
930 * @param array $meta Signup meta data.
932 apply_filters( 'wpmu_signup_user_notification_email',
933 __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
934 $user, $user_email, $key, $meta
936 site_url( "wp-activate.php?key=$key" )
938 // TODO: Don't hard code activation link.
941 * Filter the subject of the notification email of new user signup.
945 * @param string $subject Subject of the notification email.
946 * @param string $user User login name.
947 * @param string $user_email User email address.
948 * @param string $key Activation key created in wpmu_signup_user().
949 * @param array $meta Signup meta data.
951 apply_filters( 'wpmu_signup_user_notification_subject',
952 __( '[%1$s] Activate %2$s' ),
953 $user, $user_email, $key, $meta
958 wp_mail($user_email, $subject, $message, $message_headers);
965 * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
966 * that should happen only when users or sites are self-created (since
967 * those actions are not called when users and sites are created
971 * @uses wp_generate_password()
972 * @uses wpmu_welcome_user_notification()
973 * @uses add_user_to_blog()
974 * @uses wpmu_create_user()
975 * @uses wpmu_create_blog()
976 * @uses wpmu_welcome_notification()
978 * @param string $key The activation key provided to the user.
979 * @return array An array containing information about the activated user and/or blog
981 function wpmu_activate_signup($key) {
984 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
986 if ( empty( $signup ) )
987 return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
989 if ( $signup->active ) {
990 if ( empty( $signup->domain ) )
991 return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
993 return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
996 $meta = maybe_unserialize($signup->meta);
997 $password = wp_generate_password( 12, false );
999 $user_id = username_exists($signup->user_login);
1002 $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
1004 $user_already_exists = true;
1007 return new WP_Error('create_user', __('Could not create user'), $signup);
1009 $now = current_time('mysql', true);
1011 if ( empty($signup->domain) ) {
1012 $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1014 if ( isset( $user_already_exists ) )
1015 return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
1017 wpmu_welcome_user_notification( $user_id, $password, $meta );
1019 * Fires immediately after a new user is activated.
1023 * @param int $user_id User ID.
1024 * @param int $password User password.
1025 * @param array $meta Signup meta data.
1027 do_action( 'wpmu_activate_user', $user_id, $password, $meta );
1028 return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
1031 $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
1033 // TODO: What to do if we create a user but cannot create a blog?
1034 if ( is_wp_error($blog_id) ) {
1035 // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
1036 // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
1037 if ( 'blog_taken' == $blog_id->get_error_code() ) {
1038 $blog_id->add_data( $signup );
1039 $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
1044 $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1045 wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
1047 * Fires immediately after a site is activated.
1051 * @param int $blog_id Blog ID.
1052 * @param int $user_id User ID.
1053 * @param int $password User password.
1054 * @param string $signup_title Site title.
1055 * @param array $meta Signup meta data.
1057 do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
1059 return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
1065 * This function runs when a user self-registers as well as when
1066 * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
1067 * that should affect all new users, but only on Multisite (otherwise
1068 * use 'user_register').
1071 * @uses wp_create_user()
1073 * @param string $user_name The new user's login name.
1074 * @param string $password The new user's password.
1075 * @param string $email The new user's email address.
1076 * @return mixed Returns false on failure, or int $user_id on success
1078 function wpmu_create_user( $user_name, $password, $email ) {
1079 $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
1081 $user_id = wp_create_user( $user_name, $password, $email );
1082 if ( is_wp_error( $user_id ) )
1085 // Newly created users have no roles or caps until they are added to a blog.
1086 delete_user_option( $user_id, 'capabilities' );
1087 delete_user_option( $user_id, 'user_level' );
1090 * Fires immediately after a new user is created.
1094 * @param int $user_id User ID.
1096 do_action( 'wpmu_new_user', $user_id );
1104 * This function runs when a user self-registers a new site as well
1105 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
1106 * for events that should affect all new sites.
1108 * On subdirectory installs, $domain is the same as the main site's
1109 * domain, and the path is the subdirectory name (eg 'example.com'
1110 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
1111 * root domain (eg 'blog1.example.com'), and $path is '/'.
1114 * @uses domain_exists()
1115 * @uses insert_blog()
1116 * @uses wp_install_defaults()
1117 * @uses add_user_to_blog()
1119 * @param string $domain The new site's domain.
1120 * @param string $path The new site's path.
1121 * @param string $title The new site's title.
1122 * @param int $user_id The user ID of the new site's admin.
1123 * @param array $meta Optional. Used to set initial site options.
1124 * @param int $site_id Optional. Only relevant on multi-network installs.
1125 * @return mixed Returns WP_Error object on failure, int $blog_id on success
1127 function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) {
1128 $defaults = array( 'public' => 0 );
1129 $meta = wp_parse_args( $meta, $defaults );
1131 $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
1133 if ( is_subdomain_install() )
1134 $domain = str_replace( '@', '', $domain );
1136 $title = strip_tags( $title );
1137 $user_id = (int) $user_id;
1142 // Check if the domain has been used already. We should return an error message.
1143 if ( domain_exists($domain, $path, $site_id) )
1144 return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
1146 if ( !defined('WP_INSTALLING') )
1147 define( 'WP_INSTALLING', true );
1149 if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
1150 return new WP_Error('insert_blog', __('Could not create site.'));
1152 switch_to_blog($blog_id);
1153 install_blog($blog_id, $title);
1154 wp_install_defaults($user_id);
1156 add_user_to_blog($blog_id, $user_id, 'administrator');
1158 foreach ( $meta as $key => $value ) {
1159 if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
1160 update_blog_status( $blog_id, $key, $value );
1162 update_option( $key, $value );
1165 add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
1166 update_option( 'blog_public', (int) $meta['public'] );
1168 if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
1169 update_user_meta( $user_id, 'primary_blog', $blog_id );
1171 restore_current_blog();
1173 * Fires immediately after a new site is created.
1177 * @param int $blog_id Blog ID.
1178 * @param int $user_id User ID.
1179 * @param string $domain Site domain.
1180 * @param string $path Site path.
1181 * @param int $site_id Site ID. Only relevant on multi-network installs.
1182 * @param array $meta Meta data. Used to set initial site options.
1184 do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
1190 * Notifies the network admin that a new site has been activated.
1192 * Filter 'newblog_notify_siteadmin' to change the content of
1193 * the notification email.
1197 * @param int $blog_id The new site's ID.
1200 function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
1201 if ( get_site_option( 'registrationnotification' ) != 'yes' )
1204 $email = get_site_option( 'admin_email' );
1205 if ( is_email($email) == false )
1208 $options_site_url = esc_url(network_admin_url('settings.php'));
1210 switch_to_blog( $blog_id );
1211 $blogname = get_option( 'blogname' );
1212 $siteurl = site_url();
1213 restore_current_blog();
1215 $msg = sprintf( __( 'New Site: %1$s
1219 Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1221 * Filter the message body of the new site activation email sent
1222 * to the network administrator.
1226 * @param string $msg Email body.
1228 $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
1230 wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
1235 * Notifies the network admin that a new user has been activated.
1237 * Filter 'newuser_notify_siteadmin' to change the content of
1238 * the notification email.
1241 * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
1243 * @param int $user_id The new user's ID.
1246 function newuser_notify_siteadmin( $user_id ) {
1247 if ( get_site_option( 'registrationnotification' ) != 'yes' )
1250 $email = get_site_option( 'admin_email' );
1252 if ( is_email($email) == false )
1255 $user = get_userdata( $user_id );
1257 $options_site_url = esc_url(network_admin_url('settings.php'));
1258 $msg = sprintf(__('New User: %1$s
1261 Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1264 * Filter the message body of the new user activation email sent
1265 * to the network administrator.
1269 * @param string $msg Email body.
1270 * @param WP_User $user WP_User instance of the new user.
1272 $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
1273 wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
1278 * Check whether a blogname is already taken.
1280 * Used during the new site registration process to ensure
1281 * that each blogname is unique.
1285 * @param string $domain The domain to be checked.
1286 * @param string $path The path to be checked.
1287 * @param int $site_id Optional. Relevant only on multi-network installs.
1290 function domain_exists($domain, $path, $site_id = 1) {
1292 $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) );
1294 * Filter whether a blogname is taken.
1298 * @param int|null $result The blog_id if the blogname exists, null otherwise.
1299 * @param string $domain Domain to be checked.
1300 * @param string $path Path to be checked.
1301 * @param int $site_id Site ID. Relevant only on multi-network installs.
1303 return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
1307 * Store basic site info in the blogs table.
1309 * This function creates a row in the wp_blogs table and returns
1310 * the new blog's ID. It is the first step in creating a new blog.
1314 * @param string $domain The domain of the new site.
1315 * @param string $path The path of the new site.
1316 * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
1317 * @return int The ID of the new row
1319 function insert_blog($domain, $path, $site_id) {
1322 $path = trailingslashit($path);
1323 $site_id = (int) $site_id;
1325 $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
1329 $blog_id = $wpdb->insert_id;
1330 refresh_blog_details( $blog_id );
1332 wp_maybe_update_network_site_counts();
1338 * Install an empty blog.
1340 * Creates the new blog tables and options. If calling this function
1341 * directly, be sure to use switch_to_blog() first, so that $wpdb
1342 * points to the new blog.
1345 * @uses make_db_current_silent()
1346 * @uses populate_roles()
1348 * @param int $blog_id The value returned by insert_blog().
1349 * @param string $blog_title The title of the new site.
1351 function install_blog( $blog_id, $blog_title = '' ) {
1352 global $wpdb, $wp_roles;
1354 // Cast for security
1355 $blog_id = (int) $blog_id;
1357 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
1359 $suppress = $wpdb->suppress_errors();
1360 if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
1361 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>' );
1362 $wpdb->suppress_errors( $suppress );
1364 $url = get_blogaddress_by_id( $blog_id );
1366 // Set everything up
1367 make_db_current_silent( 'blog' );
1372 $url = untrailingslashit( $url );
1374 update_option( 'siteurl', $url );
1375 update_option( 'home', $url );
1377 if ( get_site_option( 'ms_files_rewriting' ) )
1378 update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
1380 update_option( 'upload_path', get_blog_option( get_current_site()->blog_id, 'upload_path' ) );
1382 update_option( 'blogname', wp_unslash( $blog_title ) );
1383 update_option( 'admin_email', '' );
1386 $table_prefix = $wpdb->get_blog_prefix();
1387 delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // delete all
1388 delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
1392 * Set blog defaults.
1394 * This function creates a row in the wp_blogs table.
1398 * @deprecated Use wp_install_defaults()
1399 * @uses wp_install_defaults()
1401 * @param int $blog_id Ignored in this function.
1402 * @param int $user_id
1404 function install_blog_defaults($blog_id, $user_id) {
1407 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
1409 $suppress = $wpdb->suppress_errors();
1411 wp_install_defaults($user_id);
1413 $wpdb->suppress_errors( $suppress );
1417 * Notify a user that their blog activation has been successful.
1419 * Filter 'wpmu_welcome_notification' to disable or bypass.
1421 * Filter 'update_welcome_email' and 'update_welcome_subject' to
1422 * modify the content and subject line of the notification email.
1426 * @param int $blog_id
1427 * @param int $user_id
1428 * @param string $password
1429 * @param string $title The new blog's title
1430 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1433 function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
1434 $current_site = get_current_site();
1437 * Filter whether to bypass the welcome email after site activation.
1439 * Returning false disables the welcome email.
1443 * @param int|bool $blog_id Blog ID.
1444 * @param int $user_id User ID.
1445 * @param string $password User password.
1446 * @param string $title Site title.
1447 * @param array $meta Signup meta data.
1449 if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
1452 $welcome_email = get_site_option( 'welcome_email' );
1453 if ( $welcome_email == false )
1454 $welcome_email = __( 'Dear User,
1456 Your new SITE_NAME site has been successfully set up at:
1459 You can log in to the administrator account with the following information:
1462 Log in here: BLOG_URLwp-login.php
1464 We hope you enjoy your new site. Thanks!
1466 --The Team @ SITE_NAME' );
1468 $url = get_blogaddress_by_id($blog_id);
1469 $user = get_userdata( $user_id );
1471 $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1472 $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
1473 $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
1474 $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1475 $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1478 * Filter the content of the welcome email after site activation.
1480 * Content should be formatted for transmission via wp_mail().
1484 * @param string $welcome_email Message body of the email.
1485 * @param int $blog_id Blog ID.
1486 * @param int $user_id User ID.
1487 * @param string $password User password.
1488 * @param string $title Site title.
1489 * @param array $meta Signup meta data.
1491 $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
1492 $admin_email = get_site_option( 'admin_email' );
1494 if ( $admin_email == '' )
1495 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1497 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1498 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1499 $message = $welcome_email;
1501 if ( empty( $current_site->site_name ) )
1502 $current_site->site_name = 'WordPress';
1505 * Filter the subject of the welcome email after site activation.
1509 * @param string $subject Subject of the email.
1511 $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_site->site_name, wp_unslash( $title ) ) );
1512 wp_mail($user->user_email, $subject, $message, $message_headers);
1517 * Notify a user that their account activation has been successful.
1519 * Filter 'wpmu_welcome_user_notification' to disable or bypass.
1521 * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
1522 * modify the content and subject line of the notification email.
1526 * @param int $user_id
1527 * @param string $password
1528 * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1531 function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
1532 $current_site = get_current_site();
1535 * Filter whether to bypass the welcome email after user activation.
1537 * Returning false disables the welcome email.
1541 * @param int $user_id User ID.
1542 * @param string $password User password.
1543 * @param array $meta Signup meta data.
1545 if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
1548 $welcome_email = get_site_option( 'welcome_user_email' );
1550 $user = get_userdata( $user_id );
1553 * Filter the content of the welcome email after user activation.
1555 * Content should be formatted for transmission via wp_mail().
1559 * @param type $welcome_email The message body of the account activation success email.
1560 * @param int $user_id User ID.
1561 * @param string $password User password.
1562 * @param array $meta Signup meta data.
1564 $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
1565 $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1566 $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1567 $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1568 $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
1570 $admin_email = get_site_option( 'admin_email' );
1572 if ( $admin_email == '' )
1573 $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1575 $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1576 $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1577 $message = $welcome_email;
1579 if ( empty( $current_site->site_name ) )
1580 $current_site->site_name = 'WordPress';
1583 * Filter the subject of the welcome email after user activation.
1587 * @param string $subject Subject of the email.
1589 $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login) );
1590 wp_mail($user->user_email, $subject, $message, $message_headers);
1595 * Get the current site info.
1597 * Returns an object containing the 'id', 'domain', 'path', and 'site_name'
1598 * properties of the site being viewed.
1600 * @see wpmu_current_site()
1606 function get_current_site() {
1607 global $current_site;
1608 return $current_site;
1612 * Get a user's most recent post.
1614 * Walks through each of a user's blogs to find the post with
1615 * the most recent post_date_gmt.
1618 * @uses get_blogs_of_user()
1620 * @param int $user_id
1621 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
1623 function get_most_recent_post_of_user( $user_id ) {
1626 $user_blogs = get_blogs_of_user( (int) $user_id );
1627 $most_recent_post = array();
1629 // Walk through each blog and get the most recent post
1630 // published by $user_id
1631 foreach ( (array) $user_blogs as $blog ) {
1632 $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
1633 $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);
1635 // Make sure we found a post
1636 if ( isset($recent_post['ID']) ) {
1637 $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
1639 // If this is the first post checked or if this post is
1640 // newer than the current recent post, make it the new
1641 // most recent post.
1642 if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
1643 $most_recent_post = array(
1644 'blog_id' => $blog->userblog_id,
1645 'post_id' => $recent_post['ID'],
1646 'post_date_gmt' => $recent_post['post_date_gmt'],
1647 'post_gmt_ts' => $post_gmt_ts
1653 return $most_recent_post;
1659 * Get the size of a directory.
1661 * A helper function that is used primarily to check whether
1662 * a blog has exceeded its allowed upload space.
1665 * @uses recurse_dirsize()
1667 * @param string $directory
1670 function get_dirsize( $directory ) {
1671 $dirsize = get_transient( 'dirsize_cache' );
1672 if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
1673 return $dirsize[ $directory ][ 'size' ];
1675 if ( false == is_array( $dirsize ) )
1678 $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
1680 set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
1681 return $dirsize[ $directory ][ 'size' ];
1685 * Get the size of a directory recursively.
1687 * Used by get_dirsize() to get a directory's size when it contains
1688 * other directories.
1692 * @param string $directory
1695 function recurse_dirsize( $directory ) {
1698 $directory = untrailingslashit( $directory );
1700 if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
1703 if ($handle = opendir($directory)) {
1704 while(($file = readdir($handle)) !== false) {
1705 $path = $directory.'/'.$file;
1706 if ($file != '.' && $file != '..') {
1707 if (is_file($path)) {
1708 $size += filesize($path);
1709 } elseif (is_dir($path)) {
1710 $handlesize = recurse_dirsize($path);
1711 if ($handlesize > 0)
1712 $size += $handlesize;
1722 * Check an array of MIME types against a whitelist.
1724 * WordPress ships with a set of allowed upload filetypes,
1725 * which is defined in wp-includes/functions.php in
1726 * get_allowed_mime_types(). This function is used to filter
1727 * that list against the filetype whitelist provided by Multisite
1728 * Super Admins at wp-admin/network/settings.php.
1732 * @param array $mimes
1735 function check_upload_mimes( $mimes ) {
1736 $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
1737 foreach ( $site_exts as $ext ) {
1738 foreach ( $mimes as $ext_pattern => $mime ) {
1739 if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
1740 $site_mimes[$ext_pattern] = $mime;
1747 * Update a blog's post count.
1749 * WordPress MS stores a blog's post count as an option so as
1750 * to avoid extraneous COUNTs when a blog's details are fetched
1751 * with get_blog_details(). This function is called when posts
1752 * are published to make sure the count stays current.
1756 function update_posts_count( $deprecated = '' ) {
1758 update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
1762 * Logs user registrations.
1766 * @param int $blog_id
1767 * @param int $user_id
1769 function wpmu_log_new_registrations( $blog_id, $user_id ) {
1771 $user = get_userdata( (int) $user_id );
1773 $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')) );
1777 * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
1781 * @see term_id_filter
1783 * @param int $term_id An ID for a term on the current blog.
1784 * @return int An ID from the global terms table mapped from $term_id.
1786 function global_terms( $term_id, $deprecated = '' ) {
1788 static $global_terms_recurse = null;
1790 if ( !global_terms_enabled() )
1793 // prevent a race condition
1794 $recurse_start = false;
1795 if ( $global_terms_recurse === null ) {
1796 $recurse_start = true;
1797 $global_terms_recurse = 1;
1798 } elseif ( 10 < $global_terms_recurse++ ) {
1802 $term_id = intval( $term_id );
1803 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
1805 $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
1806 if ( $global_id == null ) {
1807 $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
1808 if ( null == $used_global_id ) {
1809 $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1810 $global_id = $wpdb->insert_id;
1811 if ( empty( $global_id ) )
1814 $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
1815 $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
1816 $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
1817 $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1818 $global_id = $wpdb->insert_id;
1820 } elseif ( $global_id != $term_id ) {
1821 $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
1822 if ( null != $local_id )
1823 $local_id = global_terms( $local_id );
1824 if ( 10 < $global_terms_recurse )
1825 $global_id = $term_id;
1828 if ( $global_id != $term_id ) {
1829 if ( get_option( 'default_category' ) == $term_id )
1830 update_option( 'default_category', $global_id );
1832 $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
1833 $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
1834 $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
1836 clean_term_cache($term_id);
1838 if( $recurse_start )
1839 $global_terms_recurse = null;
1845 * Ensure that the current site's domain is listed in the allowed redirect host list.
1847 * @see wp_validate_redirect()
1850 * @return array The current site's domain
1852 function redirect_this_site( $deprecated = '' ) {
1853 return array( get_current_site()->domain );
1857 * Check whether an upload is too big.
1861 * @param array $upload
1862 * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
1864 function upload_is_file_too_big( $upload ) {
1865 if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
1868 if ( strlen( $upload['bits'] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
1869 return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
1875 * Add a nonce field to the signup page.
1878 * @uses wp_nonce_field()
1880 function signup_nonce_fields() {
1882 echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
1883 wp_nonce_field('signup_form_' . $id, '_signup_form', false);
1887 * Process the signup nonce created in signup_nonce_fields().
1890 * @uses wp_create_nonce()
1892 * @param array $result
1895 function signup_nonce_check( $result ) {
1896 if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
1899 if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
1900 wp_die( __( 'Please try again.' ) );
1906 * Correct 404 redirects when NOBLOGREDIRECT is defined.
1910 function maybe_redirect_404() {
1912 * Filter the redirect URL for 404s on the main site.
1914 * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
1918 * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
1920 if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
1921 if ( $destination == '%siteurl%' )
1922 $destination = network_home_url();
1923 wp_redirect( $destination );
1929 * Add a new user to a blog by visiting /newbloguser/username/.
1931 * This will only work when the user's details are saved as an option
1932 * keyed as 'new_user_x', where 'x' is the username of the user to be
1933 * added, as when a user is invited through the regular WP Add User interface.
1936 * @uses add_existing_user_to_blog()
1938 function maybe_add_existing_user_to_blog() {
1939 if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
1942 $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
1943 $key = array_pop( $parts );
1946 $key = array_pop( $parts );
1948 $details = get_option( 'new_user_' . $key );
1949 if ( !empty( $details ) )
1950 delete_option( 'new_user_' . $key );
1952 if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
1953 wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
1955 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' ) );
1959 * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
1962 * @uses add_user_to_blog()
1964 * @param array $details
1966 function add_existing_user_to_blog( $details = false ) {
1969 if ( is_array( $details ) ) {
1970 $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
1972 * Fires immediately after an existing user is added to a site.
1976 * @param int $user_id User ID.
1977 * @param mixed $result True on success or a WP_Error object if the user doesn't exist.
1979 do_action( 'added_existing_user', $details['user_id'], $result );
1985 * Add a newly created user to the appropriate blog
1987 * To add a user in general, use add_user_to_blog(). This function
1988 * is specifically hooked into the wpmu_activate_user action.
1991 * @see add_user_to_blog()
1993 * @param int $user_id
1994 * @param mixed $password Ignored.
1995 * @param array $meta
1997 function add_new_user_to_blog( $user_id, $password, $meta ) {
1998 if ( !empty( $meta[ 'add_to_blog' ] ) ) {
1999 $blog_id = $meta[ 'add_to_blog' ];
2000 $role = $meta[ 'new_role' ];
2001 remove_user_from_blog($user_id, get_current_site()->blog_id); // remove user from main blog.
2002 add_user_to_blog( $blog_id, $user_id, $role );
2003 update_user_meta( $user_id, 'primary_blog', $blog_id );
2008 * Correct From host on outgoing mail to match the site domain
2012 function fix_phpmailer_messageid( $phpmailer ) {
2013 $phpmailer->Hostname = get_current_site()->domain;
2017 * Check to see whether a user is marked as a spammer, based on user login.
2020 * @uses get_user_by()
2022 * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
2023 * or user login name as a string.
2026 function is_user_spammy( $user = null ) {
2027 if ( ! is_a( $user, 'WP_User' ) ) {
2029 $user = get_user_by( 'login', $user );
2031 $user = wp_get_current_user();
2034 return $user && isset( $user->spam ) && 1 == $user->spam;
2038 * Update this blog's 'public' setting in the global blogs table.
2040 * Public blogs have a setting of 1, private blogs are 0.
2043 * @uses update_blog_status()
2045 * @param int $old_value
2046 * @param int $value The new public value
2049 function update_blog_public( $old_value, $value ) {
2050 update_blog_status( get_current_blog_id(), 'public', (int) $value );
2052 add_action('update_option_blog_public', 'update_blog_public', 10, 2);
2055 * Check whether a usermeta key has to do with the current blog.
2058 * @uses wp_get_current_user()
2060 * @param string $key
2061 * @param int $user_id Optional. Defaults to current user.
2062 * @param int $blog_id Optional. Defaults to current blog.
2065 function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
2068 $current_user = wp_get_current_user();
2069 if ( $user_id == 0 )
2070 $user_id = $current_user->ID;
2071 if ( $blog_id == 0 )
2072 $blog_id = $wpdb->blogid;
2074 $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
2076 if ( isset( $current_user->$local_key ) )
2083 * Check whether users can self-register, based on Network settings.
2089 function users_can_register_signup_filter() {
2090 $registration = get_site_option('registration');
2091 if ( $registration == 'all' || $registration == 'user' )
2096 add_filter('option_users_can_register', 'users_can_register_signup_filter');
2099 * Ensure that the welcome message is not empty. Currently unused.
2103 * @param string $text
2106 function welcome_user_msg_filter( $text ) {
2108 remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2109 $text = __( 'Dear User,
2111 Your new account is set up.
2113 You can log in with the following information:
2120 --The Team @ SITE_NAME' );
2121 update_site_option( 'welcome_user_email', $text );
2125 add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2128 * Whether to force SSL on content.
2132 * @param string|bool $force
2133 * @return bool True if forced, false if not forced.
2135 function force_ssl_content( $force = '' ) {
2136 static $forced_content;
2138 if ( '' != $force ) {
2139 $old_forced = $forced_content;
2140 $forced_content = $force;
2144 return $forced_content;
2148 * Formats a URL to use https.
2150 * Useful as a filter.
2155 * @return string URL with https as the scheme
2157 function filter_SSL( $url ) {
2158 if ( ! is_string( $url ) )
2159 return get_bloginfo( 'url' ); // Return home blog url with proper scheme
2161 if ( force_ssl_content() && is_ssl() )
2162 $url = set_url_scheme( $url, 'https' );
2168 * Schedule update of the network-wide counts for the current network.
2172 function wp_schedule_update_network_counts() {
2173 if ( !is_main_site() )
2176 if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
2177 wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
2181 * Update the network-wide counts for the current network.
2185 function wp_update_network_counts() {
2186 wp_update_network_user_counts();
2187 wp_update_network_site_counts();
2191 * Update the count of sites for the current network.
2193 * If enabled through the 'enable_live_network_counts' filter, update the sites count
2194 * on a network when a site is created or its status is updated.
2198 * @uses wp_update_network_site_counts()
2200 function wp_maybe_update_network_site_counts() {
2201 $is_small_network = ! wp_is_large_network( 'sites' );
2204 * Filter whether to update network site or user counts when a new site is created.
2208 * @see wp_is_large_network()
2210 * @param bool $small_network Whether the network is considered small.
2211 * @param string $context Context. Either 'users' or 'sites'.
2213 if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
2216 wp_update_network_site_counts();
2220 * Update the network-wide users count.
2222 * If enabled through the 'enable_live_network_counts' filter, update the users count
2223 * on a network when a user is created or its status is updated.
2227 * @uses wp_update_network_user_counts()
2229 function wp_maybe_update_network_user_counts() {
2230 $is_small_network = ! wp_is_large_network( 'users' );
2232 /** This filter is documented in wp-includes/ms-functions.php */
2233 if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
2236 wp_update_network_user_counts();
2240 * Update the network-wide site count.
2244 function wp_update_network_site_counts() {
2247 $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) );
2248 update_site_option( 'blog_count', $count );
2252 * Update the network-wide user count.
2256 function wp_update_network_user_counts() {
2259 $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
2260 update_site_option( 'user_count', $count );
2264 * Returns the space used by the current blog.
2268 * @return int Used space in megabytes
2270 function get_space_used() {
2272 * Filter the amount of storage space used by the current site.
2276 * @param int|bool $space_used The amount of used space, in megabytes. Default false.
2278 $space_used = apply_filters( 'pre_get_space_used', false );
2279 if ( false === $space_used ) {
2280 $upload_dir = wp_upload_dir();
2281 $space_used = get_dirsize( $upload_dir['basedir'] ) / 1024 / 1024;
2288 * Returns the upload quota for the current blog.
2292 * @return int Quota in megabytes
2294 function get_space_allowed() {
2295 $space_allowed = get_option( 'blog_upload_space' );
2297 if ( ! is_numeric( $space_allowed ) )
2298 $space_allowed = get_site_option( 'blog_upload_space' );
2300 if ( empty( $space_allowed ) || ! is_numeric( $space_allowed ) )
2301 $space_allowed = 100;
2304 * Filter the upload quota for the current site.
2308 * @param int $space_allowed Upload quota in megabytes for the current blog.
2310 return apply_filters( 'get_space_allowed', $space_allowed );
2314 * Determines if there is any upload space left in the current blog's quota.
2318 * @return int of upload space available in bytes
2320 function get_upload_space_available() {
2321 $space_allowed = get_space_allowed() * 1024 * 1024;
2322 if ( get_site_option( 'upload_space_check_disabled' ) )
2323 return $space_allowed;
2325 $space_used = get_space_used() * 1024 * 1024;
2327 if ( ( $space_allowed - $space_used ) <= 0 )
2330 return $space_allowed - $space_used;
2334 * Determines if there is any upload space left in the current blog's quota.
2337 * @return bool True if space is available, false otherwise.
2339 function is_upload_space_available() {
2340 if ( get_site_option( 'upload_space_check_disabled' ) )
2343 return (bool) get_upload_space_available();
2349 * @return int of upload size limit in bytes
2351 function upload_size_limit_filter( $size ) {
2352 $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
2353 if ( get_site_option( 'upload_space_check_disabled' ) )
2354 return min( $size, $fileupload_maxk );
2356 return min( $size, $fileupload_maxk, get_upload_space_available() );
2360 * Whether or not we have a large network.
2362 * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
2363 * Plugins can alter this criteria using the 'wp_is_large_network' filter.
2366 * @param string $using 'sites or 'users'. Default is 'sites'.
2367 * @return bool True if the network meets the criteria for large. False otherwise.
2369 function wp_is_large_network( $using = 'sites' ) {
2370 if ( 'users' == $using ) {
2371 $count = get_user_count();
2373 * Filter whether the network is considered large.
2377 * @param bool $is_large_network Whether the network has more than 10000 users or sites.
2378 * @param string $component The component to count. Accepts 'users', or 'sites'.
2379 * @param int $count The count of items for the component.
2381 return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
2384 $count = get_blog_count();
2385 /** This filter is documented in wp-includes/ms-functions.php */
2386 return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
2391 * Return an array of sites for a network or networks.
2395 * @param array $args {
2396 * Array of default arguments. Optional.
2398 * @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
2399 * from all networks. Defaults to current network ID.
2400 * @type int $public Retrieve public or non-public sites. Default null, for any.
2401 * @type int $archived Retrieve archived or non-archived sites. Default null, for any.
2402 * @type int $mature Retrieve mature or non-mature sites. Default null, for any.
2403 * @type int $spam Retrieve spam or non-spam sites. Default null, for any.
2404 * @type int $deleted Retrieve deleted or non-deleted sites. Default null, for any.
2405 * @type int $limit Number of sites to limit the query to. Default 100.
2406 * @type int $offset Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
2408 * @return array An empty array if the install is considered "large" via wp_is_large_network(). Otherwise,
2409 * an associative array of site data arrays, each containing the site (network) ID, blog ID,
2410 * site domain and path, dates registered and modified, and the language ID. Also, boolean
2411 * values for whether the site is public, archived, mature, spam, and/or deleted.
2413 function wp_get_sites( $args = array() ) {
2416 if ( wp_is_large_network() )
2420 'network_id' => $wpdb->siteid,
2430 $args = wp_parse_args( $args, $defaults );
2432 $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
2434 if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
2435 $network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
2436 $query .= "AND site_id IN ($network_ids) ";
2439 if ( isset( $args['public'] ) )
2440 $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
2442 if ( isset( $args['archived'] ) )
2443 $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
2445 if ( isset( $args['mature'] ) )
2446 $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
2448 if ( isset( $args['spam'] ) )
2449 $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
2451 if ( isset( $args['deleted'] ) )
2452 $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
2454 if ( isset( $args['limit'] ) && $args['limit'] ) {
2455 if ( isset( $args['offset'] ) && $args['offset'] )
2456 $query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
2458 $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
2461 $site_results = $wpdb->get_results( $query, ARRAY_A );
2463 return $site_results;