| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @description Booking Calendar integration - Support Functions |
| 5 |
* |
| 6 |
* @author wpdevelop |
| 7 |
* @link https://oplugins.com/ |
| 8 |
* @email info@oplugins.com |
| 9 |
* |
| 10 |
* @modified 2017-06-28 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
|
| 16 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 |
// S u p p o r t Functions for integration with Booking Calendar |
| 18 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 |
|
| 20 |
|
| 21 |
// Final Class for Creation new Bookings into Booking Calendar |
| 22 |
final class WPBM_create_bookings_from_events { |
| 23 |
|
| 24 |
static private $instance = NULL; |
| 25 |
|
| 26 |
public static $current_ics_dates = array(); // unlike a const, static property values can be changed |
| 27 |
|
| 28 |
|
| 29 |
/** Init this class only once |
| 30 |
* |
| 31 |
* @return obj |
| 32 |
*/ |
| 33 |
public static function init() { |
| 34 |
|
| 35 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPBM_create_bookings_from_events ) ) { |
| 36 |
self::$instance = new WPBM_create_bookings_from_events; |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** Get array of assigned form fields from the Booking Calendar plugin |
| 43 |
* |
| 44 |
* @return array array( |
| 45 |
[0] => Array ( |
| 46 |
[ics_field_name] => title |
| 47 |
[type] => text |
| 48 |
[name] => name |
| 49 |
) |
| 50 |
[1] => Array ( |
| 51 |
[ics_field_name] => description |
| 52 |
[type] => textarea |
| 53 |
[name] => details |
| 54 |
) |
| 55 |
) |
| 56 |
*/ |
| 57 |
public static function get_assigned_form_fields() { |
| 58 |
|
| 59 |
if ( ! function_exists( 'get_bk_option' ) ) return false; |
| 60 |
|
| 61 |
$wpbc_assigned_form_fields = get_bk_option( 'booking_gcal_events_form_fields' ); |
| 62 |
$wpbc_assigned_form_fields = maybe_unserialize( $wpbc_assigned_form_fields ); |
| 63 |
/** $wpbc_assigned_form_fields = array( |
| 64 |
[title] => text^name |
| 65 |
[description] => customform^textarea^details |
| 66 |
[where] => text^ |
| 67 |
) |
| 68 |
*/ |
| 69 |
|
| 70 |
|
| 71 |
$assigned_fields_arr = array(); |
| 72 |
|
| 73 |
if ( is_array( $wpbc_assigned_form_fields ) ) { //FixIn: 2.0.9.2 |
| 74 |
foreach ( $wpbc_assigned_form_fields as $assign_ics_field_name => $wpbc_field_str ) { |
| 75 |
|
| 76 |
$wpbc_field_arr = explode( '^', $wpbc_field_str ); |
| 77 |
$wpbc_field_arr_count = count( $wpbc_field_arr ) - 1; |
| 78 |
|
| 79 |
if ( ! empty( $wpbc_field_arr[ $wpbc_field_arr_count ] ) ) { |
| 80 |
$assigned_fields_arr[] = array( |
| 81 |
'ics_field_name' => $assign_ics_field_name |
| 82 |
, 'type' => $wpbc_field_arr[ ( $wpbc_field_arr_count - 1 ) ] |
| 83 |
, 'name' => $wpbc_field_arr[ $wpbc_field_arr_count ] |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
/** $assigned_fields_arr = array( |
| 90 |
[0] => Array ( |
| 91 |
[ics_field_name] => title |
| 92 |
[type] => text |
| 93 |
[name] => name |
| 94 |
) |
| 95 |
[1] => Array ( |
| 96 |
[ics_field_name] => description |
| 97 |
[type] => textarea |
| 98 |
[name] => details |
| 99 |
) |
| 100 |
) |
| 101 |
*/ |
| 102 |
return $assigned_fields_arr; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public static function set_ics_dates( $value ) { |
| 107 |
|
| 108 |
self::$current_ics_dates = $value; |
| 109 |
} |
| 110 |
|
| 111 |
public static function get_ics_dates() { |
| 112 |
return self::$current_ics_dates; |
| 113 |
} |
| 114 |
|
| 115 |
public static function erase_ics_dates() { |
| 116 |
self::$current_ics_dates = array(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
} |
| 122 |
WPBM_create_bookings_from_events::init(); // Initial Start |
| 123 |
|
| 124 |
|
| 125 |
/** Hook for overwriting saving dates |
| 126 |
* |
| 127 |
* // From: ..\wp-content\plugins\booking\core\wpbc-dates.php |
| 128 |
* // apply_filters( 'wpbc_get_insert_sql_for_dates', $is_return_dates, $dates_in_diff_formats , $is_approved_dates, $booking_id, $is_return_only_array ); |
| 129 |
* |
| 130 |
* @param type $is_return_dates |
| 131 |
* @param type $dates_in_diff_formats |
| 132 |
* @param type $is_approved_dates |
| 133 |
* @param type $booking_id |
| 134 |
* @param type $is_return_only_array |
| 135 |
* @return type |
| 136 |
*/ |
| 137 |
function wpbm_get_insert_sql_for_dates( $is_return_dates, $dates_in_diff_formats , $is_approved_dates, $booking_id, $is_return_only_array ) { |
| 138 |
|
| 139 |
$dates_to_insert = WPBM_create_bookings_from_events::get_ics_dates(); |
| 140 |
|
| 141 |
if ( empty( $dates_to_insert ) ) |
| 142 |
return $is_return_dates; |
| 143 |
|
| 144 |
$insert_arr = array(); |
| 145 |
$insert = array(); |
| 146 |
|
| 147 |
foreach ( $dates_to_insert as $date) { |
| 148 |
|
| 149 |
// Loop |
| 150 |
$insert_arr []= array( $booking_id, $date, $is_approved_dates ); |
| 151 |
$insert []= "('$booking_id', '$date', '$is_approved_dates' )"; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
$insert = implode( ',', $insert ); |
| 156 |
|
| 157 |
//debuge($booking_id, $insert); |
| 158 |
|
| 159 |
if ( $is_return_only_array ) { |
| 160 |
return $insert_arr; |
| 161 |
} else { |
| 162 |
return $insert; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/** Hook for overwriting updating dates. Usually used for do not allow to make this updating. |
| 169 |
* |
| 170 |
* // From: ..\wp-content\plugins\booking\inc\_bl\biz_l.php |
| 171 |
* // $is_continue = apply_filters( 'wpbc_is_reupdate_dates_to_child_resources', $is_exit, $booking_id, $bktype, $dates, $start_end_time_arr , $formdata, $skip_page_checking_for_updating ); |
| 172 |
* |
| 173 |
* @param type $is_exit |
| 174 |
* @param type $booking_id |
| 175 |
* @param type $bktype |
| 176 |
* @param type $dates |
| 177 |
* @param type $start_end_time_arr |
| 178 |
* @param type $formdata |
| 179 |
* @param type $skip_page_checking_for_updating |
| 180 |
* @return boolean |
| 181 |
*/ |
| 182 |
function wpbm_is_reupdate_dates_to_child_resources( $is_exit, $booking_id, $bktype, $dates, $start_end_time_arr , $formdata, $skip_page_checking_for_updating ) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
/** Getting only Booking fields ( starting '_BOOKING' ) from Parsed ICS array |
| 189 |
* - its support helpful function |
| 190 |
* |
| 191 |
* @param type $ics_array |
| 192 |
* @return type |
| 193 |
*/ |
| 194 |
function wpbm_get_booking_fields_from_ics_array( $ics_array ) { |
| 195 |
|
| 196 |
$booking_array = array(); |
| 197 |
|
| 198 |
foreach ( $ics_array as $ics_n => $ics_event_arr ) { |
| 199 |
|
| 200 |
$one_booking = array(); |
| 201 |
|
| 202 |
foreach ( $ics_event_arr as $event_field_key => $event_field_value ) { |
| 203 |
|
| 204 |
$is_booking_field = strpos( $event_field_key, '_BOOKING' ); |
| 205 |
if ( $is_booking_field !== false ) { |
| 206 |
$one_booking[ $event_field_key ] = $event_field_value; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ( ! empty( $one_booking ) ) { |
| 211 |
$booking_array[] = $one_booking; |
| 212 |
} |
| 213 |
} |
| 214 |
return $booking_array; |
| 215 |
} |