Server : LiteSpeed
System : Linux premium92.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
User : rbnsfqys ( 805)
PHP Version : 8.1.33
Disable Function : NONE
Directory :  /home/rbnsfqys/ali.rbn.services/wp-content/plugins/Repairplugin-pro/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]


Current File : /home/rbnsfqys/ali.rbn.services/wp-content/plugins/Repairplugin-pro/google_calendar_integration.php
<?php

// Exit if accessed directly

defined( 'ABSPATH' ) || exit;

function rp_get_google_calendar_api_context() {

    $gClientId = rp_get_option('rp_google_auth_client_id', '');

    $gClientSecret = rp_get_option('rp_google_auth_client_secret', '');

    $googleCalendar = new RepairPluginPro\GoogleCalendar( $gClientId, $gClientSecret );

    $isAuthorized = $googleCalendar->isAuthorized();

    if( $isAuthorized == TRUE ) {

        return $googleCalendar;

    }

    return FALSE;

}

function rp_need_to_create_google_calendar_entry() {

    $need_to_create = rp_get_option('rp_enable_creating_google_calendar_events', '0');

    if( $need_to_create == '1' ) {

        return TRUE;

    }

    return FALSE;

}

function rp_get_booking_slots_length() {

    global $rpQuery;

    $rs_planning_and_discount = $rpQuery->prefix . "rs_planning_and_discount";

    $planning_discount = $rpQuery->get_results("SELECT * FROM $rs_planning_and_discount");

    $time_slots = $planning_discount[0]->booking_slots_length;

    $time_slots = str_replace(strtolower(' Minutes (Default)'), '', strtolower($time_slots));

    $time_slots = trim(str_replace('minutes', '', strtolower($time_slots)));

    $time_slots = (int) $time_slots;

    if( $time_slots < 1 ) {

        $time_slots = 30;

    }

    return $time_slots;

}

function rp_get_company_location_by_id( $location_id = 0 ) {

    if( empty( $location_id ) ) {

        return FALSE;

    }

    global $rpQuery;

    $company_location = $rpQuery->prefix . "rs_company_location";

    $row = $rpQuery->get_row("SELECT * FROM `$company_location` WHERE `location_id` = '".rp_escape_sql($location_id)."'");

    return $row;

}

