| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking related hooks |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Bookings; |
| 9 |
|
| 10 |
use Timetics\Core\Emails\Booking_Reminder_Email; |
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Hooks |
| 15 |
*/ |
| 16 |
class Hooks { |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialization |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function init() { |
| 25 |
add_action( 'timetics_after_booking_create', [$this, 'register_schedule'] ); |
| 26 |
add_action( 'init', [$this, 'run_booking_schedule'] ); |
| 27 |
add_action( 'timetics_booking_clear_schedule', [$this, 'clear_booking_schedule'] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register cron job for schedule a reminder email |
| 32 |
* |
| 33 |
* @param integer $booking_id |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_schedule( $booking_id ) { |
| 38 |
$booking = new Booking( $booking_id ); |
| 39 |
|
| 40 |
$date = $booking->get_start_date(); |
| 41 |
$time = $booking->get_start_time(); |
| 42 |
|
| 43 |
$booking_timestamp = strtotime( $date . ' ' . $time ); |
| 44 |
|
| 45 |
$reminder_time = timetics_get_option( 'remainder_time' ); |
| 46 |
|
| 47 |
if ( ! $reminder_time ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
foreach ( $reminder_time as $time ) { |
| 52 |
$timestamp = ''; |
| 53 |
$duration = $time['duration-time']; |
| 54 |
|
| 55 |
switch ( $time['custom_duration_type'] ) { |
| 56 |
case 'min': |
| 57 |
$timestamp = $duration * 60; |
| 58 |
break; |
| 59 |
case 'hour': |
| 60 |
$timestamp = $duration * 60 * 60; |
| 61 |
break; |
| 62 |
case 'day': |
| 63 |
$timestamp = ( $duration * 24 ) * 60 * 60; |
| 64 |
break; |
| 65 |
} |
| 66 |
|
| 67 |
$timestamp = $booking_timestamp - $timestamp; |
| 68 |
|
| 69 |
if ( ! wp_next_scheduled( 'timetics_booking_remainder_' . $booking_id ) ) { |
| 70 |
wp_schedule_single_event( $timestamp, 'timetics_booking_remainder_' . $booking_id, [$booking_id] ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Booking schedule |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function run_booking_schedule() { |
| 81 |
$bookins = Booking::all(); |
| 82 |
|
| 83 |
if ( ! $bookins ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Run cron action. |
| 88 |
foreach ( $bookins['items'] as $booking ) { |
| 89 |
add_action( 'timetics_booking_remainder_' . $booking->ID, [$this, 'send_reminder_email'] ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Send booking reminder email |
| 95 |
* |
| 96 |
* @param integer $booking_id |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function send_reminder_email( $booking_id ) { |
| 101 |
$booking = new Booking( $booking_id ); |
| 102 |
|
| 103 |
$booking_reminder = new Booking_Reminder_Email( $booking ); |
| 104 |
|
| 105 |
$booking_reminder->send(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Clear cron job schedule |
| 110 |
* |
| 111 |
* @return |
| 112 |
*/ |
| 113 |
public function clear_booking_schedule() { |
| 114 |
$bookins = Booking::all(); |
| 115 |
|
| 116 |
if ( ! $bookins ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// Run cron action. |
| 121 |
foreach ( $bookins['items'] as $booking ) { |
| 122 |
$timestamp = wp_next_scheduled( 'timetics_booking_remainder_' . $booking->ID ); |
| 123 |
|
| 124 |
if ( $timestamp && $timestamp < time() ) { |
| 125 |
wp_unschedule_event( $timestamp, 'timetics_booking_remainder_' . $booking->ID ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|