| 1 |
<?php |
| 2 |
defined('ABSPATH') || die(); |
| 3 |
|
| 4 |
class HashFormEmail { |
| 5 |
|
| 6 |
public $form; |
| 7 |
public $entry_id; |
| 8 |
public $location; |
| 9 |
|
| 10 |
public function __construct($form, $entry_id, $location) { |
| 11 |
$this->form = $form; |
| 12 |
$this->entry_id = $entry_id; |
| 13 |
$this->location = $location; |
| 14 |
} |
| 15 |
|
| 16 |
private function get_form_settings() { |
| 17 |
return $this->form->settings; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Set while replaying a deferred send, so the filter that held the email |
| 22 |
* back does not hold it back a second time. |
| 23 |
*/ |
| 24 |
public static $sending_deferred = false; |
| 25 |
|
| 26 |
public function send_email() { |
| 27 |
$attachments = array(); |
| 28 |
$form_settings = $this->get_form_settings(); |
| 29 |
$entry = HashFormEntry::get_entry_vars($this->entry_id); |
| 30 |
$metas = $entry->metas; |
| 31 |
|
| 32 |
/** |
| 33 |
* Lets an add-on postpone the notification and auto responder, which |
| 34 |
* is what the payment gateways do so an abandoned checkout does not |
| 35 |
* send an order confirmation. |
| 36 |
* |
| 37 |
* Returning false must still let the submission finish normally. |
| 38 |
*/ |
| 39 |
if (!self::$sending_deferred && !apply_filters('hashform_send_entry_emails', true, $this->form, $this->entry_id, $metas)) { |
| 40 |
do_action('hashform_after_email', array( |
| 41 |
'form' => $this->form, |
| 42 |
'entry_id' => $this->entry_id, |
| 43 |
'form_settings' => $form_settings, |
| 44 |
'metas' => $metas, |
| 45 |
'location' => $this->location |
| 46 |
)); |
| 47 |
|
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
$email_to = isset($form_settings['email_to']) ? explode(',', $form_settings['email_to']) : ''; |
| 52 |
$email_from = isset($form_settings['email_from']) ? $form_settings['email_from'] : ''; |
| 53 |
$email_from = ($email_from == '[admin_email]') ? get_option('admin_email') : esc_attr($email_from); |
| 54 |
$email_from_name = isset($form_settings['email_from_name']) ? $form_settings['email_from_name'] : ''; |
| 55 |
$email_subject = isset($form_settings['email_subject']) ? $form_settings['email_subject'] : ''; |
| 56 |
$reply_to_email = isset($form_settings['reply_to_email']) ? $form_settings['reply_to_email'] : ''; |
| 57 |
$reply_to_ar = isset($form_settings['reply_to_ar']) ? $form_settings['reply_to_ar'] : ''; |
| 58 |
|
| 59 |
$settings = HashFormSettings::get_settings(); |
| 60 |
$email_template = $settings['email_template'] ? sanitize_text_field($settings['email_template']) : 'template1'; |
| 61 |
// The value feeds both a callable name and an include path. |
| 62 |
if (!in_array($email_template, array('template1', 'template2', 'template3'), true)) { |
| 63 |
$email_template = 'template1'; |
| 64 |
} |
| 65 |
$header_image = sanitize_text_field($settings['header_image']); |
| 66 |
$form_title = $this->form->name; |
| 67 |
$file_img_placeholder = HASHFORM_URL . 'img/attachment.png'; |
| 68 |
|
| 69 |
$replace_keys = apply_filters('hashform_replace_keys_arr', array('email_message')); |
| 70 |
|
| 71 |
$frm_table = ''; |
| 72 |
$count = 0; |
| 73 |
|
| 74 |
foreach ($metas as $item => $value) { |
| 75 |
$count++; |
| 76 |
$reply_to_email = str_replace('#field_id_' . absint($item), $value['value'], $reply_to_email); |
| 77 |
$email_subject = str_replace('#field_id_' . absint($item), $value['value'], $email_subject); |
| 78 |
$reply_to_ar = str_replace('#field_id_' . absint($item), $value['value'], $reply_to_ar); |
| 79 |
$entry_value = HashFormHelper::unserialize_or_decode($value['value']); |
| 80 |
$entry_type = HashFormHelper::unserialize_or_decode($value['type']); |
| 81 |
if (is_array($entry_value)) { |
| 82 |
if ($entry_type == 'name') { |
| 83 |
$entry_value = implode(' ', array_filter($entry_value)); |
| 84 |
} elseif ($entry_type == 'repeater_field') { |
| 85 |
$entry_value = HashFormHelper::render_repeater_table($entry_value); |
| 86 |
} else { |
| 87 |
$entry_value = implode(',<br>', array_filter($entry_value)); |
| 88 |
} |
| 89 |
} |
| 90 |
// No profile link here: a recipient may have no access to wp-admin. |
| 91 |
if ($entry_type == 'user_id') { |
| 92 |
$entry_value = HashFormFieldUserID::format_value( |
| 93 |
$entry_value, |
| 94 |
HashFormFieldUserID::capture_from_options(isset($value['options']) ? $value['options'] : array()), |
| 95 |
false |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
if ($entry_type == 'upload' && trim($entry_value)) { |
| 100 |
$files_arr = explode(',', $entry_value); |
| 101 |
$upload_value = ''; |
| 102 |
foreach ($files_arr as $file) { |
| 103 |
$file_info = pathinfo($file); |
| 104 |
$file_name = $file_info['basename']; |
| 105 |
$file_extension = $file_info['extension']; |
| 106 |
|
| 107 |
$upload_value .= '<div style="margin-bottom: 10px;padding-bottom: 10px;border-bottom: 1px solid #EEE;">'; |
| 108 |
$upload_value .= '<div><a href="' . esc_url($file) . '" target="_blank">'; |
| 109 |
if (in_array($file_extension, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) { |
| 110 |
$upload_value .= '<img style="width:150px" src="' . esc_url($file) . '">'; |
| 111 |
} else { |
| 112 |
$upload_value .= '<img style="width: 40px;border: 1px solid #666;border-radius: 6px;padding: 4px;" src="' . esc_url($file_img_placeholder) . '">'; |
| 113 |
} |
| 114 |
$upload_value .= '</a></div>'; |
| 115 |
$upload_value .= '<label><a href="' . esc_url($file) . '" target="_blank">'; |
| 116 |
$upload_value .= esc_html($file_name) . '</a></label>'; |
| 117 |
$upload_value .= '</div>'; |
| 118 |
} |
| 119 |
$entry_value = $upload_value; |
| 120 |
} |
| 121 |
// Shared with the entry screen; the email was still sending the |
| 122 |
// raw stored string, so the same submission read two ways. |
| 123 |
$entry_value = HashFormHelper::format_date_value($entry_value, $entry_type); |
| 124 |
|
| 125 |
/** This filter is documented in admin/entries/entry-detail.php */ |
| 126 |
$entry_value = apply_filters('hashform_entry_display_value', $entry_value, $entry_type, $value, 'email'); |
| 127 |
|
| 128 |
$frm_table .= call_user_func('HashFormEmail::' . $email_template, $value['name'], $entry_value, $count); |
| 129 |
|
| 130 |
foreach ($replace_keys as $key) { |
| 131 |
if (isset($form_settings[$key])) { |
| 132 |
$form_settings_val = $form_settings[$key]; |
| 133 |
if ($form_settings_val && is_string($form_settings_val)) { |
| 134 |
$form_settings[$key] = str_replace('#field_id_' . $item, $entry_value, $form_settings_val); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
foreach ($replace_keys as $key) { |
| 141 |
if (isset($form_settings[$key])) { |
| 142 |
$form_settings_val = $form_settings[$key]; |
| 143 |
if ($form_settings_val && is_string($form_settings_val)) { |
| 144 |
$key_val = str_replace('#form_title', $form_title, $form_settings_val); |
| 145 |
$form_settings[$key] = str_replace('#form_details', $frm_table, $key_val); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Submitted values were substituted in above; strip line breaks so they |
| 151 |
// cannot smuggle extra mail headers. |
| 152 |
$reply_to_email = str_replace(array("\r", "\n"), '', $reply_to_email); |
| 153 |
$email_subject = str_replace(array("\r", "\n"), ' ', $email_subject); |
| 154 |
// Becomes the recipient of the auto responder further down. |
| 155 |
$reply_to_ar = str_replace(array("\r", "\n"), '', $reply_to_ar); |
| 156 |
|
| 157 |
$email_message = empty($form_settings['email_message']) ? '' : wpautop($form_settings['email_message']); |
| 158 |
|
| 159 |
ob_start(); |
| 160 |
include(HASHFORM_PATH . 'admin/settings/email-templates/' . $email_template . '.php'); |
| 161 |
$email_message = ob_get_clean(); |
| 162 |
|
| 163 |
$head = array(); |
| 164 |
$head[] = 'Content-Type: text/html; charset=UTF-8'; |
| 165 |
$head[] = 'From: ' . esc_html($email_from_name) . ' <' . esc_html($email_from) . '>'; |
| 166 |
if ($reply_to_email) { |
| 167 |
$head[] = 'Reply-To: ' . esc_html($reply_to_email); |
| 168 |
} |
| 169 |
|
| 170 |
$recipients = array(); |
| 171 |
|
| 172 |
foreach ($email_to as $row) { |
| 173 |
$recipients[] = (trim($row) == '[admin_email]') ? get_option('admin_email') : $row; |
| 174 |
} |
| 175 |
|
| 176 |
if (!empty($attachments)) { |
| 177 |
$mail = wp_mail($recipients, $email_subject, $email_message, $head, $attachments); |
| 178 |
} else { |
| 179 |
$mail = wp_mail($recipients, $email_subject, $email_message, $head); |
| 180 |
} |
| 181 |
|
| 182 |
if ($mail) { |
| 183 |
if (isset($form_settings['enable_ar']) && $form_settings['enable_ar'] == 'on') { |
| 184 |
$attachments = isset($attachments) ? $attachments : array(); |
| 185 |
$from_ar = isset($form_settings['from_ar']) ? trim($form_settings['from_ar']) : ''; |
| 186 |
$from_ar_name = isset($form_settings['from_ar_name']) && ($form_settings['from_ar_name'] != '') ? esc_html($form_settings['from_ar_name']) : esc_html__('No Name', 'hash-form'); |
| 187 |
$email_subject = isset($form_settings['email_subject_ar']) && ($form_settings['email_subject_ar'] != '') ? esc_html(apply_filters('hashform_translate_string', $form_settings['email_subject_ar'], 'Hash Form', $form_title . ' - ' . 'Email Auto Responder Subject')) : esc_html__('New Form Submission', 'hash-form'); |
| 188 |
$email_message = wpautop(isset($form_settings['email_message_ar']) ? esc_html(apply_filters('hashform_translate_string', $form_settings['email_message_ar'], 'Hash Form', $form_title . ' - ' . 'Email Auto Responder Message')) : ''); |
| 189 |
$settings = HashFormSettings::get_settings(); |
| 190 |
$header_image = $settings['header_image']; |
| 191 |
|
| 192 |
ob_start(); |
| 193 |
include(HASHFORM_PATH . 'admin/settings/email-templates/template1.php'); |
| 194 |
$form_html = ob_get_clean(); |
| 195 |
|
| 196 |
$from_ar = ($from_ar == '[admin_email]') ? get_option('admin_email') : esc_attr($from_ar); |
| 197 |
|
| 198 |
$head = array(); |
| 199 |
$head[] = 'Content-Type: text/html; charset=UTF-8'; |
| 200 |
$head[] = 'From: ' . esc_html($from_ar_name) . ' <' . esc_html($from_ar) . '>'; |
| 201 |
wp_mail($reply_to_ar, $email_subject, $form_html, $head, $attachments); |
| 202 |
} |
| 203 |
$redirect_url = ''; |
| 204 |
|
| 205 |
if ($form_settings['confirmation_type'] == 'show_page') { |
| 206 |
$redirect_url = get_permalink($form_settings['show_page_id']); |
| 207 |
} else if ($form_settings['confirmation_type'] == 'redirect_url') { |
| 208 |
$redirect_url = $form_settings['redirect_url_page']; |
| 209 |
} |
| 210 |
|
| 211 |
// A replay (resend from the admin, or a deferred payment email) |
| 212 |
// must not run the post submission actions again: those dispatch |
| 213 |
// payments and third party integrations, so repeating them would |
| 214 |
// charge the customer a second time. There is also no ajax caller |
| 215 |
// waiting for a json response. |
| 216 |
if (self::$sending_deferred) { |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
do_action('hashform_after_email', array( |
| 221 |
'form' => $this->form, |
| 222 |
'entry_id' => $this->entry_id, |
| 223 |
'form_settings' => $form_settings, |
| 224 |
'metas' => $metas, |
| 225 |
'location' => $this->location |
| 226 |
)); |
| 227 |
|
| 228 |
if (!empty($redirect_url)) { |
| 229 |
return wp_send_json(array( |
| 230 |
'status' => 'redirect', |
| 231 |
'message' => esc_url($redirect_url) |
| 232 |
)); |
| 233 |
} |
| 234 |
|
| 235 |
return wp_send_json(array( |
| 236 |
'status' => 'success', |
| 237 |
'message' => esc_html(apply_filters('hashform_translate_string', $form_settings['confirmation_message'], 'Hash Form', $form_title . ' - ' . 'Confirmation Message')) |
| 238 |
)); |
| 239 |
} else { |
| 240 |
return false; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public static function template1($title, $entry_value, $count) { |
| 245 |
ob_start(); |
| 246 |
?> |
| 247 |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; margin-bottom: 25px"> |
| 248 |
<tbody> |
| 249 |
<tr> |
| 250 |
<th style="font-family: sans-serif; font-size: 14px; vertical-align: top;text-align:left; line-height: 18px;" valign="top"><?php echo esc_html($title); ?></th> |
| 251 |
</tr> |
| 252 |
<tr> |
| 253 |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding: 10px 0 0 0; line-height: 18px;" valign="top"><?php echo wp_kses_post($entry_value); ?></td> |
| 254 |
</tr> |
| 255 |
</tbody> |
| 256 |
</table> |
| 257 |
<?php |
| 258 |
$form_html = ob_get_clean(); |
| 259 |
return $form_html; |
| 260 |
} |
| 261 |
|
| 262 |
public static function template2($title, $entry_value, $count) { |
| 263 |
ob_start(); |
| 264 |
$border_style = ''; |
| 265 |
|
| 266 |
if ($count == 1) { |
| 267 |
$border_style = 'border-top: 5px solid #4183D7;'; |
| 268 |
} |
| 269 |
|
| 270 |
if ($count % 2 == 0) { |
| 271 |
$style = 'background: #f9f9ff;'; |
| 272 |
} else { |
| 273 |
$style = 'background: #FFFFFF;'; |
| 274 |
} |
| 275 |
?> |
| 276 |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;padding: 25px; <?php echo esc_attr($border_style . $style); ?>"> |
| 277 |
<tbody> |
| 278 |
<tr> |
| 279 |
<th style="font-family: sans-serif; font-size: 14px; vertical-align: top;text-align:left; line-height: 18px;" valign="top"><?php echo esc_html($title); ?></th> |
| 280 |
</tr> |
| 281 |
<tr> |
| 282 |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding: 10px 0 0 0; line-height: 18px;" valign="top"><?php echo wp_kses_post($entry_value); ?></td> |
| 283 |
</tr> |
| 284 |
</tbody> |
| 285 |
</table> |
| 286 |
<?php |
| 287 |
$form_html = ob_get_clean(); |
| 288 |
return $form_html; |
| 289 |
} |
| 290 |
|
| 291 |
public static function template3($title, $entry_value, $count) { |
| 292 |
ob_start(); |
| 293 |
?> |
| 294 |
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background:#FFF; margin-bottom: 25px"> |
| 295 |
<tbody> |
| 296 |
<tr> |
| 297 |
<th style="font-family: sans-serif; font-size: 14px; vertical-align: top;text-align:left; line-height: 18px;background: #000;color: #FFF;text-transform: uppercase;padding: 10px 20px;" valign="top"><?php echo esc_html($title); ?></th> |
| 298 |
</tr> |
| 299 |
<tr> |
| 300 |
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top; padding: 10px 0 0 0; line-height: 18px;padding: 20px !important;" valign="top"><?php echo wp_kses_post($entry_value); ?></td> |
| 301 |
</tr> |
| 302 |
</tbody> |
| 303 |
</table> |
| 304 |
<?php |
| 305 |
$form_html = ob_get_clean(); |
| 306 |
return $form_html; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
} |
| 311 |
|