function rp_get_google_calendar_event_details( $oid = 0 ) {

    global $rpQuery;

    $rs_orders = $rpQuery->prefix . "rs_orders";

    $rs_customer = $rpQuery->prefix . "rs_customer";

    $row = $rpQuery->get_row("SELECT * FROM `$rs_orders` WHERE `o_id` = '".rp_escape_sql($oid)."'");

    if( empty( $row ) ) {

        return array();

    }

    $customer_row = $rpQuery->get_row("SELECT * FROM `$rs_customer` WHERE `customer_id` = '".rp_escape_sql($row->customer_id_fk)."'");

    $customer_name = 'N/A';

    if( !empty( $customer_row ) ) {

        $customer_name = $customer_row->customer_name;

    }

    $delivery_method = trim( strtolower( $row->delivery_method ?? '' ) );

    $delivery_date = $row->delivery_date ?? '';

    if( strpos($delivery_date, '-') === 2 ) {
        
        // it is d-m-Y so convert it to Y-m-d
        $delivery_date = explode('-', $delivery_date);

        // convert d-m-Y to Y-m-d
        $delivery_date = $delivery_date[2].'-'.$delivery_date[1].'-'.$delivery_date[0];

    }

    if( $delivery_method == 'come by our store' ) {

        $theTime = date('H:i', strtotime( $delivery_date . ' ' . $row->delivery_time ?? '' ));

        $startTime = $delivery_date.'T'.trim($theTime).':00';

        $slot_time = rp_get_booking_slots_length();

        $endTime = date('Y-m-d\TH:i:s', strtotime($delivery_date.' '.$theTime . ' +'.$slot_time.' minutes'));

        $company_location = rp_get_company_location_by_id( $row->location_id_fk );

        $location = '';

        if( !empty( $company_location ) ) {

            $location = str_replace(array('<br>', "\n", "\r"), ', ', $company_location->location_name . ', ' . $company_location->location_address . ', ' . $company_location->location_city_district);

        }

    } else if( $delivery_method == 'pickup service' || $delivery_method == 'repair on location' ) {

        $theTime = $row->delivery_time ?? '';

        $theTime = explode('—', $theTime);

        $theTime[0] = date('H:i', strtotime( $delivery_date . ' ' . $theTime[0] ?? '' ));

        $theTime[1] = date('H:i', strtotime( $delivery_date . ' ' . $theTime[1] ?? '' ));

        $startTime = $delivery_date.'T'.trim($theTime[0] ?? '').':00';

        $endTime = $delivery_date.'T'.trim($theTime[1] ?? '').':00';

        $location = $customer_row->customer_house_no.' '.$customer_row->customer_street_address.', '.$customer_row->customer_city.', '.$customer_row->customer_zipcode;

        if( !empty( $customer_row->customer_country ?? '' ) ) {

            $location .= ', '.$customer_row->customer_country;

        }

    } else {

        return array();

    }

    $timezone = wp_timezone_string();

    $appointment_description = 'You have an appointment with '.$customer_name.'.';

    $trigger_payload = rp_get_order_trigger_payload( 'scheduled_appointment_created', $oid );

    // customer name
    $appointment_description .= "\n\nCustomer Name: ".$customer_name;

    // customer email
    $appointment_description .= "\n".'Customer Email: '.$customer_row->customer_email;

    // add customer phone
    $appointment_description .= "\n".'Customer Phone: '.$customer_row->customer_phone;

    // add brand ($trigger_payload['brand'] ?? '')
    $appointment_description .= "\n".'Brand: '.($trigger_payload['brand'] ?? '');

    // add model ($trigger_payload['model'] ?? '')
    $appointment_description .= "\n".'Model: '.($trigger_payload['model'] ?? '');

    // add color ($trigger_payload['color'] ?? '')
    $appointment_description .= "\n".'Color: '.($trigger_payload['color'] ?? '');

    $items = $trigger_payload['items'] ?? array();

    if( !empty( $items ) ) {

        $item_count = 1;

        foreach( $items as $item ) {

            $appointment_description .= "\nItem ".$item_count." Name: ".$item['name'];

            $item_count++;

        }

    }

    // add customer notes
    $appointment_description .= "\n".'Customer Notes: '.($trigger_payload['customer_notes'] ?? '');

    // add delivery method ($trigger_payload['delivery_method'] ?? '')
    $appointment_description .= "\n".'Service Method: '.($trigger_payload['delivery_method'] ?? '');

    list( $payment_method, $payment_status ) = rp_get_payment_method_and_status_english( $row );

    // add payment method
    if( !empty( $payment_method ) ) {

        $appointment_description .= "\n".'Payment Method: '.$payment_method;

    }

    // add payment status
    if( !empty( $payment_status ) ) {

        $appointment_description .= "\n".'Payment Status: '.$payment_status;

    }

    $eventData = array(
        'summary' => 'Appointment #'.$row->appointment_number,
        'description' => $appointment_description,
        'start' => array(
            'dateTime' => $startTime,
            'timeZone' => $timezone
        ),
        'end' => array(
            'dateTime' => $endTime,
            'timeZone' => $timezone
        ),
        'location' => $location
    );

    return $eventData;

}

function rp_have_google_calendar_entry( $oid = 0 ) {

    global $rpQuery;

    $rs_google_calendar = $rpQuery->prefix . "rs_google_calendar";

    $row = $rpQuery->get_row("SELECT * FROM `$rs_google_calendar` WHERE `o_id` = '".rp_escape_sql($oid)."'");

    if( empty( $row ) ) {

        return FALSE;

    }

    return TRUE;

}

function rp_handle_google_calendar_integration( $action = '', $oid = 0 ) {

    if( empty( $action ) || empty( $oid ) ) {

        return;

    }

    $need_to_create = rp_need_to_create_google_calendar_entry();

    if( ( $action == 'scheduled_appointment_created' || $action == 'scheduled_appointment_updated' ) && $need_to_create == FALSE ) {

        return;

    }

    $google_calendar_entry = rp_have_google_calendar_entry( $oid );

    if( ( $action == 'scheduled_appointment_cancelled' || $action == 'scheduled_appointment_deleted' ) && $google_calendar_entry == FALSE ) {

        return;

    }

    if( $action == 'scheduled_appointment_created' || $action == 'scheduled_appointment_updated' ) {

        rp_create_or_update_google_calendar_safe( $oid );

    }

    if( $action == 'scheduled_appointment_cancelled' || $action == 'scheduled_appointment_deleted' ) {

        rp_delete_google_calendar_event_safe( $oid );

    }

}

function rp_create_or_update_google_calendar_safe( $oid = 0 ) {

    try {

        rp_create_or_update_google_calendar( $oid );

    } catch (Exception|Error $e) {

        // do nothing

    }
    
}

