| 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 = weforms()->form->get( $this->args['form_id'] )->get_notifications(); |
| 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 weforms_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 |
|
| 313 |
if ( is_array( $meta_value ) ) { |
| 314 |
$meta_value = implode(WeForms::$field_separator, $meta_value); |
| 315 |
} |
| 316 |
|
| 317 |
$text = str_replace( $matches[0][$index], $meta_value, $text ); |
| 318 |
} |
| 319 |
|
| 320 |
return $text; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Replace name tag with required value |
| 325 |
* |
| 326 |
* @param string $text |
| 327 |
* |
| 328 |
* @return string |
| 329 |
*/ |
| 330 |
public static function replace_name_tag( $text, $entry_id ) { |
| 331 |
$pattern = '/{name-(full|first|middle|last):(\w*)}/'; |
| 332 |
|
| 333 |
preg_match_all( $pattern, $text, $matches ); |
| 334 |
|
| 335 |
// bail out if nothing found to be replaced |
| 336 |
if ( !$matches[0] ) { |
| 337 |
return $text; |
| 338 |
} |
| 339 |
|
| 340 |
list( $search, $fields, $meta_key ) = $matches; |
| 341 |
|
| 342 |
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key[0], true ); |
| 343 |
$replace = explode( WeForms::$field_separator, $meta_value ); |
| 344 |
|
| 345 |
foreach ($search as $index => $search_key) { |
| 346 |
|
| 347 |
if ( 'first' == $fields[ $index ] ) { |
| 348 |
|
| 349 |
$text = str_replace( $search_key, $replace[0], $text ); |
| 350 |
|
| 351 |
} elseif ( 'middle' == $fields[ $index ] ) { |
| 352 |
|
| 353 |
$text = str_replace( $search_key, $replace[1], $text ); |
| 354 |
|
| 355 |
} elseif ( 'last' == $fields[ $index ] ) { |
| 356 |
|
| 357 |
$text = str_replace( $search_key, $replace[2], $text ); |
| 358 |
|
| 359 |
} else { |
| 360 |
|
| 361 |
$text = str_replace( $search_key, implode(' ', $replace ), $text ); |
| 362 |
} |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
return $text; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
/** |
| 371 |
* Replace image/file tag with Image URL |
| 372 |
* |
| 373 |
* @param string $text |
| 374 |
* |
| 375 |
* @return string |
| 376 |
*/ |
| 377 |
public static function replace_file_tags( $text, $entry_id ) { |
| 378 |
$pattern = '/{(?:image|file):(\w*)}/'; |
| 379 |
|
| 380 |
preg_match_all( $pattern, $text, $matches ); |
| 381 |
|
| 382 |
// bail out if nothing found to be replaced |
| 383 |
if ( !$matches ) { |
| 384 |
return $text; |
| 385 |
} |
| 386 |
|
| 387 |
foreach ($matches[1] as $index => $meta_key) { |
| 388 |
|
| 389 |
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true ); |
| 390 |
|
| 391 |
$files = array(); |
| 392 |
|
| 393 |
if ( is_array( $meta_value ) ) { |
| 394 |
|
| 395 |
foreach ( $meta_value as $key => $attachment_id ) { |
| 396 |
|
| 397 |
$file_url = wp_get_attachment_url( $attachment_id ); |
| 398 |
|
| 399 |
if ( $file_url ) { |
| 400 |
$files[] = $file_url; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
} else { |
| 405 |
|
| 406 |
$file_url = wp_get_attachment_url( $attachment_id ); |
| 407 |
|
| 408 |
if ( $file_url ) { |
| 409 |
|
| 410 |
$files[] = $file_url; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
$files = implode(" ", $files); |
| 415 |
|
| 416 |
$text = str_replace( $matches[0][$index], $files, $text ); |
| 417 |
} |
| 418 |
|
| 419 |
return $text; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Get property of a user object with failsafe check |
| 424 |
* |
| 425 |
* @param string $property |
| 426 |
* |
| 427 |
* @return string |
| 428 |
*/ |
| 429 |
public function get_user_prop( $property ) { |
| 430 |
$user = wp_get_current_user(); |
| 431 |
|
| 432 |
if ( $user->ID != 0 ) { |
| 433 |
return $user->{$property}; |
| 434 |
} |
| 435 |
|
| 436 |
return ''; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Replace text with merge tags |
| 441 |
* |
| 442 |
* @param string $text |
| 443 |
* |
| 444 |
* @return text |
| 445 |
*/ |
| 446 |
public function replace_tags( $text = '' ) { |
| 447 |
$merge_keys = array_keys( $this->merge_tags ); |
| 448 |
$merge_values = array_values( $this->merge_tags ); |
| 449 |
|
| 450 |
$text = str_replace( $merge_keys, $merge_values, $text ); |
| 451 |
$text = static::replace_field_tags( $text, $this->args['entry_id'] ); |
| 452 |
$text = static::replace_file_tags( $text, $this->args['entry_id'] ); |
| 453 |
|
| 454 |
return $text; |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Replace {all_fields} if found |
| 459 |
* |
| 460 |
* @param string $text |
| 461 |
* |
| 462 |
* @return string |
| 463 |
*/ |
| 464 |
public function replace_all_fields( $text = '' ) { |
| 465 |
|
| 466 |
// check if {all_fields} exists |
| 467 |
if ( false === strpos( $text, '{all_fields}' ) ) { |
| 468 |
return $text; |
| 469 |
} |
| 470 |
|
| 471 |
$form = weforms()->form->get( $this->args['form_id'] ); |
| 472 |
$entry = $form->entries()->get( $this->args['entry_id'] ); |
| 473 |
$fields = $entry->get_fields(); |
| 474 |
|
| 475 |
if ( !$fields ) { |
| 476 |
return $text; |
| 477 |
} |
| 478 |
|
| 479 |
$table = '<table width="600" cellpadding="0" cellspacing="0">'; |
| 480 |
$table .= '<tbody>'; |
| 481 |
|
| 482 |
foreach ($fields as $key => $value) { |
| 483 |
$table .= '<tr class="field-label">'; |
| 484 |
$table .= '<th><strong>' . $value['label'] . '</strong></th>'; |
| 485 |
$table .= '</tr>'; |
| 486 |
$table .= '<tr class="field-value">'; |
| 487 |
$table .= '<td>'; |
| 488 |
|
| 489 |
$field_value = $value[ 'value' ]; |
| 490 |
|
| 491 |
if ( in_array( $value['type'], array( 'multiple_select', 'checkbox_field' ) ) ) { |
| 492 |
$field_value = is_array( $field_value ) ? $field_value : array(); |
| 493 |
|
| 494 |
if ( $field_value ) { |
| 495 |
$table .= '<ul>'; |
| 496 |
foreach ($field_value as $value_key) { |
| 497 |
$table .= '<li>' . $value_key . '</li>'; |
| 498 |
} |
| 499 |
$table .= '</ul>'; |
| 500 |
} else { |
| 501 |
$table .= '—'; |
| 502 |
} |
| 503 |
} else { |
| 504 |
$table .= $field_value; |
| 505 |
} |
| 506 |
|
| 507 |
$table .= '</td>'; |
| 508 |
$table .= '</tr>'; |
| 509 |
} |
| 510 |
|
| 511 |
$table .= '</tbody>'; |
| 512 |
$table .= '</table>'; |
| 513 |
|
| 514 |
$text = str_replace( '{all_fields}', $table, $text ); |
| 515 |
|
| 516 |
return $text; |
| 517 |
} |
| 518 |
} |
| 519 |
|