| 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 |
|
| 36 |
/** |
| 37 |
* Register cron job for schedule a reminder email |
| 38 |
* |
| 39 |
* @param integer $booking_id |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function register_schedule( $booking_id ) { |
| 44 |
$booking = new Booking( $booking_id ); |
| 45 |
|
| 46 |
$date = $booking->get_start_date(); |
| 47 |
$time = $booking->get_start_time(); |
| 48 |
|
| 49 |
$booking_timestamp = strtotime( $date . ' ' . $time ); |
| 50 |
|
| 51 |
$reminder_time = timetics_get_option( 'remainder_time' ); |
| 52 |
|
| 53 |
if ( ! $reminder_time ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ( $reminder_time as $time ) { |
| 58 |
$timestamp = ''; |
| 59 |
$duration = $time['duration-time']; |
| 60 |
|
| 61 |
switch ( $time['custom_duration_type'] ) { |
| 62 |
case 'min': |
| 63 |
$timestamp = $duration * 60; |
| 64 |
break; |
| 65 |
case 'hour': |
| 66 |
$timestamp = $duration * 60 * 60; |
| 67 |
break; |
| 68 |
case 'day': |
| 69 |
$timestamp = ( $duration * 24 ) * 60 * 60; |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
$timestamp = $booking_timestamp - $timestamp; |
| 74 |
|
| 75 |
if ( ! wp_next_scheduled( 'timetics_booking_remainder_' . $booking_id ) ) { |
| 76 |
wp_schedule_single_event( $timestamp, 'timetics_booking_remainder_' . $booking_id, [$booking_id] ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Booking schedule |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function run_booking_schedule() { |
| 87 |
$bookins = Booking::all(); |
| 88 |
|
| 89 |
if ( ! $bookins ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// Run cron action. |
| 94 |
foreach ( $bookins['items'] as $booking ) { |
| 95 |
add_action( 'timetics_booking_remainder_' . $booking->ID, [$this, 'send_reminder_email'] ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Send booking reminder email |
| 101 |
* |
| 102 |
* @param integer $booking_id |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function send_reminder_email( $booking_id ) { |
| 107 |
$booking_reminder_customer = timetics_get_option( 'booking_reminder_customer' ); |
| 108 |
$booking_reminder_host = timetics_get_option( 'booking_reminder_host' ); |
| 109 |
|
| 110 |
$booking = new Booking( $booking_id ); |
| 111 |
|
| 112 |
if ( $booking_reminder_customer ) { |
| 113 |
$customer_reminder = new Customer_Booking_Reminder_Email( $booking ); |
| 114 |
$customer_reminder->send(); |
| 115 |
} |
| 116 |
|
| 117 |
if ( $booking_reminder_host ) { |
| 118 |
$staff_reminder = new Staff_Booking_Reminder_Email( $booking ); |
| 119 |
$staff_reminder->send(); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Clear cron job schedule |
| 126 |
* |
| 127 |
* @return |
| 128 |
*/ |
| 129 |
public function clear_booking_schedule() { |
| 130 |
$bookins = Booking::all(); |
| 131 |
|
| 132 |
if ( ! $bookins ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// Run cron action. |
| 137 |
foreach ( $bookins['items'] as $booking ) { |
| 138 |
$timestamp = wp_next_scheduled( 'timetics_booking_remainder_' . $booking->ID ); |
| 139 |
|
| 140 |
if ( $timestamp && $timestamp < time() ) { |
| 141 |
wp_unschedule_event( $timestamp, 'timetics_booking_remainder_' . $booking->ID ); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Update bookked entry if reschedule |
| 148 |
* |
| 149 |
* @param integer $booking_id |
| 150 |
* @param integer $customer_id |
| 151 |
* @param integer $meeting_id |
| 152 |
* @param array $data |
| 153 |
* @param integer $booking_entry |
| 154 |
* |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function reschedule_booking( $booking_id, $customer_id, $meeting_id, $data ) { |
| 158 |
$reschedule = ! empty( $data['reschedule'] ) ? $data['reschedule'] : false; |
| 159 |
$booking = new Booking( $booking_id ); |
| 160 |
$meeting = new Appointment( $meeting_id ); |
| 161 |
$booking_entry = new Booking_Entry(); |
| 162 |
|
| 163 |
if ( ! $reschedule ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
$entries = $booking_entry->find( |
| 168 |
[ |
| 169 |
'staff_id' => $booking->get_staff_id(), |
| 170 |
'meeting_id' => $meeting->get_id(), |
| 171 |
'date' => $booking->get_start_date(), |
| 172 |
'start' => $booking->get_start_time(), |
| 173 |
] |
| 174 |
); |
| 175 |
|
| 176 |
if ( ! $entries ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
$entry = $booking_entry->first(); |
| 181 |
$booked_seat = ! empty( $booking->get_seat() ) ? $booking->get_seat() : []; |
| 182 |
$existing_seat = ! empty( $entry->get_seats() ) ? $entry->get_seats() : []; |
| 183 |
|
| 184 |
if ( 'one-to-one' === strtolower( $meeting->get_type() ) ) { |
| 185 |
$entry->delete(); |
| 186 |
} else { |
| 187 |
$booked = intval( $entry->get_booked() ) - 1; |
| 188 |
|
| 189 |
$entry->update( [ |
| 190 |
'booked' => $booked, |
| 191 |
'seats' => array_values( array_diff( $existing_seat, $booked_seat ) ), |
| 192 |
] ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Register booking statuses |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function register_booking_status() { |
| 202 |
$statuses = timetics_get_post_status(); |
| 203 |
|
| 204 |
foreach ( $statuses as $status ) { |
| 205 |
|
| 206 |
register_post_status( $status, array( |
| 207 |
'public' => true, |
| 208 |
'exclude_from_search' => false, |
| 209 |
'show_in_admin_all_list' => false, |
| 210 |
'show_in_admin_status_list' => false, |
| 211 |
'label_count' => _n_noop( "$status (%s)", "$status (%s)", 'timetics' ), |
| 212 |
) ); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|