function rp_create_or_update_google_calendar( $oid = 0 ) {

    global $rpQuery;

    $googleCalendar = rp_get_google_calendar_api_context();

    if( $googleCalendar == FALSE ) {

        return;

    }

    $gSelectedCalendar = rp_get_option('rp_google_selected_calendar', '');

    if( empty( $gSelectedCalendar ) ) {

        return;

    }

    $allCalendars = $googleCalendar->getAllCalendars();

    if( !empty( $allCalendars ) && !empty( $allCalendars['items'] ) ) {

        $allCalendars = $allCalendars['items'];

    } else {

        $allCalendars = array();

    }

    $verifiedCalendarId = '';

    foreach( $allCalendars as $calendar ) {

        if( ( $calendar['id'] ?? '' )== $gSelectedCalendar ) {

            $verifiedCalendarId = $calendar['id'];

            break;

        }

    }

    if( empty( $verifiedCalendarId ) ) {

        return;

    }

    $connected_account_email = $googleCalendar->getUserEmail();

    if( empty( $connected_account_email ) ) {

        return;

    }

    $client_hash = $googleCalendar->getClientHash();

    if( empty( $client_hash ) ) {

        return;

    }

    $event_details = rp_get_google_calendar_event_details( $oid );

    if( empty( $event_details ) ) {

        return;

    }

    $rs_google_calendar = $rpQuery->prefix . "rs_google_calendar";

    $row = $rpQuery->get_row("SELECT * FROM `$rs_google_calendar` WHERE `o_id` = '".rp_escape_sql($oid)."' AND `calendar_id` = '".rp_escape_sql($verifiedCalendarId)."' AND `connected_account_email` = '".rp_escape_sql($connected_account_email)."' AND `client_hash` = '".rp_escape_sql($client_hash)."'");

    if( !empty( $row ) ) {

        // update
        $updatedEvent = $googleCalendar->updateEvent( $verifiedCalendarId, $row->event_id, $event_details );

    } else {

        $need_to_create = rp_need_to_create_google_calendar_entry();

        if( $need_to_create == FALSE ) {

            return;

        }

        // create
        $createdEvent = $googleCalendar->createEvent( $verifiedCalendarId, $event_details );

        if( !empty( $createdEvent ) ) {

            $createdEventId = $createdEvent['id'] ?? 0;

            if( !empty( $createdEventId ) ) {

                $rpQuery->query("INSERT INTO `$rs_google_calendar` (`o_id`, `calendar_id`, `connected_account_email`, `event_id`, `client_hash`) VALUES ('".rp_escape_sql($oid)."', '".rp_escape_sql($verifiedCalendarId)."', '".rp_escape_sql($connected_account_email)."', '".rp_escape_sql($createdEventId)."', '".rp_escape_sql($client_hash)."')");

            }

        }

    }

}

function rp_delete_google_calendar_event_safe( $oid = 0 ) {

    try {

        rp_delete_google_calendar_event( $oid );

    } catch (Exception|Error $e) {

        // do nothing

    }

}

function rp_delete_google_calendar_event( $oid = 0 ) {

    global $rpQuery;

    $googleCalendar = rp_get_google_calendar_api_context();

    if( $googleCalendar == FALSE ) {

        return;

    }

    $connected_account_email = $googleCalendar->getUserEmail();

    if( empty( $connected_account_email ) ) {

        return;

    }

    $client_hash = $googleCalendar->getClientHash();

    if( empty( $client_hash ) ) {

        return;

    }

    $rs_google_calendar = $rpQuery->prefix . "rs_google_calendar";

    $results = $rpQuery->get_results("SELECT * FROM `$rs_google_calendar` WHERE `o_id` = '".rp_escape_sql($oid)."' AND `connected_account_email` = '".rp_escape_sql($connected_account_email)."' AND `client_hash` = '".rp_escape_sql($client_hash)."'");

    if( empty( $results ) ) {

        return;

    }

    $allCalendars = $googleCalendar->getAllCalendars();

    if( !empty( $allCalendars ) && !empty( $allCalendars['items'] ) ) {

        $allCalendars = $allCalendars['items'];

    } else {

        $allCalendars = array();

    }

    foreach( $results as $row ) {

        $gSelectedCalendar = $row->calendar_id;

        $verifiedCalendarId = '';

        foreach( $allCalendars as $calendar ) {

            if( ( $calendar['id'] ?? '' )== $gSelectedCalendar ) {

                $verifiedCalendarId = $calendar['id'];

                break;

            }

        }

        if( empty( $verifiedCalendarId ) ) {

            return;

        }

        $eventId = $row->event_id;

        $rpQuery->query("DELETE FROM `$rs_google_calendar` WHERE `id` = '".rp_escape_sql($row->id)."'");

        $googleCalendar->deleteEvent( $verifiedCalendarId, $eventId );

    }

}

F1le Man4ger