PluginProbe
Order Tracking – WordPress Status Tracking Plugin / 3.2.2
Order Tracking – WordPress Status Tracking Plugin v3.2.2
3.5.4 3.5.3 3.5.2 3.5.1 3.5.0 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.3.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 All 235 releases
order-tracking / includes / Notifications.class.php

Notifications.class.php in Order Tracking – WordPress Status Tracking Plugin 3.2.2, at includes/Notifications.class.php

449 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class to handle sending notifications when an order is submitted or updated
5 */
6
7 if ( !defined( 'ABSPATH' ) )
8 exit;
9
10 if ( !class_exists( 'ewdotpNotifications' ) ) {
11 class ewdotpNotifications {
12
13 public function __construct() {
14
15 add_action( 'ewd_otp_customer_note_updated', array( $this, 'admin_customer_note_email' ) );
16 add_action( 'ewd_otp_insert_customer_order', array( $this, 'admin_customer_order_email' ) );
17 add_action( 'ewd_otp_insert_customer_order', array( $this, 'user_order_created_email' ) );
18
19 add_action( 'ewd_otp_admin_order_inserted', array( $this, 'user_order_created_email' ) );
20 add_action( 'ewd_otp_status_updated', array( $this, 'user_status_updated_email' ) );
21
22 // Sales rep notifications
23 add_action( 'ewd_otp_insert_customer_order', array( $this, 'sales_rep_status_updated_email' ) );
24 add_action( 'ewd_otp_status_updated', array( $this, 'sales_rep_status_updated_email' ) );
25 }
26
27
28 /**
29 * Send an email to the site admin when an order's customer note is updated, if selected
30 *
31 * @since 3.0.0
32 */
33 public function admin_customer_note_email( $order ) {
34 global $ewd_otp_controller;
35
36 $email_id = $ewd_otp_controller->settings->get_setting( 'customer-notes-email' );
37
38 if ( ! $email_id ) { return; }
39
40 $email_address = $ewd_otp_controller->settings->get_setting( 'admin-email' ) ? $ewd_otp_controller->settings->get_setting( 'admin-email' ) : get_option( 'admin_email' );
41
42 if ( $email_id < 0 ) {
43
44 $args = array(
45 'email_id' => $email_id * -1,
46 'order_id' => $order->id,
47 'email_address' => $email_address
48 );
49
50 if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
51 }
52 else {
53
54 $this->send_email( $email_id, $email_address, $order );
55 }
56 }
57
58 /**
59 * Send an email to the site admin when a customer order is submitted, if selected
60 *
61 * @since 3.0.0
62 */
63 public function admin_customer_order_email( $order ) {
64 global $ewd_otp_controller;
65
66 $email_id = $ewd_otp_controller->settings->get_setting( 'customer-order-email' );
67
68 if ( ! $email_id ) { return; }
69
70 $email_address = $ewd_otp_controller->settings->get_setting( 'admin-email' ) ? $ewd_otp_controller->settings->get_setting( 'admin-email' ) : get_option( 'admin_email' );
71
72 if ( $email_id < 0 ) {
73
74 $args = array(
75 'email_id' => $email_id * -1,
76 'order_id' => $order->id,
77 'email_address' => $email_address
78 );
79
80 if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
81 }
82 else {
83
84 $this->send_email( $email_id, $email_address, $order );
85 }
86 }
87
88 /**
89 * Send an email to the client when an order is created, if selected
90 *
91 * @since 3.0.0
92 */
93 public function user_order_created_email( $order ) {
94 global $ewd_otp_controller;
95
96 if ( $ewd_otp_controller->settings->get_setting( 'email-frequency' ) == 'never' ) { return; }
97
98 $statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
99
100 foreach ( $statuses as $status ) {
101
102 if ( $status->status == $order->status ) { $email_id = $status->email; }
103 }
104
105 if ( empty( $email_id ) ) { return; }
106
107 $email_addresses = explode( ',', $order->email );
108
109 if ( empty( $email_addresses ) ) { return; }
110
111 foreach ( $email_addresses as $email_address ) {
112
113 if ( $email_id < 0 ) {
114
115 $args = array(
116 'email_id' => $email_id * -1,
117 'order_id' => $order->id,
118 'email_address' => $email_address
119 );
120
121 if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
122 }
123 else {
124
125 $this->send_email( $email_id, $email_address, $order );
126 }
127 }
128 }
129
130 /**
131 * Send an email to the client when an order status is changed, if selected
132 *
133 * @since 3.0.0
134 */
135 public function user_status_updated_email( $order ) {
136 global $ewd_otp_controller;
137
138 if ( $ewd_otp_controller->settings->get_setting( 'email-frequency' ) != 'change' ) { return; }
139
140 $statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
141
142 foreach ( $statuses as $status ) {
143
144 if ( $status->status == $order->status ) { $email_id = $status->email; }
145 }
146
147 if ( empty( $email_id ) ) { return; }
148
149 $email_addresses = explode( ',', $order->email );
150
151 if ( empty( $email_addresses ) ) { return; }
152
153 foreach ( $email_addresses as $email_address ) {
154
155 if ( $email_id < 0 ) {
156
157 $args = array(
158 'email_id' => $email_id * -1,
159 'order_id' => $order->id,
160 'email_address' => $email_address
161 );
162
163 if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
164 }
165 else {
166
167 $this->send_email( $email_id, $email_address, $order );
168 }
169 }
170 }
171
172 /**
173 * Send an email to the sales rep when an order status is changed, if selected
174 *
175 * @since 3.0.0
176 */
177 public function sales_rep_status_updated_email( $order ) {
178 global $ewd_otp_controller;
179
180 if ( empty( $ewd_otp_controller->settings->get_setting( 'sales-rep-status-notifications' ) ) ) { return; }
181
182 $email_id = $ewd_otp_controller->settings->get_setting( 'sales-rep-status-email' );
183
184 if ( empty( $email_id ) ) { return; }
185
186 if ( empty( $order->sales_rep ) ) { return; }
187
188 $sales_rep = new ewdotpSalesRep();
189
190 $sales_rep->load_sales_rep_from_id( $order->sales_rep );
191
192 if ( empty( $sales_rep->email ) ) { return; }
193
194 if ( $email_id < 0 ) {
195
196 $args = array(
197 'email_id' => $email_id * -1,
198 'order_id' => $order->id,
199 'email_address' => $sales_rep->email
200 );
201
202 if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
203 }
204 else {
205
206 $this->send_email( $email_id, $sales_rep->email, $order );
207 }
208 }
209
210 /**
211 * Send an email using an admin created template
212 *
213 * @since 3.0.0
214 */
215 public function send_email( $email_id, $email_address, $order ) {
216 global $ewd_otp_controller;
217
218 $email_messages = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'email-messages' ) );
219
220 foreach ( $email_messages as $email_message ) {
221
222 if ( $email_message->id != $email_id ) { continue; }
223
224 $message = $this->substitute_email_text( $this->get_email_template( $email_message ), $order );
225 $subject = $this->substitute_email_text( $email_message->subject, $order);
226 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
227
228 $mail_success = wp_mail( $email_address, $subject, $message, $headers );
229
230 return $mail_success;
231 }
232 }
233
234 /**
235 * Replace plugin-defined tags with order information
236 *
237 * @since 3.0.0
238 */
239 function substitute_email_text( $text, $order ) {
240 global $ewd_otp_controller;
241
242 $confirmation_code = ewd_random_string();
243
244 $order->set_tracking_link_code( $confirmation_code );
245
246 $args = array(
247 'tracking_number' => $order->number,
248 'email' => $order->email,
249 'tracking_link_code' => $confirmation_code
250 );
251
252 $tracking_url = add_query_arg( $args, $ewd_otp_controller->settings->get_setting( 'tracking-page-url' ) );
253
254 $tracking_link = "[button link='" . $tracking_url . "']" . __( 'Track your order', 'order-tracking' ) . "[/button]";
255
256 date_default_timezone_set( get_option( 'timezone_string' ) );
257
258 $search = array(
259 "[order-name]",
260 "[order-number]",
261 "[order-status]",
262 "[order-notes]",
263 "[customer-notes]",
264 "[order-time]",
265 "[tracking-link]",
266 "[customer-name]",
267 "[customer-number]",
268 "[customer-id]",
269 "[sales-rep]",
270 "[sales-rep-number]"
271 );
272
273 $replace = array(
274 ! empty( $order->name ) ? $order->name : '',
275 ! empty( $order->number ) ? $order->number : '',
276 ! empty( $order->external_status ) ? $order->external_status : '',
277 ! empty( $order->notes_public ) ? $order->notes_public : '',
278 ! empty( $order->customer_notes ) ? $order->customer_notes : '',
279 date( $ewd_otp_controller->settings->get_setting( 'date-format' ), strtotime( $order->status_updated ) ),
280 $tracking_link,
281 $ewd_otp_controller->customer_manager->get_customer_field( 'name', $order->customer ),
282 $ewd_otp_controller->customer_manager->get_customer_field( 'number', $order->customer ),
283 ! empty( $order->customer ) ? $order->customer : '',
284 $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'first_name', $order->sales_rep ) . ' ' . $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'last_name', $order->sales_rep ),
285 $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'number', $order->sales_rep ),
286 );
287
288 $custom_fields = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'custom-fields' ) );
289
290 foreach ( $custom_fields as $custom_field ) {
291
292 $value = $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $order->id );
293
294 $search[] = '[' . $custom_field->slug . ']';
295 $replace[] = $value;
296 }
297
298 $order_text = str_replace( $search, $replace, $text );
299
300 return $this->replace_email_content( $order_text );
301 }
302
303 /**
304 * Returns a template of the email message, along with admin styling for it
305 *
306 * @since 3.0.0
307 */
308 public function get_email_template( $email_message ) {
309 global $ewd_otp_controller;
310
311 $message_title = $email_message->subject;
312 $message_content = $this->replace_email_content( stripslashes( $email_message->message ) );
313
314 $message = <<< EOT
315 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
316 <html xmlns="http://www.w3.org/1999/xhtml" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
317 <head>
318 <meta name="viewport" content="width=device-width" />
319 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
320 <title>$message_title</title>
321
322
323 <style type="text/css">
324
325 img {
326 max-width: 100%;
327 }
328 body {
329 -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em;
330 }
331 body {
332 background-color: #f6f6f6;
333 }
334
335 @media only screen and (max-width: 640px) {
336 body {
337 padding: 0 !important;
338 }
339 h1 {
340 font-weight: 800 !important; margin: 20px 0 5px !important;
341 }
342 h2 {
343 font-weight: 800 !important; margin: 20px 0 5px !important;
344 }
345 h3 {
346 font-weight: 800 !important; margin: 20px 0 5px !important;
347 }
348 h4 {
349 font-weight: 800 !important; margin: 20px 0 5px !important;
350 }
351 h1 {
352 font-size: 22px !important;
353 }
354 h2 {
355 font-size: 18px !important;
356 }
357 h3 {
358 font-size: 16px !important;
359 }
360 .container {
361 padding: 0 !important; width: 100% !important;
362 }
363 .content {
364 padding: 0 !important;
365 }
366 .content-wrap {
367 padding: 10px !important;
368 }
369 .invoice {
370 width: 100% !important;
371 }
372 }
373 </style>
374 </head>
375
376 <body itemscope itemtype="http://schema.org/EmailMessage" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; width: 100% !important; height: 100%; line-height: 1.6em; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6">
377
378 <table class="body-wrap" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; background-color: #f6f6f6; margin: 0;" bgcolor="#f6f6f6"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
379 <td class="container" width="600" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; display: block !important; max-width: 600px !important; clear: both !important; margin: 0 auto;" valign="top">
380 <div class="content" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; max-width: 600px; display: block; margin: 0 auto; padding: 20px;">
381 <table class="main" width="100%" cellpadding="0" cellspacing="0" itemprop="action" itemscope itemtype="http://schema.org/ConfirmAction" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; border-radius: 3px; background-color: #fff; margin: 0; border: 1px solid #e9e9e9;" bgcolor="#fff"><tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-wrap" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 20px;" valign="top">
382 <meta itemprop="name" content="Please Review" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;" /><table width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
383 $message_content
384 </div>
385 </td>
386 <td style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0;" valign="top"></td>
387 </tr></table></body>
388 </html>
389
390 EOT;
391
392 return $message;
393 }
394
395 /**
396 * Replace the structure elements of an email template
397 *
398 * @since 3.0.0
399 */
400 public function replace_email_content( $unprocessed_message ) {
401
402 $search = array('[section]', '[/section]', '[footer]', '[/footer]', '[/button]');
403 $replace = array(
404 '<tr style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">',
405 '</td></tr>',
406 '</table></td></tr></table><div class="footer" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; width: 100%; clear: both; color: #999; margin: 0; padding: 20px;"><table width="100%" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><tr style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="aligncenter content-block" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px; vertical-align: top; color: #999; text-align: center; margin: 0; padding: 0 0 20px;" align="center" valign="top">',
407 '</td></tr></table></div>',
408 '</a></td></tr>'
409 );
410 $intemediate_message = str_replace( $search, $replace, $unprocessed_message );
411 $processed_message = $this->replace_email_links( $intemediate_message );
412
413 return $processed_message;
414 }
415
416 /**
417 * Replace all of the button links used in the email template
418 *
419 * @since 3.0.0
420 */
421 public function replace_email_links( $unprocessed_message ) {
422
423 $pattern = "/\[button link=\'(.*?)\'\]/";
424
425 preg_match_all( $pattern, $unprocessed_message, $matches );
426
427 $replace = '<tr style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;"><td class="content-block" itemprop="handler" itemscope itemtype="http://schema.org/HttpActionHandler" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top"><a href="INSERTED_LINK" class="btn-primary" itemprop="url" style="font-family: \'Helvetica Neue\',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">';
428 $message = preg_replace( $pattern, $replace, $unprocessed_message );
429
430 if ( is_array( $matches[1] ) ) {
431
432 foreach ( $matches[1] as $link ) {
433
434 $pos = strpos( $message, "INSERTED_LINK" );
435
436 if ($pos !== false) {
437
438 $intermediate_message = substr_replace( $message, $link, $pos, 13 );
439 $message = $intermediate_message;
440 }
441 }
442 }
443
444 return $message;
445 }
446 }
447 } // endif;
448
449