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  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 
<?php 
/**
 * Bookings and Appointment Plugin for WooCommerce
 *
 * Class for handling 2 way Google Calendar Sync
 *
 * @author   Tyche Softwares
 * @package  BKAP/Google-Calendar-Sync
 * @category Classes
 */

// Add a new interval for import cron schedule  
add_filter( 'cron_schedules', 'woocommerce_bkap_add_cron_schedule' );

/**
 * Adds the Booking & Appointment Import Cron Job
 *
 * @param array $schedules - List of cron jobs to be run with their intervals
 * @since 2.6.3
 */

function woocommerce_bkap_add_cron_schedule( $schedules ) {
    
    $duration = get_option( 'bkap_cron_time_duration' );
    
    if ( isset( $duration ) && $duration > 0 ) {
        $duration_in_seconds = $duration * 60;
    } else {
        $duration_in_seconds = 86400;
    }
    
    $schedules['bkap_gcal_import'] = array(
        'interval' => $duration_in_seconds, // import duration in seconds
        'display'  => __( 'Booking & Appointment - GCal Import Events' ),
    );
    
    return $schedules;
}

/**
 * Schedule an action if it's not already scheduled for GCal Import
 * 
 * @since 2.6.3
 */

if ( ! wp_next_scheduled( 'woocommerce_bkap_import_events' ) ) {
    wp_schedule_event( time(), 'bkap_gcal_import', 'woocommerce_bkap_import_events' );
}

/**
 * Setup the Import of events from Google Calendar
 * 
 * @since 2.6.3
 */

function bkap_import_events_cron() {
    $calendar_sync = new bkap_calendar_sync();
    $calendar_sync->bkap_setup_import();
}

add_action( 'woocommerce_bkap_import_events', 'bkap_import_events_cron' );


/**
 * Class for Google Calendar 2 way sync for bookings
 *
 * @class bkap_calendar_sync
 */

class bkap_calendar_sync {

    /**
     * Default constructor
     *
     * @since 2.6.3
     */

    public function __construct() {

        $this->gcal_api = false;
        $this->email_ID = '';
        
        add_action( 'wp_loaded',                                            array( $this, 'bkap_setup_gcal_sync' ), 10 );
        add_action( 'admin_init',                                           array( $this, 'bkap_setup_gcal_sync' ), 10 );
        
        $this->plugin_dir = plugin_dir_path( __FILE__ );
        $this->plugin_url = plugins_url( basename( dirname( __FILE__ ) ) );
        
        add_action( 'woocommerce_checkout_update_order_meta',               array( &$this, 'bkap_google_calendar_update_order_meta' ), 11 );
        add_action( 'woocommerce_order_item_meta_end',                      array( &$this, 'bkap_add_to_woo_pages' ), 11, 3 );
        
        // These 2 hooks have been added to figure out which email is being sent, the new order email (which goes to the admin) 
        // or the customer processing order, on hold order or ccompleted order ( which goes to the customer)
        add_filter( 'woocommerce_email_subject_new_order',                  array( &$this, 'bkap_new_order_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_processing_order',  array( &$this, 'bkap_customer_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_on_hold_order',     array( &$this, 'bkap_customer_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_completed_order',   array( &$this, 'bkap_customer_email' ), 10, 1 );
        
        if( get_option( 'bkap_add_to_calendar_customer_email' ) == 'on' ) {
            add_action( 'woocommerce_order_item_meta_end',                  array( &$this, 'bkap_add_to_calendar_customer'), 12, 3 );
        }
        
        if( get_option( 'bkap_admin_add_to_calendar_email_notification' ) == 'on' && get_option( 'bkap_calendar_sync_integration_mode' ) == 'manually' ) {
            add_action( 'woocommerce_order_item_meta_end',                  array( &$this, 'bkap_add_to_calendar_admin'), 13, 3 );
        }
        
        add_action( 'wp_ajax_bkap_save_ics_url_feed',                       array( &$this, 'bkap_save_ics_url_feed' ) );
        add_action( 'wp_ajax_bkap_delete_ics_url_feed',                     array( &$this, 'bkap_delete_ics_url_feed' ) );
        add_action( 'wp_ajax_bkap_import_events',                           array( &$this, 'bkap_setup_import' ) );
        add_action( 'wp_ajax_bkap_admin_booking_calendar_events',           array( &$this, 'bkap_admin_booking_calendar_events' ) );
        
        require_once $this->plugin_dir . 'includes/iCal/SG_iCal.php';
    }

    /**
     * Set the global parameter to email subject
     * Used to identify whether the WooCommerce email is being fired for the admin or the customer.
     *
     * @hook woocommerce_email_subject_new_order
     * @since 2.6
     */

    function bkap_new_order_email( $subject ) {
        $this->email_ID = 'new_order';
        return $subject;
    }

    /**
     * Set the global parameter to email subject
     * Used to identify whether the WooCommerce email is being fired for the admin or the customer.
     *
     * @hook woocommerce_email_subject_customer_processing_order
     *       woocommerce_email_subject_customer_on_hold_order
     *       woocommerce_email_subject_customer_completed_order
     *
     * @since 2.6
     */

    function bkap_customer_email( $subject ) {
        $this->email_ID = 'customer_order';
        return $subject;
    }

    /**
     * Adds the GCal class file
     *
     * @hook admin_init - for admin side
     *       wp_loaded - for front end
     * @since 2.6
     */

    function bkap_setup_gcal_sync () {
        // GCal Integration
        $this->gcal_api = false;
        // Allow forced disabling in case of emergency
        require_once $this->plugin_dir . '/includes/class.gcal.php';
        $this->gcal_api = new BKAP_Gcal();
    }

    /**
     * This function adds booking to the Google Calendar if automated sync is enabled.
     *
     * @param integer $order_id - Order ID for which bookings need to be synced
     * @hook woocommerce_checkout_update_order_meta
     * @since 2.6
     */

    function bkap_google_calendar_update_order_meta( $order_id ) {
        
        global $wpdb;
        
        $gcal = new BKAP_Gcal();
        
        $user = get_user_by( 'email', get_option( 'admin_email' ) );
        $admin_id = 0;
        if ( isset( $user->ID ) ) {
            $admin_id = $user->ID;
        } else {
            // get the list of administrators
            $args = array( 'role' => 'administrator', 'fields' => array( 'ID' ) );
            $users = get_users( $args );
            if ( isset( $users ) && count( $users ) > 0 ) {
                $admin_id = $users[ 0 ]->ID;
            }
        }
        $order_item_ids   =   array();
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $user_id = $admin_id;
        
            $post_id = bkap_common::bkap_get_product_id( $values[ 'product_id' ] );
            $is_bookable = bkap_common::bkap_get_bookable_status( $post_id );
        
            // get the mode for the product settings as well if applicable
            if( $gcal->get_api_mode( $admin_id, $post_id ) == "directly" ) {
            
                $sub_query        =   "";
                
                $booking_settings = get_post_meta( $post_id, 'woocommerce_booking_settings', true );
                // check if tour operators are allowed to setup GCal
                if ( 'yes' == get_option( 'bkap_allow_tour_operator_gcal_api' ) ) {
                    // if tour operator addon is active, pass the tour operator user Id else the admin ID
                    if ( function_exists( 'is_bkap_tours_active' ) && is_bkap_tours_active() ) {
                        
                        if ( $is_bookable ) {
                            
                            if( isset( $booking_settings[ 'booking_tour_operator' ] ) &&  $booking_settings[ 'booking_tour_operator' ] != 0 ) {
                                $user_id = $booking_settings[ 'booking_tour_operator' ];
                            }
                        }
                    }
                }
                
                // check if it's for the admin, else the tour operator addon will do the needful
                if ( isset( $values[ 'bkap_booking' ] ) && isset( $user_id ) && $user_id == $admin_id ) {
                    
                    $_data    =   $values[ 'data' ];
                    $_booking    =   $values[ 'bkap_booking' ][0];
                    
                    if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) {
                        $post_title = $_data->post->post_title;
                    } else {
                        $post_data = $_data->get_data();
                        $post_title = $post_data[ 'name' ];
                    }
                    // Fetch line item
                    if ( count( $order_item_ids ) > 0 ) {
                        $order_item_ids_to_exclude  = implode( ",", $order_item_ids );
                        $sub_query                  = " AND order_item_id NOT IN (".$order_item_ids_to_exclude.")";
                    }
                    
                    $query               =   "SELECT order_item_id,order_id FROM `".$wpdb->prefix."woocommerce_order_items`
                                      WHERE order_id = %s AND order_item_name = %s".$sub_query;
                    
                    $results             =   $wpdb->get_results( $wpdb->prepare( $query, $order_id, $post_title ) );
                    
                    if ( isset( $results[0] ) ) {
                        $order_item_ids[]    =   $results[0]->order_item_id;
                        
                        // check the booking status, if pending confirmation, then do not insert event in the calendar
                        $booking_status = wc_get_order_item_meta( $results[0]->order_item_id, '_wapbk_booking_status' );
                        
                        if ( ( isset( $booking_status ) && 'pending-confirmation' != $booking_status ) || ( ! isset( $booking_status )) ) {
    
                            // ensure it's a future dated event
                            $is_date_set = false;
                            if ( isset( $_booking[ 'hidden_date' ] ) ) {
                                $day = date( 'Y-m-d', current_time( 'timestamp' ) );
                                
                                if ( strtotime( $_booking[ 'hidden_date' ] ) >= strtotime( $day ) ) {
                                    $is_date_set = true;
                                } 
                            }
                            if ( $is_date_set ) {
                            
                                $event_details = array();
                                
                                $event_details[ 'hidden_booking_date' ] = $_booking[ 'hidden_date' ];
                                
                                if ( isset( $_booking[ 'hidden_date_checkout' ] ) && $_booking[ 'hidden_date_checkout' ] != '' ) {

                                    if ( isset( $booking_settings[ 'booking_charge_per_day' ] ) && 'on' == $booking_settings[ 'booking_charge_per_day' ] ) {
                                      
                                      $event_details[ 'hidden_checkout_date' ] = date( 'Y-m-d', strtotime( '+1 day', strtotime( $_booking[ 'hidden_date_checkout' ] ) ) );
                                   
                                    } else {

                                    $event_details[ 'hidden_checkout_date' ] = $_booking[ 'hidden_date_checkout' ];
                                    }
                                }
                                
                                $event_details[ 'billing_email' ] = $_POST[ 'billing_email' ];
                                $event_details[ 'billing_first_name' ] = $_POST[ 'billing_first_name' ];
                                $event_details[ 'billing_last_name' ] = $_POST[ 'billing_last_name' ];
                                $event_details[ 'billing_address_1' ] = $_POST[ 'billing_address_1' ];
                                $event_details[ 'billing_address_2' ] = $_POST[ 'billing_address_2' ];
                                $event_details[ 'billing_city' ] = $_POST[ 'billing_city' ];
                            
                                $event_details[ 'billing_phone' ] = $_POST[ 'billing_phone' ];
                                $event_details[ 'order_comments' ] = $_POST[ 'order_comments' ];
                                $event_details[ 'order_id' ] = $order_id;
                                
                                
                                if ( isset( $_POST[ 'shipping_first_name' ] ) && $_POST[ 'shipping_first_name' ] != '' ) {
                                    $event_details[ 'shipping_first_name' ] = $_POST[ 'shipping_first_name' ];
                                }
                                if ( isset( $_POST[ 'shipping_last_name' ] ) && $_POST[ 'shipping_last_name' ] != '' ) {
                                    $event_details[ 'shipping_last_name' ] = $_POST[ 'shipping_last_name' ];
                                }
                                if( isset( $_POST[ 'shipping_address_1' ] ) && $_POST[ 'shipping_address_1' ] != '' ) {
                                    $event_details[ 'shipping_address_1' ] = $_POST[ 'shipping_address_1' ];
                                }
                                if ( isset( $_POST[ 'shipping_address_2' ] ) && $_POST[ 'shipping_address_2' ] != '' ) {
                                    $event_details[ 'shipping_address_2' ] = $_POST[ 'shipping_address_2' ];
                                }
                                if ( isset( $_POST[ 'shipping_city' ] ) && $_POST[ 'shipping_city' ] != '' ) { 
                                    $event_details[ 'shipping_city' ] = $_POST[ 'shipping_city' ];
                                }
                                
                                $event_details[ 'product_name' ] = $post_title;
                                $event_details[ 'product_qty' ] = $values[ 'quantity' ];
                                
                                $event_details[ 'product_total' ] = $values[ 'line_total' ];
                                
                                // if sync is disabled at the product level, set post_id to 0 to ensure admin settings are taken into consideration
                                if ( ( ! isset( $booking_settings[ 'product_sync_integration_mode' ] ) ) || ( isset( $booking_settings[ 'product_sync_integration_mode' ] ) && 'disabled' == $booking_settings[ 'product_sync_integration_mode' ] ) ) {
                                    $post_id = 0;
                                }

                                $time_exploded = array();

                                // Checking for multiple timeslots
                                if ( isset( $_booking[ 'time_slot' ] ) && $_booking[ 'time_slot' ] != '' ) {
                                
                                    if ( strpos( $_booking[ 'time_slot' ], "<br>" ) !== false ){
                                        $time_exploded = explode("<br>", $_booking[ 'time_slot' ]);
                                        array_shift( $time_exploded );
                                    }else{
                                        $time_exploded = array();
                                        $time_exploded = $_booking[ 'time_slot' ];
                                    }
                                }

                                if ( is_array( $time_exploded ) && count( $time_exploded ) > 0 ){
                                    foreach ( $time_exploded as $key_time => $value_time ) {
                                        $event_details[ 'time_slot' ] = $value_time;
                                        $gcal->insert_event( $event_details, $results[0]->order_item_id, $user_id, $post_id, false );
                                    }
                                }else{
                                    $gcal->insert_event( $event_details, $results[0]->order_item_id, $user_id, $post_id, false );
                                }
                                
                                // add an order note, mentioning an event has been created for the item
                                $order = new WC_Order( $order_id );
                                
                                $order_note = __( "Booking_details for $post_title have been exported to the Google Calendar", 'woocommerce-booking' );
                                $order->add_order_note( $order_note );
                            }
                        }
                    }
                }
            }
     
        }
    }

    /**
     * Adds buttons in the WooCommerce customer emails
     * using which customers can add bookings into their calendars.
     *
     * @param integer $item_id - Item ID of the product
     * @param WC_Order_Item_Product $item - Item Object
     * @param WC_Order $order - Order Object
     *
     * @hook woocommerce_order_item_meta_end
     * @since 2.6
     */

    function bkap_add_to_calendar_customer( $item_id, $item, $order ) {
        if ( ! is_account_page() && ! is_wc_endpoint_url( 'order-received' ) ) {
            
            // check the email ID
            if ( 'customer_order' == $this->email_ID ) {
            
                // check if it's a bookable product
                $bookable = bkap_common::bkap_get_bookable_status( $item[ 'product_id' ] );
                $valid_date = false;
                if ( isset( $item[ 'wapbk_booking_date' ] ) ) {
                    $valid_date = bkap_common::bkap_check_date_set( $item[ 'wapbk_booking_date' ] );
                }
                if ( $bookable  && $valid_date ) {
                    $bkap = $this->bkap_create_gcal_obj( $item_id, $item, $order );
                    $this->bkap_add_buttons_emails( $bkap );
                }
            }
            
        }
    }

    /**
     * Adds buttons in the WooCommerce admin emails
     * using which admin can add bookings into the calendar.
     * Executed only when manual booking sync is enabled with
     * appropriate settings.
     *
     * @param integer $item_id - Item ID of the product
     * @param WC_Order_Item_Product $item - Item Object
     * @param WC_Order $order - Order Object
     *
     * @hook woocommerce_order_item_meta_end
     * @since 2.6
     */

    function bkap_add_to_calendar_admin( $item_id, $item, $order ) {
        if ( ! is_account_page() && ! is_wc_endpoint_url( 'order-received' ) ) {
            
            if ( 'new_order' == $this->email_ID ) {
                // check if it's a bookable product
                $post_id = bkap_common::bkap_get_product_id( $item[ 'product_id' ] );
                
                $bookable = bkap_common::bkap_get_bookable_status( $post_id );
                
                $valid_date = false;
                if ( isset( $item[ 'wapbk_booking_date' ] ) ) {
                    $valid_date = bkap_common::bkap_check_date_set( $item[ 'wapbk_booking_date' ] );
                }
                if ( $bookable && $valid_date ) {
                    
                    // check if tour operators are allowed to setup GCal
                    if ( 'yes' == get_option( 'bkap_allow_tour_operator_gcal_api' ) ) {
                        // if tour operator addon is active, return if an operator is assigned
                        if ( function_exists( 'is_bkap_tours_active' ) && is_bkap_tours_active() ) {
                    
                            $booking_settings = get_post_meta( $post_id, 'woocommerce_booking_settings', true );
                    
                            if( isset( $booking_settings[ 'booking_tour_operator' ] ) &&  $booking_settings[ 'booking_tour_operator' ] != 0 ) {
                                return;
                            }
                    
                        }
                    }
                    $bkap = $this->bkap_create_gcal_obj( $item_id, $item, $order );
                    $this->bkap_add_buttons_emails( $bkap );
                }
            }
        }
    }

    /**
     * Adds buttons to WooCommerce pages such as My Account page and Thank you page
     * based on settings which allow the ability to add bookings to the calendar.
     *
     * @param integer $item_id - Item ID of the product
     * @param WC_Order_Item_Product $item - Item Object
     * @param WC_Order $order - Order Object
     *
     * @hook woocommerce_order_item_meta_end
     * @since 2.6
     */

    function bkap_add_to_woo_pages( $item_id, $item, $order ) {
        
        if ( is_account_page() && 'on' == get_option( 'bkap_add_to_calendar_my_account_page' ) ) {
            
            // check if it's a bookable product
            $bookable = bkap_common::bkap_get_bookable_status( $item[ 'product_id' ] );
            
            $valid_date = false;
            if ( isset( $item[ 'wapbk_booking_date' ] ) ) {
                $valid_date = bkap_common::bkap_check_date_set( $item[ 'wapbk_booking_date' ] );
            }
            if ( $bookable && $valid_date ) {
                wp_enqueue_style( 'gcal_sync_style', plugins_url( '/css/calendar-sync.css', __FILE__ ) , '', '', false );
                $bkap = $this->bkap_create_gcal_obj( $item_id, $item, $order );
                $this->bkap_add_buttons( $bkap );
            }
            
        }
        if( is_wc_endpoint_url( 'order-received' ) && 'on' == get_option( 'bkap_add_to_calendar_order_received_page' ) ) {
            
            // check if it's a bookable product
            $bookable = bkap_common::bkap_get_bookable_status( $item[ 'product_id' ] );
            
            $valid_date = false;
            if ( isset( $item[ 'wapbk_booking_date' ] ) ) {
                $valid_date = bkap_common::bkap_check_date_set( $item[ 'wapbk_booking_date' ] );
            }
            if ( $bookable && $valid_date ) {
                wp_enqueue_style( 'gcal_sync_style', plugins_url( '/css/calendar-sync.css', __FILE__ ) , '', '', false );
                $bkap = $this->bkap_create_gcal_obj( $item_id, $item, $order );
                $this->bkap_add_buttons( $bkap );
            }
        }
        
    }

    /**
     * Returns an object which can be used to create 'Add to Calendar'
     * buttons for Google Calendar Sync
     *
     * @param integer $item_id - Item ID of the product
     * @param WC_Order_Item_Product $item - Item Object
     * @param WC_Order $order - Order Object
     * @return object $bkap - Contains booking details
     * @since 2.6
     */

    function bkap_create_gcal_obj( $item_id, $item, $order_details ) {

        $order_id = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order_details->id : $order_details->get_id();
        $order_data = get_post_meta( $order_id );
        $order = new WC_Order( $order_id );
        
        $bkap = new stdClass();
         
        $bkap->item_id = $item_id;
        $valid_date = bkap_common::bkap_check_date_set( $item[ 'wapbk_booking_date' ] );
        
        if ( $valid_date ) {
            $bkap->start = $item[ 'wapbk_booking_date' ];
        
            $bkap->client_address = __( $order_data[ '_shipping_address_1' ][ 0 ] . " " . $order_data[ '_shipping_address_2' ][ 0 ] , 'woocommerce-booking' );
            $bkap->client_city = __( $order_data[ '_shipping_city' ][ 0 ], 'woocommerce-booking' );
             
            if ( isset( $item[ 'wapbk_checkout_date' ] ) && $item[ 'wapbk_checkout_date' ] != '' ) {
                $bkap->end = $item[ 'wapbk_checkout_date' ];
            } else {
                $bkap->end = $item[ 'wapbk_booking_date' ];
            }
         
            if( isset( $item[ 'wapbk_time_slot' ] ) && $item[ 'wapbk_time_slot' ] != '' ) {
                $timeslot = explode( " - ", $item[ 'wapbk_time_slot' ] );
                $from_time = date( "H:i", strtotime( $timeslot[ 0 ] ) );
                
                if( isset( $timeslot[ 1 ] ) && $timeslot[ 1 ] != '' ) {
                    $to_time = date( "H:i", strtotime( $timeslot[ 1 ] ) );
                    $bkap->end_time = $to_time;
                    $time_end = explode( ':', $to_time );
                } else {
                    $bkap->end_time = $from_time;
                    $time_end = explode( ':', $from_time );
                }
                
                $bkap->start_time = $from_time;
            } else {
                $bkap->start_time = "";
                $bkap->end_time = "";
            }
            
            $bkap->client_email = $order_data[ '_billing_email' ][ 0 ];
            $bkap->client_name = $order_data[ '_billing_first_name' ][ 0 ] . " " . $order_data[ '_billing_last_name' ][ 0 ];
            $bkap->client_address = $order_data[ '_billing_address_1' ][ 0 ]  . " " . $order_data[ '_billing_address_2' ][ 0 ];
            $bkap->client_phone = $order_data[ '_billing_phone' ][ 0 ];
            $bkap->order_note  = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->customer_note : $order->get_customer_note();
            
            $product = $product_with_qty = "";
            
            $product = $item[ 'name' ];
            $product_with_qty = $item[ 'name' ] . "(QTY: " . $item[ 'qty' ] . ") ";
             
            $bkap->order_total  = $item[ 'line_total' ];
            $bkap->product = $product;
            $bkap->product_with_qty = $product_with_qty;

            if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) {
                $bkap->order_date_time = $order->post->post_date;
                $order_date = date( "Y-m-d", strtotime( $order->post->post_date ) );    
            } else {
                $order_post = get_post( $order_id );
                $post_date = strtotime ( $order_post->post_date );
                $bkap->order_date_time = date( 'Y-m-d H:i:s', $post_date );
                $order_date = date( 'Y-m-d', $post_date );
            /*    $order_created_obj = $order->get_date_created();
                $bkap->order_date_time = $order_created_obj->format( 'Y-m-d H:i:s' );
                $order_date = $order_created_obj->format( 'Y-m-d' ); */
            }
            
            $bkap->order_date = $order_date;
            $bkap->id = $order_id;
              
            return $bkap;
        }
        
    }

    /**
     * Creates 'Add to Google Calendar' & 'Add to Other Calendar'
     * buttons in WooCommerce emails
     *
     * @param object $bkap - Contains Booking Details
     * @since 2.6
     */

    function bkap_add_buttons_emails( $bkap ) {
        
        $gcal = new BKAP_Gcal();
        $href = $gcal->gcal( $bkap );
        $other_calendar_href = $gcal->other_cal( $bkap );
        
        $target = '_blank';
        
        if( get_option( 'bkap_calendar_in_same_window' ) == 'on' ) {
            $target = '_self';
        } else {
            $target = '_blank';
        }
        
        ?>
        <form method="post" action="<?php echo $href; ?>" target= "<?php echo $target; ?>" id="add_to_google_calendar_form">
            <input type="submit" id="add_to_google_calendar" name="add_to_google_calendar" value="<?php _e( 'Add to Google Calendar', 'woocommerce-booking' ); ?>" />
        </form>
        <form method="post" action="<?php echo $other_calendar_href; ?>" target="<?php echo $target; ?>" id="add_to_other_calendar_form">
            <input type="submit" id="download_ics" name="download_ics" value="<?php _e( 'Add to other Calendar', 'woocommerce-booking' ); ?>" />
        </form>
                
        <?php 
    }

    /**
     * Creates 'Add to Google Calendar' & 'Add to Other Calendar'
     * buttons on WooCommerce pages.
     *
     * @param object $bkap - Contains Booking Details
     * @since 2.6
     */

    function bkap_add_buttons( $bkap ) {
        
        $gcal = new BKAP_Gcal();
        $href = $gcal->gcal( $bkap );
        $other_calendar_href = $gcal->other_cal( $bkap );
        
        $target = '_blank';
        
        if( get_option( 'bkap_calendar_in_same_window' ) == 'on' ) {
            $target = '_self';
        } else {
            $target = '_blank';
        }
                
        ?>
        <div class="add_to_calendar">
            <button onclick="myFunction( <?php echo $bkap->item_id; ?> )" class="dropbtn"><?php _e( "Add To Calendar", 'woocommerce-booking' ); ?><i class="claret"></i></button>
            <div id="add_to_calendar_menu_<?php echo $bkap->item_id; ?>" class="add_to_calendar-content">
                <a href="<?php echo $href; ?>" target= "<?php echo $target; ?>" id="add_to_google_calendar" ><img class="icon" src="<?php echo plugins_url(); ?>/woocommerce-booking/images/google-icon.ico"><?php _e( "Add to Google Calendar", 'woocommerce-booking' ); ?></a>
                <a href="<?php echo $other_calendar_href; ?>" target="<?php echo $target; ?>" id="add_to_other_calendar" ><img class="icon" src="<?php echo plugins_url(); ?>/woocommerce-booking/images/calendar-icon.ico"><?php _e( "Add to other Calendar", 'woocommerce-booking' ); ?></a>
            </div>
        </div>

        <script type="text/javascript">
        /* When the user clicks on the button, 
        toggle between hiding and showing the dropdown content */

        function myFunction( chk ) {
            document.getElementById( "add_to_calendar_menu_"+ chk ).classList.toggle( "show" );
        }
        // Close the dropdown if the user clicks outside of it
        window.onclick = function(event) {
            if ( !event.target.matches( '.dropbtn' ) ) {
                var dropdowns = document.getElementsByClassName( "dropdown-add_to_calendar-content" );
                var i;
                for ( i = 0; i < dropdowns.length; i++ ) {
                    var openDropdown = dropdowns[i];
                    if ( openDropdown.classList.contains( 'show' ) ) {
                        openDropdown.classList.remove( 'show' );
                    }
                }
            }
        }

        </script>
        <?php 

    }

    /**
     * Save the Google Import URL Feeds from
     * Booking->Settings->Google Calendar Sync Settings->Import Events
     * Called via AJAX
     *
     * @since 2.6
     */

    function bkap_save_ics_url_feed() {
        $ics_table_content = '';
        if( isset( $_POST[ 'ics_url' ] ) ) {
            $ics_url = $_POST[ 'ics_url' ];
        } else {
            $ics_url = '';
        }
    
        if( $ics_url != '' ) {
            $ics_feed_urls = get_option( 'bkap_ics_feed_urls' );
            if( $ics_feed_urls == '' || $ics_feed_urls == '{}' || $ics_feed_urls == '[]' || $ics_feed_urls == 'null' ) {
                $ics_feed_urls = array();
            }
    
            if( !in_array( $ics_url, $ics_feed_urls ) ) {
                array_push( $ics_feed_urls, $ics_url );
                update_option( 'bkap_ics_feed_urls', $ics_feed_urls );
                $ics_table_content = 'yes';
            }
        }
    
        echo $ics_table_content;
        die();
    }

    /**
     * Delete the Google Import URL Feeds from
     * Booking->Settings->Google Calendar Sync Settings->Import Events
     * Called via AJAX
     *
     * @since 2.6
     */

    function bkap_delete_ics_url_feed() {
        $ics_table_content = '';
        if( isset( $_POST[ 'ics_feed_key' ] ) ) {
            $ics_url_key = $_POST[ 'ics_feed_key' ];
        } else {
            $ics_url_key = '';
        }

        $product_id = 0;
        if ( isset( $_POST[ 'product_id' ] ) ) {
            $product_id = $_POST[ 'product_id' ];
        }
        
        if( $ics_url_key != '' ) {
            if ( isset( $product_id ) && $product_id > 0 ) {
            
                $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
            
                if ( is_array( $booking_settings[ 'ics_feed_url' ] ) && count( $booking_settings[ 'ics_feed_url' ] ) > 0 ) {
                    $ics_feed_urls = $booking_settings[ 'ics_feed_url' ];
                    if( $ics_feed_urls == '' || $ics_feed_urls == '{}' || $ics_feed_urls == '[]' || $ics_feed_urls == 'null' ) {
                        $ics_feed_urls = array();
                    }
            
                    unset( $ics_feed_urls[ $ics_url_key ] );
                    $booking_settings[ 'ics_feed_url' ] = $ics_feed_urls;
                    update_post_meta( $product_id, 'woocommerce_booking_settings', $booking_settings );
                    
                    // update the individual settings
                    update_post_meta( $product_id, '_bkap_import_url', $ics_feed_urls );
                    $ics_table_content = 'yes';
                }
            } else {
            
                $ics_feed_urls = get_option( 'bkap_ics_feed_urls' );
                if( $ics_feed_urls == '' || $ics_feed_urls == '{}' || $ics_feed_urls == '[]' || $ics_feed_urls == 'null' ) {
                    $ics_feed_urls = array();
                }
        
                unset( $ics_feed_urls[ $ics_url_key ] );
                update_option( 'bkap_ics_feed_urls', $ics_feed_urls );
                $ics_table_content = 'yes';
            }
        }
    
        echo $ics_table_content;
        die();
    }

    /**
     * Gets the ICAL Data from Google Calendar when Import is
     * performed (manual and automated) for all the Google
     * Calendars setup in the plugin.
     *
     * @since 2.6
     */

    public function bkap_setup_import() {
            
        global $wpdb;
        
        if( isset( $_POST[ 'ics_feed_key' ] ) ) {
            $ics_url_key = $_POST[ 'ics_feed_key' ];
        } else {
            $ics_url_key = '';
        }
         
        $product_id = 0;
        if ( isset( $_POST[ 'product_id' ] ) ) {
            $product_id = $_POST[ 'product_id' ];
        }
        
        if ( $product_id == 0 ) {
            $ics_feed_urls = get_option( 'bkap_ics_feed_urls' );
        } else if ( $product_id > 0 ) {
            $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
        
            if ( is_array( $booking_settings[ 'ics_feed_url' ] ) && count( $booking_settings[ 'ics_feed_url' ] ) > 0 ) {
                $ics_feed_urls = $booking_settings[ 'ics_feed_url' ];
            }
        }
        if( $ics_feed_urls == '' || $ics_feed_urls == '{}' || $ics_feed_urls == '[]' || $ics_feed_urls == 'null' ) {
            $ics_feed_urls = array();
        }
        
        if( count( $ics_feed_urls ) > 0 && isset( $ics_feed_urls[ $ics_url_key ] ) ) {
            $ics_feed = $ics_feed_urls[ $ics_url_key ];
            $ics_feed = str_replace( 'https://', '', $ics_feed );
        } else { 
            $ics_feed = '';
        }
        
        if ( $ics_feed == '' && count( $_POST ) <= 0 ) { // it means it was called using cron, so we need to auto import for all the calendars saved
            // run the import for all the calendars saved at the admin level
            if ( isset( $ics_feed_urls ) && count( $ics_feed_urls ) > 0 ) {
                
                foreach ( $ics_feed_urls as $ics_feed ) {
                    $ics_feed = str_replace( 'https://', '', $ics_feed );
                    $ical = new BKAP_iCalReader( $ics_feed );
                    $ical_array = $ical->getEvents();
                    
                    // check if the import is on an AirBNB Cal
                    if ( strpos( $ics_feed, 'airbnb' ) > 0 ) {
                        $airbnb = true;
                    } else {
                        $airbnb = false;
                    }
                    $this->bkap_import_events( $ical_array, 0, $airbnb );
                }
                
            }
            
            // run the import for all the calendars saved at the product level
            $args       = array( 'post_type' => 'product', 'posts_per_page' => -1 );
            $product    = query_posts( $args );
            
            foreach($product as $k => $v){
                $product_ids[] = $v->ID;
            }
            
            foreach( $product_ids as $k => $v ){
                $duplicate_of  = bkap_common::bkap_get_product_id( $v );
            
                $is_bookable = bkap_common::bkap_get_bookable_status( $duplicate_of );
            
                if ( $is_bookable ) {
                    $booking_settings = get_post_meta( $duplicate_of, 'woocommerce_booking_settings' , true );
            
                    if ( isset( $booking_settings[ 'ics_feed_url' ] ) && count( $booking_settings[ 'ics_feed_url' ] ) > 0 ) {
                        foreach ( $booking_settings[ 'ics_feed_url' ] as $key => $value ) {
                            
                            $value = str_replace( 'https://', '', $value );
                            $ical = new BKAP_iCalReader( $value );
                            $ical_array = $ical->getEvents();
                            
                            // check if the import is on an AirBNB Cal
                            if ( strpos( $value, 'airbnb' ) > 0 ) {
                                $airbnb = true;
                            } else {
                                $airbnb = false;
                            }
                            $this->bkap_import_events( $ical_array, $duplicate_of, $airbnb );
                        }
                    }
            
                }
            }
        } else {
            $ical = new BKAP_iCalReader( $ics_feed );
            $ical_array = $ical->getEvents();
            
            // check if the import is on an AirBNB Cal
            if ( strpos( $ics_feed, 'airbnb' ) > 0 ) {
                $airbnb = true;
            } else {
                $airbnb = false;
            }
            $this->bkap_import_events( $ical_array, $product_id, $airbnb );
            
        }
        
        die();
    }

    /**
     * Creates records for imported events and automatically maps the same
     * (based on the settings) for the ICAL Data passed in.
     *
     * @param object $ical_array - Contains all the events in the Google Calendar
     * @param integer $product_id - Product ID for which the ICAl feed is setup. 0 indicates global
     * @param boolean $airbnb - Set to true for AirBNB calendars, else false.
     * @since 2.6
     */

    public function bkap_import_events( $ical_array, $product_id = 0, $airbnb = false ) {
        
        global $wpdb;
        
        if ( isset( $product_id ) && 0 == $product_id ) {
            $event_uids = get_option( 'bkap_event_uids_ids' );
        } else {
            $event_uids = get_post_meta( $product_id, 'bkap_event_uids_ids', true );
        }
        
        if( $event_uids == '' || $event_uids == '{}' || $event_uids == '[]' || $event_uids == 'null' ) {
            $event_uids = array();
        }
        
        if( isset( $ical_array ) ) {
        
            // get the last stored event count
            $options_query = "SELECT option_name FROM `" . $wpdb->prefix. "options`
                                        WHERE option_name like 'bkap_imported_events_%'";
        
            $results = $wpdb->get_results( $options_query );
        
            if (isset( $results ) && count( $results ) > 0 ) {
                $last_count = 0;
                foreach ( $results as $results_key => $option_name ) {
                    $explode_array = explode( '_', $option_name->option_name );
                    $current_id = $explode_array[3];
                    
                    if ( $last_count < $current_id ) {
                        $last_count = $current_id;
                    }
                }
                
                $i = $last_count + 1;
                
            } else {
                $i = 0;
            }
            foreach( $ical_array as $key_event => $value_event ) {

                $uid = '';
                if ( $airbnb ) {
                    $summary = $value_event->summary;
                
                    if ( strpos( $summary, '(' ) > 0 ) {
                        $start = strpos( $summary, '(' );
                        $start += 1;
                        $end = strpos( $summary, ')' );
                        $length = $end - $start;
                        $uid = substr( $summary, $start, $length );
                    }
                } else {
                    $uid = $value_event->uid;
                }
                
                if ( $uid != '' ) {
                    //Do stuff with the event $event
                    if( !in_array( $uid, $event_uids ) ) {
                        // gmt time stamp as Google sends the UTC timestamp
                        $current_time = current_time( 'timestamp', 1 );
                        
                        // Import future dated events
                        if ( $value_event->start >= $current_time || $value_event->end >= $current_time ) {
                        
                            $option_name = 'bkap_imported_events_' . $i;        
                            add_option( $option_name, json_encode( $value_event ) );
                            
                            array_push( $event_uids, $uid );
                            if ( isset( $product_id ) && 0 == $product_id ) {
                                update_option( 'bkap_event_uids_ids', $event_uids );
                                
                                
                                $status = "bkap-unmapped";
                                bkap_calendar_sync::bkap_create_gcal_event_post( $value_event, $product_id , $status, $option_name );
                                
                            } else {
                                update_post_meta( $product_id, 'bkap_event_uids_ids', $event_uids );
                            
                                // get the product type
                                $_product = wc_get_product( $product_id );
                                $automated_mapping = 'NO';
                                
                                $status = "bkap-unmapped";
                                $created_event_post = bkap_calendar_sync::bkap_create_gcal_event_post( $value_event, $product_id , $status, $option_name );
                                
                                $booking_settings = get_post_meta( $product_id, 'woocommerce_booking_settings', true );
                                if ( isset ( $booking_settings[ 'enable_automated_mapping' ] ) && 'on' == $booking_settings[ 'enable_automated_mapping' ] ) {
                                    $automated_mapping = 'YES';
                                    $product_id_to_map = $product_id;
                                    
                                    if ( 'variable' == $_product->get_type() ) {
                                        if ( isset( $booking_settings[ 'gcal_default_variation' ] ) && '' != $booking_settings[ 'gcal_default_variation' ] ) {
                                            $product_id_to_map = $booking_settings[ 'gcal_default_variation' ];
                                            $automated_mapping = 'YES';
                                        } else {
                                            $automated_mapping = 'NO';
                                        }
                                        
                                    }
                                }
                            
                                if ( isset( $automated_mapping ) && 'YES' == $automated_mapping ) {
                                    
                                    $_POST[ 'ID' ]          = $created_event_post->id;
                                    $_POST[ 'product_id' ]  = $product_id_to_map;
                                    $_POST[ 'automated' ]   = 1;
                                    $_POST[ 'type' ]        = "by_post";
                                    
                                    // all the events will be mapped to the product.
                                    $import_bookings = new import_bookings();
                                    $import_bookings->bkap_map_imported_event();
                                } else {
                                    $user_id = 0;
                                    // if the tours addon is active, then the tour operator should receive the email
                                    if ( function_exists( 'is_bkap_tours_active' ) && is_bkap_tours_active() ) {
                                        // check if tour operators are allowed to setup GCal
                                        if ( 'yes' == get_option( 'bkap_allow_tour_operator_gcal_api' ) ) {
                                            // fetch teh tour operators ID
                                            if( isset( $booking_settings[ 'booking_tour_operator' ] ) &&  $booking_settings[ 'booking_tour_operator' ] != 0 ) {
                                                $user_id = $booking_settings[ 'booking_tour_operator' ];
                                            }
                                        }
                                    }
                            
                                    do_action( 'bkap_gcal_events_imported', $option_name, $user_id );
                                }
                            }
                        }
                    }    
                }
                $i++;
            }
            echo "All the Events are Imported.";
        }
        
    }
    
    
    /**
     * Creates & returns a booking post meta record
     * array to be inserted in post meta.
     * 
     * @param int $item_id - Item ID of the Product
     * @param int $product_id - Product ID
     * @param array $booking_details - Array containing booking details
     * 
     * @since 4.2
     */

     public static function bkap_create_gcal_event_post( $bkap_event, $product_id = 0, $status, $option_name = '', $user_id = 1 ) {
        
        $new_event_data = array();
        
        // Merge booking data
        $defaults = array(
            'user_id'          => $user_id,
            'product_id'       => $product_id,
            'start_date'       => '',
            'end_date'         => '',
            'uid'              => '',
            'summary'          => '',
            'description'      => '',
            'location'         => '',
            'reason_of_fail'   => '',
            'resource_id'      => '',
            'persons'          => array(),
            'qty'              => 1,
            'variation_id'     => 0,
            'event_option_name'=> ''
        );
        
        $new_event_data = wp_parse_args( $new_event_data, $defaults );
        
        $event_value_set = "";
        
        if ( is_object( $bkap_event ) && "" != $bkap_event ) {
           $event_uid           = $bkap_event->uid;
           
           $event_start_str     = $bkap_event->start;
           
           $event_end_str       = $bkap_event->end;
            
           $event_summary       = $bkap_event->summary;
           $event_description   = $bkap_event->description;
           $event_location      = $bkap_event->location;
           $event_value_set     = "value_set";
        }
        
        if ( "" != $event_value_set ) {
            $new_event_data['user_id']          = $user_id;
            $new_event_data['uid']              = $event_uid;
            $new_event_data['start_date']       = $event_start_str;
            $new_event_data['end_date']         = $event_end_str;
            $new_event_data['summary']          = $event_summary;
            $new_event_data['description']      = $event_description;
            $new_event_data['location']         = $event_location;
            $new_event_data['reason_of_fail']   = "";
            $new_event_data['event_option_name']= $option_name;
        }
        
        // Create the Instance using the Event Data
        $new_booking_event = bkap_calendar_sync::get_bkap_booking( $new_event_data );  
        $new_booking_event->create( $status );
        
        return $new_booking_event;
    }
    
    /**
     * Creating the instance fo the BKAP_Gcal_Event
     * 
     * @param array $id - contains the event Details
     * @return object BKAP_Booking
     * @since 4.2
     */

    static function get_bkap_booking( $id ) {
        return new BKAP_Gcal_Event( $id );
    }

    /**
     * Export bookings to Google Calendar from the Booking->View Bookings->Add to Calendar button.
     * This is especially used to export bookings for orders that were placed before
     * Automated Google Calendar Sync is enabled.
     *
     * @since 2.6
     */
    
    public function bkap_admin_booking_calendar_events() {
        
        global $wpdb;
        $user_id = $_POST[ 'user_id' ];
        
        $total_orders_to_export = bkap_common::bkap_get_total_bookings_to_export( $user_id );
        
        $gcal = new BKAP_Gcal();
        $current_time = current_time( 'timestamp' );
        
        $user = new WP_User( $user_id );
        if( 'tour_operator' == $user->roles[ 0 ] ) {
            $event_item_ids = get_the_author_meta( 'tours_event_item_ids', $user_id );
        } else {
            $event_item_ids = get_option( 'bkap_event_item_ids' );
        }
        
        if( $event_item_ids == '' || $event_item_ids == '{}' || $event_item_ids == '[]' || $event_item_ids == 'null' ) {
            $event_item_ids = array();
        }
         
        if ( isset( $total_orders_to_export ) && count( $total_orders_to_export ) > 0 ) {
            foreach( $total_orders_to_export as $order_id => $value ) {
                $data = get_post_meta( $order_id );
                foreach ( $value as $item_id ) {
                    
                    if ( !in_array( $item_id, $event_item_ids ) ) {
                        $event_details = array();
                        
                        $order = new WC_Order( $order_id );
                        $get_items        = $order->get_items();
                        
            foreach( $get_items as $get_items_key => $get_items_value ) {
                            
                            if( $get_items_key  == $item_id ){
                                
                                $item_data = $get_items_value->get_data(); // Getting Item data.
                                $item_name = $item_data['name'];
                            
                            
                                $item_booking_date = wc_get_order_item_meta( $item_id, '_wapbk_booking_date' );
                                $item_checkout_date = wc_get_order_item_meta( $item_id, '_wapbk_checkout_date' );
                                $item_booking_time = wc_get_order_item_meta( $item_id, '_wapbk_time_slot' );
                                
                                $product_id = wc_get_order_item_meta( $item_id, '_product_id' );
                                $quantity = wc_get_order_item_meta( $item_id, '_qty' );
                                
                                $booking_status = wc_get_order_item_meta( $item_id, '_wapbk_booking_status' );
                                
                                if ( ( isset( $booking_status ) && 'pending-confirmation' != $booking_status ) || ( ! isset( $booking_status )) ) {
                                
                                    if ( isset( $item_booking_date ) && $item_booking_date != '1970-01-01' ) {
                                        $event_details[ 'hidden_booking_date' ] = $item_booking_date;
                                    }
                                
                                
                                    if ( isset( $item_checkout_date ) && $item_checkout_date != '' ) {
                                        $event_details[ 'hidden_checkout_date' ] = $item_checkout_date;
                                    }
                                    
                                    if ( isset( $item_booking_time ) && $item_booking_time != '' ) {
                                        $event_details[ 'time_slot' ] = $item_booking_time;
                                    }
                                
                                    $event_details[ 'billing_email' ] = $data[ '_billing_email' ][ 0 ];
                                    $event_details[ 'billing_address_1' ] = $data[ '_billing_address_1' ][ 0 ];
                                    $event_details[ 'billing_address_2' ] = $data[ '_billing_address_2' ][ 0 ];
                                    $event_details[ 'billing_city' ] = $data[ '_billing_city' ][ 0 ];
                                    $event_details[ 'order_id' ] = $order_id;
                                    
                                    $event_details[ 'shipping_first_name' ] = $data[ '_shipping_first_name' ][ 0 ];
                                    $event_details[ 'shipping_last_name' ] = $data[ '_shipping_last_name' ][ 0 ];
                                    $event_details[ 'shipping_address_1' ] = $data[ '_shipping_address_1' ][ 0 ];
                                    $event_details[ 'shipping_address_2' ] = $data[ '_shipping_address_2' ][ 0 ];
                                    $event_details[ 'billing_phone' ] = $data[ '_billing_phone' ][ 0 ];
                                    $event_details[ 'order_comments' ]  = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->customer_note : $order->get_customer_note();
                                    
                                    $_product = wc_get_product( $product_id );
                                     
                                    $post_title = $_product->get_title();
                                    $event_details[ 'product_name' ] = $item_name;
                                    $event_details[ 'product_qty' ] = $quantity;
                                    $event_details[ 'product_total' ] = wc_get_order_item_meta( $item_id, '_line_total' );
                                    
                                    $post_id = $product_id;
                                    
                                    if ( ( ! isset( $booking_settings[ 'product_sync_integration_mode' ] ) ) || ( isset( $booking_settings[ 'product_sync_integration_mode' ] ) && 'disabled' == $booking_settings[ 'product_sync_integration_mode' ] ) ) {
                                        $post_id = 0;
                                    }
                                    
                                    $gcal->insert_event( $event_details, $item_id, $user_id, $post_id, false );
                                    
                                    // add an order note, mentioning an event has been created for the item
                                    $order_note = __( "Booking_details for $post_title have been exported to the Google Calendar", 'woocommerce-booking' );
                                    $order->add_order_note( $order_note );
                                }
                         }
                      } 
                    }
                }
            }
        }
        die();
    }
}// end of class
$bkap_calendar_sync = new bkap_calendar_sync();
API documentation generated by ApiGen