| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking Calendar |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\BookingCalendars; |
| 9 |
|
| 10 |
use Timetics\Base\Api; |
| 11 |
use Timetics\Core\Appointments\Appointment; |
| 12 |
use Timetics\Utils\Singleton; |
| 13 |
use DateTimeZone; |
| 14 |
use DateTime; |
| 15 |
|
| 16 |
class Api_Booking_Calendar extends Api { |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
/** |
| 20 |
* Store api namespace |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $namespace = 'timetics/v1'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Store rest base |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $rest_base = 'booking-calendars'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Register rest routes |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register_routes() { |
| 39 |
|
| 40 |
register_rest_route( |
| 41 |
$this->namespace, $this->rest_base, array( |
| 42 |
array( |
| 43 |
'methods' => \WP_REST_Server::READABLE, |
| 44 |
'callback' => array( $this, 'get_booking_calendars' ), |
| 45 |
'permission_callback' => '__return_true', |
| 46 |
), |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get calendar data |
| 53 |
* |
| 54 |
* @param \WP_REST_Request $request |
| 55 |
* @return \WP_REST_Response |
| 56 |
*/ |
| 57 |
public function get_booking_calendars( $request ) { |
| 58 |
$meeting_id = ! empty( $request['meeting_id'] ) ? intval( $request['meeting_id'] ) : 0; |
| 59 |
$booking_date = ! empty( $request['booking_date'] ) ? sanitize_text_field( $request['booking_date'] ) : ''; |
| 60 |
$start_time = ! empty( $request['start_time'] ) ? sanitize_text_field( $request['start_time'] ) : ''; |
| 61 |
$end_time = ! empty( $request['end_time'] ) ? sanitize_text_field( $request['end_time'] ) : ''; |
| 62 |
$location = ! empty( $request['location'] ) ? sanitize_text_field( $request['location'] ) : ''; |
| 63 |
|
| 64 |
$meeting = new Appointment( $meeting_id ); |
| 65 |
|
| 66 |
$google_url = $this->get_google_calendar_url( |
| 67 |
array( |
| 68 |
'date' => $booking_date, |
| 69 |
'start_time' => $start_time, |
| 70 |
'end_time' => $end_time, |
| 71 |
'location' => $location, |
| 72 |
'title' => $meeting->get_name(), |
| 73 |
'description' => $meeting->get_description(), |
| 74 |
'timezone' => $meeting->get_timezone(), |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
$calendars = array(); |
| 79 |
|
| 80 |
if ( $google_url ) { |
| 81 |
$calendars[] = array( |
| 82 |
'id' => 'google', |
| 83 |
'name' => esc_html__( 'Google', 'timetics' ), |
| 84 |
'url' => esc_url_raw( $google_url ), |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
// Filter to ensure calendar links can be added from timetics-pro also |
| 89 |
$calendars = apply_filters( 'timetics_booking_calendar_urls', $calendars, $request ); |
| 90 |
|
| 91 |
$data = array( |
| 92 |
'success' => 1, |
| 93 |
'status_code' => 200, |
| 94 |
'calendars' => $calendars, |
| 95 |
); |
| 96 |
|
| 97 |
return rest_ensure_response( $data ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get Google Calendar URL that will be used to add event to Google Calendar |
| 102 |
* |
| 103 |
* @param array $args { |
| 104 |
* @type string $date Booking date in 'Y-m-d' format. |
| 105 |
* @type string $start_time Start time in 'H:i' format. |
| 106 |
* @type string $end_time End time in 'H:i' format. |
| 107 |
* @type string $location Event location. |
| 108 |
* @type string $title Event title. |
| 109 |
* @type string $description Event description. |
| 110 |
* @type string $timezone Event timezone. |
| 111 |
* } |
| 112 |
* |
| 113 |
* @return string Google Calendar URL |
| 114 |
*/ |
| 115 |
private function get_google_calendar_url( $args = array() ) { |
| 116 |
$defaults = array( |
| 117 |
'date' => '', |
| 118 |
'start_time' => '', |
| 119 |
'end_time' => '', |
| 120 |
'location' => '', |
| 121 |
'title' => '', |
| 122 |
'description' => '', |
| 123 |
'timezone' => '', |
| 124 |
); |
| 125 |
|
| 126 |
$args = wp_parse_args( $args, $defaults ); |
| 127 |
|
| 128 |
if ( empty( $args['date'] ) || empty( $args['start_time'] ) || empty( $args['end_time'] ) ) { |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
// Combine and convert to UTC datetime strings |
| 133 |
try { |
| 134 |
$date = $args['date']; |
| 135 |
$start_time = $args['start_time']; |
| 136 |
$end_time = $args['end_time']; |
| 137 |
$timezone_string = $args['timezone'] ?: 'UTC'; // e.g., 'Asia/Dhaka' |
| 138 |
$timezone = new DateTimeZone($timezone_string); |
| 139 |
|
| 140 |
// Try 24-hour format first |
| 141 |
$start = DateTime::createFromFormat('Y-m-d H:i', "$date $start_time", $timezone); |
| 142 |
$end = DateTime::createFromFormat('Y-m-d H:i', "$date $end_time", $timezone); |
| 143 |
|
| 144 |
if ( ! $start ) { |
| 145 |
$start = DateTime::createFromFormat('Y-m-d h:i A', "$date $start_time", $timezone); |
| 146 |
} |
| 147 |
if ( ! $end ) { |
| 148 |
$end = DateTime::createFromFormat('Y-m-d h:i A', "$date $end_time", $timezone); |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! $start || ! $end ) { |
| 152 |
return ''; |
| 153 |
} |
| 154 |
|
| 155 |
// Convert to UTC for Google Calendar |
| 156 |
$start_utc = clone $start; |
| 157 |
$end_utc = clone $end; |
| 158 |
$start_utc->setTimezone(new DateTimeZone('UTC')); |
| 159 |
$end_utc->setTimezone(new DateTimeZone('UTC')); |
| 160 |
|
| 161 |
$start_utc_str = $start_utc->format('Ymd\THis\Z'); |
| 162 |
$end_utc_str = $end_utc->format('Ymd\THis\Z'); |
| 163 |
|
| 164 |
} catch (\Throwable $e) { |
| 165 |
return ''; |
| 166 |
} |
| 167 |
|
| 168 |
$params = array( |
| 169 |
'action' => 'TEMPLATE', |
| 170 |
'text' => $args['title'], |
| 171 |
'dates' => $start_utc_str . '/' . $end_utc_str, |
| 172 |
'details' => $args['description'], |
| 173 |
'location' => $args['location'], |
| 174 |
'sf' => 'true', |
| 175 |
'output' => 'xml', |
| 176 |
'ctz' => $timezone_string, |
| 177 |
); |
| 178 |
|
| 179 |
return 'https://calendar.google.com/calendar/render?' . http_build_query( $params ); |
| 180 |
} |
| 181 |
} |
| 182 |
|