PluginProbe
Bookit — Booking & Appointment Calendar / 1.2
Bookit — Booking & Appointment Calendar v1.2
2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 All 61 releases
bookit / api / appointment.php

appointment.php in Bookit — Booking & Appointment Calendar 1.2, at api/appointment.php

524 lines 20.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class STM_Bookit_Appointment
4 {
5
6 static function approve_appointment()
7 {
8 if (!STM_Bookit_User::check_is_admin()) die;
9
10 if (empty($_GET['id'])) die;
11 $post_id = intval($_GET['id']);
12
13 if (empty($_GET['status'])) die;
14 $status = sanitize_text_field($_GET['status']);
15
16 if ($status === 'created') {
17 update_post_meta($post_id, 'bookit_status', $status);
18 } else if ($status === 'delete') {
19 wp_delete_post($post_id);
20 stm_bookit_delete_appointment($post_id);
21 }
22 }
23
24 static function create_appointment_data()
25 {
26 return array(
27 'start_day' => array(
28 'name' => 'Start date',
29 'required' => true,
30 'validation' => 'number',
31 ),
32 'end_day' => array(
33 'name' => 'End date',
34 'required' => true,
35 'validation' => 'number',
36 ),
37 'repeating' => array(
38 'name' => 'Repeating',
39 'required' => false,
40 'value' => '',
41 'validation' => 'checkbox',
42 ),
43 'repeating_end_date' => array(
44 'name' => 'Repeating end date',
45 'required' => false,
46 'dependency' => 'repeating',
47 'value' => '',
48 'validation' => 'number',
49 ),
50 'repeating_days' => array(
51 'name' => 'Repeating days',
52 'required' => false,
53 'dependency' => 'repeating',
54 'value' => '',
55 'validation' => 'text',
56 ),
57 'name' => array(
58 'name' => 'Name',
59 'required' => true,
60 'validation' => 'text',
61 ),
62 'email' => array(
63 'name' => 'Email',
64 'required' => true,
65 'validation' => 'email',
66 )
67 ,
68 'password' => array(
69 'password' => 'Password',
70 'required' => false,
71 'validation' => 'text',
72 )
73 ,
74 'business_id' => array(
75 'name' => 'Resource',
76 'required' => true,
77 'validation' => 'number',
78 ),
79 'service' => array(
80 'name' => 'Service',
81 'required' => true,
82 'validation' => 'number',
83 ),
84
85 );
86
87
88 }
89
90 static function block_appointment_data()
91 {
92 return array(
93 'reason' => array(
94 'name' => 'Reason',
95 'required' => true,
96 'validation' => 'text',
97 ),
98 );
99 }
100
101 static function create_appointment()
102 {
103
104 $r = array(
105 'error' => false,
106 'message' => esc_html__('Appointment created.', 'bookit'),
107 'status' => 'success'
108 );
109
110 $r = apply_filters('stm_bookit_woocommerce_change_response', $r);
111
112 if (!STM_Bookit_User::check_is_admin()) {
113 $r['message'] .= esc_html__(' Check your email for appointment submission.', 'bookit');
114 }
115 /*Check if all data submitted*/
116
117 $data = STM_Bookit_Appointment::create_appointment_data();
118
119 if (STM_Bookit_Calendar::is_block()) $data = array_merge($data, STM_Bookit_Appointment::block_appointment_data());
120
121 foreach ($data as $data_name => $data_info) {
122 if ((isset($_GET[$data_name]))) {
123 $data[$data_name]['value'] = STM_Bookit_Calendar::sanitize_field($_GET[$data_name], $data_info['validation']);
124
125 /*If field is dependency*/
126 if (!empty($data[$data_name]['dependency']) and !empty($data[$data[$data_name]['dependency']]['value'] and empty($data[$data_name]['value']))) {
127 $r['error'] = true;
128 $r['message'] = sprintf(esc_html__('Please fill %s field correctly', 'bookit'), $data_info['name']);
129 $r['status'] = 'error';
130
131 break;
132 }
133
134 if (empty($data[$data_name]['value']) and $data_info['required'] === true) {
135 $r['error'] = true;
136 $r['message'] = sprintf(esc_html__('Please fill %s field correctly', 'bookit'), $data_info['name']);
137 $r['status'] = 'error';
138 break;
139 }
140 } elseif ($data_info['required'] === false) {
141 continue;
142 } else {
143 $r['error'] = true;
144 $r['message'] = sprintf(esc_html__('Please fill %s field', 'bookit'), $data_info['name']);
145 $r['status'] = 'error';
146
147 break;
148 }
149 }
150
151
152 if (!$r['error']) {
153
154 /*Check if time is empty*/
155 $start_time = intval(date('Hi', $data['start_day']['value']));
156 $start_day = (!empty($_GET['start_date'])) ? intval($_GET['start_date']) : strtotime('midnight', $data['start_day']['value']);
157 $end_time = intval(date('Hi', $data['end_day']['value']));
158 $repeating = $data['repeating']['value'];
159 $repeating_days = (empty($data['repeating_days']['value'])) ? $data['start_day']['value'] : $data['repeating_days']['value'];
160 $resource = $data['business_id']['value'];
161 $weekdays = array();
162
163 foreach (explode(',', $repeating_days) as $repeating_day) {
164 $weekday = date('w', $repeating_day);
165 if ($weekday == 7) $weekday = 0;
166 $weekdays[] = $weekday;
167 }
168
169 $weekdays = implode(',', $weekdays);
170
171 $end_day = (!empty($repeating) and $repeating == 'on') ? $data['repeating_end_date']['value'] : $end_time;
172 $end_day = (!empty($_GET['end_day'])) ? intval($_GET['end_day']) : strtotime('tomorrow', $end_day) - 1;
173
174 $excludeID = (!empty($_GET['excludeID'])) ? intval($_GET['excludeID']) : '';
175
176 /*Make API Call*/
177 $slots = file_get_contents(STM_Bookit_Calendar::get_slots_url() . "?weekdays={$weekdays}&excludeID=&{$excludeID}resource={$resource}&start_date={$start_day}&end_date={$end_day}&view=hours");
178 $slots = json_decode($slots, true);
179
180
181 if (!empty($slots)) {
182 foreach ($slots as $slot) {
183
184 $slot = intval(date('Hi', $slot));
185
186 if ($start_time <= $slot && $end_time > $slot) {
187 $r['error'] = true;
188 $r['message'] = sprintf(esc_html__('Appointment is crossing up with another one.', 'bookit'), $data_info['name']);
189 $r['status'] = 'error';
190 return $r;
191 }
192 }
193 }
194
195 $my_post = array(
196 'post_type' => 'bookit',
197 'post_title' => $data['name']['value'],
198 //'post_title' => 'user',
199 'post_status' => 'publish',
200 );
201
202
203 $user_id = STM_Bookit_User::get_user($data);
204
205 if (STM_Bookit_Calendar::booking_types() === true) {
206
207 if (!STM_Bookit_User::if_logged_in()) {
208 if (empty($data['password']['value'])) {
209 //die("existing email");
210 $r['error'] = true;
211 $r['message'] = sprintf(esc_html__('Password filed is empty', 'bookit'), $data_info['email']);
212 $r['status'] = 'error';
213 return $r;
214 }
215 }
216
217 if ($user_id === false) {
218 //die("existing email");
219 $r['error'] = true;
220 $r['message'] = sprintf(esc_html__('This email already exists', 'bookit'), $data_info['email']);
221 $r['status'] = 'error';
222 return $r;
223 }
224 }
225
226 /*If editing, just get post id*/
227 $post_id = (!empty($_GET['editing_id'])) ? intval($_GET['editing_id']) : wp_insert_post($my_post);
228
229 $r['post_id'] = $post_id;
230
231 update_post_meta($post_id, 'bookit_start_date', $data['start_day']['value']);
232 update_post_meta($post_id, 'bookit_end_date', $data['end_day']['value']);
233
234 if (STM_Bookit_Calendar::booking_types() === true) {
235 update_post_meta($post_id, 'bookit_user', $user_id);
236 } else {
237 if (STM_Bookit_User::if_logged_in() != false) {
238 update_post_meta($post_id, 'bookit_user', $user_id);
239 } else {
240 $user_id = STM_Bookit_User::get_guest_user($data['name']['value'], $data['email']['value']);
241 update_post_meta($post_id, 'bookit_guest_user', $user_id);
242 //update_post_meta($post_id, 'bookit_user', '');
243 }
244 }
245
246 $us = wp_get_current_user();
247 $us_id = $us->ID;
248
249 if( $us_id ) {
250
251 update_post_meta($post_id, 'bookit_guest_user', $us_id);
252
253 }elseif( $user_id ) {
254
255 update_post_meta($post_id, 'bookit_guest_user', $user_id);
256
257 }else {
258
259 update_post_meta($post_id, 'bookit_guest_user', '0');
260
261 }
262
263 update_post_meta($post_id, 'bookit_resource', $resource);
264 update_post_meta($post_id, 'bookit_repeating', $data['repeating']['value']);
265 update_post_meta($post_id, 'bookit_repeating_days', $repeating_days);
266 update_post_meta($post_id, 'bookit_end_date_repeating', $data['repeating_end_date']['value']);
267 update_post_meta($post_id, 'bookit_service', $data['service']['value']);
268 update_post_meta($post_id, 'bookit_payment_status', 'pending');
269
270 /*ORDER TOKEN*/
271 update_post_meta($post_id, 'bookit_app_token', $post_id . '|' . uniqid());
272
273 STM_Bookit_Appointment::create_appointment_db($post_id, $data);
274
275
276 $data['reason']['value'] = isset($data['reason']['value']) ? $data['reason']['value'] : '';
277 if (STM_Bookit_Calendar::is_block()) {
278 update_post_meta($post_id, 'bookit_status', 'blocked');
279 update_post_meta($post_id, 'bookit_block_reason', $data['reason']['value']);
280 } else {
281 $status = (STM_Bookit_User::check_is_admin()) ? 'created' : 'pending';
282 update_post_meta($post_id, 'bookit_status', $status);
283 }
284
285 // Message to E-Mail
286 $date_format = get_option('date_format');
287 $time_format = get_option('time_format');
288
289 $service = get_the_title($data['service']['value']); // get service
290 $start_time = stm_booki_date_i18n($time_format, $data['start_day']['value']); // get start day
291 $end_time = stm_booki_date_i18n($time_format, $data['end_day']['value']); // get end day
292 $total = stm_bookit_price_view(get_post_meta($post_id, 'bookit_total', true));
293
294
295 do_action('bookit_appointment_created', $post_id, $data);
296
297 if($data['repeating']['value'] == 'on'){
298 //IF REPEATING
299
300 $arr_week_days = explode(',', $data['repeating_days']['value']); //get repeating week days
301 $txt = '';
302
303 //get week days with method
304 foreach($arr_week_days as $item){
305 $txt .= stm_booki_date_i18n('l', $item) .' ';
306 }
307
308 $r_start_day = stm_booki_date_i18n($date_format, $data['start_day']['value']); // get repeating start day
309 $r_end_day = stm_booki_date_i18n($date_format, $data['repeating_end_date']['value']); // get repeating end day
310
311 $email_body = esc_html__( 'Name', 'bookit' ). ': ' . $data['name']['value']."\n".esc_html__( 'Email', 'bookit' ). ": " . $data['email']['value']."\n".esc_html__( 'Service', 'bookit' ) .": $service\n".esc_html__( 'Start time', 'bookit' ).": $start_time\n".esc_html__( 'End time', 'bookit' ).": $end_time\n". esc_html__( 'Repeating Start day', 'bookit' ) .": $r_start_day \n". esc_html__( 'Repeating days', 'bookit' ) .": $txt \n". esc_html__( 'Repeating end day', 'bookit' ) .": $r_end_day \n". esc_html__( 'Total', 'bookit' ) .": $total"; // with repeating message's done
312
313 }else {
314 //ELSE NOT REPEATING
315 $appointment_day = stm_booki_date_i18n('d F', $data['end_day']['value']); // get appointment day
316 $email_body = esc_html__( 'Name', 'bookit' ). ':' . $data['name']['value']."\n".esc_html__( 'Email', 'bookit' ). ": " . $data['email']['value']."\n".esc_html__( 'Service', 'bookit' ) .": $service\n".esc_html__( 'Start time', 'bookit' ).": $start_time\n".esc_html__( 'End time', 'bookit' ).": $end_time\n".esc_html__( 'Appointment day', 'bookit' ).": $appointment_day\n". esc_html__( 'Total', 'bookit' ) .": $total";//without repeating message's done
317 }
318 // End Message
319
320 /*Create EMAIL*/
321
322 wp_mail(get_bloginfo('admin_email'), 'New Appointment created', $email_body); // send to the admin e-mail
323 }
324
325 return $r;
326 }
327
328
329 public static function check_time($data){
330
331 $settings = get_option('bookit_settings', array());
332
333
334 // find how many time
335 $start_day_seconds = $data['start_day']['value'];
336
337 $end_day_seconds = $data['end_day']['value'];
338
339 $result_secunds = $end_day_seconds - $start_day_seconds;
340
341 $result_time = $result_secunds == ($settings['step'] * 60) ? 1 : $result_secunds / ($settings['step'] * 60);
342
343 return $result_time;
344 }
345
346 public static function create_appointment_db($post_id, $data)
347 {
348
349 /*Delete previous data*/
350 STM_Bookit_Appointment::delete_appointment_from_db($post_id);
351
352 $result_time = self::check_time($data);
353
354 if (!empty($data['repeating']['value'])) {
355 $repeating_days = explode(',', $data['repeating_days']['value']);
356 $repeating_start = $data['start_day']['value'];
357 $day_end = $data['end_day']['value'];
358 $repeating_end = $data['repeating_end_date']['value'];
359 $hours_last = ($day_end - $repeating_start);
360 $week = 7 * 24 * 60 * 60;
361
362 // echo(date('d F Y, H:i', $repeating_start) . ' - ' . date('d F Y, H:i', $repeating_start + $hours_last) . '; ');
363 $slots_count = 0;
364 foreach ($repeating_days as $repeating_start) {
365 $data['end_day']['value'] = $repeating_start + $hours_last;
366 while ($repeating_start < $repeating_end) {
367 $data['start_day']['value'] = $repeating_start;
368 STM_Bookit_Appointment::insert_appointment_db($post_id, $data);
369 $slots_count++;
370 $data['end_day']['value'] += $week;
371 $repeating_start += $week;
372 }
373 }
374
375 update_post_meta($post_id, 'bookit_total', $result_time * ($slots_count * get_post_meta($data['service']['value'], 'stm_service_price', true)));
376
377 } else {
378 STM_Bookit_Appointment::insert_appointment_db($post_id, $data);
379
380 update_post_meta($post_id, 'bookit_total', $result_time * ( get_post_meta($data['service']['value'], 'stm_service_price', true)));
381 }
382
383 }
384
385 static function delete_appointment_from_db($post_id)
386 {
387 global $wpdb;
388 $table_name = stm_bookit_appointments_table_name($wpdb);
389
390 $wpdb->delete(
391 $table_name,
392 array(
393 'appointment_id' => $post_id
394 )
395 );
396 }
397
398 static function insert_appointment_db($post_id, $data)
399 {
400 global $wpdb;
401 $table_name = stm_bookit_appointments_table_name($wpdb);
402
403 $wpdb->insert(
404 $table_name,
405 array(
406 'appointment_id' => $post_id,
407 'resource_id' => $data['business_id']['value'],
408 'starttime' => $data['start_day']['value'],
409 'endtime' => $data['end_day']['value'],
410 )
411 );
412 }
413
414 public static function get_appointments_from_db($start_date, $end_date)
415 {
416 global $wpdb;
417 $table = stm_bookit_appointments_table_name($wpdb);
418
419 $request = "SELECT * FROM {$table}
420 WHERE
421 (starttime BETWEEN {$start_date} AND {$end_date}) OR
422 (endtime BETWEEN {$start_date} AND {$end_date})";
423
424
425 $r = $wpdb->get_results($request, ARRAY_A);
426 return $r;
427 }
428
429 public static function get_appointments($params)
430 {
431
432 /* working_on */
433 $logged_in = STM_Bookit_User::check_is_admin();
434 //$logged_in = true;
435
436 $appointments_json = array();
437
438 $start_date = (!empty($_GET['start_date'])) ? intval($_GET['start_date']) : strtotime('monday this week');
439 $duration = (!empty($_GET['duration'])) ? intval($_GET['duration']) : '7';
440 $end_date = strtotime('+' . $duration . ' days', $start_date);
441
442 $appointments = STM_Bookit_Appointment::get_appointments_from_db($start_date, $end_date);
443 if (empty($appointments)) return $appointments_json;
444
445 foreach ($appointments as $appointment) {
446 $id = $appointment['appointment_id'];
447 $business_id = intval($appointment['resource_id']);
448 $meta = STM_Bookit_Calendar::clean_post_meta(get_post_meta($id));
449 $user_id = isset($meta['bookit_user'])? $meta['bookit_user'] : 0 ;
450
451 $status = (empty($meta['bookit_status'])) ? 'pending' : $meta['bookit_status'];
452 $color = get_post_meta($business_id, 'stm_resource_color', true);
453 $color = (!empty($color)) ? $color : '#536DFE';
454 $repeating_end = (!empty(intval($meta['bookit_end_date_repeating']))) ? $meta['bookit_end_date_repeating'] : '';
455 $repeating_days = (!empty($meta['bookit_repeating_days'])) ? $meta['bookit_repeating_days'] : '';
456
457 $service_id = intval($meta['bookit_service']);
458
459 if (!empty(explode(',', $repeating_days))) {
460 $repeating_days = explode(',', $repeating_days);
461 foreach ($repeating_days as $key => $repeating_day) {
462 if (is_numeric($repeating_day)) $repeating_days[$key] = intval(date('w', $repeating_day));
463 }
464 }
465
466 $appointment = array(
467 'app_id' => $appointment['id'],
468 'id' => $id,
469 'business_id' => $business_id,
470 'service_id' => $service_id,
471 'label' => get_the_title($business_id),
472 "start_time" => intval($appointment['starttime']),
473 "start_time_format" => date('d-m-Y', $appointment['starttime']),
474 "end_time" => intval($appointment['endtime']),
475 "is_repeating" => $meta['bookit_repeating'],
476 "repeating_end" => intval($repeating_end),
477 "repeating_days" => $repeating_days,
478 "info" => array(
479 "color" => $color,
480 ),
481 "status" => 'blocked',
482 );
483
484 $meta['bookit_block_reason'] = isset($meta['bookit_block_reason']) ? $meta['bookit_block_reason'] : '';
485
486 if ($logged_in) {
487
488 if (!empty($meta['bookit_user'])) {
489
490 $appointment_data = array(
491 "status" => $status,
492 "reason" => $meta['bookit_block_reason'],
493 "info" => array(
494 "name" => get_userdata($user_id)->user_firstname,
495 "email" => get_userdata($user_id)->user_email,
496 "color" => $color,
497 ),
498 "service" => get_the_title($meta['bookit_service']),
499 );
500
501 } else {
502 //$user_id = $meta['bookit_guest_user'];
503 $appointment_data = array(
504 "status" => $status,
505 "reason" => $meta['bookit_block_reason'],
506 "info" => array(
507 "name" => get_post_meta($id + 1, 'stm_user_name', true),
508 "email" => get_post_meta($id + 1, 'stm_user_email', true),
509 "color" => $color,
510 ),
511 "service" => get_the_title($meta['bookit_service']),
512 );
513 }
514
515
516 $appointment = array_merge($appointment, $appointment_data);
517 }
518
519 $appointments_json[] = $appointment;
520 }
521
522 return $appointments_json;
523 }
524 }