PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.3.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.3.0
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-notification.php

class-notification.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.3.0, at includes/class-notification.php

693 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Email Notification Class
5 */
6 class WeForms_Notification {
7
8 private $merge_tags = array();
9 private $args = array();
10
11 /**
12 * Init the class
13 *
14 * @param array $args
15 */
16 public function __construct( $args = array() ) {
17 $defaults = array(
18 'form_id' => 0,
19 'entry_id' => 0,
20 'page_id' => 0
21 );
22
23 $this->args = wp_parse_args( $args, $defaults );
24 }
25
26 /**
27 * Send notifications
28 *
29 * @return void
30 */
31 public function send_notifications() {
32 $notifications = $this->get_active_notifications();
33
34 if ( !$notifications ) {
35 return;
36 }
37
38 $this->set_merge_tags();
39
40 foreach ($notifications as $notification) {
41
42 if ( $this->meet_conditions( $notification ) ) {
43
44 if ( $notification['type'] == 'email' ) {
45
46 $this->send_notification( $notification );
47
48 } elseif( $notification['type'] == 'sms' ) {
49
50 $this->send_sms( $notification );
51 }
52 }
53 }
54 }
55
56 /**
57 * Resend entry notifications
58 *
59 * @return void
60 */
61 public function resend_entry_notifications( $notifications ) {
62 if ( !$notifications ) {
63 return;
64 }
65
66 $this->set_merge_tags();
67
68 foreach ($notifications as $notification) {
69
70 if ( $this->meet_conditions( $notification ) ) {
71
72 if ( $notification['type'] == 'email' ) {
73
74 $this->send_notification( $notification );
75
76 } elseif( $notification['type'] == 'sms' ) {
77
78 $this->send_sms( $notification );
79 }
80 }
81 }
82 }
83
84 /**
85 * Send a single notification
86 *
87 * @param array $notification
88 *
89 * @return void
90 */
91 public function send_notification( $notification ) {
92 $headers = array();
93
94 $to = $this->replace_tags( $notification['to'] );
95
96 $subject = $this->replace_tags( $notification['subject'] );
97 $subject = static::replace_name_tag( $subject, $this->args['entry_id'] );
98
99 $message = $this->replace_tags( $notification['message'] );
100 $message = static::replace_name_tag( $message, $this->args['entry_id'] );
101 $message = $this->replace_all_fields( $message );
102 $message = wpautop( $message );
103
104 $fromName = $this->replace_tags( $notification['fromName'] );
105 $fromName = static::replace_name_tag( $fromName, $this->args['entry_id'] );
106 $fromAddress = $this->replace_tags( $notification['fromAddress'] );
107 $replyTo = $this->replace_tags( $notification['replyTo'] );
108 $cc = $this->replace_tags( $notification['cc'] );
109 $bcc = $this->replace_tags( $notification['bcc'] );
110
111 if ( $fromName || $fromAddress ) {
112 $headers['from'] = array(
113 'email' => $fromAddress,
114 'name' => $fromName
115 );
116 }
117
118 if ( $cc ) {
119 $headers['cc'] = $cc;
120 }
121
122 if ( $bcc ) {
123 $headers['bcc'] = $bcc;
124 }
125
126 if ( $replyTo ) {
127 $headers['replyto'] = $replyTo;
128 }
129
130 // content type to text/html
131 $headers[] = 'Content-Type: text/html; charset=UTF-8';
132 $email_body = apply_filters( 'weforms_email_message', $this->get_formatted_body( $message ), $notification['message'], $headers );
133
134 weforms()->emailer->send( $to, $subject, $email_body, $headers );
135 }
136
137
138 /**
139 * Send a single sms notification
140 *
141 * @param array $notification
142 *
143 * @return void
144 */
145 public function send_sms( $notification ) {
146
147 if ( ! class_exists('WeForms_SMS_Notification') ) {
148 return;
149 }
150
151 $to = $this->replace_tags( $notification['smsTo'] );
152 $message = $this->replace_tags( $notification['smsText'] );
153 $message = static::replace_name_tag( $message, $this->args['entry_id'] );
154 $message = $this->replace_all_fields( $message );
155
156 $email_body = apply_filters( 'weforms_sms_message', $this->get_formatted_sms_body( $message ) );
157
158 weforms_sms()->send_sms( array( $to ), $message );
159 }
160
161 /**
162 * Check conditional logics
163 *
164 * @param array $notification
165 *
166 * @return boolean
167 */
168 public function meet_conditions( $notification ) {
169
170 $form = weforms()->form->get( $this->args['form_id'] );
171 $entry = $form->entries()->get( $this->args['entry_id'] );
172 $fields = $entry->get_fields();
173
174 $cond = !empty( $notification['weforms_cond'] ) ? $notification['weforms_cond'] : array();
175
176 if ( isset($cond['condition_status']) && 'yes' === $cond['condition_status'] ) {
177
178 $cond_logic = !empty( $cond['cond_logic'] ) ? $cond['cond_logic'] : 'any';
179
180 if ( !empty( $cond['conditions'] ) && is_array( $cond['conditions'] )) {
181
182 $status = array(); // going to store all condition result as boolean
183
184 foreach ( $cond['conditions'] as $k => $condition ) {
185
186 $field = $fields[$condition['name']];
187 $value = $field['value'];
188 $options = $field['options'];
189 $operator = $condition['operator'] == '=' ? true : false;
190
191 // probably from checkbox
192 if( is_array( $value ) ) {
193
194 // search by value
195 if ( in_array( $condition['option'], $value ) ) {
196
197 $status[$k] = $operator ? true : false;
198
199 } else {
200
201 // search by key
202 foreach ( $value as $single_value ) {
203
204 if( $condition['option'] == array_search( $single_value , $options ) ) {
205
206 $status[$k] = $operator ? true : false;
207
208 break;
209 }
210 }
211 }
212
213 if ( ! isset( $status[$k] ) ) {
214
215 $status[$k] = $operator ? false : true;
216 }
217
218 } else {
219
220 if ( $condition['option'] == $value || $condition['option'] == array_search( $value , $options ) ) {
221
222 $status[$k] = $operator ? true : false;
223
224 } else {
225
226 $status[$k] = $operator ? false : true;
227 }
228 }
229 }
230
231 if ( $cond_logic == 'any' ) {
232
233 return in_array( true, $status) ? true : false; // any true? then true
234
235
236 } elseif ( $cond_logic == 'all' ) {
237
238 return in_array( false, $status) ? false : true; // any false? then false
239
240 }
241
242 }
243
244 }
245
246 return true;
247 }
248
249 /**
250 * Get active notifications of a form
251 *
252 * @param int $form_id
253 *
254 * @return array|boolean
255 */
256 public function get_active_notifications() {
257 $notifications = weforms()->form->get( $this->args['form_id'] )->get_notifications();
258
259 if ( $notifications ) {
260 $notifications = array_filter( $notifications, function($notification) {
261 return $notification['active'] == true;
262 } );
263
264 return $notifications;
265 }
266
267 return false;
268 }
269
270 /**
271 * Get formatted HTML email
272 *
273 * @param string $message
274 *
275 * @return string
276 */
277 public function get_formatted_body( $message ) {
278 $css = '';
279 $header = apply_filters( 'weforms_email_header', '' );
280 $footer = apply_filters( 'weforms_email_footer', '' );
281
282 if ( empty( $header ) ) {
283 ob_start();
284 include WEFORMS_INCLUDES . '/email/template/header.php';
285 $header = ob_get_clean();
286 }
287
288 if ( empty( $footer ) ) {
289 ob_start();
290 include WEFORMS_INCLUDES . '/email/template/footer.php';
291 $footer = ob_get_clean();
292 }
293
294 ob_start();
295 include WEFORMS_INCLUDES . '/email/template/styles.php';
296 $css = apply_filters( 'weforms_email_styles', ob_get_clean() );
297
298 $content = $header . $message . $footer;
299
300 if ( ! class_exists( 'Emogrifier' ) ) {
301 require_once WEFORMS_INCLUDES . '/library/Emogrifier.php';
302 }
303
304 try {
305
306 // apply CSS styles inline for picky email clients
307 $emogrifier = new Emogrifier( $content, $css );
308 $content = $emogrifier->emogrify();
309
310 } catch ( Exception $e ) {
311
312 echo $e->getMessage();
313 }
314
315 return $content;
316 }
317
318 /**
319 * Get formatted HTML email
320 *
321 * @param string $message
322 *
323 * @return string
324 */
325 public function get_formatted_sms_body( $message ) {
326 $message = strip_tags( $message );
327
328 if ( strlen( $message ) > apply_filters( 'wefroms_sms_char_length', 153 ) ) {
329 $message = substr( $message, 0, apply_filters( 'wefroms_sms_char_length', 153 ) );
330 $message .= __('..', 'weforms');
331 }
332
333 return $message;
334 }
335
336 /**
337 * Populate an key/value pair of search/replace array
338 *
339 * @return array
340 */
341 public function set_merge_tags() {
342
343 // return early we had a previous array call
344 if ( $this->merge_tags ) {
345 return $this->merge_tags;
346 }
347
348 // populate the key/value array for the first time
349 $tags = weforms_get_merge_tags();
350 $replace_array = array();
351
352 foreach ($tags as $section => $child) {
353
354 if ( !$child['tags'] ) {
355 continue;
356 }
357
358 foreach ($child['tags'] as $search_key => $label) {
359 $replace_array[ '{' . $search_key . '}' ] = $this->get_merge_value( $search_key );
360 }
361 }
362
363 $this->merge_tags = $replace_array;
364 }
365
366 /**
367 * Get a merge tag value based on a tag
368 *
369 * @param string $tag
370 *
371 * @return string
372 */
373 public function get_merge_value( $tag ) {
374
375 switch ( $tag ) {
376 case 'entry_id':
377 return $this->args['entry_id'];
378 break;
379
380 case 'form_id':
381 return $this->args['form_id'];
382 break;
383
384 case 'form_name':
385 return get_post_field( 'post_title', $this->args['form_id'] );
386 break;
387
388 case 'admin_email':
389 return get_option( 'admin_email' );
390 break;
391
392 case 'date':
393 return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
394 break;
395
396 case 'site_name':
397 return get_bloginfo( 'name' );
398 break;
399
400 case 'site_url':
401 return site_url( '/' );
402 break;
403
404 case 'page_title':
405 return get_post_field( 'post_title', $this->args['page_id'] );
406 break;
407
408 case 'ip_address':
409 return weforms_get_client_ip();
410 break;
411
412 case 'user_id':
413 return get_current_user_id();
414 break;
415
416 case 'first_name':
417 return $this->get_user_prop( 'first_name' );
418 break;
419
420 case 'last_name':
421 return $this->get_user_prop( 'last_name' );
422 break;
423
424 case 'display_name':
425 return $this->get_user_prop( 'display_name' );
426 break;
427
428 case 'user_email':
429 return $this->get_user_prop( 'user_email' );
430 break;
431
432 case 'url_page':
433 return get_permalink( $this->args['page_id'] );
434 break;
435
436 case 'url_referer':
437 return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
438 break;
439
440 case 'url_login':
441 return wp_login_url();
442 break;
443
444 case 'url_logout':
445 return wp_logout_url();
446 break;
447
448 case 'url_register':
449 return wp_registration_url();
450 break;
451
452 case 'url_lost_password':
453 return wp_lostpassword_url();
454 break;
455
456 default:
457 return apply_filters( 'weforms_merge_tag_value', '', $tag, $this->args );
458 break;
459 }
460 }
461
462 /**
463 * Parse out the custom fields with entry meta values
464 *
465 * @param string $text
466 *
467 * @return string
468 */
469 public static function replace_field_tags( $text, $entry_id ) {
470 $pattern = '/{field:(\w*)}/';
471
472 preg_match_all( $pattern, $text, $matches );
473
474 // bail out if nothing found to be replaced
475 if ( !$matches ) {
476 return $text;
477 }
478
479 foreach ($matches[1] as $index => $meta_key) {
480 $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
481
482 if ( is_array( $meta_value ) ) {
483 $meta_value = implode(WeForms::$field_separator, $meta_value);
484 }
485
486 $text = str_replace( $matches[0][$index], $meta_value, $text );
487 }
488
489 return $text;
490 }
491
492 /**
493 * Replace name tag with required value
494 *
495 * @param string $text
496 *
497 * @return string
498 */
499 public static function replace_name_tag( $text, $entry_id ) {
500 $pattern = '/{name-(full|first|middle|last):(\w*)}/';
501
502 preg_match_all( $pattern, $text, $matches );
503
504 // bail out if nothing found to be replaced
505 if ( !$matches[0] ) {
506 return $text;
507 }
508
509 list( $search, $fields, $meta_key ) = $matches;
510
511 $meta_value = weforms_get_entry_meta( $entry_id, $meta_key[0], true );
512 $replace = explode( WeForms::$field_separator, $meta_value );
513
514 foreach ($search as $index => $search_key) {
515
516 if ( 'first' == $fields[ $index ] ) {
517
518 $text = str_replace( $search_key, $replace[0], $text );
519
520 } elseif ( 'middle' == $fields[ $index ] ) {
521
522 $text = str_replace( $search_key, $replace[1], $text );
523
524 } elseif ( 'last' == $fields[ $index ] ) {
525
526 $text = str_replace( $search_key, $replace[2], $text );
527
528 } else {
529
530 $text = str_replace( $search_key, implode(' ', $replace ), $text );
531 }
532
533 }
534
535 return $text;
536 }
537
538
539 /**
540 * Replace image/file tag with Image URL
541 *
542 * @param string $text
543 *
544 * @return string
545 */
546 public static function replace_file_tags( $text, $entry_id ) {
547 $pattern = '/{(?:image|file):(\w*)}/';
548
549 preg_match_all( $pattern, $text, $matches );
550
551 // bail out if nothing found to be replaced
552 if ( !$matches ) {
553 return $text;
554 }
555
556 foreach ($matches[1] as $index => $meta_key) {
557
558 $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
559
560 $files = array();
561
562 if ( is_array( $meta_value ) ) {
563
564 foreach ( $meta_value as $key => $attachment_id ) {
565
566 $file_url = wp_get_attachment_url( $attachment_id );
567
568 if ( $file_url ) {
569 $files[] = $file_url;
570 }
571 }
572
573 } else {
574
575 $file_url = wp_get_attachment_url( $attachment_id );
576
577 if ( $file_url ) {
578
579 $files[] = $file_url;
580 }
581 }
582
583 $files = implode(" ", $files);
584
585 $text = str_replace( $matches[0][$index], $files, $text );
586 }
587
588 return $text;
589 }
590
591 /**
592 * Get property of a user object with failsafe check
593 *
594 * @param string $property
595 *
596 * @return string
597 */
598 public function get_user_prop( $property ) {
599 $user = wp_get_current_user();
600
601 if ( $user->ID != 0 ) {
602 return $user->{$property};
603 }
604
605 return '';
606 }
607
608 /**
609 * Replace text with merge tags
610 *
611 * @param string $text
612 *
613 * @return text
614 */
615 public function replace_tags( $text = '' ) {
616 $merge_keys = array_keys( $this->merge_tags );
617 $merge_values = array_values( $this->merge_tags );
618
619 $text = str_replace( $merge_keys, $merge_values, $text );
620 $text = static::replace_field_tags( $text, $this->args['entry_id'] );
621 $text = static::replace_file_tags( $text, $this->args['entry_id'] );
622
623 return $text;
624 }
625
626 /**
627 * Replace {all_fields} if found
628 *
629 * @param string $text
630 *
631 * @return string
632 */
633 public function replace_all_fields( $text = '' ) {
634
635 // check if {all_fields} exists
636 if ( false === strpos( $text, '{all_fields}' ) ) {
637 return $text;
638 }
639
640 $form = weforms()->form->get( $this->args['form_id'] );
641 $entry = $form->entries()->get( $this->args['entry_id'] );
642 $fields = $entry->get_fields();
643
644 if ( !$fields ) {
645 return $text;
646 }
647
648 $table = '<table width="600" cellpadding="0" cellspacing="0">';
649 $table .= '<tbody>';
650
651 foreach ($fields as $key => $value) {
652
653 $field_value = isset( $value[ 'value' ] ) ? $value[ 'value' ] : '';
654
655 if ( ! $field_value ) {
656 continue; // let's skip empty fields
657 }
658
659 $table .= '<tr class="field-label">';
660 $table .= '<th><strong>' . $value['label'] . '</strong></th>';
661 $table .= '</tr>';
662 $table .= '<tr class="field-value">';
663 $table .= '<td>';
664
665 if ( in_array( $value['type'], array( 'multiple_select', 'checkbox_field' ) ) ) {
666 $field_value = is_array( $field_value ) ? $field_value : array();
667
668 if ( $field_value ) {
669 $table .= '<ul>';
670 foreach ($field_value as $value_key) {
671 $table .= '<li>' . $value_key . '</li>';
672 }
673 $table .= '</ul>';
674 } else {
675 $table .= '&mdash;';
676 }
677 } else {
678 $table .= $field_value;
679 }
680
681 $table .= '</td>';
682 $table .= '</tr>';
683 }
684
685 $table .= '</tbody>';
686 $table .= '</table>';
687
688 $text = str_replace( '{all_fields}', $table, $text );
689
690 return $text;
691 }
692 }
693