1: <?php
2:
3: 4: 5:
6: class Quform_Notification
7: {
8: 9: 10:
11: protected $config = array();
12:
13: 14: 15:
16: protected $form;
17:
18: 19: 20:
21: protected $options;
22:
23: 24: 25: 26: 27:
28: public function __construct(array $config, Quform_Form $form, Quform_Options $options)
29: {
30: $this->setConfig($config);
31: $this->form = $form;
32: $this->options = $options;
33: }
34:
35: 36: 37:
38: public function send()
39: {
40: do_action('quform_pre_get_mailer_config', $this, $this->form);
41: do_action('quform_pre_get_mailer_config_' . $this->getIdentifier(), $this, $this->form);
42:
43: $config = $this->getMailerConfig();
44:
45: do_action('quform_post_get_mailer_config', $this, $this->form);
46: do_action('quform_post_get_mailer_config_' . $this->getIdentifier(), $this, $this->form);
47:
48: if (is_array($config)) {
49: add_action('phpmailer_init', array($this, 'processHooks'));
50:
51: wp_mail($config['to'], $config['subject'], $config['message'], $config['headers'], $config['attachments']);
52:
53: remove_action('phpmailer_init', array($this, 'processHooks'));
54:
55: do_action('quform_post_send_notification', $this, $this->form);
56: do_action('quform_post_send_notification_' . $this->getIdentifier(), $this, $this->form);
57: }
58: }
59:
60: 61: 62: 63: 64: 65:
66: protected function replaceVariables(array $recipient)
67: {
68: $recipient['address'] = $this->form->replaceVariables($recipient['address']);
69: $recipient['name'] = $this->form->replaceVariables($recipient['name']);
70:
71: return $recipient;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function config($key, $default = null)
82: {
83: $value = Quform::get($this->config, $key, $default);
84:
85: if ($value === null) {
86: $value = Quform::get(call_user_func(array(get_class($this), 'getDefaultConfig')), $key, $default);
87: }
88:
89: return $value;
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function setConfig($key, $value = null)
100: {
101: if (is_array($key)) {
102: foreach ($key as $k => $v) {
103: $this->config[$k] = $v;
104: }
105: } else {
106: $this->config[$key] = $value;
107: }
108:
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: public function getMailerConfig()
118: {
119: $config = array(
120: 'to' => array(),
121: 'headers' => array(),
122: 'subject' => $this->form->replaceVariables($this->config('subject')),
123: 'message' => '',
124: 'attachments' => array()
125: );
126:
127: $recipients = array();
128: $emailValidator = new Quform_Validator_Email();
129:
130: if ($this->config('conditional')) {
131: foreach ($this->config('conditionals') as $conditional) {
132: if (count($conditional['logicRules']) && $this->form->checkLogicAction($conditional['logicAction'], $conditional['logicMatch'], $conditional['logicRules'])) {
133: foreach ($conditional['recipients'] as $recipient) {
134: $recipient = $this->replaceVariables($recipient);
135: if ($emailValidator->isValid($recipient['address'])) {
136: $recipients[] = $recipient;
137: }
138: }
139: }
140: }
141:
142: if ( ! count($recipients)) {
143: if ($this->config('conditionalFallback') == 'do_not_send') {
144: return false;
145: } else {
146: foreach ($this->config('recipients') as $recipient) {
147: $recipient = $this->replaceVariables($recipient);
148: if ($emailValidator->isValid($recipient['address'])) {
149: $recipients[] = $recipient;
150: }
151: }
152: }
153: }
154: } else {
155: foreach ($this->config('recipients') as $recipient) {
156: $recipient = $this->replaceVariables($recipient);
157: if ($emailValidator->isValid($recipient['address'])) {
158: $recipients[] = $recipient;
159: }
160: }
161: }
162:
163: foreach ($recipients as $recipient) {
164: $formatted = $this->formatRecipient($recipient);
165:
166: switch ($recipient['type']) {
167: case 'to':
168: $config['to'][] = $formatted;
169: break;
170: case 'cc':
171: $config['headers'][] = 'Cc: ' . $formatted;
172: break;
173: case 'bcc':
174: $config['headers'][] = 'Bcc: ' . $formatted;
175: break;
176: case 'reply':
177: $config['headers'][] = 'Reply-to: ' . $formatted;
178: break;
179: }
180: }
181:
182: if (Quform::isNonEmptyString($this->config('from.address'))) {
183: $from = $this->replaceVariables($this->config('from'));
184: if ($emailValidator->isValid($from['address'])) {
185: $config['headers'][] = 'From: ' . $this->formatRecipient($from);
186: }
187: }
188:
189: if ($this->config('format') == 'html' || $this->config('format') == 'multipart') {
190: $config['headers'][] = sprintf('Content-type: text/html; charset=%s', apply_filters('wp_mail_charset', get_bloginfo('charset')));
191: $config['message'] = $this->config('html');
192:
193: if ($this->config('autoFormat')) {
194: $config['message'] = nl2br($config['message']);
195: }
196:
197: $config['message'] = $this->form->replaceVariables($config['message'], 'html');
198:
199: if ($this->isRtl()) {
200: $config['message'] = sprintf('<div dir="rtl">%s</div>', $config['message']);
201: }
202:
203: $config['message'] = $this->wrapHtmlMessage($config['message']);
204: } else {
205: $config['message'] = $this->form->replaceVariables($this->config('text'));
206: }
207:
208: foreach ($this->config('attachments') as $attachment) {
209: if ($attachment['source'] == 'element') {
210: $element = $this->form->getElementById($attachment['element']);
211:
212: if ($element instanceof Quform_Attachable && $element->hasAttachments()) {
213: foreach ($element->getAttachments() as $file) {
214: if (is_file($file)) {
215: $config['attachments'][] = $file;
216: }
217: }
218: }
219: } elseif ($attachment['source'] == 'media') {
220: if (is_array($attachment['media'])) {
221: foreach ($attachment['media'] as $medium) {
222: $post = get_post($medium['id']);
223:
224: if ($post instanceof WP_Post && $post->post_type == 'attachment') {
225: $file = get_attached_file($post->ID);
226:
227: if (is_file($file)) {
228: $config['attachments'][] = $file;
229: }
230: }
231: }
232: }
233: }
234: }
235:
236: $config = apply_filters('quform_notification_mailer_config', $config, $this, $this->form);
237: $config = apply_filters('quform_notification_mailer_config_' . $this->getIdentifier(), $config, $this, $this->form);
238:
239: return $config;
240: }
241:
242: 243: 244: 245: 246:
247: public function isRtl()
248: {
249: if ($this->config('rtl') == 'inherit') {
250: return $this->form->isRtl();
251: }
252:
253: return $this->config('rtl') == 'yes';
254: }
255:
256: 257: 258: 259: 260:
261: public function processHooks($mailer)
262: {
263: do_action('quform_pre_send_notification', $mailer, $this, $this->form);
264: do_action('quform_pre_send_notification_' . $this->getIdentifier(), $mailer, $this, $this->form);
265: }
266:
267: 268: 269: 270: 271: 272:
273: protected function formatRecipient(array $recipient)
274: {
275: if (Quform::isNonEmptyString($recipient['name'])) {
276: $formatted = sprintf('%s <%s>', $recipient['name'], $recipient['address']);
277: } else {
278: $formatted = $recipient['address'];
279: }
280:
281: return $formatted;
282: }
283:
284:
285: 286: 287: 288: 289: 290:
291: protected function wrapHtmlMessage($message)
292: {
293: $start = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
294: <html xmlns="http://www.w3.org/1999/xhtml">
295: <head>
296: <meta http-equiv="Content-Type" content="text/html; charset=%s" />
297: <title></title>
298: </head>
299: <body style="margin:0;padding:%s;">';
300:
301: $start = sprintf(
302: $start,
303: esc_attr(apply_filters('wp_mail_charset', get_bloginfo('charset'))),
304: esc_attr(Quform::addCssUnit($this->config('padding')))
305: );
306:
307: $start = apply_filters('quform_notification_html_start', $start, $this);
308: $start = apply_filters('quform_notification_html_start_' . $this->getIdentifier(), $start, $this);
309:
310: $end = '</body></html>';
311:
312: $end = apply_filters('quform_notification_html_end', $end, $this);
313: $end = apply_filters('quform_notification_html_end_' . $this->getIdentifier(), $end, $this);
314:
315: return $start . $message . $end;
316: }
317:
318: 319: 320: 321: 322:
323: public function getIdentifier()
324: {
325: return sprintf('%d_%d', $this->form->getId(), $this->config('id'));
326: }
327:
328: 329: 330: 331: 332: 333: 334: 335: 336:
337: public static function addAltBody($mailer, Quform_Notification $notification, Quform_Form $form)
338: {
339: if ($notification->config('format') == 'multipart') {
340: $mailer->AltBody = $form->replaceVariables($notification->config('text'));
341: } else {
342: $mailer->AltBody = '';
343: }
344: }
345:
346: 347: 348: 349: 350: 351:
352: public static function getDefaultConfig($key = null)
353: {
354: $config = apply_filters('quform_default_notification', array(
355: 'name' => '',
356: 'enabled' => true,
357:
358: 'subject' => sprintf(__('New submission from %s', 'quform'), '{form_name}'),
359: 'format' => 'html',
360: 'html' => '{all_form_data}',
361: 'autoFormat' => true,
362: 'padding' => '20',
363: 'rtl' => 'inherit',
364: 'text' => '{all_form_data}',
365: 'recipients' => array(array('type' => 'to', 'address' => '{default_email_address}', 'name' => '{default_email_name}')),
366: 'conditional' => false,
367: 'conditionals' => array(),
368: 'conditionalFallback' => '',
369: 'from' => array('address' => '{default_from_email_address}', 'name' => '{default_from_email_name}'),
370: 'logicEnabled' => false,
371: 'logicAction' => true,
372: 'logicMatch' => 'all',
373: 'logicRules' => array(),
374: 'attachments' => array()
375: ));
376:
377: if (Quform::isNonEmptyString($key)) {
378: return Quform::get($config, $key);
379: }
380:
381: return $config;
382: }
383: }
384: