-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt-booking-functions.php
1578 lines (1309 loc) · 45.2 KB
/
bt-booking-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* @brief Implements the global functions.
* @author Matthias Fehring
* @version 1.0.0
* @date 2016
*
* @copyright
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
defined( 'ABSPATH' ) or die (' Am Arsch die Räuber! ');
/** @defgroup globalfns Global functions
* Globally available functions.
* @{
*/
/**
* Returns the total amount of slots for a BTB_Event.
*
* @param int $event_id BTB_Event ID.
* @return int Amount of slots.
*/
function btb_get_event_total_slots($event_id) {
global $wpdb;
$total_slots = $wpdb->get_var($wpdb->prepare("SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'btb_time')", $event_id));
return intval($total_slots);
}
/**
* Returns the total amount of booked slots for a BTB_Event.
*
* @param int $event_id BTB_Event ID.
* @return int Amount of booked slots.
*/
function btb_get_event_booked_slots($event_id) {
global $wpdb;
$slots = $wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'btb_booking' AND post_status = 'btb_booked' AND post_parent IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'btb_time'))", $event_id));
return intval($slots);
}
/**
* @brief Returns the total amount of pre booked slots for a BTB_Event.
*
* @param int $event_id BTB_Event ID.
* @return int Amount of pre-booked slots.
*/
function btb_get_event_prebooked_slots($event_id) {
global $wpdb;
$slots = $wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'btb_booking' AND post_status = 'btb_prebook' AND post_parent IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'btb_time'))", $event_id));
return intval($slots);
}
/**
* @brief Returns the total amount of free slots for a BTB_Event.
*
* @param int $event_id BTB_Event ID.
* @return int Amount of free slots.
*/
function btb_get_event_free_slots($event_id) {
$prebooked_slots = btb_get_event_prebooked_slots($event_id);
$booked_slots = btb_get_event_booked_slots($event_id);
$total_slots = btb_get_event_total_slots($event_id);
return $total_slots - $prebooked_slots - $booked_slots;
}
/**
* @brief Returns an array with a summary of slots of a BTB_Event.
*
* @par Exmaple output
* @verbatim
Array(
[free] => 5
[prebooked] => 1
[booked] => 4
[total] => 10
)@endverbatim
*
* @param int $event_id BTB_Event ID.
* @return array Associative array containing summary about slots.
*/
function btb_get_event_slots_summary($event_id) {
$prebooked_slots = btb_get_event_prebooked_slots($event_id);
$booked_slots = btb_get_event_booked_slots($event_id);
$total_slots = btb_get_event_total_slots($event_id);
$slots = array();
$slots["free"] = $total_slots - $prebooked_slots - $booked_slots;
$slots["prebooked"] = $prebooked_slots;
$slots["booked"] = $booked_slots;
$slots["total"] = $total_slots;
return $slots;
}
/**
* @brief Returns the total amount of slots for a BTB_Time.
*
* @param int $time_id BTB_Time ID.
* @return int Amount of slots.
*/
function btb_get_time_total_slots($time_id) {
return intval(get_post_meta($time_id, 'btb_slots', true));
}
/**
* @brief Returns the total amount of booked slots for a BTB_Time.
*
* @param int $time_id BTB_Time ID.
* @return int Amount of booked slots.
*/
function btb_get_time_booked_slots($time_id) {
global $wpdb;
// $slots = $wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'btb_booking' AND post_status = 'btb_booked' AND post_parent IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'btb_time'))", $event_id));
$slots = $wpdb->get_var($wpdb->prepare("SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_status = 'btb_booked')", $time_id));
return intval($slots);
}
/**
* @brief Returns the total amount of pre booked slots for a BTB_Time.
*
* @param int $time_id BTB_Time ID.
* @return int Amount of pre-booked slots.
*/
function btb_get_time_prebooked_slots($time_id) {
global $wpdb;
// $slots = $wpdb->get_var($wpdb->prepare("SELECT sum(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'btb_booking' AND post_status = 'btb_prebook' AND post_parent IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'btb_time'))", $event_id));
$slots = $wpdb->get_var($wpdb->prepare("SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'btb_slots' AND post_id IN (SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_status = 'btb_prebook')", $time_id));
return intval($slots);
}
/**
* @brief Returns the total amount of free slots for a BTB_Time.
*
* @param int $time_id BTB_Time ID.
* @return int Amount of free slots.
*/
function btb_get_time_free_slots($time_id) {
$prebooked_slots = btb_get_time_prebooked_slots($time_id);
$booked_slots = btb_get_time_booked_slots($time_id);
$total_slots = btb_get_time_total_slots($time_id);
return $total_slots - $prebooked_slots - $booked_slots;
}
/**
* @brief Returns an array with a summary of slots of a BTB_Time.
*
* @par Exmaple output
* @verbatim
Array(
[free] => 5
[prebooked] => 1
[booked] => 4
[total] => 10
)@endverbatim
*
* @param int $time_id BTB_Time ID.
* @return array Associative array containing summary about slots.
*/
function btb_get_time_slots_summary($time_id) {
$prebooked_slots = btb_get_time_prebooked_slots($time_id);
$booked_slots = btb_get_time_booked_slots($time_id);
$total_slots = btb_get_time_total_slots($time_id);
$slots = array();
$slots["free"] = $total_slots - $prebooked_slots - $booked_slots;
$slots["prebooked"] = $prebooked_slots;
$slots["booked"] = $booked_slots;
$slots["total"] = $total_slots;
return $slots;
}
/**
* Sanitize every booking field.
*
* If the context is 'raw', then the booking object or array will get minimal
* sanitization of the integer fields.
*
* @since 1.0.0
*
* @see BTB_Booking::sanitize_post_field()
*
* @param object|BTB_Booking|array $booking The booking object or array.
* @param string $context Optional. How to sanitize the booking fields.
Accepts 'raw', 'edit', 'db', 'attribute', 'js' or 'display'.
Default 'display'.
* @return object|BTB_Booking|array The now snitized BTB_Booking object or array.
*/
function btb_sanitize_booking($booking, $context = 'display') {
if (is_object($booking)) {
// Check if booking already filtered for this context.
if (isset($booking->filter) && $context == $booking->filter) {
return $booking;
}
if (!isset($booking->ID)) {
$booking->ID = 0;
}
foreach(array_keys(get_object_vars($booking)) as $field) {
$booking->$field = btb_sanitize_booking_field($field, $booking->$field, $booking->ID, $context);
}
$booking->filter = $context;
} elseif (is_array($booking)) {
// Check if booking already filtered for this context.
if (isset($booking['filter']) && $context == $booking['filter']) {
return $booking;
}
if (!isset($booking['ID'])) {
$booking['ID'] = 0;
}
foreach(array_keys($booking) as $field) {
$booking[$field] = btb_sanitize_booking_field($field, $booking[$field], $booking['ID'], $context);
}
$booking['filter'] = $context;
}
return $booking;
}
/**
* Sanitize booking field based on context.
*
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'.
* The 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' when
* calling filters.
*
* @todo Implement format_to_edit as in WordPress sanitize_post_field.
*
* @since 1.0.0
*
* @param string $field The Booking Object field name.
* @param mixed $value The Booking Object value.
* @param int $booking_id The Booking ID.
* @param string $context Optional. Ho to sanitize booking fields. Looks for 'raw', 'edit',
'db', 'display', 'attribute' and 'js'. Default 'display'.
* @return mixed Sanitized value.
*/
function btb_sanitize_booking_field($field, $value, $booking_id, $context = 'display') {
$int_fields = array('ID', 'booked_event', 'booked_time', 'booked_slots', 'booking_time');
if (in_array($field, $int_fields)) {
$value = (int) $value;
}
$float_fields = array('price');
if (in_array($field, $float_fields)) {
$value = (float) $value;
}
if ('raw' == $context) {
return $value;
}
if ('edit' == $context) {
$value = apply_filters("edit_btb_booking_${field}", $value, $booking_id);
$value = esc_attr($value);
} elseif ( 'db' == $context) {
$value = apply_filters("btb_booking_{$field}_pre", $value);
} else {
// Use display filters by default.
$value = apply_filters("btb_booking_{$field}", $value, $booking_id, $context);
}
if ('attribute' == $context) {
$value = esc_attr($value);
} elseif ('js' == $context) {
$value = ecs_js($value);
}
return $value;
}
/**
* @brief Retrieves booking data given a booking ID or booking object.
*
* @see sanitize_booking() for optional $filter values. Also, the parameter $booking
* must be given as a variable, since it is passed by reference.
*
* @param int|BTB_Booking $booking Booking ID or BTB_Booking object.
* @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A or ARRAY_N.
* @param string $filter Optional. Type fo filter to apply. Accepts 'raw', 'edit', 'db', 'display',
* 'attribute' or 'js'. Default 'raw'.
* @return BTB_Booking|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `BTB_Booking` instance is returned.
*/
function btb_get_booking($booking, $output = OBJECT, $filter = 'raw') {
if ($booking instanceof BTB_Booking) {
$_booking = $booking;
} elseif (is_object($booking)) {
if (empty($booking->filter)) {
$_booking = btb_sanitize_booking($booking, 'raw');
$_booking = new BTB_Booking($_booking);
} elseif ('raw' == $booking->filter) {
$_booking = new BTB_Booking($booking);
} else {
$_booking = BTB_Booking::get_instance($booking->ID);
}
} else {
$_booking = BTB_Booking::get_instance($booking);
}
if (!$_booking) {
return null;
}
$_booking = $_booking->filter($filter);
if ($output == ARRAY_A) {
return $_booking->to_array();
} elseif ($output == ARRAY_N) {
return array_values($_booking->to_array());
}
return $_booking;
}
/**
* @brief Retrieves booking data from master API given a booking ID.
*
* @see sanitize_booking() for optional $filter values. Also, the parameter $booking
* must be given as a variable, since it is passed by reference.
*
* @param int $booking Booking ID or BTB_Booking object.
* @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A or ARRAY_N.
* @param string $filter Optional. Type fo filter to apply. Accepts 'raw', 'edit', 'db', 'display',
* 'attribute' or 'js'. Default 'raw'.
* @return BTB_Booking|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `BTB_Booking` instance is returned.
*/
function btb_get_booking_from_api($booking, $output = OBJECT, $filter = 'raw') {
$r_url = get_option('btb_master_url', '');
$r_url .= '/wp-json/wp/v2/btb-bookings-api/' . $booking;
$headers = array (
'Authorization' => 'Basic ' . base64_encode( get_option('btb_app_user') . ':' . get_option('btb_app_secret') ),
);
$response = wp_remote_get($r_url, array(
'headers' => $headers
));
$b = json_decode($response['body']);
if (empty($b)) {
return null;
}
$_booking = new BTB_Booking;
$_booking->from_api_response($b);
return btb_get_booking($_booking, $output, $filter);
}
/**
* @brief Creates a new booking.
*
* @param int $event_id ID of the BTB_Event this booking is created for.
* @param int $time_id ID of the BTB_Time this booking is created for.
* @param array $bookingarr WP_Post meta data array used for creating the new booking and directly store
* meta data like slots, booking time and price.
* @param bool $obj Set to true if you want to return the newly created booking as a BTB_Booking object.
* @return int|BTB_Booking If \a $obj is false, the ID of the new booking will be returned, 0 on failure.
* With \a $obj true a BTB_booking object will be returned.
*/
function btb_create_booking($event_id, $time_id, $bookingarr, $obj = false) {
$booking_code = btb_gen_booking_number();
$new_booking = wp_insert_post(array(
'post_title' => $booking_code,
'post_parent' => $time_id,
'post_type' => 'btb_booking',
'post_status' => 'btb_prebook',
'meta_input' => $bookingarr
));
if (!$new_booking) {
return 0;
}
if ($obj) {
return BTB_Booking::get_instance($new_booking);
} else {
return $new_booking;
}
}
/**
* @brief Creates a new booking via the API on the master instance.
*
* @param int $time_id ID of the BTB_Time this booking is created for.
* @param array $bookingarr WP_Post meta data array used for creating the new booking and directly store
* meta data like slots, booking time and price.
* @param bool $obj Set to true if you want to return the newly created booking as a BTB_Booking object.
* @return int|BTB_Booking If \a $obj is false, the ID of the new booking will be returned, 0 on failure.
* With \a $obj true a BTB_booking object will be returned.
*/
function btb_create_booking_via_api($time_id, $bookingarr, $obj = false) {
$r_url = get_option('btb_master_url', '');
if (empty($r_url)) {
if (obj) {
return null;
} else {
return 0;
}
}
$r_url .= '/wp-json/btbooking/v1/booking/genbookingnumber';
$r_booking_code = wp_remote_get($r_url);
$booking_code = substr($r_booking_code['body'], 1, -1);
$r_url = get_option('btb_master_url', '');
$r_url .= '/wp-json/wp/v2/btb-bookings-api';
$headers = array (
'Authorization' => 'Basic ' . base64_encode( get_option('btb_app_user') . ':' . get_option('btb_app_secret') ),
);
$data = $bookingarr;
$data['btb_booking_number'] = $booking_code;
$data['btb_time_id'] = $time_id;
$data['status'] = 'btb_prebook';
$response = wp_remote_post($r_url, array(
'method' => 'POST',
'headers' => $headers,
'body' => $data
));
$new_booking = new BTB_Booking;
$new_booking->from_api_response(json_decode($response['body']));
if ($obj) {
return $new_booking;
} else {
return $new_booking->ID;
}
}
/**
* Trash or delete a booking.
*
* When the booking is permanently deleted, everything that is tied to it is deleted also.
*
* @param int $booking_id ID of the booking to delete.
* @param bool $force_delete Wether to bypass trash and force deletion.
* @return array|false|WP_Post False on failure.
*/
function btb_delete_booking($booking_id, $force_delete = false) {
wp_cache_delete($booking_id, 'btb_bookings');
return wp_delete_post($booking_id, $force_delete);
}
/**
* Trash or delete a booking on the master instance.
*
* When the booking is permanently deleted, everything that is tied to it is deleted also.
*
* @param int $booking_id ID of the booking to delete.
* @param bool $force_delete Wether to bypass trash and force deletion.
* @return array|false|WP_Post False on failure.
*/
function btb_delete_booking_via_api($booking_id, $force_delete = true) {
$r_url = get_option('btb_master_url', '');
$r_url .= '/wp-json/wp/v2/btb-bookings-api/' . $booking_id;
if ($force_delete) {
$r_url .= '?force=1';
}
$response = wp_remote_post($r_url, array(
'method' => 'DELETE',
'headers' => array ('Authorization' => 'Basic ' . base64_encode( get_option('btb_app_user') . ':' . get_option('btb_app_secret') ))
));
return array();
}
/**
* @brief Generates a unique booking code.
*
* @return string Booking code.
*/
function btb_gen_booking_number() {
$unique = false;
$rand_string = btb_gen_random_string();
global $wpdb;
while (!$unique) {
$enemies = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_title = %s", $rand_string));
if ($enemies) {
$rand_string = btb_gen_random_string();
} else {
$unique = true;
}
}
return $rand_string;
}
/**
* @brief Generates a random string.
*
* Generates a random string of \a $length characters,
* containing uppercase latin alphanumerics.
*
* @param int $length The length of the string.
* @return string Random string.
*/
function btb_gen_random_string($length = 6) {
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
/**
* @brief Updates the BTB_Booking \a $bookingarr.
*
* @param BTB_Booking|array $bookingarr The booking that should be updated.
* @return int The value 0 on failure. The booking ID on success.
*/
function btb_update_booking($bookingarr) {
if (get_option('btb_instance_type', 'master') == 'master') {
if (is_object($bookingarr)) {
$bookingarr = $bookingarr->to_array(true);
$bookingarr = wp_slash($bookingarr);
}
return wp_update_post($bookingarr);
} else {
if (is_object($bookingarr)) {
$r_url = get_option('btb_master_url', '');
$r_url .= '/wp-json/wp/v2/btb-bookings-api/' . $bookingarr->ID;
$response = wp_remote_post($r_url, array(
'headers' => array('Authorization' => 'Basic ' . base64_encode( get_option('btb_app_user') . ':' . get_option('btb_app_secret'))),
'body' => $bookingarr->to_api_array()
));
$booking = new BTB_Booking;
$booking->from_api_response(json_decode($response['body']));
return $booking->ID;
}
}
}
/**
* @brief Returns the ID of the event description page or permalink to it.
*
* If \a $permalink is true, it returns the permalink to the
* description page.
*
* @param BTB_Event|BTB_Time|BTB_Booking $obj Object to retrieve the description page for.
* @param bool $permalink If true, a permalink to the description page will be returned.
* @return int|string Returns either the ID of the description page or the permalink to it.
* Returns 0, if the page could not be found.
*/
function btb_get_description_page(&$obj, $permalink = false) {
if (!is_object($obj)) {
return 0;
}
$desc_page = 0;
if ($obj->post_type == 'btb_booking') {
if ($obj->booked_event) {
$desc_page = (int) get_post_meta($obj->booked_event, 'btb_desc_page', true);
if (0 == $desc_page) {
$desc_page = $obj->ID;
}
} elseif ($obj->booked_time) {
$event_id = (int) get_post_field('post_parent', $obj->booked_time);
$desc_page = (int) get_post_meta($event_id, 'btb_desc_page', true);
if (0 == $desc_page) {
$desc_page = $event_id;
}
}
} elseif ($obj->post_type == 'btb_time') {
$desc_page = (int) get_post_meta($obj->post_parent, 'btb_desc_page', true);
if (0 == $desc_page) {
$desc_page = $obj->post_parent;
}
} elseif ($obj->post_type == 'btb_event') {
$desc_page = $obj->desc_page;
if (0 == $desc_page) {
$desc_page = $obj->ID;
}
}
if ($permalink && 0 != $desc_page) {
return get_permalink($desc_page);
}
return $desc_page;
}
/**
* @brief Returns the full or short description of an event.
*
* Tries to find a description at first in the event itself, if that is empty and
* the event has a description page, it searches for a description in the description
* page.
*
* If @a $short_desc is true, the short description (excerpt) is returned. If you the
* source for the meta description is set to something else than default in the plugin
* settings, this source is used for the short description.
*
* @param BTB_Event $event The event to request the description for.
* @param bool $short_desc If true, the short description (excerpt) will be returned.
*
* @return string The description or an empty string.
*/
function btb_get_event_description(BTB_Event &$event, $short_desc = false) {
if ($short_desc) {
$descsrc = get_option('btb_struct_data_src_desc', 'default');
if ($descsrc == 'default') {
if (!empty($event->short_desc)) {
return $event->short_desc;
} else {
$desc_page = btb_get_description_page($event);
if ($desc_page != $event->ID) {
$dp = get_post($desc_page);
return $dp->post_excerpt;
} else {
return '';
}
}
} elseif ($descsrc == 'yoastseo') {
$desc = get_post_meta($event->ID, '_yoast_wpseo_metadesc', true);
if ($desc) {
return $desc;
} else {
$desc_page = btb_get_description_page($event);
if ($desc_page) {
$desc = get_post_meta($desc_page, '_yoast_wpseo_metadesc', true);
if ($desc) {
return $desc;
} else {
return '';
}
} else {
return '';
}
}
}
} else {
if (!empty($event->description)) {
return $event->description;
} else {
$desc_page = btb_get_description_page($event);
if ($desc_page != $event->ID) {
$dp = get_post($desc_page);
return $dp->post_content;
} else {
return '';
}
}
}
}
/**
* @brief Returns the image for the event in an array with specified sizes.
*
* The image is returned in the specified @a sizes, by default: full and thumbnail. The returned
* associative array will have the key named by the size, the value will be an array returned
* by @a wp_get_attachment_image_src.
*
* @param BTB_Event $event The event to request the image for.
* @param array $sizes The sizes to return.
*
* @return array Associative array containing imag data, if nothing found, array will be empty.
*/
function btb_get_event_images(BTB_Event &$event, array $sizes = array('thumbnail', 'full')) {
$eventImageID = get_post_thumbnail_id($event->ID);
if (empty($eventImageID) && !empty($event->desc_page)) {
$eventImageID = get_post_thumbnail_id($event->desc_page);
}
if (!empty($eventImageID)) {
$images = array();
foreach ($sizes as $size) {
$images[$size] = wp_get_attachment_image_src($eventImageID, $size);
}
return $images;
} else {
return array();
}
}
/**
* Sanitize every time field.
*
* If the context is 'raw', then the time object or array will get minimal
* sanitization of the integer fields.
*
* @since 1.0.0
*
* @see btb_sanitize_time_field($field, $value, $booking_id, $context = 'display')
*
* @param object|BTB_Time|array $time The time object or array.
* @param string $context Optional. How to sanitize the time fields.
Accepts 'raw', 'edit', 'db', 'attribute', 'js' or 'display'.
Default 'display'.
* @return object|BTB_Time|array The now sanitized BTB_Time object or array.
*/
function btb_sanitize_time($time, $context = 'display') {
if (is_object($time)) {
// Check if time already filtered for this context.
if (isset($time->filter) && $context == $time->filter) {
return $time;
}
if (!isset($time->ID)) {
$time->ID = 0;
}
foreach(array_keys(get_object_vars($time)) as $field) {
$time->$field = btb_sanitize_time_field($field, $time->$field, $time->ID, $context);
}
$time->filter = $context;
} elseif (is_array($time)) {
// Check if time already filtered for this context.
if (isset($time['filter']) && $context == $time['filter']) {
return $time;
}
if (!isset($time['ID'])) {
$time['ID'] = 0;
}
foreach(array_keys($time) as $field) {
$time[$field] = btb_sanitize_time_field($field, $time[$field], $time['ID'], $context);
}
$time['filter'] = $context;
}
return $time;
}
/**
* Sanitize time field based on context.
*
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'.
* The 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' when
* calling filters.
*
* @todo Implement format_to_edit as in WordPress sanitize_post_field.
*
* @since 1.0.0
*
* @param string $field The Time Object field name.
* @param mixed $value The Time Object value.
* @param int $time_id The Time ID.
* @param string $context Optional. Ho to sanitize time fields. Looks for 'raw', 'edit',
'db', 'display', 'attribute' and 'js'. Default 'display'.
* @return mixed Sanitized value.
*/
function btb_sanitize_time_field($field, $value, $time_id, $context = 'display') {
$int_fields = array('ID', 'start', 'end', 'slots', 'event', 'post_author');
if (in_array($field, $int_fields)) {
$value = (int) $value;
}
$float_fields = array('price');
if (in_array($field, $float_fields)) {
$value = (float) $value;
}
$bool_fields = array('date_only');
if (in_array($field, $bool_fields)) {
$value = intval($value) != 0;
}
if ('raw' == $context) {
return $value;
}
if ('edit' == $context) {
$value = apply_filters("edit_btb_time_${field}", $value, $time_id);
$value = esc_attr($value);
} elseif ( 'db' == $context) {
$value = apply_filters("btb_time_{$field}_pre", $value);
} else {
// Use display filters by default.
$value = apply_filters("btb_time_{$field}", $value, $time_id, $context);
}
if ('attribute' == $context) {
$value = esc_attr($value);
} elseif ('js' == $context) {
$value = esc_js($value);
}
return $value;
}
/**
* @brief Retrieves time data given a time ID or time object.
*
* @see sanitize_time() for optional $filter values. Also, the parameter $time
* must be given as a variable, since it is passed by reference.
*
* @param int|BTB_Time $time Time ID or BTB_Time object.
* @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A or ARRAY_N.
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', 'display',
* 'attribute' or 'js'. Default 'raw'.
* @return BTB_Time|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `BTB_Time` instance is returned.
*/
function btb_get_time($time, $output = OBJECT, $filter = 'raw') {
if ($time instanceof BTB_Time) {
$_time = $time;
} elseif (is_object($time)) {
if (empty($time->filter)) {
$_time = btb_sanitize_time($time, 'raw');
$_time = new BTB_Time($_time);
} elseif ('raw' == $time->filter) {
$_time = new BTB_Time($time);
} else {
$_time = BTB_Time::get_instance($time->ID);
}
} else {
$_time = BTB_Time::get_instance($time);
}
if (!$_time) {
return null;
}
$_time = $_time->filter($filter);
if ($output == ARRAY_A) {
return $_time->to_array();
} elseif ($output == ARRAY_N) {
return array_values($_time->to_array());
}
return $_time;
}
/**
* @brief Retrieves time data from master API given a time ID or time object.
*
* @see sanitize_time() for optional $filter values. Also, the parameter $time
* must be given as a variable, since it is passed by reference.
*
* @param int $time Time ID.
* @param string $output Optional, default is Object. Accepts OBJECT, ARRAY_A or ARRAY_N.
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', 'display',
* 'attribute' or 'js'. Default 'raw'.
* @return BTB_Time|array|null Type corresponding to $output on success or null on failure.
* When $output is OBJECT, a `BTB_Time` instance is returned.
*/
function btb_get_time_from_api($time, $output = OBJECT, $filter = 'raw') {
$r_url = get_option('btb_master_url', '');
if (empty($r_url)) {
return null;
}
$r_url .= '/wp-json/wp/v2/btb-times-api/' . $time;
$r_time = wp_remote_get($r_url);
$j_time = json_decode($r_time['body']);
if (empty($j_time)) {
return null;
}
$t = new BTB_Time;
$t->from_api_response($j_time);
return btb_get_time($t, OBJECT, 'display');
}
/**
* @brief Retrieve list of times, either all or for specific event.
*
* @param int $event BTB_Event ID.
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', 'display',
* 'attribute' or 'js'. Default 'raw'.
* @param bool $upcoming_only If true, only upcoming event times will be returned.
* @return array of BTB_Time objects.
*/
function btb_get_times($event = 0, $filter = 'raw', $upcoming_only = false) {
global $wpdb;
$querystring = "
SELECT ti.ID, ti.post_author, ti.post_date, ti.post_date_gmt, ti.post_title AS name, ti.post_status, ti.post_name,
ti.post_modified, ti.post_modified_gmt, ti.post_parent AS event, ti.guid, sl.meta_value AS slots, pr.meta_value AS price,
dato.meta_value AS date_only, st.meta_value AS start, et.meta_value AS end
FROM $wpdb->posts ti
LEFT JOIN $wpdb->postmeta sl
ON sl.post_id = ti.ID
AND sl.meta_key = 'btb_slots'
LEFT JOIN $wpdb->postmeta pr
ON pr.post_id = ti.ID
AND pr.meta_key = 'btb_price'
LEFT JOIN $wpdb->postmeta dato
ON dato.post_id = ti.ID
AND dato.meta_key = 'btb_date_only'
LEFT JOIN $wpdb->postmeta st
ON st.post_id = ti.ID
AND st.meta_key = 'btb_start'
LEFT JOIN $wpdb->postmeta et
ON et.post_id = ti.ID
AND et.meta_key = 'btb_end'
WHERE ti.post_type = 'btb_time'
";
if ($event) {
$querystring .= " AND ti.post_parent = %d";
}
if ($upcoming_only) {
$querystring .= " AND st.meta_value > " . time();
}
$querystring .= " ORDER BY st.meta_value ASC;";
if ($event) {
$times = $wpdb->get_results($wpdb->prepare($querystring, $event));
} else {
$times = $wpdb->get_results($querystring);
}