Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • BKAP
    • Addons
    • Admin-Bookings
    • Attribute-Lockout
    • Block-Pricing
    • Booking-Box
    • Booking-Confirmation
    • Booking-Process
    • Cancel-Order
    • Cart
    • Checkout-Process
    • Core
    • Date-Time
    • Emails
    • Functions
    • Global-Function
    • Global-Settings
    • Google-Calendar-Sync
    • ICS
    • Import-Booking
    • Labels
    • License
    • Menus
    • Meta-Boxes
    • Reschedule
    • Resources
    • Search-Widget
    • Special-Price
    • Uninstall
    • Validation
    • Variation-Lockout
    • Vendors
      • Dokan
      • WC-Vendors
    • View-Bookings
  • None

Classes

  • bkap_addon_compatibility_class
  • bkap_admin_bookings
  • bkap_approve_booking
  • bkap_attributes
  • bkap_block_booking
  • BKAP_Booking
  • bkap_booking_box_class
  • bkap_booking_confirmation
  • bkap_booking_process
  • BKAP_Bookings_View
  • bkap_calendar_sync
  • bkap_cancel_order
  • bkap_cart
  • bkap_checkout
  • bkap_common
  • BKAP_Customer_Meta_Box
  • BKAP_Details_Meta_Box
  • bkap_dokan_calendar_class
  • bkap_dokan_class
  • bkap_dokan_orders_class
  • bkap_dokan_products_class
  • bkap_edit_bookings_class
  • BKAP_Email_Booking_Cancelled
  • BKAP_Email_Booking_Confirmed
  • BKAP_Email_Booking_Rescheduled_Admin
  • BKAP_Email_Imported_Event
  • BKAP_Email_Manager
  • BKAP_Email_New_Booking
  • BKAP_Gcal
  • BKAP_Gcal_Event
  • bkap_gcal_sync_settings
  • bkap_global_settings
  • BKAP_Google_Events_View
  • bkap_ics
  • bkap_license
  • bkap_load_scripts_class
  • BKAP_OPC_Addon
  • BKAP_Payment_Gateway
  • BKAP_Product_Resource
  • bkap_rescheduled_order_class
  • BKAP_Resource_Details_Meta_Box
  • BKAP_Save_Meta_Box
  • bkap_special_booking_price
  • bkap_timeslot_price
  • bkap_validation
  • bkap_variations
  • BKAP_Vendors
  • bkap_wc_vendors
  • bkap_wcv_product
  • Booking_Information_In_Voucher_Template
  • Class_Bkap_Product_Resource
  • Custom_WooCommerce_Widget_Product_Search
  • EDD_BOOK_Plugin_Updater
  • global_menu
  • import_bookings
  • product_gcal_settings
  • view_bookings
  • WAPBK_Import_Bookings_Table
  • WAPBK_View_Bookings_Table
  • woocommerce_booking

