PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / Notifications.php

Notifications.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/Notifications.php

329 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes;
4
5 use Bookit\Classes\Admin\SettingsController;
6 use Bookit\Classes\Database\Appointments;
7 use Bookit\Classes\Database\Customers;
8 use Bookit\Classes\Database\Services;
9 use Bookit\Classes\Database\Staff;
10
11 class Notifications {
12
13 public static $fields_to_remove_if_price_is_zero = array(
14 'payment_status',
15 'payment_method',
16 'total',
17 );
18
19 /**
20 * Init Notifications
21 */
22 public static function init() {
23 add_action( 'bookit_appointment_created', array( self::class, 'appointment_created' ), 100, 1 );
24 add_action( 'bookit_appointment_updated', array( self::class, 'appointment_updated' ), 100, 1 );
25 add_action( 'bookit_payment_complete', array( self::class, 'payment_complete' ), 100, 1 );
26 add_action( 'bookit_appointment_status_changed', array( self::class, 'appointment_status_changed' ), 100, 1 );
27 add_action( 'bookit_appointment_deleted', array( self::class, 'appointment_deleted' ), 100, 3 );
28 add_filter( 'bookit_filter_email_data', array( self::class, 'rewrite_email_data' ), 10, 1 );
29 add_action( 'bookit_appointment_created', array( self::class, 'appointment_created_set_option' ), 100, 1 );
30 add_filter( 'stm_admin_notice_rate_bookit_single', array( self::class, 'stm_admin_notice_rate_bookit_single' ), 10, 1 );
31 }
32
33 /**
34 * Mail Content Type Filter
35 *
36 * @return string
37 */
38 public static function mail_content_type() {
39 return 'text/plain; charset=UTF-8';
40 }
41
42 public static function check_sender_name( $original_name ) {
43 $sender_name = get_option_by_path( 'bookit_settings.sender_name' );
44
45 return ! empty( $sender_name ) ? $sender_name : $original_name;
46 }
47
48 public static function check_sender_email( $original_email ) {
49 $sender_email = get_option_by_path( 'bookit_settings.sender_email' );
50
51 return ! empty( $sender_email ) ? $sender_email : $original_email;
52 }
53
54
55 /**
56 * Sent Email
57 *
58 * @param $to
59 * @param $subject
60 * @param $body
61 * @param $vars
62 * @param $template
63 */
64 public static function sent_mail( $to, $subject, $body, $vars = array(), $template = '' ) {
65
66 add_filter( 'wp_mail_content_type', array( self::class, 'mail_content_type' ) );
67 add_filter( 'wp_mail_from_name', array( self::class, 'check_sender_name' ), 10, 1 );
68 add_filter( 'wp_mail_from', array( self::class, 'check_sender_email' ), 10, 1 );
69
70 $data = apply_filters(
71 'bookit_filter_email_data',
72 array(
73 'to' => $to,
74 'subject' => $subject,
75 'body' => $body,
76 'vars' => $vars,
77 'template' => $template,
78 )
79 );
80
81 wp_mail( $data['to'], $data['subject'], $data['body'] );
82
83 remove_filter( 'wp_mail_content_type', array( self::class, 'mail_content_type' ) );
84 }
85
86 /**
87 * Appointment Created
88 *
89 * @param $appointment_id
90 */
91 public static function appointment_created( $appointment_id ) {
92 $settings = SettingsController::get_settings();
93 $vars = self::get_email_variables( $appointment_id );
94
95 if ( ! empty( $settings['emails']['appointment_created_customer']['enabled'] ) ) {
96 self::sent_mail( $vars['[customer]'], '', '', $vars, 'appointment_created_customer' );
97 }
98
99 if ( $settings['emails']['appointment_created_admin']['enabled']
100 && strpos( $settings['emails']['appointment_created_admin']['to'], '[admin]' ) !== false ) {
101 self::sent_mail( $vars['[admin]'], '', '', $vars, 'appointment_created_admin' );
102 }
103
104 if ( $settings['emails']['appointment_created_admin']['enabled']
105 && strpos( $settings['emails']['appointment_created_admin']['to'], '[staff]' ) !== false ) {
106 self::sent_mail( $vars['[staff]'], '', '', $vars, 'appointment_created_admin' );
107 }
108 }
109
110 /**
111 * Appointment Updated
112 *
113 * @param $appointment_id
114 */
115 public static function appointment_updated( $appointment_id ) {
116 $settings = SettingsController::get_settings();
117 $vars = self::get_email_variables( $appointment_id );
118
119 if ( ! empty( $settings['emails']['appointment_updated_customer']['enabled'] ) ) {
120 self::sent_mail( $vars['[customer]'], '', '', $vars, 'appointment_updated_customer' );
121 }
122
123 if ( $settings['emails']['appointment_updated_admin']['enabled']
124 && strpos( $settings['emails']['appointment_updated_admin']['to'], '[admin]' ) !== false ) {
125 self::sent_mail( $vars['[admin]'], '', '', $vars, 'appointment_updated_admin' );
126 }
127
128 if ( $settings['emails']['appointment_updated_admin']['enabled']
129 && strpos( $settings['emails']['appointment_updated_admin']['to'], '[staff]' ) !== false ) {
130 self::sent_mail( $vars['[staff]'], '', '', $vars, 'appointment_updated_admin' );
131 }
132 }
133
134 /**
135 * Payment Complete Notification
136 *
137 * @param $appointment_id
138 */
139 public static function payment_complete( $appointment_id ) {
140 $settings = SettingsController::get_settings();
141 $vars = self::get_email_variables( $appointment_id );
142
143 if ( ! empty( $settings['emails']['payment_complete_customer']['enabled'] ) ) {
144 self::sent_mail( $vars['[customer]'], '', '', $vars, 'payment_complete_customer' );
145 }
146
147 if ( $settings['emails']['payment_complete_admin']['enabled']
148 && strpos( $settings['emails']['payment_complete_admin']['to'], '[admin]' ) !== false ) {
149 self::sent_mail( $vars['[admin]'], '', '', $vars, 'payment_complete_admin' );
150 }
151
152 if ( $settings['emails']['payment_complete_admin']['enabled']
153 && strpos( $settings['emails']['payment_complete_admin']['to'], '[staff]' ) !== false ) {
154 self::sent_mail( $vars['[staff]'], '', '', $vars, 'payment_complete_admin' );
155 }
156 }
157
158 /**
159 * Appointment Status Changed Notification
160 *
161 * @param $appointment_id
162 */
163 public static function appointment_status_changed( $appointment_id ) {
164 $settings = SettingsController::get_settings();
165 $vars = self::get_email_variables( $appointment_id );
166
167 if ( ! empty( $settings['emails']['appointment_status_changed']['enabled'] ) ) {
168 self::sent_mail( $vars['[customer]'], '', '', $vars, 'appointment_status_changed' );
169 }
170
171 if ( $settings['emails']['appointment_status_changed_admin']['enabled']
172 && strpos( $settings['emails']['appointment_status_changed_admin']['to'], '[admin]' ) !== false ) {
173 self::sent_mail( $vars['[admin]'], '', '', $vars, 'appointment_status_changed_admin' );
174 }
175
176 if ( $settings['emails']['appointment_status_changed_admin']['enabled']
177 && strpos( $settings['emails']['appointment_status_changed_admin']['to'], '[staff]' ) !== false ) {
178 self::sent_mail( $vars['[staff]'], '', '', $vars, 'appointment_status_changed_admin' );
179 }
180 }
181
182 /**
183 * Get Email Variables
184 *
185 * @param $appointment_id
186 *
187 * @return array
188 */
189 public static function get_email_variables( $appointment_id, $reason = '' ) {
190 $appointment = Appointments::get_full_appointment_by_id( $appointment_id );
191 $status = $appointment->status;
192 $payment_status = $appointment->payment_status;
193
194 $payment_status_labels = array(
195 'pending' => __( 'pending', 'bookit' ),
196 'cancelled' => __( 'cancelled', 'bookit' ),
197 'rejected' => __( 'rejected', 'bookit' ),
198 'complete' => __( 'complete', 'bookit' ),
199 );
200
201 $appointment_status_labels = array(
202 'pending' => __( 'pending', 'bookit' ),
203 'approved' => __( 'approved', 'bookit' ),
204 'cancelled' => __( 'cancelled', 'bookit' ),
205 'delete' => __( 'delete', 'bookit' ),
206 );
207
208 $vars = array(
209 '[admin]' => get_option( 'admin_email' ),
210 '[staff]' => $appointment->staff_email,
211 '[customer]' => $appointment->customer_email,
212 '[staff_name]' => $appointment->staff_name,
213 '[staff_phone]' => $appointment->staff_phone,
214 '[customer_name]' => $appointment->customer_name,
215 '[customer_email]' => $appointment->customer_email,
216 '[customer_phone]' => $appointment->customer_phone,
217 '[service_title]' => $appointment->service_name,
218 '[appointment_id]' => $appointment->id,
219 '[appointment_day]' => date( get_option( 'date_format' ), $appointment->date_timestamp ),
220 '[start_time]' => date( get_option( 'time_format' ), $appointment->start_time ),
221 '[payment_method]' => $appointment->payment_method,
222 '[payment_status]' => $payment_status_labels[ $payment_status ] ?? $payment_status,
223 '[price]' => $appointment->price,
224 '[total]' => bookit_price( $appointment->price ),
225 '[status]' => $appointment_status_labels[ $status ] ?? $status,
226 '[reason]' => $reason,
227 );
228
229 return $vars;
230 }
231
232 /**
233 * Rewrite Email Data
234 *
235 * @param $data
236 *
237 * @return mixed
238 */
239 public static function rewrite_email_data( $data ) {
240 if ( empty( $data['vars'] ) || empty( $data['template'] ) ) {
241 return $data;
242 }
243
244 $template = $data['template'];
245 $settings = SettingsController::get_settings();
246
247 if ( ! empty( $settings['emails'][ $template ]['to'] ) ) {
248 $data['to'] = strtr( $settings['emails'][ $template ]['to'], $data['vars'] );
249 }
250
251 /** remove payment info from template if zero price */
252 if ( 0 == (float) $data['vars']['[price]'] ) {
253
254 foreach ( self::$fields_to_remove_if_price_is_zero as $field ) {
255 // remove varaible
256 unset( $data['vars'][ "[{ $field }]" ] );
257 // remove from template
258 $settings['emails'][ $template ]['body'] = preg_replace(
259 '/\]([^\)].*?)\[{$field}]/',
260 ']',
261 $settings['emails'][ $template ]['body']
262 );
263 }
264 }
265
266 if ( ! empty( $settings['emails'][ $template ]['subject'] ) ) {
267 $translates = apply_filters(
268 'wpml_translate_single_string',
269 $settings['emails'][ $template ]['subject'],
270 'bookit',
271 $template . '_subject'
272 );
273 $data['subject'] = strtr( $translates, $data['vars'] );
274 }
275
276 if ( ! empty( $settings['emails'][ $template ]['body'] ) ) {
277 $translates = apply_filters(
278 'wpml_translate_single_string',
279 $settings['emails'][ $template ]['body'],
280 'bookit',
281 $template . '_body'
282 );
283 $data['body'] = strtr( $translates, $data['vars'] );
284 }
285
286 return $data;
287 }
288
289 /**
290 * Appointment Delete Notification
291 *
292 * @param int $appointment_id
293 * @param string $send_notification_to
294 */
295 public static function appointment_deleted( int $appointment_id, array $send_notification_to, $reason ) {
296
297 $vars = self::get_email_variables( $appointment_id, $reason );
298 if ( $send_notification_to['customer'] ) {
299 self::sent_mail( $vars['[customer]'], '', '', $vars, 'appointment_deleted_customer' );
300 }
301
302 if ( $send_notification_to['staff'] ) {
303 self::sent_mail( $vars['[staff]'], '', '', $vars, 'appointment_deleted_staff' );
304 }
305 }
306
307 public static function appointment_created_set_option() {
308 $created = get_option( 'stm_bookit_appointment_created', false );
309
310 if ( ! $created ) {
311 $data = array(
312 'show_time' => time(),
313 'step' => 0,
314 'prev_action' => '',
315 );
316 set_transient( 'stm_bookit_single_notice_setting', $data );
317 update_option( 'stm_bookit_appointment_created', true );
318 }
319 }
320
321 public static function stm_admin_notice_rate_bookit_single( $data ) {
322 if ( is_array( $data ) ) {
323 $data['title'] = __( 'Whoa!', 'bookit' );
324 $data['content'] = __( 'You have successfully created one appointment. We hope you like it. Time to rate <strong>Bookit 5 Stars!</strong>', 'bookit' );
325 }
326 return $data;
327 }
328 }
329