PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.2
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_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_helper.php
316 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[] = [ 'value' => $customer->id, 'label' => esc_html( $customer->full_name ) ];
63 }
64
65 return $customers_options;
66 }
67
68 public static function get_full_name( $customer ) {
69 return join( ' ', array( $customer->first_name, $customer->last_name ) );
70 }
71
72 public static function get_avatar_url( $customer ) {
73 $default_avatar = LATEPOINT_IMAGES_URL . 'default-avatar.jpg';
74 if ( OsAuthHelper::can_wp_users_login_as_customers() && $customer->wordpress_user_id && empty( $customer->avatar_image_id ) ) {
75 // try to get gravatar with WP function
76 $avatar_url = get_avatar_url( $customer->wordpress_user_id );
77 } else {
78 $avatar_url = false;
79 }
80 if ( ! $avatar_url ) {
81 $avatar_url = OsImageHelper::get_image_url_by_id( $customer->avatar_image_id, 'thumbnail', $default_avatar );
82 }
83
84 return $avatar_url;
85 }
86
87
88 public static function get_avatar_image( $customer ) {
89 return '<img src="' . self::get_avatar_url( $customer ) . '"/>';
90 }
91
92
93 public static function total_new_customers_for_date( $date ) {
94 $customers = new OsCustomerModel();
95 $customers = $customers->where( array( 'DATE(created_at)' => $date ) );
96
97 return $customers->count();
98 }
99
100 public static function can_cancel_booking( OsBookingModel $booking ): bool {
101 $can_cancel = false;
102
103 if ( OsSettingsHelper::is_on( 'allow_customer_booking_cancellation' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
104 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_cancel' ) ) {
105 // check if there is a limit on when they can cancel
106 $limit_value = OsSettingsHelper::get_settings_value( 'cancellation_limit_value' );
107 $limit_unit = OsSettingsHelper::get_settings_value( 'cancellation_limit_unit' );
108 if ( $limit_value && $limit_unit ) {
109 $now = new OsWpDateTime( 'now' );
110 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
111 $can_cancel = true;
112 }
113 }
114 } else {
115 $can_cancel = true;
116 }
117 }
118
119 /**
120 * Filter to allow to modify the can_cancel booking status
121 *
122 * @param bool $can_cancel
123 * @param OsBookingModel $booking
124 * @returns bool
125 *
126 * @since 5.2.0
127 * @hook latepoint_can_cancel_booking
128 *
129 */
130 $can_cancel = apply_filters('latepoint_can_cancel_booking', $can_cancel, $booking);
131
132 return $can_cancel;
133 }
134
135 public static function can_reschedule_booking( OsBookingModel $booking ): bool {
136 if(!apply_filters('latepoint_is_feature_reschedule_available', false)) return false;
137 if ( OsSettingsHelper::is_on( 'allow_customer_booking_reschedule' ) && ( $booking->status != LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
138 if ( OsSettingsHelper::is_on( 'limit_when_customer_can_reschedule' ) ) {
139 // check if there is a limit on when they can reschedule
140 $limit_value = OsSettingsHelper::get_settings_value( 'reschedule_limit_value' );
141 $limit_unit = OsSettingsHelper::get_settings_value( 'reschedule_limit_unit' );
142 if ( $limit_value && $limit_unit ) {
143 $now = new OsWpDateTime( 'now' );
144 if ( $now <= $booking->get_start_datetime_object()->modify( '-' . $limit_value . ' ' . $limit_unit ) ) {
145 return true;
146 }
147 }
148 } else {
149 return true;
150 }
151 }
152
153 return false;
154 }
155
156
157 public static function get_customer_for_wp_user( $wp_user ) {
158 $customer = new OsCustomerModel();
159 $customer = $customer->where( [ 'wordpress_user_id' => $wp_user->ID ] )->set_limit( 1 )->get_results_as_models();
160 if ( $customer ) {
161 if ( $customer->email != $wp_user->user_email ) {
162
163 $email_already_assigned = new OsCustomerModel();
164 $email_already_assigned = $email_already_assigned->where([ 'email' => $wp_user->user_email, 'id !=' => $customer->id ])->set_limit( 1 )->get_results_as_models();
165
166 if (!$email_already_assigned) {
167 $customer->update_attributes( [ 'email' => $wp_user->user_email ] );
168 }
169 }
170
171 return $customer;
172 } else {
173 // check if customer with this email exists
174 $customer = new OsCustomerModel();
175 $customer = $customer->where( [ 'email' => $wp_user->user_email ] )->set_limit( 1 )->get_results_as_models();
176 if ( $customer ) {
177 $old_customer_data = $customer->get_data_vars();
178 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user->ID ] );
179 do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
180 } else {
181 // create new customer
182 $customer = new OsCustomerModel();
183 $customer->first_name = $wp_user->first_name;
184 $customer->last_name = $wp_user->last_name;
185 $customer->email = $wp_user->user_email;
186 $customer->password = $wp_user->user_pass;
187 $customer->is_guest = false;
188 $customer->save( true );
189 do_action( 'latepoint_customer_created', $customer );
190 }
191 }
192
193 return $customer;
194 }
195
196 public static function count_customers_not_connected_to_wp_users() {
197 $customers = new OsCustomerModel();
198
199 return $customers->where( [ 'wordpress_user_id' => [ 'OR' => [ 0, 'IS NULL' ] ] ] )->count();
200 }
201
202 public static function get_by_contact($contact_value, $contact_type){
203 if(empty($contact_value) || empty($contact_type)) return false;
204 $customer = new OsCustomerModel();
205 switch($contact_type){
206 case 'email':
207 $customer = $customer->where(['email' => $contact_value])->set_limit(1)->get_results_as_models();
208 break;
209 case 'phone':
210 $customer = $customer->where(['phone' => $contact_value])->set_limit(1)->get_results_as_models();
211 break;
212 }
213 return $customer;
214 }
215
216 public static function get_by_account_nonse( $account_nonse ) {
217 if ( empty( $account_nonse ) ) {
218 return false;
219 }
220 $account_nonse = sanitize_text_field( $account_nonse );
221 $customer = new OsCustomerModel();
222
223 return $customer->where( [ 'account_nonse' => $account_nonse ] )->set_limit( 1 )->get_results_as_models();
224 }
225
226 public static function create_wp_user_for_customer( $customer ) {
227 // NO connected wp user, create one
228 // check if wp user with this customer email already exists
229 $wp_user_id = email_exists( $customer->email );
230 if ( ! $wp_user_id ) {
231 $wp_user_id = username_exists( $customer->email );
232 }
233 if ( $wp_user_id ) {
234 // wp user with this email or username exists - check if its linked to another customer already - if not link it to current customer
235 $linked_customer = new OsCustomerModel();
236 $linked_customer = $linked_customer->where( [ 'wordpress_user_id' => $wp_user_id ] )->set_limit( 1 )->get_results_as_models();
237 if ( $linked_customer ) {
238 // wp user with this email exists and is linked already to a different latepoint customer
239 $customer->add_error( 'customer_exists', __( 'Customer with this email already exists', 'latepoint' ) );
240 } else {
241 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id, 'is_guest' => false ] );
242 }
243 } else {
244
245 $userdata = [
246 'user_email' => $customer->email,
247 'first_name' => $customer->first_name,
248 'last_name' => $customer->last_name,
249 'user_login' => $customer->email,
250 'user_pass' => $customer->password,
251 ];
252
253 $default_role = OsSettingsHelper::get_default_wp_role_for_new_customers();
254 if(wp_roles()->is_role( $default_role )){
255 $userdata['role'] = $default_role;
256 }
257
258 $wp_user_id = wp_insert_user( $userdata );
259 if ( ! is_wp_error( $wp_user_id ) ) {
260 $customer->update_attributes( [ 'wordpress_user_id' => $wp_user_id ] );
261 // update password directly in database because we already hashed it in latepoint customer
262 global $wpdb;
263 $wpdb->update(
264 $wpdb->users,
265 array(
266 'user_pass' => $customer->password,
267 'user_activation_key' => '',
268 ),
269 array( 'ID' => $wp_user_id )
270 );
271 } else {
272 OsDebugHelper::log( 'Error creating WP User for customer', 'registration_error', [ 'errors' => $wp_user_id->get_error_messages() ] );
273 }
274 }
275
276 return ( ! is_wp_error( $wp_user_id ) ) ? $wp_user_id : false;
277 }
278
279 public static function generate_booking_summary_preview_btn( int $booking_id ): string {
280 $html = 'data-os-after-call="latepoint_init_booking_summary_lightbox"
281 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'booking_id' => $booking_id ] )) . '"
282 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_booking_summary_in_lightbox' )) . '"
283 data-os-output-target="lightbox"
284 data-os-lightbox-classes="width-500 customer-dashboard-booking-summary-lightbox"';
285
286 return $html;
287 }
288
289
290 public static function generate_bundle_scheduling_btn( int $order_item_id ): string {
291 $html = 'data-os-after-call="latepoint_init_bundle_scheduling_summary"
292 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_item_id' => $order_item_id ] )) . '"
293 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'scheduling_summary_for_bundle' )) . '"
294 data-os-output-target="lightbox"
295 data-os-lightbox-classes="width-500 customer-dashboard-bundle-scheduling-summary"';
296
297 return $html;
298 }
299
300 public static function generate_order_summary_btn( int $order_id ): string {
301 $html = 'data-os-after-call="latepoint_init_order_summary_lightbox"
302 data-os-params="' . esc_attr(OsUtilHelper::build_os_params( [ 'order_id' => $order_id ] )) . '"
303 data-os-action="' . esc_attr(OsRouterHelper::build_route_name( 'customer_cabinet', 'view_order_summary_in_lightbox' )) . '"
304 data-os-output-target="lightbox"
305 data-os-lightbox-classes="width-500 customer-dashboard-order-summary-lightbox"';
306
307 return $html;
308 }
309
310 public static function get_by_uuid( string $uuid ) {
311 if(empty($uuid)) return false;
312 $customer = new OsCustomerModel();
313 return $customer->where(['uuid' => $uuid])->set_limit(1)->get_results_as_models();
314 }
315
316 }