/** * Creates a new user, and sends that user an email. Returns a WP Error if the user was unable to be created. * * @param string $username Username to create * @param string $email Email of user to create * @return int|WP_Error ID of new user, or, WP_Error */ function anno_invite_contributor($user_login, $user_email, $extra = array()) { // Wish to be able to invite other contributors, so no create_user check $current_user = wp_get_current_user(); // wp_insert_user handles all other errors if (!anno_is_valid_email($user_email)) { return new WP_Error('invalid_email', _x('Invalid email address', 'error for creating new user', 'anno')); } // We don't want wp_insert_user to just sanitize the username stripping characters, the user should be alerted if the user input is wrong if (!anno_is_valid_username($user_login)) { return new WP_Error('invalid_username', _x('Invalid username', 'error for creating new user', 'anno')); } // Create User $user_pass = wp_generate_password(); $user_login = esc_sql($user_login); $user_email = esc_sql($user_email); $role = 'contributor'; $userdata = compact('user_login', 'user_pass', 'user_email', 'role'); array_merge($extra, $userdata); $user_id = wp_insert_user($userdata); $blogname = get_bloginfo('name'); // Send notifiction with PW, Username. if (!is_wp_error($user_id)) { $subject = sprintf(_x('You have been invited to join %s', 'email subject %s represents blogname', 'anno'), $blogname); $message = sprintf(_x('%s has created a user with your email address for %s. Please use the following credentials to login and change your password: Username: %s Password: %s %s', 'User creation email body. %s mapping: User who created this new user, blogname, username, password, profile url.', 'anno'), $current_user->display_name, $blogname, $user_login, $user_pass, esc_url(admin_url('profile.php'))); wp_mail($user_email, $subject, $message); } return $user_id; }
/** * Map old author logins to local user IDs based on decisions made * in import options form. Can map to an existing user, create a new user * or falls back to the current user in case of error with either of the previous */ function get_author_mapping() { if (!isset($_POST['imported_authors'])) { return; } $create_users = $this->allow_create_users(); $this->author_errors = array(); $this->new_user_credentials = array(); foreach ((array) $_POST['imported_authors'] as $i => $old_id) { // Used to determine whether or not we're creating a new user on import. $create_new_user = false; $user_id = 0; $old_id = trim($old_id); if (!empty($_POST['user_map'][$i])) { $user = get_userdata(intval($_POST['user_map'][$i])); if (isset($user->ID)) { if ($old_id) { $this->processed_authors[$old_id] = $user->ID; } $this->author_mapping[$old_id] = $user->ID; $user_id = $user->ID; } } else { if (!empty($_POST['lookup_email'][$i]) || !empty($_POST['lookup_username'][$i])) { // Validate email // Search via email if (!empty($_POST['lookup_email'][$i]) && !empty($_POST['lookup_username'][$i])) { $this->author_errors[$i][] = _x('Please enter an Email <strong>OR</strong> a Username to search for.', 'importer error message', 'anno'); } else { if (!empty($_POST['lookup_email'][$i])) { $lookup_email = $_POST['lookup_email'][$i]; if (!anno_is_valid_email($lookup_email)) { $this->author_errors[$i][] = _x('Please enter a valid email to search for.', 'importer error message', 'anno'); } else { $users = get_users(array('search' => $lookup_email)); if (empty($users)) { $this->author_errors[$i][] = sprintf(_x('Could not find user with email: %s', 'importer error message, %s: email address', 'anno'), $lookup_email); } else { if (is_array($users)) { $user_id = $users[0]->ID; } } } } else { if (!empty($_POST['lookup_username'][$i])) { $lookup_username = $_POST['lookup_username'][$i]; if (!anno_is_valid_username($lookup_username)) { $this->author_errors[$i][] = _x('Please enter a valid username to search for.', 'importer error message', 'anno'); } else { //@TODO only search user_login column $users = get_users(array('search' => $lookup_username)); if (empty($users)) { $this->author_errors[$i][] = sprintf(_x('Could not find user with username: %s', 'importer error message, %s: username', 'anno'), $lookup_username); } else { if (is_array($users)) { $user_id = $users[0]->ID; } } } } } } } else { if ($create_users && (empty($_POST['lookup_email'][$i]) && empty($_POST['lookup_username'][$i])) && !empty($_POST['user_new'][$i]['user_email'])) { if (!empty($_POST['user_new'][$i]['user_email'])) { // Username is email. $user_new_email = $user_new_login = $_POST['user_new'][$i]['user_email']; } else { $this->author_errors[$i][] = _x('Email cannot be empty when creating a new user.', 'importer error message', 'anno'); $user_new_email = null; } // email_exists($user_email) username_exists( $user_login ) if (email_exists($user_new_email) || username_exists($user_new_login)) { $this->author_errors[$i][] = _x('This email address is already registered.', 'importer error message', 'anno'); } if (!$this->have_author_errors($i)) { if (!anno_is_valid_email($user_new_email) || !anno_is_valid_username($user_new_login)) { $this->author_errors[$i][] = _x('Please enter a valid email when creating a new user.', 'importer error message', 'anno'); } if (!$this->have_author_errors($i)) { $this->new_user_credentials[$i]['old_id'] = $old_id; $this->new_user_credentials[$i]['user_login'] = $user_new_login; $this->new_user_credentials[$i]['user_email'] = $user_new_email; $create_new_user = true; } } } } } // user_id is empty, so no lookup was attempted, and we're not creating a new user if (empty($user_id) && !$create_new_user) { // Map to current user. $user_id = get_current_user_id(); } // Map users, $user_id is only set when we've found a user to map to. if (!$create_new_user && !empty($user_id)) { if ($old_id) { $this->processed_authors[$old_id] = $user_id; } $this->author_mapping[$old_id] = $user_id; } } }