PluginProbe
Booking Calendar / 10.10
Booking Calendar v10.10
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 10.11.3 All 202 releases
booking / includes / _capacity / calendar_load.php

calendar_load.php in Booking Calendar 10.10, at includes/_capacity/calendar_load.php

335 lines 22.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4.
4
5 /**
6 * Define parameters for calendar -- which is required for JS calendar show
7 *
8 * @param $params
9 *
10 * @return string
11 */
12 function wpbc__calendar__set_js_params__before_show( $params ) {
13
14 $start_script_code = '';
15
16 // Server Balancer ------------------------------------------------------------------------------------------- //FixIn: // FixIn: 9.8.6.2.
17 $balancer_max_threads = intval( get_bk_option( 'booking_load_balancer_max_threads' ) );
18 $balancer_max_threads = ( empty( $balancer_max_threads ) ) ? 1 : $balancer_max_threads;
19 $start_script_code .= " _wpbc.balancer__set_max_threads( " . $balancer_max_threads . " ); ";
20
21 $resource_id = isset( $params['resource_id'] ) ? intval( $params['resource_id'] ) : 1;
22
23 // Start Month ----------------------------------------------------------------------------------------------------
24 $calendar_scroll_to = ( ( isset( $params['start_month_calendar'] ) ) && ( is_array( $params['start_month_calendar'] ) ) && ( count( $params['start_month_calendar'] ) > 0 ) )
25 ? "[ " . strval( intval( $params['start_month_calendar'][0] ) ) . "," . strval( intval( $params['start_month_calendar'][1] ) ) . " ]"
26 : 'false';
27 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'calendar_scroll_to' , " . $calendar_scroll_to . " ); ";
28
29 // Max months to scroll -------------------------------------------------------------------------------------------
30 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_max_monthes_in_calendar' , '" . esc_js( get_bk_option( 'booking_max_monthes_in_calendar' ) ) . "' ); "; // FixIn: 10.6.1.3.
31
32 // Start of WeekDay -----------------------------------------------------------------------------------------------
33 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_start_day_weeek' , '" . esc_js( get_bk_option( 'booking_start_day_weeek' ) ) . "' ); "; // FixIn: 10.6.1.3.
34
35 // Number of visible months -----------------------------------------------------------------------------------------------
36 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'calendar_number_of_months' , '" . $params['calendar_number_of_months'] . "' ); ";
37
38 // -----------------------------------------------------------------------------------------------------------------
39 // Days Selection
40 // -----------------------------------------------------------------------------------------------------------------
41 $days_selection_arr = wpbc__calendar__js_params__get_days_selection_arr();
42 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'days_select_mode', '" . $days_selection_arr['days_select_mode'] . "' ); ";
43 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'fixed__days_num', " . $days_selection_arr['fixed__days_num'] . " ); ";
44 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'fixed__week_days__start', [" .$days_selection_arr['fixed__week_days__start'] . "] ); ";
45 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'dynamic__days_min', " . $days_selection_arr['dynamic__days_min'] . " ); ";
46 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'dynamic__days_max', " . $days_selection_arr['dynamic__days_max'] . " ); ";
47 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'dynamic__days_specific', [" .$days_selection_arr['dynamic__days_specific'] . "] ); ";
48 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'dynamic__week_days__start', [" .$days_selection_arr['dynamic__week_days__start'] . "] ); ";
49
50 /**
51 * For Debug ::
52 // $start_script_code .= " _wpbc.calendar__set_parameters( " . $resource_id . ", {
53 // 'days_select_mode' : 'dynamic',
54 // 'dynamic__days_min' : 7,
55 // 'dynamic__days_max' : 14,
56 // 'dynamic__days_specific' : [7,10,14],
57 // 'dynamic__week_days__start' : [-1]
58 // } ); ";
59 // Calendar - Set calendar parameter and value
60 // $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'days_select_mode' , 'multiple' ); ";
61 // $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'days_select_mode' , 'fixed' ); ";
62 // $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'fixed__days_num' , 7 ); ";
63 // $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'fixed__week_days__start' , [-1] ); ";
64 */
65
66 // Date / Time formats:
67 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'booking_date_format', '" . esc_js( get_bk_option( 'booking_date_format' ) ) . "' ); ";
68 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'booking_time_format', '" . esc_js( get_bk_option( 'booking_time_format' ) ) . "' ); ";
69
70 // Capacity | availability ----------------------------------------------------------------------------------------
71 if ( class_exists( 'wpdev_bk_biz_l' ) ) {
72 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'is_parent_resource' , " . ( wpbc_get_child_resources_number( $resource_id ) ? 1 : 0 ) . " ); ";
73 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_capacity_field' , '" . wpbc_get__booking_capacity_field__name() . "' ); ";
74 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_is_dissbale_booking_for_different_sub_resources' , '" . get_bk_option( 'booking_is_dissbale_booking_for_different_sub_resources' ) . "' ); ";
75 }
76 if ( class_exists( 'wpdev_bk_biz_s' ) ) {
77 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_recurrent_time' , '" . esc_js( get_bk_option( 'booking_recurrent_time' ) ) . "' ); "; // FixIn: 10.6.1.3.
78 }
79
80 // -----------------------------------------------------------------------------------------------------------------
81 // Save initial values of days selection for later use in conditional day logic
82 $start_script_code .= " if ( 'function' === typeof ( wpbc__conditions__SAVE_INITIAL__days_selection_params__bm ) ){ wpbc__conditions__SAVE_INITIAL__days_selection_params__bm( " . $resource_id . " ); } ";
83
84 if (
85 ( ! empty( $params['shortcode_options'] ) )
86 && ( function_exists( 'wpbc_parse_shortcode_option__set_days_selection_conditions' ) )
87 ){
88 $days_selection_conditions = wpbc_parse_shortcode_option__set_days_selection_conditions( $resource_id, $params['shortcode_options'] );
89 if ( ! empty( $days_selection_conditions ) ) {
90 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . ", 'conditions', " . wp_json_encode( $days_selection_conditions['conditions'] ). " ); ";
91 $start_script_code .= " _wpbc.seasons__set( " . $resource_id . ", " . wp_json_encode( $days_selection_conditions['seasons'] ) . " ); ";
92 }
93 }
94
95 return $start_script_code;
96 }
97
98 /**
99 * Get days selection parameters, which saved in database
100 *
101 * @return array
102 */
103 function wpbc__calendar__js_params__get_days_selection_arr(){
104
105 $specific_days_selection = ( function_exists( 'wpbc_get_specific_range_dates__as_comma_list' ) )
106 ? wpbc_get_specific_range_dates__as_comma_list( get_bk_option( 'booking_range_selection_days_specific_num_dynamic' ) )
107 : '';
108 $data_arr = array();
109 // Modes :: Selection of Days
110 $data_arr['days_select_mode'] = ( 'range' === get_bk_option('booking_type_of_day_selections') )
111 ? get_bk_option('booking_range_selection_type')
112 : get_bk_option( 'booking_type_of_day_selections');
113 $data_arr['fixed__days_num'] = intval( get_bk_option( 'booking_range_selection_days_count' ) ); /* Number of days selection with 1 mouse click */
114 $data_arr['fixed__week_days__start'] = get_bk_option( 'booking_range_start_day' ); /* { -1 - Any | 0 - Su, 1 - Mo, 2 - Tu, 3 - We, 4 - Th, 5 - Fr, 6 - Sat } */
115 $data_arr['dynamic__days_min'] = intval( get_bk_option( 'booking_range_selection_days_count_dynamic' ) ); /* Min. Number of days selection with 2 mouse clicks */
116 $data_arr['dynamic__days_max'] = intval( get_bk_option( 'booking_range_selection_days_max_count_dynamic' ) ); /* Max. Number of days selection with 2 mouse clicks */
117 $data_arr['dynamic__days_specific'] = $specific_days_selection; /* Example: '5,7' */
118 $data_arr['dynamic__week_days__start'] = get_bk_option( 'booking_range_start_day_dynamic' ); /* { -1 - Any | 0 - Su, 1 - Mo, 2 - Tu, 3 - We, 4 - Th, 5 - Fr, 6 - Sat } */
119
120 // Fix about possible issue of downgrade from Paid to free version and try to use 'Range days' selection, which is not supports in that version. // FixIn: 10.4.0.4.
121 if ( ( ! class_exists( 'wpdev_bk_biz_s' ) ) && ( in_array( $data_arr['days_select_mode'], array( 'dynamic', 'fixed' ) ) ) ) {
122 $data_arr['days_select_mode'] = 'multiple';
123 }
124
125 return $data_arr;
126 }
127
128
129
130 /**
131 * Get JavaScript for Calendar Loading: 1.Show Calendar 2. Send Ajax to Load bookings / availability / data for calendar
132 *
133 * @param $params
134 *
135 * @return string
136 */
137 function wpbc__calendar__load( $params = array() ){
138
139 $defaults = array(
140 'resource_id' => '1', // $resource_id
141 'aggregate_resource_id_arr' => array(), // It is array of booking resources from aggregate parameter()
142 'selected_dates_without_calendar' => '', // $my_selected_dates_without_calendar
143 'calendar_number_of_months' => 1, // $my_boook_count
144 'start_month_calendar' => false, // $start_month_calendar
145 'shortcode_options' => '', // options from the Booking Calendar shortcode. Usually it's conditional dates selection parameters
146 'custom_form' => 'standard'
147 );
148 $params = wp_parse_args( $params, $defaults );
149
150 // Resource ID
151 $params['resource_id'] = (int) $params['resource_id'];
152
153 $start_script_code = '<script type="text/javascript"> ' . wpbc_jq_ready_start(); // FixIn: 10.1.3.7.
154
155 //$start_script_code .= " wpbc_calendar__loading__start( {$params['resource_id']} ); ";
156
157 $start_script_code .= wpbc__calendar__set_js_params__before_show( $params );
158
159 // -----------------------------------------------------------------------------------------------------------------
160 // Show calendar
161 $start_script_code .= " wpbc_calendar_show( '" . $params['resource_id'] . "' ); ";
162
163 // -----------------------------------------------------------------------------------------------------------------
164 // Set Security - Nonce for Ajax - Listing
165 $start_script_code .= " _wpbc.set_secure_param( 'nonce', '" . wp_create_nonce( 'wpbc_calendar_load_ajx' . '_wpbcnonce' ) . "' ); ";
166 $start_script_code .= " _wpbc.set_secure_param( 'user_id', '" . wpbc_get_current_user_id() . "' ); ";
167 $start_script_code .= " _wpbc.set_secure_param( 'locale', '" . get_user_locale() . "' ); ";
168
169 // -----------------------------------------------------------------------------------------------------------------
170 // Get parameters for Ajax request
171 $get_booking_hash = ( ( isset( $_GET['booking_hash'] ) ) ? sanitize_text_field( wp_unslash( $_GET['booking_hash'] ) ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing */ /* FixIn: sanitize_unslash */
172 $booking_hash = $get_booking_hash;
173
174 // FixIn: 9.8.15.10.
175 $aggregate_type = 'all';
176 if (
177 ( ! empty( $params['shortcode_options'] ) )
178 && ( function_exists( 'wpbc_parse_calendar_options__aggregate_param' ) )
179 ) {
180 $aggregate_type = wpbc_parse_calendar_options__aggregate_param( $params['shortcode_options'] );
181 if ( ( ! empty( $aggregate_type ) ) && ( ! empty( $aggregate_type['type'] ) ) ) {
182 $aggregate_type = $aggregate_type['type'];
183 } else {
184 $aggregate_type = 'all';
185 }
186 }
187
188
189 /**
190 * About ['request_uri']:
191 * At ( front-end ) side the $_SERVER['REQUEST_URI'] = '/resource-id2/' which is good
192 * but in Ajax Response (admin side ) we will have:
193 * $_SERVER['REQUEST_URI'] = '/wp-admin/admin-ajax.php'
194 * $_SERVER['HTTP_REFERER'] = 'http://beta/resource-id2/'
195 * that is why we define 'request_uri' here at front-end side.
196 */
197 $server_request_uri = ( ( isset( $_SERVER['REQUEST_URI'] ) ) ? sanitize_text_field( $_SERVER['REQUEST_URI'] ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ /* FixIn: sanitize_unslash */
198 $params_for_request = array(
199 'resource_id' => $params['resource_id'],
200 'booking_hash' => $booking_hash,
201 'request_uri' => $server_request_uri, // Is it the same as window.location.href or
202 'custom_form' => $params['custom_form'], // Optional.
203 'aggregate_resource_id_str' => implode( ',', $params['aggregate_resource_id_arr'] ), // Optional. Resource ID from aggregate parameter in shortcode.
204 'aggregate_type' => $aggregate_type // Optional. 'all' | 'bookings_only' <- it is depends on shortcode parameter: options="{aggregate type=bookings_only}"
205 );
206 $params_for_request = wp_json_encode( $params_for_request );
207
208 // -----------------------------------------------------------------------------------------------------------------
209 // Send Ajax request to load bookings
210 $start_script_code .= " wpbc_calendar__load_data__ajx( {$params_for_request} ); ";
211
212 $start_script_code .= wpbc_jq_ready_end() . '</script>'; // FixIn: 10.1.3.7.
213
214 return $start_script_code;
215 }
216
217
218 /**
219 * Response to Ajax request, about loading calendar data
220 *
221 * @return void
222 */
223 function ajax_WPBC_AJX_CALENDAR_LOAD() {
224
225 // Security ------------------------------------------------------------------------------------------------------ // in Ajax Post: 'nonce': _wpbc.get_secure_param( 'nonce' ),
226 $action_name = 'wpbc_calendar_load_ajx' . '_wpbcnonce';
227 $nonce_post_key = 'nonce';
228 if ( wpbc_is_use_nonce_at_front_end() ) { // FixIn: 10.1.1.2.
229 $result_check = check_ajax_referer( $action_name, $nonce_post_key );
230 }
231
232 $user_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
233
234 /**
235 * SQL ---------------------------------------------------------------------------
236 *
237 * in Ajax Post: 'search_params': _wpbc.search_get_all_params()
238 *
239 * Use prefix "search_params", if Ajax sent - $_REQUEST['search_params']['page_num'], $_REQUEST['search_params']['page_items_count'], ...
240 */
241
242 /**
243 * Using this class here only for escaping variables
244 */
245 $user_request = new WPBC_AJX__REQUEST( array(
246 'db_option_name' => 'booking__wpbc_calendar_load__request_params',
247 'user_id' => $user_id,
248 'request_rules_structure' => array(
249 'resource_id' => array( 'validate' => 'd', 'default' => 1 ), // 'digit_or_csd'
250 'booking_hash' => array( 'validate' => 's', 'default' => '' ),
251 'request_uri' => array( 'validate' => 's', 'default' => '' ),
252 'custom_form' => array( 'validate' => 's', 'default' => 'standard' ),
253 'aggregate_resource_id_str' => array( 'validate' => 'digit_or_csd', 'default' => '' ), // Comma separated string of resource ID, which was used in 'aggregate' parameter.
254 'aggregate_type' => array( 'validate' => 's', 'default' => 'all' ) // 'all' | 'bookings_only' // FixIn: 9.8.15.10.
255 )
256 )
257 );
258 $request_prefix = 'calendar_request_params';
259 $request_params = $user_request->get_sanitized__in_request__value_or_default( $request_prefix ); // NOT Direct: $_REQUEST['search_params']['resource_id']
260
261
262 // Do Job here !! -------------------------------------------------------------------------------------------------
263
264 make_bk_action( 'check_pending_unpaid_bookings__do_auto_cancel', $request_params['resource_id'] );
265
266 $skip_booking_id = wpbc_get_booking_id__from_hash_in_url( $request_params['booking_hash'] ); // Booking ID ( like 1101 ) to skip in calendar
267 $max_days_count = wpbc_get_max_visible_days_in_calendar(); // 365 - number of visible days
268 // Resource ID from aggregate parameter in booking shortcode!
269 $aggregate_resource_id_arr = explode( ',', $request_params['aggregate_resource_id_str'] );
270 $aggregate_resource_id_arr = array_filter( $aggregate_resource_id_arr ); // All entries of array equal to FALSE (0, '', '0' ) will be removed.
271 $aggregate_resource_id_arr = array_unique( $aggregate_resource_id_arr ); // Remove duplicates // FixIn: 9.8.15.10.
272 if ( ( $resource_id_key = array_search( $request_params['resource_id'], $aggregate_resource_id_arr ) ) !== false ) {
273 unset( $aggregate_resource_id_arr[ $resource_id_key ] ); // Remove source booking resource from aggregate ARR. // FixIn: 9.8.15.10.
274 }
275 $aggregate_resource_id_arr = array_values($aggregate_resource_id_arr); // Reset keys
276
277 $availability_per_days__params = array(
278 'resource_id' => $request_params['resource_id'],
279 'max_days_count' => $max_days_count,
280 'skip_booking_id' => $skip_booking_id,
281 'request_uri' => $request_params['request_uri'], // It different in Ajax requests than $server_request_uri . It's used for change-over days to detect for exception at specific pages
282 'custom_form' => $request_params['custom_form']
283 , 'additional_bk_types' => $aggregate_resource_id_arr // It is array of booking resources from aggregate parameter() // arrays | CSD | int // OPTIONAL
284 , 'aggregate_type' => $request_params['aggregate_type'] // It is string: 'all' | 'bookings_only' // OPTIONAL
285 // , 'as_single_resource' => true // get dates as for 'single resource' or 'parent' resource including bookings in all 'child booking resources'
286 // , 'max_days_count' => wpbc_get_max_visible_days_in_calendar() // 365
287 // , 'timeslots_to_check_intersect' => $timeslots_to_check_intersect //TODO 2023-10-25: delete it, because we get it in wpbc_get_availability_per_days_arr() //array( '12:20 - 12:55', '13:00 - 14:00' )
288 );
289
290 // If in Booking > Add booking page we use in URL .../wp-admin/admin.php?page=wpbc-new&booking_type=2&as_single_resource=1 then get availability only for single resource.
291 if (
292 ( ! empty( $request_params['request_uri'] ) )
293 && ( strpos( $request_params['request_uri'], 'as_single_resource=1' ) !== false )
294 ){
295 $availability_per_days__params['as_single_resource'] = true;
296 }
297
298 /**
299 * Tricks:
300 * if ( strpos( $request_params['request_uri'],'page=wpbc-new' ) !== false ) { $availability_per_days__params['is_days_always_available'] = true; }
301 *
302 * // Set dates in calendar always available only for specific resources with specific ID
303 * if ( in_array( $request_params['resource_id'], array( 12, 15, 17 ) ) ) { $availability_per_days__params['is_days_always_available'] = true; }
304 */
305
306 // Get unavailable dates for calendar
307 $availability_per_days_arr = wpbc_get_availability_per_days_arr( $availability_per_days__params );
308
309
310 // Show message under calendar, if needed.
311 //$availability_per_days_arr['ajx_after_action_message'] = 'Ta Da :))';
312 //$availability_per_days_arr['ajx_after_action_message_status'] = 'warning';
313
314 // Ajax ------------------------------------------------------------------------------------------------------------
315 wp_send_json( array(
316 /**
317 * Send JSON. It will make "wp_json_encode" - so pass only array, and This function call wp_die( '', '', array( 'response' => null, ) ) .
318 * Pass JS OBJ: response_data in "jQuery.post( " function on success.
319 */
320 'ajx_data' => $availability_per_days_arr,
321 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
322 'ajx_search_params' => $_REQUEST[ $request_prefix ],
323 'ajx_cleaned_params' => $request_params,
324
325 'resource_id' => $request_params['resource_id']
326 ) );
327
328 }
329
330 if ( is_admin() && ( defined( 'DOING_AJAX' ) ) && ( DOING_AJAX ) ) {
331 add_action( 'wp_ajax_nopriv_' . 'WPBC_AJX_CALENDAR_LOAD', 'ajax_' . 'WPBC_AJX_CALENDAR_LOAD' ); // Client (not logged in)
332 add_action( 'wp_ajax_' . 'WPBC_AJX_CALENDAR_LOAD', 'ajax_' . 'WPBC_AJX_CALENDAR_LOAD' ); // Logged In users (or admin panel)
333 }
334
335