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

457 lines 12.1 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 $this->send_notification( $notification );
42 }
43 }
44
45 /**
46 * Send a single notification
47 *
48 * @param array $notification
49 *
50 * @return void
51 */
52 public function send_notification( $notification ) {
53 $headers = array();
54
55 $to = $this->replace_tags( $notification['to'] );
56
57 $subject = $this->replace_tags( $notification['subject'] );
58 $subject = static::replace_name_tag( $subject, $this->args['entry_id'] );
59
60 $message = $this->replace_tags( $notification['message'] );
61 $message = static::replace_name_tag( $message, $this->args['entry_id'] );
62 $message = $this->replace_all_fields( $message );
63 $message = wpautop( $message );
64
65 $fromName = $this->replace_tags( $notification['fromName'] );
66 $fromName = static::replace_name_tag( $fromName, $this->args['entry_id'] );
67 $fromAddress = $this->replace_tags( $notification['fromAddress'] );
68 $replyTo = $this->replace_tags( $notification['replyTo'] );
69 $cc = $this->replace_tags( $notification['cc'] );
70 $bcc = $this->replace_tags( $notification['bcc'] );
71
72 if ( $fromName || $fromAddress ) {
73 $headers['from'] = array(
74 'email' => $fromAddress,
75 'name' => $fromName
76 );
77 }
78
79 if ( $cc ) {
80 $headers['cc'] = $cc;
81 }
82
83 if ( $bcc ) {
84 $headers['bcc'] = $bcc;
85 }
86
87 if ( $replyTo ) {
88 $headers['replyto'] = $replyTo;
89 }
90
91 // content type to text/html
92 $headers[] = 'Content-Type: text/html; charset=UTF-8';
93 $email_body = apply_filters( 'weforms_email_message', $this->get_formatted_body( $message ), $notification['message'], $headers );
94
95 weforms()->emailer->send( $to, $subject, $email_body, $headers );
96 }
97
98 /**
99 * Get active notifications of a form
100 *
101 * @param int $form_id
102 *
103 * @return array|boolean
104 */
105 public function get_active_notifications() {
106 $notifications = wpuf_get_form_notifications( $this->args['form_id'] );
107
108 if ( $notifications ) {
109 $notifications = array_filter( $notifications, function($notification) {
110 return $notification['active'] == true;
111 } );
112
113 return $notifications;
114 }
115
116 return false;
117 }
118
119 /**
120 * Get formatted HTML email
121 *
122 * @param string $message
123 *
124 * @return string
125 */
126 public function get_formatted_body( $message ) {
127 $css = '';
128 $header = apply_filters( 'weforms_email_header', '' );
129 $footer = apply_filters( 'weforms_email_footer', '' );
130
131 if ( empty( $header ) ) {
132 ob_start();
133 include WEFORMS_INCLUDES . '/email/template/header.php';
134 $header = ob_get_clean();
135 }
136
137 if ( empty( $footer ) ) {
138 ob_start();
139 include WEFORMS_INCLUDES . '/email/template/footer.php';
140 $footer = ob_get_clean();
141 }
142
143 ob_start();
144 include WEFORMS_INCLUDES . '/email/template/styles.php';
145 $css = apply_filters( 'weforms_email_styles', ob_get_clean() );
146
147 $content = $header . $message . $footer;
148
149 if ( ! class_exists( 'Emogrifier' ) ) {
150 require_once WEFORMS_INCLUDES . '/library/Emogrifier.php';
151 }
152
153 try {
154
155 // apply CSS styles inline for picky email clients
156 $emogrifier = new Emogrifier( $content, $css );
157 $content = $emogrifier->emogrify();
158
159 } catch ( Exception $e ) {
160
161 echo $e->getMessage();
162 }
163
164 return $content;
165 }
166
167 /**
168 * Populate an key/value pair of search/replace array
169 *
170 * @return array
171 */
172 public function set_merge_tags() {
173
174 // return early we had a previous array call
175 if ( $this->merge_tags ) {
176 return $this->merge_tags;
177 }
178
179 // populate the key/value array for the first time
180 $tags = weforms_get_merge_tags();
181 $replace_array = array();
182
183 foreach ($tags as $section => $child) {
184
185 if ( !$child['tags'] ) {
186 continue;
187 }
188
189 foreach ($child['tags'] as $search_key => $label) {
190 $replace_array[ '{' . $search_key . '}' ] = $this->get_merge_value( $search_key );
191 }
192 }
193
194 $this->merge_tags = $replace_array;
195 }
196
197 /**
198 * Get a merge tag value based on a tag
199 *
200 * @param string $tag
201 *
202 * @return string
203 */
204 public function get_merge_value( $tag ) {
205
206 switch ( $tag ) {
207 case 'entry_id':
208 return $this->args['entry_id'];
209 break;
210
211 case 'form_id':
212 return $this->args['form_id'];
213 break;
214
215 case 'form_name':
216 return get_post_field( 'post_title', $this->args['form_id'] );
217 break;
218
219 case 'admin_email':
220 return get_option( 'admin_email' );
221 break;
222
223 case 'date':
224 return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
225 break;
226
227 case 'site_name':
228 return get_bloginfo( 'name' );
229 break;
230
231 case 'site_url':
232 return site_url( '/' );
233 break;
234
235 case 'page_title':
236 return get_post_field( 'post_title', $this->args['page_id'] );
237 break;
238
239 case 'ip_address':
240 return wpuf_get_client_ip();
241 break;
242
243 case 'user_id':
244 return get_current_user_id();
245 break;
246
247 case 'first_name':
248 return $this->get_user_prop( 'first_name' );
249 break;
250
251 case 'last_name':
252 return $this->get_user_prop( 'last_name' );
253 break;
254
255 case 'display_name':
256 return $this->get_user_prop( 'display_name' );
257 break;
258
259 case 'user_email':
260 return $this->get_user_prop( 'user_email' );
261 break;
262
263 case 'url_page':
264 return get_permalink( $this->args['page_id'] );
265 break;
266
267 case 'url_referer':
268 return $_SERVER['HTTP_REFERER'];
269 break;
270
271 case 'url_login':
272 return wp_login_url();
273 break;
274
275 case 'url_logout':
276 return wp_logout_url();
277 break;
278
279 case 'url_register':
280 return wp_registration_url();
281 break;
282
283 case 'url_lost_password':
284 return wp_lostpassword_url();
285 break;
286
287 default:
288 return apply_filters( 'weforms_merge_tag_value', '', $tag, $this->args );
289 break;
290 }
291 }
292
293 /**
294 * Parse out the custom fields with entry meta values
295 *
296 * @param string $text
297 *
298 * @return string
299 */
300 public static function replace_field_tags( $text, $entry_id ) {
301 $pattern = '/{field:(\w*)}/';
302
303 preg_match_all( $pattern, $text, $matches );
304
305 // bail out if nothing found to be replaced
306 if ( !$matches ) {
307 return $text;
308 }
309
310 foreach ($matches[1] as $index => $meta_key) {
311 $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
312 $text = str_replace( $matches[0][$index], $meta_value, $text );
313 }
314
315 return $text;
316 }
317
318 /**
319 * Replace name tag with required value
320 *
321 * @param string $text
322 *
323 * @return string
324 */
325 public static function replace_name_tag( $text, $entry_id ) {
326 $pattern = '/{name-(full|first|middle|last):(\w*)}/';
327
328 preg_match_all( $pattern, $text, $matches );
329
330 // bail out if nothing found to be replaced
331 if ( !$matches[0] ) {
332 return $text;
333 }
334
335 list( $search, $fields, $meta_key ) = $matches;
336
337 $meta_value = weforms_get_entry_meta( $entry_id, $meta_key[0], true );
338 $replace = explode( WPUF_Render_Form::$separator, $meta_value );
339
340 foreach ($search as $index => $search_key) {
341
342 if ( 'first' == $fields[ $index ] ) {
343
344 $text = str_replace( $search_key, $replace[0], $text );
345
346 } elseif ( 'middle' == $fields[ $index ] ) {
347
348 $text = str_replace( $search_key, $replace[1], $text );
349
350 } elseif ( 'last' == $fields[ $index ] ) {
351
352 $text = str_replace( $search_key, $replace[2], $text );
353
354 } else {
355
356 $text = str_replace( $search_key, implode(' ', $replace ), $text );
357 }
358
359 }
360
361 return $text;
362 }
363
364 /**
365 * Get property of a user object with failsafe check
366 *
367 * @param string $property
368 *
369 * @return string
370 */
371 public function get_user_prop( $property ) {
372 $user = wp_get_current_user();
373
374 if ( $user->ID != 0 ) {
375 return $user->{$property};
376 }
377
378 return '';
379 }
380
381 /**
382 * Replace text with merge tags
383 *
384 * @param string $text
385 *
386 * @return text
387 */
388 public function replace_tags( $text = '' ) {
389 $merge_keys = array_keys( $this->merge_tags );
390 $merge_values = array_values( $this->merge_tags );
391
392 $text = str_replace( $merge_keys, $merge_values, $text );
393 $text = static::replace_field_tags( $text, $this->args['entry_id'] );
394
395 return $text;
396 }
397
398 /**
399 * Replace {all_fields} if found
400 *
401 * @param string $text
402 *
403 * @return string
404 */
405 public function replace_all_fields( $text = '' ) {
406
407 // check if {all_fields} exists
408 if ( false === strpos( $text, '{all_fields}' ) ) {
409 return $text;
410 }
411
412 $data = array();
413 $fields = weforms_get_form_field_labels( $this->args['form_id'] );
414
415 if ( !$fields ) {
416 return $text;
417 }
418
419 foreach ($fields as $meta_key => $field ) {
420 $value = weforms_get_entry_meta( $this->args['entry_id'], $meta_key, true );
421
422 if ( empty( $value ) ) {
423 $value = '&mdash;';
424 }
425
426 if ( $field['type'] == 'textarea' ) {
427 $data[ $meta_key ] = weforms_format_text( $value );
428 } elseif( $field['type'] == 'name' ) {
429 $data[ $meta_key ] = implode( ' ', explode( WPUF_Render_Form::$separator, $value ) );
430 } else {
431 $data[ $meta_key ] = $value;
432 }
433 }
434
435 $table = '<table width="600" cellpadding="0" cellspacing="0">';
436 $table .= '<tbody>';
437
438 foreach ($data as $key => $value) {
439 $table .= '<tr class="field-label">';
440 $table .= '<th><strong>' . $fields[$key]['label'] . '</strong></th>';
441 $table .= '</tr>';
442 $table .= '<tr class="field-value">';
443 $table .= '<td>';
444 $table .= $value;
445 $table .= '</td>';
446 $table .= '</tr>';
447 }
448
449 $table .= '</tbody>';
450 $table .= '</table>';
451
452 $text = str_replace( '{all_fields}', $table, $text );
453
454 return $text;
455 }
456 }
457