Overview

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface
  • Overview
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: class Quform_Notification
  7: {
  8:     /**
  9:      * @var array
 10:      */
 11:     protected $config = array();
 12: 
 13:     /**
 14:      * @var Quform_Form
 15:      */
 16:     protected $form;
 17: 
 18:     /**
 19:      * @var Quform_Options
 20:      */
 21:     protected $options;
 22: 
 23:     /**
 24:      * @param  array           $config
 25:      * @param  Quform_Form     $form
 26:      * @param  Quform_Options  $options
 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:      * Send the notification
 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:      * Replace variable tokens within the recipient fields.
 62:      *
 63:      * @param  array  $recipient
 64:      * @return array
 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:      * Returns the config value for the given $key
 76:      *
 77:      * @param   string  $key
 78:      * @param   null    $default
 79:      * @return  mixed   The config value or $default if not set
 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:      * Set the config value for the given $key or multiple values using an array
 94:      *
 95:      * @param   string|array  $key    Key or array of key/values
 96:      * @param   mixed         $value  Value or null if $key is array
 97:      * @return  $this
 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:      * Get the parameters to pass to the mail function
114:      *
115:      * @return array|false The mailer config array or false if this notification should not be sent
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:      * Is this notification RTL?
244:      *
245:      * @return bool
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:      * Run hooks before the email is sent
258:      *
259:      * @param \PHPMailer\PHPMailer\PHPMailer|PHPMailer $mailer
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:      * Format the given recipient for an email header
269:      *
270:      * @param   array   $recipient
271:      * @return  string
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:      * Wraps the given message in outer HTML tags
287:      *
288:      * @param   string  $message
289:      * @return  string
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:      * Get the notification unique ID
320:      *
321:      * @return string
322:      */
323:     public function getIdentifier()
324:     {
325:         return sprintf('%d_%d', $this->form->getId(), $this->config('id'));
326:     }
327: 
328:     /**
329:      * Add the AltBody to multipart email notifications
330:      *
331:      * Currently, this needs to be done with a hook as wp_mail() does not support multipart messages
332:      *
333:      * @param  \PHPMailer\PHPMailer\PHPMailer|PHPMailer $mailer        The PHPMailer instance
334:      * @param  Quform_Notification                      $notification  The notification instance
335:      * @param  Quform_Form                              $form          The form instance
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:      * Get the default notification configuration
348:      *
349:      * @param   string|null  $key  Get the config by key, if omitted the full config is returned
350:      * @return  array
351:      */
352:     public static function getDefaultConfig($key = null)
353:     {
354:         $config = apply_filters('quform_default_notification', array(
355:             'name' => '',
356:             'enabled' => true,
357:             /* translators: %s: the form name */
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: 
API documentation generated by ApiGen