public function test_update_customer() { $test_email = '*****@*****.**'; $customer = new Give_Customer($test_email); $customer_id = $customer->create(array('email' => $test_email)); $this->assertEquals($customer_id, $customer->id); $data_to_update = array('email' => '*****@*****.**', 'name' => 'Test Account'); $customer->update($data_to_update); $this->assertEquals('*****@*****.**', $customer->email); $this->assertEquals('Test Account', $customer->name); // Verify if we have an empty array we get false $this->assertFalse($customer->update()); }
/** * Get the Export Data * * @access public * @since 1.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 = Give()->customers->get_customers($args); if ($customers) { $allowed_payment_status = apply_filters('give_recount_customer_payment_statuses', give_get_payment_status_keys()); foreach ($customers as $customer) { $attached_payment_ids = explode(',', $customer->payment_ids); $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status); $attached_payments = (array) give_get_payments($attached_args); $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status, 'meta_query' => array(array('key' => '_give_payment_user_email', 'value' => $customer->email, 'compare' => '='))); $unattached_payments = give_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) { $should_process_payment = 'publish' == $payment->post_status ? true : false; $should_process_payment = apply_filters('give_customer_recount_should_process_payment', $should_process_payment, $payment); if (true === $should_process_payment) { if (apply_filters('give_customer_recount_should_increase_value', true, $payment)) { $purchase_value += give_get_payment_amount($payment->ID); } if (apply_filters('give_customer_recount_should_increase_count', true, $payment)) { $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 Give_Customer($customer->id); $customer_instance->update($customer_update_data); } return true; } return false; }
/** * Zero out the data on step one * * @access public * @since 1.5 * @return void */ public function pre_fetch() { if ($this->step === 1) { $allowed_payment_status = apply_filters('give_recount_customer_payment_statuses', give_get_payment_status_keys()); // Before we start, let's zero out the customer's data $customer = new Give_Customer($this->customer_id); $customer->update(array('purchase_value' => give_format_amount(0), 'purchase_count' => 0)); $attached_payment_ids = explode(',', $customer->payment_ids); $attached_args = array('post__in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status); $attached_payments = give_get_payments($attached_args); $unattached_args = array('post__not_in' => $attached_payment_ids, 'number' => -1, 'status' => $allowed_payment_status, 'meta_query' => array(array('key' => '_give_payment_user_email', 'value' => $customer->email))); $unattached_payments = give_get_payments($unattached_args); $payments = array_merge($attached_payments, $unattached_payments); $this->store_data('give_recount_customer_payments_' . $customer->id, $payments); } }
/** * Disconnect a user ID from a donor * * @since 1.0 * * @param array $args Array of arguements * * @return bool If the disconnect was sucessful */ function give_disconnect_customer_user_id($args) { $customer_edit_role = apply_filters('give_edit_customers_role', 'edit_give_payments'); if (!is_admin() || !current_user_can($customer_edit_role)) { wp_die(__('You do not have permission to edit this donor.', 'give')); } if (empty($args)) { return; } $customer_id = (int) $args['customer_id']; $nonce = $args['_wpnonce']; if (!wp_verify_nonce($nonce, 'edit-customer')) { wp_die(__('Cheatin\' eh?!', 'give')); } $customer = new Give_Customer($customer_id); if (empty($customer->id)) { return false; } do_action('give_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 = '_give_payment_user_id' AND post_id IN ( {$customer->payment_ids} )"); } $output['success'] = true; } else { $output['success'] = false; give_set_error('give-disconnect-user-fail', __('Failed to disconnect user from donor', 'give')); } do_action('give_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; }
/** * Insert Payment * * @since 1.0 * * @param array $payment_data * * @return int|bool Payment ID if payment is inserted, false otherwise */ function give_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(give_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']; } // Find the next payment number, if enabled if (give_get_option('enable_sequential')) { $number = give_get_next_payment_number(); } $args = apply_filters('give_insert_payment_args', array('post_title' => $payment_title, 'post_status' => isset($payment_data['status']) ? $payment_data['status'] : 'pending', 'post_type' => 'give_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) { $payment_meta = array('currency' => $payment_data['currency'], 'form_title' => $payment_data['give_form_title'], 'form_id' => $payment_data['give_form_id'], 'price_id' => give_get_price_id($payment_data['give_form_id'], $payment_data['price']), 'user_info' => $payment_data['user_info']); $mode = give_is_test_mode() ? 'test' : 'live'; $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : ''; $gateway = empty($gateway) && isset($_POST['give-gateway']) ? $_POST['give-gateway'] : $gateway; if (!$payment_data['price']) { // Ensures the _give_payment_total meta key is created for donations with an amount of 0 $payment_data['price'] = '0.00'; } // Create or update a customer $customer = new Give_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 give_update_payment_meta($payment, '_give_payment_meta', apply_filters('give_payment_meta', $payment_meta, $payment_data)); give_update_payment_meta($payment, '_give_payment_user_id', $payment_data['user_info']['id']); give_update_payment_meta($payment, '_give_payment_donor_id', $customer->id); give_update_payment_meta($payment, '_give_payment_user_email', $payment_data['user_email']); give_update_payment_meta($payment, '_give_payment_user_ip', give_get_ip()); give_update_payment_meta($payment, '_give_payment_purchase_key', $payment_data['purchase_key']); give_update_payment_meta($payment, '_give_payment_total', $payment_data['price']); give_update_payment_meta($payment, '_give_payment_mode', $mode); give_update_payment_meta($payment, '_give_payment_gateway', $gateway); if (give_get_option('enable_sequential')) { give_update_payment_meta($payment, '_give_payment_number', give_format_payment_number($number)); update_option('give_last_payment_number', $number); } // Clear the user's purchased cache delete_transient('give_user_' . $payment_data['user_info']['id'] . '_purchases'); do_action('give_insert_payment', $payment, $payment_data); return $payment; // Return the ID } // Return false if no payment was inserted return false; }