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; }