Functions

  • addDayswithdate
  • bkap_400_create_meta
  • bkap_400_recurring_data
  • bkap_400_update_db_tab
  • bkap_400_update_enable_specific
  • bkap_400_update_enable_week_blocking
  • bkap_400_update_holidays
  • bkap_400_update_ranges
  • bkap_400_update_recurring_lockout
  • bkap_400_update_settings
  • bkap_400_update_special
  • bkap_400_update_specific
  • bkap_410_create_meta
  • bkap_410_update_block_pricing_option_values_in_new_way
  • bkap_410_update_fixed_blocks
  • bkap_410_update_price_ranges
  • bkap_410_update_settings
  • bkap_420_add_gcal_posts
  • bkap_420_add_gcal_tour_posts
  • bkap_420_gcal_batch_size
  • bkap_420_gcal_meta
  • bkap_420_update_settings
  • bkap_add_days_to_date
  • bkap_array_of_given_date_range
  • bkap_booked_resources
  • bkap_booking_posts_for_resource
  • bkap_calculate_bookings_for_resource
  • bkap_check_booking_available
  • bkap_check_day_booking_available
  • bkap_check_for_max_date
  • bkap_check_for_min_date
  • bkap_check_holiday
  • bkap_check_in_custom_holiday_range
  • bkap_check_resource_available
  • bkap_check_resource_booked_in_date_range
  • bkap_db_420_1
  • bkap_db_420_2
  • bkap_delete_event_from_gcal
  • bkap_delete_folder_and_files
  • bkap_delete_options
  • bkap_delete_tables
  • bkap_find_matching_product_variation
  • bkap_generate_bookable_data
  • bkap_get_book_arrays
  • bkap_get_book_t
  • bkap_get_booked
  • bkap_get_booked_checkout
  • bkap_get_custom_posts
  • bkap_get_dateToCal
  • bkap_get_day_between_Week
  • bkap_get_db_count
  • bkap_get_escapeString
  • bkap_get_lockout
  • bkap_get_month_range
  • bkap_get_post_meta
  • bkap_get_posted_availability
  • bkap_get_total_timeslot_maximum_booking
  • bkap_get_week_range
  • bkap_import_events_cron
  • bkap_insert_event_to_gcal
  • bkap_manual_db_update_v420
  • bkap_orderby_date_key
  • bkap_save_resources
  • bkap_sort_by_from_time
  • bkap_update_po_file
  • bkap_vendors_get_bookings
  • bkap_widgets_init
  • bkapSortByPriority
  • calback_bkap_max_date
  • check_in_fixed_block_booking
  • check_in_range
  • check_in_range_abp
  • check_in_range_holidays
  • check_in_range_weekdays
  • create_posts
  • get_availability_for_date
  • get_booked_multiple
  • get_bookings_for_date
  • get_bookings_for_range
  • get_date_lockout
  • get_slot_availability
  • get_slot_lockout
  • get_standard_decimal_thousand_separator_price
  • is_booking_active
  • woocommerce_bkap_add_cron_schedule
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 
<?php
/**
 * Booking & Appointment Plugin for WooCommerce BKAP_Gcal_Event.
 *
 * Handler for adding imported events as a post.
 *
 * @version  4.2.0
 * @category Class
 * @author   Tyche Softwares
 * @package  BKAP/Google-Calendar-Sync
 * @class    BKAP_Gcal_Event
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'woocommerce_booking' ) ) {
    
}

/**
 * Handler for adding imported events as a post.
 *
 * @class    BKAP_Gcal_Event
 */
class BKAP_Gcal_Event {
    
    /** @public int */
    public $id;
    
    /** @public bool */
    public $populated;
    
    /** @public object */
    public $post;
    
    /** @public string */
    public $status;
    
    /**
     * Constructor, possibly sets up with post or id belonging to existing booking
     * or supplied with an array to construct a new booking
     * 
     * @param int/array/obj $gcal_event
     * @since 4.2.0
     */
    public function __construct( $gcal_event = false ) {
        
        if ( is_array( $gcal_event ) ) {
            $this->event_data = $gcal_event;
            $populated = false;
        }else if ( is_int( intval( $gcal_event ) ) && 0 < $gcal_event ) {
            $populated = $this->populate_data( $gcal_event );
        }
        
        $this->populated = $populated;
        
    }
    
    /**
     * Populate the data with the id of the event provided
     * Will query for the post belonging to this event and store it
     * 
     * @param int $event_id
     * @since 4.2.0
     */
    public function populate_data( $event_id ) {
        
        if ( ! isset( $this->post ) ) {
            $post = get_post( $event_id );
        }
    
        if ( is_object( $post ) ) {
            // We have the post object belonging to this booking, now let's populate
            $this->id            = $post->ID;
            $this->booking_date  = $post->post_date;
            $this->modified_date = $post->post_modified;
            $this->customer_id   = $post->post_author;
            $this->custom_fields = get_post_meta( $this->id );
            $this->status        = $post->post_status;
            $this->order_id      = $post->post_parent;
            
            // Define the data we're going to load: Key => Default value
            $load_data = array(
                'user_id'           => 0,
                'product_id'        => '',
                'start'             => '',
                'end'               => '',
                'uid'               => '',
                'summary'           => '',
                'description'       => '',
                'location'          => '',
                'resource_id'       => '',
                'persons'           => array(),
                'qty'               => 1
            );
    
            // Load the data from the custom fields (with prefix for this plugin )
            $meta_prefix = '_bkap_';
            
            foreach ( $load_data as $key => $default ) {
                
                if ( isset( $this->custom_fields[ $meta_prefix . $key ][0] ) && $this->custom_fields[ $meta_prefix . $key ][0] !== '' ) {
                    $this->$key = maybe_unserialize( $this->custom_fields[ $meta_prefix . $key ][0] );
                } else {
                    $this->$key = $default;
                }
            }
    
            // Start and end date converted to timestamp
            $this->start = $this->start;
            $this->end   = $this->end;
    
            // Save the post object itself for future reference
            $this->post = $post;
            return true;
        }
    
        return false;
    }
    
