| @@ -1,76 +1,115 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 2 | +if(!defined('ABSPATH')) die('You are not allowed to call this page directly.'); | |
| 5 | 3 | |
| 6 | -class FrmNotification { | |
| 7 | - public function __construct() { | |
| 8 | - self::hook_emails_to_action(); | |
| 9 | - } | |
| 4 | +class FrmNotification{ | |
| 5 | + function __construct(){ | |
| 6 | + add_action('frm_after_create_entry', array(&$this, 'entry_created'), 10, 2); | |
| 7 | + } | |
| 8 | + | |
| 9 | + function entry_created($entry_id, $form_id){ | |
| 10 | + if (apply_filters('frm_stop_standard_email', false, $entry_id)) return; | |
| 11 | + global $frm_entry, $frm_entry_meta; | |
| 12 | + | |
| 13 | + $entry = $frm_entry->getOne($entry_id, true); | |
| 14 | + $frm_form = new FrmForm(); | |
| 15 | + $form = $frm_form->getOne($form_id); | |
| 16 | + $values = $frm_entry_meta->getAll("it.item_id = $entry_id", " ORDER BY fi.field_order"); | |
| 17 | + | |
| 18 | + | |
| 19 | + if(isset($form->options['notification'])) | |
| 20 | + $notification = reset($form->options['notification']); | |
| 21 | + else | |
| 22 | + $notification = $form->options; | |
| 23 | + | |
| 24 | + // Set the from and to email names and addresses | |
| 25 | + $to_email = $notification['email_to']; | |
| 26 | + if ( empty($to_email) ) { | |
| 27 | + $to_email = '[admin_email]'; | |
| 28 | + } | |
| 10 | 29 | |
| 11 | - /** | |
| 12 | - * Trigger an email action | |
| 13 | - * | |
| 14 | - * @param object $action | |
| 15 | - * @param object $entry | |
| 16 | - * @param object $form | |
| 17 | - * | |
| 18 | - * @return void | |
| 19 | - */ | |
| 20 | - public static function trigger_email( $action, $entry, $form ) { | |
| 21 | - $email = new FrmEmail( $action, $entry, $form ); | |
| 30 | + $to_emails = explode(',', $to_email); | |
| 31 | + | |
| 32 | + $reply_to = $reply_to_name = ''; | |
| 33 | + | |
| 34 | + foreach ($values as $value) { | |
| 35 | + $val = apply_filters('frm_email_value', maybe_unserialize($value->meta_value), $value, $entry); | |
| 36 | + if (is_array($val)) | |
| 37 | + $val = implode(', ', $val); | |
| 22 | 38 | |
| 23 | - if ( ! $email->should_send() ) { | |
| 24 | - return; | |
| 25 | - } | |
| 39 | + if(isset($notification['reply_to']) and (int)$notification['reply_to'] == $value->field_id and is_email($val)) | |
| 40 | + $reply_to = $val; | |
| 26 | 41 | |
| 27 | - $sent = $email->send(); | |
| 42 | + if(isset($notification['reply_to_name']) and (int)$notification['reply_to_name'] == $value->field_id) | |
| 43 | + $reply_to_name = $val; | |
| 44 | + } | |
| 45 | + | |
| 46 | + | |
| 47 | + if ( empty($reply_to) && $notification['reply_to'] == 'custom' ) { | |
| 48 | + $reply_to = $notification['cust_reply_to']; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if ( empty($reply_to_name) && $notification['reply_to_name'] == 'custom' ){ | |
| 52 | + $reply_to_name = $notification['cust_reply_to_name']; | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Set the email message | |
| 56 | + $plain_text = (isset($notification['plain_text']) && $notification['plain_text']) ? true : false; | |
| 57 | + $mail_body = isset($notification['email_message']) ? $notification['email_message'] : ''; | |
| 58 | + | |
| 59 | + $mail_body = FrmEntriesHelper::replace_default_message($mail_body, array( | |
| 60 | + 'id' => $entry->id, 'entry' => $entry, 'plain_text' => $plain_text, | |
| 61 | + 'user_info' => (isset($notification['inc_user_info']) ? $notification['inc_user_info'] : false), | |
| 62 | + ) ); | |
| 63 | + | |
| 64 | + // Set the subject | |
| 65 | + $subject = isset($notification['email_subject']) ? $notification['email_subject'] : ''; | |
| 66 | + if ( empty($subject) ) { | |
| 67 | + $frm_blogname = wp_specialchars_decode( get_option('blogname'), ENT_QUOTES ); | |
| 68 | + $subject = sprintf(__('%1$s Form submitted on %2$s', 'formidable'), $form->name, $frm_blogname); | |
| 69 | + } | |
| 70 | + | |
| 71 | + // Send the emails now | |
| 72 | + foreach ( (array) $to_emails as $to_email) { | |
| 73 | + $this->send_notification_email(trim($to_email), $subject, $mail_body, $reply_to, $reply_to_name, $plain_text); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + function send_notification_email($to_email, $subject, $message, $reply_to='', $reply_to_name='', $plain_text=true, $attachments=array()){ | |
| 78 | + $content_type = ($plain_text) ? 'text/plain' : 'text/html'; | |
| 79 | + $reply_to_name = ($reply_to_name == '') ? wp_specialchars_decode( get_option('blogname'), ENT_QUOTES ) : $reply_to_name; //senders name | |
| 80 | + $reply_to = ($reply_to == '' or $reply_to == '[admin_email]') ? get_option('admin_email') : $reply_to; //senders e-mail address | |
| 81 | + | |
| 82 | + if($to_email == '[admin_email]') | |
| 83 | + $to_email = get_option('admin_email'); | |
| 84 | + | |
| 85 | + $charset = get_option('blog_charset'); | |
| 86 | + $recipient = $to_email; //recipient | |
| 87 | + $header = array(); | |
| 88 | + $header[] = 'From: "'. $reply_to_name .'" <'. $reply_to .'>'; | |
| 89 | + $header[] = 'Reply-To: '. $reply_to; | |
| 90 | + $header[] = 'Content-Type: '. $content_type .'; charset="'. $charset . '"'; | |
| 91 | + $subject = wp_specialchars_decode(strip_tags(stripslashes($subject)), ENT_QUOTES ); | |
| 92 | + | |
| 93 | + $message = do_shortcode($message); | |
| 94 | + $message = wordwrap($message, 70, "\r\n"); //in case any lines are longer than 70 chars | |
| 95 | + if($plain_text) | |
| 96 | + $message = wp_specialchars_decode(strip_tags($message), ENT_QUOTES ); | |
| 28 | 97 | |
| 29 | - if ( $sent ) { | |
| 30 | - self::print_recipients( $email->package_atts() ); | |
| 31 | - } | |
| 32 | - } | |
| 98 | + $header = apply_filters('frm_email_header', $header, compact('to_email', 'subject')); | |
| 99 | + | |
| 100 | + if ( apply_filters('frm_encode_subject', 1, $subject ) ) { | |
| 101 | + $subject = '=?'. $charset .'?B?'. base64_encode($subject) .'?='; | |
| 102 | + } | |
| 103 | + | |
| 104 | + remove_filter('wp_mail_from', 'bp_core_email_from_address_filter' ); | |
| 105 | + remove_filter('wp_mail_from_name', 'bp_core_email_from_name_filter'); | |
| 106 | + | |
| 107 | + if (!wp_mail($recipient, $subject, $message, $header, $attachments)){ | |
| 108 | + $header = 'From: "'. $reply_to_name .'" <'. $reply_to .'>'. "\r\n"; | |
| 109 | + mail($recipient, $subject, $message, $header); | |
| 110 | + } | |
| 33 | 111 | |
| 34 | - /** | |
| 35 | - * Remove the trigger_email function from the frm_trigger_email_action hook | |
| 36 | - * | |
| 37 | - * @since 2.03.04 | |
| 38 | - * | |
| 39 | - * @return void | |
| 40 | - */ | |
| 41 | - public static function stop_emails() { | |
| 42 | - remove_action( 'frm_trigger_email_action', 'FrmNotification::trigger_email', 10 ); | |
| 43 | - } | |
| 112 | + do_action('frm_notification', $recipient, $subject, $message); | |
| 113 | + } | |
| 44 | 114 | |
| 45 | - /** | |
| 46 | - * Hook the trigger_email function to frm_trigger_email_action action | |
| 47 | - * | |
| 48 | - * @since 2.03.04 | |
| 49 | - * | |
| 50 | - * @return void | |
| 51 | - */ | |
| 52 | - public static function hook_emails_to_action() { | |
| 53 | - add_action( 'frm_trigger_email_action', 'FrmNotification::trigger_email', 10, 3 ); | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Print the recipients | |
| 58 | - * | |
| 59 | - * @since 2.03.04 | |
| 60 | - * | |
| 61 | - * @param array $atts | |
| 62 | - * | |
| 63 | - * @return void | |
| 64 | - */ | |
| 65 | - private static function print_recipients( $atts ) { | |
| 66 | - if ( ! apply_filters( 'frm_echo_emails', false ) ) { | |
| 67 | - return; | |
| 68 | - } | |
| 69 | - | |
| 70 | - $sent_to = array_merge( (array) $atts['to_email'], (array) $atts['cc'], (array) $atts['bcc'] ); | |
| 71 | - $sent_to = array_filter( $sent_to ); | |
| 72 | - $temp = str_replace( '<', '<', $sent_to ); | |
| 73 | - echo ' '; | |
| 74 | - FrmAppHelper::kses_echo( implode( ', ', $temp ) ); | |
| 75 | - } | |
| 76 | 115 | } |