PluginProbe ʕ •ᴥ•ʔ
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress / 5.6.8
Appointment Booking Plugin – LatePoint | Calendar & Scheduling for WordPress v5.6.8
5.6.8 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 / manage_booking_by_key_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 2 months ago auth_controller.php 4 months ago booking_form_settings_controller.php 4 months ago bookings_controller.php 1 week ago calendars_controller.php 4 months ago carts_controller.php 1 week ago controller.php 4 months ago customer_cabinet_controller.php 1 day ago customers_controller.php 1 week ago dashboard_controller.php 2 months ago default_agent_controller.php 4 months ago events_controller.php 4 months ago form_fields_controller.php 2 weeks ago integrations_controller.php 4 months ago invoices_controller.php 1 week ago manage_booking_by_key_controller.php 4 months ago manage_order_by_key_controller.php 4 months ago notifications_controller.php 4 months ago orders_controller.php 1 week ago pro_controller.php 3 weeks ago process_jobs_controller.php 4 months ago processes_controller.php 1 month ago razorpay_connect_controller.php 2 weeks ago search_controller.php 4 months ago services_controller.php 4 months ago settings_controller.php 2 months ago steps_controller.php 3 weeks ago stripe_connect_controller.php 2 weeks ago support_topics_controller.php 4 months ago todos_controller.php 4 months ago transactions_controller.php 1 week ago wizard_controller.php 2 weeks ago
manage_booking_by_key_controller.php
405 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( 'OsManageBookingByKeyController' ) ) :
12
13
14 class OsManageBookingByKeyController extends OsController {
15 private $booking;
16 private $key_for;
17 private $key = '';
18
19 private function set_booking_by_key() {
20 if ( empty( $this->params['key'] ) ) {
21 return;
22 }
23
24 $key = sanitize_text_field( $this->params['key'] );
25 $data = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $key );
26 if ( empty( $data ) ) {
27 return;
28 }
29 $booking = new OsBookingModel( $data['booking_id'] );
30 if ( $booking->id ) {
31 $this->key = $key;
32 $this->booking = $booking;
33 $this->key_for = $data['for'];
34 }
35 }
36
37 function __construct() {
38 parent::__construct();
39 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'manage_booking_by_key/';
40
41 $this->action_access['public'] = array_merge(
42 $this->action_access['public'],
43 [
44 'show',
45 'print',
46 'ical_download',
47 'change_status',
48 'request_cancellation',
49 'process_reschedule_request',
50 'request_reschedule_calendar',
51 ]
52 );
53
54 $this->set_booking_by_key();
55 }
56
57
58 function show() {
59 if ( empty( $this->booking->id ) ) {
60 return;
61 }
62
63
64 $this->vars['key'] = $this->key;
65 $this->vars['for'] = $this->key_for;
66 $this->vars['viewer'] = $this->key_for == 'agent' ? 'agent' : 'customer';
67 $this->vars['booking'] = $this->booking;
68 $this->vars['order'] = $this->booking->get_order();
69 $this->vars['order_manage_key'] = OsMetaHelper::get_order_meta_by_key( 'key_to_manage_for_' . $this->key_for, $this->booking->get_order()->id );
70
71 $this->vars['timezone_name'] = $this->key_for == 'agent' ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name();
72
73 if ( $this->get_return_format() == 'json' ) {
74 $this->set_layout( 'none' );
75 $response_html = $this->format_render_return( __FUNCTION__ );
76 $this->send_json(
77 array(
78 'status' => LATEPOINT_STATUS_SUCCESS,
79 'message' => $response_html,
80 )
81 );
82 } else {
83 $this->set_layout( 'clean' );
84 $content = $this->format_render_return( __FUNCTION__ );
85 echo $content;
86 }
87 }
88
89
90 function ical_download() {
91 if ( empty( $this->booking->id ) ) {
92 return;
93 }
94
95 header( 'Content-Type: text/calendar; charset=utf-8' );
96 header( 'Content-Disposition: attachment; filename=booking_' . $this->booking->id . '.ics' );
97
98 echo OsBookingHelper::generate_ical_event_string( $this->booking );
99 }
100
101 function print() {
102 if ( empty( $this->booking->id ) ) {
103 return;
104 }
105
106 $this->vars['booking'] = $this->booking;
107 $this->vars['order'] = $this->booking->get_order();
108 $this->vars['customer'] = $this->booking->customer;
109 $this->set_layout( 'print' );
110 $content = $this->format_render_return( 'print_booking_info', [], [], true );
111 echo $content;
112 }
113
114
115 function process_reschedule_request() {
116 if ( empty( $this->booking->id ) || empty( $this->params['start_date'] ) || empty( $this->params['start_time'] ) ) {
117 $this->send_json(
118 [
119 'status' => LATEPOINT_STATUS_ERROR,
120 'message' => __( 'Invalid request', 'latepoint' ),
121 ]
122 );
123 return;
124 }
125
126 // Verify CSRF nonce
127 $this->check_nonce( 'reschedule_booking_' . $this->booking->id );
128
129 $allowed = ( $this->key_for == 'agent' ) ? true : OsCustomerHelper::can_reschedule_booking( $this->booking );
130
131 // For agent keys, verify booking belongs to this agent
132 if ( $this->key_for == 'agent' ) {
133 $key_info = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $this->key );
134 if ( ! $key_info || $key_info['for'] !== 'agent' ) {
135 $this->send_json(
136 [
137 'status' => LATEPOINT_STATUS_ERROR,
138 'message' => __( 'Invalid agent key', 'latepoint' ),
139 ]
140 );
141 return;
142 }
143
144 $key_owner_booking = new OsBookingModel( $key_info['booking_id'] );
145
146 if ( $this->booking->agent_id != $key_owner_booking->agent_id ) {
147 $this->send_json(
148 [
149 'status' => LATEPOINT_STATUS_ERROR,
150 'message' => __( 'You cannot modify other agents\' bookings', 'latepoint' ),
151 ]
152 );
153 return;
154 }
155 }
156
157 if ( $allowed ) {
158 $old_booking = clone $this->booking;
159 $this->booking->start_date = $this->params['start_date'];
160 $this->booking->start_time = $this->params['start_time'];
161
162 $this->booking->convert_start_datetime_into_server_timezone( $this->params['timezone_name'], ( $this->key_for == 'customer' ) );
163
164 if ( $this->booking->is_start_date_and_time_set() ) {
165 $this->booking->calculate_end_date_and_time();
166 $this->booking->set_utc_datetimes();
167 }
168
169 // check if booking time is still available
170 if ( ! OsBookingHelper::is_booking_request_available( \LatePoint\Misc\BookingRequest::create_from_booking_model( $this->booking ), [ 'exclude_booking_ids' => [ $this->booking->id ] ] ) ) {
171 $response_html = __( 'Unfortunately the selected time slot is not available anymore, please select another timeslot.', 'latepoint' );
172 $status = LATEPOINT_STATUS_ERROR;
173 } else {
174 // customer rescheduled, perform actions
175 if ( $this->key_for == 'customer' ) {
176 if ( OsSettingsHelper::is_on( 'change_status_on_customer_reschedule' ) ) {
177 $allowed_statuses = OsBookingHelper::get_statuses_list();
178 if ( isset( $allowed_statuses[ OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' ) ] ) ) {
179 $this->booking->status = OsSettingsHelper::get_settings_value( 'status_to_set_after_customer_reschedule' );
180 }
181 }
182 }
183 if ( $this->booking->save() ) {
184 /**
185 * Booking is updated
186 *
187 * @param {OsBookingModel} $this->>booking Updated instance of booking model
188 * @param {OsBookingModel} $old_booking Instance of booking model before it was updated
189 *
190 * @since 4.9.0
191 * @hook latepoint_booking_updated
192 *
193 */
194 do_action( 'latepoint_booking_updated', $this->booking, $old_booking );
195 $this->vars['booking'] = $this->booking;
196 $this->vars['timezone_name'] = ( $this->key_for == 'agent' ) ? OsTimeHelper::get_wp_timezone_name() : $this->booking->customer->get_selected_timezone_name();
197 $status = LATEPOINT_STATUS_SUCCESS;
198 $this->vars['viewer'] = $this->key_for == 'agent' ? 'agent' : 'customer';
199 $this->set_layout( 'none' );
200 $response_html = $this->format_render_return( __FUNCTION__, [], [], true );
201 } else {
202 OsDebugHelper::log( 'Error rescheduling appointment', 'booking_reschedule_error', $this->booking->get_error_messages() );
203 $response_html = __( 'Error! Please try again later', 'latepoint' );
204 $status = LATEPOINT_STATUS_ERROR;
205 }
206 }
207 } else {
208 $status = LATEPOINT_STATUS_ERROR;
209 $response_html = __( 'Error! LKDFU343', 'latepoint' );
210 }
211
212 if ( $this->get_return_format() == 'json' ) {
213 $this->send_json(
214 array(
215 'status' => $status,
216 'message' => $response_html,
217 )
218 );
219 }
220 }
221
222 function request_reschedule_calendar() {
223 if ( empty( $this->booking->id ) ) {
224 return;
225 }
226
227 $allowed = ( $this->key_for == 'agent' ) ? true : OsCustomerHelper::can_reschedule_booking( $this->booking );
228
229 // For agent keys, verify booking belongs to this agent
230 if ( $this->key_for == 'agent' ) {
231 $key_info = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $this->key );
232 if ( ! $key_info || $key_info['for'] !== 'agent' ) {
233 $this->send_json(
234 [
235 'status' => LATEPOINT_STATUS_ERROR,
236 'message' => __( 'Invalid agent key', 'latepoint' ),
237 ]
238 );
239 return;
240 }
241
242 $key_owner_booking = new OsBookingModel( $key_info['booking_id'] );
243
244 if ( $this->booking->agent_id != $key_owner_booking->agent_id ) {
245 $this->send_json(
246 [
247 'status' => LATEPOINT_STATUS_ERROR,
248 'message' => __( 'You cannot modify other agents\' bookings', 'latepoint' ),
249 ]
250 );
251 return;
252 }
253 }
254
255 if ( $allowed ) {
256 if ( $this->key_for == 'customer' ) {
257 // change timezone for customer to the one supplied, because we can't change it using the default change timezone method as we don't have a logged in customer here
258 $timezone_name = sanitize_text_field( $this->params['timezone_name'] ?? $this->booking->customer->get_selected_timezone_name() );
259 OsMetaHelper::save_customer_meta_by_key( 'timezone_name', $timezone_name, $this->booking->customer_id );
260 }
261 $this->vars['booking'] = $this->booking;
262 $this->vars['key'] = $this->key;
263 $this->vars['calendar_start_date'] = ! empty( $this->params['calendar_start_date'] ) ? new OsWpDateTime( $this->params['calendar_start_date'] ) : new OsWpDateTime( 'today' );
264 $timezone_name = ( $this->key_for == 'agent' ) ? ( $this->params['timezone_name'] ?? OsTimeHelper::get_wp_timezone_name() ) : $this->booking->customer->get_selected_timezone_name();
265 $this->vars['timezone_name'] = $timezone_name;
266
267 $this->set_layout( 'none' );
268 $response_html = $this->format_render_return( __FUNCTION__, [], [], true );
269 } else {
270 $status = LATEPOINT_STATUS_ERROR;
271 $response_html = __( 'Reschedule is not allowed', 'latepoint' );
272 }
273 if ( $this->get_return_format() == 'json' ) {
274 $this->send_json(
275 array(
276 'status' => $status,
277 'message' => $response_html,
278 )
279 );
280 }
281 }
282
283 function request_cancellation() {
284 if ( empty( $this->booking->id ) ) {
285 return;
286 }
287
288 // For agent keys, verify booking belongs to this agent
289 if ( $this->key_for == 'agent' ) {
290 $key_info = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $this->key );
291 if ( ! $key_info || $key_info['for'] !== 'agent' ) {
292 $this->send_json(
293 [
294 'status' => LATEPOINT_STATUS_ERROR,
295 'message' => __( 'Invalid agent key', 'latepoint' ),
296 ]
297 );
298 return;
299 }
300
301 $key_owner_booking = new OsBookingModel( $key_info['booking_id'] );
302
303 if ( $this->booking->agent_id != $key_owner_booking->agent_id ) {
304 $this->send_json(
305 [
306 'status' => LATEPOINT_STATUS_ERROR,
307 'message' => __( 'You cannot modify other agents\' bookings', 'latepoint' ),
308 ]
309 );
310 return;
311 }
312 }
313
314 // Verify CSRF nonce
315 $this->check_nonce( 'cancel_booking_' . $this->booking->id );
316
317 if ( OsCustomerHelper::can_cancel_booking( $this->booking ) ) {
318 if ( $this->booking->update_status( LATEPOINT_BOOKING_STATUS_CANCELLED ) ) {
319 $status = LATEPOINT_STATUS_SUCCESS;
320 $response_html = __( 'Appointment Status Updated', 'latepoint' );
321 } else {
322 $status = LATEPOINT_STATUS_ERROR;
323 $response_html = __( 'Error Updating Booking Status!', 'latepoint' ) . ' ' . implode( ',', $this->booking->get_error_messages() );
324 }
325 } else {
326 $status = LATEPOINT_STATUS_ERROR;
327 $response_html = __( 'Not allowed to cancel', 'latepoint' );
328 }
329 if ( $this->get_return_format() == 'json' ) {
330 $this->send_json(
331 array(
332 'status' => $status,
333 'message' => $response_html,
334 )
335 );
336 }
337 }
338
339 function change_status() {
340 // only agent key can cancel
341 if ( $this->key_for != 'agent' || empty( $this->booking->id ) || empty( $this->params['status'] ) ) {
342 $this->send_json(
343 [
344 'status' => LATEPOINT_STATUS_ERROR,
345 'message' => __( 'Invalid request', 'latepoint' ),
346 ]
347 );
348 return;
349 }
350
351 // Verify booking belongs to this agent's key
352 $key_info = OsBookingHelper::get_booking_id_and_manage_ability_by_key( $this->key );
353 if ( ! $key_info || $key_info['for'] !== 'agent' ) {
354 $this->send_json(
355 [
356 'status' => LATEPOINT_STATUS_ERROR,
357 'message' => __( 'Invalid agent key', 'latepoint' ),
358 ]
359 );
360 return;
361 }
362
363 // Load the booking that this key belongs to
364 $key_owner_booking = new OsBookingModel( $key_info['booking_id'] );
365
366 // Verify the target booking belongs to the same agent as the key owner
367 if ( $this->booking->agent_id != $key_owner_booking->agent_id ) {
368 $this->send_json(
369 [
370 'status' => LATEPOINT_STATUS_ERROR,
371 'message' => __( 'You cannot modify other agents\' bookings', 'latepoint' ),
372 ]
373 );
374 return;
375 }
376
377
378 // Verify CSRF nonce
379 $this->check_nonce( 'change_status_booking_' . $this->booking->id );
380
381 $statuses = OsBookingHelper::get_statuses_list();
382 if ( ! isset( $statuses[ $this->params['status'] ] ) ) {
383 return;
384 }
385
386 if ( $this->booking->update_status( $this->params['status'] ) ) {
387 $status = LATEPOINT_STATUS_SUCCESS;
388 $response_html = __( 'Appointment Status Updated', 'latepoint' );
389 } else {
390 $status = LATEPOINT_STATUS_ERROR;
391 $response_html = __( 'Error Updating Booking Status!', 'latepoint' ) . ' ' . implode( ',', $this->booking->get_error_messages() );
392 }
393
394 if ( $this->get_return_format() == 'json' ) {
395 $this->send_json(
396 array(
397 'status' => $status,
398 'message' => $response_html,
399 )
400 );
401 }
402 }
403 }
404 endif;
405