/** * Invite a user as a contributor via POST vars. * AJAX handler for adding users. */ function annowf_invite_user() { check_ajax_referer('anno_create_user', '_ajax_nonce-create-user'); // Array for json response $data_array = array('code' => '', 'message' => '', 'user' => ''); // Only allow entering of email addresses, so a user can't create 'bobisdumb' as the username $user_id = anno_invite_contributor($_POST['user_email'], $_POST['user_email']); // Error creating user if (is_wp_error($user_id)) { $data_array['code'] = 'error'; $data_array['message'] = $user_id->get_error_message(); } else { //Pass the username back so it can be added as whatever role (co_author, reviewer) via JS. $data_array['code'] = 'success'; $data_array['message'] = _x('User has been created and added', 'status message for user creation', 'anno'); $users = get_users(array('include' => array($user_id), 'fields' => array('user_login'))); if (!empty($users) && is_array($users)) { $data_array['user'] = $users[0]->user_login; } } echo json_encode($data_array); die; }
/** * Creates users, whose credentials were created in get_author_mapping. Maps newly created users. */ function create_users() { $create_users = $this->allow_create_users(); if ($create_users) { $new_users = $this->new_user_credentials; foreach ($new_users as $i => $user_creds) { $old_id = $user_creds['old_id']; $extra = array('display_name' => $this->authors[$old_id]['author_display_name'], 'first_name' => $this->authors[$old_id]['author_first_name'], 'last_name' => $this->authors[$old_id]['author_last_name']); // Create the user, send them an email. $user_id = anno_invite_contributor($user_creds['user_login'], $user_creds['user_email'], $extra); // All checks for validity (username/email existance) have occured in get_author_mapping. // This should not produce an error under any normal circumstance, but just in case. if (is_wp_error($user_id) || empty($user_id)) { $user_id = get_current_user_id(); } else { // Keep track of the created users so we can // import their meta information on import $this->created_users[$old_id] = $user_id; } $this->processed_authors[$old_id] = $user_id; $this->author_mapping[$old_id] = $user_id; } } }