PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.91
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.91
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 / controllers / customer_cabinet_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 1 year ago auth_controller.php 1 year ago booking_form_settings_controller.php 1 year ago bookings_controller.php 1 year ago calendars_controller.php 1 year ago carts_controller.php 1 year ago controller.php 1 year ago customer_cabinet_controller.php 1 year ago customers_controller.php 1 year ago dashboard_controller.php 1 year ago default_agent_controller.php 1 year ago events_controller.php 1 year ago form_fields_controller.php 1 year ago integrations_controller.php 1 year ago invoices_controller.php 1 year ago manage_booking_by_key_controller.php 1 year ago manage_order_by_key_controller.php 1 year ago notifications_controller.php 1 year ago orders_controller.php 1 year ago pro_controller.php 1 year ago process_jobs_controller.php 1 year ago processes_controller.php 1 year ago search_controller.php 1 year ago services_controller.php 1 year ago settings_controller.php 1 year ago steps_controller.php 1 year ago stripe_connect_controller.php 1 year ago support_topics_controller.php 1 year ago todos_controller.php 1 year ago transactions_controller.php 1 year ago wizard_controller.php 1 year ago
customer_cabinet_controller.php
487 lines
1 <?php
2 /*
3 * Copyright (c) 2023 LatePoint LLC. All rights reserved.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10
11 if ( ! class_exists( 'OsCustomerCabinetController' ) ) :
12
13
14 class OsCustomerCabinetController extends OsController {
15
16 function __construct() {
17 parent::__construct();
18
19 $this->action_access['customer'] = array_merge( $this->action_access['customer'], [
20 'update',
21 'request_cancellation',
22 'print_booking_info',
23 'print_order_info',
24 'ical_download',
25 'process_reschedule_request',
26 'request_reschedule_calendar',
27 'view_order_summary_in_lightbox',
28 'view_booking_summary_in_lightbox',
29 'scheduling_summary_for_bundle',
30 'reload_booking_tile'
31 ] );
32 $this->action_access['public'] = array_merge( $this->action_access['public'], [
33 'logout',
34 'dashboard',
35 'login',
36 'do_login',
37 'password_reset_form',
38 'request_password_reset_token',
39 'change_password',
40 'set_account_password_on_booking_completion'
41 ] );
42 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'customer_cabinet/';
43 }
44
45
46 public function scheduling_summary_for_bundle() {
47 if ( ! filter_var( $this->params['order_item_id'], FILTER_VALIDATE_INT ) ) {
48 exit();
49 }
50 $order_item = new OsOrderItemModel( $this->params['order_item_id'] );
51 $bundle = $order_item->build_original_object_from_item_data();
52 $this->vars['order_item'] = $order_item;
53 $this->vars['bundle'] = $bundle;
54 $this->format_render( __FUNCTION__ );
55 }
56
57 public function view_order_summary_in_lightbox() {
58 if ( ! filter_var( $this->params['order_id'], FILTER_VALIDATE_INT ) ) {
59 exit();
60 }
61 $order = new OsOrderModel( $this->params['order_id'] );
62 $this->vars['order'] = $order;
63 $this->vars['price_breakdown_rows'] = $order->generate_price_breakdown_rows();
64 $this->format_render( __FUNCTION__ );
65 }
66
67 public function view_booking_summary_in_lightbox() {
68 if ( ! filter_var( $this->params['booking_id'], FILTER_VALIDATE_INT ) ) {
69 exit();
70 }
71 $booking = new OsBookingModel( $this->params['booking_id'] );
72 $order_item = new OsOrderItemModel( $booking->order_item_id );
73 $order = new OsOrderModel( $order_item->order_id );
74 $this->vars['booking'] = $booking;
75 $this->vars['order_item'] = $order_item;
76 $this->vars['order'] = $order;
77 $this->format_render( __FUNCTION__ );
78 }
79
80
81 function print_order_info() {
82 if ( ! filter_var( $this->params['latepoint_order_id'], FILTER_VALIDATE_INT ) ) {
83 exit();
84 }
85 $order_id = $this->params['latepoint_order_id'];
86 if ( empty( $order_id ) ) {
87 return;
88 }
89 $order = new OsOrderModel( $order_id );
90 if ( $order->id && OsAuthHelper::is_customer_logged_in() && ( $order->customer_id == OsAuthHelper::get_logged_in_customer_id() ) ) {
91 $customer = $order->customer;
92 $this->vars['order'] = $order;
93 $this->vars['customer'] = $customer;
94 $this->set_layout( 'print' );
95 $content = $this->format_render_return( __FUNCTION__, [], [], true );
96 echo $content;
97 }
98 }
99
100 function print_booking_info() {
101 if ( ! filter_var( $this->params['latepoint_booking_id'], FILTER_VALIDATE_INT ) ) {
102 exit();
103 }
104 $booking_id = $this->params['latepoint_booking_id'];
105 if ( empty( $booking_id ) ) {
106 return;
107 }
108 $booking = new OsBookingModel( $booking_id );
109 if ( $booking->id && OsAuthHelper::is_customer_logged_in() && ( $booking->customer_id == OsAuthHelper::get_logged_in_customer_id() ) ) {
110 $customer = $booking->customer;
111 $this->vars['booking'] = $booking;
112 $this->vars['customer'] = $customer;
113 $this->set_layout( 'print' );
114 $content = $this->format_render_return( __FUNCTION__, [], [], true );
115 echo $content;
116 }
117 }
118
119 function ical_download() {
120 if ( ! filter_var( $this->params['latepoint_booking_id'], FILTER_VALIDATE_INT ) ) {
121 exit();
122 }
123 $booking_id = $this->params['latepoint_booking_id'];
124 if ( empty( $booking_id ) ) {
125 return;
126 }
127 $booking = new OsBookingModel( $booking_id );
128 if ( $booking->id && OsAuthHelper::is_customer_logged_in() && ( $booking->customer_id == OsAuthHelper::get_logged_in_customer_id() ) ) {
129
130 header( 'Content-Type: text/calendar; charset=utf-8' );
131 header( 'Content-Disposition: attachment; filename=booking_' . $booking->id . '.ics' );
132
133 echo OsBookingHelper::generate_ical_event_string( $booking );
134 }
135 }
136
137
138 function process_reschedule_request() {
139 if ( ! filter_var( $this->params['booking_id'], FILTER_VALIDATE_INT ) ) {
140 exit();
141 }
142 $booking = new OsBookingModel( $this->params['booking_id'] );
143
144 if ( empty( $booking->id ) || empty( $this->params['start_date'] ) || empty( $this->params['start_time'] ) ) {
145 return;
146 }
147
148 if ( ( OsAuthHelper::get_logged_in_customer_id() == $booking->customer_id ) && OsCustomerHelper::can_reschedule_booking( $booking ) ) {
149 $old_booking = clone $booking;
150 $booking->start_date = $this->params['start_date'];
151 $booking->start_time = $this->params['start_time'];
152
153 $booking->convert_start_datetime_into_server_timezone($booking->get_customer_timezone_name());
154
155 if ( $booking->is_start_date_and_time_set() ) {
156 $booking->calculate_end_date_and_time();
157 $booking->set_utc_datetimes();
158 }
159
160 // check if booking time is still available
161 if ( ! OsBookingHelper::is_booking_request_available( \LatePoint\Misc\BookingRequest::create_from_booking_model( $booking ), [ 'exclude_booking_ids' => [ $booking->id ] ] ) ) {
162 $response_html = __( 'Unfortunately the selected time slot is not available anymore, please select another timeslot.', 'latepoint' );
163 $status = LATEPOINT_STATUS_ERROR;
164 } else {
165 if ( OsSettingsHelper::is_on( 'change_status_on_customer_reschedule' ) ) {
166 $allowed_statuses = OsBookingHelper::get_statuses_list();
167 if ( isset( $allowed_statuses[ OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' ) ] ) ) {
168 $booking->status = OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' );
169 }
170 }
171 if ( $booking->save() ) {
172 /**
173 * Booking is updated
174 *
175 * @param {OsBookingModel} $this->>booking Updated instance of booking model
176 * @param {OsBookingModel} $old_booking Instance of booking model before it was updated
177 *
178 * @since 4.9.0
179 * @hook latepoint_booking_updated
180 *
181 */
182 do_action( 'latepoint_booking_updated', $booking, $old_booking );
183 $this->vars['booking'] = $booking;
184 $this->vars['timezone_name'] = OsTimeHelper::get_timezone_name_from_session();
185 $this->vars['viewer'] = 'customer';
186 $status = LATEPOINT_STATUS_SUCCESS;
187 $this->set_layout( 'none' );
188 $response_html = $this->format_render_return( __FUNCTION__, [], [], true );
189 } else {
190 OsDebugHelper::log( 'Error rescheduling appointment', 'booking_reschedule_error', $booking->get_error_messages() );
191 $response_html = __( 'Error! Please try again later', 'latepoint' );
192 $status = LATEPOINT_STATUS_ERROR;
193 }
194 }
195 } else {
196 $status = LATEPOINT_STATUS_ERROR;
197 $response_html = __( 'Error! LKDFU343', 'latepoint' );
198 }
199
200 if ( $this->get_return_format() == 'json' ) {
201 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
202 }
203 }
204
205 function request_reschedule_calendar() {
206 if ( ! filter_var( $this->params['booking_id'], FILTER_VALIDATE_INT ) ) {
207 exit();
208 }
209 $booking = new OsBookingModel( $this->params['booking_id'] );
210
211 if ( ! empty( $booking->id ) && ( OsAuthHelper::get_logged_in_customer_id() == $booking->customer_id ) && OsCustomerHelper::can_reschedule_booking( $booking ) ) {
212 $this->vars['booking'] = $booking;
213 $this->vars['calendar_start_date'] = ! empty( $this->params['calendar_start_date'] ) ? new OsWpDateTime( $this->params['calendar_start_date'] ) : new OsWpDateTime( 'today' );
214 $this->vars['timezone_name'] = $booking->get_customer_timezone_name();
215
216 $this->set_layout( 'none' );
217 $response_html = $this->format_render_return( __FUNCTION__, [], [], true );
218 } else {
219 $status = LATEPOINT_STATUS_ERROR;
220 $response_html = __( 'Reschedule is not allowed', 'latepoint' );
221 }
222 if ( $this->get_return_format() == 'json' ) {
223 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
224 }
225 }
226
227 function request_cancellation() {
228 if ( ! filter_var( $this->params['id'], FILTER_VALIDATE_INT ) ) {
229 exit();
230 }
231
232 $booking_id = $this->params['id'];
233 $booking = new OsBookingModel( $booking_id );
234 if ( ! empty( $booking->id ) && ( OsAuthHelper::get_logged_in_customer_id() == $booking->customer_id ) && OsCustomerHelper::can_cancel_booking( $booking ) ) {
235 if ( $booking->update_status( LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
236 $status = LATEPOINT_STATUS_SUCCESS;
237 $response_html = __( 'Appointment Status Updated', 'latepoint' );
238 } else {
239 $status = LATEPOINT_STATUS_ERROR;
240 $response_html = __( 'Error Updating Booking Status!', 'latepoint' ) . ' ' . implode( ',', $booking->get_error_messages() );
241 }
242 } else {
243 $status = LATEPOINT_STATUS_ERROR;
244 $response_html = __( 'Not allowed to cancel', 'latepoint' );
245 }
246 if ( $this->get_return_format() == 'json' ) {
247 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
248 }
249 }
250
251 /*
252 Update profile
253 */
254
255 public function update() {
256 if ( ! filter_var( $this->params['customer']['id'], FILTER_VALIDATE_INT ) ) {
257 exit();
258 }
259 if ( ( OsAuthHelper::get_highest_current_user_type() == 'customer' ) && ( OsAuthHelper::get_logged_in_customer_id() != $this->params['customer']['id'] ) ) {
260 if ( $this->get_return_format() == 'json' ) {
261 $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => 'Customer Not Authorized' ) );
262 } else {
263 echo "Customer Not Authorized";
264 }
265 exit;
266 }
267 $customer = new OsCustomerModel( $this->params['customer']['id'] );
268 $old_customer_data = $customer->get_data_vars();
269 $customer->set_data( $this->params['customer'], LATEPOINT_PARAMS_SCOPE_CUSTOMER );
270 if ( $customer->save() ) {
271 $response_html = __( 'Information Saved', 'latepoint' );
272 $status = LATEPOINT_STATUS_SUCCESS;
273 do_action( 'latepoint_customer_updated', $customer, $old_customer_data );
274 } else {
275 $response_html = $customer->get_error_messages();
276 $status = LATEPOINT_STATUS_ERROR;
277 }
278 if ( $this->get_return_format() == 'json' ) {
279 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
280 }
281 }
282
283 public function reload_booking_tile() {
284 if ( ! filter_var( $this->params['booking_id'], FILTER_VALIDATE_INT ) ) {
285 exit();
286 }
287
288 $booking_id = $this->params['booking_id'];
289 $booking = new OsBookingModel( $booking_id );
290
291 if ( $booking->id && OsAuthHelper::get_logged_in_customer_id() == $booking->customer_id ) {
292 $this->vars['booking'] = $booking;
293 $this->vars['is_upcoming_booking'] = $booking->is_upcoming();
294 $this->set_layout( 'none' );
295 $response_html = $this->format_render_return( '_booking_tile' );
296 $status = LATEPOINT_STATUS_SUCCESS;
297 } else {
298 $response_html = __( 'Invalid Booking', 'latepoint' );
299 $status = LATEPOINT_STATUS_ERROR;
300 }
301
302 if ( $this->get_return_format() == 'json' ) {
303 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
304 }
305
306 }
307
308 public function logout() {
309 OsAuthHelper::logout_customer();
310 nocache_headers();
311 wp_redirect( OsSettingsHelper::get_customer_dashboard_url(), 302 );
312 }
313
314 public function login() {
315 $this->set_layout( 'none' );
316
317 return $this->format_render_return( __FUNCTION__ );
318 }
319
320 public function do_login() {
321 $customer = OsAuthHelper::login_customer( sanitize_email( $this->params['customer_login']['email'] ), $this->params['customer_login']['password'] );
322 if ( $customer ) {
323 $response_html = OsSettingsHelper::get_customer_dashboard_url();
324 $status = LATEPOINT_STATUS_SUCCESS;
325 } else {
326 $status = LATEPOINT_STATUS_ERROR;
327 $response_html = __( 'Invalid password or email', 'latepoint' );
328 }
329 if ( $this->get_return_format() == 'json' ) {
330 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
331 }
332 }
333
334
335 public function password_reset_form() {
336 $this->vars['from_booking'] = ( isset( $this->params['from_booking'] ) && $this->params['from_booking'] );
337 $this->set_layout( 'none' );
338
339 return $this->format_render_return( __FUNCTION__ );
340 }
341
342 public function request_password_reset_token() {
343 $this->set_layout( 'none' );
344 $this->vars['from_booking'] = ( isset( $this->params['from_booking'] ) && $this->params['from_booking'] );
345
346 if ( isset( $this->params['password_reset_email'] ) ) {
347 $customer_model = new OsCustomerModel();
348 $customer = $customer_model->where( [ 'email' => sanitize_email( $this->params['password_reset_email'] ) ] )->set_limit( 1 )->get_results_as_models();
349 $customer_mailer = new OsCustomerMailer();
350 if ( $customer && $customer_mailer->password_reset_request( $customer, $customer->account_nonse ) ) {
351 return $this->format_render_return( 'password_reset_form' );
352 } else {
353 $this->vars['reset_token_error'] = ( $customer ) ? __( 'Error! Email was not sent.', 'latepoint' ) : __( 'Email does not match any customer', 'latepoint' );
354
355 return $this->format_render_return( __FUNCTION__ );
356 }
357 } else {
358 return $this->format_render_return( __FUNCTION__ );
359 }
360 }
361
362 public function dashboard( array $params = [] ) {
363 if ( ! OsAuthHelper::is_customer_logged_in() ) {
364 $this->set_layout( 'none' );
365
366 return $this->format_render_return( 'login' );
367 } else {
368 $customer = OsAuthHelper::get_logged_in_customer();
369 $this->vars['customer'] = $customer;
370 $this->vars['orders'] = $customer->get_orders();
371
372 $this->vars['future_bookings'] = $customer->get_future_bookings();
373 $this->vars['past_bookings'] = $customer->get_past_bookings();
374 $this->vars['cancelled_bookings'] = $customer->get_cancelled_bookings();
375 $this->vars['not_scheduled_bundles'] = $customer->get_not_scheduled_bundles();
376
377 $this->vars['cart_not_empty'] = ( ! OsCartsHelper::is_current_cart_empty() && OsCartsHelper::can_checkout_multiple_items() );
378
379 $this->vars['hide_new_appointment_ui'] = $params['hide_new_appointment_ui'] ?? false;
380
381 $this->set_layout( 'none' );
382
383 return $this->format_render_return( __FUNCTION__ );
384 }
385 }
386
387 public function change_password() {
388 $params = OsParamsHelper::permit_params( $this->params, [
389 'password_reset_token',
390 'password',
391 'password_confirmation',
392 ] );
393
394 if ( ! empty( $params['password_reset_token'] ) ) {
395 $params['password_reset_token'] = sanitize_text_field( $params['password_reset_token'] );
396 }
397
398 if ( OsAuthHelper::is_customer_logged_in() ) {
399 $customer = OsAuthHelper::get_logged_in_customer();
400 } elseif ( $params['password_reset_token'] ) {
401 $customer = OsCustomerHelper::get_by_account_nonse( $params['password_reset_token'] );
402 if ( ! $customer ) {
403 $response_html = __( 'Invalid Secret Key', 'latepoint' );
404 $status = LATEPOINT_STATUS_ERROR;
405 }
406 } else {
407 $status = LATEPOINT_STATUS_ERROR;
408 $response_html = __( 'Error!', 'latepoint' );
409 }
410 if ( $customer ) {
411 if ( ! empty( $params['password'] ) && $params['password'] == $params['password_confirmation'] ) {
412 if ( $customer->update_password( $params['password'] ) ) {
413 // update connected wp user password
414 if ( OsAuthHelper::wp_users_as_customers() && $customer->wordpress_user_id ) {
415 global $wpdb;
416 $wpdb->update(
417 $wpdb->users,
418 array(
419 'user_pass' => $customer->password,
420 'user_activation_key' => '',
421 ),
422 array( 'ID' => $customer->wordpress_user_id )
423 );
424 }
425 $status = LATEPOINT_STATUS_SUCCESS;
426 $response_html = __( 'Your password was successfully updated.', 'latepoint' );
427 } else {
428 $response_html = __( 'Error! Message Code: KS723J', 'latepoint' );
429 $status = LATEPOINT_STATUS_ERROR;
430 }
431 } else {
432 $status = LATEPOINT_STATUS_ERROR;
433 $response_html = __( 'Error! Passwords do not match.', 'latepoint' );
434 }
435 } else {
436 $status = LATEPOINT_STATUS_ERROR;
437 $response_html = __( 'Customer Not Found', 'latepoint' );
438 }
439
440
441 if ( $this->get_return_format() == 'json' ) {
442 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
443 }
444 }
445
446 public function set_account_password_on_booking_completion() {
447
448 $params = OsParamsHelper::permit_params( $this->params, [
449 'account_nonse',
450 'password',
451 ] );
452
453 if ( ! empty( $params['account_nonse'] ) ) {
454 $params['account_nonse'] = sanitize_text_field( $params['account_nonse'] );
455 }
456
457 $customer = OsCustomerHelper::get_by_account_nonse( $params['account_nonse'] );
458
459 if ( $customer ) {
460 if ( ! empty( $params['password'] ) ) {
461 if ( $customer->update_password( $params['password'] ) ) {
462 $status = LATEPOINT_STATUS_SUCCESS;
463 $response_html = __( 'Account Password Set', 'latepoint' );
464 } else {
465 $response_html = __( 'Error! Message Code: KS723J', 'latepoint' );
466 $status = LATEPOINT_STATUS_ERROR;
467 }
468 } else {
469 $status = LATEPOINT_STATUS_ERROR;
470 $response_html = __( 'Error! Password is empty.', 'latepoint' );
471 }
472 } else {
473 $response_html = __( 'Error! Message Code: JS76SD', 'latepoint' );
474 $status = LATEPOINT_STATUS_ERROR;
475 }
476
477
478 if ( $this->get_return_format() == 'json' ) {
479 $this->send_json( array( 'status' => $status, 'message' => $response_html ) );
480 }
481 }
482
483
484 }
485
486
487 endif;