PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.0
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.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.2.0, at includes/class-notification.php

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