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_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_helper.php
379 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 /**
239 * Check whether a WP user has only customer-safe roles (subscriber, customer, latepoint_customer).
240 * Used to prevent linking a LatePoint customer record to a privileged WordPress account.
241 *
242 * @param int $wp_user_id WordPress user ID to check.
243 * @return bool True if the user exists and has only allowlisted roles; false otherwise.
244 */
245 public static function is_wp_user_safe_for_customer_link( int $wp_user_id ): bool {
246 $wp_user = get_userdata( $wp_user_id );
247 if ( ! $wp_user || empty( $wp_user->roles ) ) {
248 return false;
249 }
250 $allowed_roles = array( LATEPOINT_WP_CUSTOMER_ROLE, 'subscriber', 'customer' );
251 return empty( array_diff( (array) $wp_user->roles, $allowed_roles ) );
252 }
253
254 public static function create_wp_user_for_customer( $customer ) {
255 // NO connected wp user, create one
256 // check if wp user with this customer email already exists
257 $wp_user_id = email_exists( $customer->email );
258 if ( ! $wp_user_id ) {
259 $wp_user_id = username_exists( $customer->email );
260 }
261 if ( $wp_user_id ) {
262 // wp user with this email or username exists
263 if ( ! self::is_wp_user_safe_for_customer_link( $wp_user_id ) ) {
264 // Do not link to privileged WP accounts (admin, editor, etc.)
265 $customer->add_error( 'privileged_user', __( 'Cannot link to a privileged WordPress account.', 'latepoint' ) );
266 } else {
267 // check if its linked to another customer already - if not link it to current customer
268 $linked_customer = new OsCustomerModel();
269 $linked_customer = $linked_customer->where( [ 'wordpress_user_id' => $wp_user_id ] )->set_limit( 1 )->get_results_as_models();
270 if ( $linked_customer ) {
271 // wp user with this email exists and is linked already to a different latepoint customer
272 $customer->add_error( 'customer_exists', __( 'Customer with this email already exists', 'latepoint' ) );
273 } else {
274 $customer->update_attributes(
275 [
276 'wordpress_user_id' => $wp_user_id,
277 'is_guest' => false,
278 ]
279 );
280 }
281 }
282 } else {
283
284 $userdata = [
285 'user_email' => $customer->email,
286 'first_name' => $customer->first_name,
287 'last_name' => $customer->last_name,
288 'user_login' => $customer->email,
289 'user_pass' => $customer->password,
290 ];
291
292 $default_role = OsSettingsHelper::get_default_wp_role_for_new_customers();
293 if ( wp_roles()->is_role( $default_role ) ) {
294 $userdata['role'] = $default_role;
295 }
296
297 $wp_user_id = wp_insert_user( $userdata );
298 if ( ! is_wp_error( $wp_user_id ) ) {
299 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id ] );
300 // update password directly in database because we already hashed it in latepoint customer
301 global $wpdb;
302 $wpdb->update(
303 $wpdb->users,
304 array(
305 'user_pass' => $customer->password,
306 'user_activation_key' => '',
307 ),
308 array( 'ID' => $wp_user_id )
309 );
310 } else {
311 OsDebugHelper::log( 'Error creating WP User for customer', 'registration_error', [ 'errors' => $wp_user_id->get_error_messages() ] );
312 }
313 }
314
315 return ( ! is_wp_error( $wp_user_id ) ) ? $wp_user_id : false;
316 }
317
318 public static function generate_booking_summary_preview_btn( int $booking_id ): string {
319 $html = 'data-os-after-call="latepoint_init_booking_summary_lightbox"
320 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'booking_id' => $booking_id ] ) ) . '"
321 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'view_booking_summary_in_lightbox' ) ) . '"
322 data-os-output-target="lightbox"
323 data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"';
324
325 return $html;
326 }
327
328
329 public static function generate_bundle_scheduling_btn( int $order_item_id ): string {
330 $html = 'data-os-after-call="latepoint_init_bundle_scheduling_summary"
331 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'order_item_id' => $order_item_id ] ) ) . '"
332 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'scheduling_summary_for_bundle' ) ) . '"
333 data-os-output-target="lightbox"
334 data-os-lightbox-classes="width-500 customer-dashboard-bundle-scheduling-summary"';
335
336 return $html;
337 }
338
339 public static function generate_order_summary_btn( int $order_id ): string {
340 $html = 'data-os-after-call="latepoint_init_order_summary_lightbox"
341 data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'order_id' => $order_id ] ) ) . '"
342 data-os-action="' . esc_attr( OsRouterHelper::build_route_name( 'customer_cabinet', 'view_order_summary_in_lightbox' ) ) . '"
343 data-os-output-target="lightbox"
344 data-os-lightbox-classes="width-500 customer-dashboard-order-summary-lightbox"';
345
346 return $html;
347 }
348
349 public static function get_by_uuid( string $uuid ) {
350 if ( empty( $uuid ) ) {
351 return false;
352 }
353 $customer = new OsCustomerModel();
354 return $customer->where( [ 'uuid' => $uuid ] )->set_limit( 1 )->get_results_as_models();
355 }
356
357 /**
358 * Invalidate password reset token after successful password reset
359 *
360 * Deletes the token creation timestamp and regenerates account_nonse
361 * to prevent token reuse and ensure single-use password reset links.
362 *
363 * @since 5.1.0 Security fix for password reset token expiration
364 * @param OsCustomerModel $customer Customer object
365 * @return void
366 */
367 public static function invalidate_password_reset_token( $customer ) {
368 if ( empty( $customer ) || ! $customer->id ) {
369 return;
370 }
371
372 // Delete the token creation timestamp
373 OsMetaHelper::delete_customer_meta_by_key( 'password_reset_token_created_at', $customer->id );
374
375 // Regenerate account_nonse for additional security
376 $customer->account_nonse = sha1( wp_rand( 10000, 99999 ) . time() . wp_generate_password( 32, true, true ) );
377 $customer->save();
378 }
379 }