PluginProbe
Booking Calendar / 11.2
Booking Calendar v11.2
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 10.12.0 10.12.1 All 199 releases
booking / core / wpbc-dev-api.php

wpbc-dev-api.php in Booking Calendar 11.2, at core/wpbc-dev-api.php

793 lines 41.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @version 1.0
4 * @package Booking Calendar
5 * @subpackage Dev API for integration Booking Calendar with third party
6 * @category API
7 *
8 * @author wpdevelop
9 * @link https://wpbookingcalendar.com/
10 * @email info@wpbookingcalendar.com
11 *
12 * @modified 2017-06-24
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19
20 // ---------------------------------------------------------------------------------------------------------------------
21 // Add New Booking .
22 // ---------------------------------------------------------------------------------------------------------------------
23 /**
24 * Add New Booking
25 *
26 * @param array $booking_dates // array( '2017-06-24', '2017-06-24', '2017-06-25' );
27 * @param array $booking_data // array(
28 * 'secondname' => array( 'value' => 'Rika' , 'type' => 'text' )
29 * , 'name' => 'Jo' // 'text' field type, if in such format
30 * , 'rangetime' => array( 'value' => '14:00 - 16:00', 'type' => 'selectbox-one' )
31 * , 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' )
32 * )
33 * @param int $resource_id // Optional. Default: 1
34 * @param type $params // Optional. Default: array(
35 * 'is_send_emeils' => 0
36 * , 'booking_form_type' => ''
37 * , 'wpdev_active_locale' => 'en_US'
38 * , 'is_show_payment_form' => 0
39 * , 'is_edit_booking' => false | array( 'booking_id' => 75, 'booking_type' => 1 )
40 * , 'save_booking_even_if_unavailable' => 0 // 0 | 1 - if 1 then save booking without checking if this date available. Usually save to main booking resource
41 * , 'is_use_booking_recurrent_time' => false
42 * )
43 * @return int|WP_Error - booking ID or WP_Error
44 *
45 *
46 ***********************************************************************************************************************
47 * Notes!
48 * If you need to book for specific time, then its have to be in appropriate field(s) at $booking_data - booking form. In $booking_dates times have been sliced.
49 * It does not check about booked | available dates in calendar with capacity > 1 !!!!
50 * If the single booking resource booked for specific dates and settings have activated "Checking to prevent double booking, during submitting booking" then system just DIE
51 ***********************************************************************************************************************
52 *
53 * Examples:
54 *
55 - AddSimple
56 $booking = array(
57 'dates' => array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
58 , 'data' => array(
59 'secondname' => array( 'value' => 'Rika', 'type' => 'text' )
60 , 'name' => 'John'
61 , 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' )
62 )
63 );
64 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ] );
65
66 - Resource
67 $booking = array(
68 'dates' => array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-26' )
69 , 'data' => array(
70 'secondname' => array( 'value' => 'Rika', 'type' => 'text' )
71 , 'name' => 'JoNNNNNNNNNN'
72 , 'rangetime' => array( 'value' => '14:00 - 16:00', 'type' => 'selectbox-one' )
73 , 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' )
74 )
75 , 'resource_id' => 3
76
77 );
78 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ] );
79
80
81 - Edit:
82 $booking = array(
83 'dates' => array( '2017-06-24', '2017-06-24', '2017-06-25', '2017-06-28' )
84 , 'data' => array(
85 'secondname' => array( 'value' => 'Rika', 'type' => 'text' )
86 , 'name' => 'BoBy'
87 , 'rangetime' => array( 'value' => '14:00 - 16:00', 'type' => 'selectbox-one' )
88 , 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' )
89 )
90 , 'resource_id' => 3
91 , 'params' => array( 'is_edit_booking' => array( 'booking_id' => 79, 'booking_type' => 3 ) )
92
93 );
94 $booking_id = wpbc_api_booking_add_new( $booking[ 'dates' ], $booking[ 'data' ], $booking[ 'resource_id' ], $booking[ 'params' ] );
95 *
96 */
97 function wpbc_api_booking_add_new( $booking_dates, $booking_data, $resource_id = 1, $params = array() ) {
98
99 /*
100 // Dates in format: 'Y-m-d'
101 $booking_dates = array( '2017-06-24', '2017-06-24', '2017-06-25' );
102
103 // Booking Form params
104 $booking_data = array(
105 'secondname' => array( 'value' => 'Rika' , 'type' => 'text' )
106 , 'rangetime' => array( 'value' => '14:00 - 16:00', 'type' => 'selectbox-one' )
107 , 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' )
108 );
109 // Booking resource ID
110 $resource_id = 1;
111 */
112
113 // Other params
114 $defaults = array(
115 'is_send_emeils' => 0
116 , 'booking_form_type' => '' // custom_form_name
117 , 'wpdev_active_locale' => 'en_US' // locale
118 , 'is_show_payment_form' => 0 // Parameters for adding booking in the HTML:
119 , 'is_edit_booking' => false // array( 'booking_id' => 75, 'booking_type' => 1 ) // Update Booking params
120 , 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) )
121 );
122 $params = wp_parse_args( $params, $defaults );
123
124
125 // booking resource ID
126 $resource_id = intval( $resource_id );
127 $resource_id = ( empty( $resource_id ) ? 1 : $resource_id );
128 $params[ 'bktype' ] = $resource_id;
129
130
131 // Dates ///////////////////////////////////////////////////////////////////////////////////////////////////////////
132 $booking_dates = array_map( 'strtotime', $booking_dates ); // Array ( [0] => 1498262400 [1] => 1498348800 )
133 sort( $booking_dates ); // Sort
134 $booking_dates = array_unique( $booking_dates ); // Remove Duplicates
135 $dates_formats = array_fill( 0, count( $booking_dates ), "d.m.Y" ); // Array ( [0] => d.m.Y [1] => d.m.Y )
136 $booking_dates = array_map( 'date_i18n', $dates_formats , $booking_dates ); // Array ( [0] => 24.06.2017 [1] => 25.06.2017 )
137 $booking_dates = implode(', ', $booking_dates); // 24.06.2017, 25.06.2017
138 $params[ 'dates' ] = $booking_dates;
139
140
141 // Booking Form ////////////////////////////////////////////////////////////////////////////////////////////////////
142 $booking_form = array();
143 foreach ( $booking_data as $field_name => $field_params ) {
144
145 if ( is_array( $field_params ) ) {
146
147 $booking_form_field = array( $field_params['type'], $field_name . $resource_id, $field_params['value'] );
148 } else { // value just string
149 $booking_form_field = array( 'text', $field_name . $resource_id, $field_params );
150 }
151 $booking_form_field[ 0 ] = str_replace( array( '^', '~' ), array( 'curret', 'tilde' ), $booking_form_field[ 0 ] ); // replace to temp symbols
152 $booking_form_field[ 1 ] = str_replace( array( '^', '~' ), array( 'curret', 'tilde' ), $booking_form_field[ 1 ] );
153 $booking_form_field[ 2 ] = str_replace( array( '^', '~' ), array( 'curret', 'tilde' ), $booking_form_field[ 2 ] );
154
155 $booking_form_field = implode( '^' , $booking_form_field );
156 $booking_form []= $booking_form_field;
157 }
158 $booking_form = implode( '~' , $booking_form );
159 $params[ 'form' ] = $booking_form;
160
161
162 $resource_id = $params['bktype'];
163
164
165 $booking_hash = '';
166 if ( ! empty( $params['is_edit_booking'] ) ) {
167
168 $hash__arr = wpbc_hash__get_booking_hash__resource_id( $params['is_edit_booking']['booking_id'] ); // Get new booking hash
169
170 if ( ! empty( $hash__arr ) ) {
171 list( $booking_hash, $resource_id ) = $hash__arr;
172 }
173 }
174
175
176 // Create a new booking
177 $request_save_params = array(
178 'resource_id' => $resource_id, // 2
179 'dates_ddmmyy_csv' => $params[ 'dates' ], // '04.10.2023, 05.10.2023, 06.10.2023'
180 'form_data' => $params[ 'form' ], // 'text^cost_hint2^150.00฿~selectbox-multiple^rangetime2[]^14:00...'
181 'booking_hash' => $booking_hash, // 'sdfsf34534rf'
182 'custom_form' => $params['booking_form_type'], // 'custom_form_name'
183 'is_emails_send' => $params['is_send_emeils'], // 0 | 1
184 'is_show_payment_form' => intval( $params['is_show_payment_form'] ), // 0 | 1
185 // 'request_uri' => $_SERVER['HTTP_REFERER']
186 // 'user_id' => wpbc_get_current_user_id()
187 'sync_gid' => ( ( ! empty( $params['sync_gid'] ) ) ? $params['sync_gid'] : '' ),
188 'is_approve_booking' => ( ( ! empty( $params['is_approve_booking'] ) ) ? $params['is_approve_booking'] : 0 ), // Auto approve booking ??
189 'save_booking_even_if_unavailable' => ( ( ! empty( $params['save_booking_even_if_unavailable'] ) ) ? $params['save_booking_even_if_unavailable'] : 0 ), // Force saving booking without checking
190 'is_use_booking_recurrent_time' => intval( $params['is_use_booking_recurrent_time'] )
191 );
192
193 $booking_save_arr = wpbc_booking_save( $request_save_params );
194 if ( 'ok' === $booking_save_arr['ajx_data']['status'] ) { // Everything Cool :) - booking has been duplicated
195 $booking_id = $booking_save_arr['booking_id'];
196 } else { // Error
197 $booking_id = 0;
198 // Error message: $booking_save_arr['ajx_data']['ajx_after_action_message'];
199 return new WP_Error( 'wpbc_api_booking_add_new__error', $booking_save_arr['ajx_data']['ajx_after_action_message'] );
200 }
201
202 return $booking_id;
203 }
204
205 // ---------------------------------------------------------------------------------------------------------------------
206 // Is Date Booked ?
207 // ---------------------------------------------------------------------------------------------------------------------
208 /**
209 * Check if dates available in specific resource
210 *
211 * @param array $booking_dates // [ "2023-09-27 23:30:01","2023-09-28 00:00:00","2023-09-29 00:59:52" ] Range List of Dates in MySQL format, like: array( '2017-06-23 14:00:01', '2017-06-24 00:00:00', '2017-06-25', '2017-06-26 12:00:02' );
212 * @param int $resource_id // 9 Optional. Default: 1
213 * @param array $params // [ 'is_use_booking_recurrent_time' => false ] Optional. Default: array( ) -- For future improvement
214 *
215 * @return bool // true | false
216 *
217 * Examples:
218 *
219 * $booking = array(
220 * 'dates' => array( '2017-06-23 14:00:01', '2017-06-24 00:00:00', '2017-06-25', '2017-06-26 12:00:02' )
221 * , 'resource_id' => 1
222 * , 'params' => array( 'is_use_booking_recurrent_time' => false )
223 * );
224 * $result = wpbc_api_is_dates_booked( $booking[ 'dates' ], $booking[ 'resource_id' ], $booking[ 'params' ] );
225 */
226 function wpbc_api_is_dates_booked( $booking_dates, $resource_id = 1, $params = array() ) {
227
228 $defaults = array(
229 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) )
230 );
231 $params = wp_parse_args( $params, $defaults );
232
233
234 // Convert: ["2023-09-27 23:30:01","2023-09-28 00:00:00","2023-09-29 00:59:52"] -> [ "2023-10-18", "2023-10-25", "2023-11-25" ]
235 $dates_only_sql_arr = array_map( function ( $value ) {
236 $value_arr = explode( ' ', $value );
237 return $value_arr[0];
238 }, $booking_dates );
239
240 // Convert: ["2023-09-27 23:30:01","2023-09-28 00:00:00","2023-09-29 00:59:52"] -> [ 36000, 39600 ]
241 $time_as_seconds_arr = array( $booking_dates[0], $booking_dates[ ( count( $booking_dates ) - 1 ) ], );
242 $time_as_seconds_arr = array_map( function ( $value ) {
243 $value_arr = explode( ' ', $value );
244 $time_sec = wpbc_transform__24_hours_his__in__seconds( $value_arr[1] );
245 return $time_sec;
246 }, $time_as_seconds_arr );
247
248
249 /**
250 * Get slots [] where we can save booking = [ 'resources_in_dates' => [ 2023-10-18 = [ 2, 12, 10, 11 ]
251 * 2023-10-19 = [ 2, 12, 10, 11 ]
252 * 2023-10-20 = [ 2, 12, 10, 11 ]
253 * ],
254 * 'time_to_book' => [ "14:00:01" , "12:00:01" ],
255 * 'result' => 'ok'
256 * 'main__resource_id' => 2
257 * ]
258 * OR
259 * [ 'result' => 'error', 'message' => 'Booking can not be saved ...' ]
260 */
261 $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 */
262 $server_http_referer_uri = ( ( isset( $_SERVER['HTTP_REFERER'] ) ) ? sanitize_text_field( $_SERVER['HTTP_REFERER'] ) : '' ); /* phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */ /* FixIn: sanitize_unslash */
263 $where_to_save_booking = wpbc__where_to_save_booking( array(
264 'resource_id' => $resource_id, // 2
265 'skip_booking_id' => '', // '', | 125 if edit booking
266 'dates_only_sql_arr' => $dates_only_sql_arr, // [ "2023-10-18", "2023-10-25", "2023-11-25" ]
267 'time_as_seconds_arr' => $time_as_seconds_arr, // [ 36000, 39600 ]
268 'how_many_items_to_book' => 1, // 1
269 'request_uri' => ( ( ( defined( 'DOING_AJAX' ) ) && ( DOING_AJAX ) ) ? $server_http_referer_uri : $server_request_uri ), // front-end: $server_request_uri | ajax: $_SERVER['HTTP_REFERER'] // It different in Ajax requests. It's used for change-over days to detect for exception at specific pages, // 'http://beta/resource-id2/'
270 'as_single_resource' => true, // false
271 'is_use_booking_recurrent_time' => $params['is_use_booking_recurrent_time']
272 ) );
273
274 if ( 'ok' != $where_to_save_booking['result'] ) {
275 $is_dates_times_unavailable = true;
276 /**
277 *
278 $ajx_data_arr['status'] = 'error';
279 $ajx_data_arr['status_error'] = 'booking_can_not_save';
280 $ajx_data_arr['ajx_after_action_message'] = $where_to_save_booking['message'];
281 $ajx_data_arr['ajx_after_action_message_status'] = 'warning';
282 return array( 'ajx_data' => $ajx_data_arr );
283 */
284 } else {
285 $is_dates_times_unavailable = false;
286 }
287
288 return $is_dates_times_unavailable;
289 }
290
291 // ---------------------------------------------------------------------------------------------------------------------
292 // Get Bookings Array - [Listing]
293 // ---------------------------------------------------------------------------------------------------------------------
294 /**
295 * Deprecated: Get bookings array from Booking Calendar
296 *
297 * @param aray $params
298 * @return array
299 ( [bookings] => Array (
300 [2661] => stdClass Object (
301 [booking_id] => 2661
302 [trash] => 0
303 [sync_gid] => 5t3ogfsb3tqj09po7fiou6hh60@google.com
304 [is_new] => 1
305 [status] =>
306 [sort_date] => 2017-08-07 20:00:01
307 [modification_date] => 2017-07-08 11:54:03
308 [form] => text^name4^Event (timezone Pacific GMT-07:00)~....
309 [hash] => 69afc11e2ce86044dd55fbddf582ce66
310 [booking_type] => 4
311 [remark] =>
312 [cost] => 51.98
313 [pay_status] => 149950764311
314 [pay_request] => 0
315 [dates] => Array (
316 [0] => stdClass Object (
317 [booking_id] => 2661
318 [booking_date] => 2017-08-07 20:00:01
319 [approved] => 0
320 [type_id] => )
321 [1] => stdClass Object (
322 [booking_id] => 2661
323 [booking_date] => 2017-08-08 00:00:00
324 [approved] => 0
325 [type_id] =>
326 )
327 )
328 [dates_short] => Array (
329 [0] => 2017-08-07 20:00:01
330 [1] => -
331 [2] => 2017-08-08 00:00:00 )
332 [form_show] => 'First Name: John....'
333 [form_data] => Array (
334 [email] => ics@beta
335 [name] => Event (timezone Pacific GMT-07:00)
336 [secondname] =>
337 [visitors] => 1
338 [coupon] =>
339 [_all_] => Array (
340 [name4] => Event (timezone Pacific GMT-07:00)
341 [details4] => 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
342 [email4] => ics@beta
343 [rangetime4] => 20:00 - 00:00
344 [sync_gid4] => 5t3ogfsb3tqj09po7fiou6hh60@google.com
345 )
346 [_all_fields_] => Array (
347 [name] => Event (timezone Pacific GMT-07:00)
348 [details] => 8/7/2017 1:00pm TO 3:30pm 8/8/2017 (GMT-07:00) Pacific Time
349 [email] => ics@beta
350 [rangetime] => 20:00 - 00:00
351 [sync_gid] => 5t3ogfsb3tqj09po7fiou6hh60@google.com
352 [booking_resource_id] => 4
353 [resource_id] => 4
354 [type_id] => 4
355 [type] => 4
356 [resource] => 4
357 [booking_id] => 2661
358 [resource_title] => stdClass Object (
359 [booking_type_id] => 4
360 [title] => Apartment#3
361 [users] => 1
362 [import] => some_email@group.calendar.google.com
363 [cost] => 25.99
364 [default_form] => standard
365 [prioritet] => 40
366 [parent] => 0
367 [visitors] => 1
368 [id] => 4
369 [count] => 1
370 [ID] => 4
371 )
372 )
373 [rangetime] => 20:00 - 00:00
374 )
375 [dates_short_id] => Array (
376 [0] =>
377 [1] =>
378 [2] =>
379 )
380 )
381 ....
382 )
383 [resources] => Array (
384 [4] => stdClass Object
385 (
386 [booking_type_id] => 4
387 [title] => Apartment#3
388 [users] => 1
389 [import] => some_email@group.calendar.google.com
390 [cost] => 25.99
391 [default_form] => standard
392 [prioritet] => 40
393 [parent] => 0
394 [visitors] => 1
395 [id] => 4
396 [count] => 1
397 [ID] => 4
398 )
399 ....
400 )
401 [bookings_count] => 2
402 [page_num] => 1
403 [count_per_page] => 100000
404 )
405 *
406 */
407 function wpbc_api_get_bookings_arr( $params = array() ) {
408
409 // Start Date of getting bookings
410 $real_date = strtotime( 'now' );
411 $wh_booking_date = date_i18n( "Y-m-d", $real_date ); // '2012-12-01';
412
413 // End date of getting bookings
414 $real_date = strtotime( '+1 year' );
415 $wh_booking_date2 = date_i18n( "Y-m-d", $real_date ); // '2013-02-31';
416
417 // params
418 $defaults = array(
419 'wh_booking_type' => '1'
420 , 'wh_approved' => ''
421 , 'wh_booking_id' => ''
422 , 'wh_is_new' => ''
423 , 'wh_pay_status' => 'all'
424 , 'wh_keyword' => ''
425 , 'wh_booking_date' => $wh_booking_date
426 , 'wh_booking_date2' => $wh_booking_date2
427 , 'wh_modification_date' => '3'
428 , 'wh_modification_date2' => ''
429 , 'wh_cost' => ''
430 , 'wh_cost2' => ''
431 , 'or_sort' => ''
432 , 'page_num' => '1'
433 , 'wh_trash' => '' // '' | trash | any // FixIn: 8.0.2.8.
434 , 'limit_hours' => '0,24'
435 , 'only_booked_resources' => 0
436 , 'page_items_count' => '100000'
437 );
438 $params = wp_parse_args( $params, $defaults );
439
440 $bookings_arr = wpbc_get_bookings_objects( $params );
441
442 return $bookings_arr;
443 }
444
445 // FixIn: 8.7.6.4.
446 /**
447 * Get Booking Data as array of properties
448 *
449 * @param string $booking_id - digit '11' or comma separated '11,19,12'
450 *
451 * @return array
452 */
453 function wpbc_api_get_booking_by_id( $booking_id = '' ) {
454
455 global $wpdb;
456 $booking_id = wpbc_clean_digit_or_csd( $booking_id );
457
458 $slct_sql = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}booking as b left join {$wpdb->prefix}bookingdates as bd on (b.booking_id = bd.booking_id) WHERE b.booking_id IN (%s) LIMIT 0,1", $booking_id );
459
460 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
461 $slct_sql_results = $wpdb->get_results( $slct_sql, ARRAY_A );
462
463 $data = array();
464
465 if ( count( $slct_sql_results ) > 0 ) {
466 $data = $slct_sql_results[0];
467 $formdata_array = explode( '~', $data['form'] );
468
469 $formdata_array_count = count( $formdata_array );
470 for ( $i = 0; $i < $formdata_array_count; $i ++ ) {
471
472 if ( empty( $formdata_array[ $i ] ) ) {
473 continue;
474 }
475 $elemnts = explode( '^', $formdata_array[ $i ] );
476 $type = $elemnts[0];
477 $element_name = $elemnts[1];
478 $value = $elemnts[2];
479 $value = nl2br( $value );
480 $data['formdata'][ $element_name ] = $value;
481 }
482 }
483 return $data;
484 }
485
486 // FixIn: 8.7.7.3.
487 /**
488 * Get booking form fields in Booking Calendar Free version
489 * @return array
490 */
491 function wpbc_get_form_fields_free() {
492 $obj = array();
493 if ( function_exists( 'wpbc_simple_form__db__get_visual_form_structure' ) ) {
494
495 $form_fields = wpbc_simple_form__db__get_visual_form_structure();
496
497 foreach ( $form_fields as $field ) {
498 // FixIn: 8.7.8.7.
499 if ( ( ! empty( $field['name'] ) )
500 && ( ! empty( $field['label'] ) )
501 && ( ! in_array( $field['type'], array(
502 'captcha',
503 'submit'
504 ) ) )
505 ) {
506 $obj[ $field['name'] ] = $field['label'];
507 }
508 }
509 }
510 return $obj;
511 }
512
513
514 /**
515 * Hook for definition of the different DEFAULT parameters for single booking resource, that was just created by user at the Booking > Resources page.
516 * In this example, you need to uncomment this line:
517 * //add_action('wpbc_resource_created','wpbc_resource_created__add_other_params', 10, 1);
518 * And the following function add Duration-Based Costs for the exist booking resources and some Search Filters.
519 */
520 //add_action('wpbc_resource_created','wpbc_resource_created__add_other_params', 10, 1);
521 function wpbc_resource_created__add_other_params( $resource_id ) {
522
523 global $wpdb;
524
525 // -----------------------------------------------------------------------------------------------------------------
526 // Add default 'Duration-Based Costs'
527 // -----------------------------------------------------------------------------------------------------------------
528 $wp_queries = array();
529
530 //WP Booking Calendar > Prices > Daily Costs page -> section Duration-Based Costs
531 $season_id = 4;
532 $meta_arr = array(
533 array( 'active' => 'On', 'type' => 'summ', 'from' => 7, 'to' => 14, 'cost' => 90, 'cost_apply_to' => '%' ,'season_filter' => $season_id ),
534 array( 'active' => 'On', 'type' => '>', 'from' => 8, 'to' => 27, 'cost' => 90, 'cost_apply_to' => '%' ,'season_filter' => $season_id ),
535 array( 'active' => 'On', 'type' => 'summ', 'from' => 28, 'to' => 14, 'cost' => 75, 'cost_apply_to' => '%' ,'season_filter' => $season_id ),
536 array( 'active' => 'On', 'type' => '>', 'from' => 29, 'to' => 60, 'cost' => 75, 'cost_apply_to' => '%' ,'season_filter' => $season_id ),
537 array( 'active' => 'On', 'type' => '=', 'from' => 'LAST', 'to' => 14, 'cost' => 0, 'cost_apply_to' => 'fixed' ,'season_filter' => $season_id )
538 );
539 $meta_str = maybe_serialize($meta_arr);
540 $wp_queries[] = "INSERT INTO {$wpdb->prefix}booking_types_meta ( type_id, meta_key, meta_value ) VALUES ( {$resource_id}, 'costs_depends', '{$meta_str}' );";
541
542 // // WP Booking Calendar > Prices > Daily Costs page -> section Seasonal Pricing
543 // $meta_str = "a:3:{s:6:\"filter\";a:3:{i:3;s:3:\"Off\";i:2;s:3:\"Off\";i:1;s:2:\"On\";}s:4:\"rate\";a:3:{i:3;s:1:\"0\";i:2;s:1:\"0\";i:1;s:3:\"200\";}s:9:\"rate_type\";a:3:{i:3;s:1:\"%\";i:2;s:1:\"%\";i:1;s:1:\"%\";}}";
544 // $wp_queries[] = "INSERT INTO {$wpdb->prefix}booking_types_meta ( type_id, meta_key, meta_value ) VALUES ( {$resource_id}, 'rates', '{$meta_str}' );";
545 //
546 // //WP Booking Calendar > Availability > Season Availability page
547 // $meta_str = "a:2:{s:7:\"general\";s:2:\"On\";s:6:\"filter\";a:3:{i:3;s:3:\"Off\";i:2;s:2:\"On\";i:1;s:3:\"Off\";}}";
548 // $wp_queries[] = "INSERT INTO {$wpdb->prefix}booking_types_meta ( type_id, meta_key, meta_value ) VALUES ( {$resource_id}, 'availability', '{$meta_str}' );";
549
550 foreach ( $wp_queries as $wp_q ) {
551 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
552 $wpdb->query( $wp_q );
553 }
554
555 // -----------------------------------------------------------------------------------------------------------------
556 // Add default 'Searchable Filters':
557 // -----------------------------------------------------------------------------------------------------------------
558 // Get search options for all booking resources
559 $search_options_arr = wpbc_searchable_resources__get_all_options();
560
561 // Booking Calendar Business Large version
562 $demo_examples = array();
563 $demo_examples[ $resource_id ] = array();
564 $demo_examples[ $resource_id ]['is_searchable'] = 'On';
565 // $demo_examples[ $resource_id ]['title'] = 'General Consultation';
566 // $demo_examples[ $resource_id ]['url'] = get_site_url() . '/demo/service-a/#main';
567 // $demo_examples[ $resource_id ]['picture'] = get_site_url() . '/demo_assets/service-a.jpg';
568 // $demo_examples[ $resource_id ]['description'] = '...';
569 $demo_examples[ $resource_id ]['options'] = 'neighborhood^=^La Ecovilla~housing_type^=^Entire home~price_range^=^$150-$199~bedrooms^<=^1~my_contribution^=^1';
570
571 foreach ( $demo_examples as $res_id => $res_options ) {
572 $search_options_arr[ $res_id ] = $res_options;
573 }
574 wpbc_searchable_resources__save_all_options( $search_options_arr );
575
576 }
577
578
579 /**
580 * DEPRECATED :: Hook action after creation new booking
581 * @param int $booking_id
582 * @param int $resource_id
583 * @param string $str_dates__dd_mm_yyyy - "30.02.2014, 31.02.2014, 01.03.2014"
584 * @param array $times_array - array($start_time, $end_time )
585 * @param string $booking_form
586 function your_cust_func_add_new_booking( $booking_id, $resource_id, $str_dates__dd_mm_yyyy, $times_array , $booking_form ) {
587
588 }
589 add_action( 'show_payment_forms__for_ajax', 'your_cust_func_add_new_booking', 100, 5 );
590 *
591 Previously: add_action( 'wpdev _new_booking', 'your_cust_func_add_new_booking', 100, 5 );
592 */
593
594 /**
595 * NEW :: Hook action after creation / edit bookings
596 *
597 * How to use this hook?
598 *
599 * Add code similar to this in your functions.php file in your theme, or in some other php file:
600 *
601 * // Track adding new booking
602 * //
603 * // @param $params = array (
604 * 'str_dates__dd_mm_yyyy' => '08.10.2023,09.10.2023,10.10.2023,11.10.2023',
605 * 'booking_id' => 254,
606 * 'resource_id' => 11, // child or parent or single
607 * 'initial_resource_id' => 2, // parent or single
608 * 'form_data' => 'text^selected_short_dates_hint11^Sun...',
609 * 'times_array' => array ( array ( '14', '00', '01' ), array( '12', '00', '02' ) ),
610 * 'is_edit_booking' => 0,
611 * 'custom_form' => '',
612 * 'is_duplicate_booking' => 0,
613 * 'is_from_admin_panel' => false,
614 * 'is_show_payment_form' => 1
615 * )
616 * function my_booking_tracking( $params ){
617 * // Your code here
618 * ?><!-- Google Code for Booking Conversion Page -->
619 * <script type="text/javascript">
620 * // Insert bellow your Google Conversion Code
621 * </script><?php
622 * }
623 * add_action( 'wpbc_track_new_booking', 'my_booking_tracking' );
624 *
625 *
626 *
627 * Useful hook booking edit tracking
628 *
629 * Add code similar to this in your functions.php file in your theme, or in some other php file:
630 *
631 * // Track edit existing booking
632 * //
633 * // @param $params = array (
634 * 'str_dates__dd_mm_yyyy' => '08.10.2023,09.10.2023,10.10.2023,11.10.2023',
635 * 'booking_id' => 254,
636 * 'resource_id' => 11, // child or parent or single
637 * 'initial_resource_id' => 2, // parent or single
638 * 'form_data' => 'text^selected_short_dates_hint11^Sun...',
639 * 'times_array' => array ( array ( '14', '00', '01' ), array( '12', '00', '02' ) ),
640 * 'is_edit_booking' => 1,
641 * 'custom_form' => '',
642 * 'is_duplicate_booking' => 0,
643 * 'is_from_admin_panel' => false,
644 * 'is_show_payment_form' => 1
645 * )
646 * function my_edit_booking_tracking( $params ){
647 * // Your code here
648 * ?><!-- Google Code for Booking Conversion Page -->
649 * <script type="text/javascript">
650 * // Insert bellow your Google Conversion Code
651 * </script><?php
652 * }
653 * add_action( 'wpbc_track_edit_booking', 'my_edit_booking_tracking' );
654 */
655
656 /**
657 * Hook action after approving of booking: do_action( 'wpbc_booking_approved' , $booking_id , $is_approved_dates );
658 * @param int/string $booking_id - can be '1' or 99 or comma separated ID of bookings: '10,22,45'
659 * @param int/string $is_approved_dates - '1' | '0' | 1 | 0 1 -approved, 0 - pending
660 function your_cust_func_wpbc_booking_approved( $booking_id, $is_approved_dates ) {
661
662 }
663 add_action( 'wpbc_booking_approved', 'your_cust_func_wpbc_booking_approved', 100, 2 ); // FixIn: 8.7.6.1.
664 */
665
666 /**
667 * Hook action after trash of booking:
668 * do_action( 'wpbc_booking_trash', $booking_id, $is_trash ); // FixIn: 8.7.6.2.
669 */
670
671 /**
672 * Hook action after delete of booking:
673 * do_action( 'wpbc_booking_delete', $approved_id_str ); // FixIn: 8.7.6.3.
674 */
675
676 /**
677 * Hooks in new Booking Listing page for different actions:
678 * // FixIn: 9.5.3.3.
679 do_action( 'wpbc_set_booking_locale', $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
680 do_action( 'wpbc_set_booking_pending', $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
681 do_action( 'wpbc_set_booking_approved' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
682 do_action( 'wpbc_move_booking_to_trash' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
683 do_action( 'wpbc_restore_booking_from_trash' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
684 do_action( 'wpbc_delete_booking_completely' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
685 do_action( 'wpbc_set_booking_as_read' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
686 do_action( 'wpbc_set_booking_as_unread' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
687
688 do_action( 'wpbc_set_booking_note' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
689 do_action( 'wpbc_change_booking_resource' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
690
691 do_action( 'wpbc_set_payment_status' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
692 do_action( 'wpbc_set_booking_cost' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
693 do_action( 'wpbc_send_payment_request' $params, $action_result ); // where $params is array, which contain $params['booking_id'], and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
694 do_action( 'wpbc_import_google_calendar' $params, $action_result ); // where $params is array and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
695 do_action( 'wpbc_export_csv' $params, $action_result ); // where $params is array and $action_result array and have $action_result['after_action_result'], which contains a boolean value of the result of an operation.
696
697 */
698
699 /**
700 * Hook for adding new payment status(es) in Booking Listing page:
701 *
702 * apply_filters ('wpbc_filter_payment_status_list' , $payment_status_titles );
703 *
704 * Use this code in your functions.php file in your theme to add your own Payment status for Booking Listing page:
705 *
706 * function my_wpbc_filter_payment_status_list( $payment_status_titles ) {
707 * $payment_status_titles[ 'invoice' ] = ' Invoice Payment';
708 * return $payment_status_titles;
709 * }
710 * add_filter( 'wpbc_filter_payment_status_list', 'my_wpbc_filter_payment_status_list', 10, 1 );
711 */
712
713 /**
714 * Hook that executes upon the deletion of booking resources: do_action( 'wpbc_deleted_booking_resources', $bulk_action_arr_id ); // (10.0.0.35)
715 *
716 * Example:
717 * function my_func__on_resource_delete( $resources_id_str ){
718 * $resources_id_arr = explode( ',', $resources_id_str );
719 * // Do something ...
720 * }
721 * add_action( 'wpbc_deleted_booking_resources', 'my_func__on_resource_delete' );
722 */
723
724
725 /**
726 * Some exmaples of fucntions usage.
727 *
728 * @return void
729 */
730 function wpbc_create_a_new_booking_exmaple(){
731
732 // -----------------------------------------------------------------------------------------------------------------
733 // Example #1.
734 // -----------------------------------------------------------------------------------------------------------------
735 $booking = array(
736 'dates' => array( '2027-06-24', '2027-06-24', '2027-06-25', '2027-06-26' ),
737 'data' => array(
738 'secondname' => array( 'value' => 'Smith', 'type' => 'text' ),
739 'name' => 'Rika',
740 'email' => array( 'value' => 'rika@cost.com', 'type' => 'email' ),
741 )
742 );
743 $booking_id = wpbc_api_booking_add_new( $booking['dates'], $booking['data'] );
744
745 // -----------------------------------------------------------------------------------------------------------------
746 // Example #2. - Add bew booking, without checking if the dates/times are available in source booking resource.
747 // -----------------------------------------------------------------------------------------------------------------
748 $booking = array(
749 'dates' => array( '2027-08-24', '2027-08-25', '2027-08-26' ),
750 'data' => array(
751 'rangetime' => array( 'value' => '14:00 - 16:00', 'type' => 'selectbox-one' ), // Define booked time-slot.
752 'name' => 'John', // Simple configuration.
753 'secondname' => array( 'value' => 'Smith', 'type' => 'text' ), // Advanced configuration.
754 'email' => array( 'value' => 'John@cost.com', 'type' => 'email' ),
755 ),
756 'resource_id' => 3,
757 'options' => array(
758 'save_booking_even_if_unavailable' => 1, // [0 | 1] if 1 - skip checking if the dates/times are available.
759 ),
760 );
761 $booking_id = wpbc_api_booking_add_new( $booking['dates'], $booking['data'], $booking['resource_id'], $booking['options'] );
762
763 // -----------------------------------------------------------------------------------------------------------------
764 // Example #3. - Check if time slot on specific date available.
765 // -----------------------------------------------------------------------------------------------------------------
766 $booking = array(
767 'dates' => array( '2027-08-24 15:00:01', '2027-08-24 16:00:02' ),
768 'resource_id' => 3,
769 'params' => array( 'is_use_booking_recurrent_time' => false ),
770 );
771 $is_dates_booked = wpbc_api_is_dates_booked( $booking['dates'], $booking['resource_id'], $booking['params'] );
772 if ( $is_dates_booked ) {
773 // Booked.
774 } else {
775 // Available.
776 }
777
778 // -----------------------------------------------------------------------------------------------------------------
779 // Example #4. - Check if dates available.
780 // -----------------------------------------------------------------------------------------------------------------
781 $booking = array(
782 'dates' => array( '2027-08-24 00:00:00', '2027-08-25 00:00:00', '2027-08-26 00:00:00' ),
783 'resource_id' => 3,
784 'params' => array( 'is_use_booking_recurrent_time' => false ),
785 );
786 $is_dates_booked = wpbc_api_is_dates_booked( $booking['dates'], $booking['resource_id'], $booking['params'] );
787 if ( $is_dates_booked ) {
788 // Booked.
789 } else {
790 // Available.
791 }
792
793 }