PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
1.0.64 1.0.62 1.0.63 1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 All 65 releases
timetics / core / booking-calendars / api-booking-calendar.php

api-booking-calendar.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.64, at core/booking-calendars/api-booking-calendar.php

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