PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.6.6
LatePoint – Calendar Booking Plugin for Appointments and Events v5.6.6
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 3 months ago agent_helper.php 3 months ago analytics_helper.php 1 week ago auth_helper.php 6 days ago blocks_helper.php 3 weeks ago booking_helper.php 4 days ago bricks_helper.php 3 months ago bundles_helper.php 4 days ago calendar_helper.php 2 days ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 1 month ago customer_import_helper.php 1 month ago database_helper.php 1 week ago debug_helper.php 3 months ago defaults_helper.php 3 months ago elementor_helper.php 3 months ago email_helper.php 3 months ago encrypt_helper.php 3 months ago events_helper.php 3 months ago form_helper.php 3 months ago icalendar_helper.php 3 months ago image_helper.php 3 months ago invoices_helper.php 3 weeks ago license_helper.php 3 months ago location_helper.php 3 months ago marketing_systems_helper.php 3 months ago meeting_systems_helper.php 3 months ago menu_helper.php 3 weeks ago meta_helper.php 3 months ago migrations_helper.php 3 months ago money_helper.php 3 months ago notifications_helper.php 3 months ago nps_survey_helper.php 3 months ago order_intent_helper.php 2 months ago orders_helper.php 3 months ago otp_helper.php 3 months ago pages_helper.php 3 months ago params_helper.php 3 months ago payments_helper.php 2 months ago plugin_version_update_helper.php 2 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago razorpay_connect_helper.php 1 week ago replacer_helper.php 3 months ago resource_helper.php 4 days ago roles_helper.php 3 weeks ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 1 week ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 3 weeks ago sms_helper.php 3 months ago steps_helper.php 1 week ago stripe_connect_helper.php 1 week ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 2 months ago timeline_helper.php 2 months ago transaction_helper.php 1 month ago transaction_intent_helper.php 3 months ago util_helper.php 1 month ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 1 month ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
customer_import_helper.php
228 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 [
42 'status' => false,
43 'message' => sprintf( esc_html__( 'Invalid email address: %s', 'latepoint' ), $email ),
44 ];
45 }
46 $customer = new OsCustomerModel();
47 $customer = $customer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
48 if ( $customer ) {
49 return [
50 'status' => false,
51 'message' => sprintf( esc_html__( 'Customer with email already exists: %s', 'latepoint' ), $email ),
52 ];
53 }
54 return [ 'status' => true ];
55 }
56
57
58 /**
59 * Validate mapping between csv and fields from db
60 * @param array $column_mapping
61 * @return bool
62 */
63 public static function validate_import_mapping( array $column_mapping ): bool {
64 if ( empty( $column_mapping ) ) {
65 return false;
66 }
67
68 $email_field_index = array_search( 'email', $column_mapping );
69 return $email_field_index !== false;
70 }
71
72
73 /**
74 * Get the temporary file path for the uploaded CSV file.
75 * @return string
76 * @throws Exception
77 */
78 public static function get_import_tmp_filepath(): string {
79 $file_path = get_transient( 'csv_import_file_' . OsWpUserHelper::get_current_user_id() );
80
81 if ( empty( $file_path ) || ! file_exists( $file_path ) ) {
82 throw new Exception( 'Import file not found or expired. Please upload the file again.' );
83 }
84
85 return $file_path;
86 }
87
88
89 /**
90 * Check CSV to find number of existing customers
91 * @param array $csv_data
92 * @param array $column_mapping
93 *
94 * @return array
95 */
96 public static function validate_csv_data( array $csv_data, array $column_mapping ): array {
97 $email_field_index = array_search( 'email', $column_mapping );
98 $conflicts = [];
99 $importableCount = 0;
100
101 foreach ( $csv_data as $row_index => $row_data ) {
102 // Skip header row
103 if ( $row_index === 0 ) {
104 continue;
105 }
106
107 $email = $row_data[ $email_field_index ] ?? '';
108 $validation_result = OsCustomerImportHelper::validate_customer_email( $email );
109
110 if ( ! $validation_result['status'] ) {
111 $conflicts[ $validation_result['type'] ][] = $validation_result['data'];
112 } else {
113 $importableCount++;
114 }
115 }
116
117 return [
118 'conflicts' => $conflicts,
119 'importable_count' => $importableCount,
120 ];
121 }
122
123
124 /**
125 * Validate customer email
126 * @param string $email
127 * @return array
128 */
129 public static function validate_customer_email( string $email ): array {
130 if ( empty( $email ) || ! OsUtilHelper::is_valid_email( $email ) ) {
131 return [
132 'status' => false,
133 'type' => 'invalid',
134 'data' => $email,
135 ];
136 }
137
138 $existingCustomer = new OsCustomerModel();
139 $existingCustomer = $existingCustomer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
140 if ( $existingCustomer ) {
141 return [
142 'status' => false,
143 'type' => 'duplicate',
144 'data' => $email,
145 ];
146 }
147
148 return [ 'status' => true ];
149 }
150
151 /**
152 * Import customers from CSV
153 * @param array $csv_data
154 * @param array $column_mapping
155 * @param bool $update_existing
156 *
157 * @return array
158 */
159 public static function import_customers( array $csv_data, array $column_mapping, bool $update_existing = false ): array {
160 $emailFieldIndex = array_search( 'email', $column_mapping );
161 $skippedCount = 0;
162 $updatedCount = 0;
163
164 foreach ( $csv_data as $rowIndex => $rowData ) {
165 // Skip header row
166 if ( $rowIndex === 0 ) {
167 continue;
168 }
169
170 $email = $rowData[ $emailFieldIndex ] ?? '';
171
172 if ( empty( $email ) || ! OsUtilHelper::is_valid_email( $email ) ) {
173 $skippedCount++;
174 continue;
175 }
176
177 $customer = new OsCustomerModel();
178 $customer = $customer->where( [ 'email' => $email ] )->set_limit( 1 )->get_results_as_models();
179
180 // Skip if customer exists and update is not allowed
181 if ( $customer && ! $update_existing ) {
182 $skippedCount++;
183 continue;
184 }
185
186 // Create new customer if not found
187 if ( ! $customer ) {
188 $customer = new OsCustomerModel();
189 }
190
191 // Prepare save data
192 $save_data = [];
193 foreach ( $rowData as $column_index => $field_value ) {
194 if ( ! empty( $column_mapping[ $column_index ] ) ) {
195 $save_data[ $column_mapping[ $column_index ] ] = $field_value;
196 }
197 }
198 $customer->set_data( $save_data, 'public' );
199
200 if ( $customer->save() ) {
201 $updatedCount++;
202 do_action( 'latepoint_customer_imported', $customer, $rowData, $column_mapping );
203 } else {
204 $skippedCount++;
205 }
206 }
207
208 return [
209 'skipped_count' => $skippedCount,
210 'updated_count' => $updatedCount,
211 ];
212 }
213
214 /**
215 * Delete temp file
216 * @return void
217 */
218 public static function cleanup_stored_file(): void {
219 $file_name = 'csv_import_file_' . OsWpUserHelper::get_current_user_id();
220 $file_path = get_transient( $file_name );
221
222 if ( $file_path && file_exists( $file_path ) ) {
223 unlink( $file_path );
224 delete_transient( $file_name );
225 }
226 }
227 }
228