/** * Add an email address to the customer from within the admin and log a customer note * * @since 2.6 * @param array $args Array of arguments: nonce, customer id, and email address * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string) */ function edd_add_customer_email($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.', 'easy-digital-downloads')); } $output = array(); if (empty($args) || empty($args['email']) || empty($args['customer_id'])) { $output['success'] = false; if (empty($args['email'])) { $output['message'] = __('Email address is required.', 'easy-digital-downloads'); } else { if (empty($args['customer_id'])) { $output['message'] = __('Customer ID is required.', 'easy-digital-downloads'); } else { $output['message'] = __('An error has occured. Please try again.', 'easy-digital-downloads'); } } } else { if (!wp_verify_nonce($args['_wpnonce'], 'edd-add-customer-email')) { $output = array('success' => false, 'message' => __('Nonce verification failed.', 'easy-digital-downloads')); } else { if (!is_email($args['email'])) { $output = array('success' => false, 'message' => __('Invalid email address.', 'easy-digital-downloads')); } else { $email = sanitize_email($args['email']); $customer_id = (int) $args['customer_id']; $primary = 'true' === $args['primary'] ? true : false; $customer = new EDD_Customer($customer_id); if (false === $customer->add_email($email, $primary)) { if (in_array($email, $customer->emails)) { $output = array('success' => false, 'message' => __('Email already associated with this customer.', 'easy-digital-downloads')); } else { $output = array('success' => false, 'message' => __('Email address is already associated with another customer.', 'easy-digital-downloads')); } } else { $redirect = admin_url('edit.php?post_type=download&page=edd-customers&view=overview&id=' . $customer_id . '&edd-message=email-added'); $output = array('success' => true, 'message' => __('Email successfully added to customer.', 'easy-digital-downloads'), 'redirect' => $redirect); $user = wp_get_current_user(); $user_login = !empty($user->user_login) ? $user->user_login : '******'; $customer_note = __(sprintf('Email address %s added by %s', $email, $user_login), 'easy-digital-downloads'); $customer->add_note($customer_note); if ($primary) { $customer_note = __(sprintf('Email address %s set as primary by %s', $email, $user_login), 'easy-digital-downloads'); $customer->add_note($customer_note); } } } } } do_action('edd_post_add_customer_email', $customer_id, $args); if (defined('DOING_AJAX') && DOING_AJAX) { header('Content-Type: application/json'); echo json_encode($output); wp_die(); } return $output; }
private function set_customer($row) { global $wpdb; if (!empty($this->field_mapping['email']) && !empty($row[$this->field_mapping['email']])) { $email = sanitize_text_field($row[$this->field_mapping['email']]); } // Look for a customer from the canonical source, if any if (!empty($this->field_mapping['customer_id']) && !empty($row[$this->field_mapping['customer_id']])) { $canonical_id = absint($row[$this->field_mapping['customer_id']]); $mapped_id = $wpdb->get_var($wpdb->prepare("SELECT customer_id FROM {$wpdb->customermeta} WHERE meta_key = '_canonical_import_id' AND meta_value = %d LIMIT 1", $canonical_id)); } if (!empty($mapped_id)) { $customer = new EDD_Customer($mapped_id); } if (empty($mapped_id) || !$customer->id > 0) { // Look for a customer based on provided ID, if any if (!empty($this->field_mapping['customer_id']) && !empty($row[$this->field_mapping['customer_id']])) { $customer_id = absint($row[$this->field_mapping['customer_id']]); $customer_by_id = new EDD_Customer($customer_id); } // Now look for a customer based on provided email if (!empty($email)) { $customer_by_email = new EDD_Customer($email); } // Now compare customer records. If they don't match, customer_id will be stored in meta and we will use the customer that matches the email if ((empty($customer_by_id) || $customer_by_id->id !== $customer_by_email->id) && !empty($customer_by_email)) { $customer = $customer_by_email; } else { if (!empty($customer_by_id)) { $customer = $customer_by_id; if (!empty($email)) { $customer->add_email($email); } } } // Make sure we found a customer. Create one if not. if (empty($customer->id)) { if (!is_a($customer, 'EDD_Customer')) { $customer = new EDD_Customer(); } $first_name = ''; $last_name = ''; if (!empty($this->field_mapping['first_name']) && !empty($row[$this->field_mapping['first_name']])) { $first_name = sanitize_text_field($row[$this->field_mapping['first_name']]); } if (!empty($this->field_mapping['last_name']) && !empty($row[$this->field_mapping['last_name']])) { $last_name = sanitize_text_field($row[$this->field_mapping['last_name']]); } $customer->create(array('name' => $first_name . ' ' . $last_name, 'email' => $email)); if (!empty($canonical_id) && (int) $canonical_id !== (int) $customer->id) { $customer->update_meta('_canonical_import_id', $canonical_id); } } } if ($email && $email != $customer->email) { $customer->add_email($email); } return $customer->id; }