/**
  * Get the Export Data
  *
  * @access public
  * @since 2.5
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @return array $data The data for the CSV file
  */
 public function get_data()
 {
     $args = array('number' => $this->per_step, 'offset' => $this->per_step * ($this->step - 1), 'orderby' => 'id', 'order' => 'DESC');
     $customers = EDD()->customers->get_customers($args);
     if ($customers) {
         foreach ($customers as $customer) {
             $attached_payment_ids = explode(',', $customer->payment_ids);
             $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1);
             $attached_payments = edd_get_payments($attached_args);
             $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'meta_query' => array(array('key' => '_edd_payment_user_email', 'value' => $customer->email, 'compare' => '=')));
             $unattached_payments = edd_get_payments($unattached_args);
             $payments = array_merge($attached_payments, $unattached_payments);
             $purchase_value = 0.0;
             $purchase_count = 0;
             $payment_ids = array();
             if ($payments) {
                 foreach ($payments as $payment) {
                     if ('publish' == $payment->post_status || 'revoked' == $payment->post_status) {
                         $purchase_value += edd_get_payment_amount($payment->ID);
                         $purchase_count++;
                     }
                     $payment_ids[] = $payment->ID;
                 }
             }
             $payment_ids = implode(',', $payment_ids);
             $customer_update_data = array('purchase_count' => $purchase_count, 'purchase_value' => $purchase_value, 'payment_ids' => $payment_ids);
             $customer_instance = new EDD_Customer($customer->id);
             $customer_instance->update($customer_update_data);
         }
         return true;
     }
     return false;
 }
 /**
  * Maybe create a user when payment is created
  *
  * @since 1.3
  */
 public function maybe_insert_user($payment_id, $payment_data)
 {
     // User account already associated
     if ($payment_data['user_info']['id'] > 0) {
         return;
     }
     // User account already exists
     if (get_user_by('email', $payment_data['user_info']['email'])) {
         return;
     }
     $user_name = sanitize_user($payment_data['user_info']['email']);
     // Username already exists
     if (username_exists($user_name)) {
         return;
     }
     // Okay we need to create a user and possibly log them in
     $user_args = apply_filters('edd_auto_register_insert_user_args', array('user_login' => $user_name, 'user_pass' => wp_generate_password(32), 'user_email' => $payment_data['user_info']['email'], 'first_name' => $payment_data['user_info']['first_name'], 'last_name' => $payment_data['user_info']['last_name'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')), $payment_id, $payment_data);
     // Insert new user
     $user_id = wp_insert_user($user_args);
     // Validate inserted user
     if (is_wp_error($user_id)) {
         return;
     }
     $payment_meta = edd_get_payment_meta($payment_id);
     $payment_meta['user_info']['id'] = $user_id;
     edd_update_payment_meta($payment_id, '_edd_payment_user_id', $user_id);
     edd_update_payment_meta($payment_id, '_edd_payment_meta', $payment_meta);
     $customer = new EDD_Customer($payment_data['user_info']['email']);
     $customer->update(array('user_id' => $user_id));
     // Allow themes and plugins to hook
     do_action('edd_auto_register_insert_user', $user_id, $user_args, $payment_id);
     if (function_exists('did_action') && did_action('edd_purchase')) {
         // Only log user in if processing checkout screen
         edd_log_user_in($user_id, $user_args['user_login'], $user_args['user_pass']);
     }
 }
/**
 * Insert Payment
 *
 * @since 1.0
 * @param array $payment_data
 * @return int|bool Payment ID if payment is inserted, false otherwise
 */
function edd_insert_payment($payment_data = array())
{
    if (empty($payment_data)) {
        return false;
    }
    // Make sure the payment is inserted with the correct timezone
    date_default_timezone_set(edd_get_timezone_id());
    // Construct the payment title
    if (isset($payment_data['user_info']['first_name']) || isset($payment_data['user_info']['last_name'])) {
        $payment_title = $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'];
    } else {
        $payment_title = $payment_data['user_email'];
    }
    // Retrieve the ID of the discount used, if any
    if ($payment_data['user_info']['discount'] != 'none') {
        $discount = edd_get_discount_by('code', $payment_data['user_info']['discount']);
    }
    // Find the next payment number, if enabled
    if (edd_get_option('enable_sequential')) {
        $number = edd_get_next_payment_number();
    }
    $args = apply_filters('edd_insert_payment_args', array('post_title' => $payment_title, 'post_status' => isset($payment_data['status']) ? $payment_data['status'] : 'pending', 'post_type' => 'edd_payment', 'post_parent' => isset($payment_data['parent']) ? $payment_data['parent'] : null, 'post_date' => isset($payment_data['post_date']) ? $payment_data['post_date'] : null, 'post_date_gmt' => isset($payment_data['post_date']) ? get_gmt_from_date($payment_data['post_date']) : null), $payment_data);
    // Create a blank payment
    $payment = wp_insert_post($args);
    if ($payment) {
        if (isset($payment_data['tax'])) {
            $cart_tax = $payment_data['tax'];
        } else {
            $taxes = $payment_data['cart_details'] ? wp_list_pluck($payment_data['cart_details'], 'tax') : array();
            $cart_tax = array_sum($taxes);
            $cart_tax += edd_get_cart_fee_tax();
        }
        $payment_meta = array('currency' => $payment_data['currency'], 'downloads' => $payment_data['downloads'], 'user_info' => $payment_data['user_info'], 'cart_details' => $payment_data['cart_details']);
        $mode = edd_is_test_mode() ? 'test' : 'live';
        $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : '';
        $gateway = empty($gateway) && isset($_POST['edd-gateway']) ? $_POST['edd-gateway'] : $gateway;
        if (!$payment_data['price']) {
            // Ensures the _edd_payment_total meta key is created for purchases with an amount of 0
            $payment_data['price'] = '0.00';
        }
        // Create or update a customer
        $customer = new EDD_Customer($payment_data['user_email']);
        $customer_data = array('name' => $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'], 'email' => $payment_data['user_email'], 'user_id' => $payment_data['user_info']['id']);
        if (empty($customer->id)) {
            $customer->create($customer_data);
        } else {
            // Only update the customer if their name or email has changed
            if ($customer_data['email'] !== $customer->email || $customer_data['name'] !== $customer->name) {
                // We shouldn't be updating the User ID here, that is an admin task
                unset($customer_data['user_id']);
                $customer->update($customer_data);
            }
        }
        $customer->attach_payment($payment, false);
        // Record the payment details
        edd_update_payment_meta($payment, '_edd_payment_meta', apply_filters('edd_payment_meta', $payment_meta, $payment_data));
        edd_update_payment_meta($payment, '_edd_payment_user_id', $payment_data['user_info']['id']);
        edd_update_payment_meta($payment, '_edd_payment_customer_id', $customer->id);
        edd_update_payment_meta($payment, '_edd_payment_user_email', $payment_data['user_email']);
        edd_update_payment_meta($payment, '_edd_payment_user_ip', edd_get_ip());
        edd_update_payment_meta($payment, '_edd_payment_purchase_key', $payment_data['purchase_key']);
        edd_update_payment_meta($payment, '_edd_payment_total', $payment_data['price']);
        edd_update_payment_meta($payment, '_edd_payment_mode', $mode);
        edd_update_payment_meta($payment, '_edd_payment_gateway', $gateway);
        edd_update_payment_meta($payment, '_edd_payment_tax', $cart_tax);
        if (!empty($discount)) {
            edd_update_payment_meta($payment, '_edd_payment_discount_id', $discount->ID);
        }
        if (edd_get_option('enable_sequential')) {
            edd_update_payment_meta($payment, '_edd_payment_number', edd_format_payment_number($number));
            update_option('edd_last_payment_number', $number);
        }
        // Clear the user's purchased cache
        delete_transient('edd_user_' . $payment_data['user_info']['id'] . '_purchases');
        do_action('edd_insert_payment', $payment, $payment_data);
        return $payment;
        // Return the ID
    }
    // Return false if no payment was inserted
    return false;
}
 /**
  * Processes the supplied payment data to possibly register a user
  *
  * @since  1.3.3
  * @param  array   $payment_data The Payment data
  * @param  int     $payment_id   The payment ID
  * @return int|WP_Error          The User ID created or an instance of WP_Error if the insert fails
  */
 public function create_user($payment_data = array(), $payment_id = 0)
 {
     // User account already associated
     if ($payment_data['user_info']['id'] > 0) {
         return false;
     }
     // User account already exists
     if (get_user_by('email', $payment_data['user_info']['email'])) {
         return false;
     }
     $user_name = sanitize_user($payment_data['user_info']['email']);
     // Username already exists
     if (username_exists($user_name)) {
         return false;
     }
     // Okay we need to create a user and possibly log them in
     // Since this filter existed before, we must send in a $payment_id, which we default to false if none is supplied
     $user_args = apply_filters('edd_auto_register_insert_user_args', array('user_login' => $user_name, 'user_pass' => wp_generate_password(32), 'user_email' => $payment_data['user_info']['email'], 'first_name' => $payment_data['user_info']['first_name'], 'last_name' => $payment_data['user_info']['last_name'], 'user_registered' => date('Y-m-d H:i:s'), 'role' => get_option('default_role')), $payment_id, $payment_data);
     // Insert new user
     $user_id = wp_insert_user($user_args);
     if (!is_wp_error($user_id)) {
         // Allow themes and plugins to hook
         do_action('edd_auto_register_insert_user', $user_id, $user_args, $payment_id);
         $maybe_login_user = function_exists('did_action') && did_action('edd_purchase');
         $maybe_login_user = apply_filters('edd_auto_register_login_user', $maybe_login_user);
         if (true === $maybe_login_user) {
             edd_log_user_in($user_id, $user_args['user_login'], $user_args['user_pass']);
         }
         $customer = new EDD_Customer($payment_data['user_info']['email']);
         $customer->update(array('user_id' => $user_id));
     }
     return $user_id;
 }
/**
 * Run the upgrade for the customers to find all payment attachments
 *
 * @since  2.3
 * @return void
 */
function edd_v23_upgrade_customer_purchases()
{
    global $wpdb;
    if (!current_user_can('manage_shop_settings')) {
        wp_die(__('You do not have permission to do shop upgrades', 'edd'), __('Error', 'edd'), array('response' => 403));
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        @set_time_limit(0);
    }
    $step = isset($_GET['step']) ? absint($_GET['step']) : 1;
    $number = 50;
    $offset = $step == 1 ? 0 : ($step - 1) * $number;
    if ($step < 2) {
        // Check if we have any payments before moving on
        $sql = "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'edd_payment' LIMIT 1";
        $has_payments = $wpdb->get_col($sql);
        if (empty($has_payments)) {
            // We had no payments, just complete
            update_option('edd_version', preg_replace('/[^0-9.].*/', '', EDD_VERSION));
            edd_set_upgrade_complete('upgrade_customer_payments_association');
            delete_option('edd_doing_upgrade');
            wp_redirect(admin_url());
            exit;
        }
    }
    $total = isset($_GET['total']) ? absint($_GET['total']) : false;
    if (empty($total) || $total <= 1) {
        $total = EDD()->customers->count();
    }
    $customers = EDD()->customers->get_customers(array('number' => $number, 'offset' => $offset));
    if (!empty($customers)) {
        foreach ($customers as $customer) {
            // Get payments by email and user ID
            $select = "SELECT ID FROM {$wpdb->posts} p ";
            $join = "LEFT JOIN {$wpdb->postmeta} m ON p.ID = m.post_id ";
            $where = "WHERE p.post_type = 'edd_payment' ";
            if (!empty($customer->user_id) && intval($customer->user_id) > 0) {
                $where .= "AND ( ( m.meta_key = '_edd_payment_user_email' AND m.meta_value = '{$customer->email}' ) OR ( m.meta_key = '_edd_payment_customer_id' AND m.meta_value = '{$customer->id}' ) OR ( m.meta_key = '_edd_payment_user_id' AND m.meta_value = '{$customer->user_id}' ) )";
            } else {
                $where .= "AND ( ( m.meta_key = '_edd_payment_user_email' AND m.meta_value = '{$customer->email}' ) OR ( m.meta_key = '_edd_payment_customer_id' AND m.meta_value = '{$customer->id}' ) ) ";
            }
            $sql = $select . $join . $where;
            $found_payments = $wpdb->get_col($sql);
            $unique_payment_ids = array_unique(array_filter($found_payments));
            if (!empty($unique_payment_ids)) {
                $unique_ids_string = implode(',', $unique_payment_ids);
                $customer_data = array('payment_ids' => $unique_ids_string);
                $purchase_value_sql = "SELECT SUM( m.meta_value ) FROM {$wpdb->postmeta} m LEFT JOIN {$wpdb->posts} p ON m.post_id = p.ID WHERE m.post_id IN ( {$unique_ids_string} ) AND p.post_status IN ( 'publish', 'revoked' ) AND m.meta_key = '_edd_payment_total'";
                $purchase_value = $wpdb->get_col($purchase_value_sql);
                $purchase_count_sql = "SELECT COUNT( m.post_id ) FROM {$wpdb->postmeta} m LEFT JOIN {$wpdb->posts} p ON m.post_id = p.ID WHERE m.post_id IN ( {$unique_ids_string} ) AND p.post_status IN ( 'publish', 'revoked' ) AND m.meta_key = '_edd_payment_total'";
                $purchase_count = $wpdb->get_col($purchase_count_sql);
                if (!empty($purchase_value) && !empty($purchase_count)) {
                    $purchase_value = $purchase_value[0];
                    $purchase_count = $purchase_count[0];
                    $customer_data['purchase_count'] = $purchase_count;
                    $customer_data['purchase_value'] = $purchase_value;
                }
            } else {
                $customer_data['purchase_count'] = 0;
                $customer_data['purchase_value'] = 0;
                $customer_data['payment_ids'] = '';
            }
            if (!empty($customer_data)) {
                $customer = new EDD_Customer($customer->id);
                $customer->update($customer_data);
            }
        }
        // More Payments found so upgrade them
        $step++;
        $redirect = add_query_arg(array('page' => 'edd-upgrades', 'edd-upgrade' => 'upgrade_customer_payments_association', 'step' => $step, 'number' => $number, 'total' => $total), admin_url('index.php'));
        wp_redirect($redirect);
        exit;
    } else {
        // No more customers found, finish up
        update_option('edd_version', preg_replace('/[^0-9.].*/', '', EDD_VERSION));
        edd_set_upgrade_complete('upgrade_customer_payments_association');
        delete_option('edd_doing_upgrade');
        wp_redirect(admin_url());
        exit;
    }
}
/**
 * Disconnect a user ID from a customer
 *
 * @since  2.3
 * @param  array $args Array of arguements
 * @return bool        If the disconnect was sucessful
 */
function edd_disconnect_customer_user_id($args)
{
    $customer_edit_role = apply_filters('edd_edit_customers_role', 'edit_shop_payments');
    if (!is_admin() || !current_user_can($customer_edit_role)) {
        wp_die(__('You do not have permission to edit this customer.', 'edd'));
    }
    if (empty($args)) {
        return;
    }
    $customer_id = (int) $args['customer_id'];
    $nonce = $args['_wpnonce'];
    if (!wp_verify_nonce($nonce, 'edit-customer')) {
        wp_die(__('Cheatin\' eh?!', 'edd'));
    }
    $customer = new EDD_Customer($customer_id);
    if (empty($customer->id)) {
        return false;
    }
    do_action('edd_pre_customer_disconnect_user_id', $customer_id, $customer->user_id);
    $customer_args = array('user_id' => 0);
    if ($customer->update($customer_args)) {
        global $wpdb;
        if (!empty($customer->payment_ids)) {
            $wpdb->query("UPDATE {$wpdb->postmeta} SET meta_value = 0 WHERE meta_key = '_edd_payment_user_id' AND post_id IN ( {$customer->payment_ids} )");
        }
        $output['success'] = true;
    } else {
        $output['success'] = false;
        edd_set_error('edd-disconnect-user-fail', __('Failed to disconnect user from customer', 'edd'));
    }
    do_action('edd_post_customer_disconnect_user_id', $customer_id);
    if (defined('DOING_AJAX') && DOING_AJAX) {
        header('Content-Type: application/json');
        echo json_encode($output);
        wp_die();
    }
    return $output;
}
 /**
  * Zero out the data on step one
  *
  * @access public
  * @since 2.5
  * @return void
  */
 public function pre_fetch()
 {
     if ($this->step === 1) {
         // Before we start, let's zero out the customer's data
         $customer = new EDD_Customer($this->customer_id);
         $customer->update(array('purchase_value' => edd_format_amount(0), 'purchase_count' => 0));
         $attached_payment_ids = explode(',', $customer->payment_ids);
         $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1);
         $attached_payments = edd_get_payments($attached_args);
         $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'meta_query' => array(array('key' => '_edd_payment_user_email', 'value' => $customer->email)));
         $unattached_payments = edd_get_payments($unattached_args);
         $payments = array_merge($attached_payments, $unattached_payments);
         $this->store_data('edd_recount_customer_payments_' . $customer->id, $payments);
     }
 }
/**
 * 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->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();
    }
}
/**
 * When a user is deleted, detach that user id from the customer record
 *
 * @since  2.5
 * @param  int $user_id The User ID being deleted
 * @return bool         If the detachment was successful
 */
function edd_detach_deleted_user($user_id)
{
    $customer = new EDD_Customer($user_id, true);
    $detached = false;
    if ($customer->id > 0) {
        $detached = $customer->update(array('user_id' => 0));
    }
    do_action('edd_detach_deleted_user', $user_id, $customer, $detached);
    return $detached;
}
/**
 * Attach the newly created user_id to a customer, if one exists
 *
 * @since  2.4.6
 * @param  int $user_id The User ID that was created
 * @return void
 */
function edd_connect_existing_customer_to_new_user($user_id)
{
    $email = get_the_author_meta('user_email', $user_id);
    // Update the user ID on the customer
    $customer = new EDD_Customer($email);
    if ($customer->id > 0) {
        $customer->update(array('user_id' => $user_id));
    }
}