PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.11
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.11
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_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 3 months ago agent_helper.php 3 months ago analytics_helper.php 4 months ago auth_helper.php 3 months ago blocks_helper.php 3 months ago booking_helper.php 3 months ago bricks_helper.php 3 months ago bundles_helper.php 3 months ago calendar_helper.php 3 months ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 3 months ago customer_import_helper.php 3 months ago database_helper.php 3 months 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 months 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 months 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 3 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 3 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago replacer_helper.php 3 months ago resource_helper.php 3 months ago roles_helper.php 3 months ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 3 months ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 3 months ago sms_helper.php 3 months ago steps_helper.php 3 months ago stripe_connect_helper.php 3 months ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 3 months ago timeline_helper.php 3 months ago transaction_helper.php 3 months ago transaction_intent_helper.php 3 months ago util_helper.php 3 months ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 3 months ago work_periods_helper.php 3 months ago wp_datetime.php 3 months ago wp_user_helper.php 3 months ago
customer_helper.php
357 lines
1 <?php
2
3 class OsCustomerHelper {
4
5
6 public static function quick_customer_btn_html( $customer_id = false, $params = array() ) {
7 $html = '';
8 if ( $customer_id ) {
9 $params['customer_id'] = $customer_id;
10 }
11 $route = OsRouterHelper::build_route_name( 'customers', ! empty( $customer_id ) ? 'quick_edit' : 'quick_new' );
12
13 $params_str = http_build_query( $params );
14 $html = 'data-os-params="' . esc_attr( $params_str ) . '"
15 data-os-action="' . esc_attr( $route ) . '"
16 data-os-output-target="side-panel"
17 data-os-after-call="latepoint_init_quick_customer_form"';
18
19 return $html;
20 }
21
22 public static function generate_summary_for_customer( OsCustomerModel $customer, bool $editable = false ): void {
23 ?>
24 <div class="summary-box summary-box-customer-info">
25 <div class="summary-box-heading">
26 <div class="sbh-item"><?php esc_html_e( 'Customer', 'latepoint' ) ?></div>
27 <div class="sbh-line"></div>
28 </div>
29 <div class="summary-box-content with-media">
30 <div class="os-avatar-w">
31 <div class="os-avatar"><span><?php echo esc_html( $customer->get_initials() ); ?></span></div>
32 </div>
33 <div class="sbc-content-i">
34 <?php if ( $editable ) { ?>
35 <div class="sbc-main-item sbc-with-action"><div class="sbc-label"><?php echo esc_html( $customer->full_name ); ?></div><div class="sbc-action load-customer-step-trigger"><i class="latepoint-icon latepoint-icon-edit-3"></i></div></div>
36 <?php } else { ?>
37 <div class="sbc-main-item"><?php echo esc_html( $customer->full_name ); ?></div>
38 <?php } ?>
39 <div class="sbc-sub-item"><?php echo esc_html( $customer->primary_contact_type() ); ?></div>
40 </div>
41 </div>
42 <?php
43 $customer_attributes = [];
44 $customer_attributes = apply_filters( 'latepoint_booking_summary_customer_attributes', $customer_attributes, $customer );
45 if ( $customer_attributes ) {
46 echo '<div class="summary-attributes sa-clean sa-hidden">';
47 foreach ( $customer_attributes as $attribute ) {
48 echo '<span>' . esc_html( $attribute['label'] ) . ': <strong>' . esc_html( $attribute['value'] ) . '</strong></span>';
49 }
50 echo '</div>';
51 }
52 ?>
53 </div>
54 <?php
55 }
56
57 public static function get_customers_for_select() {
58 $customers = new OsCustomerModel();
59 $customers = $customers->set_limit( 100 )->get_results_as_models();
60 $customers_options = [];
61 foreach ( $customers as $customer ) {
62 $customers_options[] = [
63 'value' => $customer->id,
64 'label' => esc_html( $customer->full_name ),
65 ];
66 }
67
68 return $customers_options;
69 }
70
71 public static function get_full_name( $customer ) {
72 return join( ' ', array( $customer->first_name, $customer->last_name ) );
73 }
74
75 public static function get_avatar_url( $customer ) {
76 $default_avatar = LATEPOINT_IMAGES_URL . 'default-avatar.jpg';
77 if ( OsAuthHelper::can_wp_users_login_as_customers() && $customer->wordpress_user_id && empty( $customer->avatar_image_id ) ) {
78 // try to get gravatar with WP function
79 $avatar_url = get_avatar_url( $customer->wordpress_user_id );
80 } else {
81 $avatar_url = false;
82 }
83 if ( ! $avatar_url ) {
84 $avatar_url = OsImageHelper::get_image_url_by_id( $customer->avatar_image_id, 'thumbnail', $default_avatar );
85 }
86
87 return $avatar_url;
88 }
89
90
91 public static function get_avatar_image( $customer ) {
92 return '<img src="' . self::get_avatar_url( $customer ) . '"/>';
93 }
94
95
96 public static function total_new_customers_for_date( $date ) {
97 $customers = new OsCustomerModel();
98 $customers = $customers->where( array( 'DATE(created_at)' => $date ) );
99
100 return $customers->count();
101 }
102
103 public static function can_cancel_booking( OsBookingModel $booking ): bool {
104 $can_cancel = false;
105
106 if ( OsSettingsHelper::is_on( 'allow_customer_booking_cancellation' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
107 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_cancel' ) ) {
108 // check if there is a limit on when they can cancel
109 $limit_value = OsSettingsHelper::get_settings_value( 'cancellation_limit_value' );
110 $limit_unit = OsSettingsHelper::get_settings_value( 'cancellation_limit_unit' );
111 if ( $limit_value && $limit_unit ) {
112 $now = new OsWpDateTime( 'now' );
113 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
114 $can_cancel = true;
115 }
116 }
117 } else {
118 $can_cancel = true;
119 }
120 }
121
122 /**
123 * Filter to allow to modify the can_cancel booking status
124 *
125 * @param bool $can_cancel
126 * @param OsBookingModel $booking
127 * @returns bool
128 *
129 * @since 5.2.0
130 * @hook latepoint_can_cancel_booking
131 *
132 */
133 $can_cancel = apply_filters( 'latepoint_can_cancel_booking', $can_cancel, $booking );
134
135 return $can_cancel;
136 }
137
138 public static function can_reschedule_booking( OsBookingModel $booking ): bool {
139 if ( ! apply_filters( 'latepoint_is_feature_reschedule_available', false ) ) {
140 return false;
141 }
142 if ( OsSettingsHelper::is_on( 'allow_customer_booking_reschedule' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
143 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_reschedule' ) ) {
144 // check if there is a limit on when they can reschedule
145 $limit_value = OsSettingsHelper::get_settings_value( 'reschedule_limit_value' );
146 $limit_unit = OsSettingsHelper::get_settings_value( 'reschedule_limit_unit' );
147 if ( $limit_value && $limit_unit ) {
148 $now = new OsWpDateTime( 'now' );
149 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
150 return true;
151 }
152 }
153 } else {
154 return true;
155 }
156 }
157
158 return false;
159 }
160
161
162 public static function get_customer_for_wp_user( $wp_user ) {
163 $customer = new OsCustomerModel();
164 $customer = $customer->where( [ 'wordpress_user_id' => $wp_user->ID ] )->set_limit( 1 )->get_results_as_models();
165 if ( $customer ) {
166 if ( $customer->email != $wp_user->user_email ) {
167
168 $email_already_assigned = new OsCustomerModel();
169 $email_already_assigned = $email_already_assigned->where(
170 [
171 'email' => $wp_user->user_email,
172 'id !=' => $customer->id,
173 ]
174 )->set_limit( 1 )->get_results_as_models();
175
176 if ( ! $email_already_assigned ) {
177 $customer->update_attributes( [ 'email' => $wp_user->user_email ] );
178 }
179 }
180
181 return $customer;
182 } else {
183 // check if customer with this email exists
184 $customer = new OsCustomerModel();
185 $customer = $customer->where( [ 'email' => $wp_user->user_email ] )->set_limit( 1 )->get_results_as_models();
186 if ( $customer ) {
187 $old_customer_data = $customer->get_data_vars();
188 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user->ID ] );
189 do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
190 } else {
191 // create new customer
192 $customer = new OsCustomerModel();
193 $customer->first_name = $wp_user->first_name;
194 $customer->last_name = $wp_user->last_name;
195 $customer->email = $wp_user->user_email;
196 $customer->password = $wp_user->user_pass;
197 $customer->is_guest = false;
198 $customer->save( true );
199 do_action( 'latepoint_customer_created', $customer );
200 }
201 }
202
203 return $customer;
204 }
205
206 public static function count_customers_not_connected_to_wp_users() {
207 $customers = new OsCustomerModel();
208
209 return $customers->where( [ 'wordpress_user_id' => [ 'OR' => [ 0, 'IS NULL' ] ] ] )->count();
210 }
211
212 public static function get_by_contact( $contact_value, $contact_type ) {
213 if ( empty( $contact_value ) || empty( $contact_type ) ) {
214 return false;
215 }
216 $customer = new OsCustomerModel();
217 switch ( $contact_type ) {
218 case 'email':
219 $customer = $customer->where( [ 'email' => $contact_value ] )->set_limit( 1 )->get_results_as_models();
220 break;
221 case 'phone':
222 $customer = $customer->where( [ 'phone' => $contact_value ] )->set_limit( 1 )->get_results_as_models();
223 break;
224 }
225 return $customer;
226 }
227
228 public static function get_by_account_nonse( $account_nonse ) {
229 if ( empty( $account_nonse ) ) {
230 return false;
231 }
232 $account_nonse = sanitize_text_field( $account_nonse );
233 $customer = new OsCustomerModel();
234
235 return $customer->where( [ 'account_nonse' => $account_nonse ] )->set_limit( 1 )->get_results_as_models();
236 }
237
238 public static function create_wp_user_for_customer( $customer ) {
239 // NO connected wp user, create one
240 // check if wp user with this customer email already exists
241 $wp_user_id = email_exists( $customer->email );
242 if ( ! $wp_user_id ) {
243 $wp_user_id = username_exists( $customer->email );
244 }
245 if ( $wp_user_id ) {
246 // wp user with this email or username exists - check if its linked to another customer already - if not link it to current customer
247 $linked_customer = new OsCustomerModel();
248 $linked_customer = $linked_customer->where( [ 'wordpress_user_id' => $wp_user_id ] )->set_limit( 1 )->get_results_as_models();
249 if ( $linked_customer ) {
250 // wp user with this email exists and is linked already to a different latepoint customer
251 $customer->add_error( 'customer_exists', __( 'Customer with this email already exists', 'latepoint' ) );
252 } else {
253 $customer->update_attributes(
254 [
255 'wordpress_user_id' => $wp_user_id,
256 'is_guest' => false,
257 ]
258 );
259 }
260 } else {
261
262 $userdata = [
263 'user_email' => $customer->email,
264 'first_name' => $customer->first_name,
265 'last_name' => $customer->last_name,
266 'user_login' => $customer->email,
267 'user_pass' => $customer->password,
268 ];
269
270 $default_role = OsSettingsHelper::get_default_wp_role_for_new_customers();
271 if ( wp_roles()->is_role( $default_role ) ) {
272 $userdata['role'] = $default_role;
273 }
274
275 $wp_user_id = wp_insert_user( $userdata );
276 if ( ! is_wp_error( $wp_user_id ) ) {
277 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id ] );
278 // update password directly in database because we already hashed it in latepoint customer
279 global $wpdb;
280 $wpdb->update(
281 $wpdb->users,
282 array(
283 'user_pass' => $customer->password,
284 'user_activation_key' => '',
285 ),
286 array( 'ID' => $wp_user_id )
287 );
288 } else {
289 OsDebugHelper::log( 'Error creating WP User for customer', 'registration_error', [ 'errors' => $wp_user_id->get_error_messages() ] );
290 }
291 }
292
293 return ( ! is_wp_error( $wp_user_id ) ) ? $wp_user_id : false;
294 }
295
296 public static function generate_booking_summary_preview_btn( int $booking_id ): string {
297 $html = 'data-os-after-call="latepoint_init_booking_summary_lightbox"
298 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'booking_id' => $booking_id ] ) ) . '"
299 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'view_booking_summary_in_lightbox' ) ) . '"
300 data-os-output-target="lightbox"
301 data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"';
302
303 return $html;
304 }
305
306
307 public static function generate_bundle_scheduling_btn( int $order_item_id ): string {
308 $html = 'data-os-after-call="latepoint_init_bundle_scheduling_summary"
309 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'order_item_id' => $order_item_id ] ) ) . '"
310 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'scheduling_summary_for_bundle' ) ) . '"
311 data-os-output-target="lightbox"
312 data-os-lightbox-classes="width-500 customer-dashboard-bundle-scheduling-summary"';
313
314 return $html;
315 }
316
317 public static function generate_order_summary_btn( int $order_id ): string {
318 $html = 'data-os-after-call="latepoint_init_order_summary_lightbox"
319 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'order_id' => $order_id ] ) ) . '"
320 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'view_order_summary_in_lightbox' ) ) . '"
321 data-os-output-target="lightbox"
322 data-os-lightbox-classes="width-500 customer-dashboard-order-summary-lightbox"';
323
324 return $html;
325 }
326
327 public static function get_by_uuid( string $uuid ) {
328 if ( empty( $uuid ) ) {
329 return false;
330 }
331 $customer = new OsCustomerModel();
332 return $customer->where( [ 'uuid' => $uuid ] )->set_limit( 1 )->get_results_as_models();
333 }
334
335 /**
336 * Invalidate password reset token after successful password reset
337 *
338 * Deletes the token creation timestamp and regenerates account_nonse
339 * to prevent token reuse and ensure single-use password reset links.
340 *
341 * @since 5.1.0 Security fix for password reset token expiration
342 * @param OsCustomerModel $customer Customer object
343 * @return void
344 */
345 public static function invalidate_password_reset_token( $customer ) {
346 if ( empty( $customer ) || ! $customer->id ) {
347 return;
348 }
349
350 // Delete the token creation timestamp
351 OsMetaHelper::delete_customer_meta_by_key( 'password_reset_token_created_at', $customer->id );
352
353 // Regenerate account_nonse for additional security
354 $customer->account_nonse = sha1( wp_rand( 10000, 99999 ) . time() . wp_generate_password( 32, true, true ) );
355 $customer->save();
356 }
357 }