PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.11
Timetics – Appointment Booking Calendar & Scheduling v1.0.11
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 / bookings / hooks.php

hooks.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.11, at core/bookings/hooks.php

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