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

Packages

  • None
  • Order-Delivery-Date-Pro-for-WooCommerce
    • Admin
      • Edit-Order
      • Non-Sensitive-Data-Capture
      • Settings
        • Custom-Delivery
        • General
        • Google-Calendar-Sync
    • Class-ORDDD-Email-Manager
    • Common-Functions
    • Delivery-Calendar
    • Emails
      • Class-ORDDD-Email-Update-Date
    • Filter
    • Frontend
      • Checkout-Page-Processes
      • ICS-Files-Data
    • Google-Calendar
    • Integration
    • Lang
    • License
    • Plugin-Updates
      • EDD-SL-Plugin-Updater

Classes

  • EDD_SL_Plugin_Updater
  • orddd_additional_settings
  • orddd_admin_delivery_class
  • orddd_appearance_settings
  • orddd_calendar_sync
  • orddd_calendar_sync_settings
  • orddd_class_view_deliveries
  • orddd_common
  • orddd_date_settings
  • orddd_delivery_days_settings
  • ORDDD_Email_Manager
  • ORDDD_Email_Update_Date
  • orddd_filter
  • orddd_holidays_settings
  • orddd_integration
  • orddd_license
  • orddd_process
  • orddd_settings
  • orddd_shipping_based_settings
  • orddd_shipping_days_settings
  • orddd_shipping_multiple_address
  • orddd_time_settings
  • orddd_time_slot_settings
  • ORDDD_View_Delivery_Dates_Table
  • ORDDD_View_Disable_Time_Slots_Table
  • ORDDD_View_Holidays_Table
  • ORDDD_View_Shipping_Settings_Table
  • ORDDD_View_Time_Slots_Table
  • OrdddGcal
  • order_delivery_date
  • TS_Tracker
  • TS_tracking

Functions

  • orddd_get_dateToCal
  • orddd_get_escapeString
  • orddd_t
  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 
<?php
/**
 * Order Delivery Date Pro for WooCommerce
 *
 * Sync Delivery Dte & Time to Google Calendar
 *
 * @author      Tyche Softwares
 * @package     Order-Delivery-Date-Pro-for-WooCommerce/Google-Calendar
 * @since       4.0
 */

