PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.2.2
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.2.2
5.6.8 5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / customer_import_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months ago blocks_helper.php 1 year ago booking_helper.php 1 year ago bricks_helper.php 1 year ago bundles_helper.php 1 year ago calendar_helper.php 9 months ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months ago license_helper.php 1 year ago location_helper.php 1 year ago marketing_systems_helper.php 1 year ago meeting_systems_helper.php 1 year ago menu_helper.php 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months ago pages_helper.php 1 year ago params_helper.php 1 year ago payments_helper.php 1 year ago price_breakdown_helper.php 1 year ago process_jobs_helper.php 1 year ago processes_helper.php 1 year ago replacer_helper.php 1 year ago resource_helper.php 1 year ago roles_helper.php 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_helper.php 1 year ago work_periods_helper.php 1 year ago wp_datetime.php 1 year ago wp_user_helper.php 1 year ago
customer_import_helper.php
214 lines
1 <?php
2
3 class OsCustomerImportHelper {
4
5 /**
6 * Get an array of fields for mapping during customer import.
7 * @return array
8 */
9 public static function get_import_fields(): array {
10 $import_fields = [
11 '' => 'Do not import',
12 'first_name' => __( 'First Name', 'latepoint' ),
13 'last_name' => __( 'Last Name', 'latepoint' ),
14 'email' => __( 'Email', 'latepoint' ),
15 'phone' => __( 'Phone Number', 'latepoint' ),
16 'notes' => __( 'Notes', 'latepoint' ),
17 'admin_notes' => __( 'Admin Notes', 'latepoint' ),
18 ];
19
20 /**
21 * Returns an array of fields for mapping during customer import.
22 *
23 * @since 5.2.0
24 * @hook latepoint_customer_import_fields
25 *
26 * @param {array} $import_fields Array of fields for mapping during customer import
27 *
28 * @returns {array} array of fields for mapping during customer import
29 */
30 return apply_filters('latepoint_customer_import_fields', $import_fields);
31 }
32
33
34 /**
35 * Check if a customer can be imported by email.
36 * @param $email
37 * @return array|true[]
38 */
39 public static function check_import_client_by_email( $email = '' ):array {
40 if ( empty( $email ) || ! OsUtilHelper::is_valid_email( $email ) ) {
41 return ['status' => false, 'message' => esc_html__('Invalid email address: ' . $email, 'latepoint')];
42 }
43 $customer = new OsCustomerModel();
44 $customer = $customer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
45 if ($customer) {
46 return ['status' => false, 'message' => esc_html__('Customer with email already exists: ' . $email, 'latepoint')];
47 }
48 return ['status' => true];
49 }
50
51
52 /**
53 * Validate mapping between csv and fields from db
54 * @param array $column_mapping
55 * @return bool
56 */
57 public static function validate_import_mapping(array $column_mapping): bool {
58 if (empty($column_mapping)) {
59 return false;
60 }
61
62 $email_field_index = array_search('email', $column_mapping);
63 return $email_field_index !== false;
64 }
65
66
67 /**
68 * Get the temporary file path for the uploaded CSV file.
69 * @return string
70 * @throws Exception
71 */
72 public static function get_import_tmp_filepath( ): string {
73 $file_path = get_transient('csv_import_file_' . OsWpUserHelper::get_current_user_id());
74
75 if (empty($file_path) || !file_exists($file_path)) {
76 throw new Exception('Import file not found or expired. Please upload the file again.');
77 }
78
79 return $file_path;
80 }
81
82
83 /**
84 * Check CSV to find number of existing customers
85 * @param array $csv_data
86 * @param array $column_mapping
87 *
88 * @return array
89 */
90 public static function validate_csv_data(array $csv_data, array $column_mapping): array {
91 $email_field_index = array_search('email', $column_mapping);
92 $conflicts = [];
93 $importableCount = 0;
94
95 foreach ($csv_data as $row_index => $row_data) {
96 // Skip header row
97 if ($row_index === 0) {
98 continue;
99 }
100
101 $email = $row_data[$email_field_index] ?? '';
102 $validation_result = OsCustomerImportHelper::validate_customer_email($email);
103
104 if (!$validation_result['status']) {
105 $conflicts[$validation_result['type']][] = $validation_result['data'];
106 } else {
107 $importableCount++;
108 }
109 }
110
111 return [
112 'conflicts' => $conflicts,
113 'importable_count' => $importableCount
114 ];
115 }
116
117
118 /**
119 * Validate customer email
120 * @param string $email
121 * @return array
122 */
123 public static function validate_customer_email(string $email): array {
124 if (empty($email) || !OsUtilHelper::is_valid_email($email)) {
125 return [ 'status' => false, 'type' => 'invalid', 'data' => $email ];
126 }
127
128 $existingCustomer = new OsCustomerModel();
129 $existingCustomer = $existingCustomer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
130 if ($existingCustomer) {
131 return [ 'status' => false, 'type' => 'duplicate', 'data' => $email];
132 }
133
134 return ['status' => true];
135 }
136
137 /**
138 * Import customers from CSV
139 * @param array $csv_data
140 * @param array $column_mapping
141 * @param bool $update_existing
142 *
143 * @return array
144 */
145 public static function import_customers(array $csv_data, array $column_mapping, bool $update_existing = false): array {
146 $emailFieldIndex = array_search('email', $column_mapping);
147 $skippedCount = 0;
148 $updatedCount = 0;
149
150 foreach ($csv_data as $rowIndex => $rowData) {
151 // Skip header row
152 if ($rowIndex === 0) {
153 continue;
154 }
155
156 $email = $rowData[$emailFieldIndex] ?? '';
157
158 if (empty($email) || !OsUtilHelper::is_valid_email($email)) {
159 $skippedCount++;
160 continue;
161 }
162
163 $customer = new OsCustomerModel();
164 $customer = $customer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
165
166 // Skip if customer exists and update is not allowed
167 if ($customer && !$update_existing) {
168 $skippedCount++;
169 continue;
170 }
171
172 // Create new customer if not found
173 if (!$customer) {
174 $customer = new OsCustomerModel();
175 }
176
177 // Prepare save data
178 $save_data = [];
179 foreach ($rowData as $column_index => $field_value) {
180 if (!empty($column_mapping[$column_index])) {
181 $save_data[$column_mapping[$column_index]] = $field_value;
182 }
183 }
184 $customer->set_data($save_data);
185
186 if ($customer->save()) {
187 $updatedCount++;
188 do_action('latepoint_customer_imported', $customer, $rowData, $column_mapping);
189 } else {
190 $skippedCount++;
191 }
192 }
193
194 return [
195 'skipped_count' => $skippedCount,
196 'updated_count' => $updatedCount
197 ];
198 }
199
200 /**
201 * Delete temp file
202 * @return void
203 */
204 public static function cleanup_stored_file(): void {
205 $file_name = 'csv_import_file_' . OsWpUserHelper::get_current_user_id();
206 $file_path = get_transient($file_name);
207
208 if ($file_path && file_exists($file_path)) {
209 unlink($file_path);
210 delete_transient($file_name);
211 }
212 }
213
214 }