PluginProbe
Booking Calendar / 10.1.3
Booking Calendar v10.1.3
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / page-bookings / bookings__sql.php

bookings__sql.php in Booking Calendar 10.1.3, at includes/page-bookings/bookings__sql.php

2,547 lines 108.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php /**
2 * @version 1.0
3 * @description Bookings Listing SQL
4 * @category Booking Listing
5 * @author wpdevelop
6 *
7 * @web-site http://oplugins.com/
8 * @email info@oplugins.com
9 *
10 * @modified 2022-04-07
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 // <editor-fold defaultstate="collapsed" desc=" D E F A U L T R E Q U E S T " >
16
17 /**
18 * Get params names for escaping and/or default value of such params
19 *
20 * @param string $structure_type 'validate_and_default' (default) | 'validate' | 'default'
21 *
22 * @return array
23 * if $structure_type == '' array ( 'keyword' => array( 'validate' => 's', 'default' => '' )
24 * , 'wh_booking_date' => array( 'validate' => 'digit_or_date', 'default' => '3' ), ...
25 * if $structure_type == 'validate' array ( 'keyword' => 's'
26 * , 'wh_booking_date' => 'digit_or_date'), ...
27 * if $structure_type == 'default' array ( 'keyword' => '' )
28 * , 'wh_booking_date' => '3' ), ...
29 */
30 function wpbc_ajx_get__request_params__names_default( $structure_type = 'validate_and_default' ){
31
32 // Clean specific $_REQUEST params, if param is NOT set then return "default"
33 $params_for_cleaning = array(
34 // 'wh_booking_id' => array( 'validate' => 'digit_or_date', 'default' => '' ),
35
36 // 'digit_or_csd' can check about 'digit_or_csd' in arrays, as well
37 'wh_booking_type' => array( 'validate' => 'digit_or_csd', 'default' => array( '0' ) ) // if ['0'] - All booking resources, ['-1'] - lost bookings in deleted resources
38
39 , 'wh_approved' => array( 'validate' => 'digit_or_csd', 'default' => '' ) // '0' | '1' | ''
40
41 , 'wh_booking_date' => array( 'validate' => 'array', 'default' => array( "3" ) ) // array( "0" ) - Current dates, | array( "3" ) - All dates // number | date 2016-07-20
42 , 'ui_wh_booking_date_radio' => array( 'validate' => 'd', 'default' => 0 ) // '1' | '2' ....
43 , 'ui_wh_booking_date_next' => array( 'validate' => 'd', 'default' => 0 ) // '1' | '2' ....
44 , 'ui_wh_booking_date_prior' => array( 'validate' => 'd', 'default' => 0 ) // '1' | '2' ....
45 , 'ui_wh_booking_date_checkin' => array( 'validate' => 'digit_or_date', 'default' => '' ) // number | date 2016-07-20
46 , 'ui_wh_booking_date_checkout' => array( 'validate' => 'digit_or_date', 'default' => '' ) // number | date 2016-07-20
47
48 , 'wh_what_bookings' => array( 'validate' => array( 'any', 'new', 'imported', 'in_plugin' ), 'default' => 'any' ) // '1' | ''
49
50 , 'wh_modification_date' => array( 'validate' => 'array', 'default' => array( "3" ) ) // number | date 2016-07-20
51 , 'ui_wh_modification_date_radio' => array( 'validate' => 'd', 'default' => 0 ) // '1' | '2' ....
52 , 'ui_wh_modification_date_prior' => array( 'validate' => 'd', 'default' => 0 ) // '1' | '2' ....
53 , 'ui_wh_modification_date_checkin' => array( 'validate' => 'digit_or_date', 'default' => '' ) // number | date 2016-07-20
54 , 'ui_wh_modification_date_checkout' => array( 'validate' => 'digit_or_date', 'default' => '' ) // number | date 2016-07-20
55
56 , 'keyword' => array( 'validate' => 's', 'default' => '' ) //string
57
58 , 'wh_pay_status' => array( 'validate' => 'array', 'default' => array( 'all' ) )
59 , 'ui_wh_pay_status_radio' => array( 'validate' => 's', 'default' => '' ) // string
60 , 'ui_wh_pay_status_custom' => array( 'validate' => 's', 'default' => '' ) // string
61
62 , 'wh_cost' => array( 'validate' => 'float_or_empty', 'default' => '' ) // '1' | ''
63 , 'wh_cost2' => array( 'validate' => 'float_or_empty', 'default' => '' ) // '1' | ''
64
65 , 'wh_sort' => array( 'validate' => array(
66 'booking_id__asc',
67 'booking_id__desc',
68 'dates__asc',
69 'dates__desc',
70 'resource__asc',
71 'resource__desc',
72 'cost__asc',
73 'cost__desc'
74 ), 'default' => 'booking_id__desc' )
75 , 'wh_trash' => array( 'validate' => array( '0', 'trash', 'any' ), 'default' => 'any' )
76
77 , 'page_num' => array( 'validate' => 'd', 'default' => 1 ) // '1' | ''
78 , 'page_items_count' => array( 'validate' => 'd', 'default' => 10 ) // '1' | ''
79
80 , 'ui_usr__send_emails' => array( 'validate' => array( 'send', 'not_send' ), 'default' => 'send' )
81 , 'ui_usr__is_expand_remarks' => array( 'validate' => array( 'On', 'Off' ), 'default' => 'Off' )
82 , 'ui_usr__default_selected_toolbar' => array( 'validate' => array( 'filters', 'actions', 'options' ), 'default' => 'filters' )
83
84 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
85 , 'ui_reset' => array( 'validate' => 's', 'default' => '' ) // string
86 , 'ui_usr__dates_short_wide' => array( 'validate' => array( 'short', 'wide' ), 'default' => 'short' )
87 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88 , 'view_days_num' => array( 'validate' => 'd', 'default' => 30 ) // '1' | '' //FixIn: 8.9.2.1
89 , 'scroll_start_date' => array( 'validate' => 'digit_or_date', 'default' => '' ) // number | date 2016-07-20
90 , 'scroll_day' => array( 'validate' => 'd', 'default' => 0 ) // '1' | ''
91 , 'scroll_month' => array( 'validate' => 'd', 'default' => 0 ) // '1' | ''
92 , 'limit_hours' => array( 'validate' => 'digit_or_csd', 'default' => '' ) //
93 , 'only_booked_resources' => array( 'validate' => 'd', 'default' => 0 ) // '1' | ''
94
95 );
96
97 if ( 'validate_and_default' == $structure_type ) {
98 return $params_for_cleaning;
99 }
100
101 $return_simple_arr = array();
102
103 foreach ( $params_for_cleaning as $key => $value ) {
104 $return_simple_arr[ $key ] = $value[ $structure_type ];
105 }
106 return $return_simple_arr;
107 }
108
109 // </editor-fold>
110
111
112 // <editor-fold defaultstate="collapsed" desc=" S A V E U S E R R E Q U E S T " >
113
114 /**
115 * Save user filter request - saving user filters in toolbar
116 *
117 * @param array $params
118 * @param integer $user_id
119 *
120 * @return bool|int
121 */
122 function wpbc_ajx__user_request_params__save( $params, $user_id ) {
123
124 // Reset some parameters, like selected page
125 if ( isset( $params['page_num'] ) ) {
126 $params['page_num'] = 1;
127 $params['keyword'] = '';
128 }
129
130 return update_user_option( (int) $user_id, 'booking_listing_request_params' , $params );
131 }
132
133 /**
134 * Delete saved user request - used for resetting user filters in toolbar
135 * @param integer $user_id
136 *
137 * @return bool
138 */
139 function wpbc_ajx__user_request_params__delete( $user_id ) {
140
141 if ( empty( $user_id ) ) {
142 $user_id = wpbc_get_current_user_id();
143 }
144
145 return delete_user_option( (int) $user_id, 'booking_listing_request_params' );
146 }
147
148 /**
149 * Get saved user filter request - params for user filters in toolbar
150 *
151 * @param integer $user_id
152 *
153 * @return false|mixed
154 */
155 function wpbc_ajx__user_request_params__get( $user_id ) {
156
157 if ( empty( $user_id ) ) {
158 $user_id = wpbc_get_current_user_id();
159 }
160
161 // wpbc_ajx__user_request_params__delete($user_id); //For debugging. delete it.
162
163 return get_user_option( 'booking_listing_request_params', (int) $user_id );
164 }
165
166 /**
167 * Get sanitized array of use request parameters that was saved before
168 *
169 * @param $user_id int
170 *
171 * @return array|false
172 */
173 function wpbc_ajx__user_request_params__get_sanitized( $user_id ) {
174
175 $request_params_values_arr = wpbc_ajx__user_request_params__get( $user_id ); // - $request_params_values_arr - unserialized here automatically
176
177 if ( false !== $request_params_values_arr ) {
178
179 $request_params_structure = wpbc_ajx_get__request_params__names_default(); /**
180 * array( 'page_num' => array( 'validate' => 'd', 'default' => 1 )
181 * , 'page_items_count' => array( 'validate' => 'd', 'default' => 10 )
182 * , 'sort' => array( 'validate' => array( 'booking_id' ), 'default' => 'booking_id' )
183 * , 'sort_type' => array( 'validate' => array( 'ASC', 'DESC'),'default' => 'DESC' )
184 * , 'keyword' => array( 'validate' => 's', 'default' => '' )
185 * , 'create_date' => array( 'validate' => 'date', 'default' => '' )
186 * )
187 */
188
189 $escaped_request_params = wpbc_sanitize_params_in_arr( $request_params_values_arr, $request_params_structure ); // Escaping params here
190
191
192 return $escaped_request_params;
193 } else {
194 return false;
195 }
196 }
197
198 /**
199 * Get user saved option from Request in Booking Listing
200 *
201 * @param $user_id int 1
202 * @param $option_name string 'ui_usr__send_emails'
203 *
204 * @return false|mixed
205 */
206 function wpbc_ajx__user_request_params__get_option( $user_id, $option_name ){
207
208 // Get User saved option from request
209 $escaped_request_params = wpbc_ajx__user_request_params__get_sanitized( $user_id );
210 if ( ( ! empty( $escaped_request_params ) ) && ( ! empty( $escaped_request_params[ $option_name ] ) ) ) {
211 return $escaped_request_params[ $option_name ];
212 }
213
214 // Get default option
215 $default_param_values = wpbc_ajx_get__request_params__names_default( 'default' );
216 if ( ! empty( $default_param_values[ $option_name ] ) ) {
217 return $default_param_values[ $option_name ];
218 }
219
220 // There is no such option
221 return false;
222 }
223
224
225 /**
226 * Is send emails ? Check DB SAVED user defined option from Options toolbar.
227 * @param $user_id int ID of user
228 *
229 * @return int 1 | 0
230 */
231 function wpbc_ajx__user_request_option__is_send_emails( $user_id ){
232
233 $is_send_emeils = wpbc_ajx__user_request_params__get_option( $user_id, 'ui_usr__send_emails' );
234
235 $is_send_emeils = ( 'send' == $is_send_emeils ) ? 1 : 0;
236
237 return $is_send_emeils;
238 }
239
240 /**
241 * Is expand remarks by default ? Check DB SAVED user defined option from Options toolbar.
242 *
243 * It's useful only, when we are checking during Ajax action requests, where need to check data in Database,
244 * For templates, need to use JavaScript:
245 * <# if ( 'Off' === wpbc_ajx_booking_listing.search_get_param('ui_usr__is_expand_remarks') ) { #>display: none;<# } #>
246 *
247 *
248 * @param $user_id int ID of user
249 *
250 * @return int 1 | 0
251 */
252 function wpbc_ajx__user_request_option__is_expand_remarks( $user_id ){
253
254 $is_send_emeils = wpbc_ajx__user_request_params__get_option( $user_id, 'ui_usr__is_expand_remarks' );
255
256 $is_send_emeils = ( 'On' == $is_send_emeils ) ? 1 : 0;
257
258 return $is_send_emeils;
259 }
260
261
262 // </editor-fold>
263
264
265 // <editor-fold defaultstate="collapsed" desc=" S Q L " >
266
267
268 /**
269 * Get array of bookings and total number of items in all pages.
270 *
271 * @param array $request_params = Array(
272 [wh_booking_id] =>
273 [wh_booking_type] =>
274 [booking_type] =>
275 [wh_approved] =>
276 [wh_booking_date] =>
277 [wh_booking_date2] =>
278 [wh_booking_datenext] => 0
279 [wh_booking_dateprior] => 0
280 [wh_booking_datefixeddates] =>
281 [wh_booking_date2fixeddates] =>
282 [wh_is_new] => 0
283 [wh_modification_date] =>
284 [wh_modification_date2] =>
285 [wh_modification_dateprior] => 0
286 [wh_modification_datefixeddates] =>
287 [wh_modification_date2fixeddates] =>
288 [wh_pay_statuscustom] =>
289 [wh_pay_status] =>
290 [wh_cost] => 0
291 [wh_cost2] => 0
292 [wh_sort] =>
293 [wh_trash] =>
294 [page_num] => 1
295 [page_items_count] => 10
296 [view_days_num] => 30
297 [scroll_start_date] =>
298 [scroll_day] => 0
299 [scroll_month] => 0
300 [limit_hours] =>
301 [only_booked_resources] => 0
302 )
303 *
304 * @return array = Array(
305 [count] => 14
306 [data_arr] => Array (
307 [0] => Array (
308 [ajx_booking_id] => 14
309 [last_check_booking_id] => 0
310 [status] =>
311 [last_run_date] =>
312 [booking] => Array (
313 [email_template] => super_new
314 [conditions] => Array (
315 [0] => Array (
316 [if] => __system__|source
317 [sign] => >=
318 [value] => 1"0'0\0
319 )
320 ), ...
321 )
322 [ru_create_date] => 2020-01-25 10:36:55
323 ...
324 ), ...
325 */
326 function wpbc_ajx_get_booking_data_arr( $request_params ){
327
328 // 1. Get booking resources (sql)
329 $resources_arr = wpbc_ajx_get_all_booking_resources_arr(); /**
330 * Array ( [0] => Array ( [booking_type_id] => 1
331 [title] => Standard
332 [users] => 1
333 [import] =>
334 [export] =>
335 [cost] => 25
336 [default_form] => standard
337 [prioritet] => 0
338 [parent] => 0
339 [visitors] => 2
340 ), ... */
341
342 $resources_arr_sorted = wpbc_ajx_get_sorted_booking_resources_arr( $resources_arr );
343
344 // 2. Get all bookings (sql)
345 $bookings = wpbc_ajx_get__bookings_obj__sql( $request_params ); /**
346 * Array ( [count] => 172, [bookings] => Array (
347 * [0] => stdClass Object (
348 [booking_id] => 175
349 [trash] => 0
350 [sync_gid] =>
351 [is_new] => 1
352 [status] =>
353 [sort_date] => 2022-04-19 12:00:01
354 [modification_date] => 2022-04-03 08:05:13
355 [form] => selectbox-one^rangetime5^12:00 - 14:00~text^name5^test data~ ... ~checkbox^term_and_condition5[]^I Accept terms
356 [hash] => ae964965356f7c735139764eebe12a63
357 [booking_type] => 5
358 [remark] =>
359 [cost] => 50.00
360 [pay_status] => 164896951360.82
361 [pay_request] => 0
362 ), ....
363 */
364 $bookings_obj = $bookings['bookings'];
365 $bookings_count = $bookings['count'];
366
367
368 // 3. Get booking dates (sql)
369 $booking_dates_obj = wpbc_ajx_get__booking_dates_obj__sql( $bookings_obj ); /**
370 * Array ( [0] => stdClass Object (
371 [booking_dates_id] => 333
372 [booking_id] => 165
373 [booking_date] => 2022-03-28 11:00:01
374 [approved] => 0
375 [type_id] =>
376 )
377 [1] => ....
378 */
379
380
381 // 4. Include dates into bookings (Wide and Short dates view)
382 $bookings_with_dates = wpbc_ajx_include_bookingdates_in_bookings( $bookings_obj, $booking_dates_obj );
383 /**
384 Array ( [182] => stdClass Object (
385
386 [booking_db] => stdClass Object (
387 [booking_id] => 182
388 [trash] => 0
389 [sync_gid] =>
390 [is_new] => 1
391 [status] =>
392 [sort_date] => 2023-01-23 10:00:01
393 [modification_date] => 2022-04-18 12:23:30
394 [form] => selectbox-one^rangetime2^10:00 - 12:00~text^name2^rr~text^secondname2^hjk~email^email2^hyui@nbco.csdf~text^phone2^h~text^address2^khj~text^city2^e~text^postcode2^hj~selectbox-one^country2^HT~selectbox-one^visitors2^1~selectbox-one^children2^0~textarea^details2^djkh~checkbox^term_and_condition2[]^I Accept term and conditions
395 [hash] => a1ae510d8fee961b7f8ae53101632151
396 [booking_type] => 2
397 [remark] =>
398 [cost] => 75.00
399 [pay_status] => 165028101080.84
400 [pay_request] => 0
401 )
402 [id] => 182
403 [approved] => 0
404 [dates] => Array (
405 [0] => 2023-01-23 10:00:01
406 [1] => 2023-01-25 00:00:00
407 [2] => 2023-01-27 12:00:02
408 )
409 [child_id] => Array (
410 [0] =>
411 [1] =>
412 [2] =>
413 )
414 [short_dates] => Array (
415 [0] => 2023-01-23 10:00:01
416 [1] => ,
417 [2] => 2023-01-25 00:00:00
418 [3] => ,
419 [4] => 2023-01-27 12:00:02
420 )
421 [short_dates_child_id] => Array (
422 [0] =>
423 [1] =>
424 [2] =>
425 [3] =>
426 [4] =>
427 )
428 )
429 [181] => stdClass Object ...
430 */
431
432 // 4.5 remove some bookings, that does not fit to Dates conditions
433 list( $bookings_with_dates, $bookings_count ) = wpbc_ajx_get__remove_bookings__where_dates_outside( $request_params, $bookings_with_dates, $bookings_count );
434
435 // 5. Parse forms
436 $parsed_bookings = wpbc_ajx_parse_bookings( $bookings_with_dates, $resources_arr );
437 /** array( [188] => stdClass Object (
438 [booking_db] => stdClass Object (
439 [booking_id] => 188
440 [trash] => 0
441 [sync_gid] =>
442 [is_new] => 1
443 [status] =>
444 [sort_date] => 2023-03-05 10:00:01
445 [modification_date] => 2022-04-19 11:58:31
446 [form] => text^selected_short_dates_hint4^March 5, 2023~text^days_number_hint4^1~text^cost_hint4^&amp;#36;95~selectbox-one^rangetime4^10:00 - 12:00~text^name4^test~email^email4^test@wpbookingcalendar.com~selectbox-one^my_select4^1~selectbox-multiple^multi_select4[]^0,1,2,3~checkbox^my_checkbx4[]^false~checkbox^my_multi_checkbx4[]^1~checkbox^my_multi_checkbx4[]^~checkbox^my_multi_checkbx4[]^3~checkbox^exclusive_multi_checkbx4^~checkbox^exclusive_multi_checkbx4^2~checkbox^exclusive_multi_checkbx4^~radio^my_radio4^2~selectbox-one^country4^GB~textarea^details4^s~checkbox^term_and_condition4[]^I Accept term and conditions
447 [hash] => dd12c3a61f14aaca693f52d110d2723a
448 [booking_type] => 4
449 [remark] =>
450 [cost] => 95.00
451 [pay_status] => 165036591118.88
452 [pay_request] => 0 )
453 [id] => 188
454 [approved] => 0
455 [dates] => Array ( [0] => 2023-03-05 10:00:01 [1] => 2023-03-05 12:00:02 )
456 [child_id] => Array([0] => [1] => )
457 [short_dates] => Array ( [0] => 2023-03-05 10:00:01 [1] => - [2] => 2023-03-05 12:00:02 )
458 [short_dates_child_id] => Array ( [0] => [1] => [2] => )
459 [form_data] => Array
460 (
461 [selected_short_dates_hint] => March 5, 2023
462 [days_number_hint] => 1
463 [cost_hint] => &amp;#36;95
464 [rangetime] => 10:00 AM - 12:00 PM
465 [name] => test
466 ...
467 [term_and_condition] => I Accept term and conditions
468 [booking_id] => 188
469 [trash] => 0
470 [sync_gid] =>
471 [is_new] => 1
472 [status] =>
473 [sort_date] => 2023-03-05 10:00:01
474 [modification_date] => 2022-04-19 11:58:31
475 [hash] => dd12c3a61f14aaca693f52d110d2723a
476 [booking_type] => 4
477 [cost] => 95.00
478 [pay_status] => 165036591118.88
479 [pay_request] => 0
480 [id] => 188
481 [approved] => 0
482 [resource_title] => Apartment#3
483 [_form_show] => "<div class="payment-content-form"><strong>Times</strong>:<span class="fieldvalue">10:00 AM - 12:00 PM</span>&nbsp;&nbsp; ...."
484 )
485 ...
486 */
487
488 // Reset array keys for correct DESC sorting during sending Ajax request.
489 $parsed_bookings = array_values( $parsed_bookings );
490
491 return array(
492 'booking_resources' => $resources_arr_sorted, //$resources_arr,
493 'data_arr' => $parsed_bookings,
494 'count' => $bookings_count
495 );
496 }
497
498 /**
499 * R E S O U R C E S - Get SORTED booking resources as array
500 *
501 * @param $resources_arr array Array ( [1] => Array (
502 [booking_type_id] => 1
503 [title] => Standard
504 [users] => 1
505 [import] =>
506 [export] =>
507 [cost] => 25
508 [default_form] => standard
509 [prioritet] => 0
510 [parent] => 0
511 [visitors] => 2
512 ), ...
513 *
514 * @return array
515 */
516 function wpbc_ajx_get_sorted_booking_resources_arr( $resources_arr ) {
517
518 if ( ! empty( $resources_arr ) ) {
519 $resources_arr_sorted = wpbc_ajx_arrange_booking_resources_arr( $resources_arr );
520 $resources_arr_sorted = $resources_arr_sorted['linear_resources'];
521 foreach ( $resources_arr_sorted as $key_id => $resource ) {
522 $resources_arr_sorted[ $key_id ]['title'] = wpbc_lang( $resources_arr_sorted[ $key_id ]['title'] );
523 }
524 // Reset keys for having correct sorting (important for parent/child resources) after ajax response
525 $resources_arr_sorted = array_values( $resources_arr_sorted );
526 } else {
527 $resources_arr_sorted = $resources_arr;
528 }
529
530 return $resources_arr_sorted;
531 }
532
533
534 /**
535 * R E S O U R C E S - Get all booking resources as array
536 *
537 * @return array Array ( [1] => Array (
538 [booking_type_id] => 1
539 [title] => Standard
540 [users] => 1
541 [import] =>
542 [export] =>
543 [cost] => 25
544 [default_form] => standard
545 [prioritet] => 0
546 [parent] => 0
547 [visitors] => 2
548 ), ...
549 */
550 function wpbc_ajx_get_all_booking_resources_arr( ){
551
552 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
553 return array();
554 }
555
556 $db_names = wpbc_get_db_names();
557
558 $sql = array();
559
560 $sql['select'] = "SELECT * FROM {$db_names['resources']} as bt";
561
562 $sql['where'] = " WHERE ( 1 = 1 )";
563
564 if ( class_exists( 'wpdev_bk_multiuser' ) ) { // MultiUser - only specific booking resources for specific Regular User in Admin panel.
565
566 if ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) {
567 $user_bk_id = intval( $_REQUEST['wpbc_ajx_user_id'] );
568 } else {
569 $user_bk_id = wpbc_get_current_user_id();
570 }
571 $is_user_super_admin = apply_bk_filter( 'is_user_super_admin', $user_bk_id );
572
573 if ( ! $is_user_super_admin ) {
574 $sql['where'] .= 'AND users = ' . $user_bk_id . ' ';
575 }
576 }
577
578 // Order depends from version //FixIn: 9.9.0.23
579 if ( class_exists( 'wpdev_bk_biz_l' ) ) {
580 $sql['order'] = " ORDER BY parent, prioritet, title, booking_type_id";
581 } else {
582 $sql['order'] = " ORDER BY title, booking_type_id";
583 }
584
585 global $wpdb;
586 $sql_prepared = //$wpdb->prepare(
587 $sql['select']
588 . $sql['where']
589 . $sql['order'];
590 // , $sql['sql_args']
591 // );
592
593 $resources = $wpdb->get_results( $sql_prepared );
594
595 $resources_arr = array();
596
597 foreach ( $resources as $resource ) {
598 $resources_arr[ $resource->booking_type_id ] = get_object_vars( $resource );
599 }
600
601 return $resources_arr;
602 }
603
604
605 /**
606 * Get arranged / sorted booking resources arrays
607 *
608 * @param $resources_sql_arr array( ...
609 * [4] => Array (
610 [booking_type_id] => 4
611 [title] => Apartment#3
612 [users] => 1
613 [import] =>
614 [export] =>
615 [cost] => 270
616 [default_form] => standard
617 [prioritet] => 1
618 [parent] => 0
619 [visitors] => 1
620 )
621 [5] => Array (
622 [booking_type_id] => 5
623 [title] => Standard-1
624 [users] => 1
625 [import] =>
626 [export] =>
627 [cost] => 25
628 [default_form] => standard
629 [prioritet] => 1
630 [parent] => 1
631 [visitors] => 1
632 )
633 ...
634 */
635 function wpbc_ajx_arrange_booking_resources_arr( $all_resources ){
636
637 if ( count( $all_resources ) > 0 ) {
638
639 $resources = array();
640 $child_resources = array();
641 $parent_single_resources = array();
642
643 foreach ( $all_resources as $single_resources ) {
644
645 $single_resources = $single_resources ;
646 $single_resources['id'] = $single_resources['booking_type_id'];
647
648 // Child booking resource
649 if ( ( ! empty( $single_resources[ 'parent' ] ) )
650 ){ // Child
651
652 if ( ! isset( $child_resources[ $single_resources['parent'] ] ) )
653 $child_resources[ $single_resources['parent'] ] = array();
654
655 $child_resources[ $single_resources['parent'] ][ $single_resources['id'] ] = $single_resources;
656
657 } else { // Parent or Single
658
659 $parent_single_resources[ $single_resources['id'] ] = $single_resources;
660 }
661 // All resources
662 $resources[ $single_resources['id'] ] = $single_resources;
663 }
664
665
666 $final_resource_array = array();
667 foreach ( $parent_single_resources as $key => $res) {
668
669 // Calc Capacity
670 if ( isset( $child_resources[$res['id']] ) ) $res['count'] = count( $child_resources[$res['id']] ) + 1;
671 else $res['count'] = 1;
672
673 // Fill the parent resource
674 $final_resource_array[ $res['id'] ] = $res;
675
676 // Fill all child resources (its already sorted) - for having linear array with child resourecs.
677 if ( isset( $child_resources[ $res['id'] ] ) ) {
678 foreach ( $child_resources[ $res['id'] ] as $child_obj ) {
679 $child_obj['count'] = 1;
680 $final_resource_array[ $child_obj['id'] ] = $child_obj;
681 }
682 }
683 }
684
685 return array(
686 'linear_resources' => $final_resource_array
687 , 'single_or_parent' => $parent_single_resources
688 , 'child' => $child_resources
689 );
690 } else {
691 return false;
692 }
693 }
694
695
696 /**
697 * S Q L - B O O K I N G S - Get array of "Bookings" objects from DB
698 * based on request params
699 *
700 * @param array $request_params
701 *
702 * @return array of bookings sql objects
703 */
704 function wpbc_ajx_get__bookings_obj__sql( $request_params ) {
705
706 $defaults = wpbc_ajx_get__request_params__names_default( 'default' );
707
708 $params = wp_parse_args( $request_params, $defaults );
709
710 global $wpdb;
711 $db_names = wpbc_get_db_names();
712
713 $sql_args = array();
714 $sql = array();
715
716
717 $sql['start_select'] = " SELECT * ";
718 $sql['start_count'] = " SELECT COUNT(*) as count";
719 $sql['from'] = " FROM {$db_names['bookings']} as bk";
720
721 $sql['where'] = " WHERE ( 1 = 1 )";
722
723 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
724 // DATES
725 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
726 $sql['where'] .= " AND ( EXISTS (
727 SELECT *
728 FROM {$db_names['dates']} as dt
729 WHERE bk.booking_id = dt.booking_id ";
730
731 // W H E R E : Approved | Pending ----------------------------------------------------------------------
732 if ( $request_params['wh_approved'] !== '' ) {
733 $sql['where'] .= " AND approved = {$request_params['wh_approved']} ";
734 }
735
736 // W H E R E : D A T E S --------------------------------------------------------------------------------
737 if (1){
738 if ( is_array( $request_params['wh_booking_date'] ) ) {
739 $wh_booking_date = array_replace( array( '', '' ), $request_params['wh_booking_date'] ); // Always have minimum 2 values in arr
740 } else {
741 $wh_booking_date = array( '', '' );
742 }
743
744 if ( count( $wh_booking_date ) > 2 ) {
745 $removed_el = array_shift( $wh_booking_date ); // Remove an element off the beginning of array
746 }
747 }
748 $sql['where'] .= wpbc_ajx__sql_where_for_dates( (string) $wh_booking_date[0], (string) $wh_booking_date[1] );
749
750 $sql['where'] .= " )
751 )";
752 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
753
754
755 // W H E R E : M o d i f i c a t i o n D A T E S --------------------------------------------------
756 if (1){
757 if ( is_array( $request_params['wh_modification_date'] ) ) {
758 $wh_modification_date = array_replace( array( '', '' ), $request_params['wh_modification_date'] ); // Always have minimum 2 values in arr
759 } else {
760 $wh_modification_date = array( '', '' );
761 }
762
763 if ( count( $wh_modification_date ) > 2 ) {
764 $removed_el = array_shift( $wh_modification_date ); // Remove an element off the beginning of array
765 }
766 }
767 $sql['where'] .= wpbc_ajx__sql_where_for_modification_date( (string) $wh_modification_date[0], (string) $wh_modification_date[1] );
768
769
770
771 // W H E R E : Resources ------------------------------------------------------------------------------
772 if ( class_exists( 'wpdev_bk_personal' ) ) {
773
774 if ( ! empty( $request_params['wh_booking_type'] ) ) {
775
776 $sql['where'] .= wpbc_ajx__sql_where_for_resources(
777 $request_params['wh_booking_type'],
778 $request_params['wh_approved'],
779 (string) $wh_booking_date[0],
780 (string) $wh_booking_date[1]
781 );
782 }
783 }
784
785
786 // W H E R E : Payment Status -------------------------------------------------------------------------
787 if ( class_exists( 'wpdev_bk_biz_s' ) ) {
788
789 if ( ! empty( $request_params['wh_pay_status'] ) ) {
790
791 $sql_and_args = wpbc_ajx__sql_where_for_payment_status( $request_params['wh_pay_status'] );
792
793 $sql['where'] .= $sql_and_args[0];
794
795 if ( count( $sql_and_args[1] ) > 0 ) {
796 foreach ( $sql_and_args[1] as $my_arg ) {
797 $sql_args[] = $my_arg;
798 }
799 }
800
801 }
802 }
803
804
805 // W H E R E : C o s t Min - Max -------------------------------------------------------------------
806 if ( class_exists( 'wpdev_bk_biz_s' ) ) {
807
808 if ( ( isset( $request_params['wh_cost'] ) )
809 || ( isset( $request_params['wh_cost2'] ) ) ) {
810
811 $wh_cost_min = ( isset( $request_params['wh_cost'] ) ) ? $request_params['wh_cost'] : '';
812 $wh_cost_max = ( isset( $request_params['wh_cost2'] ) ) ? $request_params['wh_cost2'] : '';
813
814 $sql_and_args = wpbc_ajx__sql_where_cost_min_max( $wh_cost_min, $wh_cost_max );
815
816 $sql['where'] .= $sql_and_args[0];
817
818 if ( count( $sql_and_args[1] ) > 0 ) {
819 foreach ( $sql_and_args[1] as $my_arg ) {
820 $sql_args[] = $my_arg;
821 }
822 }
823
824 }
825 }
826
827
828
829 // W H E R E : Trash -----------------------------------------------------------------------------------
830 if ( isset( $request_params['wh_trash'] ) ) {
831
832 if ( '0' === $request_params['wh_trash'] ) { // Existing
833 $sql['where'] .= " AND bk.trash = 0 ";
834 }
835
836 if ( 'trash' === $request_params['wh_trash'] ) { // In trash
837 $sql['where'] .= " AND bk.trash = 1 ";
838 }
839
840 //if ( 'any' === $request_params['wh_trash'] ) { } // Any
841 }
842
843
844 // W H E R E : All bookings | New bookings | Imported bookings | Plugin bookings
845 if ( isset( $request_params['wh_what_bookings'] ) ) {
846
847 if ( 'any' === $request_params['wh_what_bookings'] ) {
848 //$sql['where'] .= " AND bk.is_new = 0 ";
849 }
850 if ( 'new' === $request_params['wh_what_bookings'] ) {
851 $sql['where'] .= " AND bk.is_new = 1";
852 }
853 if ( 'imported' === $request_params['wh_what_bookings'] ) {
854 $sql['where'] .= " AND bk.sync_gid != '' ";
855 }
856 if ( 'in_plugin' === $request_params['wh_what_bookings'] ) {
857 $sql['where'] .= " AND bk.sync_gid = '' ";
858 }
859 }
860
861
862 // W H E R E : K E Y W O R D
863 if ( ! empty( $params['keyword'] ) ) {
864 $sql['where'] .= " AND ( ";
865
866 /**
867 * Relative configuration of LIKE sanitization
868 * check at the bottom here https://code.tutsplus.com/articles/data-sanitization-and-validation-with-wordpress--wp-25536
869 *
870 * So this is a correct way:
871 *
872 * $sql['where'] .= "( bk.form LIKE %s ) ";
873 * $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
874 *
875 */
876
877 $sql['where'] .= "( bk.form LIKE %s ) ";
878 $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
879
880 if ( is_numeric( $params['keyword'] ) ) {
881 $sql['where'] .= " OR ( bk.booking_id = %d ) ";
882 $sql_args[] = intval( $params['keyword'] );
883 }
884
885 $sql['where'] .= " OR ( bk.sync_gid LIKE %s ) ";
886 $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
887
888 if ( class_exists( 'wpdev_bk_personal' ) ) {
889 $sql['where'] .= " OR ( bk.remark LIKE %s ) ";
890 $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
891
892 $sql['where'] .= " OR ( bk.hash LIKE %s ) ";
893 $sql_args[] = '%' . $wpdb->esc_like( $params['keyword'] ) . '%';
894 }
895
896 $sql['where'] .= " )";
897
898
899 // W H E R E : booking ID //FixIn: 9.4.3.9
900 $is_id = strpos( trim( strtolower( $params['keyword'] ) ) , 'id:' ); // Keyword, like 'id:200' OR 'id:100,105'
901 if ( 0 === $is_id ){
902
903 $search_booking_id = substr( trim( $params['keyword'] ), 3 );
904 $search_booking_id_arr = explode( ',', $search_booking_id ); // Get array of booking ID: from 'id:200,201,15' we get [200,201,15]
905
906 $sql_where_arr = array();
907 $sql_args = array(); // It's last WHERE in a list so all previous arguments ($sql_args) we are resetting
908
909 foreach ( $search_booking_id_arr as $bk_id ) {
910
911 $sql_where_arr[] = ' bk.booking_id = %d ';
912 $sql_args[] = intval( $bk_id );
913 }
914
915 $sql['where'] = " WHERE ( " . implode( ' OR ', $sql_where_arr ) . ") ";
916
917 $sql['where'] = apply_bk_filter('update_where_sql_for_getting_bookings_in_multiuser', $sql['where'] ); // Add 'AND bk.booking_type IN ( $user_resources )' to the end of Where
918 }
919 }
920
921 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
922
923
924 switch ( $params['wh_sort'] ) {
925 case 'booking_id__asc':
926 $order_by = 'booking_id ASC ';
927 break;
928 case 'booking_id__desc':
929 $order_by = 'booking_id DESC ';
930 break;
931 case 'dates__asc':
932 $order_by = 'sort_date ASC ';
933 break;
934 case 'dates__desc':
935 $order_by = 'sort_date DESC ';
936 break;
937 case 'resource__asc':
938 $order_by = 'booking_type ASC ';
939 break;
940 case 'resource__desc':
941 $order_by = 'booking_type DESC ';
942 break;
943 case 'cost__asc':
944 $order_by = 'cost ASC ';
945 break;
946 case 'cost__desc':
947 $order_by = 'cost DESC ';
948 break;
949 default:
950 $order_by = 'booking_id ASC ';
951 }
952 $sql['order'] = " ORDER BY bk." . $order_by; // $sql['order'] = " ORDER BY bk." . esc_sql( $params['sort'] ) . ( ( 'DESC' == $params['sort_type'] ) ? " DESC " : " ASC " );
953
954 $sql_args_count = $sql_args; // For SELECT COUNT(*) as count we do not need other parameters
955
956 $sql['limit'] = " LIMIT %d, %d ";
957
958 $sql_args[] = ( $params['page_num'] - 1 ) * $params['page_items_count'];
959 $sql_args[] = $params['page_items_count'];
960
961
962
963 /**
964 * Good Practice: https://blog.ircmaxell.com/2017/10/disclosure-wordpress-wpdb-sql-injection-technical.html
965 * fixed in WordPress 4.8.3
966 *
967 $where = "WHERE foo = %s";
968 $args = [$_GET['data']];
969 $args[] = 1;
970 $args[] = 2;
971 $query = $wpdb->prepare("SELECT * FROM something $where LIMIT %d, %d", $args);
972 *
973 */
974
975 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
976 // SELECT at this specific PAGE /////////////////////////////////////////////////////////////////
977 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
978 $sql_prepared = $wpdb->prepare(
979 $sql['start_select']
980 . $sql['from']
981 . $sql['where']
982 . $sql['order']
983 . $sql['limit']
984 , $sql_args
985 );
986
987 $bookings_sql_obj = $wpdb->get_results($sql_prepared);
988
989
990 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
991 // COUNT of items with this WHERE ///////////////////////////////////////////////////////////////////
992 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
993 $sql_for_listing_count = $sql['start_count']
994 . $sql['from']
995 . $sql['where'];
996
997 if ( false === strpos( $sql_for_listing_count, '%' ) ) {
998
999 $sql_prepared = $sql_for_listing_count;
1000
1001 } else {
1002 $sql_prepared = $wpdb->prepare(
1003 $sql_for_listing_count
1004 , $sql_args_count
1005 );
1006 }
1007 $bookings_count = $wpdb->get_results( $sql_prepared );
1008 $bookings_count = ( ( count( $bookings_count ) > 0 ) ? $bookings_count[0]->count : 0 );
1009
1010 return array(
1011 'count' => $bookings_count
1012 , 'bookings' => $bookings_sql_obj
1013 );
1014 }
1015
1016 // W H E R E C O N D I T I O N S
1017
1018 /**
1019 * Get SQL W H E R E conditions for D a t e s of bookings
1020 *
1021 * @param string $wh_booking_date - Parameter from Booking Listing request (usually its number)
1022 * @param string $wh_booking_date2 - Parameter from Booking Listing request (usually its number)
1023 * @param string $pref - Optional. Prefix for table.
1024 * @return string - WHERE conditions for SQL
1025 */
1026 function wpbc_ajx__sql_where_for_dates( $wh_booking_date, $wh_booking_date2, $pref = 'dt.' ) {
1027
1028 $wh_booking_date = (string) $wh_booking_date;
1029 $wh_booking_date2 = (string) $wh_booking_date2;
1030
1031 $sql_where= '';
1032 if ($pref == 'dt.') { $and_pre = ' AND '; $and_suf = ''; }
1033 else { $and_pre = ''; $and_suf = ' AND '; }
1034
1035 // Actual
1036 if ( ( ( $wh_booking_date === '' ) && ( $wh_booking_date2 === '' ) ) || ($wh_booking_date === '0') ) {
1037 $sql_where = $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; //FixIn: 8.5.2.14
1038
1039 } else if ($wh_booking_date === '1') { // Today //FixIn: 7.1.2.8
1040 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ;
1041 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; //FixIn: 8.4.7.21
1042
1043
1044 } else if ($wh_booking_date === '2') { // Previous
1045 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; //FixIn: 8.5.2.16
1046
1047 } else if ($wh_booking_date === '3') { // All
1048 $sql_where = '';
1049
1050 } else if ($wh_booking_date === '4') { // Next
1051 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL ". $wh_booking_date2 . " DAY ) ) ".$and_suf ;
1052 // $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL 1 DAY ) ) ".$and_suf ;
1053 $sql_where .= $and_pre."( ".$pref."booking_date > ( CURDATE() ) ) ".$and_suf ; //FixIn: 8.0.1.1
1054
1055 } else if ($wh_booking_date === '5') { // Prior
1056 $wh_booking_date2 = str_replace('-', '', $wh_booking_date2);
1057 $sql_where = $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL ". $wh_booking_date2 . " DAY ) ) ".$and_suf ;
1058 $sql_where .= $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1059
1060 } else if ($wh_booking_date === '7') { // Check In date - Today/Tomorrow
1061 // $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ;
1062 // $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() ) ) ".$and_suf ;
1063 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '1 23:59:59' DAY_SECOND ) ) ".$and_suf ;
1064 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1065
1066 } else if ($wh_booking_date === '8') { // Check Out date - Tomorrow
1067 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '1 23:59:59' DAY_SECOND ) ) ".$and_suf ;
1068 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1069
1070 } else if ( ( $wh_booking_date === '9' ) || ( $wh_booking_date === '10' ) || ( $wh_booking_date === '11' ) ) { // Today check in/out
1071 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1072 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL 1 DAY ) ) ".$and_suf ;
1073
1074 } else { // Fixed
1075
1076 $wh_booking_date = wpbc_sanitize_date( $wh_booking_date );
1077 $wh_booking_date2 = wpbc_sanitize_date( $wh_booking_date2 );
1078
1079 /*
1080 if ( $wh_booking_date !== '' ){
1081 $sql_where .= $and_pre . "( " . $pref . "booking_date >= ( '" . $wh_booking_date . "' - INTERVAL '00:00:01' HOUR_SECOND ) ) " . $and_suf;
1082 }
1083 if ( $wh_booking_date2 !== '' ) {
1084 $sql_where .= $and_pre . "( " . $pref . "booking_date <= ( '" . $wh_booking_date2 . "' + INTERVAL '23:59:59' HOUR_SECOND ) ) " . $and_suf;
1085 }
1086 */
1087
1088 if ( $wh_booking_date !== '' ) {
1089 if ( strpos( $wh_booking_date, ':' ) === false ) {
1090 $sql_where .= $and_pre . "( " . $pref . "booking_date >= '" . $wh_booking_date . " 00:00:00' ) " . $and_suf;
1091 } else {
1092 $sql_where .= $and_pre . "( " . $pref . "booking_date >= '" . $wh_booking_date . "' ) " . $and_suf;
1093 }
1094 }
1095
1096 if ( $wh_booking_date2 !== '' ) {
1097 if ( strpos( $wh_booking_date2, ':' ) === false ) {
1098 $sql_where .= $and_pre . "( " . $pref . "booking_date <= '" . $wh_booking_date2 . " 23:59:59' ) " . $and_suf;
1099 } else {
1100 $sql_where .= $and_pre . "( " . $pref . "booking_date <= '" . $wh_booking_date2 . "' ) " . $and_suf;
1101 }
1102 }
1103
1104
1105 }
1106
1107 return $sql_where;
1108 }
1109
1110
1111 /**
1112 * Get SQL W H E R E conditions for M o d i f i c a t i o n D a t e of bookings
1113 *
1114 * @param string $wh_modification_date - Parameter from Booking Listing request (usually its number)
1115 * @param string $wh_modification_date2 - Parameter from Booking Listing request (usually its number)
1116 * @param string $pref - Optional. Prefix for table.
1117 * @return string - WHERE conditions for SQL
1118 */
1119 function wpbc_ajx__sql_where_for_modification_date( $wh_modification_date, $wh_modification_date2, $pref = 'bk.' ) {
1120
1121 $sql_where = '';
1122
1123 if ($pref == 'bk.') { $and_pre = ' AND '; $and_suf = ''; }
1124 else { $and_pre = ''; $and_suf = ' AND '; }
1125
1126 if ($wh_modification_date === '1') { // Today
1127 $sql_where = $and_pre."( ".$pref."modification_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ; //FixIn: 8.4.7.22
1128 $sql_where .= $and_pre."( ".$pref."modification_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; //FixIn: 8.4.7.22
1129
1130 } else if ($wh_modification_date === '3') { // All
1131 $sql_where = '';
1132
1133 } else if ($wh_modification_date === '5') { // Prior
1134 $wh_modification_date2 = str_replace('-', '', $wh_modification_date2);
1135 $sql_where = $and_pre."( ".$pref."modification_date >= ( CURDATE() - INTERVAL ". $wh_modification_date2 . " DAY ) ) ".$and_suf ;
1136 $sql_where .= $and_pre."( ".$pref."modification_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1137
1138 } else { // Fixed
1139
1140 $wh_modification_date = wpbc_sanitize_date( $wh_modification_date ); //FixIn: 9.4.4.1
1141 $wh_modification_date2 = wpbc_sanitize_date( $wh_modification_date2 );
1142
1143 if ( $wh_modification_date !== '' )
1144 $sql_where.= $and_pre."( ".$pref."modification_date >= ( '" . $wh_modification_date . "' - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf;
1145
1146 if ( $wh_modification_date2 !== '' )
1147 $sql_where.= $and_pre."( ".$pref."modification_date <= ( '" . $wh_modification_date2 . "' + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf;
1148 }
1149
1150 return $sql_where;
1151 }
1152
1153
1154 /**
1155 * Get SQL W H E R E conditions for B o o k i n g R e s o u r c e s
1156 *
1157 * @param $wh_booking_type
1158 * @param $wh_approved
1159 * @param $wh_booking_date
1160 * @param $wh_booking_date2
1161 *
1162 * @return string - SQL
1163 */
1164 function wpbc_ajx__sql_where_for_resources( $wh_booking_type, $wh_approved, $wh_booking_date, $wh_booking_date2 ){
1165 global $wpdb;
1166
1167 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
1168 return '';
1169 }
1170
1171 if ( is_array( $wh_booking_type ) ) {
1172 $wh_booking_type = implode( ',', $wh_booking_type );
1173 }
1174
1175 $sql_where = '';
1176
1177 if ( '0' === $wh_booking_type ) { // All booking resources
1178 // Get all booking resources of this user
1179 $resources_sql_arr = wpbc_ajx_get_all_booking_resources_arr();
1180 $wh_booking_type = implode( ',', array_keys( $resources_sql_arr ) );
1181 //$resources_arr = wpbc_ajx_arrange_booking_resources_arr( $resources_sql_arr );
1182 }
1183
1184 if ( '-1' === $wh_booking_type ) { // Lost booking resources
1185
1186 $is_show_lost = true;
1187
1188 if ( class_exists( 'wpdev_bk_multiuser' ) ) { // MultiUser - Only for super booking admin user
1189
1190 $user_bk_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id();
1191
1192 $is_user_super_admin = apply_bk_filter( 'is_user_super_admin', $user_bk_id );
1193
1194 if ( ! $is_user_super_admin ) {
1195 $is_show_lost = false; // For regular user show all bookings from the booking resources that belong to this user.
1196 $sql_where .= " AND bk.booking_type IN ( SELECT DISTINCT booking_type_id FROM {$wpdb->prefix}bookingtypes WHERE users = " . $user_bk_id . " ) ";
1197 }
1198 }
1199
1200 // "Lost" bookings in deleted booking resources
1201 if ( $is_show_lost ) {
1202 $sql_where .= " AND bk.booking_type NOT IN ( SELECT DISTINCT booking_type_id FROM {$wpdb->prefix}bookingtypes ) ";
1203 }
1204
1205 } else if ( ! empty( $wh_booking_type ) ) {
1206 // P
1207 $sql_where .= " AND ( ";
1208 $sql_where .= " ( bk.booking_type IN ( " . $wh_booking_type . " ) ) ";
1209
1210 // BL - Show bookings from child booking resources, if was selected only parent booking resource
1211 $is_show_bookings_for_child_resources = true;
1212 if ( $is_show_bookings_for_child_resources ) {
1213 $sql_where .= wpbc_ajx__sql_where_for_resources_bl( $wh_booking_type, $wh_approved, $wh_booking_date, $wh_booking_date2 );
1214 }
1215
1216 // P
1217 $sql_where .= " ) ";
1218
1219 // MU - Check if searching bookings are belongs to specific user in Booking Calendar MultiUser version
1220 $sql_where = apply_bk_filter( 'update_where_sql_for_getting_bookings_in_multiuser', $sql_where );
1221 }
1222
1223 return $sql_where;
1224 }
1225
1226
1227 /**
1228 * Get SQL W H E R E conditions for B o o k i n g R e s o u r c e s BL - Capacity
1229 *
1230 * @param $wh_booking_type
1231 * @param $wh_approved
1232 * @param $wh_booking_date
1233 * @param $wh_booking_date2
1234 *
1235 * @return string - SQL
1236 */
1237 function wpbc_ajx__sql_where_for_resources_bl( $wh_booking_type, $wh_approved, $wh_booking_date, $wh_booking_date2 ){
1238
1239 if ( ! class_exists( 'wpdev_bk_biz_l' ) ) {
1240 return '';
1241 }
1242
1243 global $wpdb;
1244 $sql_where = '';
1245
1246 // BL // Childs in dif sub resources
1247 $sql_where.= " OR ( bk.booking_id IN (
1248 SELECT DISTINCT booking_id
1249 FROM {$wpdb->prefix}bookingdates as dtt
1250 WHERE " ;
1251 if ($wh_approved !== '')
1252 $sql_where.= " dtt.approved = $wh_approved AND " ;
1253 $sql_where .= wpbc_ajx__sql_where_for_dates( $wh_booking_date, $wh_booking_date2, 'dtt.' );
1254
1255 $sql_where.= " (
1256 dtt.type_id IN ( ". $wh_booking_type ." )
1257 OR dtt.type_id IN (
1258 SELECT booking_type_id
1259 FROM {$wpdb->prefix}bookingtypes as bt
1260 WHERE bt.parent IN ( ". $wh_booking_type ." )
1261 )
1262 )
1263 )
1264 ) " ;
1265
1266 if ( ( isset($_REQUEST['view_mode']) ) && ( $_REQUEST['view_mode']== 'vm_calendar' ) ) {
1267
1268 // Skip the bookings from the children resources, if we are in the Calendar view mode at the admin panel
1269
1270 } else {
1271 // BL // Just children booking resources
1272 $sql_where .= " OR ( bk.booking_type IN (
1273 SELECT booking_type_id
1274 FROM {$wpdb->prefix}bookingtypes as bt
1275 WHERE bt.parent IN ( " . $wh_booking_type . " )
1276 )
1277 )";
1278 }
1279
1280 return $sql_where;
1281 }
1282
1283
1284 /**
1285 * Get SQL W H E R E conditions for P a y m e n t S t a t u s
1286 *
1287 * @param $wh_pay_status [ 'any ' ] | ['group_ok'] | ['group_unknown' ] | ['group_pending'] | ['group_failed'] | [ 'user_entered', 'myCustom status' ]
1288 *
1289 * @return array [ string SQL, array ARGS ]
1290 */
1291 function wpbc_ajx__sql_where_for_payment_status( $wh_pay_status_arr ){
1292
1293 // [ 'any ' ] | ['group_ok'] | ['group_unknown' ] | ['group_pending'] | ['group_failed'] | [ 'user_entered', 'myCustom status' ]
1294
1295 $wh_pay_status_custom = '';
1296 $wh_pay_status = '';
1297
1298 if ( ! empty( $wh_pay_status_arr ) ) {
1299
1300 if ( count( $wh_pay_status_arr ) > 1 ) {
1301 $wh_pay_status_custom = $wh_pay_status_arr[1];
1302 }
1303 $wh_pay_status = $wh_pay_status_arr[0];
1304 }
1305
1306 $sql_where = '';
1307 $sql_args = array();
1308
1309 if ( ( '' != $wh_pay_status ) && ( 'all' != $wh_pay_status ) ) {
1310
1311 $sql_where .= " AND ( ";
1312
1313 // Check firstly if we are selected some goup of payment status
1314 if ( $wh_pay_status == 'group_ok' ) { // SUCCESS
1315
1316 $payment_status = wpbc_get_payment_status_ok();
1317
1318 foreach ( $payment_status as $label ) {
1319 $sql_where .= " ( bk.pay_status = '" . $label . "' ) OR";
1320 }
1321 $sql_where = substr( $sql_where, 0, - 2 );
1322
1323 } else if ( ( $wh_pay_status == 'group_unknown' ) || ( is_numeric( $wh_pay_status_custom ) ) ) { // UNKNOWN
1324
1325 $payment_status = wpbc_get_payment_status_unknown();
1326 foreach ( $payment_status as $label ) {
1327 $sql_where .= " ( bk.pay_status = '" . $label . "' ) OR";
1328 }
1329 //$sql_where = substr($sql_where, 0, -2);
1330 $sql_where .= " ( bk.pay_status = '' ) OR ( bk.pay_status regexp '^[0-9]') ";
1331
1332 } else if ( $wh_pay_status == 'group_pending' ) { // Pending
1333
1334 $payment_status = wpbc_get_payment_status_pending();
1335 foreach ( $payment_status as $label ) {
1336 $sql_where .= " ( bk.pay_status = '" . $label . "' ) OR";
1337 }
1338 $sql_where = substr( $sql_where, 0, - 2 );
1339
1340 } else if ( $wh_pay_status == 'group_failed' ) { // Failed
1341
1342 $payment_status = wpbc_get_payment_status_error();
1343 foreach ( $payment_status as $label ) {
1344 $sql_where .= " ( bk.pay_status = '" . $label . "' ) OR";
1345 }
1346 $sql_where = substr( $sql_where, 0, - 2 );
1347
1348 } else { // CUSTOM Payment Status
1349 $sql_where .= " bk.pay_status = %s ";
1350
1351 // $wh_pay_status_custom = htmlspecialchars_decode( $wh_pay_status_custom ); // ? Convert special HTML entities back to characters: "&#60; - &#62;" to "< - >"
1352 $sql_args[] = $wh_pay_status_custom;
1353 }
1354
1355 $sql_where .= " ) ";
1356 }
1357
1358 return array( $sql_where, $sql_args );
1359 }
1360
1361
1362 /**
1363 * Get SQL W H E R E conditions for C o s t Min - Max
1364 *
1365 * @param $wh_cost_min '' or number
1366 * @param $wh_cost_max '' or number
1367 *
1368 * @return array [ string SQL, array ARGS ]
1369 */
1370 function wpbc_ajx__sql_where_cost_min_max( $wh_cost_min, $wh_cost_max ) {
1371
1372 $sql_where = '';
1373 $sql_args = array();
1374
1375 if ( $wh_cost_min !== '' ) {
1376 $sql_where .= " AND ( bk.cost >= %f ) ";
1377 $sql_args[] = $wh_cost_min;
1378 }
1379 if ( $wh_cost_max !== '' ) {
1380 $sql_where .= " AND ( bk.cost <= %f ) ";
1381 $sql_args[] = $wh_cost_max;
1382 }
1383
1384 return array( $sql_where, $sql_args );
1385
1386 }
1387
1388
1389 /**
1390 * D A T E S - Get array of "Booking Dates" objects from DB
1391 * relative to specific bookings - array of Bookings objects from DB
1392 *
1393 * @param array of object $bookings_sql_obj
1394 *
1395 * @return array of booking dates sql objects
1396 */
1397 function wpbc_ajx_get__booking_dates_obj__sql( $bookings_sql_obj ){
1398
1399 global $wpdb;
1400 $db_names = wpbc_get_db_names();
1401
1402 // Get list of booking ID from bookings obj ///////////////////////////////////////////////////////////////
1403 $booking_id_list = array();
1404 foreach ( $bookings_sql_obj as $booking ) {
1405 $booking_id_list[] = intval( $booking->booking_id );
1406 }
1407 $booking_id_list = array_unique( $booking_id_list ); // remove duplicates
1408 $booking_id_list = implode( ",", $booking_id_list );
1409 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
1410
1411
1412 if ( ! empty( $booking_id_list ) ) { // Get Dates for all our Bookings
1413
1414 $sql = "SELECT *
1415 FROM {$db_names['dates']} as dt
1416 WHERE dt.booking_id in ( {$booking_id_list} ) ";
1417
1418 if ( class_exists( 'wpdev_bk_biz_l' ) ) {
1419 $sql .= " ORDER BY booking_id, type_id, booking_date ";
1420 } else {
1421 $sql .= " ORDER BY booking_id, booking_date ";
1422 }
1423
1424 $booking_dates_sql_obj = $wpdb->get_results( $sql );
1425 } else {
1426 $booking_dates_sql_obj = array();
1427 }
1428
1429 return $booking_dates_sql_obj;
1430 }
1431
1432
1433 /**
1434 * S Q L - B O O K I N G S - Get array of "Bookings" objects from DB
1435 * based on request params
1436 *
1437 * @param array $request_params
1438 * @param array of object $bookings_sql_obj
1439 *
1440 * @return array of array( booking dates, $bookings_count) array( sql objects, int )
1441 */
1442 function wpbc_ajx_get__remove_bookings__where_dates_outside($request_params, $bookings_obj, $bookings_count){
1443
1444 $is_id = strpos( trim( strtolower( $request_params['keyword'] ) ) , 'id:' );
1445 if ( false !== $is_id ) {
1446 return array( $bookings_obj, $bookings_count );
1447 }
1448 // W H E R E : D A T E S --------------------------------------------------------------------------------
1449 if (1){
1450 if ( is_array( $request_params['wh_booking_date'] ) ) {
1451 $wh_booking_date_arr = array_replace( array( '', '' ), $request_params['wh_booking_date'] ); // Always have minimum 2 values in arr
1452 } else {
1453 $wh_booking_date_arr = array( '', '' );
1454 }
1455
1456 if ( count( $wh_booking_date_arr ) > 2 ) {
1457 $removed_el = array_shift( $wh_booking_date_arr ); // Remove an element off the beginning of array
1458 }
1459 }
1460
1461 $wh_booking_date = (string) $wh_booking_date_arr[0];
1462 $wh_booking_date2 =(string) $wh_booking_date_arr[1];
1463
1464 // Check In date - Tomorrow
1465 if ( $wh_booking_date === '7' ) {
1466
1467 $today_mysql_format = date_i18n( 'Y-m-d', time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) + DAY_IN_SECONDS ); // Tommorow day with gmt offset
1468 foreach ( $bookings_obj as $bc_id => $bc_value ) {
1469 $check_in_date = $bc_value->short_dates[0];
1470 $check_in_date = explode( ' ', $check_in_date );
1471 $check_in_date = $check_in_date[0]; // 2014-02-25
1472 if ( $today_mysql_format != $check_in_date ) {
1473 unset( $bookings_obj[ $bc_id ] );
1474 $bookings_count--;
1475 }
1476 }
1477 }
1478
1479 // Check Out date - Tomorrow
1480 if ( $wh_booking_date === '8' ) {
1481
1482 $tomorrow_mysql_format = date_i18n( 'Y-m-d', time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) + DAY_IN_SECONDS ); // Tommorow day with gmt offset
1483 foreach ( $bookings_obj as $bc_id => $bc_value ) {
1484 if ( 1 == count( $bc_value->short_dates ) ) {
1485 $check_out_date = $bc_value->short_dates[0];
1486 } else {
1487 $check_out_date = $bc_value->short_dates[2];
1488 }
1489 $check_out_date = explode( ' ', $check_out_date );
1490 $check_out_date = $check_out_date[0]; // 2014-02-25
1491 if ( $tomorrow_mysql_format != $check_out_date ) {
1492 unset( $bookings_obj[ $bc_id ] );
1493 $bookings_count --;
1494 }
1495 }
1496 }
1497
1498 // Today == check in/out | Today = Check in | Today = Check out
1499 if ( ( $wh_booking_date === '9' ) || ( $wh_booking_date === '10' ) || ( $wh_booking_date === '11' ) ) {
1500
1501 $today_mysql_format = date_i18n( 'Y-m-d', time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) + 0 * DAY_IN_SECONDS ); // Today day with gmt offset
1502
1503 foreach ( $bookings_obj as $bc_id => $bc_value ) {
1504
1505 $check_in_date = $bc_value->short_dates[0];
1506 $check_in_date = explode( ' ', $check_in_date );
1507 $check_in_date = $check_in_date[0]; // 2014-02-25
1508
1509 if ( count( $bc_value->short_dates ) == 1 ) {
1510 $check_out_date = $bc_value->short_dates[0];
1511 } else {
1512 $check_out_date = $bc_value->short_dates[2];
1513 }
1514 $check_out_date = explode( ' ', $check_out_date );
1515 $check_out_date = $check_out_date[0]; // 2014-02-25
1516
1517 // Check In
1518 if ( $wh_booking_date === '10' ) {
1519 if ( $today_mysql_format != $check_in_date ) {
1520 unset( $bookings_obj[ $bc_id ] );
1521 $bookings_count --;
1522 }
1523 }
1524 // Check out
1525 if ( $wh_booking_date === '11' ) {
1526 if ( $today_mysql_format != $check_out_date ) {
1527 unset( $bookings_obj[ $bc_id ] );
1528 $bookings_count --;
1529 }
1530 }
1531 // Check In | Check out //FixIn: 9.6.3.12
1532 if ( $wh_booking_date === '9' ) {
1533 if ( ( $today_mysql_format != $check_in_date ) && ( $today_mysql_format != $check_out_date ) ) {
1534 unset( $bookings_obj[ $bc_id ] );
1535 $bookings_count --;
1536 }
1537 }
1538
1539 }
1540 }
1541
1542 return array( $bookings_obj, $bookings_count );
1543 }
1544
1545 /**
1546 * Get array of bookings with dates (wide and short dates view)
1547 * after inserting dates into the bookings
1548 *
1549 * @param $bookings_obj Array (
1550 [0] => stdClass Object (
1551 [booking_id] => 182
1552 [trash] => 0
1553 [sync_gid] =>
1554 [is_new] => 1
1555 [status] =>
1556 [sort_date] => 2023-01-23 10:00:01
1557 [modification_date] => 2022-04-18 12:23:30
1558 [form] => selectbox-one^rangetime2^10:00 - 12:00~text^name2^rr~text^secondname2^hjk~email^email2^hyui@nbco.csdf~text^phone2^h~text^address2^khj~text^city2^e~text^postcode2^hj~selectbox-one^country2^HT~selectbox-one^visitors2^1~selectbox-one^children2^0~textarea^details2^djkh~checkbox^term_and_condition2[]^I Accept term and conditions
1559 [hash] => a1ae510d8fee961b7f8ae53101632151
1560 [booking_type] => 2
1561 [remark] =>
1562 [cost] => 75.00
1563 [pay_status] => 165028101080.84
1564 [pay_request] => 0
1565 )
1566 [1] => stdClass Object, ....
1567 * @param $booking_dates_obj Array (
1568 [0] => stdClass Object (
1569 [booking_dates_id] => 294
1570 [booking_id] => 147
1571 [booking_date] => 2022-06-27 12:00:01
1572 [approved] => 0
1573 [type_id] =>
1574 )
1575 [1] => stdClass Object, ...
1576 *
1577 * @return array Array (
1578 * [182] => stdClass Object (
1579 * ... many other props ....
1580 * [id] => 182
1581 * [approved] => 0
1582 * [dates] => Array (
1583 * [0] => 2023-01-23 10:00:01
1584 * [1] => 2023-01-25 00:00:00
1585 * [2] => 2023-01-27 12:00:02
1586 * )
1587 * [child_id] => Array (
1588 * [0] =>
1589 * [1] =>
1590 * [2] =>
1591 * )
1592 * [short_dates] => Array (
1593 * [0] => 2023-01-23 10:00:01
1594 * [1] => ,
1595 * [2] => 2023-01-25 00:00:00
1596 * [3] => ,
1597 * [4] => 2023-01-27 12:00:02
1598 * )
1599 * [short_dates_child_id] => Array (
1600 * [0] =>
1601 * [1] =>
1602 * [2] =>
1603 * [3] =>
1604 * [4] =>
1605 * )
1606 * )
1607 * [181] => stdClass Object (
1608 * ....
1609 */
1610 function wpbc_ajx_include_bookingdates_in_bookings( $bookings_obj, $booking_dates_obj ){
1611
1612 $bookings_arr = array();
1613 foreach ( $bookings_obj as $booking ) {
1614
1615 $bookings_arr[$booking->booking_id] = new StdClass;
1616 $bookings_arr[$booking->booking_id]->booking_db = $booking;
1617 $bookings_arr[$booking->booking_id]->id = $booking->booking_id;
1618 $bookings_arr[$booking->booking_id]->approved = 0;
1619 $bookings_arr[$booking->booking_id]->dates = array();
1620 $bookings_arr[$booking->booking_id]->child_id = array();
1621 /*
1622 $resource_id = (isset( $booking->booking_type )) ? $booking->booking_type : '1';
1623
1624 // booking Form parse can be here
1625
1626
1627 if ( ( isset( $booking->sync_gid ) ) && (!empty( $booking->sync_gid )) ) {
1628 $booking->form .= "~text^sync_gid{$booking->booking_type}^{$booking->sync_gid}";
1629 }
1630
1631 $cont = wpbc__legacy__get_form_content_arr( $booking->form
1632 , $resource_id
1633 , ''
1634 , array(
1635 'booking_id' => $booking->booking_id
1636 , 'resource_title' => (isset( $booking_types[$booking->booking_type] )) ? $booking_types[$booking->booking_type] : ''
1637 )
1638 );
1639 $search = array( "'(<br[ ]?[/]?>)+'si", "'(<[/]?p[ ]?>)+'si" );
1640 $replace = array( "&nbsp;&nbsp;", " &nbsp; ", " &nbsp; " );
1641 $cont['content'] = preg_replace( $search, $replace, $cont['content'] );
1642 //debuge( htmlentities( $cont['content'] ) );die;
1643 $bookings_arr[$booking->booking_id]->form_show = $cont['content'];
1644 unset( $cont['content'] );
1645 $bookings_arr[$booking->booking_id]->form_data = $cont;
1646
1647 /**/
1648 }
1649
1650 // Wide dates in bookings
1651 foreach ( $booking_dates_obj as $date ) {
1652 $bookings_arr[ $date->booking_id ]->approved = $date->approved;
1653
1654 $bookings_arr[ $date->booking_id ]->dates[] = $date->booking_date;
1655 $bookings_arr[ $date->booking_id ]->child_id[] = ( isset( $date->type_id ) ) ? $date->type_id : '';
1656 }
1657
1658 // Short dates
1659 foreach ( $bookings_arr as $booking_id => $booking ) {
1660
1661 if ( count( $booking->dates ) == 0 ) { continue; } // If no dates, then skip
1662
1663 $bookings_arr[ $booking_id ]->short_dates = array( $booking->dates[0] ); // First Day
1664 $bookings_arr[ $booking_id ]->short_dates_child_id = array( $bookings_arr[ $booking_id ]->child_id[0] );
1665 $previous = array(
1666 'date' => $booking->dates[0],
1667 'date_index' => 0,
1668 'separator' => false
1669 );
1670
1671 if ( count( $booking->dates ) == 1 ) { continue; } // if 1 date, then skip
1672
1673
1674 for ( $date_indx = 1; $date_indx < ( count( $booking->dates ) ); $date_indx ++ ) { // Start from 2nd day
1675
1676 $date = $booking->dates[ $date_indx ];
1677
1678 // if ( wpbc_is_next_day( $date, $previous['date'] ) ) {
1679 $next_day_if__check_in__then__check_out = true;
1680 if ( wpbc_is_less_than_next_day( $date, $previous['date'], $next_day_if__check_in__then__check_out ) ) {
1681
1682 if ( '-' != $previous['separator'] ) {
1683 $bookings_arr[ $booking_id ]->short_dates[] = '-';
1684 $bookings_arr[ $booking_id ]->short_dates_child_id[] = '';
1685 }
1686 $previous['separator'] = '-';
1687
1688 } else {
1689
1690 if ( '-' == $previous['separator'] ) {
1691
1692 $bookings_arr[ $booking_id ]->short_dates[] = $previous['date'];
1693 $bookings_arr[ $booking_id ]->short_dates_child_id[] = $bookings_arr[ $booking_id ]->child_id[ $previous['date_index'] ];
1694 }
1695 $bookings_arr[ $booking_id ]->short_dates[] = ',';
1696 $bookings_arr[ $booking_id ]->short_dates_child_id[] = '';
1697
1698 $bookings_arr[ $booking_id ]->short_dates[] = $date;
1699 $bookings_arr[ $booking_id ]->short_dates_child_id[] = $bookings_arr[ $booking_id ]->child_id[ $date_indx ];
1700
1701 $previous['separator'] = ',';
1702 }
1703
1704 $previous['date'] = $date;
1705 $previous['date_index'] = $date_indx;
1706
1707 } // dates loop: $date_indx => $date
1708
1709 if ( '-' == $previous['separator'] ) {
1710 $bookings_arr[ $booking_id ]->short_dates[] = $previous['date'];
1711 $bookings_arr[ $booking_id ]->short_dates_child_id[] = $bookings_arr[ $booking_id ]->child_id[ $previous['date_index'] ];
1712 }
1713
1714 } // bookings loop: $booking_id => $booking
1715
1716 return $bookings_arr;
1717 }
1718
1719
1720 function wpbc_ajx_parse_bookings( $bookings_arr, $resources_arr ) {
1721
1722 $user_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id();
1723
1724 foreach ( $bookings_arr as $booking_id => $booking ) {
1725
1726 // Booking resource ------------------------------------------------------------------------------------
1727 $resource_id = ( isset( $booking->booking_db->booking_type ) ) ? $booking->booking_db->booking_type : '1';
1728 $resource_title = '';
1729 $resource_owner_user = $user_id;
1730
1731 if ( class_exists( 'wpdev_bk_personal' ) ) {
1732 $resource_title = ( isset( $resources_arr[ $resource_id ] ) ) ? $resources_arr[ $resource_id ]['title'] : __( 'Resource not exist', 'booking' );
1733 $resource_title = wpbc_lang( $resource_title );
1734 }
1735 if ( class_exists( 'wpdev_bk_multiuser' ) ) {
1736 $resource_owner_user = ( isset( $resources_arr[ $resource_id ] ) ) ? $resources_arr[ $resource_id ]['users'] : $user_id;
1737 }
1738
1739 // Parse form fields only from $booking->booking_db->form ------------------------------------------
1740 $booking_data_arr = wpbc_parse_booking_data_fields( $booking->booking_db->form,
1741 array( 'resource_id' => $resource_id )
1742 );
1743
1744
1745 foreach ( $booking_data_arr as $booking_data_arr_key => $booking_data_arr_value ) {
1746 // Booking Listing escaping
1747 $booking_data_arr[$booking_data_arr_key] = wpbc_string__escape__then_convert__n_amp__html( $booking_data_arr_value );
1748 }
1749
1750 // Add system keys to data fields arr, like ( 'booking_id', 'trash', 'sync_gid'... ---------------------
1751 $booking_data_arr = wpbc_add_system_booking_data_fields(
1752 $booking_data_arr,
1753 array_merge(
1754 get_object_vars( $booking->booking_db ),
1755 array( 'id' => $booking->id,
1756 'approved' => $booking->approved,
1757 'wpbc_custom_booking_form' => '' //FixIn: 9.4.3.12 //FixIn: 9.4.4.9
1758 )
1759 )
1760 , array(
1761 'booking_id',
1762 'trash',
1763 'sync_gid',
1764 'is_new',
1765 'status',
1766 'sort_date',
1767 'modification_date',
1768 'creation_date',
1769 'hash',
1770 'booking_type',
1771 'remark',
1772 'cost',
1773 'pay_status',
1774 'pay_request',
1775 'id',
1776 'approved',
1777 'booking_options',
1778 'wpbc_custom_booking_form', //FixIn: 9.4.3.12
1779 )
1780 );
1781
1782 // Set dates and times in correct format ---------------------------------------------------------------
1783 $booking_data_arr = wpbc_parse_booking_data_fields_formats( $booking_data_arr );
1784
1785 // Get SHORT / WIDE Dates showing data -----------------------------------------------------------------
1786 $short_dates_content = wpbc_get_formated_dates__short( $booking->short_dates, (boolean) $booking->approved, $booking->short_dates_child_id, $resources_arr );
1787 $wide_dates_content = wpbc_get_formated_dates__wide( $booking->dates, (boolean) $booking->approved, $booking->child_id, $resources_arr );
1788
1789 //------------------------------------------------------------------------------------------------------
1790 // Payment Status
1791 //------------------------------------------------------------------------------------------------------
1792 if ( 1 ) {
1793 $booking_data_arr['is_paid'] = 0; // 0 | 1
1794 $booking_data_arr['pay_print_status'] = ''; // "Unknown" | "Stripe_v3:OK"
1795 // $booking_data_arr['pay_status'] // "165458416073.66" | "Stripe_v3:OK" | "PayPal:Ok"
1796
1797 if ( class_exists( 'wpdev_bk_biz_s' ) ) {
1798
1799 if ( wpbc_is_payment_status_ok( trim( $booking_data_arr['pay_status'] ) ) ) {
1800 $booking_data_arr['is_paid'] = 1;
1801 }
1802
1803 $payment_status_titles = get_payment_status_titles();
1804
1805 $current_payment_status_title = array_search( $booking_data_arr['pay_status'], $payment_status_titles );
1806
1807 if ( $current_payment_status_title === false ) {
1808 $current_payment_status_title = $booking_data_arr['pay_status'];
1809 }
1810
1811 if ( $booking_data_arr['is_paid'] ) {
1812 $booking_data_arr['pay_print_status'] = $booking_data_arr['pay_status'];
1813 if ( $current_payment_status_title == 'Completed' ) {
1814 $booking_data_arr['pay_print_status'] = $current_payment_status_title;
1815 }
1816 } else if ( ( is_numeric( $booking_data_arr['pay_status'] ) ) || ( $booking_data_arr['pay_status'] == '' ) ) {
1817 $booking_data_arr['pay_print_status'] = __( 'Unknown', 'booking' );
1818 } else {
1819 $booking_data_arr['pay_print_status'] = $current_payment_status_title;
1820 }
1821 ////////////////////////////////////////////////////////////////////////////////////////////////////
1822
1823 if ( $booking_data_arr['pay_print_status'] == 'Completed' ) { //FixIn: 8.4.7.11
1824 $booking_data_arr['pay_print_status'] = __( 'Completed', 'booking' );
1825 }
1826 $real_payment_css = empty( $booking_data_arr['pay_status'] ) ? $current_payment_status_title : $booking_data_arr['pay_status']; //FixIn: 8.7.7.13
1827 $css_payment_label = 'wpbc_label_payment_status_' . wpbc_check_payment_status( $real_payment_css ); // 'success' | 'pending' | 'unknown' | 'error' //FixIn: 8.7.7.13
1828
1829 if ( $booking_data_arr['is_paid'] ) {
1830 $css_payment_label .= ' wpbc_label_payment_status_success';
1831 }
1832 $payment_label_template = '<span class="wpbc_label wpbc_label_payment_status ' . $css_payment_label . '">'
1833 . '<span style="font-size:07px;padding: 0 1em 0 0;line-height: 2em;">'
1834 . __( 'Payment', 'booking' )
1835 . '</span> '
1836 . '<span>'
1837 . $booking_data_arr['pay_print_status']
1838 . '</span> '
1839 . '</span>';
1840 } else {
1841 $payment_label_template = '';
1842 }
1843 }
1844
1845 //------------------------------------------------------------------------------------------------------
1846 // Currency
1847 //------------------------------------------------------------------------------------------------------
1848 if ( class_exists( 'wpdev_bk_biz_s' ) ) {
1849 $currency_symbol = wpbc_get_currency_symbol_for_user( $resource_id );
1850 $booking_data_arr['currency_symbol'] = $currency_symbol;
1851 }
1852
1853 //------------------------------------------------------------------------------------------------------
1854 // Add some fields to [ 'parsed_fields' ]
1855 //------------------------------------------------------------------------------------------------------
1856 $booking_data_arr['resource_title'] = $resource_title;
1857 $booking_data_arr['resource_id'] = $resource_id;
1858 $booking_data_arr['resource_owner_user'] = $resource_owner_user;
1859 //$booking_data_arr['short_dates_content'] = $short_dates_content;
1860 //$booking_data_arr['wide_dates_content'] = $wide_dates_content;
1861
1862 //------------------------------------------------------------------------------------------------------
1863 // Form Show - "Content of booking fields data" form
1864 //------------------------------------------------------------------------------------------------------
1865
1866 $custom_booking_form_name = ( ! empty( $booking_data_arr['wpbc_custom_booking_form'] ) ) ? $booking_data_arr['wpbc_custom_booking_form'] : ''; //FixIn: 9.4.3.12
1867 $form_show_template = wpbc_get_content_booking_form_show( $resource_id , $custom_booking_form_name ); // <strong>First Name</strong>:<span class="fieldvalue">[name]</span>&nbsp;&nbsp; ...
1868 $parsed_form_show = wpbc_get_parsed_content_booking_form_show( $booking_data_arr, $form_show_template ); // <strong>First Name</strong>:<span class="fieldvalue">John</span>&nbsp;&nbsp; ...
1869
1870 //------------------------------------------------------------------------------------------------------
1871 // Google Calendar link
1872 //------------------------------------------------------------------------------------------------------
1873 $booking_data_arr['google_calendar_link'] = wpbc_booking_do_action__get_google_calendar_link( array(
1874 'form_data' => $booking_data_arr,
1875 'form_show' => $parsed_form_show, //strip_tags( $parsed_form_show ),
1876 'dates_short' => $booking->short_dates
1877 ) );
1878
1879 // =====================================================================================================
1880 $bookings_arr[ $booking_id ]->parsed_fields = $booking_data_arr;
1881
1882
1883 // =====================================================================================================
1884 $bookings_arr[ $booking_id ]->templates = array(
1885 'form_show' => $parsed_form_show,
1886 'form_show_nohtml' => strip_tags( $parsed_form_show ),
1887 'short_dates_content' => $short_dates_content,
1888 'wide_dates_content' => $wide_dates_content,
1889 'payment_label_template' => $payment_label_template
1890 );
1891 }
1892
1893 return $bookings_arr;
1894 }
1895
1896
1897 /**
1898 * Get SHORT Dates showing data
1899 *
1900 * @param array $short_dates_arr - Array of dates
1901 * @param bool $is_approved - is dates approved or not
1902 * @param type $dates_type_id_arr
1903 * @param type $booking_resources_arr
1904 *
1905 * @return string
1906 */
1907 function wpbc_get_formated_dates__short( $short_dates_arr, $is_approved = false, $dates_type_id_arr = array() , $booking_resources_arr = array() ){
1908
1909 $css_class_approved = ( $is_approved ) ? 'approved' : '';
1910 $short_dates_content = '';
1911 $date_number = 0;
1912
1913 foreach ( $short_dates_arr as $dt ) {
1914
1915 if ( $dt == '-' ) {
1916
1917 $short_dates_content .= '<span class="date_tire"> - </span>';
1918
1919 } elseif ( $dt == ',' ) {
1920
1921 $short_dates_content .= '<span class="date_tire">, </span>';
1922
1923 } else {
1924
1925 list( $formatted_date, $formatted_time ) = wpbc_get_date_in_correct_format( $dt );
1926
1927 $short_dates_content .= '<a href="javascript:void(0)" onclick="javascript:wpbc_ajx_click_on_dates_toggle(this);" class="wpbc_label wpbc_label_booking_dates ' . $css_class_approved . '"><span>';
1928 $short_dates_content .= $formatted_date;
1929 $short_dates_content .= '<sup class="field-booking-time">' . $formatted_time . '</sup>';
1930
1931 // BL
1932 if (
1933 ( class_exists( 'wpdev_bk_biz_l' ) )
1934 && ( ! empty( $dates_type_id_arr[ $date_number ] ) )
1935 && ( isset( $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ] ) )
1936 ){
1937
1938 $resource_title = ( isset( $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ] ) )
1939 ? $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ]['title']
1940 : __( 'Resource not exist', 'booking' );
1941 $resource_title = wpbc_lang( $resource_title );
1942
1943 if ( strlen( $resource_title ) > 19 ) {
1944 $resource_title = substr( $resource_title, 0, 13 ) . '...' . substr( $resource_title, - 3 );
1945 }
1946
1947 $short_dates_content .= '<sup class="field-booking-time date_from_dif_type"> ' . $resource_title . '</sup>';
1948 }
1949
1950 $short_dates_content .= '</span></a>';
1951 }
1952 $date_number++;
1953 }
1954
1955 return $short_dates_content;
1956 }
1957
1958
1959 /**
1960 * Get Wide Dates showing data
1961 *
1962 * @param array $dates_arr - array of dates in sql string format
1963 * @param bool $is_approved - is approved (true) or pending (false)
1964 * @param array $dates_type_id_arr - array of $date->type_id from the dates SQL object
1965 * @param type $booking_resources_arr - array of booking resources objects
1966 *
1967 * @return string
1968 */
1969 function wpbc_get_formated_dates__wide( $dates_arr, $is_approved = false, $dates_type_id_arr = array(), $booking_resources_arr = array() ){
1970
1971 $wide_dates_arr = array();
1972
1973 $css_class_approved = ( $is_approved ) ? 'approved' : '';
1974
1975 foreach ( $dates_arr as $date_number => $sql_booking_date ) {
1976
1977 list( $formatted_date, $formatted_time ) = wpbc_get_date_in_correct_format( $sql_booking_date );
1978
1979 $wide_date = '<a href="javascript:void(0)" onclick="javascript:wpbc_ajx_click_on_dates_toggle(this);" class="wpbc_label wpbc_label_booking_dates ' . $css_class_approved . '"><span>';
1980 $wide_date .= $formatted_date;
1981 $wide_date .= '<sup class="field-booking-time">' . $formatted_time . '</sup>';
1982
1983 // BL
1984 if (
1985 ( class_exists( 'wpdev_bk_biz_l' ) )
1986 && ( '' != $dates_type_id_arr[ $date_number ] )
1987 && ( isset( $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ] ) )
1988 ){
1989 $resource_title = ( isset( $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ] ) )
1990 ? $booking_resources_arr[ $dates_type_id_arr[ $date_number ] ]['title']
1991 : __( 'Resource not exist', 'booking' );
1992 $resource_title = wpbc_lang( $resource_title );
1993
1994 if ( strlen( $resource_title ) > 19 ) {
1995 $resource_title = substr( $resource_title, 0, 13 ) . '...' . substr( $resource_title, - 3 );
1996 }
1997 $wide_date .= '<sup class="field-booking-time date_from_dif_type"> ' . $resource_title . '</sup>';
1998 }
1999
2000 $wide_date .= '</span></a>';
2001
2002 $wide_dates_arr[] = $wide_date;
2003 }
2004
2005 $wide_dates_content = implode( '<span class="date_tire">, </span>' , $wide_dates_arr );
2006
2007 return $wide_dates_content;
2008 }
2009
2010
2011 /**
2012 * Parse booking "data field" and get array
2013 *
2014 * @param string $data - "id^2~booking_type^Standard~status^Approved~dates^2019-09-18 00:00:00 - 2019-09-20 00:00:00 , 2019-09-18 00:00:00 (Standard-1) - 2019-09-20 00:00:00 (Standard-1)~modification_date^2019-09-05 10:50:04~cost^100.00~pay_status^156766972609.7~selected_short_timedates_hint^09/18/2019 - 09/20/2019~nights_number_hint^2~cost_hint^$75.00~name^John test~secondname^Smith~email^user@beta.com~phone^~visitors^4~children^~details^~term_and_condition^~user^Support A~wpbc_other_action^~rangetime^~other_email^~visitorsselector^~visitors_fee^~visitors_fee_hint^~trash^~remark^Approved by:John Smith (user@beta.com) [2019-09-11 09:30] Declined by:John Smith (user@beta.com) [2019-09-11 09:30]"
2015 * @param array $attr - array( 'r_separator' => '~', 'f_separator' => '^' )
2016 *
2017 * @return array - Array (
2018 [id] => 2
2019 [booking_type] => Standard
2020 [status] => Approved
2021 ... )
2022 */
2023 function wpbc_parse_booking_data_fields( $data , $attr = array() ){
2024
2025 $defaults = array(
2026 'r_separator' => '~' ,
2027 'f_separator' => '^' ,
2028 'resource_id' => '1' ,
2029 'system_fields' => array(
2030 'booking_id',
2031 'trash',
2032 //'sync_gid',
2033 'is_new',
2034 'status',
2035 'sort_date',
2036 'modification_date',
2037 'hash',
2038 'booking_type',
2039 'remark',
2040 'cost',
2041 'pay_status',
2042 'pay_request',
2043 'id',
2044 'approved'
2045 )
2046 );
2047 $attr = wp_parse_args( $attr, $defaults );
2048
2049 $data_arr = array();
2050
2051 if ( ! empty( $data ) ) {
2052
2053 $data = explode( $attr['r_separator'] , $data ); // ~
2054
2055 foreach ( $data as $data_rows ) {
2056
2057 $data_rows = explode( $attr['f_separator'] , $data_rows ); // ^
2058
2059 $data_field = array( 'type' => $data_rows[0], 'name' => $data_rows[1], 'value' => $data_rows[2] );
2060
2061 // remove checkboxes suffix []
2062 $data_field['name'] = str_replace( '[]', '', $data_field['name'] );
2063
2064 // remove booking resource ID suffix
2065 $data_field['name'] = substr( $data_field['name'], 0, - 1 * strlen( strval( $attr['resource_id'] ) ) );
2066
2067 // System fields - adjust fields with same name
2068 if ( in_array( $data_field['name'], $attr['system_fields'] ) ) {
2069 $data_field['name'] .= '_data';
2070 }
2071
2072 //Checkboxes
2073 if ( ( 'checkbox' == $data_field['type'] ) && ( empty( $data_field['value'] ) ) ) {
2074 continue;
2075 }
2076 if ( ( 'checkbox' == $data_field['type'] ) && ( ! is_array( $data_field['value'] ) ) ) {
2077 $data_field['value'] = str_replace( array( 'true', 'false' ), array(
2078 strtolower( __( 'Yes', 'booking' ) ),
2079 strtolower( __( 'No', 'booking' ) )
2080 ), $data_field['value'] );
2081 }
2082
2083 // Several items with same name - multi checkboxes
2084 if ( ! isset( $data_arr[ $data_field['name'] ] ) ) {
2085 $data_arr[ $data_field['name'] ] = $data_field['value'];
2086 } else {
2087 if ( is_array( $data_arr[ $data_field['name'] ] ) ) {
2088 $data_arr[ $data_field['name'] ][] = $data_field['value'];
2089 } else {
2090 $data_arr[ $data_field['name'] ] = array( $data_arr[ $data_field['name'] ], $data_field['value'] );
2091 }
2092 }
2093
2094 }
2095 }
2096
2097 foreach ( $data_arr as $key => $value ) {
2098
2099 // Multi checkboxes: [9] => checkbox^my_multi_checkbx4[]^1 [10] => checkbox^my_multi_checkbx4[]^ [11] => checkbox^my_multi_checkbx4[]^3
2100 if ( is_array( $value ) ) {
2101 $value = implode( ',', $value );
2102 $data_arr[$key] = $value;
2103 }
2104 }
2105
2106 return $data_arr;
2107 }
2108
2109
2110 /**
2111 * Add system fields to Booking fields array and get final array of fields
2112 * @param array $booking_data_arr Original array of form fields from DB
2113 Array (
2114 [selected_short_dates_hint] => March 5, 2023
2115 [days_number_hint] => 1
2116 [cost_hint] => &amp;#36;95
2117 [rangetime] => 10:00 AM - 12:00 PM
2118 [name] => test
2119 [email] => test@wpbookingcalendar.com
2120 ...
2121 )
2122
2123 * @param array $booking_system_arr array of system fields from DB
2124 Array ( [booking_id] => 188
2125 [trash] => 0
2126 ...
2127 [cost] => 95.00
2128 [pay_status] => 165036591118.88
2129 [pay_request] => 0
2130 [id] => 188
2131 [approved] => 0
2132 )
2133
2134 * @param array $system_keys_arr system fields keys that need to be added, like this:
2135 Array ( booking_id, trash, sync_gid, is_new, status
2136 , sort_date, modification_date, hash, booking_type
2137 , remark, cost, pay_status, pay_request, id, approved )
2138
2139 * @return array array of form fields with system fields
2140 */
2141 function wpbc_add_system_booking_data_fields( $booking_data_arr, $booking_system_arr, $system_keys_arr ){
2142
2143 foreach ( $system_keys_arr as $key ) {
2144
2145 if (
2146 ( ! isset( $booking_system_arr[ $key ] ) )
2147 || ( is_null( $booking_system_arr[ $key ] ) )
2148 ) {
2149 $booking_system_arr[ $key ] = ''; //Some fields, like remark or booking_options can be null, so we define them as ''
2150 }
2151
2152 if ( isset( $booking_system_arr[ $key ] ) ) {
2153
2154 if ( ! isset( $booking_data_arr[ $key ] ) ) {
2155 $booking_data_arr[ $key ] = maybe_unserialize( $booking_system_arr[ $key ] );
2156 } else {
2157 $booking_data_arr[ $key . '_system' ] = maybe_unserialize( $booking_system_arr[ $key ] );
2158 }
2159 }
2160
2161 }
2162 return $booking_data_arr;
2163 }
2164
2165
2166 /**
2167 * Parse booking form fields format - for example: time fields in specific format
2168 * @param $data_arr
2169 *
2170 * @return array
2171 */
2172 function wpbc_parse_booking_data_fields_formats( $data_arr ){
2173
2174 foreach ( $data_arr as $key => $value ) {
2175
2176 if ( 'rangetime' == $key ) {
2177 $data_arr[$key] = wpbc_time_slot_in_format( $value );
2178 $data_arr[ $key . '_in_24_hour' ] = $value; //FixIn: 10.0.0.52
2179 }
2180
2181 if ( in_array( $key, array( 'starttime', 'endtime' ) ) ) {
2182 $data_arr[$key] = wpbc_time_in_format( $value );
2183 $data_arr[ $key . '_in_24_hour' ] = $value; //FixIn: 10.0.0.52
2184 }
2185
2186 if ( in_array( $key, array( 'modification_date', 'creation_date' ) ) ) {
2187
2188 //$data_arr[$key] = wpbc_get_dates_comma_string_localized( $value );
2189
2190
2191 // This date $values in GMT date/Time format. So we need to switch to WordPress locale with TIME sum of actual GMT date/time value + shift of timezone from WordPress.
2192 $is_add_wp_timezone = true;
2193 $its_for_default_datetime_format = '';
2194 $data_arr[ $key ] = wpbc_datetime_localized( $value, $its_for_default_datetime_format, $is_add_wp_timezone );
2195 $data_arr[ $key . '_in_24_hour' ] = $value; //FixIn: 10.0.0.52
2196 }
2197
2198 if ( ( 'hash' === $key ) && ( empty( $value ) ) ) { //FixIn: 9.2.3.4 //FixIn: 9.4.3.10
2199 // Update booking Hash if it was empty
2200 wpbc_hash__update_booking_hash( $data_arr['id'], $data_arr['booking_type'] );
2201 // Get new booking hash
2202 $hash__arr = wpbc_hash__get_booking_hash__resource_id( $data_arr['id'] );
2203 if ( ! empty( $hash__arr ) ) {
2204 $data_arr[ $key ] = $hash__arr[0];
2205 }
2206 }
2207 }
2208 return $data_arr;
2209 }
2210
2211
2212 /**
2213 * Get pure "Content of booking fields data" with shortcodes inside.
2214 * it can depend on specific booking resource in Business Medium version or User in MultiUser version
2215 *
2216 * @param int $resource_id
2217 * @param sting $custom_booking_form_name - use this custom booking form, if possible
2218 *
2219 * @return string
2220 */
2221 function wpbc_get_content_booking_form_show( $resource_id , $custom_booking_form_name = '' ){ //FixIn: 9.4.3.12
2222
2223 if ( $resource_id == -1 ) {
2224 if ( function_exists('get__default_type') )
2225 $resource_id = get__default_type();
2226 else
2227 $resource_id = 1;
2228 }
2229
2230 if ( ! class_exists('wpdev_bk_personal') ) {
2231
2232 $booking_form_show = wpbc_get_free_booking_show_form();
2233
2234 } else {
2235
2236 $booking_form_show = get_bk_option( 'booking_form_show' );
2237 $booking_form_show = wpbc_bf__replace_custom_html_shortcodes( $booking_form_show );
2238
2239 if ( class_exists('wpdev_bk_biz_m') ) {
2240
2241 if ( ! empty( $custom_booking_form_name ) ) { //FixIn: 9.4.3.12
2242
2243 $booking_form_show = apply_bk_filter( 'wpdev_get_booking_form_content', $booking_form_show, $custom_booking_form_name );
2244 $my_booking_form_name = $custom_booking_form_name;
2245 } else {
2246 // BM :: Get default Custom Form of Resource
2247 $my_booking_form_name = apply_bk_filter( 'wpbc_get_default_custom_form', 'standard', $resource_id );
2248 if ( ( $my_booking_form_name != 'standard' ) && ( ! empty( $my_booking_form_name ) ) ) {
2249 $booking_form_show = apply_bk_filter( 'wpdev_get_booking_form_content', $booking_form_show, $my_booking_form_name );
2250 }
2251 }
2252 //MU :: if resource of "Regular User" - then GET STANDARD user form ( if ( get_bk_option( 'booking_is_custom_forms_for_regular_users' ) !== 'On' ) )
2253 $booking_form_show = apply_bk_filter( 'wpbc_multiuser_get_booking_form_show_of_regular_user', $booking_form_show, $resource_id, $my_booking_form_name ); //FixIn: 8.1.3.19
2254
2255 }
2256 }
2257
2258 // Language
2259 $booking_form_show = wpbc_lang( $booking_form_show );
2260
2261 $search = array( "'(<br[ ]?[/]?>)+'si", "'(<[/]?p[ ]?>)+'si" );
2262 $replace = array( "&nbsp;&nbsp;", " &nbsp; ", " &nbsp; " );
2263 $booking_form_show = preg_replace( $search, $replace, $booking_form_show );
2264
2265 return $booking_form_show;
2266 }
2267
2268
2269 /**
2270 * Get parsed "Content of booking fields data" - Shortcodes replaced by Values
2271 *
2272 * @param array $booking_data_arr
2273 * @param string $booking_form_show
2274 *
2275 * @return string
2276 */
2277 function wpbc_get_parsed_content_booking_form_show( $booking_data_arr, $booking_form_show ){
2278
2279 foreach ( $booking_data_arr as $key_param => $value_param ) {
2280 if ( ( gettype ( $value_param ) != 'array' )
2281 && ( gettype ( $value_param ) != 'object' )
2282 ) {
2283 $booking_form_show = str_replace( '['. $key_param .']', $value_param ,$booking_form_show);
2284 }
2285 }
2286 // Remove all shortcodes, which have not replaced early.
2287 $booking_form_show = preg_replace ('/[\s]{0,}\[[a-zA-Z0-9.,-_]{0,}\][\s]{0,}/', '', $booking_form_show);
2288
2289 $booking_form_show = str_replace( "&amp;", '&', $booking_form_show );
2290
2291 return $booking_form_show;
2292 }
2293
2294 // </editor-fold>
2295
2296
2297 // <editor-fold defaultstate="collapsed" desc=" F u n c t i o n s f o r C l e a n i ng s " >
2298
2299 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2300 // Escaping
2301 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2302
2303 /**
2304 * Escaping for inline JavaScript.
2305 * Based on WordPress esc_js()
2306 * plus removing \n in the code
2307 * and back converting escaped single quotes from \' to '
2308 *
2309 * This function has to be used in the code marked with double quote symbols, not single.
2310 * Example: onclick="javascript:<?php echo wpbc_esc_js( $item_params['action'] ); ?>"
2311 *
2312 * @param $text
2313 *
2314 * @return string
2315 */
2316 function wpbc_esc_js( $text ) {
2317
2318 $text = str_replace( "\n", '', $text ); // removing \n in the code
2319
2320 //$safe_text = esc_js( $text ); // js
2321
2322 // This code get from esc_js() and modified for correct working with (' symbols) in translations
2323 $safe_text = wp_check_invalid_utf8( $text );
2324 $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
2325 $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "\'", stripslashes( $safe_text ) );
2326 $safe_text = str_replace( "\r", '', $safe_text );
2327 $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
2328
2329 $safe_text = str_replace( "\'", "'", $safe_text ); // back converting escaped single quotes from \' to '
2330 $safe_text = str_replace( "\\\'", "\'", $safe_text ); // back converting escaped single quotes from \' to '
2331
2332 return $safe_text;
2333 }
2334
2335
2336 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2337 // DB
2338 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2339
2340 /**
2341 * Check $value for injections. Basically can be used in Requests params checking.
2342 *
2343 * @param type $value
2344 *
2345 * @return type
2346 */
2347 function wpbc_esc_clean_parameter( $value ) {
2348
2349 $value = preg_replace( '/<[^>]*>/', '', $value ); // clean any tags
2350 $value = str_replace( '<', ' ', $value );
2351 $value = str_replace( '>', ' ', $value );
2352 $value = strip_tags( $value ); // Strip HTML and PHP tags from a string
2353
2354 // Clean SQL injection
2355 $value = esc_sql( $value );
2356 $value = esc_textarea( $value ); //FixIn: 7.1.1.2
2357
2358 return $value;
2359 }
2360
2361
2362 function wpbc_esc_sql_like( $value_trimmed ) {
2363
2364 // check here https://code.tutsplus.com/articles/data-sanitization-and-validation-with-wordpress--wp-25536
2365
2366 global $wpdb;
2367 if ( method_exists( $wpdb ,'esc_like' ) )
2368 return $wpdb->esc_like( $value_trimmed ); // Its require minimum WP 4.0.0
2369 else
2370 return addcslashes( $value_trimmed, '_%\\' ); // Direct implementation from $wpdb->esc_like(
2371 }
2372
2373
2374 /**
2375 * Clean user string for using in SQL LIKE statement - append to LIKE sql
2376 *
2377 * @param string $value - to clean
2378 *
2379 * @return string - escaped
2380 * Exmaple:
2381 * $search_escaped_like_title = wpbc_esc_clean_like_string_for_append_in_sql_for_db( $input_var );
2382 *
2383 * $where_sql = " WHERE title LIKE ". $search_escaped_like_title ." ";
2384 */
2385 function wpbc_esc_clean_like_string_for_append_in_sql_for_db( $value ) {
2386
2387 // check here https://code.tutsplus.com/articles/data-sanitization-and-validation-with-wordpress--wp-25536
2388
2389 global $wpdb;
2390
2391 $value_trimmed = trim( stripslashes( $value ) );
2392
2393 $wild = '%';
2394 $like = $wild . wpbc_esc_sql_like( $value_trimmed ) . $wild;
2395 $sql = $wpdb->prepare( "'%s'", $like );
2396
2397 return $sql;
2398
2399
2400 /* Help:
2401 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
2402 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
2403 *
2404 * Example Prepared Statement:
2405 *
2406 * $wild = '%';
2407 * $find = 'only 43% of planets';
2408 * $like = $wild . wpbc_esc_sql_like( $find ) . $wild;
2409 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
2410 *
2411 * Example Escape Chain:
2412 *
2413 * $sql = esc_sql( wpbc_esc_sql_like( $input ) );
2414 */
2415
2416 }
2417
2418
2419 /**
2420 * Clean string for using in SQL LIKE requests inside single quotes: WHERE title LIKE '%". $escaped_search_title ."%'
2421 * Replaced _ to \_ % to \% \ to \\
2422 *
2423 * @param string $value - to clean
2424 *
2425 * @return string - escaped
2426 * Exmaple:
2427 * $search_escaped_like_title = wpbc_esc_clean_like_string_for_db( $input_var );
2428 *
2429 * $where_sql = " WHERE title LIKE '%". $search_escaped_like_title ."%' ";
2430 *
2431 * Important! Use SINGLE quotes after in SQL query: LIKE '%".$data."%'
2432 */
2433 function wpbc_esc_clean_like_string_for_db( $value ){
2434
2435 // check here https://code.tutsplus.com/articles/data-sanitization-and-validation-with-wordpress--wp-25536
2436
2437 global $wpdb;
2438
2439 $value_trimmed = trim( stripslashes( $value ) );
2440
2441 $value_trimmed = wpbc_esc_sql_like( $value_trimmed );
2442
2443 $value = trim( $wpdb->prepare( "'%s'", $value_trimmed ) , "'" );
2444
2445 return $value;
2446
2447 /* Help:
2448 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
2449 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
2450 *
2451 * Example Prepared Statement:
2452 *
2453 * $wild = '%';
2454 * $find = 'only 43% of planets';
2455 * $like = $wild . wpbc_esc_sql_like( $find ) . $wild;
2456 * $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_content LIKE '%s'", $like );
2457 *
2458 * Example Escape Chain:
2459 *
2460 * $sql = esc_sql( wpbc_esc_sql_like( $input ) );
2461 */
2462 }
2463
2464
2465 // </editor-fold>
2466
2467
2468 // <editor-fold defaultstate="collapsed" desc=" D B " >
2469
2470 /***
2471 * Get list of DB of this plugin
2472 * @return array
2473 */
2474 function wpbc_get_db_names(){
2475
2476 global $wpdb;
2477
2478 $db_names = array(
2479 'bookings' => $wpdb->prefix . 'booking',
2480 'dates' => $wpdb->prefix . 'bookingdates',
2481 'resources' => $wpdb->prefix . 'bookingtypes',
2482 'resources_meta' => $wpdb->prefix . 'booking_types_meta',
2483 'seasons' => $wpdb->prefix . 'booking_seasons',
2484 'coupons' => $wpdb->prefix . 'booking_coupons'
2485 );
2486
2487 return $db_names;
2488 }
2489
2490 // </editor-fold>
2491
2492
2493 // <editor-fold defaultstate="collapsed" desc=" E r r o r s H a n d l i n g " >
2494
2495 /**
2496 * PHP Fatal Error! Display expanded error info at Booking Listing page
2497 *
2498 * @param $message
2499 * @param $error
2500 *
2501 * @return mixed|string
2502 */
2503 function wpbc_php_error_message( $message, $error ) {
2504
2505 // Check to show this error, only from Booking Listing page, checking secure parameters from wpbc_ajx_booking_listing in ..{Booking Calendar Folder}/includes/page-bookings/_src/ajx_booking_listing.js
2506 if ( ! ( ! isset( $_POST['search_params'] ) || empty( $_POST['search_params'] ) ) ) {
2507 // Security ----------------------------------------------------------------------------------------------- // in Ajax Post: 'nonce': wpbc_ajx_booking_listing.get_secure_param( 'nonce' ),
2508 $action_name = 'wpbc_ajx_booking_listing_ajx' . '_wpbcnonce';
2509 $nonce_post_key = 'nonce';
2510 // $result_check = check_ajax_referer( $action_name, $nonce_post_key );
2511 if ( isset( $_REQUEST[ $nonce_post_key ] ) ) {
2512
2513 $nonce_request = $_REQUEST[ $nonce_post_key ];
2514
2515 $result_verify_nonce = wp_verify_nonce( $nonce_request, $action_name );
2516
2517 if ( false !== $result_verify_nonce ) {
2518 if ( ( ! empty( $error ) ) && ( ! empty( $error['message'] ) ) ) {
2519 $message .= '<br>' . $error['message'];
2520 }
2521 }
2522 }
2523 }
2524
2525 return $message;
2526 }
2527 add_filter( 'wp_php_error_message', 'wpbc_php_error_message', 2 , 10 );
2528
2529
2530 /**
2531 * Nonce Error! Error Handing for not passed nonce, in booking Listing page.
2532 * Probably nonce was expired (more than 24 hours)
2533 *
2534 * @param $action
2535 * @param $result - 1 Good ( 0-12 hours ago ) , 2 Good ( 12-24 hours ago ) , false - Error ( > 24 hours - nonce is invalid )
2536 */
2537 function wpbc_check_ajax_referer__for_booking_listing( $action, $result ){
2538 if (
2539 ( false === $result )
2540 && ( 'wpbc_ajx_booking_listing_ajx' . '_wpbcnonce' === $action )
2541 && ( wp_doing_ajax() )
2542 ) {
2543 die( '<strong>Error!</strong> Probably nonce for this page has been expired. Please <a href="javascript:void(0)" onclick="javascript:location.reload();">reload the page</a>.' );
2544 }
2545 }
2546 add_action( 'check_ajax_referer', 'wpbc_check_ajax_referer__for_booking_listing', 2, 10 );
2547 // </editor-fold>