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