    /**
     * Actual create for the new event
     * 
     * @param string Status for new event
     * @since 4.2.0
     */    
    public function create( $status = 'bkap-unmapped' ) {
        
        $this->new_booking( $status, $this->event_data );
    }
    
    
    /**
     * Makes the new event.
     * 
     * @param string $status The status for this new event
     * @param array $event_data Array with all the new event data
     * 
     * @since 4.2.0
     */
    private function new_booking( $status, $event_data ) {
        
        $event_data = wp_parse_args( $event_data, array(
            'user_id'           => 1,
            'product_id'        => '',
            'start_date'        => '',
            'end_date'          => '',
            'uid'               => '',
            'summary'           => '',
            'description'       => '',
            'location'          => '',
            'reason_of_fail'    => '',
            'event_option_name' => '',
            'resource_id'       => '',
            'persons'           => array(),
            'qty'               => 1
        ) );
        
        $booking_event_data = array(
            'post_type'   => 'bkap_gcal_event',
            'post_title'  => sprintf( __( 'Google Event &ndash; %s', 'woocommerce-booking' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Booking date parsed by strftime', 'woocommerce-booking' ) ) ),
            'post_status' => $status,
            'ping_status' => 'closed',
            'post_author' => $event_data['user_id']
        );
        
        $this->id = wp_insert_post( $booking_event_data );
        
        // Setup the required data for the current user
        if ( ! $event_data['user_id'] ) {
            if ( is_user_logged_in() ) {
                $event_data['user_id'] = get_current_user_id();
            } else {
                $event_data['user_id'] = 0;
            }
        }
        
        $event_meta_args = array(
            '_bkap_user_id'       => $event_data['user_id'],
            '_bkap_product_id'    => $event_data['product_id'],
            '_bkap_resource_id'   => $event_data['resource_id'],
            '_bkap_persons'       => $event_data['persons'],
            '_bkap_start'         => $event_data['start_date'],
            '_bkap_end'           => $event_data['end_date'],
            '_bkap_uid'           => $event_data['uid'],
            '_bkap_summary'       => $event_data['summary'],
            '_bkap_description'   => $event_data['description'],
            '_bkap_location'      => $event_data['location'],
            '_bkap_reason_of_fail'=> $event_data['reason_of_fail'],
            '_bkap_customer_id'   => $event_data['user_id'],
            '_bkap_qty'           => $event_data['qty'],
            '_bkap_variation_id'  => $event_data['variation_id'],
            '_bkap_event_option_name'  => $event_data['event_option_name'],
            
        );
        
        foreach ( $event_meta_args as $key => $value ) {
            update_post_meta( $this->id, $key, $value );
        }
    }
    
    /**
     * Returns the status of this booking
     * 
     * @param Bool to ask for pretty status name (if false)
     * @return String of the booking status
     * @since 4.2.0
     */
    public function get_status( $raw = true ) {
        
        if ( $this->populated ) {
            if ( $raw ) {
                return $this->status;
            } else {
                $status_object = get_post_status_object( $this->status );
                return $status_object->label;
            }
        }
    
        return false;
    }
    
    /**
     * Returns the Start Date
     * @return Start Date as strtotime
     * 
     * @since 4.2.0
     */
    
    function get_start() {
        return $start = get_post_meta( $this->id, '_bkap_start', true );
    }
    
    /**
     * Returns the End Date
     * 
     * @return End Date as strtotime
     * @since 4.2.0
     */
    function get_end() {
        return $end = get_post_meta( $this->id, '_bkap_end', true );
    }
    
    /**
     * Returns the Time Slot
     * 
     * @return Time Slot as H:i
     * @since 4.2.0
     */
    function get_time() {
        $global_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
        $time_format = $global_settings->booking_time_format;
    
        // Commenting since we need 24 hour format for comparision
        //$time_format = ( $time_format === '12' ) ? 'h:i A' : 'H:i';
        $time_format = 'H:i';
        
        $start_time = date( $time_format, strtotime( $this->get_start() ) );
    
        $end_time = date( $time_format, strtotime( $this->get_end() ) );
    
        return "$start_time - $end_time";
    }
    
    /**
     * Returns the Start Date
     * 
     * @return Date in the format set in Booking->Settings->Date Format
     * @since 4.2.0
     */
    function get_start_date() {
    
        $start_date = '';
        $start = $this->get_start();
    
        $global_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
        $date_formats       = bkap_get_book_arrays( 'bkap_date_formats' );
        // get the global settings to find the date formats
        $global_settings    = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
        $date_format_set    = $date_formats[ $global_settings->booking_date_format ];
        
        if( $start ){
            $start_date = date( $date_format_set, $start );
        }
        
        return $start_date;
    
    }
    
    /**
     * Returns the End Date
     * 
     * @return Date in the format set in Booking->Settings->Date Format
     * @since 4.2.0
     */
    function get_end_date() {
    
        $end_date = '';
    
        $start = $this->get_start();
        $end = $this->get_end();
    
        if ( $start !== $end && !is_null( $end ) ) {
    
            $global_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
            $date_formats       = bkap_get_book_arrays( 'bkap_date_formats' );
            // get the global settings to find the date formats
            $global_settings    = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
            $date_format_set    = $date_formats[ $global_settings->booking_date_format ];
            $end_date = date( $date_format_set, $end );
        }
    
        return $end_date;
    }
    
    /**
     * Returns Start Time
     * 
     * @return Start Time in the format set in Booking->Settings->Time Format
     * @since 4.2.0
     */
    function get_start_time() {
    
        $start_time = '';
        $global_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
        $time_format    = $global_settings->booking_time_format;
        $time_format    = ( $time_format === '12' ) ? 'h:i A' : 'H:i';
        $tdif           = $this->get_tdif();
        $start          = $this->get_start();
        
        $start_time_timestamp    = $start + $tdif;
        
        if ( $start ) {
            $start_time = date( $time_format, $start_time_timestamp );
        }
    
        return $start_time;
    }
    
    /**
     * Returns Start Time
     * 
     * @return Start Time in the format set in Booking->Settings->Time Format
     * @since 4.2.0
     */
    function get_end_time() {
    
        $end_time = '';
        $global_settings = json_decode( get_option( 'woocommerce_booking_global_settings' ) );
    
        $time_format            = $global_settings->booking_time_format;
        $time_format            = ( $time_format === '12' ) ? 'h:i A' : 'H:i';
        
        $tdif                   = $this->get_tdif();        
        $end                    = $this->get_end();
        
        $end_time_timestamp     = $end + $tdif;
        
        if( $end ){
            $end_time = date( $time_format, $end_time_timestamp );
        }
    
        return $end_time;
    }
    
    /**
     * Calculate the time difference for UTC and the TimeZone set for the website.
     *
     * @return Time Difference $tdif
     * @since 4.2.0
     */
    function get_tdif(){
        
        if ( !current_time( 'timestamp' ) ) {
            $tdif = 0;
        } else {
            $tdif = current_time( 'timestamp' ) - time();
        }
        
        return $tdif;
        
    }
    
    /**
     * Failed reason of the mapping of event
     *
     * @return string $failed_reason
     * @since 4.2.0
     */
    function get_failed_reason() {
        return $failed_reason = get_post_meta( $this->id, '_bkap_reason_of_fail', true );
    }
    
    /**
     * Set the new status for this event
     * 
     * @param string $status
     * @return bool
     */
    public function update_status( $status ) {
        
        $current_status   = $this->get_status( true );
        $allowed_statuses = bkap_common::get_bkap_event_statuses();
        
        if ( $this->populated ) {
             
            if ( array_key_exists( $status, $allowed_statuses ) ) {
                wp_update_post( array( 'ID' => $this->id, 'post_status' => $status ) );
    
                // Trigger actions
                do_action( 'bkap_post_' . $current_status . '_to_' . $status, $this->id );
                do_action( 'bkap_post_' . $status, $this->id );
                
                return true;
            }
        }
    
        return false;
    }
}
?>
API documentation generated by ApiGen