| 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 |
$vars = array( |
| 195 |
'[admin]' => get_option( 'admin_email' ), |
| 196 |
'[staff]' => $appointment->staff_email, |
| 197 |
'[customer]' => $appointment->customer_email, |
| 198 |
'[staff_name]' => $appointment->staff_name, |
| 199 |
'[staff_phone]' => $appointment->staff_phone, |
| 200 |
'[customer_name]' => $appointment->customer_name, |
| 201 |
'[customer_email]' => $appointment->customer_email, |
| 202 |
'[customer_phone]' => $appointment->customer_phone, |
| 203 |
'[service_title]' => $appointment->service_name, |
| 204 |
'[appointment_id]' => $appointment->id, |
| 205 |
'[appointment_day]' => date( get_option( 'date_format' ), $appointment->date_timestamp ), |
| 206 |
'[start_time]' => date( get_option( 'time_format' ), $appointment->start_time ), |
| 207 |
'[payment_method]' => $appointment->payment_method, |
| 208 |
'[payment_status]' => __( $payment_status, 'bookit' ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText |
| 209 |
'[price]' => $appointment->price, |
| 210 |
'[total]' => bookit_price( $appointment->price ), |
| 211 |
'[status]' => __( $status, 'bookit' ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText |
| 212 |
'[reason]' => $reason, |
| 213 |
); |
| 214 |
|
| 215 |
return $vars; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Rewrite Email Data |
| 220 |
* |
| 221 |
* @param $data |
| 222 |
* |
| 223 |
* @return mixed |
| 224 |
*/ |
| 225 |
public static function rewrite_email_data( $data ) { |
| 226 |
if ( empty( $data['vars'] ) || empty( $data['template'] ) ) { |
| 227 |
return $data; |
| 228 |
} |
| 229 |
|
| 230 |
$template = $data['template']; |
| 231 |
$settings = SettingsController::get_settings(); |
| 232 |
|
| 233 |
if ( ! empty( $settings['emails'][ $template ]['to'] ) ) { |
| 234 |
$data['to'] = strtr( $settings['emails'][ $template ]['to'], $data['vars'] ); |
| 235 |
} |
| 236 |
|
| 237 |
/** remove payment info from template if zero price */ |
| 238 |
if ( 0 == (float) $data['vars']['[price]'] ) { |
| 239 |
|
| 240 |
foreach ( self::$fields_to_remove_if_price_is_zero as $field ) { |
| 241 |
// remove varaible |
| 242 |
unset( $data['vars'][ "[{ $field }]" ] ); |
| 243 |
// remove from template |
| 244 |
$settings['emails'][ $template ]['body'] = preg_replace( |
| 245 |
'/\]([^\)].*?)\[{$field}]/', |
| 246 |
']', |
| 247 |
$settings['emails'][ $template ]['body'] |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if ( ! empty( $settings['emails'][ $template ]['subject'] ) ) { |
| 253 |
$translates = apply_filters( |
| 254 |
'wpml_translate_single_string', |
| 255 |
$settings['emails'][ $template ]['subject'], |
| 256 |
'bookit', |
| 257 |
$template . '_subject' |
| 258 |
); |
| 259 |
$data['subject'] = strtr( $translates, $data['vars'] ); |
| 260 |
} |
| 261 |
|
| 262 |
if ( ! empty( $settings['emails'][ $template ]['body'] ) ) { |
| 263 |
$translates = apply_filters( |
| 264 |
'wpml_translate_single_string', |
| 265 |
$settings['emails'][ $template ]['body'], |
| 266 |
'bookit', |
| 267 |
$template . '_body' |
| 268 |
); |
| 269 |
$data['body'] = strtr( $translates, $data['vars'] ); |
| 270 |
} |
| 271 |
|
| 272 |
return $data; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Appointment Delete Notification |
| 277 |
* |
| 278 |
* @param int $appointment_id |
| 279 |
* @param string $send_notification_to |
| 280 |
*/ |
| 281 |
public static function appointment_deleted( int $appointment_id, array $send_notification_to, $reason ) { |
| 282 |
|
| 283 |
$vars = self::get_email_variables( $appointment_id, $reason ); |
| 284 |
if ( $send_notification_to['customer'] ) { |
| 285 |
self::sent_mail( $vars['[customer]'], '', '', $vars, 'appointment_deleted_customer' ); |
| 286 |
} |
| 287 |
|
| 288 |
if ( $send_notification_to['staff'] ) { |
| 289 |
self::sent_mail( $vars['[staff]'], '', '', $vars, 'appointment_deleted_staff' ); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
public static function appointment_created_set_option() { |
| 294 |
$created = get_option( 'stm_bookit_appointment_created', false ); |
| 295 |
|
| 296 |
if ( ! $created ) { |
| 297 |
$data = array( |
| 298 |
'show_time' => time(), |
| 299 |
'step' => 0, |
| 300 |
'prev_action' => '', |
| 301 |
); |
| 302 |
set_transient( 'stm_bookit_single_notice_setting', $data ); |
| 303 |
update_option( 'stm_bookit_appointment_created', true ); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
public static function stm_admin_notice_rate_bookit_single( $data ) { |
| 308 |
if ( is_array( $data ) ) { |
| 309 |
$data['title'] = __( 'Whoa!', 'bookit' ); |
| 310 |
$data['content'] = __( 'You have successfully created one appointment. We hope you like it. Time to rate <strong>Bookit 5 Stars!</strong>', 'bookit' ); |
| 311 |
} |
| 312 |
return $data; |
| 313 |
} |
| 314 |
} |
| 315 |
|