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

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