diff -r c7c34916027a -r 177826044cd9 wp/wp-includes/ms-functions.php --- a/wp/wp-includes/ms-functions.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-includes/ms-functions.php Mon Oct 14 18:28:13 2019 +0200 @@ -39,15 +39,16 @@ */ function get_active_blog_for_user( $user_id ) { $blogs = get_blogs_of_user( $user_id ); - if ( empty( $blogs ) ) + if ( empty( $blogs ) ) { return; + } if ( ! is_multisite() ) { return $blogs[ get_current_blog_id() ]; } $primary_blog = get_user_meta( $user_id, 'primary_blog', true ); - $first_blog = current($blogs); + $first_blog = current( $blogs ); if ( false !== $primary_blog ) { if ( ! isset( $blogs[ $primary_blog ] ) ) { update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id ); @@ -67,18 +68,21 @@ if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) { $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs. - $ret = false; + $ret = false; if ( is_array( $blogs ) && count( $blogs ) > 0 ) { foreach ( (array) $blogs as $blog_id => $blog ) { - if ( $blog->site_id != get_current_network_id() ) + if ( $blog->site_id != get_current_network_id() ) { continue; + } $details = get_site( $blog_id ); if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) { - $ret = $blog; - if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id ) + $ret = $details; + if ( get_user_meta( $user_id, 'primary_blog', true ) != $blog_id ) { update_user_meta( $user_id, 'primary_blog', $blog_id ); - if ( !get_user_meta($user_id , 'source_domain', true) ) - update_user_meta( $user_id, 'source_domain', $blog->domain ); + } + if ( ! get_user_meta( $user_id, 'source_domain', true ) ) { + update_user_meta( $user_id, 'source_domain', $details->domain ); + } break; } } @@ -97,7 +101,7 @@ * The count is cached and updated twice daily. This is not a live count. * * @since MU (3.0.0) - * @since 4.8.0 The $network_id parameter has been added. + * @since 4.8.0 The `$network_id` parameter has been added. * * @param int|null $network_id ID of the network. Default is the current network. * @return int Number of active users on the network. @@ -112,8 +116,8 @@ * The count is cached and updated twice daily. This is not a live count. * * @since MU (3.0.0) - * @since 3.7.0 The $network_id parameter has been deprecated. - * @since 4.8.0 The $network_id parameter is now being used. + * @since 3.7.0 The `$network_id` parameter has been deprecated. + * @since 4.8.0 The `$network_id` parameter is now being used. * * @param int|null $network_id ID of the network. Default is the current network. * @return int Number of active sites on the network. @@ -152,7 +156,7 @@ * @return true|WP_Error */ function add_user_to_blog( $blog_id, $user_id, $role ) { - switch_to_blog($blog_id); + switch_to_blog( $blog_id ); $user = get_userdata( $user_id ); @@ -184,13 +188,13 @@ return new WP_Error( 'user_cannot_be_added', __( 'User cannot be added to this site.' ) ); } - if ( !get_user_meta($user_id, 'primary_blog', true) ) { - update_user_meta($user_id, 'primary_blog', $blog_id); + if ( ! get_user_meta( $user_id, 'primary_blog', true ) ) { + update_user_meta( $user_id, 'primary_blog', $blog_id ); $site = get_site( $blog_id ); update_user_meta( $user_id, 'source_domain', $site->domain ); } - $user->set_role($role); + $user->set_role( $role ); /** * Fires immediately after a user is added to a site. @@ -226,9 +230,9 @@ * @param string $reassign Optional. A user to whom to reassign posts. * @return true|WP_Error */ -function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') { +function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) { global $wpdb; - switch_to_blog($blog_id); + switch_to_blog( $blog_id ); $user_id = (int) $user_id; /** * Fires before a user is removed from a site. @@ -242,36 +246,37 @@ // If being removed from the primary blog, set a new primary if the user is assigned // to multiple blogs. - $primary_blog = get_user_meta($user_id, 'primary_blog', true); + $primary_blog = get_user_meta( $user_id, 'primary_blog', true ); if ( $primary_blog == $blog_id ) { - $new_id = ''; + $new_id = ''; $new_domain = ''; - $blogs = get_blogs_of_user($user_id); + $blogs = get_blogs_of_user( $user_id ); foreach ( (array) $blogs as $blog ) { - if ( $blog->userblog_id == $blog_id ) + if ( $blog->userblog_id == $blog_id ) { continue; - $new_id = $blog->userblog_id; + } + $new_id = $blog->userblog_id; $new_domain = $blog->domain; break; } - update_user_meta($user_id, 'primary_blog', $new_id); - update_user_meta($user_id, 'source_domain', $new_domain); + update_user_meta( $user_id, 'primary_blog', $new_id ); + update_user_meta( $user_id, 'source_domain', $new_domain ); } // wp_revoke_user($user_id); $user = get_userdata( $user_id ); if ( ! $user ) { restore_current_blog(); - return new WP_Error('user_does_not_exist', __('That user does not exist.')); + return new WP_Error( 'user_does_not_exist', __( 'That user does not exist.' ) ); } $user->remove_all_caps(); - $blogs = get_blogs_of_user($user_id); - if ( count($blogs) == 0 ) { - update_user_meta($user_id, 'primary_blog', ''); - update_user_meta($user_id, 'source_domain', ''); + $blogs = get_blogs_of_user( $user_id ); + if ( count( $blogs ) == 0 ) { + update_user_meta( $user_id, 'primary_blog', '' ); + update_user_meta( $user_id, 'source_domain', '' ); } if ( $reassign != '' ) { @@ -330,22 +335,24 @@ */ function get_blog_id_from_url( $domain, $path = '/' ) { $domain = strtolower( $domain ); - $path = strtolower( $path ); - $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' ); + $path = strtolower( $path ); + $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' ); - if ( $id == -1 ) // blog does not exist + if ( $id == -1 ) { // blog does not exist return 0; - elseif ( $id ) + } elseif ( $id ) { return (int) $id; + } - $args = array( - 'domain' => $domain, - 'path' => $path, - 'fields' => 'ids', - 'number' => 1, + $args = array( + 'domain' => $domain, + 'path' => $path, + 'fields' => 'ids', + 'number' => 1, + 'update_site_meta_cache' => false, ); $result = get_sites( $args ); - $id = array_shift( $result ); + $id = array_shift( $result ); if ( ! $id ) { wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' ); @@ -374,20 +381,22 @@ */ function is_email_address_unsafe( $user_email ) { $banned_names = get_site_option( 'banned_email_domains' ); - if ( $banned_names && ! is_array( $banned_names ) ) + if ( $banned_names && ! is_array( $banned_names ) ) { $banned_names = explode( "\n", $banned_names ); + } $is_email_address_unsafe = false; if ( $banned_names && is_array( $banned_names ) && false !== strpos( $user_email, '@', 1 ) ) { - $banned_names = array_map( 'strtolower', $banned_names ); + $banned_names = array_map( 'strtolower', $banned_names ); $normalized_email = strtolower( $user_email ); list( $email_local_part, $email_domain ) = explode( '@', $normalized_email ); foreach ( $banned_names as $banned_domain ) { - if ( ! $banned_domain ) + if ( ! $banned_domain ) { continue; + } if ( $email_domain == $banned_domain ) { $is_email_address_unsafe = true; @@ -433,13 +442,13 @@ * @param string $user_email The email provided by the user. * @return array Contains username, email, and error messages. */ -function wpmu_validate_user_signup($user_name, $user_email) { +function wpmu_validate_user_signup( $user_name, $user_email ) { global $wpdb; $errors = new WP_Error(); $orig_username = $user_name; - $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) ); + $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) ); if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) { $errors->add( 'user_name', __( 'Usernames can only contain lowercase letters (a-z) and numbers.' ) ); @@ -448,23 +457,24 @@ $user_email = sanitize_email( $user_email ); - if ( empty( $user_name ) ) - $errors->add('user_name', __( 'Please enter a username.' ) ); + if ( empty( $user_name ) ) { + $errors->add( 'user_name', __( 'Please enter a username.' ) ); + } $illegal_names = get_site_option( 'illegal_names' ); if ( ! is_array( $illegal_names ) ) { - $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ); + $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ); add_site_option( 'illegal_names', $illegal_names ); } if ( in_array( $user_name, $illegal_names ) ) { - $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) ); + $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) ); } /** This filter is documented in wp-includes/user.php */ $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); if ( in_array( strtolower( $user_name ), array_map( 'strtolower', $illegal_logins ) ) ) { - $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) ); + $errors->add( 'user_name', __( 'Sorry, that username is not allowed.' ) ); } if ( ! is_email( $user_email ) ) { @@ -473,57 +483,69 @@ $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.' ) ); } - if ( strlen( $user_name ) < 4 ) - $errors->add('user_name', __( 'Username must be at least 4 characters.' ) ); + if ( strlen( $user_name ) < 4 ) { + $errors->add( 'user_name', __( 'Username must be at least 4 characters.' ) ); + } if ( strlen( $user_name ) > 60 ) { $errors->add( 'user_name', __( 'Username may not be longer than 60 characters.' ) ); } // all numeric? - if ( preg_match( '/^[0-9]*$/', $user_name ) ) - $errors->add('user_name', __('Sorry, usernames must have letters too!')); + if ( preg_match( '/^[0-9]*$/', $user_name ) ) { + $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!' ) ); + } $limited_email_domains = get_site_option( 'limited_email_domains' ); if ( is_array( $limited_email_domains ) && ! empty( $limited_email_domains ) ) { - $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) ); - if ( ! in_array( $emaildomain, $limited_email_domains ) ) { - $errors->add('user_email', __('Sorry, that email address is not allowed!')); + $limited_email_domains = array_map( 'strtolower', $limited_email_domains ); + $emaildomain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) ); + if ( ! in_array( $emaildomain, $limited_email_domains, true ) ) { + $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!' ) ); } } // Check if the username has been used already. - if ( username_exists($user_name) ) + if ( username_exists( $user_name ) ) { $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) ); + } // Check if the email address has been used already. - if ( email_exists($user_email) ) + if ( email_exists( $user_email ) ) { $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) ); + } // Has someone already signed up for this username? - $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) ); + $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name ) ); if ( $signup != null ) { - $registered_at = mysql2date('U', $signup->registered); - $now = current_time( 'timestamp', true ); - $diff = $now - $registered_at; + $registered_at = mysql2date( 'U', $signup->registered ); + $now = time(); + $diff = $now - $registered_at; // If registered more than two days ago, cancel registration and let this signup go through. - if ( $diff > 2 * DAY_IN_SECONDS ) + if ( $diff > 2 * DAY_IN_SECONDS ) { $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) ); - else - $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.')); + } else { + $errors->add( 'user_name', __( 'That username is currently reserved but may be available in a couple of days.' ) ); + } } - $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) ); + $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email ) ); if ( $signup != null ) { - $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered); + $diff = time() - mysql2date( 'U', $signup->registered ); // If registered more than two days ago, cancel registration and let this signup go through. - if ( $diff > 2 * DAY_IN_SECONDS ) + if ( $diff > 2 * DAY_IN_SECONDS ) { $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) ); - else - $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.')); + } else { + $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.' ) ); + } } - $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors); + $result = array( + 'user_name' => $user_name, + 'orig_username' => $orig_username, + 'user_email' => $user_email, + 'errors' => $errors, + ); /** * Filters the validated user registration details. @@ -573,11 +595,11 @@ global $wpdb, $domain; $current_network = get_network(); - $base = $current_network->path; + $base = $current_network->path; $blog_title = strip_tags( $blog_title ); - $errors = new WP_Error(); + $errors = new WP_Error(); $illegal_names = get_site_option( 'illegal_names' ); if ( $illegal_names == false ) { $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ); @@ -592,15 +614,17 @@ $illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() ); } - if ( empty( $blogname ) ) - $errors->add('blogname', __( 'Please enter a site name.' ) ); + if ( empty( $blogname ) ) { + $errors->add( 'blogname', __( 'Please enter a site name.' ) ); + } if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) { $errors->add( 'blogname', __( 'Site names can only contain lowercase letters (a-z) and numbers.' ) ); } - if ( in_array( $blogname, $illegal_names ) ) - $errors->add('blogname', __( 'That name is not allowed.' ) ); + if ( in_array( $blogname, $illegal_names ) ) { + $errors->add( 'blogname', __( 'That name is not allowed.' ) ); + } /** * Filters the minimum site name length required when validating a site signup. @@ -617,12 +641,14 @@ } // do not allow users to create a blog that conflicts with a page on the main blog. - 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 ) ) ) + 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 ) ) ) { $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) ); + } // all numeric? - if ( preg_match( '/^[0-9]*$/', $blogname ) ) - $errors->add('blogname', __('Sorry, site names must have letters too!')); + if ( preg_match( '/^[0-9]*$/', $blogname ) ) { + $errors->add( 'blogname', __( 'Sorry, site names must have letters too!' ) ); + } /** * Filters the new site name during registration. @@ -636,39 +662,56 @@ */ $blogname = apply_filters( 'newblogname', $blogname ); - $blog_title = wp_unslash( $blog_title ); + $blog_title = wp_unslash( $blog_title ); - if ( empty( $blog_title ) ) - $errors->add('blog_title', __( 'Please enter a site title.' ) ); + if ( empty( $blog_title ) ) { + $errors->add( 'blog_title', __( 'Please enter a site title.' ) ); + } // Check if the domain/path has been used already. if ( is_subdomain_install() ) { $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain ); - $path = $base; + $path = $base; } else { $mydomain = "$domain"; - $path = $base.$blogname.'/'; + $path = $base . $blogname . '/'; } - if ( domain_exists($mydomain, $path, $current_network->id) ) + if ( domain_exists( $mydomain, $path, $current_network->id ) ) { $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) ); + } if ( username_exists( $blogname ) ) { - if ( ! is_object( $user ) || ( is_object($user) && ( $user->user_login != $blogname ) ) ) + if ( ! is_object( $user ) || ( is_object( $user ) && ( $user->user_login != $blogname ) ) ) { $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) ); + } } // Has someone already signed up for this domain? - $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too? - if ( ! empty($signup) ) { - $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered); + $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) ); // TODO: Check email too? + if ( ! empty( $signup ) ) { + $diff = time() - mysql2date( 'U', $signup->registered ); // If registered more than two days ago, cancel registration and let this signup go through. - if ( $diff > 2 * DAY_IN_SECONDS ) - $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) ); - else - $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.')); + if ( $diff > 2 * DAY_IN_SECONDS ) { + $wpdb->delete( + $wpdb->signups, + array( + 'domain' => $mydomain, + 'path' => $path, + ) + ); + } else { + $errors->add( 'blogname', __( 'That site is currently reserved but may be available in a couple days.' ) ); + } } - $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors); + $result = array( + 'domain' => $mydomain, + 'path' => $path, + 'blogname' => $blogname, + 'blog_title' => $blog_title, + 'user' => $user, + 'errors' => $errors, + ); /** * Filters site details and error messages following registration. @@ -703,7 +746,7 @@ * @param string $user_email The user's email address. * @param array $meta Optional. Signup meta data. By default, contains the requested privacy setting and lang_id. */ -function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) { +function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) { global $wpdb; $key = substr( md5( time() . wp_rand() . $domain ), 0, 16 ); @@ -725,16 +768,19 @@ */ $meta = apply_filters( 'signup_site_meta', $meta, $domain, $path, $title, $user, $user_email, $key ); - $wpdb->insert( $wpdb->signups, array( - 'domain' => $domain, - 'path' => $path, - 'title' => $title, - 'user_login' => $user, - 'user_email' => $user_email, - 'registered' => current_time('mysql', true), - 'activation_key' => $key, - 'meta' => serialize( $meta ) - ) ); + $wpdb->insert( + $wpdb->signups, + array( + 'domain' => $domain, + 'path' => $path, + 'title' => $title, + 'user_login' => $user, + 'user_email' => $user_email, + 'registered' => current_time( 'mysql', true ), + 'activation_key' => $key, + 'meta' => serialize( $meta ), + ) + ); /** * Fires after site signup information has been written to the database. @@ -770,9 +816,9 @@ global $wpdb; // Format data - $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) ); + $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) ); $user_email = sanitize_email( $user_email ); - $key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 ); + $key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 ); /** * Filters the metadata for a user signup. @@ -788,16 +834,19 @@ */ $meta = apply_filters( 'signup_user_meta', $meta, $user, $user_email, $key ); - $wpdb->insert( $wpdb->signups, array( - 'domain' => '', - 'path' => '', - 'title' => '', - 'user_login' => $user, - 'user_email' => $user_email, - 'registered' => current_time('mysql', true), - 'activation_key' => $key, - 'meta' => serialize( $meta ) - ) ); + $wpdb->insert( + $wpdb->signups, + array( + 'domain' => '', + 'path' => '', + 'title' => '', + 'user_login' => $user, + 'user_email' => $user_email, + 'registered' => current_time( 'mysql', true ), + 'activation_key' => $key, + 'meta' => serialize( $meta ), + ) + ); /** * Fires after a user's signup information has been written to the database. @@ -856,19 +905,21 @@ } // Send email with activation link. - if ( !is_subdomain_install() || get_current_network_id() != 1 ) - $activate_url = network_site_url("wp-activate.php?key=$key"); - else + if ( ! is_subdomain_install() || get_current_network_id() != 1 ) { + $activate_url = network_site_url( "wp-activate.php?key=$key" ); + } else { $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API + } - $activate_url = esc_url($activate_url); - $admin_email = get_site_option( 'admin_email' ); - if ( $admin_email == '' ) + $activate_url = esc_url( $activate_url ); + $admin_email = get_site_option( 'admin_email' ); + if ( $admin_email == '' ) { $admin_email = 'support@' . $_SERVER['SERVER_NAME']; - $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) ); - $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; + } + $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) ); + $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; - $user = get_user_by( 'login', $user_login ); + $user = get_user_by( 'login', $user_login ); $switched_locale = switch_to_locale( get_user_locale( $user ) ); $message = sprintf( @@ -888,9 +939,16 @@ * @param string $key Activation key created in wpmu_signup_blog(). * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id. */ - apply_filters( 'wpmu_signup_blog_notification_email', - __( "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" ), - $domain, $path, $title, $user_login, $user_email, $key, $meta + apply_filters( + 'wpmu_signup_blog_notification_email', + __( "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" ), + $domain, + $path, + $title, + $user_login, + $user_email, + $key, + $meta ), $activate_url, esc_url( "http://{$domain}{$path}" ), @@ -912,10 +970,17 @@ * @param string $key Activation key created in wpmu_signup_blog(). * @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id. */ - apply_filters( 'wpmu_signup_blog_notification_subject', + apply_filters( + 'wpmu_signup_blog_notification_subject', /* translators: New site notification email subject. 1: Network name, 2: New site URL */ _x( '[%1$s] Activate %2$s', 'New site notification email subject' ), - $domain, $path, $title, $user_login, $user_email, $key, $meta + $domain, + $path, + $title, + $user_login, + $user_email, + $key, + $meta ), $from_name, esc_url( 'http://' . $domain . $path ) @@ -962,19 +1027,21 @@ * @param string $key Activation key created in wpmu_signup_user(). * @param array $meta Signup meta data. Default empty array. */ - if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) + if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) { return false; + } - $user = get_user_by( 'login', $user_login ); + $user = get_user_by( 'login', $user_login ); $switched_locale = switch_to_locale( get_user_locale( $user ) ); // Send email with activation link. $admin_email = get_site_option( 'admin_email' ); - if ( $admin_email == '' ) + if ( $admin_email == '' ) { $admin_email = 'support@' . $_SERVER['SERVER_NAME']; - $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) ); - $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n"; - $message = sprintf( + } + $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) ); + $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n"; + $message = sprintf( /** * Filters the content of the notification email for new user sign-up. * @@ -988,9 +1055,13 @@ * @param string $key Activation key created in wpmu_signup_user(). * @param array $meta Signup meta data. Default empty array. */ - apply_filters( 'wpmu_signup_user_notification_email', + apply_filters( + 'wpmu_signup_user_notification_email', __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ), - $user_login, $user_email, $key, $meta + $user_login, + $user_email, + $key, + $meta ), site_url( "wp-activate.php?key=$key" ) ); @@ -1007,10 +1078,14 @@ * @param string $key Activation key created in wpmu_signup_user(). * @param array $meta Signup meta data. Default empty array. */ - apply_filters( 'wpmu_signup_user_notification_subject', + apply_filters( + 'wpmu_signup_user_notification_subject', /* translators: New user notification email subject. 1: Network name, 2: New user login */ _x( '[%1$s] Activate %2$s', 'New user notification email subject' ), - $user_login, $user_email, $key, $meta + $user_login, + $user_email, + $key, + $meta ), $from_name, $user_login @@ -1039,41 +1114,53 @@ * @param string $key The activation key provided to the user. * @return array|WP_Error An array containing information about the activated user and/or blog */ -function wpmu_activate_signup($key) { +function wpmu_activate_signup( $key ) { global $wpdb; - $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) ); + $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) ); - if ( empty( $signup ) ) + if ( empty( $signup ) ) { return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) ); + } if ( $signup->active ) { - if ( empty( $signup->domain ) ) + if ( empty( $signup->domain ) ) { return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup ); - else + } else { return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup ); + } } - $meta = maybe_unserialize($signup->meta); + $meta = maybe_unserialize( $signup->meta ); $password = wp_generate_password( 12, false ); - $user_id = username_exists($signup->user_login); + $user_id = username_exists( $signup->user_login ); - if ( ! $user_id ) - $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email); - else + if ( ! $user_id ) { + $user_id = wpmu_create_user( $signup->user_login, $password, $signup->user_email ); + } else { $user_already_exists = true; + } + + if ( ! $user_id ) { + return new WP_Error( 'create_user', __( 'Could not create user' ), $signup ); + } + + $now = current_time( 'mysql', true ); - if ( ! $user_id ) - return new WP_Error('create_user', __('Could not create user'), $signup); - - $now = current_time('mysql', true); + if ( empty( $signup->domain ) ) { + $wpdb->update( + $wpdb->signups, + array( + 'active' => 1, + 'activated' => $now, + ), + array( 'activation_key' => $key ) + ); - if ( empty($signup->domain) ) { - $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); - - if ( isset( $user_already_exists ) ) - return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup); + if ( isset( $user_already_exists ) ) { + return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup ); + } /** * Fires immediately after a new user is activated. @@ -1085,23 +1172,41 @@ * @param array $meta Signup meta data. */ do_action( 'wpmu_activate_user', $user_id, $password, $meta ); - return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta ); + return array( + 'user_id' => $user_id, + 'password' => $password, + 'meta' => $meta, + ); } $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, get_current_network_id() ); // TODO: What to do if we create a user but cannot create a blog? - if ( is_wp_error($blog_id) ) { + if ( is_wp_error( $blog_id ) ) { // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and // setting the activation flag. Let's just set the active flag and instruct the user to reset their password. if ( 'blog_taken' == $blog_id->get_error_code() ) { $blog_id->add_data( $signup ); - $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) ); + $wpdb->update( + $wpdb->signups, + array( + 'active' => 1, + 'activated' => $now, + ), + array( 'activation_key' => $key ) + ); } return $blog_id; } - $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) ); + $wpdb->update( + $wpdb->signups, + array( + 'active' => 1, + 'activated' => $now, + ), + array( 'activation_key' => $key ) + ); /** * Fires immediately after a site is activated. * @@ -1115,7 +1220,13 @@ */ do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta ); - return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta); + return array( + 'blog_id' => $blog_id, + 'user_id' => $user_id, + 'password' => $password, + 'title' => $signup->title, + 'meta' => $meta, + ); } /** @@ -1137,8 +1248,9 @@ $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) ); $user_id = wp_create_user( $user_name, $password, $email ); - if ( is_wp_error( $user_id ) ) + if ( is_wp_error( $user_id ) ) { return false; + } // Newly created users have no roles or caps until they are added to a blog. delete_user_option( $user_id, 'capabilities' ); @@ -1174,7 +1286,7 @@ * @param string $path The new site's path. * @param string $title The new site's title. * @param int $user_id The user ID of the new site's admin. - * @param array $meta Optional. Array of key=>value pairs used to set initial site options. + * @param array $options Optional. Array of key=>value pairs used to set initial site options. * If valid status keys are included ('public', 'archived', 'mature', * 'spam', 'deleted', or 'lang_id') the given site status(es) will be * updated. Otherwise, keys and values will be used to set options for @@ -1182,67 +1294,47 @@ * @param int $network_id Optional. Network ID. Only relevant on multi-network installations. * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success. */ -function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 ) { +function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) { $defaults = array( 'public' => 0, - 'WPLANG' => get_network_option( $network_id, 'WPLANG' ), ); - $meta = wp_parse_args( $meta, $defaults ); + $options = wp_parse_args( $options, $defaults ); - $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) ); - - if ( is_subdomain_install() ) - $domain = str_replace( '@', '', $domain ); - - $title = strip_tags( $title ); + $title = strip_tags( $title ); $user_id = (int) $user_id; - if ( empty($path) ) - $path = '/'; - // Check if the domain has been used already. We should return an error message. - if ( domain_exists($domain, $path, $network_id) ) + if ( domain_exists( $domain, $path, $network_id ) ) { return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) ); + } if ( ! wp_installing() ) { wp_installing( true ); } - if ( ! $blog_id = insert_blog($domain, $path, $network_id) ) - return new WP_Error('insert_blog', __('Could not create site.')); - - switch_to_blog($blog_id); - install_blog($blog_id, $title); - wp_install_defaults($user_id); - - add_user_to_blog($blog_id, $user_id, 'administrator'); + $site_data_whitelist = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); - foreach ( $meta as $key => $value ) { - if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) - update_blog_status( $blog_id, $key, $value ); - else - update_option( $key, $value ); - } - - update_option( 'blog_public', (int) $meta['public'] ); + $site_data = array_merge( + array( + 'domain' => $domain, + 'path' => $path, + 'network_id' => $network_id, + ), + array_intersect_key( $options, array_flip( $site_data_whitelist ) ) + ); - if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) - update_user_meta( $user_id, 'primary_blog', $blog_id ); + // Data to pass to wp_initialize_site(). + $site_initialization_data = array( + 'title' => $title, + 'user_id' => $user_id, + 'options' => array_diff_key( $options, array_flip( $site_data_whitelist ) ), + ); - restore_current_blog(); - /** - * Fires immediately after a new site is created. - * - * @since MU (3.0.0) - * - * @param int $blog_id Site ID. - * @param int $user_id User ID. - * @param string $domain Site domain. - * @param string $path Site path. - * @param int $network_id Network ID. Only relevant on multi-network installations. - * @param array $meta Meta data. Used to set initial site options. - */ - do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta ); + $blog_id = wp_insert_site( array_merge( $site_data, $site_initialization_data ) ); + + if ( is_wp_error( $blog_id ) ) { + return $blog_id; + } wp_cache_set( 'last_changed', microtime(), 'sites' ); @@ -1256,32 +1348,47 @@ * the notification email. * * @since MU (3.0.0) + * @since 5.1.0 $blog_id now supports input from the {@see 'wp_initialize_site'} action. * - * @param int $blog_id The new site's ID. - * @param string $deprecated Not used. + * @param WP_Site|int $blog_id The new site's object or ID. + * @param string $deprecated Not used. * @return bool */ function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) { - if ( get_site_option( 'registrationnotification' ) != 'yes' ) + if ( is_object( $blog_id ) ) { + $blog_id = $blog_id->blog_id; + } + + if ( get_site_option( 'registrationnotification' ) != 'yes' ) { return false; + } $email = get_site_option( 'admin_email' ); - if ( is_email($email) == false ) + if ( is_email( $email ) == false ) { return false; + } - $options_site_url = esc_url(network_admin_url('settings.php')); + $options_site_url = esc_url( network_admin_url( 'settings.php' ) ); switch_to_blog( $blog_id ); $blogname = get_option( 'blogname' ); - $siteurl = site_url(); + $siteurl = site_url(); restore_current_blog(); /* translators: New site notification email. 1: Site URL, 2: User IP address, 3: Settings screen URL */ - $msg = sprintf( __( 'New Site: %1$s + $msg = sprintf( + __( + 'New Site: %1$s URL: %2$s Remote IP address: %3$s -Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url); +Disable these notifications: %4$s' + ), + $blogname, + $siteurl, + wp_unslash( $_SERVER['REMOTE_ADDR'] ), + $options_site_url + ); /** * Filters the message body of the new site activation email sent * to the network administrator. @@ -1308,22 +1415,31 @@ * @return bool */ function newuser_notify_siteadmin( $user_id ) { - if ( get_site_option( 'registrationnotification' ) != 'yes' ) + if ( get_site_option( 'registrationnotification' ) != 'yes' ) { return false; + } $email = get_site_option( 'admin_email' ); - if ( is_email($email) == false ) + if ( is_email( $email ) == false ) { return false; + } $user = get_userdata( $user_id ); - $options_site_url = esc_url(network_admin_url('settings.php')); + $options_site_url = esc_url( network_admin_url( 'settings.php' ) ); /* translators: New user notification email. 1: User login, 2: User IP address, 3: Settings screen URL */ - $msg = sprintf(__('New User: %1$s + $msg = sprintf( + __( + 'New User: %1$s Remote IP address: %2$s -Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url); +Disable these notifications: %3$s' + ), + $user->user_login, + wp_unslash( $_SERVER['REMOTE_ADDR'] ), + $options_site_url + ); /** * Filters the message body of the new user activation email sent @@ -1335,7 +1451,7 @@ * @param WP_User $user WP_User instance of the new user. */ $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user ); - wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg ); + wp_mail( $email, sprintf( __( 'New User Registration: %s' ), $user->user_login ), $msg ); return true; } @@ -1356,13 +1472,14 @@ * @return int|null The site ID if the site name exists, null otherwise. */ function domain_exists( $domain, $path, $network_id = 1 ) { - $path = trailingslashit( $path ); - $args = array( - 'network_id' => $network_id, - 'domain' => $domain, - 'path' => $path, - 'fields' => 'ids', - 'number' => 1, + $path = trailingslashit( $path ); + $args = array( + 'network_id' => $network_id, + 'domain' => $domain, + 'path' => $path, + 'fields' => 'ids', + 'number' => 1, + 'update_site_meta_cache' => false, ); $result = get_sites( $args ); $result = array_shift( $result ); @@ -1384,133 +1501,6 @@ } /** - * Store basic site info in the blogs table. - * - * This function creates a row in the wp_blogs table and returns - * the new blog's ID. It is the first step in creating a new blog. - * - * @since MU (3.0.0) - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param string $domain The domain of the new site. - * @param string $path The path of the new site. - * @param int $network_id Unless you're running a multi-network installation, be sure to set this value to 1. - * @return int|false The ID of the new row - */ -function insert_blog($domain, $path, $network_id) { - global $wpdb; - - $path = trailingslashit($path); - $network_id = (int) $network_id; - - $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $network_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) ); - if ( ! $result ) - return false; - - $blog_id = $wpdb->insert_id; - clean_blog_cache( $blog_id ); - - wp_maybe_update_network_site_counts( $network_id ); - - return $blog_id; -} - -/** - * Install an empty blog. - * - * Creates the new blog tables and options. If calling this function - * directly, be sure to use switch_to_blog() first, so that $wpdb - * points to the new blog. - * - * @since MU (3.0.0) - * - * @global wpdb $wpdb - * @global WP_Roles $wp_roles - * - * @param int $blog_id The value returned by insert_blog(). - * @param string $blog_title The title of the new site. - */ -function install_blog( $blog_id, $blog_title = '' ) { - global $wpdb, $wp_roles; - - // Cast for security - $blog_id = (int) $blog_id; - - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); - - $suppress = $wpdb->suppress_errors(); - if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) - die( '
' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '