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

Packages

  • None
  • Product-Delivery-Date-Pro-for-WooCommerce
    • Addon
    • Admin
      • Settings
        • Field-Labels
        • General
        • Google-Sync
      • View-Deliveries
    • Calendar
    • Common-Functions
    • Configuration
    • Delivery-Charges
    • Edit-Deliveries
    • Emails
    • Frontend
      • Cart
      • Checkout
    • Google-Calendar
    • Languages
    • License
    • Reschedule-Deliveries
    • View-Deliveries

Classes

  • EDD_PRDD_Plugin_Updater
  • prdd_addon_compatibility_class
  • prdd_admin_deliveries
  • prdd_adminevent_jsons
  • prdd_box_class
  • prdd_cart
  • prdd_checkout
  • prdd_common
  • prdd_delivery_labels_settings
  • prdd_delivery_settings
  • prdd_edit_deliveries_class
  • PRDD_Email_Delivery_Rescheduled
  • PRDD_Email_Delivery_Rescheduled_Admin
  • PRDD_Email_Manager
  • prdd_estimate_delivery
  • prdd_global_menu
  • prdd_ics
  • prdd_license
  • prdd_process
  • prdd_rescheduled_order_class
  • prdd_special_delivery_price
  • prdd_timeslot_price
  • prdd_validation
  • PRDD_View_Deliveries_Table
  • ts_google_calendar_sync
  • ts_google_calendar_sync_settings
  • TsGcal
  • view_deliveries
  • woocommerce_prdd

Functions

  • is_prdd_active
  • prdd_get_dateToCal
  • prdd_get_delivery_arrays
  • prdd_get_delivery_t
  • prdd_get_escapeString
  • prdd_woocommerce_delete
  • ts_get_option
  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 
<?php
/**
 * PRDDD Handles the two way Google Calendar sync.
 *
 * @author  Tyche Softwares
 * @package Product-Delivery-Date-Pro-for-WooCommerce/Google-Calendar
 * @since   1.0
 */

class ts_google_calendar_sync {

    /**
     * Default Constructor
     * 
     * @since 1.0 
     */
    public function __construct() {
        $this->gcal_api = false;
        $this->email_ID = '';
        
        add_action( 'wp_loaded', array( $this, 'prdd_setup_gcal_sync' ), 10 );
        add_action( 'admin_init', array( $this, 'prdd_setup_gcal_sync' ), 10 );
        
        $this->plugin_dir = plugin_dir_path( __FILE__ );
        $this->plugin_url = plugins_url( basename( dirname( __FILE__ ) ) );
    

        add_filter( 'woocommerce_order_item_meta_end', array( &$this, 'prdd_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 ( which goes to the customer)
        add_filter( 'woocommerce_email_subject_new_order', array( &$this, 'prdd_new_order_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_processing_order', array( &$this, 'prdd_customer_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_on_hold_order', array( &$this, 'prdd_customer_email' ), 10, 1 );
        add_filter( 'woocommerce_email_subject_customer_completed_order', array( &$this, 'prdd_customer_email' ), 10, 1 );

        if( get_option( 'prdd_add_to_calendar_customer_email' ) == 'on' ) {
            add_action( 'woocommerce_order_item_meta_end', array( &$this, 'prdd_add_to_calendar_customer'), 12, 3 );
        }
        
        if( get_option( 'prdd_admin_add_to_calendar_email_notification' ) == 'on' && get_option( 'ts_calendar_sync_integration_mode' ) == 'manually' ) {
            add_action( 'woocommerce_order_item_meta_end', array( &$this, 'prdd_add_to_calendar_admin'), 13, 3 );
        }

        add_action( 'wp_ajax_prdd_admin_delivery_calendar_events', array( &$this, 'prdd_admin_delivery_calendar_events' ) );
        
    }
    
    /**
     * 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
     *
     * @param string $subject Email subject
     *
     * @return string
     * 
     * @since 1.0
     */
    function prdd_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
     */

     /**
      * 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
      * @hook woocommerce_email_subject_customer_on_hold_order
      * @hook woocommerce_email_subject_customer_completed_order
      *
      * @param string $subject Email subject
      *
      * @return string
      * 
      * @since 1.0
      */

    function prdd_customer_email( $subject ) {
        $this->email_ID = 'customer_order';
        return $subject;
    }
    
     /**
      * Adds the GCal class file
      *
      * @hook wp_loaded
      * @hook admin_init
      * @since 1.0
      */
    function prdd_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 TsGcal();
    }
    
     /**
     * Adds buttons on WooCommerce Order received page using which customers can add deliveries into their calendars.
     *
     * @hook woocommerce_order_item_meta_end
     *
     * @param int $item_id Item ID of the product
     * @param resource $item Item Object
     * @param resource $order Order Object
     * 
     * @since 1.0
     */   

    function prdd_add_to_calendar_customer( $item_id, $item, $order ) {
        /*$ts_calendar_export_custom_field = get_option( 'ts_calendar_export_custom_field', true );
        $ts_calendar_export_custom_field_arr = array();
        if( strpos($ts_calendar_export_custom_field, "," ) !== false ) {
            $ts_calendar_export_custom_field_arr = explode( ",", $ts_calendar_export_custom_field );
        }

        if( in_array( 'prdd_date', $ts_calendar_export_custom_field_arr ) ) {*/
            if ( ! is_account_page() && ! is_wc_endpoint_url( 'order-received' ) ) {
                // check the email ID
                if ( 'customer_order' == $this->email_ID ) {
                    $prdd = $this->prdd_create_gcal_obj( $item_id, $item, $order );
                    if( count( (array)$prdd ) > 0 ) {
                        $this->prdd_add_buttons_emails( $prdd );
                    }
                }
            }
        //}
    }
    
    /**
     * Adds buttons on WooCommerce admin emails using which admin can add deliveries into their calendars.
     *
     * @hook woocommerce_order_item_meta_end
     *
     * @param int $item_id Item ID of the product
     * @param resource $item Item Object
     * @param resource $order Order Object
     * 
     * @since 1.0
     */  

    function prdd_add_to_calendar_admin( $item_id, $item, $order ) {
        /*$ts_calendar_export_custom_field = get_option( 'ts_calendar_export_custom_field', true );
        $ts_calendar_export_custom_field_arr = array();
        if( strpos($ts_calendar_export_custom_field, "," ) !== false ) {
            $ts_calendar_export_custom_field_arr = explode( ",", $ts_calendar_export_custom_field );
        }

        if( in_array( 'prdd_date', $ts_calendar_export_custom_field_arr ) ) {*/
            if ( ! is_account_page() && ! is_wc_endpoint_url( 'order-received' ) ) {
                if ( 'new_order' == $this->email_ID ) {
                    $prdd = $this->prdd_create_gcal_obj( $item_id, $item, $order );
                    $this->prdd_add_buttons_emails( $prdd );
                }
            }
        //}
    }

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

    function prdd_add_to_woo_pages( $item_id, $item, $order ) {
        /*$ts_calendar_export_custom_field = get_option( 'ts_calendar_export_custom_field', true );
        $ts_calendar_export_custom_field_arr = array();
        if( strpos($ts_calendar_export_custom_field, "," ) !== false ) {
            $ts_calendar_export_custom_field_arr = explode( ",", $ts_calendar_export_custom_field );
        }

        if( in_array( 'prdd_date', $ts_calendar_export_custom_field_arr ) ) {*/
            $prdd = $this->prdd_create_gcal_obj( $item_id, $item, $order );
            if ( ( is_account_page() && 'on' == get_option( 'prdd_add_to_calendar_my_account_page' ) ) || ( is_wc_endpoint_url( 'order-received' ) && 'on' == get_option( 'prdd_add_to_calendar_order_received_page' ) ) && count( (array)$prdd ) > 0 ) {
                wp_enqueue_style( 'gcal_sync_style', plugins_url( '/css/calendar-sync.css', __FILE__ ) , '', '', false );
                $this->prdd_add_buttons( $prdd );
            }
        //}
    }
    

    /**
     * Returns an object which can be used to create 'Add to Calendar' buttons for Google Calendar Sync.
     *
     * @param int $item_id Item ID of the product
     * @param resource $item Item Object
     * @param resource $order_details Order Object
     *
     * @return resource Contains delivery details
     * @since 1.0
     */

    function prdd_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 );

        $item_data = $item->get_meta_data();
        $meta_data = array();
        foreach( $item_data as $dk => $dv ) {
            if( $dv->key == '_prdd_date' ) {
                $meta_data[ '_prdd_date' ] = $dv->value;
            } else if( $dv->key == '_prdd_time_slot' ) {
                $meta_data[ '_prdd_time_slot' ] = $dv->value;
            }
        }

        $prdd = new stdClass();
        
        if ( array_key_exists( '_prdd_date', $meta_data ) ) {
            $prdd->item_id = $item_id;
            $prdd->start = $meta_data[ '_prdd_date' ];
            $prdd->end = $meta_data[ '_prdd_date' ];

            if( isset( $meta_data[ '_prdd_time_slot' ] ) && $meta_data[ '_prdd_time_slot' ] != '' ) {
                $timeslot = explode( " - ", $meta_data[ '_prdd_time_slot' ] );
                $from_time = date( "H:i", strtotime( $timeslot[ 0 ] ) );
                
                if( isset( $timeslot[ 1 ] ) && $timeslot[ 1 ] != '' ) {
                    $to_time = date( "H:i", strtotime( $timeslot[ 1 ] ) );
                    $prdd->end_time = $to_time;
                    $time_end = explode( ':', $to_time );
                } else {
                    $prdd->end_time = $from_time;
                    $time_end = explode( ':', $from_time );
                }
                
                $prdd->start_time = $from_time;
            } else {
                $prdd->start_time = "";
                $prdd->end_time = "";
            }

            if ( get_option( 'woocommerce_calc_shipping' ) == 'yes' ) {
                if ( get_option( 'woocommerce_ship_to_destination' ) == 'shipping' ) {
                    if ( ( isset( $order_data[ '_shipping_first_name' ][ 0 ] ) && $order_data[ '_shipping_first_name' ][ 0 ]  != '' ) && ( isset( $order_data[ '_shipping_last_name' ][ 0 ]  ) && $order_data[ '_shipping_last_name' ][ 0 ]  != '' ) ) {
                        $prdd->client_name = $order_data[ '_shipping_first_name' ][ 0 ]  . " " . $order_data[ '_shipping_last_name' ][ 0 ] ;
                    } else {
                        if( isset( $order_data[ '_billing_first_name' ][ 0 ]  ) && isset( $order_data[ '_billing_last_name' ][ 0 ]  ) ) {
                            $prdd->client_name = $order_data[ '_billing_first_name' ][ 0 ]  . " " . $order_data[ '_billing_last_name' ][ 0 ] ;
                        } else {
                            $prdd->client_name = "";
                        }
                    }
                    
                    if ( ( isset( $order_data[ '_shipping_address_1' ][ 0 ] ) && $order_data[ '_shipping_address_1' ][ 0 ] != '' ) ) {
                        $prdd->client_address = $order_data[ '_shipping_address_1' ][ 0 ] . " " . $order_data[ '_shipping_address_2' ][ 0 ];
                    } else {
                        $billing_address = '';
                        if( isset( $order_data[ '_billing_address_1' ][ 0 ] ) ) {
                            $billing_address .= $order_data[ '_billing_address_1' ][ 0 ] . " ";
                        }
                        if( isset( $order_data[ '_billing_address_2' ][ 0 ] ) ) {
                            $billing_address .= $order_data[ '_billing_address_2' ][ 0 ];
                        }
                        $prdd->client_address = $billing_address ;
                    }
                    
                    if ( isset( $order_data[ '_shipping_city' ][ 0 ] ) && $order_data[ '_shipping_city' ][ 0 ] != '' ) {
                        $prdd->client_city = $order_data[ '_shipping_city' ][ 0 ];
                    } else {
                        if( isset( $order_data[ '_billing_city' ][ 0 ] ) ) {
                            $prdd->client_city = $order_data[ '_billing_city' ][ 0 ];
                        } else {
                            $prdd->client_city = '';
                        }
                    }
                    
                    if( $order->get_formatted_shipping_address() != "" ) {
                        $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_shipping_address() );
                    } else {
                        $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                    }

                } else if ( get_option( 'woocommerce_ship_to_destination' ) == 'billing' ) {
                    if ( ( isset( $order_data[ '_shipping_first_name' ][ 0 ] ) && $order_data[ '_shipping_first_name' ][ 0 ] != '' ) && ( isset( $order_data[ '_shipping_last_name' ][ 0 ] ) && $order_data[ '_shipping_last_name' ][ 0 ] != '' ) ) {
                        $prdd->client_name = $order_data[ '_shipping_first_name' ][ 0 ] . " " . $order_data[ '_shipping_last_name' ][ 0 ];
                    } else if( ( isset( $order_data[ '_billing_first_name' ][ 0 ] ) && $order_data[ '_billing_first_name' ][ 0 ] != '' ) && ( isset( $order_data[ '_billing_last_name' ][ 0 ] ) && $order_data[ '_billing_last_name' ][ 0 ] != '' )  ) {
                        $prdd->client_name = $order_data[ '_billing_first_name' ][ 0 ] . " " . $order_data[ '_billing_last_name' ][ 0 ];
                    } else {
                        $prdd->client_name = '';
                    }

                    if ( ( isset( $order_data[ '_shipping_address_1' ][ 0 ] ) && $order_data[ '_shipping_address_1' ][ 0 ] != '' ) ) {
                        $prdd->client_address = $order_data[ '_shipping_address_1' ][ 0 ] . " " . $order_data[ '_shipping_address_2' ][ 0 ];
                    } else if ( ( isset( $order_data[ '_billing_address_1' ][ 0 ] ) && $order_data[ '_billing_address_1' ][ 0 ] != '' ) ) {
                        $prdd->client_address = $order_data[ '_billing_address_1' ][ 0 ] . " " . $order_data[ '_billing_address_2' ][ 0 ];
                    } else {
                         $prdd->client_address = '';
                    }

                    if ( isset( $order_data[ '_shipping_city' ][ 0 ] ) && $order_data[ '_shipping_city' ][ 0 ] != '' ) {
                        $prdd->client_city = $order_data[ '_shipping_city' ][ 0 ];
                    } else {
                        if ( isset( $order_data[ '_billing_city' ][ 0 ] ) && $order_data[ '_billing_city' ][ 0 ] != '' ) {
                           $prdd->client_city = $order_data[ '_billing_city' ][ 0 ];
                        } else {
                            $prdd->client_city = "";
                        }
                    }
                    if( $order->get_formatted_shipping_address() != "" ) {
                        $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_shipping_address() );
                    } else {
                        $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                    }
                } else if ( get_option( 'woocommerce_ship_to_destination' ) == 'billing_only') {
                    if( isset( $order_data[ '_billing_first_name' ][ 0 ] ) && isset( $order_data[ '_billing_last_name' ][ 0 ] ) ) {
                        $prdd->client_name = $order_data[ '_billing_first_name' ][ 0 ] . " " . $order_data[ '_billing_last_name' ][ 0 ];
                    } else {
                        $prdd->client_name = "";
                    }
                    
                    if( isset( $order_data[ '_billing_address_1' ][ 0 ] ) && isset( $order_data[ '_billing_address_1' ][ 0 ] ) ) {
                        $prdd->client_address = $order_data[ '_billing_address_1' ][ 0 ] . " " . $order_data[ '_billing_address_2' ][ 0 ];
                    } else {
                        $prdd->client_address = "";
                    }
                    
                    if( isset( $order_data[ '_billing_city' ][ 0 ] ) ) {
                        $prdd->client_city = $order_data[ '_billing_city' ][ 0 ];
                    } else {
                        $prdd->client_city = "";
                    }
                    $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                }
            } else {
            if( isset( $order_data[ '_billing_first_name' ][ 0 ] ) && isset( $order_data[ '_billing_last_name' ][ 0 ] ) ) {
                    $prdd->client_name = $order_data[ '_billing_first_name' ][ 0 ] . " " . $order_data[ '_billing_last_name' ][ 0 ];
                } else {
                    $prdd->client_name = "";
                }
                
                if( isset( $order_data[ '_billing_address_1' ][ 0 ] ) && isset( $order_data[ '_billing_address_1' ][ 0 ] ) ) {
                    $prdd->client_address = $order_data[ '_billing_address_1' ][ 0 ] . " " . $order_data[ '_billing_address_2' ][ 0 ];
                } else {
                    $prdd->client_address = "";
                }
                
                if( isset( $order_data[ '_billing_city' ][ 0 ] ) ) {
                    $prdd->client_city = $order_data[ '_billing_city' ][ 0 ];
                } else {
                    $prdd->client_city = "";
                }
                
                $prdd->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
            }

            $prdd->client_email = $order_data[ '_billing_email' ][ 0 ];
            $prdd->client_phone = $order_data[ '_billing_phone' ][ 0 ];
            $prdd->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' ] . ") ";
             
            $prdd->order_total  = $item[ 'line_total' ];

            $prdd->product = $product;
            $prdd->product_with_qty = $product_with_qty;

            if ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) {
                $prdd->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 );
                $prdd->order_date_time = date( 'Y-m-d H:i:s', $post_date );
                $order_date = date( 'Y-m-d', $post_date );
            }
            
            $prdd->order_date = $order_date;

            $prdd->id = $order_id;
        }
        return $prdd;
    }
    
    /**
     * Creates 'Add to Google Calendar' & 'Add to Other Calendar' buttons in WooCommerce emails.
     *
     * @param resource $prdd Contains Delivery Details
     * @since 1.0
     */

    function prdd_add_buttons_emails( $prdd ) {
        $gcal = new TSGcal();
        $href = $gcal->gcal( $prdd );
        $other_calendar_href = $gcal->other_cal( $prdd );
        
        $target = '_blank';
        if( get_option( 'prdd_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', 'ts' ); ?>" />
        </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', 'ts' ); ?>" />
        </form>
                
        <?php 
    }
    
    /**
     * Creates 'Add to Google Calendar' & 'Add to Other Calendar' buttons on WooCommerce pages.
     *
     * @param resource $prdd Contains Delivery Details
     * @since 1.0
     */

    function prdd_add_buttons( $prdd ) {
        if( count( (array) $prdd ) > 0 ) {
            $gcal = new TSGcal();
            $href = $gcal->gcal( $prdd );
            $other_calendar_href = $gcal->other_cal( $prdd );
            
            $target = '_blank';
            
            if( get_option( 'prdd_calendar_in_same_window' ) == 'on' ) {
                $target = '_self';
            } else {
                $target = '_blank';
            }
                    
            ?>
            <div class="add_to_calendar">
                <button onclick="myFunction( <?php echo $prdd->item_id; ?> )" class="dropbtn">Add To Calendar<i class="claret"></i></button>
                <div id="add_to_calendar_menu_<?php echo $prdd->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(); ?>/product-delivery-date/images/google-icon.ico">Add to Google Calendar</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(); ?>/product-delivery-date/images/calendar-icon.ico">Add to other Calendar</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 
        }

    }

    /**
     * Export deliveries to Google Calendar from the Product Delivery Date->View Deliveries->Add to Calendar button.
     * This is especially used to export deliveries for orders that were placed before automated Google Calendar Sync is enabled.
     *
     * @globals resource $wpdb
     * @hook wp_ajax_prdd_admin_delivery_calendar_events
     * @since 1.0
     */

    function prdd_admin_delivery_calendar_events() {
        global $wpdb;
                
        $total_orders_to_export = prdd_common::prdd_get_total_deliveries_to_export();
        
        $gcal = new TSGcal();
        $current_time = current_time( 'timestamp' );
        
        $event_item_ids = get_option( 'ts_event_item_ids' );
                
        if( $event_item_ids == '' || $event_item_ids == '{}' || $event_item_ids == '[]' || $event_item_ids == 'null' ) {
            $event_item_ids = array();
        }
        //print_r( $total_orders_to_export );
        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 ) {
                    $event_details = array();
                    
                    $order = new WC_Order( $order_id );

                    $item_date = wc_get_order_item_meta( $item_id, '_prdd_date' );
                    $item_date_timestamp = strtotime( $item_date );
                    $item_timeslot = wc_get_order_item_meta( $item_id, '_prdd_time_slot' );
                    $product_id = wc_get_order_item_meta( $item_id, '_product_id' );
                    $quantity = wc_get_order_item_meta( $item_id, '_qty' );

                    $event_details[ 'prdd_date' ] = $item_date;
                                       
                    if ( isset( $item_timeslot ) && $item_timeslot != '' ) {
                        $event_details[ 'prdd_time' ] = $item_timeslot;
                    }
                        
                    $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 ];

                    $order_note  = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $order->customer_note : $order->get_customer_note();
                    $event_details[ 'order_comments' ]  = $order_note;
                        
                    $_product = wc_get_product( $product_id );
                         
                    $event_details[ 'product_name' ] = $_product->get_title();
                    $event_details[ 'product_qty' ] = $quantity;
                    $event_details[ 'product_total' ] = wc_get_order_item_meta( $item_id, '_line_total' );
                    $gcal->insert_event( $event_details, $item_id, false );
                }
            }
        }
    }
}
$ts_google_calendar_sync = new ts_google_calendar_sync();
API documentation generated by ApiGen