wp/wp-includes/ms-functions.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    37  * @param int $user_id The unique ID of the user
    37  * @param int $user_id The unique ID of the user
    38  * @return WP_Site|void The blog object
    38  * @return WP_Site|void The blog object
    39  */
    39  */
    40 function get_active_blog_for_user( $user_id ) {
    40 function get_active_blog_for_user( $user_id ) {
    41 	$blogs = get_blogs_of_user( $user_id );
    41 	$blogs = get_blogs_of_user( $user_id );
    42 	if ( empty( $blogs ) )
    42 	if ( empty( $blogs ) ) {
    43 		return;
    43 		return;
       
    44 	}
    44 
    45 
    45 	if ( ! is_multisite() ) {
    46 	if ( ! is_multisite() ) {
    46 		return $blogs[ get_current_blog_id() ];
    47 		return $blogs[ get_current_blog_id() ];
    47 	}
    48 	}
    48 
    49 
    49 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    50 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
    50 	$first_blog = current($blogs);
    51 	$first_blog   = current( $blogs );
    51 	if ( false !== $primary_blog ) {
    52 	if ( false !== $primary_blog ) {
    52 		if ( ! isset( $blogs[ $primary_blog ] ) ) {
    53 		if ( ! isset( $blogs[ $primary_blog ] ) ) {
    53 			update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
    54 			update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
    54 			$primary = get_site( $first_blog->userblog_id );
    55 			$primary = get_site( $first_blog->userblog_id );
    55 		} else {
    56 		} else {
    65 		}
    66 		}
    66 	}
    67 	}
    67 
    68 
    68 	if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
    69 	if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
    69 		$blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
    70 		$blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
    70 		$ret = false;
    71 		$ret   = false;
    71 		if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
    72 		if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
    72 			foreach ( (array) $blogs as $blog_id => $blog ) {
    73 			foreach ( (array) $blogs as $blog_id => $blog ) {
    73 				if ( $blog->site_id != get_current_network_id() )
    74 				if ( $blog->site_id != get_current_network_id() ) {
    74 					continue;
    75 					continue;
       
    76 				}
    75 				$details = get_site( $blog_id );
    77 				$details = get_site( $blog_id );
    76 				if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
    78 				if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
    77 					$ret = $blog;
    79 					$ret = $details;
    78 					if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
    80 					if ( get_user_meta( $user_id, 'primary_blog', true ) != $blog_id ) {
    79 						update_user_meta( $user_id, 'primary_blog', $blog_id );
    81 						update_user_meta( $user_id, 'primary_blog', $blog_id );
    80 					if ( !get_user_meta($user_id , 'source_domain', true) )
    82 					}
    81 						update_user_meta( $user_id, 'source_domain', $blog->domain );
    83 					if ( ! get_user_meta( $user_id, 'source_domain', true ) ) {
       
    84 						update_user_meta( $user_id, 'source_domain', $details->domain );
       
    85 					}
    82 					break;
    86 					break;
    83 				}
    87 				}
    84 			}
    88 			}
    85 		} else {
    89 		} else {
    86 			return;
    90 			return;
    95  * The number of active users in your installation.
    99  * The number of active users in your installation.
    96  *
   100  *
    97  * The count is cached and updated twice daily. This is not a live count.
   101  * The count is cached and updated twice daily. This is not a live count.
    98  *
   102  *
    99  * @since MU (3.0.0)
   103  * @since MU (3.0.0)
   100  * @since 4.8.0 The $network_id parameter has been added.
   104  * @since 4.8.0 The `$network_id` parameter has been added.
   101  *
   105  *
   102  * @param int|null $network_id ID of the network. Default is the current network.
   106  * @param int|null $network_id ID of the network. Default is the current network.
   103  * @return int Number of active users on the network.
   107  * @return int Number of active users on the network.
   104  */
   108  */
   105 function get_user_count( $network_id = null ) {
   109 function get_user_count( $network_id = null ) {
   110  * The number of active sites on your installation.
   114  * The number of active sites on your installation.
   111  *
   115  *
   112  * The count is cached and updated twice daily. This is not a live count.
   116  * The count is cached and updated twice daily. This is not a live count.
   113  *
   117  *
   114  * @since MU (3.0.0)
   118  * @since MU (3.0.0)
   115  * @since 3.7.0 The $network_id parameter has been deprecated.
   119  * @since 3.7.0 The `$network_id` parameter has been deprecated.
   116  * @since 4.8.0 The $network_id parameter is now being used.
   120  * @since 4.8.0 The `$network_id` parameter is now being used.
   117  *
   121  *
   118  * @param int|null $network_id ID of the network. Default is the current network.
   122  * @param int|null $network_id ID of the network. Default is the current network.
   119  * @return int Number of active sites on the network.
   123  * @return int Number of active sites on the network.
   120  */
   124  */
   121 function get_blog_count( $network_id = null ) {
   125 function get_blog_count( $network_id = null ) {
   150  * @param int    $user_id ID of the user you're adding.
   154  * @param int    $user_id ID of the user you're adding.
   151  * @param string $role    The role you want the user to have
   155  * @param string $role    The role you want the user to have
   152  * @return true|WP_Error
   156  * @return true|WP_Error
   153  */
   157  */
   154 function add_user_to_blog( $blog_id, $user_id, $role ) {
   158 function add_user_to_blog( $blog_id, $user_id, $role ) {
   155 	switch_to_blog($blog_id);
   159 	switch_to_blog( $blog_id );
   156 
   160 
   157 	$user = get_userdata( $user_id );
   161 	$user = get_userdata( $user_id );
   158 
   162 
   159 	if ( ! $user ) {
   163 	if ( ! $user ) {
   160 		restore_current_blog();
   164 		restore_current_blog();
   182 		}
   186 		}
   183 
   187 
   184 		return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );
   188 		return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) );
   185 	}
   189 	}
   186 
   190 
   187 	if ( !get_user_meta($user_id, 'primary_blog', true) ) {
   191 	if ( ! get_user_meta( $user_id, 'primary_blog', true ) ) {
   188 		update_user_meta($user_id, 'primary_blog', $blog_id);
   192 		update_user_meta( $user_id, 'primary_blog', $blog_id );
   189 		$site = get_site( $blog_id );
   193 		$site = get_site( $blog_id );
   190 		update_user_meta( $user_id, 'source_domain', $site->domain );
   194 		update_user_meta( $user_id, 'source_domain', $site->domain );
   191 	}
   195 	}
   192 
   196 
   193 	$user->set_role($role);
   197 	$user->set_role( $role );
   194 
   198 
   195 	/**
   199 	/**
   196 	 * Fires immediately after a user is added to a site.
   200 	 * Fires immediately after a user is added to a site.
   197 	 *
   201 	 *
   198 	 * @since MU (3.0.0)
   202 	 * @since MU (3.0.0)
   224  * @param int    $user_id  ID of the user you're removing.
   228  * @param int    $user_id  ID of the user you're removing.
   225  * @param int    $blog_id  ID of the blog you're removing the user from.
   229  * @param int    $blog_id  ID of the blog you're removing the user from.
   226  * @param string $reassign Optional. A user to whom to reassign posts.
   230  * @param string $reassign Optional. A user to whom to reassign posts.
   227  * @return true|WP_Error
   231  * @return true|WP_Error
   228  */
   232  */
   229 function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
   233 function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) {
   230 	global $wpdb;
   234 	global $wpdb;
   231 	switch_to_blog($blog_id);
   235 	switch_to_blog( $blog_id );
   232 	$user_id = (int) $user_id;
   236 	$user_id = (int) $user_id;
   233 	/**
   237 	/**
   234 	 * Fires before a user is removed from a site.
   238 	 * Fires before a user is removed from a site.
   235 	 *
   239 	 *
   236 	 * @since MU (3.0.0)
   240 	 * @since MU (3.0.0)
   240 	 */
   244 	 */
   241 	do_action( 'remove_user_from_blog', $user_id, $blog_id );
   245 	do_action( 'remove_user_from_blog', $user_id, $blog_id );
   242 
   246 
   243 	// If being removed from the primary blog, set a new primary if the user is assigned
   247 	// If being removed from the primary blog, set a new primary if the user is assigned
   244 	// to multiple blogs.
   248 	// to multiple blogs.
   245 	$primary_blog = get_user_meta($user_id, 'primary_blog', true);
   249 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
   246 	if ( $primary_blog == $blog_id ) {
   250 	if ( $primary_blog == $blog_id ) {
   247 		$new_id = '';
   251 		$new_id     = '';
   248 		$new_domain = '';
   252 		$new_domain = '';
   249 		$blogs = get_blogs_of_user($user_id);
   253 		$blogs      = get_blogs_of_user( $user_id );
   250 		foreach ( (array) $blogs as $blog ) {
   254 		foreach ( (array) $blogs as $blog ) {
   251 			if ( $blog->userblog_id == $blog_id )
   255 			if ( $blog->userblog_id == $blog_id ) {
   252 				continue;
   256 				continue;
   253 			$new_id = $blog->userblog_id;
   257 			}
       
   258 			$new_id     = $blog->userblog_id;
   254 			$new_domain = $blog->domain;
   259 			$new_domain = $blog->domain;
   255 			break;
   260 			break;
   256 		}
   261 		}
   257 
   262 
   258 		update_user_meta($user_id, 'primary_blog', $new_id);
   263 		update_user_meta( $user_id, 'primary_blog', $new_id );
   259 		update_user_meta($user_id, 'source_domain', $new_domain);
   264 		update_user_meta( $user_id, 'source_domain', $new_domain );
   260 	}
   265 	}
   261 
   266 
   262 	// wp_revoke_user($user_id);
   267 	// wp_revoke_user($user_id);
   263 	$user = get_userdata( $user_id );
   268 	$user = get_userdata( $user_id );
   264 	if ( ! $user ) {
   269 	if ( ! $user ) {
   265 		restore_current_blog();
   270 		restore_current_blog();
   266 		return new WP_Error('user_does_not_exist', __('That user does not exist.'));
   271 		return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) );
   267 	}
   272 	}
   268 
   273 
   269 	$user->remove_all_caps();
   274 	$user->remove_all_caps();
   270 
   275 
   271 	$blogs = get_blogs_of_user($user_id);
   276 	$blogs = get_blogs_of_user( $user_id );
   272 	if ( count($blogs) == 0 ) {
   277 	if ( count( $blogs ) == 0 ) {
   273 		update_user_meta($user_id, 'primary_blog', '');
   278 		update_user_meta( $user_id, 'primary_blog', '' );
   274 		update_user_meta($user_id, 'source_domain', '');
   279 		update_user_meta( $user_id, 'source_domain', '' );
   275 	}
   280 	}
   276 
   281 
   277 	if ( $reassign != '' ) {
   282 	if ( $reassign != '' ) {
   278 		$reassign = (int) $reassign;
   283 		$reassign = (int) $reassign;
   279 		$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
   284 		$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $user_id ) );
   328  * @param string $path   Optional. Not required for subdomain installations.
   333  * @param string $path   Optional. Not required for subdomain installations.
   329  * @return int 0 if no blog found, otherwise the ID of the matching blog
   334  * @return int 0 if no blog found, otherwise the ID of the matching blog
   330  */
   335  */
   331 function get_blog_id_from_url( $domain, $path = '/' ) {
   336 function get_blog_id_from_url( $domain, $path = '/' ) {
   332 	$domain = strtolower( $domain );
   337 	$domain = strtolower( $domain );
   333 	$path = strtolower( $path );
   338 	$path   = strtolower( $path );
   334 	$id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
   339 	$id     = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
   335 
   340 
   336 	if ( $id == -1 ) // blog does not exist
   341 	if ( $id == -1 ) { // blog does not exist
   337 		return 0;
   342 		return 0;
   338 	elseif ( $id )
   343 	} elseif ( $id ) {
   339 		return (int) $id;
   344 		return (int) $id;
   340 
   345 	}
   341 	$args = array(
   346 
   342 		'domain' => $domain,
   347 	$args   = array(
   343 		'path' => $path,
   348 		'domain'                 => $domain,
   344 		'fields' => 'ids',
   349 		'path'                   => $path,
   345 		'number' => 1,
   350 		'fields'                 => 'ids',
       
   351 		'number'                 => 1,
       
   352 		'update_site_meta_cache' => false,
   346 	);
   353 	);
   347 	$result = get_sites( $args );
   354 	$result = get_sites( $args );
   348 	$id = array_shift( $result );
   355 	$id     = array_shift( $result );
   349 
   356 
   350 	if ( ! $id ) {
   357 	if ( ! $id ) {
   351 		wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
   358 		wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
   352 		return 0;
   359 		return 0;
   353 	}
   360 	}
   372  * @param string $user_email The email provided by the user at registration.
   379  * @param string $user_email The email provided by the user at registration.
   373  * @return bool Returns true when the email address is banned.
   380  * @return bool Returns true when the email address is banned.
   374  */
   381  */
   375 function is_email_address_unsafe( $user_email ) {
   382 function is_email_address_unsafe( $user_email ) {
   376 	$banned_names = get_site_option( 'banned_email_domains' );
   383 	$banned_names = get_site_option( 'banned_email_domains' );
   377 	if ( $banned_names && ! is_array( $banned_names ) )
   384 	if ( $banned_names && ! is_array( $banned_names ) ) {
   378 		$banned_names = explode( "\n", $banned_names );
   385 		$banned_names = explode( "\n", $banned_names );
       
   386 	}
   379 
   387 
   380 	$is_email_address_unsafe = false;
   388 	$is_email_address_unsafe = false;
   381 
   389 
   382 	if ( $banned_names && is_array( $banned_names ) && false !== strpos( $user_email, '@', 1 ) ) {
   390 	if ( $banned_names && is_array( $banned_names ) && false !== strpos( $user_email, '@', 1 ) ) {
   383 		$banned_names = array_map( 'strtolower', $banned_names );
   391 		$banned_names     = array_map( 'strtolower', $banned_names );
   384 		$normalized_email = strtolower( $user_email );
   392 		$normalized_email = strtolower( $user_email );
   385 
   393 
   386 		list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
   394 		list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
   387 
   395 
   388 		foreach ( $banned_names as $banned_domain ) {
   396 		foreach ( $banned_names as $banned_domain ) {
   389 			if ( ! $banned_domain )
   397 			if ( ! $banned_domain ) {
   390 				continue;
   398 				continue;
       
   399 			}
   391 
   400 
   392 			if ( $email_domain == $banned_domain ) {
   401 			if ( $email_domain == $banned_domain ) {
   393 				$is_email_address_unsafe = true;
   402 				$is_email_address_unsafe = true;
   394 				break;
   403 				break;
   395 			}
   404 			}
   431  *
   440  *
   432  * @param string $user_name  The login name provided by the user.
   441  * @param string $user_name  The login name provided by the user.
   433  * @param string $user_email The email provided by the user.
   442  * @param string $user_email The email provided by the user.
   434  * @return array Contains username, email, and error messages.
   443  * @return array Contains username, email, and error messages.
   435  */
   444  */
   436 function wpmu_validate_user_signup($user_name, $user_email) {
   445 function wpmu_validate_user_signup( $user_name, $user_email ) {
   437 	global $wpdb;
   446 	global $wpdb;
   438 
   447 
   439 	$errors = new WP_Error();
   448 	$errors = new WP_Error();
   440 
   449 
   441 	$orig_username = $user_name;
   450 	$orig_username = $user_name;
   442 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
   451 	$user_name     = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
   443 
   452 
   444 	if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
   453 	if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
   445 		$errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
   454 		$errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) );
   446 		$user_name = $orig_username;
   455 		$user_name = $orig_username;
   447 	}
   456 	}
   448 
   457 
   449 	$user_email = sanitize_email( $user_email );
   458 	$user_email = sanitize_email( $user_email );
   450 
   459 
   451 	if ( empty( $user_name ) )
   460 	if ( empty( $user_name ) ) {
   452 	   	$errors->add('user_name', __( 'Please enter a username.' ) );
   461 		$errors->add( 'user_name', __( 'Please enter a username.' ) );
       
   462 	}
   453 
   463 
   454 	$illegal_names = get_site_option( 'illegal_names' );
   464 	$illegal_names = get_site_option( 'illegal_names' );
   455 	if ( ! is_array( $illegal_names ) ) {
   465 	if ( ! is_array( $illegal_names ) ) {
   456 		$illegal_names = array(  'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
   466 		$illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
   457 		add_site_option( 'illegal_names', $illegal_names );
   467 		add_site_option( 'illegal_names', $illegal_names );
   458 	}
   468 	}
   459 	if ( in_array( $user_name, $illegal_names ) ) {
   469 	if ( in_array( $user_name, $illegal_names ) ) {
   460 		$errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
   470 		$errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
   461 	}
   471 	}
   462 
   472 
   463 	/** This filter is documented in wp-includes/user.php */
   473 	/** This filter is documented in wp-includes/user.php */
   464 	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
   474 	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
   465 
   475 
   466 	if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
   476 	if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) {
   467 		$errors->add( 'user_name',  __( 'Sorry, that username is not allowed.' ) );
   477 		$errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) );
   468 	}
   478 	}
   469 
   479 
   470 	if ( ! is_email( $user_email ) ) {
   480 	if ( ! is_email( $user_email ) ) {
   471 		$errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
   481 		$errors->add( 'user_email', __( 'Please enter a valid email address.' ) );
   472 	} elseif ( is_email_address_unsafe( $user_email ) ) {
   482 	} elseif ( is_email_address_unsafe( $user_email ) ) {
   473 		$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.' ) );
   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.' ) );
   474 	}
   484 	}
   475 
   485 
   476 	if ( strlen( $user_name ) < 4 )
   486 	if ( strlen( $user_name ) < 4 ) {
   477 		$errors->add('user_name',  __( 'Username must be at least 4 characters.' ) );
   487 		$errors->add( 'user_name', __( 'Username must be at least 4 characters.' ) );
       
   488 	}
   478 
   489 
   479 	if ( strlen( $user_name ) > 60 ) {
   490 	if ( strlen( $user_name ) > 60 ) {
   480 		$errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
   491 		$errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) );
   481 	}
   492 	}
   482 
   493 
   483 	// all numeric?
   494 	// all numeric?
   484 	if ( preg_match( '/^[0-9]*$/', $user_name ) )
   495 	if ( preg_match( '/^[0-9]*$/', $user_name ) ) {
   485 		$errors->add('user_name', __('Sorry, usernames must have letters too!'));
   496 		$errors->add( 'user_name', __( 'Sorry, usernames must have letters too!' ) );
       
   497 	}
   486 
   498 
   487 	$limited_email_domains = get_site_option( 'limited_email_domains' );
   499 	$limited_email_domains = get_site_option( 'limited_email_domains' );
   488 	if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
   500 	if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) {
   489 		$emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
   501 		$limited_email_domains = array_map( 'strtolower', $limited_email_domains );
   490 		if ( ! in_array( $emaildomain, $limited_email_domains ) ) {
   502 		$emaildomain           = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );
   491 			$errors->add('user_email', __('Sorry, that email address is not allowed!'));
   503 		if ( ! in_array( $emaildomain, $limited_email_domains, true ) ) {
       
   504 			$errors->add( 'user_email', __( 'Sorry, that email address is not allowed!' ) );
   492 		}
   505 		}
   493 	}
   506 	}
   494 
   507 
   495 	// Check if the username has been used already.
   508 	// Check if the username has been used already.
   496 	if ( username_exists($user_name) )
   509 	if ( username_exists( $user_name ) ) {
   497 		$errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
   510 		$errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
       
   511 	}
   498 
   512 
   499 	// Check if the email address has been used already.
   513 	// Check if the email address has been used already.
   500 	if ( email_exists($user_email) )
   514 	if ( email_exists( $user_email ) ) {
   501 		$errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
   515 		$errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
       
   516 	}
   502 
   517 
   503 	// Has someone already signed up for this username?
   518 	// Has someone already signed up for this username?
   504 	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
   519 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name ) );
   505 	if ( $signup != null ) {
   520 	if ( $signup != null ) {
   506 		$registered_at =  mysql2date('U', $signup->registered);
   521 		$registered_at = mysql2date( 'U', $signup->registered );
   507 		$now = current_time( 'timestamp', true );
   522 		$now           = time();
   508 		$diff = $now - $registered_at;
   523 		$diff          = $now - $registered_at;
   509 		// If registered more than two days ago, cancel registration and let this signup go through.
   524 		// If registered more than two days ago, cancel registration and let this signup go through.
   510 		if ( $diff > 2 * DAY_IN_SECONDS )
   525 		if ( $diff > 2 * DAY_IN_SECONDS ) {
   511 			$wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
   526 			$wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
   512 		else
   527 		} else {
   513 			$errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
   528 			$errors->add( 'user_name', __( 'That username is currently reserved but may be available in a couple of days.' ) );
   514 	}
   529 		}
   515 
   530 	}
   516 	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
   531 
       
   532 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email ) );
   517 	if ( $signup != null ) {
   533 	if ( $signup != null ) {
   518 		$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
   534 		$diff = time() - mysql2date( 'U', $signup->registered );
   519 		// If registered more than two days ago, cancel registration and let this signup go through.
   535 		// If registered more than two days ago, cancel registration and let this signup go through.
   520 		if ( $diff > 2 * DAY_IN_SECONDS )
   536 		if ( $diff > 2 * DAY_IN_SECONDS ) {
   521 			$wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
   537 			$wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
   522 		else
   538 		} else {
   523 			$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.'));
   539 			$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.' ) );
   524 	}
   540 		}
   525 
   541 	}
   526 	$result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
   542 
       
   543 	$result = array(
       
   544 		'user_name'     => $user_name,
       
   545 		'orig_username' => $orig_username,
       
   546 		'user_email'    => $user_email,
       
   547 		'errors'        => $errors,
       
   548 	);
   527 
   549 
   528 	/**
   550 	/**
   529 	 * Filters the validated user registration details.
   551 	 * Filters the validated user registration details.
   530 	 *
   552 	 *
   531 	 * This does not allow you to override the username or email of the user during
   553 	 * This does not allow you to override the username or email of the user during
   571  */
   593  */
   572 function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
   594 function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
   573 	global $wpdb, $domain;
   595 	global $wpdb, $domain;
   574 
   596 
   575 	$current_network = get_network();
   597 	$current_network = get_network();
   576 	$base = $current_network->path;
   598 	$base            = $current_network->path;
   577 
   599 
   578 	$blog_title = strip_tags( $blog_title );
   600 	$blog_title = strip_tags( $blog_title );
   579 
   601 
   580 	$errors = new WP_Error();
   602 	$errors        = new WP_Error();
   581 	$illegal_names = get_site_option( 'illegal_names' );
   603 	$illegal_names = get_site_option( 'illegal_names' );
   582 	if ( $illegal_names == false ) {
   604 	if ( $illegal_names == false ) {
   583 		$illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
   605 		$illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
   584 		add_site_option( 'illegal_names', $illegal_names );
   606 		add_site_option( 'illegal_names', $illegal_names );
   585 	}
   607 	}
   590 	 */
   612 	 */
   591 	if ( ! is_subdomain_install() ) {
   613 	if ( ! is_subdomain_install() ) {
   592 		$illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
   614 		$illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
   593 	}
   615 	}
   594 
   616 
   595 	if ( empty( $blogname ) )
   617 	if ( empty( $blogname ) ) {
   596 		$errors->add('blogname', __( 'Please enter a site name.' ) );
   618 		$errors->add( 'blogname', __( 'Please enter a site name.' ) );
       
   619 	}
   597 
   620 
   598 	if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
   621 	if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
   599 		$errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) );
   622 		$errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) );
   600 	}
   623 	}
   601 
   624 
   602 	if ( in_array( $blogname, $illegal_names ) )
   625 	if ( in_array( $blogname, $illegal_names ) ) {
   603 		$errors->add('blogname',  __( 'That name is not allowed.' ) );
   626 		$errors->add( 'blogname', __( 'That name is not allowed.' ) );
       
   627 	}
   604 
   628 
   605 	/**
   629 	/**
   606 	 * Filters the minimum site name length required when validating a site signup.
   630 	 * Filters the minimum site name length required when validating a site signup.
   607 	 *
   631 	 *
   608 	 * @since 4.8.0
   632 	 * @since 4.8.0
   615 		/* translators: %s: minimum site name length */
   639 		/* translators: %s: minimum site name length */
   616 		$errors->add( 'blogname', sprintf( _n( 'Site name must be at least %s character.', 'Site name must be at least %s characters.', $minimum_site_name_length ), number_format_i18n( $minimum_site_name_length ) ) );
   640 		$errors->add( 'blogname', sprintf( _n( 'Site name must be at least %s character.', 'Site name must be at least %s characters.', $minimum_site_name_length ), number_format_i18n( $minimum_site_name_length ) ) );
   617 	}
   641 	}
   618 
   642 
   619 	// do not allow users to create a blog that conflicts with a page on the main blog.
   643 	// do not allow users to create a blog that conflicts with a page on the main blog.
   620 	if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
   644 	if ( ! is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( 'SELECT post_name FROM ' . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) ) {
   621 		$errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
   645 		$errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
       
   646 	}
   622 
   647 
   623 	// all numeric?
   648 	// all numeric?
   624 	if ( preg_match( '/^[0-9]*$/', $blogname ) )
   649 	if ( preg_match( '/^[0-9]*$/', $blogname ) ) {
   625 		$errors->add('blogname', __('Sorry, site names must have letters too!'));
   650 		$errors->add( 'blogname', __( 'Sorry, site names must have letters too!' ) );
       
   651 	}
   626 
   652 
   627 	/**
   653 	/**
   628 	 * Filters the new site name during registration.
   654 	 * Filters the new site name during registration.
   629 	 *
   655 	 *
   630 	 * The name is the site's subdomain or the site's subdirectory
   656 	 * The name is the site's subdomain or the site's subdirectory
   634 	 *
   660 	 *
   635 	 * @param string $blogname Site name.
   661 	 * @param string $blogname Site name.
   636 	 */
   662 	 */
   637 	$blogname = apply_filters( 'newblogname', $blogname );
   663 	$blogname = apply_filters( 'newblogname', $blogname );
   638 
   664 
   639 	$blog_title = wp_unslash(  $blog_title );
   665 	$blog_title = wp_unslash( $blog_title );
   640 
   666 
   641 	if ( empty( $blog_title ) )
   667 	if ( empty( $blog_title ) ) {
   642 		$errors->add('blog_title', __( 'Please enter a site title.' ) );
   668 		$errors->add( 'blog_title', __( 'Please enter a site title.' ) );
       
   669 	}
   643 
   670 
   644 	// Check if the domain/path has been used already.
   671 	// Check if the domain/path has been used already.
   645 	if ( is_subdomain_install() ) {
   672 	if ( is_subdomain_install() ) {
   646 		$mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
   673 		$mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
   647 		$path = $base;
   674 		$path     = $base;
   648 	} else {
   675 	} else {
   649 		$mydomain = "$domain";
   676 		$mydomain = "$domain";
   650 		$path = $base.$blogname.'/';
   677 		$path     = $base . $blogname . '/';
   651 	}
   678 	}
   652 	if ( domain_exists($mydomain, $path, $current_network->id) )
   679 	if ( domain_exists( $mydomain, $path, $current_network->id ) ) {
   653 		$errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
   680 		$errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
       
   681 	}
   654 
   682 
   655 	if ( username_exists( $blogname ) ) {
   683 	if ( username_exists( $blogname ) ) {
   656 		if ( ! is_object( $user ) || ( is_object($user) && ( $user->user_login != $blogname ) ) )
   684 		if ( ! is_object( $user ) || ( is_object( $user ) && ( $user->user_login != $blogname ) ) ) {
   657 			$errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
   685 			$errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
       
   686 		}
   658 	}
   687 	}
   659 
   688 
   660 	// Has someone already signed up for this domain?
   689 	// Has someone already signed up for this domain?
   661 	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
   690 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) ); // TODO: Check email too?
   662 	if ( ! empty($signup) ) {
   691 	if ( ! empty( $signup ) ) {
   663 		$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
   692 		$diff = time() - mysql2date( 'U', $signup->registered );
   664 		// If registered more than two days ago, cancel registration and let this signup go through.
   693 		// If registered more than two days ago, cancel registration and let this signup go through.
   665 		if ( $diff > 2 * DAY_IN_SECONDS )
   694 		if ( $diff > 2 * DAY_IN_SECONDS ) {
   666 			$wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
   695 			$wpdb->delete(
   667 		else
   696 				$wpdb->signups,
   668 			$errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
   697 				array(
   669 	}
   698 					'domain' => $mydomain,
   670 
   699 					'path'   => $path,
   671 	$result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
   700 				)
       
   701 			);
       
   702 		} else {
       
   703 			$errors->add( 'blogname', __( 'That site is currently reserved but may be available in a couple days.' ) );
       
   704 		}
       
   705 	}
       
   706 
       
   707 	$result = array(
       
   708 		'domain'     => $mydomain,
       
   709 		'path'       => $path,
       
   710 		'blogname'   => $blogname,
       
   711 		'blog_title' => $blog_title,
       
   712 		'user'       => $user,
       
   713 		'errors'     => $errors,
       
   714 	);
   672 
   715 
   673 	/**
   716 	/**
   674 	 * Filters site details and error messages following registration.
   717 	 * Filters site details and error messages following registration.
   675 	 *
   718 	 *
   676 	 * @since MU (3.0.0)
   719 	 * @since MU (3.0.0)
   701  * @param string $title      The requested site title.
   744  * @param string $title      The requested site title.
   702  * @param string $user       The user's requested login name.
   745  * @param string $user       The user's requested login name.
   703  * @param string $user_email The user's email address.
   746  * @param string $user_email The user's email address.
   704  * @param array  $meta       Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
   747  * @param array  $meta       Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
   705  */
   748  */
   706 function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() )  {
   749 function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
   707 	global $wpdb;
   750 	global $wpdb;
   708 
   751 
   709 	$key = substr( md5( time() . wp_rand() . $domain ), 0, 16 );
   752 	$key = substr( md5( time() . wp_rand() . $domain ), 0, 16 );
   710 
   753 
   711 	/**
   754 	/**
   723 	 * @param string $user_email The user's email address.
   766 	 * @param string $user_email The user's email address.
   724 	 * @param string $key        The user's activation key.
   767 	 * @param string $key        The user's activation key.
   725 	 */
   768 	 */
   726 	$meta = apply_filters( 'signup_site_meta', $meta, $domain, $path, $title, $user, $user_email, $key );
   769 	$meta = apply_filters( 'signup_site_meta', $meta, $domain, $path, $title, $user, $user_email, $key );
   727 
   770 
   728 	$wpdb->insert( $wpdb->signups, array(
   771 	$wpdb->insert(
   729 		'domain' => $domain,
   772 		$wpdb->signups,
   730 		'path' => $path,
   773 		array(
   731 		'title' => $title,
   774 			'domain'         => $domain,
   732 		'user_login' => $user,
   775 			'path'           => $path,
   733 		'user_email' => $user_email,
   776 			'title'          => $title,
   734 		'registered' => current_time('mysql', true),
   777 			'user_login'     => $user,
   735 		'activation_key' => $key,
   778 			'user_email'     => $user_email,
   736 		'meta' => serialize( $meta )
   779 			'registered'     => current_time( 'mysql', true ),
   737 	) );
   780 			'activation_key' => $key,
       
   781 			'meta'           => serialize( $meta ),
       
   782 		)
       
   783 	);
   738 
   784 
   739 	/**
   785 	/**
   740 	 * Fires after site signup information has been written to the database.
   786 	 * Fires after site signup information has been written to the database.
   741 	 *
   787 	 *
   742 	 * @since 4.4.0
   788 	 * @since 4.4.0
   768  */
   814  */
   769 function wpmu_signup_user( $user, $user_email, $meta = array() ) {
   815 function wpmu_signup_user( $user, $user_email, $meta = array() ) {
   770 	global $wpdb;
   816 	global $wpdb;
   771 
   817 
   772 	// Format data
   818 	// Format data
   773 	$user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
   819 	$user       = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
   774 	$user_email = sanitize_email( $user_email );
   820 	$user_email = sanitize_email( $user_email );
   775 	$key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 );
   821 	$key        = substr( md5( time() . wp_rand() . $user_email ), 0, 16 );
   776 
   822 
   777 	/**
   823 	/**
   778 	 * Filters the metadata for a user signup.
   824 	 * Filters the metadata for a user signup.
   779 	 *
   825 	 *
   780 	 * The metadata will be serialized prior to storing it in the database.
   826 	 * The metadata will be serialized prior to storing it in the database.
   786 	 * @param string $user_email The user's email address.
   832 	 * @param string $user_email The user's email address.
   787 	 * @param string $key        The user's activation key.
   833 	 * @param string $key        The user's activation key.
   788 	 */
   834 	 */
   789 	$meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key );
   835 	$meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key );
   790 
   836 
   791 	$wpdb->insert( $wpdb->signups, array(
   837 	$wpdb->insert(
   792 		'domain' => '',
   838 		$wpdb->signups,
   793 		'path' => '',
   839 		array(
   794 		'title' => '',
   840 			'domain'         => '',
   795 		'user_login' => $user,
   841 			'path'           => '',
   796 		'user_email' => $user_email,
   842 			'title'          => '',
   797 		'registered' => current_time('mysql', true),
   843 			'user_login'     => $user,
   798 		'activation_key' => $key,
   844 			'user_email'     => $user_email,
   799 		'meta' => serialize( $meta )
   845 			'registered'     => current_time( 'mysql', true ),
   800 	) );
   846 			'activation_key' => $key,
       
   847 			'meta'           => serialize( $meta ),
       
   848 		)
       
   849 	);
   801 
   850 
   802 	/**
   851 	/**
   803 	 * Fires after a user's signup information has been written to the database.
   852 	 * Fires after a user's signup information has been written to the database.
   804 	 *
   853 	 *
   805 	 * @since 4.4.0
   854 	 * @since 4.4.0
   854 	if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
   903 	if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
   855 		return false;
   904 		return false;
   856 	}
   905 	}
   857 
   906 
   858 	// Send email with activation link.
   907 	// Send email with activation link.
   859 	if ( !is_subdomain_install() || get_current_network_id() != 1 )
   908 	if ( ! is_subdomain_install() || get_current_network_id() != 1 ) {
   860 		$activate_url = network_site_url("wp-activate.php?key=$key");
   909 		$activate_url = network_site_url( "wp-activate.php?key=$key" );
   861 	else
   910 	} else {
   862 		$activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
   911 		$activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
   863 
   912 	}
   864 	$activate_url = esc_url($activate_url);
   913 
   865 	$admin_email = get_site_option( 'admin_email' );
   914 	$activate_url = esc_url( $activate_url );
   866 	if ( $admin_email == '' )
   915 	$admin_email  = get_site_option( 'admin_email' );
       
   916 	if ( $admin_email == '' ) {
   867 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
   917 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
   868 	$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
   918 	}
   869 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
   919 	$from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
   870 
   920 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
   871 	$user = get_user_by( 'login', $user_login );
   921 
       
   922 	$user            = get_user_by( 'login', $user_login );
   872 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
   923 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
   873 
   924 
   874 	$message = sprintf(
   925 	$message = sprintf(
   875 		/**
   926 		/**
   876 		 * Filters the message content of the new blog notification email.
   927 		 * Filters the message content of the new blog notification email.
   886 		 * @param string $user_login User login name.
   937 		 * @param string $user_login User login name.
   887 		 * @param string $user_email User email address.
   938 		 * @param string $user_email User email address.
   888 		 * @param string $key        Activation key created in wpmu_signup_blog().
   939 		 * @param string $key        Activation key created in wpmu_signup_blog().
   889 		 * @param array  $meta       Signup meta data. By default, contains the requested privacy setting and lang_id.
   940 		 * @param array  $meta       Signup meta data. By default, contains the requested privacy setting and lang_id.
   890 		 */
   941 		 */
   891 		apply_filters( 'wpmu_signup_blog_notification_email',
   942 		apply_filters(
   892 			__( "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" ),
   943 			'wpmu_signup_blog_notification_email',
   893 			$domain, $path, $title, $user_login, $user_email, $key, $meta
   944 			__( "To activate your blog, please click the following link:\n\n%1\$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%2\$s" ),
       
   945 			$domain,
       
   946 			$path,
       
   947 			$title,
       
   948 			$user_login,
       
   949 			$user_email,
       
   950 			$key,
       
   951 			$meta
   894 		),
   952 		),
   895 		$activate_url,
   953 		$activate_url,
   896 		esc_url( "http://{$domain}{$path}" ),
   954 		esc_url( "http://{$domain}{$path}" ),
   897 		$key
   955 		$key
   898 	);
   956 	);
   910 		 * @param string $user_login User login name.
   968 		 * @param string $user_login User login name.
   911 		 * @param string $user_email User email address.
   969 		 * @param string $user_email User email address.
   912 		 * @param string $key        Activation key created in wpmu_signup_blog().
   970 		 * @param string $key        Activation key created in wpmu_signup_blog().
   913 		 * @param array  $meta       Signup meta data. By default, contains the requested privacy setting and lang_id.
   971 		 * @param array  $meta       Signup meta data. By default, contains the requested privacy setting and lang_id.
   914 		 */
   972 		 */
   915 		apply_filters( 'wpmu_signup_blog_notification_subject',
   973 		apply_filters(
       
   974 			'wpmu_signup_blog_notification_subject',
   916 			/* translators: New site notification email subject. 1: Network name, 2: New site URL */
   975 			/* translators: New site notification email subject. 1: Network name, 2: New site URL */
   917 			_x( '[%1$s] Activate %2$s', 'New site notification email subject' ),
   976 			_x( '[%1$s] Activate %2$s', 'New site notification email subject' ),
   918 			$domain, $path, $title, $user_login, $user_email, $key, $meta
   977 			$domain,
       
   978 			$path,
       
   979 			$title,
       
   980 			$user_login,
       
   981 			$user_email,
       
   982 			$key,
       
   983 			$meta
   919 		),
   984 		),
   920 		$from_name,
   985 		$from_name,
   921 		esc_url( 'http://' . $domain . $path )
   986 		esc_url( 'http://' . $domain . $path )
   922 	);
   987 	);
   923 	wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
   988 	wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
   960 	 * @param string $user_login User login name.
  1025 	 * @param string $user_login User login name.
   961 	 * @param string $user_email User email address.
  1026 	 * @param string $user_email User email address.
   962 	 * @param string $key        Activation key created in wpmu_signup_user().
  1027 	 * @param string $key        Activation key created in wpmu_signup_user().
   963 	 * @param array  $meta       Signup meta data. Default empty array.
  1028 	 * @param array  $meta       Signup meta data. Default empty array.
   964 	 */
  1029 	 */
   965 	if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) )
  1030 	if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) {
   966 		return false;
  1031 		return false;
   967 
  1032 	}
   968 	$user = get_user_by( 'login', $user_login );
  1033 
       
  1034 	$user            = get_user_by( 'login', $user_login );
   969 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1035 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
   970 
  1036 
   971 	// Send email with activation link.
  1037 	// Send email with activation link.
   972 	$admin_email = get_site_option( 'admin_email' );
  1038 	$admin_email = get_site_option( 'admin_email' );
   973 	if ( $admin_email == '' )
  1039 	if ( $admin_email == '' ) {
   974 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1040 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
   975 	$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1041 	}
   976 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1042 	$from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
   977 	$message = sprintf(
  1043 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
       
  1044 	$message         = sprintf(
   978 		/**
  1045 		/**
   979 		 * Filters the content of the notification email for new user sign-up.
  1046 		 * Filters the content of the notification email for new user sign-up.
   980 		 *
  1047 		 *
   981 		 * Content should be formatted for transmission via wp_mail().
  1048 		 * Content should be formatted for transmission via wp_mail().
   982 		 *
  1049 		 *
   986 		 * @param string $user_login User login name.
  1053 		 * @param string $user_login User login name.
   987 		 * @param string $user_email User email address.
  1054 		 * @param string $user_email User email address.
   988 		 * @param string $key        Activation key created in wpmu_signup_user().
  1055 		 * @param string $key        Activation key created in wpmu_signup_user().
   989 		 * @param array  $meta       Signup meta data. Default empty array.
  1056 		 * @param array  $meta       Signup meta data. Default empty array.
   990 		 */
  1057 		 */
   991 		apply_filters( 'wpmu_signup_user_notification_email',
  1058 		apply_filters(
       
  1059 			'wpmu_signup_user_notification_email',
   992 			__( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
  1060 			__( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
   993 			$user_login, $user_email, $key, $meta
  1061 			$user_login,
       
  1062 			$user_email,
       
  1063 			$key,
       
  1064 			$meta
   994 		),
  1065 		),
   995 		site_url( "wp-activate.php?key=$key" )
  1066 		site_url( "wp-activate.php?key=$key" )
   996 	);
  1067 	);
   997 	// TODO: Don't hard code activation link.
  1068 	// TODO: Don't hard code activation link.
   998 	$subject = sprintf(
  1069 	$subject = sprintf(
  1005 		 * @param string $user_login User login name.
  1076 		 * @param string $user_login User login name.
  1006 		 * @param string $user_email User email address.
  1077 		 * @param string $user_email User email address.
  1007 		 * @param string $key        Activation key created in wpmu_signup_user().
  1078 		 * @param string $key        Activation key created in wpmu_signup_user().
  1008 		 * @param array  $meta       Signup meta data. Default empty array.
  1079 		 * @param array  $meta       Signup meta data. Default empty array.
  1009 		 */
  1080 		 */
  1010 		apply_filters( 'wpmu_signup_user_notification_subject',
  1081 		apply_filters(
       
  1082 			'wpmu_signup_user_notification_subject',
  1011 			/* translators: New user notification email subject. 1: Network name, 2: New user login */
  1083 			/* translators: New user notification email subject. 1: Network name, 2: New user login */
  1012 			_x( '[%1$s] Activate %2$s', 'New user notification email subject' ),
  1084 			_x( '[%1$s] Activate %2$s', 'New user notification email subject' ),
  1013 			$user_login, $user_email, $key, $meta
  1085 			$user_login,
       
  1086 			$user_email,
       
  1087 			$key,
       
  1088 			$meta
  1014 		),
  1089 		),
  1015 		$from_name,
  1090 		$from_name,
  1016 		$user_login
  1091 		$user_login
  1017 	);
  1092 	);
  1018 	wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1093 	wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1037  * @global wpdb $wpdb WordPress database abstraction object.
  1112  * @global wpdb $wpdb WordPress database abstraction object.
  1038  *
  1113  *
  1039  * @param string $key The activation key provided to the user.
  1114  * @param string $key The activation key provided to the user.
  1040  * @return array|WP_Error An array containing information about the activated user and/or blog
  1115  * @return array|WP_Error An array containing information about the activated user and/or blog
  1041  */
  1116  */
  1042 function wpmu_activate_signup($key) {
  1117 function wpmu_activate_signup( $key ) {
  1043 	global $wpdb;
  1118 	global $wpdb;
  1044 
  1119 
  1045 	$signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  1120 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
  1046 
  1121 
  1047 	if ( empty( $signup ) )
  1122 	if ( empty( $signup ) ) {
  1048 		return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
  1123 		return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
       
  1124 	}
  1049 
  1125 
  1050 	if ( $signup->active ) {
  1126 	if ( $signup->active ) {
  1051 		if ( empty( $signup->domain ) )
  1127 		if ( empty( $signup->domain ) ) {
  1052 			return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  1128 			return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
  1053 		else
  1129 		} else {
  1054 			return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  1130 			return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
  1055 	}
  1131 		}
  1056 
  1132 	}
  1057 	$meta = maybe_unserialize($signup->meta);
  1133 
       
  1134 	$meta     = maybe_unserialize( $signup->meta );
  1058 	$password = wp_generate_password( 12, false );
  1135 	$password = wp_generate_password( 12, false );
  1059 
  1136 
  1060 	$user_id = username_exists($signup->user_login);
  1137 	$user_id = username_exists( $signup->user_login );
  1061 
  1138 
  1062 	if ( ! $user_id )
  1139 	if ( ! $user_id ) {
  1063 		$user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
  1140 		$user_id = wpmu_create_user( $signup->user_login, $password, $signup->user_email );
  1064 	else
  1141 	} else {
  1065 		$user_already_exists = true;
  1142 		$user_already_exists = true;
  1066 
  1143 	}
  1067 	if ( ! $user_id )
  1144 
  1068 		return new WP_Error('create_user', __('Could not create user'), $signup);
  1145 	if ( ! $user_id ) {
  1069 
  1146 		return new WP_Error( 'create_user', __( 'Could not create user' ), $signup );
  1070 	$now = current_time('mysql', true);
  1147 	}
  1071 
  1148 
  1072 	if ( empty($signup->domain) ) {
  1149 	$now = current_time( 'mysql', true );
  1073 		$wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1150 
  1074 
  1151 	if ( empty( $signup->domain ) ) {
  1075 		if ( isset( $user_already_exists ) )
  1152 		$wpdb->update(
  1076 			return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  1153 			$wpdb->signups,
       
  1154 			array(
       
  1155 				'active'    => 1,
       
  1156 				'activated' => $now,
       
  1157 			),
       
  1158 			array( 'activation_key' => $key )
       
  1159 		);
       
  1160 
       
  1161 		if ( isset( $user_already_exists ) ) {
       
  1162 			return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup );
       
  1163 		}
  1077 
  1164 
  1078 		/**
  1165 		/**
  1079 		 * Fires immediately after a new user is activated.
  1166 		 * Fires immediately after a new user is activated.
  1080 		 *
  1167 		 *
  1081 		 * @since MU (3.0.0)
  1168 		 * @since MU (3.0.0)
  1083 		 * @param int   $user_id  User ID.
  1170 		 * @param int   $user_id  User ID.
  1084 		 * @param int   $password User password.
  1171 		 * @param int   $password User password.
  1085 		 * @param array $meta     Signup meta data.
  1172 		 * @param array $meta     Signup meta data.
  1086 		 */
  1173 		 */
  1087 		do_action( 'wpmu_activate_user', $user_id, $password, $meta );
  1174 		do_action( 'wpmu_activate_user', $user_id, $password, $meta );
  1088 		return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
  1175 		return array(
       
  1176 			'user_id'  => $user_id,
       
  1177 			'password' => $password,
       
  1178 			'meta'     => $meta,
       
  1179 		);
  1089 	}
  1180 	}
  1090 
  1181 
  1091 	$blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, get_current_network_id() );
  1182 	$blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, get_current_network_id() );
  1092 
  1183 
  1093 	// TODO: What to do if we create a user but cannot create a blog?
  1184 	// TODO: What to do if we create a user but cannot create a blog?
  1094 	if ( is_wp_error($blog_id) ) {
  1185 	if ( is_wp_error( $blog_id ) ) {
  1095 		// If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  1186 		// If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  1096 		// setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  1187 		// setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  1097 		if ( 'blog_taken' == $blog_id->get_error_code() ) {
  1188 		if ( 'blog_taken' == $blog_id->get_error_code() ) {
  1098 			$blog_id->add_data( $signup );
  1189 			$blog_id->add_data( $signup );
  1099 			$wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  1190 			$wpdb->update(
       
  1191 				$wpdb->signups,
       
  1192 				array(
       
  1193 					'active'    => 1,
       
  1194 					'activated' => $now,
       
  1195 				),
       
  1196 				array( 'activation_key' => $key )
       
  1197 			);
  1100 		}
  1198 		}
  1101 		return $blog_id;
  1199 		return $blog_id;
  1102 	}
  1200 	}
  1103 
  1201 
  1104 	$wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1202 	$wpdb->update(
       
  1203 		$wpdb->signups,
       
  1204 		array(
       
  1205 			'active'    => 1,
       
  1206 			'activated' => $now,
       
  1207 		),
       
  1208 		array( 'activation_key' => $key )
       
  1209 	);
  1105 	/**
  1210 	/**
  1106 	 * Fires immediately after a site is activated.
  1211 	 * Fires immediately after a site is activated.
  1107 	 *
  1212 	 *
  1108 	 * @since MU (3.0.0)
  1213 	 * @since MU (3.0.0)
  1109 	 *
  1214 	 *
  1113 	 * @param string $signup_title  Site title.
  1218 	 * @param string $signup_title  Site title.
  1114 	 * @param array  $meta          Signup meta data. By default, contains the requested privacy setting and lang_id.
  1219 	 * @param array  $meta          Signup meta data. By default, contains the requested privacy setting and lang_id.
  1115 	 */
  1220 	 */
  1116 	do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
  1221 	do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
  1117 
  1222 
  1118 	return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  1223 	return array(
       
  1224 		'blog_id'  => $blog_id,
       
  1225 		'user_id'  => $user_id,
       
  1226 		'password' => $password,
       
  1227 		'title'    => $signup->title,
       
  1228 		'meta'     => $meta,
       
  1229 	);
  1119 }
  1230 }
  1120 
  1231 
  1121 /**
  1232 /**
  1122  * Create a user.
  1233  * Create a user.
  1123  *
  1234  *
  1135  */
  1246  */
  1136 function wpmu_create_user( $user_name, $password, $email ) {
  1247 function wpmu_create_user( $user_name, $password, $email ) {
  1137 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  1248 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  1138 
  1249 
  1139 	$user_id = wp_create_user( $user_name, $password, $email );
  1250 	$user_id = wp_create_user( $user_name, $password, $email );
  1140 	if ( is_wp_error( $user_id ) )
  1251 	if ( is_wp_error( $user_id ) ) {
  1141 		return false;
  1252 		return false;
       
  1253 	}
  1142 
  1254 
  1143 	// Newly created users have no roles or caps until they are added to a blog.
  1255 	// Newly created users have no roles or caps until they are added to a blog.
  1144 	delete_user_option( $user_id, 'capabilities' );
  1256 	delete_user_option( $user_id, 'capabilities' );
  1145 	delete_user_option( $user_id, 'user_level' );
  1257 	delete_user_option( $user_id, 'user_level' );
  1146 
  1258 
  1172  *
  1284  *
  1173  * @param string $domain     The new site's domain.
  1285  * @param string $domain     The new site's domain.
  1174  * @param string $path       The new site's path.
  1286  * @param string $path       The new site's path.
  1175  * @param string $title      The new site's title.
  1287  * @param string $title      The new site's title.
  1176  * @param int    $user_id    The user ID of the new site's admin.
  1288  * @param int    $user_id    The user ID of the new site's admin.
  1177  * @param array  $meta       Optional. Array of key=>value pairs used to set initial site options.
  1289  * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
  1178  *                           If valid status keys are included ('public', 'archived', 'mature',
  1290  *                           If valid status keys are included ('public', 'archived', 'mature',
  1179  *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
  1291  *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
  1180  *                           updated. Otherwise, keys and values will be used to set options for
  1292  *                           updated. Otherwise, keys and values will be used to set options for
  1181  *                           the new site. Default empty array.
  1293  *                           the new site. Default empty array.
  1182  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
  1294  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
  1183  * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  1295  * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  1184  */
  1296  */
  1185 function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) {
  1297 function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
  1186 	$defaults = array(
  1298 	$defaults = array(
  1187 		'public' => 0,
  1299 		'public' => 0,
  1188 		'WPLANG' => get_network_option( $network_id, 'WPLANG' ),
       
  1189 	);
  1300 	);
  1190 	$meta = wp_parse_args( $meta, $defaults );
  1301 	$options  = wp_parse_args( $options, $defaults );
  1191 
  1302 
  1192 	$domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
  1303 	$title   = strip_tags( $title );
  1193 
       
  1194 	if ( is_subdomain_install() )
       
  1195 		$domain = str_replace( '@', '', $domain );
       
  1196 
       
  1197 	$title = strip_tags( $title );
       
  1198 	$user_id = (int) $user_id;
  1304 	$user_id = (int) $user_id;
  1199 
  1305 
  1200 	if ( empty($path) )
       
  1201 		$path = '/';
       
  1202 
       
  1203 	// Check if the domain has been used already. We should return an error message.
  1306 	// Check if the domain has been used already. We should return an error message.
  1204 	if ( domain_exists($domain, $path, $network_id) )
  1307 	if ( domain_exists( $domain, $path, $network_id ) ) {
  1205 		return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
  1308 		return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
       
  1309 	}
  1206 
  1310 
  1207 	if ( ! wp_installing() ) {
  1311 	if ( ! wp_installing() ) {
  1208 		wp_installing( true );
  1312 		wp_installing( true );
  1209 	}
  1313 	}
  1210 
  1314 
  1211 	if ( ! $blog_id = insert_blog($domain, $path, $network_id) )
  1315 	$site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  1212 		return new WP_Error('insert_blog', __('Could not create site.'));
  1316 
  1213 
  1317 	$site_data = array_merge(
  1214 	switch_to_blog($blog_id);
  1318 		array(
  1215 	install_blog($blog_id, $title);
  1319 			'domain'     => $domain,
  1216 	wp_install_defaults($user_id);
  1320 			'path'       => $path,
  1217 
  1321 			'network_id' => $network_id,
  1218 	add_user_to_blog($blog_id, $user_id, 'administrator');
  1322 		),
  1219 
  1323 		array_intersect_key( $options, array_flip( $site_data_whitelist ) )
  1220 	foreach ( $meta as $key => $value ) {
  1324 	);
  1221 		if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
  1325 
  1222 			update_blog_status( $blog_id, $key, $value );
  1326 	// Data to pass to wp_initialize_site().
  1223 		else
  1327 	$site_initialization_data = array(
  1224 			update_option( $key, $value );
  1328 		'title'   => $title,
  1225 	}
  1329 		'user_id' => $user_id,
  1226 
  1330 		'options' => array_diff_key( $options, array_flip( $site_data_whitelist ) ),
  1227 	update_option( 'blog_public', (int) $meta['public'] );
  1331 	);
  1228 
  1332 
  1229 	if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
  1333 	$blog_id = wp_insert_site( array_merge( $site_data, $site_initialization_data ) );
  1230 		update_user_meta( $user_id, 'primary_blog', $blog_id );
  1334 
  1231 
  1335 	if ( is_wp_error( $blog_id ) ) {
  1232 	restore_current_blog();
  1336 		return $blog_id;
  1233 	/**
  1337 	}
  1234 	 * Fires immediately after a new site is created.
       
  1235 	 *
       
  1236 	 * @since MU (3.0.0)
       
  1237 	 *
       
  1238 	 * @param int    $blog_id    Site ID.
       
  1239 	 * @param int    $user_id    User ID.
       
  1240 	 * @param string $domain     Site domain.
       
  1241 	 * @param string $path       Site path.
       
  1242 	 * @param int    $network_id Network ID. Only relevant on multi-network installations.
       
  1243 	 * @param array  $meta       Meta data. Used to set initial site options.
       
  1244 	 */
       
  1245 	do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta );
       
  1246 
  1338 
  1247 	wp_cache_set( 'last_changed', microtime(), 'sites' );
  1339 	wp_cache_set( 'last_changed', microtime(), 'sites' );
  1248 
  1340 
  1249 	return $blog_id;
  1341 	return $blog_id;
  1250 }
  1342 }
  1254  *
  1346  *
  1255  * Filter {@see 'newblog_notify_siteadmin'} to change the content of
  1347  * Filter {@see 'newblog_notify_siteadmin'} to change the content of
  1256  * the notification email.
  1348  * the notification email.
  1257  *
  1349  *
  1258  * @since MU (3.0.0)
  1350  * @since MU (3.0.0)
  1259  *
  1351  * @since 5.1.0 $blog_id now supports input from the {@see 'wp_initialize_site'} action.
  1260  * @param int    $blog_id    The new site's ID.
  1352  *
  1261  * @param string $deprecated Not used.
  1353  * @param WP_Site|int $blog_id    The new site's object or ID.
       
  1354  * @param string      $deprecated Not used.
  1262  * @return bool
  1355  * @return bool
  1263  */
  1356  */
  1264 function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1357 function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1265 	if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1358 	if ( is_object( $blog_id ) ) {
       
  1359 		$blog_id = $blog_id->blog_id;
       
  1360 	}
       
  1361 
       
  1362 	if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
  1266 		return false;
  1363 		return false;
       
  1364 	}
  1267 
  1365 
  1268 	$email = get_site_option( 'admin_email' );
  1366 	$email = get_site_option( 'admin_email' );
  1269 	if ( is_email($email) == false )
  1367 	if ( is_email( $email ) == false ) {
  1270 		return false;
  1368 		return false;
  1271 
  1369 	}
  1272 	$options_site_url = esc_url(network_admin_url('settings.php'));
  1370 
       
  1371 	$options_site_url = esc_url( network_admin_url( 'settings.php' ) );
  1273 
  1372 
  1274 	switch_to_blog( $blog_id );
  1373 	switch_to_blog( $blog_id );
  1275 	$blogname = get_option( 'blogname' );
  1374 	$blogname = get_option( 'blogname' );
  1276 	$siteurl = site_url();
  1375 	$siteurl  = site_url();
  1277 	restore_current_blog();
  1376 	restore_current_blog();
  1278 
  1377 
  1279 	/* translators: New site notification email. 1: Site URL, 2: User IP address, 3: Settings screen URL */
  1378 	/* translators: New site notification email. 1: Site URL, 2: User IP address, 3: Settings screen URL */
  1280 	$msg = sprintf( __( 'New Site: %1$s
  1379 	$msg = sprintf(
       
  1380 		__(
       
  1381 			'New Site: %1$s
  1281 URL: %2$s
  1382 URL: %2$s
  1282 Remote IP address: %3$s
  1383 Remote IP address: %3$s
  1283 
  1384 
  1284 Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1385 Disable these notifications: %4$s'
       
  1386 		),
       
  1387 		$blogname,
       
  1388 		$siteurl,
       
  1389 		wp_unslash( $_SERVER['REMOTE_ADDR'] ),
       
  1390 		$options_site_url
       
  1391 	);
  1285 	/**
  1392 	/**
  1286 	 * Filters the message body of the new site activation email sent
  1393 	 * Filters the message body of the new site activation email sent
  1287 	 * to the network administrator.
  1394 	 * to the network administrator.
  1288 	 *
  1395 	 *
  1289 	 * @since MU (3.0.0)
  1396 	 * @since MU (3.0.0)
  1306  *
  1413  *
  1307  * @param int $user_id The new user's ID.
  1414  * @param int $user_id The new user's ID.
  1308  * @return bool
  1415  * @return bool
  1309  */
  1416  */
  1310 function newuser_notify_siteadmin( $user_id ) {
  1417 function newuser_notify_siteadmin( $user_id ) {
  1311 	if ( get_site_option( 'registrationnotification' ) != 'yes' )
  1418 	if ( get_site_option( 'registrationnotification' ) != 'yes' ) {
  1312 		return false;
  1419 		return false;
       
  1420 	}
  1313 
  1421 
  1314 	$email = get_site_option( 'admin_email' );
  1422 	$email = get_site_option( 'admin_email' );
  1315 
  1423 
  1316 	if ( is_email($email) == false )
  1424 	if ( is_email( $email ) == false ) {
  1317 		return false;
  1425 		return false;
       
  1426 	}
  1318 
  1427 
  1319 	$user = get_userdata( $user_id );
  1428 	$user = get_userdata( $user_id );
  1320 
  1429 
  1321 	$options_site_url = esc_url(network_admin_url('settings.php'));
  1430 	$options_site_url = esc_url( network_admin_url( 'settings.php' ) );
  1322 	/* translators: New user notification email. 1: User login, 2: User IP address, 3: Settings screen URL */
  1431 	/* translators: New user notification email. 1: User login, 2: User IP address, 3: Settings screen URL */
  1323 	$msg = sprintf(__('New User: %1$s
  1432 	$msg = sprintf(
       
  1433 		__(
       
  1434 			'New User: %1$s
  1324 Remote IP address: %2$s
  1435 Remote IP address: %2$s
  1325 
  1436 
  1326 Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
  1437 Disable these notifications: %3$s'
       
  1438 		),
       
  1439 		$user->user_login,
       
  1440 		wp_unslash( $_SERVER['REMOTE_ADDR'] ),
       
  1441 		$options_site_url
       
  1442 	);
  1327 
  1443 
  1328 	/**
  1444 	/**
  1329 	 * Filters the message body of the new user activation email sent
  1445 	 * Filters the message body of the new user activation email sent
  1330 	 * to the network administrator.
  1446 	 * to the network administrator.
  1331 	 *
  1447 	 *
  1333 	 *
  1449 	 *
  1334 	 * @param string  $msg  Email body.
  1450 	 * @param string  $msg  Email body.
  1335 	 * @param WP_User $user WP_User instance of the new user.
  1451 	 * @param WP_User $user WP_User instance of the new user.
  1336 	 */
  1452 	 */
  1337 	$msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  1453 	$msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
  1338 	wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
  1454 	wp_mail( $email, sprintf( __( 'New User Registration: %s' ), $user->user_login ), $msg );
  1339 	return true;
  1455 	return true;
  1340 }
  1456 }
  1341 
  1457 
  1342 /**
  1458 /**
  1343  * Checks whether a site name is already taken.
  1459  * Checks whether a site name is already taken.
  1354  * @param string $path       The path to be checked.
  1470  * @param string $path       The path to be checked.
  1355  * @param int    $network_id Optional. Network ID. Relevant only on multi-network installations.
  1471  * @param int    $network_id Optional. Network ID. Relevant only on multi-network installations.
  1356  * @return int|null The site ID if the site name exists, null otherwise.
  1472  * @return int|null The site ID if the site name exists, null otherwise.
  1357  */
  1473  */
  1358 function domain_exists( $domain, $path, $network_id = 1 ) {
  1474 function domain_exists( $domain, $path, $network_id = 1 ) {
  1359 	$path = trailingslashit( $path );
  1475 	$path   = trailingslashit( $path );
  1360 	$args = array(
  1476 	$args   = array(
  1361 		'network_id' => $network_id,
  1477 		'network_id'             => $network_id,
  1362 		'domain'     => $domain,
  1478 		'domain'                 => $domain,
  1363 		'path'       => $path,
  1479 		'path'                   => $path,
  1364 		'fields'     => 'ids',
  1480 		'fields'                 => 'ids',
  1365 		'number'     => 1,
  1481 		'number'                 => 1,
       
  1482 		'update_site_meta_cache' => false,
  1366 	);
  1483 	);
  1367 	$result = get_sites( $args );
  1484 	$result = get_sites( $args );
  1368 	$result = array_shift( $result );
  1485 	$result = array_shift( $result );
  1369 
  1486 
  1370 	/**
  1487 	/**
  1379 	 * @param string   $domain     Domain to be checked.
  1496 	 * @param string   $domain     Domain to be checked.
  1380 	 * @param string   $path       Path to be checked.
  1497 	 * @param string   $path       Path to be checked.
  1381 	 * @param int      $network_id Network ID. Relevant only on multi-network installations.
  1498 	 * @param int      $network_id Network ID. Relevant only on multi-network installations.
  1382 	 */
  1499 	 */
  1383 	return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
  1500 	return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
  1384 }
       
  1385 
       
  1386 /**
       
  1387  * Store basic site info in the blogs table.
       
  1388  *
       
  1389  * This function creates a row in the wp_blogs table and returns
       
  1390  * the new blog's ID. It is the first step in creating a new blog.
       
  1391  *
       
  1392  * @since MU (3.0.0)
       
  1393  *
       
  1394  * @global wpdb $wpdb WordPress database abstraction object.
       
  1395  *
       
  1396  * @param string $domain     The domain of the new site.
       
  1397  * @param string $path       The path of the new site.
       
  1398  * @param int    $network_id Unless you're running a multi-network installation, be sure to set this value to 1.
       
  1399  * @return int|false The ID of the new row
       
  1400  */
       
  1401 function insert_blog($domain, $path, $network_id) {
       
  1402 	global $wpdb;
       
  1403 
       
  1404 	$path = trailingslashit($path);
       
  1405 	$network_id = (int) $network_id;
       
  1406 
       
  1407 	$result = $wpdb->insert( $wpdb->blogs, array('site_id' => $network_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
       
  1408 	if ( ! $result )
       
  1409 		return false;
       
  1410 
       
  1411 	$blog_id = $wpdb->insert_id;
       
  1412 	clean_blog_cache( $blog_id );
       
  1413 
       
  1414 	wp_maybe_update_network_site_counts( $network_id );
       
  1415 
       
  1416 	return $blog_id;
       
  1417 }
       
  1418 
       
  1419 /**
       
  1420  * Install an empty blog.
       
  1421  *
       
  1422  * Creates the new blog tables and options. If calling this function
       
  1423  * directly, be sure to use switch_to_blog() first, so that $wpdb
       
  1424  * points to the new blog.
       
  1425  *
       
  1426  * @since MU (3.0.0)
       
  1427  *
       
  1428  * @global wpdb     $wpdb
       
  1429  * @global WP_Roles $wp_roles
       
  1430  *
       
  1431  * @param int    $blog_id    The value returned by insert_blog().
       
  1432  * @param string $blog_title The title of the new site.
       
  1433  */
       
  1434 function install_blog( $blog_id, $blog_title = '' ) {
       
  1435 	global $wpdb, $wp_roles;
       
  1436 
       
  1437 	// Cast for security
       
  1438 	$blog_id = (int) $blog_id;
       
  1439 
       
  1440 	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
       
  1441 
       
  1442 	$suppress = $wpdb->suppress_errors();
       
  1443 	if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
       
  1444 		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>' );
       
  1445 	$wpdb->suppress_errors( $suppress );
       
  1446 
       
  1447 	$url = get_blogaddress_by_id( $blog_id );
       
  1448 
       
  1449 	// Set everything up
       
  1450 	make_db_current_silent( 'blog' );
       
  1451 	populate_options();
       
  1452 	populate_roles();
       
  1453 
       
  1454 	// populate_roles() clears previous role definitions so we start over.
       
  1455 	$wp_roles = new WP_Roles();
       
  1456 
       
  1457 	$siteurl = $home = untrailingslashit( $url );
       
  1458 
       
  1459 	if ( ! is_subdomain_install() ) {
       
  1460 
       
  1461  		if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
       
  1462  			$siteurl = set_url_scheme( $siteurl, 'https' );
       
  1463  		}
       
  1464  		if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
       
  1465  			$home = set_url_scheme( $home, 'https' );
       
  1466  		}
       
  1467 
       
  1468 	}
       
  1469 
       
  1470 	update_option( 'siteurl', $siteurl );
       
  1471 	update_option( 'home', $home );
       
  1472 
       
  1473 	if ( get_site_option( 'ms_files_rewriting' ) )
       
  1474 		update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
       
  1475 	else
       
  1476 		update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
       
  1477 
       
  1478 	update_option( 'blogname', wp_unslash( $blog_title ) );
       
  1479 	update_option( 'admin_email', '' );
       
  1480 
       
  1481 	// remove all perms
       
  1482 	$table_prefix = $wpdb->get_blog_prefix();
       
  1483 	delete_metadata( 'user', 0, $table_prefix . 'user_level',   null, true ); // delete all
       
  1484 	delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
       
  1485 }
       
  1486 
       
  1487 /**
       
  1488  * Set blog defaults.
       
  1489  *
       
  1490  * This function creates a row in the wp_blogs table.
       
  1491  *
       
  1492  * @since MU (3.0.0)
       
  1493  * @deprecated MU
       
  1494  * @deprecated Use wp_install_defaults()
       
  1495  *
       
  1496  * @global wpdb $wpdb WordPress database abstraction object.
       
  1497  *
       
  1498  * @param int $blog_id Ignored in this function.
       
  1499  * @param int $user_id
       
  1500  */
       
  1501 function install_blog_defaults($blog_id, $user_id) {
       
  1502 	global $wpdb;
       
  1503 
       
  1504 	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
       
  1505 
       
  1506 	$suppress = $wpdb->suppress_errors();
       
  1507 
       
  1508 	wp_install_defaults($user_id);
       
  1509 
       
  1510 	$wpdb->suppress_errors( $suppress );
       
  1511 }
  1501 }
  1512 
  1502 
  1513 /**
  1503 /**
  1514  * Notify a user that their blog activation has been successful.
  1504  * Notify a user that their blog activation has been successful.
  1515  *
  1505  *
  1541 	 * @param int      $user_id  User ID.
  1531 	 * @param int      $user_id  User ID.
  1542 	 * @param string   $password User password.
  1532 	 * @param string   $password User password.
  1543 	 * @param string   $title    Site title.
  1533 	 * @param string   $title    Site title.
  1544 	 * @param array    $meta     Signup meta data. By default, contains the requested privacy setting and lang_id.
  1534 	 * @param array    $meta     Signup meta data. By default, contains the requested privacy setting and lang_id.
  1545 	 */
  1535 	 */
  1546 	if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
  1536 	if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) ) {
  1547 		return false;
  1537 		return false;
       
  1538 	}
  1548 
  1539 
  1549 	$user = get_userdata( $user_id );
  1540 	$user = get_userdata( $user_id );
  1550 
  1541 
  1551 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1542 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1552 
  1543 
  1553 	$welcome_email = get_site_option( 'welcome_email' );
  1544 	$welcome_email = get_site_option( 'welcome_email' );
  1554 	if ( $welcome_email == false ) {
  1545 	if ( $welcome_email == false ) {
  1555 		/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1546 		/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1556 		$welcome_email = __( 'Howdy USERNAME,
  1547 		$welcome_email = __(
       
  1548 			'Howdy USERNAME,
  1557 
  1549 
  1558 Your new SITE_NAME site has been successfully set up at:
  1550 Your new SITE_NAME site has been successfully set up at:
  1559 BLOG_URL
  1551 BLOG_URL
  1560 
  1552 
  1561 You can log in to the administrator account with the following information:
  1553 You can log in to the administrator account with the following information:
  1564 Password: PASSWORD
  1556 Password: PASSWORD
  1565 Log in here: BLOG_URLwp-login.php
  1557 Log in here: BLOG_URLwp-login.php
  1566 
  1558 
  1567 We hope you enjoy your new site. Thanks!
  1559 We hope you enjoy your new site. Thanks!
  1568 
  1560 
  1569 --The Team @ SITE_NAME' );
  1561 --The Team @ SITE_NAME'
  1570 	}
  1562 		);
  1571 
  1563 	}
  1572 	$url = get_blogaddress_by_id($blog_id);
  1564 
       
  1565 	$url = get_blogaddress_by_id( $blog_id );
  1573 
  1566 
  1574 	$welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1567 	$welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
  1575 	$welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1568 	$welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
  1576 	$welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1569 	$welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
  1577 	$welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1570 	$welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
  1590 	 * @param string $password      User password.
  1583 	 * @param string $password      User password.
  1591 	 * @param string $title         Site title.
  1584 	 * @param string $title         Site title.
  1592 	 * @param array  $meta          Signup meta data. By default, contains the requested privacy setting and lang_id.
  1585 	 * @param array  $meta          Signup meta data. By default, contains the requested privacy setting and lang_id.
  1593 	 */
  1586 	 */
  1594 	$welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
  1587 	$welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
  1595 	$admin_email = get_site_option( 'admin_email' );
  1588 	$admin_email   = get_site_option( 'admin_email' );
  1596 
  1589 
  1597 	if ( $admin_email == '' )
  1590 	if ( $admin_email == '' ) {
  1598 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1591 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1599 
  1592 	}
  1600 	$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1593 
  1601 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1594 	$from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1602 	$message = $welcome_email;
  1595 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
  1603 
  1596 	$message         = $welcome_email;
  1604 	if ( empty( $current_network->site_name ) )
  1597 
       
  1598 	if ( empty( $current_network->site_name ) ) {
  1605 		$current_network->site_name = 'WordPress';
  1599 		$current_network->site_name = 'WordPress';
       
  1600 	}
  1606 
  1601 
  1607 	/* translators: New site notification email subject. 1: Network name, 2: New site name */
  1602 	/* translators: New site notification email subject. 1: Network name, 2: New site name */
  1608 	$subject = __( 'New %1$s Site: %2$s' );
  1603 	$subject = __( 'New %1$s Site: %2$s' );
  1609 
  1604 
  1610 	/**
  1605 	/**
  1641  */
  1636  */
  1642 function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
  1637 function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
  1643 	$current_network = get_network();
  1638 	$current_network = get_network();
  1644 
  1639 
  1645 	/**
  1640 	/**
  1646  	 * Filters whether to bypass the welcome email after user activation.
  1641 	 * Filters whether to bypass the welcome email after user activation.
  1647 	 *
  1642 	 *
  1648 	 * Returning false disables the welcome email.
  1643 	 * Returning false disables the welcome email.
  1649 	 *
  1644 	 *
  1650 	 * @since MU (3.0.0)
  1645 	 * @since MU (3.0.0)
  1651 	 *
  1646 	 *
  1652 	 * @param int    $user_id  User ID.
  1647 	 * @param int    $user_id  User ID.
  1653 	 * @param string $password User password.
  1648 	 * @param string $password User password.
  1654 	 * @param array  $meta     Signup meta data. Default empty array.
  1649 	 * @param array  $meta     Signup meta data. Default empty array.
  1655 	 */
  1650 	 */
  1656 	if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
  1651 	if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) ) {
  1657 		return false;
  1652 		return false;
       
  1653 	}
  1658 
  1654 
  1659 	$welcome_email = get_site_option( 'welcome_user_email' );
  1655 	$welcome_email = get_site_option( 'welcome_user_email' );
  1660 
  1656 
  1661 	$user = get_userdata( $user_id );
  1657 	$user = get_userdata( $user_id );
  1662 
  1658 
  1680 	$welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1676 	$welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
  1681 	$welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1677 	$welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
  1682 
  1678 
  1683 	$admin_email = get_site_option( 'admin_email' );
  1679 	$admin_email = get_site_option( 'admin_email' );
  1684 
  1680 
  1685 	if ( $admin_email == '' )
  1681 	if ( $admin_email == '' ) {
  1686 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1682 		$admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1687 
  1683 	}
  1688 	$from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1684 
  1689 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1685 	$from_name       = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
  1690 	$message = $welcome_email;
  1686 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
  1691 
  1687 	$message         = $welcome_email;
  1692 	if ( empty( $current_network->site_name ) )
  1688 
       
  1689 	if ( empty( $current_network->site_name ) ) {
  1693 		$current_network->site_name = 'WordPress';
  1690 		$current_network->site_name = 'WordPress';
       
  1691 	}
  1694 
  1692 
  1695 	/* translators: New user notification email subject. 1: Network name, 2: New user login */
  1693 	/* translators: New user notification email subject. 1: Network name, 2: New user login */
  1696 	$subject = __( 'New %1$s User: %2$s' );
  1694 	$subject = __( 'New %1$s User: %2$s' );
  1697 
  1695 
  1698 	/**
  1696 	/**
  1700 	 *
  1698 	 *
  1701 	 * @since MU (3.0.0)
  1699 	 * @since MU (3.0.0)
  1702 	 *
  1700 	 *
  1703 	 * @param string $subject Subject of the email.
  1701 	 * @param string $subject Subject of the email.
  1704 	 */
  1702 	 */
  1705 	$subject = apply_filters( 'update_welcome_user_subject', sprintf( $subject, $current_network->site_name, $user->user_login) );
  1703 	$subject = apply_filters( 'update_welcome_user_subject', sprintf( $subject, $current_network->site_name, $user->user_login ) );
  1706 	wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1704 	wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
  1707 
  1705 
  1708 	if ( $switched_locale ) {
  1706 	if ( $switched_locale ) {
  1709 		restore_previous_locale();
  1707 		restore_previous_locale();
  1710 	}
  1708 	}
  1745  * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1743  * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1746  */
  1744  */
  1747 function get_most_recent_post_of_user( $user_id ) {
  1745 function get_most_recent_post_of_user( $user_id ) {
  1748 	global $wpdb;
  1746 	global $wpdb;
  1749 
  1747 
  1750 	$user_blogs = get_blogs_of_user( (int) $user_id );
  1748 	$user_blogs       = get_blogs_of_user( (int) $user_id );
  1751 	$most_recent_post = array();
  1749 	$most_recent_post = array();
  1752 
  1750 
  1753 	// Walk through each blog and get the most recent post
  1751 	// Walk through each blog and get the most recent post
  1754 	// published by $user_id
  1752 	// published by $user_id
  1755 	foreach ( (array) $user_blogs as $blog ) {
  1753 	foreach ( (array) $user_blogs as $blog ) {
  1756 		$prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
  1754 		$prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
  1757 		$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);
  1755 		$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 );
  1758 
  1756 
  1759 		// Make sure we found a post
  1757 		// Make sure we found a post
  1760 		if ( isset($recent_post['ID']) ) {
  1758 		if ( isset( $recent_post['ID'] ) ) {
  1761 			$post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1759 			$post_gmt_ts = strtotime( $recent_post['post_date_gmt'] );
  1762 
  1760 
  1763 			// If this is the first post checked or if this post is
  1761 			// If this is the first post checked or if this post is
  1764 			// newer than the current recent post, make it the new
  1762 			// newer than the current recent post, make it the new
  1765 			// most recent post.
  1763 			// most recent post.
  1766 			if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1764 			if ( ! isset( $most_recent_post['post_gmt_ts'] ) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
  1767 				$most_recent_post = array(
  1765 				$most_recent_post = array(
  1768 					'blog_id'		=> $blog->userblog_id,
  1766 					'blog_id'       => $blog->userblog_id,
  1769 					'post_id'		=> $recent_post['ID'],
  1767 					'post_id'       => $recent_post['ID'],
  1770 					'post_date_gmt'	=> $recent_post['post_date_gmt'],
  1768 					'post_date_gmt' => $recent_post['post_date_gmt'],
  1771 					'post_gmt_ts'	=> $post_gmt_ts
  1769 					'post_gmt_ts'   => $post_gmt_ts,
  1772 				);
  1770 				);
  1773 			}
  1771 			}
  1774 		}
  1772 		}
  1775 	}
  1773 	}
  1776 
  1774 
  1777 	return $most_recent_post;
  1775 	return $most_recent_post;
  1778 }
  1776 }
  1779 
  1777 
  1780 // Misc functions
  1778 // Misc functions
  1781 
       
  1782 /**
       
  1783  * Get the size of a directory.
       
  1784  *
       
  1785  * A helper function that is used primarily to check whether
       
  1786  * a blog has exceeded its allowed upload space.
       
  1787  *
       
  1788  * @since MU (3.0.0)
       
  1789  *
       
  1790  * @param string $directory Full path of a directory.
       
  1791  * @return int Size of the directory in MB.
       
  1792  */
       
  1793 function get_dirsize( $directory ) {
       
  1794 	$dirsize = get_transient( 'dirsize_cache' );
       
  1795 	if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
       
  1796 		return $dirsize[ $directory ][ 'size' ];
       
  1797 
       
  1798 	if ( ! is_array( $dirsize ) )
       
  1799 		$dirsize = array();
       
  1800 
       
  1801 	// Exclude individual site directories from the total when checking the main site,
       
  1802 	// as they are subdirectories and should not be counted.
       
  1803 	if ( is_main_site() ) {
       
  1804 		$dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory, $directory . '/sites' );
       
  1805 	} else {
       
  1806 		$dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
       
  1807 	}
       
  1808 
       
  1809 	set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
       
  1810 	return $dirsize[ $directory ][ 'size' ];
       
  1811 }
       
  1812 
       
  1813 /**
       
  1814  * Get the size of a directory recursively.
       
  1815  *
       
  1816  * Used by get_dirsize() to get a directory's size when it contains
       
  1817  * other directories.
       
  1818  *
       
  1819  * @since MU (3.0.0)
       
  1820  * @since 4.3.0 $exclude parameter added.
       
  1821  *
       
  1822  * @param string $directory Full path of a directory.
       
  1823  * @param string $exclude   Optional. Full path of a subdirectory to exclude from the total.
       
  1824  * @return int|false Size in MB if a valid directory. False if not.
       
  1825  */
       
  1826 function recurse_dirsize( $directory, $exclude = null ) {
       
  1827 	$size = 0;
       
  1828 
       
  1829 	$directory = untrailingslashit( $directory );
       
  1830 
       
  1831 	if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
       
  1832 		return false;
       
  1833 	}
       
  1834 
       
  1835 	if ($handle = opendir($directory)) {
       
  1836 		while(($file = readdir($handle)) !== false) {
       
  1837 			$path = $directory.'/'.$file;
       
  1838 			if ($file != '.' && $file != '..') {
       
  1839 				if (is_file($path)) {
       
  1840 					$size += filesize($path);
       
  1841 				} elseif (is_dir($path)) {
       
  1842 					$handlesize = recurse_dirsize( $path, $exclude );
       
  1843 					if ($handlesize > 0)
       
  1844 						$size += $handlesize;
       
  1845 				}
       
  1846 			}
       
  1847 		}
       
  1848 		closedir($handle);
       
  1849 	}
       
  1850 	return $size;
       
  1851 }
       
  1852 
  1779 
  1853 /**
  1780 /**
  1854  * Check an array of MIME types against a whitelist.
  1781  * Check an array of MIME types against a whitelist.
  1855  *
  1782  *
  1856  * WordPress ships with a set of allowed upload filetypes,
  1783  * WordPress ships with a set of allowed upload filetypes,
  1863  *
  1790  *
  1864  * @param array $mimes
  1791  * @param array $mimes
  1865  * @return array
  1792  * @return array
  1866  */
  1793  */
  1867 function check_upload_mimes( $mimes ) {
  1794 function check_upload_mimes( $mimes ) {
  1868 	$site_exts = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1795 	$site_exts  = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1869 	$site_mimes = array();
  1796 	$site_mimes = array();
  1870 	foreach ( $site_exts as $ext ) {
  1797 	foreach ( $site_exts as $ext ) {
  1871 		foreach ( $mimes as $ext_pattern => $mime ) {
  1798 		foreach ( $mimes as $ext_pattern => $mime ) {
  1872 			if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
  1799 			if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false ) {
  1873 				$site_mimes[$ext_pattern] = $mime;
  1800 				$site_mimes[ $ext_pattern ] = $mime;
       
  1801 			}
  1874 		}
  1802 		}
  1875 	}
  1803 	}
  1876 	return $site_mimes;
  1804 	return $site_mimes;
  1877 }
  1805 }
  1878 
  1806 
  1897 
  1825 
  1898 /**
  1826 /**
  1899  * Logs the user email, IP, and registration date of a new site.
  1827  * Logs the user email, IP, and registration date of a new site.
  1900  *
  1828  *
  1901  * @since MU (3.0.0)
  1829  * @since MU (3.0.0)
       
  1830  * @since 5.1.0 Parameters now support input from the {@see 'wp_initialize_site'} action.
  1902  *
  1831  *
  1903  * @global wpdb $wpdb WordPress database abstraction object.
  1832  * @global wpdb $wpdb WordPress database abstraction object.
  1904  *
  1833  *
  1905  * @param int $blog_id
  1834  * @param WP_Site|int $blog_id The new site's object or ID.
  1906  * @param int $user_id
  1835  * @param int|array   $user_id User ID, or array of arguments including 'user_id'.
  1907  */
  1836  */
  1908 function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1837 function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1909 	global $wpdb;
  1838 	global $wpdb;
       
  1839 
       
  1840 	if ( is_object( $blog_id ) ) {
       
  1841 		$blog_id = $blog_id->blog_id;
       
  1842 	}
       
  1843 
       
  1844 	if ( is_array( $user_id ) ) {
       
  1845 		$user_id = ! empty( $user_id['user_id'] ) ? $user_id['user_id'] : 0;
       
  1846 	}
       
  1847 
  1910 	$user = get_userdata( (int) $user_id );
  1848 	$user = get_userdata( (int) $user_id );
  1911 	if ( $user )
  1849 	if ( $user ) {
  1912 		$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')) );
  1850 		$wpdb->insert(
       
  1851 			$wpdb->registration_log,
       
  1852 			array(
       
  1853 				'email'           => $user->user_email,
       
  1854 				'IP'              => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ),
       
  1855 				'blog_id'         => $blog_id,
       
  1856 				'date_registered' => current_time( 'mysql' ),
       
  1857 			)
       
  1858 		);
       
  1859 	}
  1913 }
  1860 }
  1914 
  1861 
  1915 /**
  1862 /**
  1916  * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1863  * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
  1917  *
  1864  *
  1928  */
  1875  */
  1929 function global_terms( $term_id, $deprecated = '' ) {
  1876 function global_terms( $term_id, $deprecated = '' ) {
  1930 	global $wpdb;
  1877 	global $wpdb;
  1931 	static $global_terms_recurse = null;
  1878 	static $global_terms_recurse = null;
  1932 
  1879 
  1933 	if ( !global_terms_enabled() )
  1880 	if ( ! global_terms_enabled() ) {
  1934 		return $term_id;
  1881 		return $term_id;
       
  1882 	}
  1935 
  1883 
  1936 	// prevent a race condition
  1884 	// prevent a race condition
  1937 	$recurse_start = false;
  1885 	$recurse_start = false;
  1938 	if ( $global_terms_recurse === null ) {
  1886 	if ( $global_terms_recurse === null ) {
  1939 		$recurse_start = true;
  1887 		$recurse_start        = true;
  1940 		$global_terms_recurse = 1;
  1888 		$global_terms_recurse = 1;
  1941 	} elseif ( 10 < $global_terms_recurse++ ) {
  1889 	} elseif ( 10 < $global_terms_recurse++ ) {
  1942 		return $term_id;
  1890 		return $term_id;
  1943 	}
  1891 	}
  1944 
  1892 
  1945 	$term_id = intval( $term_id );
  1893 	$term_id = intval( $term_id );
  1946 	$c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1894 	$c       = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1947 
  1895 
  1948 	$global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1896 	$global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1949 	if ( $global_id == null ) {
  1897 	if ( $global_id == null ) {
  1950 		$used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1898 		$used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
  1951 		if ( null == $used_global_id ) {
  1899 		if ( null == $used_global_id ) {
  1952 			$wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1900 			$wpdb->insert(
       
  1901 				$wpdb->sitecategories,
       
  1902 				array(
       
  1903 					'cat_ID'            => $term_id,
       
  1904 					'cat_name'          => $c->name,
       
  1905 					'category_nicename' => $c->slug,
       
  1906 				)
       
  1907 			);
  1953 			$global_id = $wpdb->insert_id;
  1908 			$global_id = $wpdb->insert_id;
  1954 			if ( empty( $global_id ) )
  1909 			if ( empty( $global_id ) ) {
  1955 				return $term_id;
  1910 				return $term_id;
       
  1911 			}
  1956 		} else {
  1912 		} else {
  1957 			$max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1913 			$max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
  1958 			$max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1914 			$max_local_id  = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
  1959 			$new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1915 			$new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
  1960 			$wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
  1916 			$wpdb->insert(
       
  1917 				$wpdb->sitecategories,
       
  1918 				array(
       
  1919 					'cat_ID'            => $new_global_id,
       
  1920 					'cat_name'          => $c->name,
       
  1921 					'category_nicename' => $c->slug,
       
  1922 				)
       
  1923 			);
  1961 			$global_id = $wpdb->insert_id;
  1924 			$global_id = $wpdb->insert_id;
  1962 		}
  1925 		}
  1963 	} elseif ( $global_id != $term_id ) {
  1926 	} elseif ( $global_id != $term_id ) {
  1964 		$local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1927 		$local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
  1965 		if ( null != $local_id ) {
  1928 		if ( null != $local_id ) {
  1969 			}
  1932 			}
  1970 		}
  1933 		}
  1971 	}
  1934 	}
  1972 
  1935 
  1973 	if ( $global_id != $term_id ) {
  1936 	if ( $global_id != $term_id ) {
  1974 		if ( get_option( 'default_category' ) == $term_id )
  1937 		if ( get_option( 'default_category' ) == $term_id ) {
  1975 			update_option( 'default_category', $global_id );
  1938 			update_option( 'default_category', $global_id );
  1976 
  1939 		}
  1977 		$wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1940 
  1978 		$wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1941 		$wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
  1979 		$wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1942 		$wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
  1980 
  1943 		$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) );
  1981 		clean_term_cache($term_id);
  1944 
  1982 	}
  1945 		clean_term_cache( $term_id );
  1983 	if ( $recurse_start )
  1946 	}
       
  1947 	if ( $recurse_start ) {
  1984 		$global_terms_recurse = null;
  1948 		$global_terms_recurse = null;
       
  1949 	}
  1985 
  1950 
  1986 	return $global_id;
  1951 	return $global_id;
  1987 }
  1952 }
  1988 
  1953 
  1989 /**
  1954 /**
  2008  *
  1973  *
  2009  * @param array $upload
  1974  * @param array $upload
  2010  * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  1975  * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  2011  */
  1976  */
  2012 function upload_is_file_too_big( $upload ) {
  1977 function upload_is_file_too_big( $upload ) {
  2013 	if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
  1978 	if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
  2014 		return $upload;
  1979 		return $upload;
  2015 
  1980 	}
  2016 	if ( strlen( $upload['bits'] )  > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  1981 
  2017 		return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
  1982 	if ( strlen( $upload['bits'] ) > ( KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
       
  1983 		/* translators: %s: maximum allowed file size in kilobytes */
       
  1984 		return sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ) );
  2018 	}
  1985 	}
  2019 
  1986 
  2020 	return $upload;
  1987 	return $upload;
  2021 }
  1988 }
  2022 
  1989 
  2026  * @since MU (3.0.0)
  1993  * @since MU (3.0.0)
  2027  */
  1994  */
  2028 function signup_nonce_fields() {
  1995 function signup_nonce_fields() {
  2029 	$id = mt_rand();
  1996 	$id = mt_rand();
  2030 	echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1997 	echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  2031 	wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1998 	wp_nonce_field( 'signup_form_' . $id, '_signup_form', false );
  2032 }
  1999 }
  2033 
  2000 
  2034 /**
  2001 /**
  2035  * Process the signup nonce created in signup_nonce_fields().
  2002  * Process the signup nonce created in signup_nonce_fields().
  2036  *
  2003  *
  2038  *
  2005  *
  2039  * @param array $result
  2006  * @param array $result
  2040  * @return array
  2007  * @return array
  2041  */
  2008  */
  2042 function signup_nonce_check( $result ) {
  2009 function signup_nonce_check( $result ) {
  2043 	if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  2010 	if ( ! strpos( $_SERVER['PHP_SELF'], 'wp-signup.php' ) ) {
  2044 		return $result;
  2011 		return $result;
  2045 
  2012 	}
  2046 	if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  2013 
  2047 		wp_die( __( 'Please try again.' ) );
  2014 	if ( ! wp_verify_nonce( $_POST['_signup_form'], 'signup_form_' . $_POST['signup_form_id'] ) ) {
       
  2015 		$result['errors']->add( 'invalid_nonce', __( 'Unable to submit this form, please try again.' ) );
       
  2016 	}
  2048 
  2017 
  2049 	return $result;
  2018 	return $result;
  2050 }
  2019 }
  2051 
  2020 
  2052 /**
  2021 /**
  2063 	 * @since 3.0.0
  2032 	 * @since 3.0.0
  2064 	 *
  2033 	 *
  2065 	 * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
  2034 	 * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
  2066 	 */
  2035 	 */
  2067 	if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  2036 	if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
  2068 		if ( $destination == '%siteurl%' )
  2037 		if ( $destination == '%siteurl%' ) {
  2069 			$destination = network_home_url();
  2038 			$destination = network_home_url();
       
  2039 		}
  2070 		wp_redirect( $destination );
  2040 		wp_redirect( $destination );
  2071 		exit();
  2041 		exit();
  2072 	}
  2042 	}
  2073 }
  2043 }
  2074 
  2044 
  2080  * added, as when a user is invited through the regular WP Add User interface.
  2050  * added, as when a user is invited through the regular WP Add User interface.
  2081  *
  2051  *
  2082  * @since MU (3.0.0)
  2052  * @since MU (3.0.0)
  2083  */
  2053  */
  2084 function maybe_add_existing_user_to_blog() {
  2054 function maybe_add_existing_user_to_blog() {
  2085 	if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
  2055 	if ( false === strpos( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
  2086 		return;
  2056 		return;
  2087 
  2057 	}
  2088 	$parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  2058 
  2089 	$key = array_pop( $parts );
  2059 	$parts = explode( '/', $_SERVER['REQUEST_URI'] );
  2090 
  2060 	$key   = array_pop( $parts );
  2091 	if ( $key == '' )
  2061 
       
  2062 	if ( $key == '' ) {
  2092 		$key = array_pop( $parts );
  2063 		$key = array_pop( $parts );
       
  2064 	}
  2093 
  2065 
  2094 	$details = get_option( 'new_user_' . $key );
  2066 	$details = get_option( 'new_user_' . $key );
  2095 	if ( !empty( $details ) )
  2067 	if ( ! empty( $details ) ) {
  2096 		delete_option( 'new_user_' . $key );
  2068 		delete_option( 'new_user_' . $key );
  2097 
  2069 	}
  2098 	if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
  2070 
  2099 		wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
  2071 	if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) ) {
  2100 
  2072 		wp_die( sprintf( __( 'An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.' ), home_url() ) );
  2101 	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 &rsaquo; Success' ), array( 'response' => 200 ) );
  2073 	}
       
  2074 
       
  2075 	wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%1$s">homepage</a> or <a href="%2$s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress &rsaquo; Success' ), array( 'response' => 200 ) );
  2102 }
  2076 }
  2103 
  2077 
  2104 /**
  2078 /**
  2105  * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  2079  * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
  2106  *
  2080  *
  2110  * @return true|WP_Error|void
  2084  * @return true|WP_Error|void
  2111  */
  2085  */
  2112 function add_existing_user_to_blog( $details = false ) {
  2086 function add_existing_user_to_blog( $details = false ) {
  2113 	if ( is_array( $details ) ) {
  2087 	if ( is_array( $details ) ) {
  2114 		$blog_id = get_current_blog_id();
  2088 		$blog_id = get_current_blog_id();
  2115 		$result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
  2089 		$result  = add_user_to_blog( $blog_id, $details['user_id'], $details['role'] );
  2116 
  2090 
  2117 		/**
  2091 		/**
  2118 		 * Fires immediately after an existing user is added to a site.
  2092 		 * Fires immediately after an existing user is added to a site.
  2119 		 *
  2093 		 *
  2120 		 * @since MU (3.0.0)
  2094 		 * @since MU (3.0.0)
  2141  * @param int   $user_id
  2115  * @param int   $user_id
  2142  * @param mixed $password Ignored.
  2116  * @param mixed $password Ignored.
  2143  * @param array $meta
  2117  * @param array $meta
  2144  */
  2118  */
  2145 function add_new_user_to_blog( $user_id, $password, $meta ) {
  2119 function add_new_user_to_blog( $user_id, $password, $meta ) {
  2146 	if ( !empty( $meta[ 'add_to_blog' ] ) ) {
  2120 	if ( ! empty( $meta['add_to_blog'] ) ) {
  2147 		$blog_id = $meta[ 'add_to_blog' ];
  2121 		$blog_id = $meta['add_to_blog'];
  2148 		$role = $meta[ 'new_role' ];
  2122 		$role    = $meta['new_role'];
  2149 		remove_user_from_blog( $user_id, get_network()->site_id ); // remove user from main blog.
  2123 		remove_user_from_blog( $user_id, get_network()->site_id ); // remove user from main blog.
  2150 
  2124 
  2151 		$result = add_user_to_blog( $blog_id, $user_id, $role );
  2125 		$result = add_user_to_blog( $blog_id, $user_id, $role );
  2152 
  2126 
  2153 		if ( ! is_wp_error( $result ) ) {
  2127 		if ( ! is_wp_error( $result ) ) {
  2171  * Check to see whether a user is marked as a spammer, based on user login.
  2145  * Check to see whether a user is marked as a spammer, based on user login.
  2172  *
  2146  *
  2173  * @since MU (3.0.0)
  2147  * @since MU (3.0.0)
  2174  *
  2148  *
  2175  * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
  2149  * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
  2176  * 	                           or user login name as a string.
  2150  *                             or user login name as a string.
  2177  * @return bool
  2151  * @return bool
  2178  */
  2152  */
  2179 function is_user_spammy( $user = null ) {
  2153 function is_user_spammy( $user = null ) {
  2180     if ( ! ( $user instanceof WP_User ) ) {
  2154 	if ( ! ( $user instanceof WP_User ) ) {
  2181 		if ( $user ) {
  2155 		if ( $user ) {
  2182 			$user = get_user_by( 'login', $user );
  2156 			$user = get_user_by( 'login', $user );
  2183 		} else {
  2157 		} else {
  2184 			$user = wp_get_current_user();
  2158 			$user = wp_get_current_user();
  2185 		}
  2159 		}
  2208  * @since MU (3.0.0)
  2182  * @since MU (3.0.0)
  2209  *
  2183  *
  2210  * @return bool
  2184  * @return bool
  2211  */
  2185  */
  2212 function users_can_register_signup_filter() {
  2186 function users_can_register_signup_filter() {
  2213 	$registration = get_site_option('registration');
  2187 	$registration = get_site_option( 'registration' );
  2214 	return ( $registration == 'all' || $registration == 'user' );
  2188 	return ( $registration == 'all' || $registration == 'user' );
  2215 }
  2189 }
  2216 
  2190 
  2217 /**
  2191 /**
  2218  * Ensure that the welcome message is not empty. Currently unused.
  2192  * Ensure that the welcome message is not empty. Currently unused.
  2221  *
  2195  *
  2222  * @param string $text
  2196  * @param string $text
  2223  * @return string
  2197  * @return string
  2224  */
  2198  */
  2225 function welcome_user_msg_filter( $text ) {
  2199 function welcome_user_msg_filter( $text ) {
  2226 	if ( !$text ) {
  2200 	if ( ! $text ) {
  2227 		remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  2201 		remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  2228 
  2202 
  2229 		/* translators: Do not translate USERNAME, PASSWORD, LOGINLINK, SITE_NAME: those are placeholders. */
  2203 		/* translators: Do not translate USERNAME, PASSWORD, LOGINLINK, SITE_NAME: those are placeholders. */
  2230 		$text = __( 'Howdy USERNAME,
  2204 		$text = __(
       
  2205 			'Howdy USERNAME,
  2231 
  2206 
  2232 Your new account is set up.
  2207 Your new account is set up.
  2233 
  2208 
  2234 You can log in with the following information:
  2209 You can log in with the following information:
  2235 Username: USERNAME
  2210 Username: USERNAME
  2236 Password: PASSWORD
  2211 Password: PASSWORD
  2237 LOGINLINK
  2212 LOGINLINK
  2238 
  2213 
  2239 Thanks!
  2214 Thanks!
  2240 
  2215 
  2241 --The Team @ SITE_NAME' );
  2216 --The Team @ SITE_NAME'
       
  2217 		);
  2242 		update_site_option( 'welcome_user_email', $text );
  2218 		update_site_option( 'welcome_user_email', $text );
  2243 	}
  2219 	}
  2244 	return $text;
  2220 	return $text;
  2245 }
  2221 }
  2246 
  2222 
  2256  */
  2232  */
  2257 function force_ssl_content( $force = '' ) {
  2233 function force_ssl_content( $force = '' ) {
  2258 	static $forced_content = false;
  2234 	static $forced_content = false;
  2259 
  2235 
  2260 	if ( '' != $force ) {
  2236 	if ( '' != $force ) {
  2261 		$old_forced = $forced_content;
  2237 		$old_forced     = $forced_content;
  2262 		$forced_content = $force;
  2238 		$forced_content = $force;
  2263 		return $old_forced;
  2239 		return $old_forced;
  2264 	}
  2240 	}
  2265 
  2241 
  2266 	return $forced_content;
  2242 	return $forced_content;
  2275  *
  2251  *
  2276  * @param string $url URL
  2252  * @param string $url URL
  2277  * @return string URL with https as the scheme
  2253  * @return string URL with https as the scheme
  2278  */
  2254  */
  2279 function filter_SSL( $url ) {
  2255 function filter_SSL( $url ) {
  2280 	if ( ! is_string( $url ) )
  2256 	if ( ! is_string( $url ) ) {
  2281 		return get_bloginfo( 'url' ); // Return home blog url with proper scheme
  2257 		return get_bloginfo( 'url' ); // Return home blog url with proper scheme
  2282 
  2258 	}
  2283 	if ( force_ssl_content() && is_ssl() )
  2259 
       
  2260 	if ( force_ssl_content() && is_ssl() ) {
  2284 		$url = set_url_scheme( $url, 'https' );
  2261 		$url = set_url_scheme( $url, 'https' );
       
  2262 	}
  2285 
  2263 
  2286 	return $url;
  2264 	return $url;
  2287 }
  2265 }
  2288 
  2266 
  2289 /**
  2267 /**
  2290  * Schedule update of the network-wide counts for the current network.
  2268  * Schedule update of the network-wide counts for the current network.
  2291  *
  2269  *
  2292  * @since 3.1.0
  2270  * @since 3.1.0
  2293  */
  2271  */
  2294 function wp_schedule_update_network_counts() {
  2272 function wp_schedule_update_network_counts() {
  2295 	if ( !is_main_site() )
  2273 	if ( ! is_main_site() ) {
  2296 		return;
  2274 		return;
  2297 
  2275 	}
  2298 	if ( ! wp_next_scheduled('update_network_counts') && ! wp_installing() )
  2276 
  2299 		wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
  2277 	if ( ! wp_next_scheduled( 'update_network_counts' ) && ! wp_installing() ) {
       
  2278 		wp_schedule_event( time(), 'twicedaily', 'update_network_counts' );
       
  2279 	}
  2300 }
  2280 }
  2301 
  2281 
  2302 /**
  2282 /**
  2303  * Update the network-wide counts for the current network.
  2283  * Update the network-wide counts for the current network.
  2304  *
  2284  *
  2305  * @since 3.1.0
  2285  * @since 3.1.0
  2306  * @since 4.8.0 The $network_id parameter has been added.
  2286  * @since 4.8.0 The `$network_id` parameter has been added.
  2307  *
  2287  *
  2308  * @param int|null $network_id ID of the network. Default is the current network.
  2288  * @param int|null $network_id ID of the network. Default is the current network.
  2309  */
  2289  */
  2310 function wp_update_network_counts( $network_id = null ) {
  2290 function wp_update_network_counts( $network_id = null ) {
  2311 	wp_update_network_user_counts( $network_id );
  2291 	wp_update_network_user_counts( $network_id );
  2317  *
  2297  *
  2318  * If enabled through the {@see 'enable_live_network_counts'} filter, update the sites count
  2298  * If enabled through the {@see 'enable_live_network_counts'} filter, update the sites count
  2319  * on a network when a site is created or its status is updated.
  2299  * on a network when a site is created or its status is updated.
  2320  *
  2300  *
  2321  * @since 3.7.0
  2301  * @since 3.7.0
  2322  * @since 4.8.0 The $network_id parameter has been added.
  2302  * @since 4.8.0 The `$network_id` parameter has been added.
  2323  *
  2303  *
  2324  * @param int|null $network_id ID of the network. Default is the current network.
  2304  * @param int|null $network_id ID of the network. Default is the current network.
  2325  */
  2305  */
  2326 function wp_maybe_update_network_site_counts( $network_id = null ) {
  2306 function wp_maybe_update_network_site_counts( $network_id = null ) {
  2327 	$is_small_network = ! wp_is_large_network( 'sites', $network_id );
  2307 	$is_small_network = ! wp_is_large_network( 'sites', $network_id );
  2334 	 * @see wp_is_large_network()
  2314 	 * @see wp_is_large_network()
  2335 	 *
  2315 	 *
  2336 	 * @param bool   $small_network Whether the network is considered small.
  2316 	 * @param bool   $small_network Whether the network is considered small.
  2337 	 * @param string $context       Context. Either 'users' or 'sites'.
  2317 	 * @param string $context       Context. Either 'users' or 'sites'.
  2338 	 */
  2318 	 */
  2339 	if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
  2319 	if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) ) {
  2340 		return;
  2320 		return;
       
  2321 	}
  2341 
  2322 
  2342 	wp_update_network_site_counts( $network_id );
  2323 	wp_update_network_site_counts( $network_id );
  2343 }
  2324 }
  2344 
  2325 
  2345 /**
  2326 /**
  2347  *
  2328  *
  2348  * If enabled through the {@see 'enable_live_network_counts'} filter, update the users count
  2329  * If enabled through the {@see 'enable_live_network_counts'} filter, update the users count
  2349  * on a network when a user is created or its status is updated.
  2330  * on a network when a user is created or its status is updated.
  2350  *
  2331  *
  2351  * @since 3.7.0
  2332  * @since 3.7.0
  2352  * @since 4.8.0 The $network_id parameter has been added.
  2333  * @since 4.8.0 The `$network_id` parameter has been added.
  2353  *
  2334  *
  2354  * @param int|null $network_id ID of the network. Default is the current network.
  2335  * @param int|null $network_id ID of the network. Default is the current network.
  2355  */
  2336  */
  2356 function wp_maybe_update_network_user_counts( $network_id = null ) {
  2337 function wp_maybe_update_network_user_counts( $network_id = null ) {
  2357 	$is_small_network = ! wp_is_large_network( 'users', $network_id );
  2338 	$is_small_network = ! wp_is_large_network( 'users', $network_id );
  2358 
  2339 
  2359 	/** This filter is documented in wp-includes/ms-functions.php */
  2340 	/** This filter is documented in wp-includes/ms-functions.php */
  2360 	if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
  2341 	if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
  2361 		return;
  2342 		return;
       
  2343 	}
  2362 
  2344 
  2363 	wp_update_network_user_counts( $network_id );
  2345 	wp_update_network_user_counts( $network_id );
  2364 }
  2346 }
  2365 
  2347 
  2366 /**
  2348 /**
  2367  * Update the network-wide site count.
  2349  * Update the network-wide site count.
  2368  *
  2350  *
  2369  * @since 3.7.0
  2351  * @since 3.7.0
  2370  * @since 4.8.0 The $network_id parameter has been added.
  2352  * @since 4.8.0 The `$network_id` parameter has been added.
  2371  *
  2353  *
  2372  * @param int|null $network_id ID of the network. Default is the current network.
  2354  * @param int|null $network_id ID of the network. Default is the current network.
  2373  */
  2355  */
  2374 function wp_update_network_site_counts( $network_id = null ) {
  2356 function wp_update_network_site_counts( $network_id = null ) {
  2375 	$network_id = (int) $network_id;
  2357 	$network_id = (int) $network_id;
  2376 	if ( ! $network_id ) {
  2358 	if ( ! $network_id ) {
  2377 		$network_id = get_current_network_id();
  2359 		$network_id = get_current_network_id();
  2378 	}
  2360 	}
  2379 
  2361 
  2380 	$count = get_sites( array(
  2362 	$count = get_sites(
  2381 		'network_id' => $network_id,
  2363 		array(
  2382 		'spam'       => 0,
  2364 			'network_id'             => $network_id,
  2383 		'deleted'    => 0,
  2365 			'spam'                   => 0,
  2384 		'archived'   => 0,
  2366 			'deleted'                => 0,
  2385 		'count'      => true,
  2367 			'archived'               => 0,
  2386 	) );
  2368 			'count'                  => true,
       
  2369 			'update_site_meta_cache' => false,
       
  2370 		)
       
  2371 	);
  2387 
  2372 
  2388 	update_network_option( $network_id, 'blog_count', $count );
  2373 	update_network_option( $network_id, 'blog_count', $count );
  2389 }
  2374 }
  2390 
  2375 
  2391 /**
  2376 /**
  2392  * Update the network-wide user count.
  2377  * Update the network-wide user count.
  2393  *
  2378  *
  2394  * @since 3.7.0
  2379  * @since 3.7.0
  2395  * @since 4.8.0 The $network_id parameter has been added.
  2380  * @since 4.8.0 The `$network_id` parameter has been added.
  2396  *
  2381  *
  2397  * @global wpdb $wpdb WordPress database abstraction object.
  2382  * @global wpdb $wpdb WordPress database abstraction object.
  2398  *
  2383  *
  2399  * @param int|null $network_id ID of the network. Default is the current network.
  2384  * @param int|null $network_id ID of the network. Default is the current network.
  2400  */
  2385  */
  2437  * @return int Quota in megabytes
  2422  * @return int Quota in megabytes
  2438  */
  2423  */
  2439 function get_space_allowed() {
  2424 function get_space_allowed() {
  2440 	$space_allowed = get_option( 'blog_upload_space' );
  2425 	$space_allowed = get_option( 'blog_upload_space' );
  2441 
  2426 
  2442 	if ( ! is_numeric( $space_allowed ) )
  2427 	if ( ! is_numeric( $space_allowed ) ) {
  2443 		$space_allowed = get_site_option( 'blog_upload_space' );
  2428 		$space_allowed = get_site_option( 'blog_upload_space' );
  2444 
  2429 	}
  2445 	if ( ! is_numeric( $space_allowed ) )
  2430 
       
  2431 	if ( ! is_numeric( $space_allowed ) ) {
  2446 		$space_allowed = 100;
  2432 		$space_allowed = 100;
       
  2433 	}
  2447 
  2434 
  2448 	/**
  2435 	/**
  2449 	 * Filters the upload quota for the current site.
  2436 	 * Filters the upload quota for the current site.
  2450 	 *
  2437 	 *
  2451 	 * @since 3.7.0
  2438 	 * @since 3.7.0
  2466 	$allowed = get_space_allowed();
  2453 	$allowed = get_space_allowed();
  2467 	if ( $allowed < 0 ) {
  2454 	if ( $allowed < 0 ) {
  2468 		$allowed = 0;
  2455 		$allowed = 0;
  2469 	}
  2456 	}
  2470 	$space_allowed = $allowed * MB_IN_BYTES;
  2457 	$space_allowed = $allowed * MB_IN_BYTES;
  2471 	if ( get_site_option( 'upload_space_check_disabled' ) )
  2458 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
  2472 		return $space_allowed;
  2459 		return $space_allowed;
       
  2460 	}
  2473 
  2461 
  2474 	$space_used = get_space_used() * MB_IN_BYTES;
  2462 	$space_used = get_space_used() * MB_IN_BYTES;
  2475 
  2463 
  2476 	if ( ( $space_allowed - $space_used ) <= 0 )
  2464 	if ( ( $space_allowed - $space_used ) <= 0 ) {
  2477 		return 0;
  2465 		return 0;
       
  2466 	}
  2478 
  2467 
  2479 	return $space_allowed - $space_used;
  2468 	return $space_allowed - $space_used;
  2480 }
  2469 }
  2481 
  2470 
  2482 /**
  2471 /**
  2484  *
  2473  *
  2485  * @since 3.0.0
  2474  * @since 3.0.0
  2486  * @return bool True if space is available, false otherwise.
  2475  * @return bool True if space is available, false otherwise.
  2487  */
  2476  */
  2488 function is_upload_space_available() {
  2477 function is_upload_space_available() {
  2489 	if ( get_site_option( 'upload_space_check_disabled' ) )
  2478 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
  2490 		return true;
  2479 		return true;
       
  2480 	}
  2491 
  2481 
  2492 	return (bool) get_upload_space_available();
  2482 	return (bool) get_upload_space_available();
  2493 }
  2483 }
  2494 
  2484 
  2495 /**
  2485 /**
  2500  * @param  int $size Upload size limit in bytes.
  2490  * @param  int $size Upload size limit in bytes.
  2501  * @return int       Upload size limit in bytes.
  2491  * @return int       Upload size limit in bytes.
  2502  */
  2492  */
  2503 function upload_size_limit_filter( $size ) {
  2493 function upload_size_limit_filter( $size ) {
  2504 	$fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
  2494 	$fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
  2505 	if ( get_site_option( 'upload_space_check_disabled' ) )
  2495 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
  2506 		return min( $size, $fileupload_maxk );
  2496 		return min( $size, $fileupload_maxk );
       
  2497 	}
  2507 
  2498 
  2508 	return min( $size, $fileupload_maxk, get_upload_space_available() );
  2499 	return min( $size, $fileupload_maxk, get_upload_space_available() );
  2509 }
  2500 }
  2510 
  2501 
  2511 /**
  2502 /**
  2513  *
  2504  *
  2514  * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
  2505  * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
  2515  * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2506  * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2516  *
  2507  *
  2517  * @since 3.3.0
  2508  * @since 3.3.0
  2518  * @since 4.8.0 The $network_id parameter has been added.
  2509  * @since 4.8.0 The `$network_id` parameter has been added.
  2519  *
  2510  *
  2520  * @param string   $using      'sites or 'users'. Default is 'sites'.
  2511  * @param string   $using      'sites or 'users'. Default is 'sites'.
  2521  * @param int|null $network_id ID of the network. Default is the current network.
  2512  * @param int|null $network_id ID of the network. Default is the current network.
  2522  * @return bool True if the network meets the criteria for large. False otherwise.
  2513  * @return bool True if the network meets the criteria for large. False otherwise.
  2523  */
  2514  */
  2531 		$count = get_user_count( $network_id );
  2522 		$count = get_user_count( $network_id );
  2532 		/**
  2523 		/**
  2533 		 * Filters whether the network is considered large.
  2524 		 * Filters whether the network is considered large.
  2534 		 *
  2525 		 *
  2535 		 * @since 3.3.0
  2526 		 * @since 3.3.0
  2536 		 * @since 4.8.0 The $network_id parameter has been added.
  2527 		 * @since 4.8.0 The `$network_id` parameter has been added.
  2537 		 *
  2528 		 *
  2538 		 * @param bool   $is_large_network Whether the network has more than 10000 users or sites.
  2529 		 * @param bool   $is_large_network Whether the network has more than 10000 users or sites.
  2539 		 * @param string $component        The component to count. Accepts 'users', or 'sites'.
  2530 		 * @param string $component        The component to count. Accepts 'users', or 'sites'.
  2540 		 * @param int    $count            The count of items for the component.
  2531 		 * @param int    $count            The count of items for the component.
  2541 		 * @param int    $network_id       The ID of the network being checked.
  2532 		 * @param int    $network_id       The ID of the network being checked.
  2555  *
  2546  *
  2556  * @return array $names Array of reserved subdirectory names.
  2547  * @return array $names Array of reserved subdirectory names.
  2557  */
  2548  */
  2558 function get_subdirectory_reserved_names() {
  2549 function get_subdirectory_reserved_names() {
  2559 	$names = array(
  2550 	$names = array(
  2560 		'page', 'comments', 'blog', 'files', 'feed', 'wp-admin',
  2551 		'page',
  2561 		'wp-content', 'wp-includes', 'wp-json', 'embed'
  2552 		'comments',
       
  2553 		'blog',
       
  2554 		'files',
       
  2555 		'feed',
       
  2556 		'wp-admin',
       
  2557 		'wp-content',
       
  2558 		'wp-includes',
       
  2559 		'wp-json',
       
  2560 		'embed',
  2562 	);
  2561 	);
  2563 
  2562 
  2564 	/**
  2563 	/**
  2565 	 * Filters reserved site names on a sub-directory Multisite installation.
  2564 	 * Filters reserved site names on a sub-directory Multisite installation.
  2566 	 *
  2565 	 *
  2586 function update_network_option_new_admin_email( $old_value, $value ) {
  2585 function update_network_option_new_admin_email( $old_value, $value ) {
  2587 	if ( $value == get_site_option( 'admin_email' ) || ! is_email( $value ) ) {
  2586 	if ( $value == get_site_option( 'admin_email' ) || ! is_email( $value ) ) {
  2588 		return;
  2587 		return;
  2589 	}
  2588 	}
  2590 
  2589 
  2591 	$hash = md5( $value . time() . mt_rand() );
  2590 	$hash            = md5( $value . time() . mt_rand() );
  2592 	$new_admin_email = array(
  2591 	$new_admin_email = array(
  2593 		'hash'     => $hash,
  2592 		'hash'     => $hash,
  2594 		'newemail' => $value,
  2593 		'newemail' => $value,
  2595 	);
  2594 	);
  2596 	update_site_option( 'network_admin_hash', $new_admin_email );
  2595 	update_site_option( 'network_admin_hash', $new_admin_email );
  2597 
  2596 
  2598 	$switched_locale = switch_to_locale( get_user_locale() );
  2597 	$switched_locale = switch_to_locale( get_user_locale() );
  2599 
  2598 
  2600 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  2599 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  2601 	$email_text = __( 'Howdy ###USERNAME###,
  2600 	$email_text = __(
       
  2601 		'Howdy ###USERNAME###,
  2602 
  2602 
  2603 You recently requested to have the network admin email address on
  2603 You recently requested to have the network admin email address on
  2604 your network changed.
  2604 your network changed.
  2605 
  2605 
  2606 If this is correct, please click on the following link to change it:
  2606 If this is correct, please click on the following link to change it:
  2611 
  2611 
  2612 This email has been sent to ###EMAIL###
  2612 This email has been sent to ###EMAIL###
  2613 
  2613 
  2614 Regards,
  2614 Regards,
  2615 All at ###SITENAME###
  2615 All at ###SITENAME###
  2616 ###SITEURL###' );
  2616 ###SITEURL###'
       
  2617 	);
  2617 
  2618 
  2618 	/**
  2619 	/**
  2619 	 * Filters the text of the email sent when a change of network admin email address is attempted.
  2620 	 * Filters the text of the email sent when a change of network admin email address is attempted.
  2620 	 *
  2621 	 *
  2621 	 * The following strings have a special meaning and will get replaced dynamically:
  2622 	 * The following strings have a special meaning and will get replaced dynamically:
  2636 	 * }
  2637 	 * }
  2637 	 */
  2638 	 */
  2638 	$content = apply_filters( 'new_network_admin_email_content', $email_text, $new_admin_email );
  2639 	$content = apply_filters( 'new_network_admin_email_content', $email_text, $new_admin_email );
  2639 
  2640 
  2640 	$current_user = wp_get_current_user();
  2641 	$current_user = wp_get_current_user();
  2641 	$content = str_replace( '###USERNAME###', $current_user->user_login, $content );
  2642 	$content      = str_replace( '###USERNAME###', $current_user->user_login, $content );
  2642 	$content = str_replace( '###ADMIN_URL###', esc_url( network_admin_url( 'settings.php?network_admin_hash=' . $hash ) ), $content );
  2643 	$content      = str_replace( '###ADMIN_URL###', esc_url( network_admin_url( 'settings.php?network_admin_hash=' . $hash ) ), $content );
  2643 	$content = str_replace( '###EMAIL###', $value, $content );
  2644 	$content      = str_replace( '###EMAIL###', $value, $content );
  2644 	$content = str_replace( '###SITENAME###', wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ), $content );
  2645 	$content      = str_replace( '###SITENAME###', wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ), $content );
  2645 	$content = str_replace( '###SITEURL###', network_home_url(), $content );
  2646 	$content      = str_replace( '###SITEURL###', network_home_url(), $content );
  2646 
  2647 
  2647 	wp_mail( $value, sprintf( __( '[%s] New Network Admin Email Address' ), wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ) ), $content );
  2648 	/* translators: Email change notification email subject. %s: Network title */
       
  2649 	wp_mail( $value, sprintf( __( '[%s] Network Admin Email Change Request' ), wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES ) ), $content );
  2648 
  2650 
  2649 	if ( $switched_locale ) {
  2651 	if ( $switched_locale ) {
  2650 		restore_previous_locale();
  2652 		restore_previous_locale();
  2651 	}
  2653 	}
  2652 }
  2654 }
  2684 	if ( ! $send ) {
  2686 	if ( ! $send ) {
  2685 		return;
  2687 		return;
  2686 	}
  2688 	}
  2687 
  2689 
  2688 	/* translators: Do not translate OLD_EMAIL, NEW_EMAIL, SITENAME, SITEURL: those are placeholders. */
  2690 	/* translators: Do not translate OLD_EMAIL, NEW_EMAIL, SITENAME, SITEURL: those are placeholders. */
  2689 	$email_change_text = __( 'Hi,
  2691 	$email_change_text = __(
       
  2692 		'Hi,
  2690 
  2693 
  2691 This notice confirms that the network admin email address was changed on ###SITENAME###.
  2694 This notice confirms that the network admin email address was changed on ###SITENAME###.
  2692 
  2695 
  2693 The new network admin email address is ###NEW_EMAIL###.
  2696 The new network admin email address is ###NEW_EMAIL###.
  2694 
  2697 
  2695 This email has been sent to ###OLD_EMAIL###
  2698 This email has been sent to ###OLD_EMAIL###
  2696 
  2699 
  2697 Regards,
  2700 Regards,
  2698 All at ###SITENAME###
  2701 All at ###SITENAME###
  2699 ###SITEURL###' );
  2702 ###SITEURL###'
       
  2703 	);
  2700 
  2704 
  2701 	$email_change_email = array(
  2705 	$email_change_email = array(
  2702 		'to'      => $old_email,
  2706 		'to'      => $old_email,
  2703 		/* translators: Network admin email change notification email subject. %s: Network title */
  2707 		/* translators: Network admin email change notification email subject. %s: Network title */
  2704 		'subject' => __( '[%s] Notice of Network Admin Email Change' ),
  2708 		'subject' => __( '[%s] Network Admin Email Changed' ),
  2705 		'message' => $email_change_text,
  2709 		'message' => $email_change_text,
  2706 		'headers' => '',
  2710 		'headers' => '',
  2707 	);
  2711 	);
  2708 	// get network name
  2712 	// get network name
  2709 	$network_name = wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES );
  2713 	$network_name = wp_specialchars_decode( get_site_option( 'site_name' ), ENT_QUOTES );
  2730 	 * @param string $new_email  The new network admin email address.
  2734 	 * @param string $new_email  The new network admin email address.
  2731 	 * @param int    $network_id ID of the network.
  2735 	 * @param int    $network_id ID of the network.
  2732 	 */
  2736 	 */
  2733 	$email_change_email = apply_filters( 'network_admin_email_change_email', $email_change_email, $old_email, $new_email, $network_id );
  2737 	$email_change_email = apply_filters( 'network_admin_email_change_email', $email_change_email, $old_email, $new_email, $network_id );
  2734 
  2738 
  2735 	$email_change_email['message'] = str_replace( '###OLD_EMAIL###', $old_email,    $email_change_email['message'] );
  2739 	$email_change_email['message'] = str_replace( '###OLD_EMAIL###', $old_email, $email_change_email['message'] );
  2736 	$email_change_email['message'] = str_replace( '###NEW_EMAIL###', $new_email,    $email_change_email['message'] );
  2740 	$email_change_email['message'] = str_replace( '###NEW_EMAIL###', $new_email, $email_change_email['message'] );
  2737 	$email_change_email['message'] = str_replace( '###SITENAME###',  $network_name, $email_change_email['message'] );
  2741 	$email_change_email['message'] = str_replace( '###SITENAME###', $network_name, $email_change_email['message'] );
  2738 	$email_change_email['message'] = str_replace( '###SITEURL###',   home_url(),    $email_change_email['message'] );
  2742 	$email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] );
  2739 
  2743 
  2740 	wp_mail( $email_change_email['to'], sprintf(
  2744 	wp_mail(
  2741 		$email_change_email['subject'],
  2745 		$email_change_email['to'],
  2742 		$network_name
  2746 		sprintf(
  2743 	), $email_change_email['message'], $email_change_email['headers'] );
  2747 			$email_change_email['subject'],
  2744 }
  2748 			$network_name
       
  2749 		),
       
  2750 		$email_change_email['message'],
       
  2751 		$email_change_email['headers']
       
  2752 	);
       
  2753 }