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 / _capacity / calendar_load.php

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

327 lines 21.3 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' , '" . get_bk_option( 'booking_max_monthes_in_calendar' ) . "' ); ";
31
32 // Start of WeekDay -----------------------------------------------------------------------------------------------
33 $start_script_code .= " _wpbc.calendar__set_param_value( " . $resource_id . " , 'booking_start_day_weeek' , '" . get_bk_option( 'booking_start_day_weeek' ) . "' ); ";
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' , '" . get_bk_option( 'booking_recurrent_time' ) . "' ); ";
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 return $data_arr;
121 }
122
123
124
125 /**
126 * Get JavaScript for Calendar Loading: 1.Show Calendar 2. Send Ajax to Load bookings / availability / data for calendar
127 *
128 * @param $params
129 *
130 * @return string
131 */
132 function wpbc__calendar__load( $params = array() ){
133
134 $defaults = array(
135 'resource_id' => '1', // $resource_id
136 'aggregate_resource_id_arr' => array(), // It is array of booking resources from aggregate parameter()
137 'selected_dates_without_calendar' => '', // $my_selected_dates_without_calendar
138 'calendar_number_of_months' => 1, // $my_boook_count
139 'start_month_calendar' => false, // $start_month_calendar
140 'shortcode_options' => '', // options from the Booking Calendar shortcode. Usually it's conditional dates selection parameters
141 'custom_form' => 'standard'
142 );
143 $params = wp_parse_args( $params, $defaults );
144
145 // Resource ID
146 $params['resource_id'] = (int) $params['resource_id'];
147
148 $start_script_code = '<script type="text/javascript"> ' . wpbc_jq_ready_start(); //FixIn: 10.1.3.7
149
150 //$start_script_code .= " wpbc_calendar__loading__start( {$params['resource_id']} ); ";
151
152 $start_script_code .= wpbc__calendar__set_js_params__before_show( $params );
153
154 // -----------------------------------------------------------------------------------------------------------------
155 // Show calendar
156 $start_script_code .= " wpbc_calendar_show( '" . $params['resource_id'] . "' ); ";
157
158 // -----------------------------------------------------------------------------------------------------------------
159 // Set Security - Nonce for Ajax - Listing
160 $start_script_code .= " _wpbc.set_secure_param( 'nonce', '" . wp_create_nonce( 'wpbc_calendar_load_ajx' . '_wpbcnonce' ) . "' ); ";
161 $start_script_code .= " _wpbc.set_secure_param( 'user_id', '" . wpbc_get_current_user_id() . "' ); ";
162 $start_script_code .= " _wpbc.set_secure_param( 'locale', '" . get_user_locale() . "' ); ";
163
164 // -----------------------------------------------------------------------------------------------------------------
165 // Get parameters for Ajax request
166 $booking_hash = isset( $_GET['booking_hash'] ) ? $_GET['booking_hash'] : '';
167
168 //FixIn: 9.8.15.10
169 $aggregate_type = 'all';
170 if (
171 ( ! empty( $params['shortcode_options'] ) )
172 && ( function_exists( 'wpbc_parse_calendar_options__aggregate_param' ) )
173 ) {
174 $aggregate_type = wpbc_parse_calendar_options__aggregate_param( $params['shortcode_options'] );
175 if ( ( ! empty( $aggregate_type ) ) && ( ! empty( $aggregate_type['type'] ) ) ) {
176 $aggregate_type = $aggregate_type['type'];
177 } else {
178 $aggregate_type = 'all';
179 }
180 }
181
182
183 /**
184 * About ['request_uri']:
185 * At ( front-end ) side the $_SERVER['REQUEST_URI'] = '/resource-id2/' which is good
186 * but in Ajax Response (admin side ) we will have:
187 * $_SERVER['REQUEST_URI'] = '/wp-admin/admin-ajax.php'
188 * $_SERVER['HTTP_REFERER'] = 'http://beta/resource-id2/'
189 * that is why we define 'request_uri' here at front-end side.
190 */
191 $params_for_request = array(
192 'resource_id' => $params['resource_id'],
193 'booking_hash' => $booking_hash,
194 'request_uri' => $_SERVER['REQUEST_URI'], // Is it the same as window.location.href or
195 'custom_form' => $params['custom_form'], // Optional.
196 'aggregate_resource_id_str' => implode( ',', $params['aggregate_resource_id_arr'] ), // Optional. Resource ID from aggregate parameter in shortcode.
197 'aggregate_type' => $aggregate_type // Optional. 'all' | 'bookings_only' <- it is depends on shortcode parameter: options="{aggregate type=bookings_only}"
198 );
199 $params_for_request = wp_json_encode( $params_for_request );
200
201 // -----------------------------------------------------------------------------------------------------------------
202 // Send Ajax request to load bookings
203 $start_script_code .= " wpbc_calendar__load_data__ajx( {$params_for_request} ); ";
204
205 $start_script_code .= wpbc_jq_ready_end() . '</script>'; //FixIn: 10.1.3.7
206
207 return $start_script_code;
208 }
209
210
211 /**
212 * Response to Ajax request, about loading calendar data
213 *
214 * @return void
215 */
216 function ajax_WPBC_AJX_CALENDAR_LOAD() {
217
218 // Security ------------------------------------------------------------------------------------------------------ // in Ajax Post: 'nonce': _wpbc.get_secure_param( 'nonce' ),
219 $action_name = 'wpbc_calendar_load_ajx' . '_wpbcnonce';
220 $nonce_post_key = 'nonce';
221 if ( wpbc_is_use_nonce_at_front_end() ) { //FixIn: 10.1.1.2
222 $result_check = check_ajax_referer( $action_name, $nonce_post_key );
223 }
224
225 $user_id = ( isset( $_REQUEST['wpbc_ajx_user_id'] ) ) ? intval( $_REQUEST['wpbc_ajx_user_id'] ) : wpbc_get_current_user_id();
226
227 /**
228 * SQL ---------------------------------------------------------------------------
229 *
230 * in Ajax Post: 'search_params': _wpbc.search_get_all_params()
231 *
232 * Use prefix "search_params", if Ajax sent - $_REQUEST['search_params']['page_num'], $_REQUEST['search_params']['page_items_count'], ...
233 */
234
235 /**
236 * Using this class here only for escaping variables
237 */
238 $user_request = new WPBC_AJX__REQUEST( array(
239 'db_option_name' => 'booking__wpbc_calendar_load__request_params',
240 'user_id' => $user_id,
241 'request_rules_structure' => array(
242 'resource_id' => array( 'validate' => 'd', 'default' => 1 ), // 'digit_or_csd'
243 'booking_hash' => array( 'validate' => 's', 'default' => '' ),
244 'request_uri' => array( 'validate' => 's', 'default' => '' ),
245 'custom_form' => array( 'validate' => 's', 'default' => 'standard' ),
246 'aggregate_resource_id_str' => array( 'validate' => 'digit_or_csd', 'default' => '' ), // Comma separated string of resource ID, which was used in 'aggregate' parameter.
247 'aggregate_type' => array( 'validate' => 's', 'default' => 'all' ) // 'all' | 'bookings_only' //FixIn: 9.8.15.10
248 )
249 )
250 );
251 $request_prefix = 'calendar_request_params';
252 $request_params = $user_request->get_sanitized__in_request__value_or_default( $request_prefix ); // NOT Direct: $_REQUEST['search_params']['resource_id']
253
254
255 // Do Job here !! -------------------------------------------------------------------------------------------------
256
257 make_bk_action( 'check_pending_unpaid_bookings__do_auto_cancel', $request_params['resource_id'] );
258
259 $skip_booking_id = wpbc_get_booking_id__from_hash_in_url( $request_params['booking_hash'] ); // Booking ID ( like 1101 ) to skip in calendar
260 $max_days_count = wpbc_get_max_visible_days_in_calendar(); // 365 - number of visible days
261 // Resource ID from aggregate parameter in booking shortcode!
262 $aggregate_resource_id_arr = explode( ',', $request_params['aggregate_resource_id_str'] );
263 $aggregate_resource_id_arr = array_filter( $aggregate_resource_id_arr ); // All entries of array equal to FALSE (0, '', '0' ) will be removed.
264 $aggregate_resource_id_arr = array_unique( $aggregate_resource_id_arr ); // Remove duplicates //FixIn: 9.8.15.10
265 if ( ( $resource_id_key = array_search( $request_params['resource_id'], $aggregate_resource_id_arr ) ) !== false ) {
266 unset( $aggregate_resource_id_arr[ $resource_id_key ] ); // Remove source booking resource from aggregate ARR. //FixIn: 9.8.15.10
267 }
268 $aggregate_resource_id_arr = array_values($aggregate_resource_id_arr); // Reset keys
269
270 $availability_per_days__params = array(
271 'resource_id' => $request_params['resource_id'],
272 'max_days_count' => $max_days_count,
273 'skip_booking_id' => $skip_booking_id,
274 '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
275 'custom_form' => $request_params['custom_form']
276 , 'additional_bk_types' => $aggregate_resource_id_arr // It is array of booking resources from aggregate parameter() // arrays | CSD | int // OPTIONAL
277 , 'aggregate_type' => $request_params['aggregate_type'] // It is string: 'all' | 'bookings_only' // OPTIONAL
278 // , 'as_single_resource' => true // get dates as for 'single resource' or 'parent' resource including bookings in all 'child booking resources'
279 // , 'max_days_count' => wpbc_get_max_visible_days_in_calendar() // 365
280 // , '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' )
281 );
282
283 // 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.
284 if (
285 ( ! empty( $request_params['request_uri'] ) )
286 && ( strpos( $request_params['request_uri'], 'as_single_resource=1' ) !== false )
287 ){
288 $availability_per_days__params['as_single_resource'] = true;
289 }
290
291 /**
292 * Tricks:
293 * if ( strpos( $request_params['request_uri'],'page=wpbc-new' ) !== false ) { $availability_per_days__params['is_days_always_available'] = true; }
294 *
295 * // Set dates in calendar always available only for specific resources with specific ID
296 * if ( in_array( $request_params['resource_id'], array( 12, 15, 17 ) ) ) { $availability_per_days__params['is_days_always_available'] = true; }
297 */
298
299 // Get unavailable dates for calendar
300 $availability_per_days_arr = wpbc_get_availability_per_days_arr( $availability_per_days__params );
301
302
303 // Show message under calendar, if needed.
304 //$availability_per_days_arr['ajx_after_action_message'] = 'Ta Da :))';
305 //$availability_per_days_arr['ajx_after_action_message_status'] = 'warning';
306
307 // Ajax ------------------------------------------------------------------------------------------------------------
308 wp_send_json( array(
309 /**
310 * Send JSON. It will make "wp_json_encode" - so pass only array, and This function call wp_die( '', '', array( 'response' => null, ) ) .
311 * Pass JS OBJ: response_data in "jQuery.post( " function on success.
312 */
313 'ajx_data' => $availability_per_days_arr,
314 'ajx_search_params' => $_REQUEST[ $request_prefix ],
315 'ajx_cleaned_params' => $request_params,
316
317 'resource_id' => $request_params['resource_id']
318 ) );
319
320 }
321
322 if ( is_admin() && ( defined( 'DOING_AJAX' ) ) && ( DOING_AJAX ) ) {
323 add_action( 'wp_ajax_nopriv_' . 'WPBC_AJX_CALENDAR_LOAD', 'ajax_' . 'WPBC_AJX_CALENDAR_LOAD' ); // Client (not logged in)
324 add_action( 'wp_ajax_' . 'WPBC_AJX_CALENDAR_LOAD', 'ajax_' . 'WPBC_AJX_CALENDAR_LOAD' ); // Logged In users (or admin panel)
325 }
326
327