PluginProbe
Booking Calendar / 10.11
Booking Calendar v10.11
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 10.11.3 10.11.4 All 201 releases
booking / core / admin / wpbc-sql.php

wpbc-sql.php in Booking Calendar 10.11, at core/admin/wpbc-sql.php

1,261 lines 66.7 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 * @package Booking Calendar
4 * @category Data Engine for Booking Listing / Calendar Overview pages
5 * @author wpdevelop
6 *
7 * @web-site https://wpbookingcalendar.com/
8 * @email info@wpbookingcalendar.com
9 *
10 * @modified 2015-12-08
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit, if accessed directly
14
15
16 /** Trick here to overload default REQUST parameters before page is loading */
17 function wpbc_define_listing_page_parameters( $page_tag ) {
18
19 // $page_tag - here can be all defined in plugin menu pages
20 // So we need to check activated page. By default its inside of $_GET['page'],
21
22 // Execute it only for Booking Listing & Timeline admin pages.
23 //if ( ( isset( $_GET[ 'page' ] ) ) && ( $_GET[ 'page' ] == 'wpbc' ) ) {
24
25 if ( wpbc_is_bookings_page() ) { // We are inside of this page. Menu item selected.
26
27 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
28 if ( ( isset( $_REQUEST['tab'] ) ) && ( 'vm_booking_listing' === $_REQUEST['tab'] ) ) { //FixIn: 9.2.0
29 return;
30 }
31
32 $booking_default_view_mode = wpbc_get_default_saved_view_mode_for_wpbc_page();
33 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
34 if ( ! isset( $_REQUEST['tab'] ) ) {
35 $_REQUEST['tab'] = $booking_default_view_mode; // Set to REQUEST
36 }
37
38 // Get saved filters set, (if its not set in request yet), like "tab" & "view_mode" and overload $_REQUEST
39 wpbc_set_default_saved_params_to_request_for_booking_listing( 'default' );
40 }
41 }
42 // We are set 9 to execute earlier than hook in WPBC_Admin_Menus
43 add_action('wpbc_define_nav_tabs', 'wpbc_define_listing_page_parameters', 1 ); // This Hook fire in the class WPBC_Admin_Menus for showing page content of specific menu
44
45 ////////////////////////////////////////////////////////////////////////////////
46 // R e q u e s t S u p p o r t
47 ////////////////////////////////////////////////////////////////////////////////
48
49 /**
50 * Get here default View mode saved in a General Booking Settings page or From REQUEST view_mode
51 *
52 * @return string = 'vm_calendar' | 'vm_booking_listing'
53 */
54 function wpbc_get_default_saved_view_mode_for_wpbc_page() {
55
56 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
57 if ( ! isset( $_REQUEST['tab'] ) ) {
58 $booking_default_view_mode = get_bk_option( 'booking_listing_default_view_mode' );
59 } else {
60 $booking_default_view_mode = sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
61 }
62
63 // TODO:2023-05-20 remove it!!. 'vm_booking_listing';.
64 if ( ! in_array( $booking_default_view_mode, array( 'vm_calendar', 'vm_booking_listing' ), true ) ) {
65 $booking_default_view_mode = 'vm_booking_listing'; // FixIn: 9.2.1 // FixIn: 9.6.3.5.
66 }
67
68 $booking_default_view_mode = ( 'vm_listing' === $booking_default_view_mode ) ? 'vm_booking_listing' : $booking_default_view_mode; // FixIn: 9.6.3.5.
69
70 return $booking_default_view_mode; // 'vm_calendar' / 'vm_booking_listing'.
71 }
72
73
74 /**
75 * Get from SETTINGS (if its not set in request yet) the "tab" & "view_mode" and set to $_REQUEST
76 If we have the "saved" filter set so LOAD it and set to REQUEST, if REQUEST was not set previously
77 & skip "wh_booking_type" from the saved filter set
78 *
79 * @param string $filter_name - name of saved filter set. Currntly is using only one "Default"
80 */
81 function wpbc_set_default_saved_params_to_request_for_booking_listing( $filter_name ) {
82
83 // Exclude some parameters from the saved Default parameters - the values of these parameters are loading from General Booking Settings page or from the request.
84 $exclude_options_from_saved_params = array(
85 'tab', // Default.
86 'page', // From plugin.
87 'wh_booking_type', // Default.
88 'view_days_num', // Default.
89 'blank_field__this_field_only_for_formatting_buttons', // Skip this, this parameter for formating purpose in toolbar.
90 );
91
92 $wpdevbk_filter_params = array();
93
94 // Get here default View mode saved in a General Booking Settings page.
95 $booking_default_view_mode = wpbc_get_default_saved_view_mode_for_wpbc_page();
96 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
97 if ( ! isset( $_REQUEST['tab'] ) ) {
98 $wpdevbk_filter_params['tab'] = $booking_default_view_mode;
99 }
100 // 'vm_calendar' / 'vm_booking_listing' ;.
101 $_REQUEST['tab'] = $booking_default_view_mode;
102
103
104 // Get here default view_days_num.
105 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
106 if ( ! isset( $_REQUEST['view_days_num'] ) ) {
107 $booking_view_days_num = get_bk_option( 'booking_view_days_num' );
108 if ( false !== $booking_view_days_num ) {
109 $wpdevbk_filter_params['view_days_num'] = $booking_view_days_num; // '30'.
110 $_REQUEST['view_days_num'] = $booking_view_days_num;
111 } else {
112 $_REQUEST['view_days_num'] = '365';
113 }
114 }
115 }
116
117
118
119 /**
120 * Clean Request Parameters
121 *
122 */
123 function wpbc_check_request_paramters() { // FixIn: 6.2.1.4.
124
125 //debuge($_REQUEST);
126 $clean_params = array();
127
128 $clean_params['wh_booking_id'] = 'digit_or_csd';
129 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
130 if ( ( ! empty( $_REQUEST['wh_booking_type'] ) ) && ( 'lost' == $_REQUEST['wh_booking_type'] ) ) { // FixIn: 8.5.2.19.
131 $clean_params['wh_booking_type'] = 'checked_skip_it';
132 } else {
133 $clean_params['wh_booking_type'] = 'digit_or_csd';
134 }
135 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
136 if ( ( ! empty( $_REQUEST['booking_type'] ) ) && ( 'lost' == $_REQUEST['booking_type'] ) ) { // FixIn: 8.9.2.1.
137 $clean_params['booking_type'] = 'checked_skip_it';
138 } else {
139 $clean_params['booking_type'] = 'digit_or_csd';
140 }
141
142 $clean_params['wh_approved'] = 'digit_or_csd'; // '0' | '1' | ''
143
144 $clean_params['wh_booking_date'] = 'digit_or_date'; // number | date 2016-07-20
145 $clean_params['wh_booking_date2'] = 'digit_or_date'; // number | date 2016-07-20
146 $clean_params['wh_booking_datenext'] = 'd'; // '1' | '2' ....
147 $clean_params['wh_booking_dateprior'] = 'd'; // '1' | '2' ....
148 $clean_params['wh_booking_datefixeddates'] = 'digit_or_date'; // number | date 2016-07-20
149 $clean_params['wh_booking_date2fixeddates'] = 'digit_or_date'; // number | date 2016-07-20
150
151 $clean_params['wh_is_new'] = 'd'; // '1' | ''
152
153 $clean_params['wh_modification_date'] = 'digit_or_date'; // number | date 2016-07-20
154 $clean_params['wh_modification_date2'] = 'digit_or_date'; // number | date 2016-07-20
155 $clean_params['wh_modification_dateprior'] = 'd'; // '1' | '2' ....
156 $clean_params['wh_modification_datefixeddates'] = 'digit_or_date'; // number | date 2016-07-20
157 $clean_params['wh_modification_date2fixeddates']= 'digit_or_date'; // number | date 2016-07-20
158
159 $clean_params['wh_keyword'] = 's'; //string
160
161 $clean_params['wh_pay_statuscustom'] = 's'; //string !!! LIKE !!!
162 $clean_params['wh_pay_status'] = array('all', 'group_ok', 'group_unknown', 'group_pending', 'group_failed');
163
164 $clean_params['wh_cost'] = 'd'; // '1' | ''
165 $clean_params['wh_cost2'] = 'd'; // '1' | ''
166
167 $clean_params['or_sort'] = array('', 'sort_date', 'booking_type', 'cost', 'booking_id_asc', 'sort_date_asc', 'booking_type_asc', 'cost_asc');
168 $clean_params['wh_trash'] = array('0' , 'trash', 'any');
169
170
171 $clean_params['page_num'] = 'd'; // '' | '1' ... // does not exist in 6.2.1.4
172 $clean_params['page_items_count'] = 'd'; // '' | '1' ... // does not exist in 6.2.1.4
173 $clean_params['view_days_num'] = 'd'; // '' | '1' ... // does not exist in 6.2.1.4
174
175 // FixIn: 8.9.2.1.
176 $clean_params['scroll_start_date'] = 'digit_or_date';
177 $clean_params['scroll_day'] = 'd';
178 $clean_params['scroll_month'] = 'd';
179 $clean_params['limit_hours'] = 'digit_or_csd';
180 $clean_params['only_booked_resources'] = 'd';
181
182
183 foreach ( $clean_params as $request_key => $clean_type ) {
184
185 // elements only listed in array::
186 if ( is_array( $clean_type ) ) { // check only values from the list in this array
187
188 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
189 if ( ( isset( $_REQUEST[ $request_key ] ) ) && ( ! in_array( $_REQUEST[ $request_key ], $clean_type ) ) )
190 $clean_type = 's';
191 else
192 $clean_type = 'checked_skip_it';
193 }
194
195 switch ( $clean_type ) {
196
197 case 'checked_skip_it':
198
199 break;
200
201 case 'digit_or_date': // digit or comma separated digit
202 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
203 if ( isset( $_REQUEST[ $request_key ] ) ) {
204 $_REQUEST[ $request_key ] = wpbc_clean_digit_or_date( $_REQUEST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
205 }
206 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
207 if ( isset( $_GET[ $request_key ] ) ) {
208 $_GET[ $request_key ] = wpbc_clean_digit_or_date( $_GET[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
209 }
210 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
211 if ( isset( $_POST[ $request_key ] ) ) {
212 $_POST[ $request_key ] = wpbc_clean_digit_or_date( $_POST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
213 }
214
215 break;
216
217 case 'digit_or_csd': // digit or comma separated digit
218 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
219 if ( isset( $_REQUEST[ $request_key ] ) ) {
220 $_REQUEST[ $request_key ] = wpbc_clean_digit_or_csd( $_REQUEST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
221 }
222 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
223 if ( isset( $_GET[ $request_key ] ) ) {
224 $_GET[ $request_key ] = wpbc_clean_digit_or_csd( $_GET[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
225 }
226 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
227 if ( isset( $_POST[ $request_key ] ) ) {
228 $_POST[ $request_key ] = wpbc_clean_digit_or_csd( $_POST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
229 }
230
231 break;
232
233 case 's': // string
234 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
235 if ( isset( $_REQUEST[ $request_key ] ) ) {
236 $_REQUEST[ $request_key ] = wpbc_clean_like_string_for_db( $_REQUEST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
237 }
238 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
239 if ( isset( $_GET[ $request_key ] ) ) {
240 $_GET[ $request_key ] = wpbc_clean_like_string_for_db( $_GET[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
241 }
242 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
243 if ( isset( $_POST[ $request_key ] ) ) {
244 $_POST[ $request_key ] = wpbc_clean_like_string_for_db( $_POST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
245 }
246
247 break;
248
249 case 'd': // digit
250 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
251 if ( ( isset( $_REQUEST[ $request_key ] ) ) && ( $_REQUEST[ $request_key ] !== '' ) ) {
252 $_REQUEST[ $request_key ] = intval( $_REQUEST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
253 }
254 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
255 if ( ( isset( $_GET[ $request_key ] ) ) && ( $_GET[ $request_key ] !== '' ) ) {
256 $_GET[ $request_key ] = intval( $_GET[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
257 }
258 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
259 if ( ( isset( $_POST[ $request_key ] ) ) && ( $_POST[ $request_key ] !== '' ) ) {
260 $_POST[ $request_key ] = intval( $_POST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
261 }
262
263 break;
264
265 default:
266 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
267 if ( isset( $_REQUEST[ $request_key ] ) ) {
268 $_REQUEST[ $request_key ] = intval( $_REQUEST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
269 }
270 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
271 if ( isset( $_GET[ $request_key ] ) ) {
272 $_GET[ $request_key ] = intval( $_GET[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
273 }
274 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
275 if ( isset( $_POST[ $request_key ] ) ) {
276 $_POST[ $request_key ] = intval( $_POST[ $request_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
277 }
278 break;
279 }
280
281
282 }
283
284 //debuge($_REQUEST);
285 }
286
287
288 // FixIn: 9.6.3.5.
289
290 /**
291 * Get array of cleaned (limited number) paramas from REQUEST
292 *
293 * @return array
294 */
295 function wpbc_get_clean_paramas_from_request_for_timeline() {
296
297
298 // Reset
299 $start_year = gmdate("Y"); //2012
300 $start_month = gmdate("m"); //09
301 $start_day = 1;//date("d");//1; //31
302 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
303 if (! empty($_REQUEST['scroll_start_date'])) { // scroll_start_date=2013-07-01
304 $scroll_start_date = explode( '-', $_REQUEST['scroll_start_date'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
305
306 $start_year = $scroll_start_date[0]; //2012
307 $start_month = $scroll_start_date[1]; //09
308 $start_day = $scroll_start_date[2]; //date("d");//1; //31
309 }
310
311 $scroll_day = 0;
312 $scroll_month = 0;
313
314 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
315 if (isset($_REQUEST['view_days_num'])) $view_days_num = sanitize_text_field( wp_unslash( $_REQUEST['view_days_num'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
316 else $view_days_num = get_bk_option( 'booking_view_days_num');
317
318 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
319 if ((isset($_REQUEST['wh_booking_type'])) && ( strpos($_REQUEST['wh_booking_type'], ',') !== false ) )
320 $is_show_resources_matrix = true;
321 else $is_show_resources_matrix = false;
322
323 if ($is_show_resources_matrix) {
324
325 switch ($view_days_num) {
326
327 case '1':
328 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
329 if (empty($_REQUEST['scroll_start_date'])) $start_day = gmdate("d");
330 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
331 if (isset($_REQUEST['scroll_day'])) $scroll_day = sanitize_text_field( wp_unslash( $_REQUEST['scroll_day'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
332
333 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + intval( $scroll_day ) ), intval( $start_year ) );
334 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-11-29';
335
336 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + intval( $scroll_day ) ), intval( $start_year ) );
337 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2013-12-3';
338 break;
339
340 case '7':
341 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
342 if (empty($_REQUEST['scroll_start_date'])) $start_day = gmdate("d");
343 $start_week_day_num = gmdate("w");
344 $start_day_weeek = esc_js(get_bk_option( 'booking_start_day_weeek' )); //[0]:Sun .. [6]:Sut
345 if ($start_week_day_num != $start_day_weeek) {
346 for ($d_inc = 1; $d_inc < 8; $d_inc++) { // Just get week back
347 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) - intval( $d_inc ) ), intval( $start_year ) );
348 $start_week_day_num = gmdate("w", $real_date);
349 if ($start_week_day_num == $start_day_weeek) {
350 $start_day = gmdate("d", $real_date);
351 $start_year = gmdate("Y", $real_date);
352 $start_month = gmdate("m", $real_date);
353 $d_inc=9;
354 }
355 }
356 }
357
358 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
359 if (isset($_REQUEST['scroll_day'])) $scroll_day = sanitize_text_field( wp_unslash( $_REQUEST['scroll_day'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
360
361 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + intval( $scroll_day ) ), intval( $start_year ) );
362 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
363
364 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + 7 + intval( $scroll_day ) ), intval( $start_year ) );
365 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2012-12-7';
366 break;
367
368 case '30':
369 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
370 if (isset($_REQUEST['scroll_month'])) $scroll_month = sanitize_text_field( wp_unslash( $_REQUEST['scroll_month'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
371
372 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + intval( $scroll_month ) ), intval( $start_day ), intval( $start_year ) );
373 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
374
375 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + 1 + intval( $scroll_month ) ), ( intval( $start_day ) - 1 ), intval( $start_year ) );
376 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2012-12-31';
377 break;
378
379 case '60':
380 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
381 if (isset($_REQUEST['scroll_month'])) $scroll_month = sanitize_text_field( wp_unslash( $_REQUEST['scroll_month'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
382
383 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + intval( $scroll_month ) ), intval( $start_day ), intval( $start_year ) );
384 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
385
386 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + 2 + intval( $scroll_month ) ), ( intval( $start_day ) - 1 ), intval( $start_year ) );
387 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2013-02-31';
388 break;
389
390 ////////////////////////////////////////////////////////////////////////////////
391 default: // 30 - default
392 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
393 if (isset($_REQUEST['scroll_month'])) $scroll_month = sanitize_text_field( wp_unslash( $_REQUEST['scroll_month'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
394
395 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + intval( $scroll_month ) ), intval( $start_day ), intval( $start_year ) );
396 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
397
398 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + 1 + intval( $scroll_month ) ), ( intval( $start_day ) - 1 ), intval( $start_year ) );
399 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2012-12-31';
400 break;
401 }
402
403 } else { // Single resource
404
405 switch ($view_days_num) {
406 case '90':
407
408 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
409 if (empty($_REQUEST['scroll_start_date'])) $start_day = gmdate("d");
410 $start_week_day_num = gmdate("w");
411 $start_day_weeek = esc_js(get_bk_option( 'booking_start_day_weeek' )); //[0]:Sun .. [6]:Sut
412
413 if ($start_week_day_num != $start_day_weeek) {
414 for ($d_inc = 1; $d_inc < 8; $d_inc++) { // Just get week back
415 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) - intval( $d_inc ) ), intval( $start_year ) );
416 $start_week_day_num = gmdate("w", $real_date);
417 if ($start_week_day_num == $start_day_weeek) {
418 $start_day = gmdate("d", $real_date);
419 $start_year = gmdate("Y", $real_date);
420 $start_month = gmdate("m", $real_date);
421 $d_inc=9;
422 //break;
423 }
424 }
425 }
426
427 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
428 if (isset($_REQUEST['scroll_day'])) $scroll_day = sanitize_text_field( wp_unslash( $_REQUEST['scroll_day'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
429
430 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + intval( $scroll_day ) ), intval( $start_year ) );
431 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
432
433 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + 7 * 12 + 7 + intval( $scroll_day ) ), intval( $start_year ) );
434 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2013-12-31';
435 break;
436
437 case '30':
438 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
439 if (empty($_REQUEST['scroll_start_date'])) $start_day = gmdate("d");
440
441 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
442 if (isset($_REQUEST['scroll_day'])) $scroll_day = sanitize_text_field( wp_unslash( $_REQUEST['scroll_day'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
443
444 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + intval( $scroll_day ) ), intval( $start_year ) );
445 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
446
447 $real_date = mktime( 0, 0, 0, intval( $start_month ), ( intval( $start_day ) + 31 + intval( $scroll_day ) ), intval( $start_year ) );
448 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2013-12-31';
449 break;
450
451 default: // 365
452
453 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
454 if (isset($_REQUEST['scroll_month'])) $scroll_month = sanitize_text_field( wp_unslash( $_REQUEST['scroll_month'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
455 else $scroll_month = 0;
456
457 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + intval( $scroll_month ) ), intval( $start_day ), intval( $start_year ) );
458 $wh_booking_date = gmdate("Y-m-d", $real_date); // '2012-12-01';
459
460 $real_date = mktime( 0, 0, 0, ( intval( $start_month ) + intval( $scroll_month ) + 13 ), ( intval( $start_day ) - 1 ), intval( $start_year ) );
461 $wh_booking_date2 = gmdate("Y-m-d", $real_date); // '2013-12-31';
462
463 break;
464 }
465 }
466
467
468 $or_sort = '' ;
469
470 $args = array(
471 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
472 'wh_booking_type' => ( isset( $_REQUEST['wh_booking_type'] ) ) ? $_REQUEST['wh_booking_type'] : '',
473 'wh_approved' => '', // Any.
474 'wh_booking_id' => '', // Any.
475 'wh_is_new' => '', // (isset($_REQUEST['wh_is_new']))?$_REQUEST['wh_is_new']:'', ?.
476 'wh_pay_status' => 'all', // (isset($_REQUEST['wh_pay_status']))?$_REQUEST['wh_pay_status']:'', // ?.
477 'wh_keyword' => '', // (isset($_REQUEST['wh_keyword']))?$_REQUEST['wh_keyword']:'', // ?.
478 'wh_booking_date' => $wh_booking_date,
479 'wh_booking_date2' => $wh_booking_date2,
480 'wh_modification_date' => '3', // (isset($_REQUEST['wh_modification_date']))?$_REQUEST['wh_modification_date']:'', // ?
481 'wh_modification_date2' => '', // (isset($_REQUEST['wh_modification_date2']))?$_REQUEST['wh_modification_date2']:'', // ?
482 'wh_cost' => '', // (isset($_REQUEST['wh_cost']))?$_REQUEST['wh_cost']:'', // ?
483 'wh_cost2' => '', // (isset($_REQUEST['wh_cost2']))?$_REQUEST['wh_cost2']:'', // ?
484 'or_sort' => $or_sort,
485 'page_num' => '1',
486 'page_items_count' => '100000',
487 );
488
489 return $args;
490 }
491
492
493 /** Set initial $_REQUEST['view_days_num'] depend on from selected booking resources */
494 function wpbc_set_request_params_for_timeline() {
495
496 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
497 if ( ( isset( $_REQUEST['wh_booking_type'] ) ) && ( strpos( $_REQUEST['wh_booking_type'], ',' ) !== false ) ) {
498 $is_show_resources_matrix = true;
499 } else {
500 $is_show_resources_matrix = false;
501 }
502
503 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
504 if ( ! isset( $_REQUEST['view_days_num'] ) ) {
505 $_REQUEST['view_days_num'] = get_bk_option( 'booking_view_days_num' );
506 }
507
508 // We do not have the Year (365) and (90) view modes in the Matrix mode so we are set to the closest variant. And the same backward.
509 if ( ( $is_show_resources_matrix ) ) { // Switching from the Single to Matrix mode.
510 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
511 if ( isset( $_REQUEST['view_days_num'] ) && ( $_REQUEST['view_days_num'] == '365' ) ) {
512 $_REQUEST['view_days_num'] = 60;
513 }
514 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
515 if ( isset( $_REQUEST['view_days_num'] ) && ( $_REQUEST['view_days_num'] == '90' ) ) {
516 $_REQUEST['view_days_num'] = 7;
517 }
518 } else { // Switching from the Matrix to Single mode.
519 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
520 if ( isset( $_REQUEST['view_days_num'] ) && ( $_REQUEST['view_days_num'] == '60' ) ) {
521 $_REQUEST['view_days_num'] = 365;
522 }
523 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
524 if ( ( $_REQUEST['view_days_num'] == '7' ) || ( $_REQUEST['view_days_num'] == '1' ) ) {
525 $_REQUEST['view_days_num'] = 30;
526 }
527 }
528 }
529
530
531 /** Define default booking resource to $_GET request and check if user can be here in MU version. */
532 function wpbc_set_default_resource_to__get() {
533
534 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
535 if ( isset( $_GET['booking_type'] ) ) {
536
537 // Check if User can be here in MultiUser version for this booking resource (is this user owner of this resource or not).
538 if ( class_exists( 'wpdev_bk_multiuser' ) ) {
539
540 $default_booking_resource = sanitize_text_field( wp_unslash( $_GET['booking_type'] ) ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
541
542 // Check if this MU user activated or superadmin, otherwise show warning.
543 if ( ! wpbc_is_mu_user_can_be_here( 'activated_user' ) ) {
544 return false;
545 }
546
547 // Check if this MU user owner of this resource or superadmin, otherwise show warning.
548 if ( ! wpbc_is_mu_user_can_be_here( 'resource_owner', $default_booking_resource ) ) {
549 return false;
550 }
551 }
552 } else { // Set default booking resource to $_GET.
553
554 // Get ID of default booking resource, or return false (in case if user have no access to this resource and show some warnings).
555 $default_booking_resource = wpbc_get_default_resource();
556
557 if ( empty( $default_booking_resource ) ) {
558
559 return false; // User can not be here, Warnings have shown.
560
561 } else {
562
563 $_GET['booking_type'] = $default_booking_resource;
564 }
565
566 // Check if this resource parent and has some additional childs, if yes then assign $_ GET['parent_res'] = 1 only in case, if its for loading default booking resource.
567 make_bk_action( 'check_if_bk_res_parent_with_childs_set_parent_res', $default_booking_resource );
568 }
569
570 return true;
571 }
572
573
574 /**
575 * Get ID of default booking resource, or return false (in case if user have no access to this resource and show some warnings).
576 *
577 * @return boolean|int
578 */
579 function wpbc_get_default_resource() {
580
581 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
582 return 1;
583 } // Free, i.e., default 1.
584
585 // Get assigned default booking resource from General Booking Settings page.
586 $default_booking_resource = get_bk_option( 'booking_default_booking_resource' ); // If empty, i.e., "" - its all resources - no default booking resource.
587
588 if ( empty( $default_booking_resource ) ) { // We do not have default resource.
589
590 // Get first resource in a list.
591 // If its MU, then for superadmin get first resource in a list OR if user DO NOT have resources get FIRST resource in LIST FROM ALL resources.
592 $default_booking_resource = get__default_type();
593 }
594
595 // MU.
596 if ( class_exists( 'wpdev_bk_multiuser' ) ) {
597
598 // Check if this MU user activated or superadmin, otherwise show warning.
599 if ( ! wpbc_is_mu_user_can_be_here( 'activated_user' ) ) {
600 return false;
601 }
602
603 // Check if this MU user owner of this resource or superadmin, otherwise show warning.
604 if ( ! wpbc_is_mu_user_can_be_here( 'resource_owner', $default_booking_resource ) ) {
605 return false;
606 }
607 }
608
609 return $default_booking_resource;
610 }
611
612
613 /** Get list of all booking resources.
614 *
615 * @return array (
616 [1] => Array (
617 [title] => Default [parent resource]
618 [attr] => Array (
619 [class] => wpbc_parent_resource
620 )
621 )
622 [5] => Array (
623 [title] => Default-1
624 [attr] => Array (
625 [class] => wpbc_child_resource
626 )
627 )
628 [6] => Array ( ...
629 */
630 function wpbc_get_all_booking_resources_list(){ //FixIn: 8.1.3.5.2
631
632 if ( ! class_exists( 'wpdev_bk_personal' ) ) {
633 return array();
634 }
635
636 $resources_cache = wpbc_br_cache(); // Get booking resources from cache
637
638 $resource_objects = $resources_cache->get_resources();
639 // $resource_objects = $resources_cache->get_single_parent_resources();
640
641 //$resource_options = $params['resources'];
642 $resource_options = array(); // FixIn: 8.2.1.12.
643
644 foreach ( $resource_objects as $br ) {
645
646 $br_option = array();
647 $br_option['title'] = wpbc_lang( $br['title'] );
648
649 if ( ( isset( $br['parent'] ) ) && ( $br['parent'] == 0 ) && ( isset( $br['count'] ) ) && ( $br['count'] > 1 ) ) {
650 $br_option['title'] .= ' [' . __( 'parent resource', 'booking' ) . ']';
651 }
652
653 $br_option['attr'] = array();
654 $br_option['attr']['class'] = 'wpbc_single_resource';
655 if ( isset( $br['parent'] ) ) {
656 if ( $br['parent'] == 0 ) {
657 if ( ( isset( $br['count'] ) ) && ( $br['count'] > 1 ) ) {
658 $br_option['attr']['class'] = 'wpbc_parent_resource';
659 }
660 } else {
661 $br_option['attr']['class'] = 'wpbc_child_resource';
662 }
663 }
664
665 $sufix = '';
666
667 $resource_options[ $br['id'] . $sufix ] = $br_option;
668
669 if ( $resource_options[ $br['id'] ]['attr']['class'] === 'wpbc_child_resource' ) {
670 $resource_options[ $br['id'] ]['title'] = ' &nbsp;&nbsp;&nbsp; ' . $resource_options[ $br['id'] ]['title'];
671 }
672 }
673
674 return $resource_options;
675 }
676
677 ////////////////////////////////////////////////////////////////////////////////
678 // S u p p o r t
679 ////////////////////////////////////////////////////////////////////////////////
680
681 /**
682 * Check if current WP user can be here
683 * Checking if the user activated, and booking resource belong to specific user
684 *
685 * @param string $check_condition 'activated_user' | 'resource_owner' | 'only_super_admin'
686 * @param string $booking_resource - Optional. ID of booking resource for 'resource_owner' condition
687 * @return boolean
688 */
689 function wpbc_is_mu_user_can_be_here( $check_condition, $booking_resource = '' ) {
690
691 $is_can = true;
692
693 if ( $check_condition == 'activated_user' )
694 $is_can = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'check_for_active_users' );
695
696 if ( $check_condition == 'only_super_admin' )
697 $is_can = apply_bk_filter( 'multiuser_is_user_can_be_here', true, 'only_super_admin' );
698
699 if ( $check_condition == 'resource_owner' )
700 $is_can = apply_bk_filter( 'multiuser_is_user_can_be_here', true, $booking_resource );
701
702 return $is_can;
703
704 }
705
706
707 ////////////////////////////////////////////////////////////////////////////////
708 // E n g i n e B o o k i n g L i s t i n g
709 ////////////////////////////////////////////////////////////////////////////////
710
711 /**
712 * E n g i n e for getting Bookings objects for Booking Listing pages
713 *
714 * @param array $args - Request paramters from filters
715 * @return array(
716 'bookings' => $bookings
717 , 'resources' => $booking_types
718 , 'bookings_count' => $bookings_count
719 , 'page_num' => $page_num
720 , 'count_per_page' => $page_items_count
721 );
722 */
723 function wpbc_get_bookings_objects( $args ){
724
725 global $wpdb;
726
727 // Initial variables //////////////////////////////////////////////////////
728
729 $sql_boking_listing = wpbc_get_sql_for_booking_listing( $args );
730 $sql_start_count = $sql_boking_listing[ 'sql_start_count' ];
731 $sql_start_select = $sql_boking_listing[ 'sql_start_select' ];
732 $sql = $sql_boking_listing[ 'sql' ];
733 $sql_where = $sql_boking_listing[ 'where' ];
734 $sql_order = $sql_boking_listing[ 'order' ];
735 $sql_limit = $sql_boking_listing[ 'limit' ];
736
737 $num_per_page_check = 10;
738 $defaults = array(
739 'wh_booking_type' => ''
740 , 'wh_approved' => ''
741 , 'wh_booking_id' => ''
742 , 'wh_is_new' => ''
743 , 'wh_pay_status' => ''
744 , 'wh_keyword' => ''
745 , 'wh_booking_date' => ''
746 , 'wh_booking_date2' => ''
747 , 'wh_modification_date' => ''
748 , 'wh_modification_date2' => ''
749 , 'wh_cost' => ''
750 , 'wh_cost2' => ''
751 , 'or_sort' => ''
752 , 'page_num' => '1'
753 , 'wh_trash' => '' // FixIn: 6.1.1.10.
754 , 'page_items_count' => ( ( empty( $num_per_page_check ) ) ? '10' : $num_per_page_check )
755 );
756
757 $r = wp_parse_args( $args, $defaults );
758
759 $r = apply_filters( 'wpbc_request_params_for_get_booking_obj', $r ); // FixIn: 7.0.1.41.
760
761 extract( $r, EXTR_SKIP );
762
763 $page_start = ( $page_num - 1 ) * $page_items_count;
764
765 //debuge( $sql_start_select . $sql . $sql_where . $sql_order . $sql_limit );
766
767 // -----------------------------------------------------------------------------------------------------------------
768 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
769 $bookings_res = $wpdb->get_results( $sql_start_select . $sql . $sql_where . $sql_order . $sql_limit ); // Get Bookings.
770
771 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
772 $bookings_count = $wpdb->get_results( $sql_start_count . $sql . $sql_where ); // Get Number of bookings.
773
774 $bookings_count = ( ( count( $bookings_count ) > 0 ) ? $bookings_count[0]->count : 0 );
775
776 $booking_types = apply_bk_filter( 'wpdebk_get_keyed_all_bk_resources', array() ); // Get Resources
777
778
779 // -----------------------------------------------------------------------------------------------------------------
780 $booking_id_list = array(); // ID list of ALL bookings
781 $bookings = array();
782 $short_days = array();
783 $short_days_type_id = array();
784
785 foreach ( $bookings_res as $booking ) {
786
787 if ( ! in_array( $booking->booking_id, $booking_id_list ) )
788 $booking_id_list[] = $booking->booking_id;
789
790 $bookings[$booking->booking_id] = $booking;
791 $bookings[$booking->booking_id]->dates = array();
792 $bookings[$booking->booking_id]->dates_short = array();
793
794 $bk_list_type = (isset( $booking->booking_type )) ? $booking->booking_type : '1';
795
796 if ( ( isset( $booking->sync_gid ) ) && (!empty( $booking->sync_gid )) ) {
797 $booking->form .= "~text^sync_gid{$booking->booking_type}^{$booking->sync_gid}";
798 }
799
800 $cont = wpbc__legacy__get_form_content_arr( $booking->form
801 , $bk_list_type
802 , ''
803 , array(
804 'booking_id' => $booking->booking_id
805 , 'resource_title' => (isset( $booking_types[$booking->booking_type] )) ? $booking_types[$booking->booking_type] : ''
806 )
807 );
808
809 $search = array( "'(<br[ ]?[/]?>)+'si", "'(<[/]?p[ ]?>)+'si"/*, "'(<[/]?div[ ]?>)+'si"*/ ); // FixIn: 8.8.1.6.
810 $replace = array( "&nbsp;&nbsp;", " &nbsp; ", " &nbsp; " );
811 $cont['content'] = preg_replace( $search, $replace, $cont['content'] );
812
813 $bookings[$booking->booking_id]->form_show = $cont['content'];
814 unset( $cont['content'] );
815 $bookings[$booking->booking_id]->form_data = $cont;
816 }
817 $booking_id_list = implode( ",", $booking_id_list );
818 $booking_id_list = wpbc_clean_like_string_for_db( $booking_id_list );
819
820 // -----------
821
822 if ( ! empty( $booking_id_list ) ) { // Get Dates for all our Bookings.
823 $sql = "SELECT *
824 FROM {$wpdb->prefix}bookingdates as dt
825 WHERE dt.booking_id in ( {$booking_id_list} ) ";
826
827 if ( class_exists( 'wpdev_bk_biz_l' ) ) {
828 $sql .= ' ORDER BY booking_id, type_id, booking_date ';
829 } else {
830 $sql .= ' ORDER BY booking_id, booking_date ';
831 }
832
833 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
834 $booking_dates = $wpdb->get_results( $sql );
835
836 } else {
837 $booking_dates = array();
838 }
839
840 // -------------
841
842 $last_booking_id = '';
843
844 foreach ( $booking_dates as $date ) { // Add Dates to Bookings array
845
846 $bookings[ $date->booking_id ]->dates[] = $date;
847
848 if ( $date->booking_id != $last_booking_id ) {
849 if ( !empty( $last_booking_id ) ) {
850 if ( $last_show_day != $dte ) {
851 $short_days[] = $dte;
852 $short_days_type_id[] = $last_day_id;
853 }
854
855 $bookings[$last_booking_id]->dates_short = $short_days;
856 $bookings[$last_booking_id]->dates_short_id = $short_days_type_id;
857 }
858 $last_day = '';
859 $last_day_id = '';
860 $last_show_day = '';
861 $short_days = array();
862 $short_days_type_id = array();
863 }
864
865 $last_booking_id = $date->booking_id;
866 $dte = $date->booking_date;
867
868 if ( empty( $last_day ) ) { // First date
869 $short_days[] = $dte;
870 $short_days_type_id[] = (isset( $date->type_id )) ? $date->type_id : '';
871 $last_show_day = $dte;
872 } else { // All other days
873 //if ( wpbc_is_next_day( $dte, $last_day ) ) {
874 $next_day_if__check_in__then__check_out = true;
875 if ( wpbc_is_less_than_next_day( $dte, $last_day, $next_day_if__check_in__then__check_out ) ) {
876 if ( $last_show_day != '-' ) {
877 $short_days[] = '-';
878 $short_days_type_id[] = '';
879 }
880 $last_show_day = '-';
881 } else {
882 if ( $last_show_day != $last_day ) {
883 $short_days[] = $last_day;
884 $short_days_type_id[] = $last_day_id;
885 }
886 $short_days[] = ',';
887 $short_days_type_id[] = '';
888 $short_days[] = $dte;
889 $short_days_type_id[] = (isset( $date->type_id )) ? $date->type_id : '';
890 $last_show_day = $dte;
891 }
892 }
893 $last_day = $dte;
894 $last_day_id = (isset( $date->type_id )) ? $date->type_id : '';
895 }
896
897 if ( isset( $dte ) )
898 if ( $last_show_day != $dte ) {
899 $short_days[] = $dte;
900 $short_days_type_id[] = (isset( $date->type_id )) ? $date->type_id : '';
901 }
902 if ( isset( $bookings[$last_booking_id] ) ) {
903 $bookings[$last_booking_id]->dates_short = $short_days;
904 $bookings[$last_booking_id]->dates_short_id = $short_days_type_id;
905 }
906
907 //debuge( 'Before filtering:', gmdate('Y-m-d', time() +86400 ), $_REQUEST, $bookings ) ;
908
909 // -----------------------------------------------------------------------------------------------------------------
910 // Filter some bookings
911 // -----------------------------------------------------------------------------------------------------------------
912 // Showing only bookings that starting or ending during "Today" ( Today check in/out )
913 if ( (isset( $args['wh_booking_date'] )) && ($args['wh_booking_date'] == '9') ) {
914
915 $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
916
917 foreach ( $bookings as $bc_id => $bc_value ) {
918
919 $check_in_date = $bc_value->dates_short[0];
920 $check_in_date = explode( ' ', $check_in_date );
921 $check_in_date = $check_in_date[0]; // 2014-02-25
922
923 if ( count( $bc_value->dates_short ) == 1 )
924 $check_out_date = $bc_value->dates_short[0];
925 else
926 $check_out_date = $bc_value->dates_short[2];
927 $check_out_date = explode( ' ', $check_out_date );
928 $check_out_date = $check_out_date[0]; // 2014-02-25
929
930 if ( ( $today_mysql_format != $check_in_date ) && ( $today_mysql_format != $check_out_date ) ) {
931 unset( $bookings[$bc_id] );
932 $bookings_count--;
933 }
934 }
935 }
936
937
938 // If we selected the Dates as "Check In - Today/Tommorow", then show only the bookings, where check in date is Today
939 if ( (isset( $args['wh_booking_date'] )) && ($args['wh_booking_date'] == '7') ) {
940 //$today_mysql_format = gmdate('Y-m-d');
941 //$today_mysql_format = gmdate('Y-m-d', time() +86400 ); // 1 Day = 24*60*60 = 86400
942 $today_mysql_format = date_i18n( 'Y-m-d', time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) + DAY_IN_SECONDS ); // Tommorow day with gmt offset
943 foreach ( $bookings as $bc_id => $bc_value ) {
944 $check_in_date = $bc_value->dates_short[0];
945 $check_in_date = explode( ' ', $check_in_date );
946 $check_in_date = $check_in_date[0]; // 2014-02-25
947 if ( $today_mysql_format != $check_in_date ) {
948 unset( $bookings[$bc_id] );
949 $bookings_count--;
950 }
951 }
952 }
953
954
955 // If we selected the Dates as "Check Out - Tomorow", then show only the bookings, where check out date is Tomorrow .
956 if ( ( isset( $args['wh_booking_date'] ) ) && ( 8 === intval( $args['wh_booking_date'] ) ) ) {
957
958 $tomorrow_mysql_format = date_i18n( 'Y-m-d', time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) + DAY_IN_SECONDS ); // Tommorow day with gmt offset.
959 foreach ( $bookings as $bc_id => $bc_value ) {
960 if ( count( $bc_value->dates_short ) === 1 ) {
961 $check_out_date = $bc_value->dates_short[0];
962 } else {
963 $check_out_date = $bc_value->dates_short[2];
964 }
965 $check_out_date = explode( ' ', $check_out_date );
966 $check_out_date = $check_out_date[0]; // 2014-02-25
967 if ( $tomorrow_mysql_format !== $check_out_date ) {
968 unset( $bookings[ $bc_id ] );
969 --$bookings_count;
970 }
971 }
972 }
973
974
975 $return_booking_structure = array(
976 'bookings' => $bookings,
977 'resources' => $booking_types,
978 'bookings_count' => $bookings_count,
979 'page_num' => $page_num,
980 'count_per_page' => $page_items_count,
981 );
982
983
984 return $return_booking_structure;
985 }
986
987
988 ////////////////////////////////////////////////////////////////////////////////
989 // S Q L
990 ////////////////////////////////////////////////////////////////////////////////
991
992 /**
993 * Get S Q L for bookings at Booking Listing page
994 *
995 * @param array $args - Request paramters from filters
996 * @return array - Get array with SQL for getting bookings
997 */
998 function wpbc_get_sql_for_booking_listing( $args ){
999
1000 global $wpdb;
1001
1002 $num_per_page_check = 10;
1003
1004 $defaults = array(
1005 'wh_booking_type' => ''
1006 , 'wh_approved' => ''
1007 , 'wh_booking_id' => ''
1008 , 'wh_is_new' => ''
1009 , 'wh_pay_status' => ''
1010 , 'wh_keyword' => ''
1011 , 'wh_booking_date' => ''
1012 , 'wh_booking_date2' => ''
1013 , 'wh_modification_date' => ''
1014 , 'wh_modification_date2' => ''
1015 , 'wh_cost' => ''
1016 , 'wh_cost2' => ''
1017 , 'or_sort' => ''
1018 , 'page_num' => '1'
1019 , 'wh_trash' => '' // FixIn: 6.1.1.10.
1020 , 'wh_sync_gid' => '' // '' | 'imported' | 'plugin' // FixIn: 8.8.3.19.
1021 , 'page_items_count' => $num_per_page_check
1022 );
1023 $r = wp_parse_args( $args, $defaults );
1024 extract( $r, EXTR_SKIP );
1025
1026 $page_start = ( $page_num - 1 ) * $page_items_count;
1027
1028
1029 $posible_sorts = array( 'booking_id_asc', 'sort_date', 'sort_date_asc', 'booking_type', 'booking_type_asc', 'cost', 'cost_asc' );
1030
1031 if ( ($or_sort == '') || ($or_sort == 'id') || ( !in_array( $or_sort, $posible_sorts ) ) )
1032 $or_sort = 'booking_id';
1033
1034
1035 ////////////////////////////////////////////////////////////////////////
1036 // S Q L
1037 ////////////////////////////////////////////////////////////////////////
1038 $sql_start_select = " SELECT * ";
1039 $sql_start_count = " SELECT COUNT(*) as count";
1040 $sql = " FROM {$wpdb->prefix}booking as bk"; // GET ONLY ROWS OF THE B o o k i n g s - So we can limit the requests
1041
1042 //debuge($wh_trash);
1043 //FixIn: 6.1.1.10 - check also below usage of {$trash_bookings}
1044 $trash_bookings = " AND bk.trash = 0 ";
1045 if ( isset( $wh_trash ) ) {
1046
1047 if ( $wh_trash == "trash" ) $trash_bookings = " AND bk.trash = 1 ";
1048 else if ( $wh_trash == "any" ) $trash_bookings = '';
1049 }
1050 //debuge($trash_bookings);
1051
1052 if ( empty( $wh_booking_id ) ) {
1053
1054 $sql_where = " WHERE " . // Date (single) connection (Its required for the correct Pages in SQL: LIMIT Keyword)
1055 " EXISTS (
1056 SELECT *
1057 FROM {$wpdb->prefix}bookingdates as dt
1058 WHERE bk.booking_id = dt.booking_id ";
1059
1060 if ( 'lost' == $wh_booking_type ) { // FixIn: 8.5.2.19.
1061
1062 $sql_where.= " AND bk.booking_type NOT IN ( SELECT DISTINCT booking_type_id FROM {$wpdb->prefix}bookingtypes ) ";
1063 $sql_where.= " ) ";
1064 $wh_booking_type = '';
1065
1066 } else {
1067
1068 if ( $wh_approved !== '' )
1069 $sql_where.= " AND approved = $wh_approved "; // Approved or Pending
1070
1071 $sql_where .= wpbc_set_sql_where_for_dates( $wh_booking_date, $wh_booking_date2 );
1072
1073 $sql_where.= " ) ";
1074
1075 $sql_where .= " {$trash_bookings} "; // FixIn: 6.1.1.10.
1076
1077 // FixIn: 8.8.3.19.
1078 if ( 'imported' === $wh_sync_gid ) {
1079 $sql_where .= " AND bk.sync_gid != '' ";
1080 }
1081 if ( 'plugin' === $wh_sync_gid ) {
1082 $sql_where .= " AND bk.sync_gid = '' ";
1083 }
1084
1085 if ( $wh_is_new !== '' )
1086 $sql_where .= " AND bk.is_new = " . $wh_is_new . " ";
1087
1088 $sql_where .= apply_bk_filter( 'get_bklist_sql_keyword', '', $wh_keyword ); // P
1089
1090 $sql_where .= wpbc_set_sql_where_for_modification_date( $wh_modification_date, $wh_modification_date2 );
1091
1092 $sql_where .= apply_bk_filter( 'get_bklist_sql_paystatus', '', $wh_pay_status ); // BS
1093 $sql_where .= apply_bk_filter( 'get_bklist_sql_cost', '', $wh_cost, $wh_cost2 ); // BS
1094
1095 $sql_where .= apply_bk_filter( 'get_bklist_sql_resources', '', $wh_booking_type, $wh_approved, $wh_booking_date, $wh_booking_date2 ); // P || BL
1096 }
1097
1098 } else {
1099
1100 // FixIn: 8.7.7.10.
1101 if ( strpos( $wh_booking_id, '<' ) !== false ) {
1102 $wh_booking_id = str_replace( '<', '', $wh_booking_id );
1103 $wh_booking_id = intval( $wh_booking_id );
1104 $sql_where = " WHERE bk.booking_id < " . $wh_booking_id . " ";
1105 }
1106
1107 else if ( strpos( $wh_booking_id, '>' ) !== false ) {
1108 $wh_booking_id = str_replace( '>', '', $wh_booking_id );
1109 $wh_booking_id = intval( $wh_booking_id );
1110 $sql_where = " WHERE bk.booking_id > " . $wh_booking_id . " ";
1111
1112 }
1113
1114 else if ( strpos( $wh_booking_id, ',' ) !== false ) {
1115 $sql_where = " WHERE bk.booking_id IN (" . $wh_booking_id . ") ";
1116
1117 } else {
1118 $sql_where = " WHERE bk.booking_id = " . $wh_booking_id . " ";
1119 }
1120
1121 // Check if searching booking is belonging to specific user in Booking Calendar MultiUser version
1122 $sql_where = apply_bk_filter('update_where_sql_for_getting_bookings_in_multiuser', $sql_where );
1123
1124 }
1125
1126 if ( strpos( $or_sort, '_asc' ) !== false ) { // Order
1127 $or_sort = str_replace( '_asc', '', $or_sort );
1128 $sql_order = " ORDER BY " . $or_sort . " ASC ";
1129 } else
1130 $sql_order = " ORDER BY " . $or_sort . " DESC ";
1131
1132
1133 $sql_limit = $wpdb->prepare( " LIMIT %d, %d ", $page_start, $page_items_count );
1134
1135 $return_res = array(
1136 'sql_start_count' => $sql_start_count
1137 , 'sql_start_select' => $sql_start_select
1138 , 'sql' => $sql
1139 , 'where' => $sql_where
1140 , 'order' => $sql_order
1141 , 'limit' => $sql_limit
1142 );
1143
1144 //debuge($return_res);
1145 return $return_res;
1146 }
1147
1148
1149 ////////////////////////////////////////////////////////////////////////////////
1150 // S Q L W H E R E
1151 ////////////////////////////////////////////////////////////////////////////////
1152
1153 /**
1154 * Get SQL W H E R E conditions for D a t e s of bookings
1155 *
1156 * @param string $wh_booking_date - Parameter from Booking Listing request (usually its number)
1157 * @param string $wh_booking_date2 - Parameter from Booking Listing request (usually its number)
1158 * @param string $pref - Optional. Prefix for table.
1159 * @return string - WHERE conditions for SQL
1160 */
1161 function wpbc_set_sql_where_for_dates( $wh_booking_date, $wh_booking_date2, $pref = 'dt.' ) {
1162
1163 $sql_where= '';
1164 if ($pref == 'dt.') { $and_pre = ' AND '; $and_suf = ''; }
1165 else { $and_pre = ''; $and_suf = ' AND '; }
1166
1167 // Actual
1168 if ( ( ( $wh_booking_date === '' ) && ( $wh_booking_date2 === '' ) ) || ($wh_booking_date === '0') ) {
1169 $sql_where = $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; // FixIn: 8.5.2.14.
1170
1171 } else if ($wh_booking_date === '1') { // Today // FixIn: 7.1.2.8.
1172 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ;
1173 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; // FixIn: 8.4.7.21.
1174
1175
1176 } else if ($wh_booking_date === '2') { // Previous
1177 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; // FixIn: 8.5.2.16.
1178
1179 } else if ($wh_booking_date === '3') { // All
1180 $sql_where = '';
1181
1182 } else if ($wh_booking_date === '4') { // Next
1183 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL ". $wh_booking_date2 . " DAY ) ) ".$and_suf ;
1184 // $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL 1 DAY ) ) ".$and_suf ;
1185 $sql_where .= $and_pre."( ".$pref."booking_date > ( CURDATE() ) ) ".$and_suf ; // FixIn: 8.0.1.1.
1186
1187 } else if ($wh_booking_date === '5') { // Prior
1188 $wh_booking_date2 = str_replace('-', '', $wh_booking_date2);
1189 $sql_where = $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL ". $wh_booking_date2 . " DAY ) ) ".$and_suf ;
1190 $sql_where .= $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1191
1192 } else if ($wh_booking_date === '7') { // Check In date - Today/Tomorrow
1193 // $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ;
1194 // $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() ) ) ".$and_suf ;
1195 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '1 23:59:59' DAY_SECOND ) ) ".$and_suf ;
1196 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1197
1198 } else if ($wh_booking_date === '8') { // Check Out date - Tomorrow
1199 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL '1 23:59:59' DAY_SECOND ) ) ".$and_suf ;
1200 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1201
1202 } else if ($wh_booking_date === '9') { // Today check in/out
1203 $sql_where = $and_pre."( ".$pref."booking_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1204 $sql_where .= $and_pre."( ".$pref."booking_date >= ( CURDATE() - INTERVAL 1 DAY ) ) ".$and_suf ;
1205
1206 } else { // Fixed
1207
1208 if ( $wh_booking_date !== '' )
1209 if ( strpos($wh_booking_date,':')===false ) // we are do not have the time in this date, so set it
1210 $sql_where.= $and_pre."( ".$pref."booking_date >= '" . $wh_booking_date . " 00:00:00' ) ".$and_suf;
1211 else $sql_where.= $and_pre."( ".$pref."booking_date >= '" . $wh_booking_date . "' ) ".$and_suf;
1212
1213 if ( $wh_booking_date2 !== '' )
1214 if ( strpos($wh_booking_date2,':')===false ) // we are do not have the time in this date, so set it
1215 $sql_where.= $and_pre."( ".$pref."booking_date <= '" . $wh_booking_date2 . " 23:59:59' ) ".$and_suf;
1216 else $sql_where.= $and_pre."( ".$pref."booking_date <= '" . $wh_booking_date2 . "' ) ".$and_suf;
1217 }
1218
1219 return $sql_where;
1220 }
1221
1222
1223 /**
1224 * 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
1225 *
1226 * @param type $wh_modification_date - Parameter from Booking Listing request (usually its number)
1227 * @param type $wh_modification_date2 - Parameter from Booking Listing request (usually its number)
1228 * @param string $pref - Optional. Prefix for table.
1229 * @return string - WHERE conditions for SQL
1230 */
1231 function wpbc_set_sql_where_for_modification_date( $wh_modification_date, $wh_modification_date2, $pref = 'bk.' ) {
1232
1233 $sql_where = '';
1234
1235 if ($pref == 'bk.') { $and_pre = ' AND '; $and_suf = ''; }
1236 else { $and_pre = ''; $and_suf = ' AND '; }
1237
1238 if ($wh_modification_date === '1') { // Today
1239 $sql_where = $and_pre."( ".$pref."modification_date <= ( CURDATE() + INTERVAL '23:59:59' HOUR_SECOND ) ) ".$and_suf ; // FixIn: 8.4.7.22.
1240 $sql_where .= $and_pre."( ".$pref."modification_date >= ( CURDATE() - INTERVAL '00:00:01' HOUR_SECOND ) ) ".$and_suf ; // FixIn: 8.4.7.22.
1241
1242 } else if ($wh_modification_date === '3') { // All
1243 $sql_where = '';
1244
1245 } else if ($wh_modification_date === '5') { // Prior
1246 $wh_modification_date2 = str_replace('-', '', $wh_modification_date2);
1247 $sql_where = $and_pre."( ".$pref."modification_date >= ( CURDATE() - INTERVAL ". $wh_modification_date2 . " DAY ) ) ".$and_suf ;
1248 $sql_where .= $and_pre."( ".$pref."modification_date <= ( CURDATE() + INTERVAL 1 DAY ) ) ".$and_suf ;
1249
1250 } else { // Fixed
1251
1252 if ( $wh_modification_date !== '' )
1253 $sql_where.= $and_pre."( ".$pref."modification_date >= '" . $wh_modification_date . "' ) ".$and_suf;
1254
1255 if ( $wh_modification_date2 !== '' )
1256 $sql_where.= $and_pre."( ".$pref."modification_date <= '" . $wh_modification_date2 . "' ) ".$and_suf;
1257 }
1258
1259 return $sql_where;
1260 }
1261