/**
 * Set an email address as the primary for a customer from within the admin and log a customer note
 * and redirect back to the customer interface for feedback
 *
 * @since  2.6
 * @return void
 */
function edd_set_customer_primary_email()
{
    if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
        return false;
    }
    if (empty($_GET['email']) || !is_email($_GET['email'])) {
        return false;
    }
    if (empty($_GET['_wpnonce'])) {
        return false;
    }
    $nonce = $_GET['_wpnonce'];
    if (!wp_verify_nonce($nonce, 'edd-set-customer-primary-email')) {
        wp_die(__('Nonce verification failed', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    $customer = new EDD_Customer($_GET['id']);
    if ($customer->set_primary_email($_GET['email'])) {
        $url = add_query_arg('edd-message', 'primary-email-updated', admin_url('edit.php?post_type=download&page=edd-customers&view=overview&id=' . $customer->id));
        $user = wp_get_current_user();
        $user_login = !empty($user->user_login) ? $user->user_login : '******';
        $customer_note = __(sprintf('Email address %s set as primary by %s', $_GET['email'], $user_login), 'easy-digital-downloads');
        $customer->add_note($customer_note);
    } else {
        $url = add_query_arg('edd-message', 'primary-email-failed', admin_url('edit.php?post_type=download&page=edd-customers&view=overview&id=' . $customer->id));
    }
    wp_safe_redirect($url);
    exit;
}
/**
 * Process Profile Updater Form
 *
 * Processes the profile updater form by updating the necessary fields
 *
 * @since 1.4
 * @author Sunny Ratilal
 * @param array $data Data sent from the profile editor
 * @return void
 */
function edd_process_profile_editor_updates($data)
{
    // Profile field change request
    if (empty($_POST['edd_profile_editor_submit']) && !is_user_logged_in()) {
        return false;
    }
    // Pending users can't edit their profile
    if (edd_user_pending_verification()) {
        return false;
    }
    // Nonce security
    if (!wp_verify_nonce($data['edd_profile_editor_nonce'], 'edd-profile-editor-nonce')) {
        return false;
    }
    $user_id = get_current_user_id();
    $old_user_data = get_userdata($user_id);
    $display_name = isset($data['edd_display_name']) ? sanitize_text_field($data['edd_display_name']) : $old_user_data->display_name;
    $first_name = isset($data['edd_first_name']) ? sanitize_text_field($data['edd_first_name']) : $old_user_data->first_name;
    $last_name = isset($data['edd_last_name']) ? sanitize_text_field($data['edd_last_name']) : $old_user_data->last_name;
    $email = isset($data['edd_email']) ? sanitize_email($data['edd_email']) : $old_user_data->user_email;
    $line1 = isset($data['edd_address_line1']) ? sanitize_text_field($data['edd_address_line1']) : '';
    $line2 = isset($data['edd_address_line2']) ? sanitize_text_field($data['edd_address_line2']) : '';
    $city = isset($data['edd_address_city']) ? sanitize_text_field($data['edd_address_city']) : '';
    $state = isset($data['edd_address_state']) ? sanitize_text_field($data['edd_address_state']) : '';
    $zip = isset($data['edd_address_zip']) ? sanitize_text_field($data['edd_address_zip']) : '';
    $country = isset($data['edd_address_country']) ? sanitize_text_field($data['edd_address_country']) : '';
    $userdata = array('ID' => $user_id, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, 'user_email' => $email);
    $address = array('line1' => $line1, 'line2' => $line2, 'city' => $city, 'state' => $state, 'zip' => $zip, 'country' => $country);
    do_action('edd_pre_update_user_profile', $user_id, $userdata);
    // New password
    if (!empty($data['edd_new_user_pass1'])) {
        if ($data['edd_new_user_pass1'] !== $data['edd_new_user_pass2']) {
            edd_set_error('password_mismatch', __('The passwords you entered do not match. Please try again.', 'easy-digital-downloads'));
        } else {
            $userdata['user_pass'] = $data['edd_new_user_pass1'];
        }
    }
    // Make sure the new email doesn't belong to another user
    if ($email != $old_user_data->user_email) {
        // Make sure the new email is valid
        if (!is_email($email)) {
            edd_set_error('email_invalid', __('The email you entered is invalid. Please enter a valid email.', 'easy-digital-downloads'));
        }
        // Make sure the new email doesn't belong to another user
        if (email_exists($email)) {
            edd_set_error('email_exists', __('The email you entered belongs to another user. Please use another.', 'easy-digital-downloads'));
        }
    }
    // Check for errors
    $errors = edd_get_errors();
    if ($errors) {
        // Send back to the profile editor if there are errors
        wp_redirect($data['edd_redirect']);
        edd_die();
    }
    // Update the user
    $meta = update_user_meta($user_id, '_edd_user_address', $address);
    $updated = wp_update_user($userdata);
    // Possibly update the customer
    $customer = new EDD_Customer($user_id, true);
    if ($customer->email === $email || is_array($customer->emails) && in_array($email, $customer->emails)) {
        $customer->set_primary_email($email);
    }
    if ($customer->id > 0) {
        $update_args = array('name' => $first_name . ' ' . $last_name);
        $customer->update($update_args);
    }
    if ($updated) {
        do_action('edd_user_profile_updated', $user_id, $userdata);
        wp_redirect(add_query_arg('updated', 'true', $data['edd_redirect']));
        edd_die();
    }
}