if ( !class_exists( 'OrdddGcal' ) ) {
    /**
     * Sync Delivery Dte & Time to Google Calendar.
     *
     * @class OrdddGcal
     */

    class OrdddGcal {

        /**
        * Default constructor
        * 
        * @globals resource $wpdb
        * @since 4.0
        */
        function __construct() {
            global $wpdb;

            $this->plugin_dir = plugin_dir_path( __FILE__ );
            $this->plugin_url = plugins_url( basename( dirname( __FILE__ ) ) );
            $this->local_time = current_time( 'timestamp' );

            if ( !$this->start_of_week = get_option( 'start_of_week' ) ) {
                $this->start_of_week = 0;
            }

            $this->time_format = get_option( 'time_format' );
            if ( empty( $this->time_format ) ) {
                $this->time_format = "H:i";
            }

            $this->date_format = get_option( 'date_format' );
            if ( empty( $this->date_format ) ) {
                $this->date_format = "Y-m-d";
            }
            
            $this->datetime_format = $this->date_format . " " . $this->time_format;
            
            require_once $this->plugin_dir . '/external/google/Client.php';
            // Try to start a session. If cannot, log it.
            
            add_action( 'init', array( &$this, 'orddd_init' ), 12 );
            
            if ( !session_id() && !@session_start() ) {
                $this->log( __( 'Session could not be started. This may indicate a theme issue.', 'order-delivery-date' ) );
            }
            
            // Prevent exceptions to kill the page
            if ( ( isset( $_POST[ 'gcal_api_test' ] ) && 1 == $_POST[ 'gcal_api_test' ] )
                || ( isset( $_POST['gcal_import_now'] ) && $_POST[ 'gcal_import_now' ] ) ) {
                set_exception_handler( array( &$this, 'exception_error_handler' ) );
            }
                
            // Set log file location
            $uploads = wp_upload_dir();
            if ( isset( $uploads[ "basedir" ] ) ) {
                $this->uploads_dir  = $uploads[ "basedir" ] . "/";
            } else {
                $this->uploads_dir  = WP_CONTENT_DIR . "/uploads/";
            }
            
            $this->log_file = $this->uploads_dir . "orddd-log.txt";
            
            add_action( 'wp_ajax_display_nag', array( &$this, "display_nag" ) );
        }
    
        /**
         * Refresh the page with the exception as GET parameter, so that page is not killed
         *
         * @since 4.0
         */
        function exception_error_handler( $exception ) {
            // If we don't remove these GETs there will be an infinite loop
            if ( !headers_sent() ) {
                wp_redirect( esc_url( add_query_arg( array( 'gcal_api_test_result' => urlencode( $exception ), 'gcal_import_now' => false, 'gcal_api_test' => false, 'gcal_api_pre_test' => false ) ) ) );
            } else {
                $this->log( $exception );
            }
        }
        
        /**
         * Displays nag
         * 
         * @hook wp_ajax_display_nag
         * @since 4.0
         */
        function display_nag() {
            $error = false;
            $message = '';
            $upload_dir = wp_upload_dir();
            if ( isset( $_POST[ 'gcal_api_test' ] ) && 1 == $_POST[ 'gcal_api_test' ] ) {
                if ( $result = $this->is_not_suitable() ) {
                    $message .= $result;
                } else {
                    // Insert a test event
                    $result = $this->insert_event( array(), 0, true );
                    if ( $result ) {
                        $message .= __( '<b>Test is successful</b>. Please REFRESH your Google Calendar and check that test appointment has been saved.', 'order-delivery-date' );
                    } else {
                        $message .= __( '<b>Test failed</b>. Please inspect your log file for more info here: ' . $upload_dir[ 'baseurl' ] . '/orddd-log.txt', 'order-delivery-date' );
                    }
                }
            }
            
            if ( isset( $_POST[ 'gcal_api_test_result' ] ) && '' != $_POST[ 'gcal_api_test_result' ] ) {
                $m = stripslashes( urldecode( $_POST[ 'gcal_api_test_result' ] ) );
                // Get rid of unnecessary information
                if ( strpos( $m, 'Stack trace' ) !== false ) {
                    $temp = explode( 'Stack trace', $m );
                    $m = $temp[0];
                }
                if ( strpos( $this->get_selected_calendar(), 'group.calendar.google.com' ) === false ) {
                    $add = '<br />'. __( 'Do NOT use your primary Google calendar, but create a new one.', 'order-delivery-date' );
                } else {
                    $add = '';
                }
                $message = __( 'The following error has been reported by Google Calendar API:<br />', 'order-delivery-date' ) . $m . '<br />' . 
                           __( '<b>Recommendation:</b> Please double check your settings.' . $add, 'order-delivery-date' );
            }
            
            echo $message;
            die();
        }
        
        /**
         * Copy the key file to the uploads folder.
         * 
         * @hook init
         * @since 4.0
         */
        function orddd_init() {
            if ( 'disabled' != $this->get_api_mode() && '' != $this->get_api_mode() ) {
                // Try to create key file folder if it doesn't exist
                $this->create_key_file_folder( );
                $kff = $this->key_file_folder( );
                
                // Copy index.php to this folder and to uploads folder
                if ( is_dir( $kff ) && !file_exists( $kff . 'index.php' ) ) {
                    @copy( $this->plugin_dir . 'gcal/key/index.php', $kff . 'index.php' );
                }
                if ( is_dir( $this->uploads_dir ) && !file_exists( $this->uploads_dir . 'index.php') ) {
                    @copy( $this->plugin_dir . 'gcal/key/index.php', $this->uploads_dir . 'index.php' );
                }
        
                // Copy key file to uploads folder
                $kfn = $this->get_key_file(). '.p12';
                if ( $kfn && is_dir( $kff ) && !file_exists( $kff . $kfn ) && file_exists( $this->plugin_dir . 'gcal/key/' . $kfn ) ) {
                    @copy( $this->plugin_dir . 'gcal/key/' . $kfn, $kff . $kfn );
                }
            }
        }
         
        
        /**
         * Return GCal API mode (Manually, Directly or Disabled )
         * 
         * @return string Setting value
         * @since 4.0
         */
        function get_api_mode() {
            return get_option( 'orddd_calendar_sync_integration_mode' );
        }
    
        /**
         * Return GCal service account
         * 
         * @return string
         * @since 4.0
         */
        function get_service_account() {
            $gcal_service_account_arr = get_option( 'orddd_calendar_details_1' );
            if( isset( $gcal_service_account_arr[ 'orddd_calendar_service_acc_email_address' ] ) ) {
                $gcal_service_account = $gcal_service_account_arr[ 'orddd_calendar_service_acc_email_address' ];
            } else {
                $gcal_service_account = '';
            }
            return $gcal_service_account;
        }
    
        /**
         * Return GCal key file name without the extension
         *
         * @return string Key file name
         * @since 4.0
         */
        function get_key_file() {
            $gcal_key_file_arr = get_option( 'orddd_calendar_details_1' );
            if( isset( $gcal_key_file_arr[ 'orddd_calendar_key_file_name' ] ) ) {
                $gcal_key_file = $gcal_key_file_arr[ 'orddd_calendar_key_file_name' ];
            } else {
                $gcal_key_file = '';
            }
            
            return $gcal_key_file;
        }
    
        /**
         * Return GCal selected calendar ID
         *
         * @return string Calendar ID
         * @since 4.0
         */
        function get_selected_calendar() {
            $gcal_selected_calendar_arr = get_option( 'orddd_calendar_details_1' );
            if( isset( $gcal_selected_calendar_arr[ 'orddd_calendar_id' ] ) ) {
                $gcal_selected_calendar = $gcal_selected_calendar_arr[ 'orddd_calendar_id' ];
            } else {
                $gcal_selected_calendar = '';
            }
            return $gcal_selected_calendar;
        }
    
        /**
         * Return GCal Summary (name of Event)
         * 
         * @return string 
         * @since 4.0
         */
        function get_summary() {
            return get_option( 'orddd_calendar_event_summary' );
        }
    
        /**
         * Return GCal description
         *
         * @return string
         * @since 4.0
         */
        function get_description() {
            return get_option( 'orddd_calendar_event_description' );
        }
    
        /**
         * Checks if php version and extentions are correct
         *
         * @return string Error is not Suitable. Empty string means suitable.
         * @since 4.0
         */
        function is_not_suitable() {
            if ( version_compare(PHP_VERSION, '5.3.0', '<') ) {
                return __( 'Google PHP API Client <b>requires at least PHP 5.3</b>', 'order-delivery-date' );
            }
    
            // Disabled for now
            if ( false && memory_get_usage() < 31000000 ) {
                return sprintf( __( 'Google PHP API Client <b>requires at least 32 MByte Server RAM</b>. Please check this link how to increase it: %s', 'order-delivery-date' ), '<a href="http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP" target="_blank">'.__( 'Increasing_memory_allocated_to_PHP', 'order-delivery-date' ).'</a>' );
            }
    
            if ( !function_exists( 'curl_init' ) ) {
                return __( 'Google PHP API Client <b>requires the CURL PHP extension</b>', 'order-delivery-date' );
            }
    
            if ( !function_exists( 'json_decode' ) ) {
                return __( 'Google PHP API Client <b>requires the JSON PHP extension</b>', 'order-delivery-date' );
            }
    
            if ( !function_exists( 'http_build_query' ) ) {
                return __( 'Google PHP API Client <b>requires http_build_query()</b>', 'order-delivery-date' );
            }
    
            // Dont continue further if this is pre check
            if ( isset( $_POST[ 'gcal_api_pre_test' ] ) && 1== $_POST[ 'gcal_api_pre_test' ] ) {
                return __( 'Your server installation meets requirements.', 'order-delivery-date' );
            }
    
            if ( !$this->_file_exists() ) {
                return __( '<b>Key file does not exist</b>', 'order-delivery-date' );
            }
            
            return '';
        }
    
        /**
         * Checks if key file exists
         * 
         * @return bool
         * @since 4.0
         */
        function _file_exists() {
            if ( file_exists( $this->key_file_folder(). $this->get_key_file() . '.p12' ) ) {
                return true;
            } else if ( file_exists( $this->plugin_dir . 'gcal/key/'. $this->get_key_file() . '.p12' ) ) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * Get contents of the key file
         * 
         * @return string
         * @since 4.0
         */
        function _file_get_contents() {
            if ( file_exists( $this->key_file_folder( ). $this->get_key_file() . '.p12' ) ) {
                return @file_get_contents( $this->key_file_folder(). $this->get_key_file() . '.p12' );
            } else if ( file_exists( $this->plugin_dir . 'gcal/key/'. $this->get_key_file() . '.p12' ) ) {
                return @file_get_contents( $this->plugin_dir . 'gcal/key/'. $this->get_key_file() . '.p12' );
            } else {
                return '';
            }
        }
        
        /**
         * Try to create an encrypted key file folder
         * 
         * @return string
         * @since 4.0
         */
        
        function create_key_file_folder( ) {
            if ( !is_dir( $this->uploads_dir . 'orddd_uploads/' ) ) {
                @mkdir( $this->uploads_dir . 'orddd_uploads/' );
            }
        }
         
        /**
         * Return key file folder name
         * 
         * @return string
         * @since 4.0
         */
        function key_file_folder() {
            return $this->uploads_dir . 'orddd_uploads/';
        }
    
        /**
         * Checks for settings and prerequisites
         * 
         * @return bool
         * @since 4.0
         */
        function is_active() {
            // If integration is disabled, nothing to do
            if ( 'disabled' == $this->get_api_mode() || '' == $this->get_api_mode() || !$this->get_api_mode() ) {
                return false;
            }
            if ( $this->is_not_suitable() ) {
                return false;
            }
    
            if ( $this->get_key_file() &&  $this->get_service_account() && $this->get_selected_calendar() ) {
                return true;
            }
            // None of the other cases are allowed
            return false;
        }
    
        /**
         * Connects to GCal API
         * 
         * @return bool Return true if connected successfully, else false.
         * @since 4.0
         */
        function connect() {
            // Disallow faultly plugins to ruin what we are trying to do here
            @ob_start();
    
            if ( !$this->is_active() ) {
                return false;
            }
            // Just in case
            require_once $this->plugin_dir . '/external/google/Client.php';
    
            $config = new Orddd_Google_OrdddGoogleConfig( apply_filters( 'orddd-gcal-client_parameters', array(
                //'cache_class' => 'Google_Cache_Null', // For an example
            )));
            
            $this->client = new Orddd_Google_Client( $config );
            $this->client->setApplicationName( "Order Delivery Date" );;
            $key = $this->_file_get_contents();
            $this->client->setAssertionCredentials( new Orddd_Google_Auth_AssertionCredentials(
                $this->get_service_account(),
                array( 'https://www.googleapis.com/auth/calendar' ),
                $key)
            );
    
            $this->service = new Orddd_Google_Service_Calendar( $this->client );
    
            return true;
        }
    
        /**
         * Creates a Google Event object and set its parameters
         *
         * @param array $app Data to export
         * @since 4.0
         */
        function set_event_parameters( $app ) {
            $full_address = $client_name = $start_time = $end_time = $start_date = $end_date = $email = $client_address = $products = $product_with_qty = $client_city = $order_date_time = $order_date = $id = $order_total = $client_phone = $order_note = $client_full_address = $client_address = $client_email = '';
            if( isset( $app->client_full_address ) ) {
                $full_address = $app->client_full_address;
            }
            
            if( isset( $app->client_name ) ) {
                $client_name = $app->client_name;
            }
            
            if( isset( $app->start_time ) ) {
                $start_time = $app->start_time;
            }
            
            if( isset( $app->start ) ) {
                $start_date = $app->start;
            }
            
            if( isset( $app->end ) ) {
                $end_date = $app->end;
            }
            
            if( isset( $app->end_time ) ) {
                $end_time = $app->end_time;
            }
            
            if ( isset( $app->client_address ) ) {
                $client_address = $app->client_address;
            }
            
            if ( isset( $app->products ) ) {
                $products = $app->products;
            }
            
            if ( isset( $app->product_with_qty ) ) {
                $product_with_qty = $app->product_with_qty;
            }

            if ( isset( $app->client_city ) ) {
                $client_city = $app->client_city;
            }

            if ( isset( $app->order_date_time ) ) {
                $order_date_time = $app->order_date_time;
            }

            if ( isset( $app->order_date ) ) {
                $order_date = $app->order_date;
            }

            if ( isset( $app->id ) ) {
                $id =$app->id;
            }

            if ( isset( $app->order_total ) ) {
                $order_total = $app->order_total;
            }

            if ( isset( $app->client_phone ) ) {
                $client_phone = $app->client_phone;
            }

            if ( isset( $app->order_note ) ) {
                $order_note = $app->order_note;
            }

            if ( isset( $app->client_full_address ) ) {
                $client_full_address = $app->client_full_address;
            }

            if ( isset( $app->client_address ) ) {
                $client_address = $app->client_address;
            }

            if ( isset( $app->client_email ) ) {
                $client_email = $app->client_email;
            }
            
            if ( get_option( 'orddd_calendar_event_location' ) != "" ) {
                $location = str_replace( array( 'FULL_ADDRESS', 'ADDRESS', 'CITY' ), array( $full_address, $client_address, $client_city ), get_option( 'orddd_calendar_event_location' ) );
            } else {
                $location = get_bloginfo( 'description' );
            }
            
            $summary = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY', 'ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER', 'PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'), 
                array( get_bloginfo( 'name' ), $client_name, $products, $product_with_qty, $order_date_time, $order_date, $id , $order_total, $client_phone, $order_note, $client_full_address, $client_address, $client_email ), $this->get_summary() );
            
            $description = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY','ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER', 'PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'),
                array( get_bloginfo( 'name' ), $client_name, $products, $product_with_qty, $order_date_time, $order_date, $id , $order_total, $client_phone, $order_note, $client_full_address, $client_address, $client_email ), $this->get_description() );
            
            // Find time difference from Greenwich as GCal asks UTC
            if ( !current_time( 'timestamp' ) ) {
                $tdif = 0;
            } else {
                $tdif = current_time( 'timestamp' ) - time();
            }
            
            if( $start_time == "" && $end_time == "" ) {
                $start = new Orddd_Google_Service_Calendar_EventDateTime();
                $start->setDate( date( "Y-m-d", strtotime( $start_date ) ) );
                
                $end = new Orddd_Google_Service_Calendar_EventDateTime();
                $end->setDate( date( "Y-m-d", strtotime( $end_date . "+1 day" ) ) );
            } else if( $end_time == "" ) {
                $start = new Orddd_Google_Service_Calendar_EventDateTime();
                $start->setDateTime( date( "Y-m-d\TH:i:s\Z", strtotime( $start_date . " " . $start_time ) - $tdif ) );
                
                $end = new Orddd_Google_Service_Calendar_EventDateTime();
                $end->setDateTime( date( "Y-m-d\TH:i:s\Z", strtotime( '+30 minutes', strtotime( $end_date . " " . $start_time ) )  - $tdif ) );
            } else {
                $start = new Orddd_Google_Service_Calendar_EventDateTime();
                $start->setDateTime( date( "Y-m-d\TH:i:s\Z", strtotime( $start_date . " " . $start_time ) - $tdif ) );
    
                $end = new Orddd_Google_Service_Calendar_EventDateTime();
                $end->setDateTime( date( "Y-m-d\TH:i:s\Z", strtotime( $end_date . " " . $end_time ) - $tdif ) );
            }
            
            if( isset( $app->client_email ) ) {
                $email = $app->client_email;
            }
            
            $attendee1 = new Orddd_Google_Service_Calendar_EventAttendee();
            $attendee1->setEmail( $email );
            $attendees = array( $attendee1 );
    
            $this->event = new Orddd_Google_Service_Calendar_Event();
            $this->event->setLocation( $location );
            $this->event->setStart( $start );
            $this->event->setEnd( $end );
            $this->event->setSummary( apply_filters(
                'orddd-gcal-set_summary',
                $summary
            ));
            $this->event->setDescription( apply_filters(
                'orddd-gcal-set_description',
                $description
            ));
        }
        
        /**
         * Delete event from Gcal when an order is cancelled.
         * 
         * @param int $order_id Order ID
         * @since 4.0
         */
        function delete_event( $order_id ) {
            if ( !$this->connect() ) {
                return false;
            }
            // calendar ID
            $calendar_id = $this->get_selected_calendar();
            // get the event UID
            $event_uid = '';
            $event_id = '';
               
            $event_uids = get_option( 'orddd_event_uids_ids' );
            if ( is_array( $event_uids ) && count( $event_uids ) > 0 ) {
                if( isset( $event_uids[ $order_id ] ) ) {
                    $event_id = $event_uids[ $order_id ];
                    $event_uid = str_replace( '@google.com', '', $event_id );
                }
            }
            if ( $event_uid != '' && $calendar_id != '' ) {
                try {
                   $deletedEvent = $this->service->events->delete( $calendar_id, $event_uid );
                } catch( Exception $e ) { 
                }
            }
        }
        
        /**
         * Inserts an event to the selected calendar with delivery date & time
         *
         * @globals resource $wpdb
         *
         * @param array $event_details Event details
         * @param int $event_id Even ID
         * @param bool $test Check if it is a test event or not.
         *
         * @return bool True if event inserted successfully, else false.
         * @since 4.0
         */
        function insert_event( $event_details, $event_id, $test = false ) {
            if ( !$this->connect() ) {
                return false;
            }
            global $wpdb;
            $user = get_user_by( 'email', get_option( 'admin_email' ) );
            if( isset( $user->ID ) ) {
                $address_1 = get_user_meta( $user->ID, 'shipping_address_1' );
                $address_2 = get_user_meta( $user->ID, 'shipping_address_2' );
                $first_name = get_user_meta( $user->ID, 'shipping_first_name' );
                $last_name = get_user_meta( $user->ID, 'shipping_last_name' );
                $phone = get_user_meta( $user->ID, 'billing_phone' );
                $city = get_user_meta( $user->ID, 'shipping_city' );
                $full_address = orddd_common::orddd_get_formatted_shipping_customer_address( $user->ID );
            } else {
                $address_1 = "";
                $address_2 = "";
                $first_name = "";
                $last_name = "";
                $phone = "";
                $city = "";
                $full_address = "";
            }
            $app = new stdClass();
            if ( $test ) {
                $app->start = date( 'Y-m-d', $this->local_time );
                $app->end = date( 'Y-m-d', $this->local_time );
                $app->start_time = date( "H:i:s", $this->local_time + 600 );
                $app->end_time = date( 'H:i:s', $this->local_time + 2400 );
                $app->client_email = get_option( 'admin_email' );
                if( isset( $first_name[ 0 ] ) && isset( $last_name[ 0 ] ) ) {
                    $app->client_name = $first_name[ 0 ] . " " . $last_name[ 0 ];
                } else {
                    $app->client_name = "";
                }
                if( isset( $address_1[ 0 ] ) && isset( $address_2[ 0 ] ) ) {
                    $app->client_address = $address_1[ 0 ] . " " . $address_2[ 0 ];
                } else {
                    $app->client_address = "";
                } 
                $app->client_full_address = $full_address;
                if( isset( $city[ 0 ] ) ) {
                    $app->client_city = __( $city[ 0 ], 'order-delivery-date');
                } else {
                    $app->client_city = "";
                }
                
                if( isset( $phone[ 0 ] ) ) {
                    $app->client_phone = $phone[ 0 ];
                } else {
                    $app->client_phone = '';
                }
                $app->order_note  = "";
                $app->order_total  = "";
                $app->products = "";
                $app->product_with_qty = "";
                $app->order_date_time = "";
                $app->order_date = "";
                $app->id = "";
            } else {
                if ( isset( $event_details[ 'h_deliverydate' ] ) && $event_details[ 'h_deliverydate' ] != '' ) {
                    $delivery_date = $event_details[ 'h_deliverydate' ];
                    $order = wc_get_order( $event_id );
                    $app->start = date( 'Y-m-d', strtotime( $delivery_date ) );
                    
                    $end_date = '';
                    if ( has_filter( 'orddd_to_add_end_date_to_gcal' ) ) {
                        $end_date = apply_filters( 'orddd_to_add_end_date_to_gcal', $event_details, $event_id, $test );
                    }
                    if( '' != $end_date ) {
                        $app->end = date( 'Y-m-d', strtotime( $end_date ) );
                    } else {
                        $app->end = date( 'Y-m-d', strtotime( $delivery_date ) );
                    }
                    
                    if( isset( $event_details[ 'time_slot' ] ) && $event_details[ 'time_slot' ] != '' && $event_details[ 'time_slot' ] != 'NA'  && $event_details[ 'time_slot' ] != 'choose' && $event_details[ 'time_slot' ] != 'select' ) {
                        $timeslot = explode( " - ", $event_details[ 'time_slot' ] );
                        $from_time = date( "H:i", strtotime( $timeslot[ 0 ] ) );
                        if( isset( $timeslot[ 1 ] ) && $timeslot[ 1 ] != '' ) {
                            $to_time = date( "H:i", strtotime( $timeslot[ 1 ] ) );
                            $app->end_time = $to_time;
                        } else {
                            $app->end_time = $from_time;
                        }
                        $app->start_time = $from_time;
                    } else if( isset( $event_details[ '_orddd_timestamp' ] ) && $event_details[ '_orddd_timestamp'] != '' ) {
                        $time_settings = date( "H:i", $event_details[ '_orddd_timestamp' ] );
                        if ( $time_settings != '00:01' && $time_settings != '' && $time_settings != '00:00' ) {
                            $app->start_time = $time_settings;
                            $app->end_time = $time_settings;
                        } else {
                            $app->start_time = "";
                            $app->end_time = "";
                        }
                    } else if( isset( $event_details[ 'orddd_time_settings_selected' ] ) && $event_details[ 'orddd_time_settings_selected' ] != '' ) {
                        $from_time = date( "H:i", strtotime( $event_details[ 'orddd_time_settings_selected' ] ) );
                        $app->start_time = $from_time;
                        $app->end_time = $from_time;
                    } else if( get_option( 'orddd_enable_delivery_time' ) == "on" )  {
                        $time_settings_arr = explode( " ", $event_details[ 'e_deliverydate' ] );
                        array_pop( $time_settings_arr );
                        $time_settings = end( $time_settings_arr );
                        $from_time = date( "H:i", strtotime( $time_settings ) );
                        $app->start_time = $from_time;
                        $app->end_time = $from_time;
                    } else {
                        $app->start_time = "";
                        $app->end_time = "";
                    }

                    if ( has_filter( 'orddd_to_add_end_time_to_gcal' ) ) {
                        $app->end_time = apply_filters( 'orddd_to_add_end_time_to_gcal', $event_details, $event_id, $test );
                    }
                        
                    $app->client_email = $event_details[ 'billing_email' ];
                    if ( get_option( 'woocommerce_calc_shipping' ) == 'yes' ) {
                        if ( get_option( 'woocommerce_ship_to_destination' ) == 'shipping' ) {
                            if ( ( isset( $event_details[ 'shipping_first_name' ] ) && $event_details[ 'shipping_first_name' ] != '' ) && ( isset( $event_details[ 'shipping_last_name' ] ) && $event_details[ 'shipping_last_name' ] != '' ) ) {
                                $app->client_name = $event_details[ 'shipping_first_name' ] . " " . $event_details[ 'shipping_last_name' ];
                            } else {
                                if( isset( $event_details[ 'billing_first_name' ] ) && isset( $event_details[ 'billing_last_name' ] ) ) {
                                    $app->client_name = $event_details[ 'billing_first_name' ] . " " . $event_details[ 'billing_last_name' ];
                                } else {
                                    $app->client_name = "";
                                }
                            }
                            
                            if ( ( isset( $event_details[ 'shipping_address_1' ] ) && $event_details[ 'shipping_address_1' ] != '' ) ) {
                                $app->client_address = $event_details[ 'shipping_address_1' ] . " " . $event_details[ 'shipping_address_2' ];
                            } else {
                                $billing_address = '';
                                if( isset( $event_details[ 'billing_address_1' ] ) ) {
                                    $billing_address .= $event_details[ 'billing_address_1' ] . " ";
                                }
                                if( isset( $event_details[ 'billing_address_2' ] ) ) {
                                    $billing_address .= $event_details[ 'billing_address_2' ];
                                }
                                $app->client_address = $billing_address ;
                            }
                            
                            if ( isset( $event_details[ 'shipping_city' ] ) && $event_details[ 'shipping_city' ] != '' ) {
                                $app->client_city = $event_details[ 'shipping_city' ];
                            } else {
                                if( isset( $event_details[ 'billing_city' ] ) ) {
                                    $app->client_city = $event_details[ 'billing_city' ];
                                } else {
                                    $app->client_city = '';
                                }
                            }
                            
                            if( $order->get_formatted_shipping_address() != "" ) {
                                $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_shipping_address() );
                            } else {
                                $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                            }
                        } else if ( get_option( 'woocommerce_ship_to_destination' ) == 'billing' ) {
                            if ( ( isset( $event_details[ 'shipping_first_name' ] ) && $event_details[ 'shipping_first_name' ] != '' ) && ( isset( $event_details[ 'shipping_last_name' ] ) && $event_details[ 'shipping_last_name' ] != '' ) ) {
                                $app->client_name = $event_details[ 'shipping_first_name' ] . " " . $event_details[ 'shipping_last_name' ];
                            } else if( ( isset( $event_details[ 'billing_first_name' ] ) && $event_details[ 'billing_first_name' ] != '' ) && ( isset( $event_details[ 'billing_last_name' ] ) && $event_details[ 'billing_last_name' ] != '' )  ) {
                                $app->client_name = $event_details[ 'billing_first_name' ] . " " . $event_details[ 'billing_last_name' ];
                            } else {
                                $app->client_name = '';
                            }

                            if ( ( isset( $event_details[ 'shipping_address_1' ] ) && $event_details[ 'shipping_address_1' ] != '' ) ) {
                                $app->client_address = $event_details[ 'shipping_address_1' ] . " " . $event_details[ 'shipping_address_2' ];
                            } else if ( ( isset( $event_details[ 'billing_address_1' ] ) && $event_details[ 'billing_address_1' ] != '' ) ) {
                                $app->client_address = $event_details[ 'billing_address_1' ] . " " . $event_details[ 'billing_address_2' ];
                            } else {
                                 $app->client_address = '';
                            }

                            if ( isset( $event_details[ 'shipping_city' ] ) && $event_details[ 'shipping_city' ] != '' ) {
                                $app->client_city = $event_details[ 'shipping_city' ];
                            } else {
                                if ( isset( $event_details[ 'billing_city' ] ) && $event_details[ 'billing_city' ] != '' ) {
                                   $app->client_city = $event_details[ 'billing_city' ];
                                } else {
                                    $app->client_city = "";
                                }
                            }
                            if( $order->get_formatted_shipping_address() != "" ) {
                                $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_shipping_address() );
                            } else {
                                $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                            }
                        } else if ( get_option( 'woocommerce_ship_to_destination' ) == 'billing_only') {
                            if( isset( $event_details[ 'billing_first_name' ] ) && isset( $event_details[ 'billing_last_name' ] ) ) {
                                $app->client_name = $event_details[ 'billing_first_name' ] . " " . $event_details[ 'billing_last_name' ];
                            } else {
                                $app->client_name = "";
                            }
                            
                            if( isset( $event_details[ 'billing_address_1' ] ) && isset( $event_details[ 'billing_address_2' ] ) ) {
                                $app->client_address = $event_details[ 'billing_address_1' ] . " " . $event_details[ 'billing_address_2' ];
                            } else {
                                $app->client_address = "";
                            }
                            
                            if( isset( $event_details[ 'billing_city' ] ) ) {
                                $app->client_city = $event_details[ 'billing_city' ];
                            } else {
                                $app->client_city = "";
                            }
                            $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                        }
                    } else {
                        if( isset( $event_details[ 'billing_first_name' ] ) && isset( $event_details[ 'billing_last_name' ] ) ) {
                            $app->client_name = $event_details[ 'billing_first_name' ] . " " . $event_details[ 'billing_last_name' ];
                        } else {
                            $app->client_name = "";
                        }
                         
                        if( isset( $event_details[ 'billing_address_1' ] ) && isset( $event_details[ 'billing_address_2' ] ) ) {
                            $app->client_address = $event_details[ 'billing_address_1' ] . " " . $event_details[ 'billing_address_2' ];
                        } else {
                            $app->client_address = "";
                        }
                         
                        if( isset( $event_details[ 'billing_city' ] ) ) {
                            $app->client_city = $event_details[ 'billing_city' ];
                        } else {
                            $app->client_city = "";
                        }
                        
                        $app->client_full_address = str_replace( "<br/>", ",", $order->get_formatted_billing_address() );
                    }
                    $app->client_phone = $event_details[ 'billing_phone' ];
                    $app->order_note  = $event_details[ 'order_comments' ];
                    $get_order_items = $order->get_items();
                    $products = $product_with_qty = "";
                    foreach( $get_order_items as $key => $value ) {
                        $products .= $value[ 'name' ] . ", ";
                        $product_with_qty .= $value[ 'name' ] . "(QTY: " . $value[ 'qty' ] . "), ";
                    }
                    $products = substr( $products, 0, strlen( $products )-2 );
                    $product_with_qty = substr( $product_with_qty, 0, strlen( $product_with_qty )-2 );
                    $app->order_total  = $order->get_total();
                    $app->products = $products;
                    $app->product_with_qty = $product_with_qty;

                    $order_post = get_post( $event_id );
                    $app->order_date_time = date( 'Y-m-d H:i:s', strtotime ( $order_post->post_date ) );   
                    $order_date = date( "Y-m-d", strtotime( $order_post->post_date ) );
                    $app->order_date = $order_date;
                    $app->id = $order->get_order_number();                    
                }
            }
            
            // Create Event object and set parameters
            $this->set_event_parameters( $app );
            // Insert event
            try {
                $createdEvent = $this->service->events->insert( $this->get_selected_calendar(), $this->event );
                $uid = $createdEvent->iCalUID;
                
                $event_orders = get_option( 'orddd_event_order_ids' );
                if( $event_orders == '' || $event_orders == '{}' || $event_orders == '[]' || $event_orders == 'null' ) {
                    $event_orders = array();
                }
                array_push( $event_orders, $event_id );
                update_option( 'orddd_event_order_ids', $event_orders );
                
                $event_uids = get_option( 'orddd_event_uids_ids' );
                if( $event_uids == '' || $event_uids == '{}' || $event_uids == '[]' || $event_uids == 'null' ) {
                    $event_uids = array();
                }
                $event_uids[ $event_id ] = $uid;
                update_option( 'orddd_event_uids_ids', $event_uids );
                return true;
            } catch ( Exception $e ) {
                $this->log( "Insert went wrong: " . $e->getMessage() );
                return false;
            }
        }
        
        /**
         * Logs the error if something went wrong.
         * 
         * @param string $message Message to be sent to the log file.
         * @since 4.0
         */
        function log( $message = '' ) {
            if ( $message ) {
                $to_put = '<b>['. date_i18n( $this->datetime_format, $this->local_time ) .']</b> '. $message;
                // Prevent multiple messages with same text and same timestamp
                if ( !file_exists( $this->log_file ) || strpos( @file_get_contents( $this->log_file ), $to_put ) === false )
                    @file_put_contents( $this->log_file, $to_put . chr(10). chr(13), FILE_APPEND );
            }
        }
        
        /**
         * Build GCal url for GCal Button. It requires UTC time.
         * 
         * @param resource $orddd Event Details Object
         *
         * @return string URL 
         * @since 4.0
         */
        function gcal( $orddd ) {
            // Find time difference from Greenwich as GCal asks UTC
            $summary = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY', 'ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER','PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'), 
                array( get_bloginfo( 'name' ), $orddd->client_name, $orddd->products, $orddd->product_with_qty, $orddd->order_date_time, $orddd->order_date, $orddd->id , $orddd->order_total, $orddd->client_phone, $orddd->order_note, $orddd->client_full_address, $orddd->client_address, $orddd->client_email ), $this->get_summary() );
            
            $description = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY', 'ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER','PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'),
                array( get_bloginfo( 'name' ), $orddd->client_name, $orddd->products, $orddd->product_with_qty, $orddd->order_date_time, $orddd->order_date, $orddd->id , $orddd->order_total, $orddd->client_phone, $orddd->order_note, $orddd->client_full_address, $orddd->client_address, $orddd->client_email ), $this->get_description() );
            
            if( $orddd->start_time == "" && $orddd->end_time == "" ) {
                $start = strtotime( $orddd->start );            
                $end = strtotime( $orddd->end . "+1 day");
                
                $gmt_start = get_gmt_from_date( date( 'Y-m-d', $start ), "Ymd" );
                $gmt_end = get_gmt_from_date( date( 'Y-m-d', $end ), "Ymd" );
            } else if( $orddd->end_time == "" ) {
                $start = strtotime( $orddd->start . " " . $orddd->start_time );
                $end = strtotime( $orddd->end . " " . $orddd->start_time );
                
                $gmt_start = get_gmt_from_date( date( 'Y-m-d H:i:s', $start ), "Ymd\THis\Z" );
                $gmt_end = get_gmt_from_date( date( 'Y-m-d H:i:s', $end ), "Ymd\THis\Z" );
            } else {
                $start = strtotime( $orddd->start . " " . $orddd->start_time );
                $end = strtotime( $orddd->end . " " . $orddd->end_time );
                 
                $gmt_start = get_gmt_from_date( date( 'Y-m-d H:i:s', $start ), "Ymd\THis\Z" );
                $gmt_end = get_gmt_from_date( date( 'Y-m-d H:i:s', $end ), "Ymd\THis\Z" );
            }
            
            if ( get_option( 'orddd_calendar_event_location' ) != "" ) {
                $location = str_replace( array( 'FULL_ADDRESS', 'ADDRESS', 'CITY' ), array( $orddd->client_full_address, $orddd->client_address, $orddd->client_city ), get_option( 'orddd_calendar_event_location' ) );
            } else {
                $location = get_bloginfo( 'description' );
            }
            
            $param = array(
                'action' => 'TEMPLATE',
                'text' => $summary,
                'dates' => $gmt_start . "/" . $gmt_end,
                'location' => $location,
                'details' => $description
            );

            return esc_url( add_query_arg( array( $param, $start, $end ),
                'http://www.google.com/calendar/event'
            ) );
        }
        
        /**
         * Build url for Other calendar Button. It requires UTC time.
         * 
         * @param resource $orddd Event Details Object
         *
         * @return string URL 
         * @since 4.0
         */

        function other_cal( $orddd ) {
            // Find time difference from Greenwich as GCal asks UTC
            $summary = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY', 'ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER','PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'), 
                array( get_bloginfo( 'name' ), $orddd->client_name, $orddd->products, $orddd->product_with_qty, $orddd->order_date_time, $orddd->order_date, $orddd->id , $orddd->order_total, $orddd->client_phone, $orddd->order_note, $orddd->client_full_address, $orddd->client_address, $orddd->client_email ), $this->get_summary() );
            
            $description = str_replace( array( 'SITE_NAME', 'CLIENT', 'PRODUCTS', 'PRODUCT_WITH_QTY', 'ORDER_DATE_TIME', 'ORDER_DATE', 'ORDER_NUMBER','PRICE', 'PHONE', 'NOTE', 'FULL_ADDRESS', 'ADDRESS', 'EMAIL'),
                array( get_bloginfo( 'name' ), $orddd->client_name, $orddd->products, $orddd->product_with_qty, $orddd->order_date_time, $orddd->order_date, $orddd->id , $orddd->order_total, $orddd->client_phone, $orddd->order_note, $orddd->client_full_address, $orddd->client_address, $orddd->client_email ), $this->get_description() );
                
            if( $orddd->start_time == "" && $orddd->end_time == "" ) {
                $gmt_start = strtotime( $orddd->start );
                $gmt_end = strtotime( "+1 day", strtotime( $orddd->end ) );
            } else if( $orddd->end_time == "" ) {
                $time_start = explode( ":", $orddd->start_time );
                $gmt_start = strtotime( $orddd->start ) + $time_start[ 0 ]*60*60 + $time_start[ 1 ]*60 + ( time() - current_time('timestamp') );
                $gmt_end = strtotime( $orddd->end ) + $time_start[ 0 ]*60*60 + $time_start[ 1 ]*60 + ( time() - current_time('timestamp') );
            } else {
                $time_start = explode( ":", $orddd->start_time );
                $time_end = explode( ":", $orddd->end_time );
                $gmt_start = strtotime( $orddd->start ) + $time_start[ 0 ]*60*60 + $time_start[ 1 ]*60 + ( time() - current_time('timestamp') );
                $gmt_end = strtotime( $orddd->end ) + $time_end[ 0 ]*60*60 + $time_end[ 1 ]*60 + ( time() - current_time('timestamp') );
            }
                
            if ( get_option( 'orddd_calendar_event_location' ) != "" ) {
                $location = str_replace( array( 'FULL_ADDRESS', 'ADDRESS', 'CITY' ), array( $orddd->client_full_address, $orddd->client_address, $orddd->client_city ), get_option( 'orddd_calendar_event_location' ) );
            } else {
                $location = get_bloginfo( 'description' );
            }
            
            $current_time = current_time( 'timestamp' );
            
            return plugins_url( "order-delivery-date/includes/ical.php?event_date_start=$gmt_start&amp;event_date_end=$gmt_end&amp;current_time=$current_time&amp;summary=$summary&amp;description=$description&amp;event_location=$location" );
        }
    }
}
API documentation generated by ApiGen