| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking related hooks |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Bookings; |
| 9 |
|
| 10 |
use Timetics\Core\Appointments\Appointment; |
| 11 |
use Timetics\Core\Emails\Customer_Booking_Reminder_Email; |
| 12 |
use Timetics\Core\Emails\Staff_Booking_Reminder_Email; |
| 13 |
use Timetics\Utils\Singleton; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Hooks |
| 17 |
*/ |
| 18 |
class Hooks { |
| 19 |
use Singleton; |
| 20 |
|
| 21 |
/** |
| 22 |
* Initialization |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function init() { |
| 27 |
add_action( 'timetics_after_booking_create', [$this, 'register_schedule'] ); |
| 28 |
add_action( 'init', [$this, 'run_booking_schedule'] ); |
| 29 |
add_action( 'timetics_booking_clear_schedule', [$this, 'clear_booking_schedule'] ); |
| 30 |
|
| 31 |
add_action( 'timetics_after_booking_create', [$this, 'reschedule_booking'], 10, 4 ); |
| 32 |
|
| 33 |
add_action( 'init', [$this, 'register_booking_status'] ); |
| 34 |
|
| 35 |
add_action( 'admin_init', [$this, 'delete_booking_before_paid'] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register cron job for schedule a reminder email |
| 40 |
* |
| 41 |
* @param integer $booking_id |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function register_schedule( $booking_id ) { |
| 46 |
$booking = new Booking( $booking_id ); |
| 47 |
|
| 48 |
$date = $booking->get_start_date(); |
| 49 |
$time = $booking->get_start_time(); |
| 50 |
|
| 51 |
$booking_timestamp = strtotime( $date . ' ' . $time ); |
| 52 |
|
| 53 |
$reminder_time = timetics_get_option( 'remainder_time' ); |
| 54 |
|
| 55 |
if ( ! $reminder_time ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
foreach ( $reminder_time as $time ) { |
| 60 |
$timestamp = ''; |
| 61 |
$duration = $time['duration-time']; |
| 62 |
|
| 63 |
switch ( $time['custom_duration_type'] ) { |
| 64 |
case 'min': |
| 65 |
$timestamp = $duration * 60; |
| 66 |
break; |
| 67 |
case 'hour': |
| 68 |
$timestamp = $duration * 60 * 60; |
| 69 |
break; |
| 70 |
case 'day': |
| 71 |
$timestamp = ( $duration * 24 ) * 60 * 60; |
| 72 |
break; |
| 73 |
} |
| 74 |
|
| 75 |
$timestamp = $booking_timestamp - $timestamp; |
| 76 |
|
| 77 |
if ( ! wp_next_scheduled( 'timetics_booking_remainder_' . $booking_id ) ) { |
| 78 |
wp_schedule_single_event( $timestamp, 'timetics_booking_remainder_' . $booking_id, [$booking_id] ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Booking schedule |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function run_booking_schedule() { |
| 89 |
$bookins = Booking::all(); |
| 90 |
|
| 91 |
if ( ! $bookins ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// Run cron action. |
| 96 |
foreach ( $bookins['items'] as $booking ) { |
| 97 |
add_action( 'timetics_booking_remainder_' . $booking->ID, [$this, 'send_reminder_email'] ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Send booking reminder email |
| 103 |
* |
| 104 |
* @param integer $booking_id |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function send_reminder_email( $booking_id ) { |
| 109 |
$booking_reminder_customer = timetics_get_option( 'booking_reminder_customer' ); |
| 110 |
$booking_reminder_host = timetics_get_option( 'booking_reminder_host' ); |
| 111 |
|
| 112 |
$booking = new Booking( $booking_id ); |
| 113 |
|
| 114 |
if ( $booking_reminder_customer ) { |
| 115 |
$customer_reminder = new Customer_Booking_Reminder_Email( $booking ); |
| 116 |
$customer_reminder->send(); |
| 117 |
} |
| 118 |
|
| 119 |
if ( $booking_reminder_host ) { |
| 120 |
$staff_reminder = new Staff_Booking_Reminder_Email( $booking ); |
| 121 |
$staff_reminder->send(); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Clear cron job schedule |
| 128 |
* |
| 129 |
* @return |
| 130 |
*/ |
| 131 |
public function clear_booking_schedule() { |
| 132 |
$bookins = Booking::all(); |
| 133 |
|
| 134 |
if ( ! $bookins ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// Run cron action. |
| 139 |
foreach ( $bookins['items'] as $booking ) { |
| 140 |
$timestamp = wp_next_scheduled( 'timetics_booking_remainder_' . $booking->ID ); |
| 141 |
|
| 142 |
if ( $timestamp && $timestamp < time() ) { |
| 143 |
wp_unschedule_event( $timestamp, 'timetics_booking_remainder_' . $booking->ID ); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Update bookked entry if reschedule |
| 150 |
* |
| 151 |
* @param integer $booking_id |
| 152 |
* @param integer $customer_id |
| 153 |
* @param integer $meeting_id |
| 154 |
* @param array $data |
| 155 |
* @param integer $booking_entry |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function reschedule_booking( $booking_id, $customer_id, $meeting_id, $data ) { |
| 160 |
$reschedule = ! empty( $data['reschedule'] ) ? $data['reschedule'] : false; |
| 161 |
$booking = new Booking( $booking_id ); |
| 162 |
$meeting = new Appointment( $meeting_id ); |
| 163 |
$booking_entry = new Booking_Entry(); |
| 164 |
|
| 165 |
if ( ! $reschedule ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$entries = $booking_entry->find( |
| 170 |
[ |
| 171 |
'staff_id' => $booking->get_staff_id(), |
| 172 |
'meeting_id' => $meeting->get_id(), |
| 173 |
'date' => $booking->get_start_date(), |
| 174 |
'start' => $booking->get_start_time(), |
| 175 |
] |
| 176 |
); |
| 177 |
|
| 178 |
if ( ! $entries ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
$entry = $booking_entry->first(); |
| 183 |
$booked_seat = ! empty( $booking->get_seat() ) ? $booking->get_seat() : []; |
| 184 |
$existing_seat = ! empty( $entry->get_seats() ) ? $entry->get_seats() : []; |
| 185 |
|
| 186 |
if ( 'one-to-one' === strtolower( $meeting->get_type() ) ) { |
| 187 |
$entry->delete(); |
| 188 |
} else { |
| 189 |
$booked = intval( $entry->get_booked() ) - 1; |
| 190 |
|
| 191 |
$entry->update( [ |
| 192 |
'booked' => $booked, |
| 193 |
'seats' => array_values( array_diff( $existing_seat, $booked_seat ) ), |
| 194 |
] ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Register booking statuses |
| 200 |
* |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
public function register_booking_status() { |
| 204 |
$statuses = timetics_get_post_status(); |
| 205 |
|
| 206 |
foreach ( $statuses as $status ) { |
| 207 |
|
| 208 |
register_post_status( $status, array( |
| 209 |
'public' => true, |
| 210 |
'exclude_from_search' => false, |
| 211 |
'show_in_admin_all_list' => false, |
| 212 |
'show_in_admin_status_list' => false, |
| 213 |
'label_count' => _n_noop( "$status (%s)", "$status (%s)", 'timetics' ), |
| 214 |
) ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Delete bookings if unpaid before 30 mins |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public function delete_booking_before_paid() { |
| 224 |
$args = [ |
| 225 |
'post_type' => 'timetics-booking', |
| 226 |
'numberposts' => -1, |
| 227 |
'meta_query' => array( |
| 228 |
'relation' => 'OR', |
| 229 |
array( |
| 230 |
'key' => '_tt_booking_payment_method', |
| 231 |
'value' => 'stripe', |
| 232 |
'compare' => '=', |
| 233 |
), |
| 234 |
array( |
| 235 |
'key' => '_tt_booking_payment_method', |
| 236 |
'value' => 'paypal', |
| 237 |
'compare' => '=', |
| 238 |
), |
| 239 |
), |
| 240 |
]; |
| 241 |
|
| 242 |
$bookings = get_posts( $args ); |
| 243 |
|
| 244 |
foreach ( $bookings as $booking ) { |
| 245 |
$booking = new Booking( $booking->ID ); |
| 246 |
|
| 247 |
if ( 'completed' != $booking->get_status() && $this->is_booking_payment_expire( $booking ) ) { |
| 248 |
$this->update_booking_entry( $booking->get_id() ); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Check booking payment time expaire or not |
| 255 |
* |
| 256 |
* @param Object $booking |
| 257 |
* |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
public function is_booking_payment_expire( $booking ) { |
| 261 |
// Booking date and time |
| 262 |
$post = get_post( $booking->get_id() ); |
| 263 |
$booking_datetime = $post->post_date; |
| 264 |
|
| 265 |
// Convert the booking date and time to a DateTime object |
| 266 |
$booking_datetime_object = new \DateTime( $booking_datetime ); |
| 267 |
|
| 268 |
// Calculate 30 minutes from the booking date and time |
| 269 |
$target_datetime = clone $booking_datetime_object; |
| 270 |
$target_datetime->modify( '+30 minutes' ); |
| 271 |
|
| 272 |
// Get the current date and time |
| 273 |
$current_datetime = new \DateTime(); |
| 274 |
|
| 275 |
// Check if 30 minutes have passed |
| 276 |
if ( $current_datetime > $target_datetime ) { |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Update booking entry if payment time expire |
| 285 |
* |
| 286 |
* @param integer $booking_id |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function update_booking_entry( $booking_id ) { |
| 291 |
$booking = new Booking( $booking_id ); |
| 292 |
$meeting = new Appointment( $booking->get_appointment() ); |
| 293 |
|
| 294 |
if ( ! $booking->is_booking() ) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
$current_user_id = get_current_user_id(); |
| 299 |
|
| 300 |
if ( |
| 301 |
$meeting->is_appointment() |
| 302 |
&& ! user_can( $current_user_id, 'manage_options' ) |
| 303 |
&& $meeting->get_author() != $current_user_id |
| 304 |
) { |
| 305 |
$data = [ |
| 306 |
'success' => 0, |
| 307 |
'message' => __( 'You are not allowed to delete this booking.', 'timetics' ), |
| 308 |
]; |
| 309 |
|
| 310 |
return new \WP_HTTP_Response( $data, 403 ); |
| 311 |
} |
| 312 |
|
| 313 |
$booking_entry = new Booking_Entry(); |
| 314 |
|
| 315 |
$date_time = timetics_convert_timezone( $booking->get_start_date() . ' ' . $booking->get_start_time(), $booking->get_timezone(), $meeting->get_timezone() ); |
| 316 |
|
| 317 |
$entries = $booking_entry->find( |
| 318 |
[ |
| 319 |
'staff_id' => $booking->get_staff_id(), |
| 320 |
'meeting_id' => $booking->get_appointment(), |
| 321 |
'date' => $date_time->format( 'Y-m-d' ), |
| 322 |
'start' => $date_time->format( 'h:i a' ), |
| 323 |
] |
| 324 |
); |
| 325 |
|
| 326 |
if ( $entries ) { |
| 327 |
$entry = $booking_entry->first(); |
| 328 |
|
| 329 |
if ( 'one-to-one' == strtolower( $meeting->get_type() ) ) { |
| 330 |
$entry->delete(); |
| 331 |
} else { |
| 332 |
$booked = intval( $entry->get_booked() ) - 1; |
| 333 |
$booked_seat = ! empty( $booking->get_seat() ) ? $booking->get_seat() : []; |
| 334 |
$existing_seat = ! empty( $entry->get_seats() ) ? $entry->get_seats() : []; |
| 335 |
|
| 336 |
$entry->update( [ |
| 337 |
'booked' => $booked, |
| 338 |
'seats' => array_values( array_diff( $existing_seat, $booked_seat ) ), |
| 339 |
] ); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|