| 1 |
<?php |
| 2 |
|
| 3 |
class AF_Core_Emails { |
| 4 |
|
| 5 |
function __construct() { |
| 6 |
|
| 7 |
add_action( 'af/form/submission', array( $this, 'send_form_emails' ), 15, 3 ); |
| 8 |
add_action( 'af/emails/send_form_email', array( $this, 'send_single_form_email' ), 10, 4 ); |
| 9 |
|
| 10 |
add_filter( 'af/form/valid_form', array( $this, 'valid_form' ), 10, 1 ); |
| 11 |
add_filter( 'af/form/from_post', array( $this, 'form_from_post' ), 10, 2 ); |
| 12 |
add_action( 'af/form/to_post', array( $this, 'form_to_post' ), 10, 2 ); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Add the email field to the default valid form |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* |
| 21 |
*/ |
| 22 |
function valid_form( $form ) { |
| 23 |
|
| 24 |
$form['emails'] = false; |
| 25 |
|
| 26 |
return $form; |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Add any email settings to form object for forms loaded from posts |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* |
| 36 |
*/ |
| 37 |
function form_from_post( $form, $post ) { |
| 38 |
|
| 39 |
$emails = get_field( 'field_form_emails', $post->ID ); |
| 40 |
|
| 41 |
if ( $emails && ! empty( $emails ) ) { |
| 42 |
|
| 43 |
$form['emails'] = $emails; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
return $form; |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function form_to_post( $form, $post ) { |
| 53 |
if ( $form['emails'] ) { |
| 54 |
update_field( 'field_form_emails', $form['emails'], $post->ID ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Send form emails after submission |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
*/ |
| 65 |
function send_form_emails( $form, $fields, $args ) { |
| 66 |
|
| 67 |
$emails = $form['emails']; |
| 68 |
|
| 69 |
if ( $emails && ! empty( $emails ) ) { |
| 70 |
|
| 71 |
foreach ( $emails as $email ) { |
| 72 |
|
| 73 |
do_action( 'af/emails/send_form_email', $email, $form, $fields, $args ); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Send single form email |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* |
| 87 |
*/ |
| 88 |
function send_single_form_email( $email, $form, $fields, $args ) { |
| 89 |
// Bail if this email is deactivated |
| 90 |
if ( ! $email['active'] ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Recipient |
| 95 |
$recipient = ''; |
| 96 |
if ( 'field' == $email['recipient_type'] ) { |
| 97 |
foreach( $fields as $field ) { |
| 98 |
if ( $field['key'] == $email['recipient_field'] ) { |
| 99 |
$recipient = $field['_input']; |
| 100 |
} |
| 101 |
} |
| 102 |
} elseif ( 'custom' == $email['recipient_type'] ) { |
| 103 |
$recipient = $email['recipient_custom']; |
| 104 |
} |
| 105 |
|
| 106 |
$recipient = apply_filters( 'af/form/email/recipient', $recipient, $email, $form, $fields ); |
| 107 |
$recipient = apply_filters( 'af/form/email/recipient/id=' . $form['post_id'], $recipient, $email, $form, $fields ); |
| 108 |
$recipient = apply_filters( 'af/form/email/recipient/key=' . $form['key'], $recipient, $email, $form, $fields ); |
| 109 |
|
| 110 |
// Bail early if there are no recipients |
| 111 |
// This allows emails to be stopped by returning false from the filter |
| 112 |
if ( $recipient === false ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
// Subject line |
| 117 |
$subject = $email['subject']; |
| 118 |
$subject = apply_filters( 'af/form/email/subject', $subject, $email, $form, $fields ); |
| 119 |
$subject = apply_filters( 'af/form/email/subject/id=' . $form['post_id'], $subject, $email, $form, $fields ); |
| 120 |
$subject = apply_filters( 'af/form/email/subject/key=' . $form['key'], $subject, $email, $form, $fields ); |
| 121 |
|
| 122 |
$subject = af_resolve_merge_tags( $subject, $fields ); |
| 123 |
|
| 124 |
|
| 125 |
// Email contents |
| 126 |
$content = apply_filters( 'the_content', $email['content'] ); |
| 127 |
$content = apply_filters( 'af/form/email/content', $content, $email, $form, $fields ); |
| 128 |
$content = apply_filters( 'af/form/email/content/id=' . $form['post_id'], $content, $email, $form, $fields ); |
| 129 |
$content = apply_filters( 'af/form/email/content/key=' . $form['key'], $content, $email, $form, $fields ); |
| 130 |
|
| 131 |
$content = af_resolve_merge_tags( $content, $fields ); |
| 132 |
|
| 133 |
// Add default HTML template |
| 134 |
$use_template = true; |
| 135 |
$use_template = apply_filters( 'af/form/email/use_template', $use_template, $email, $form, $args ); |
| 136 |
$use_template = apply_filters( 'af/form/email/use_template/id=' . $form['post_id'], $use_template, $email, $form, $args ); |
| 137 |
$use_template = apply_filters( 'af/form/email/use_template/key=' . $form['key'], $use_template, $email, $form, $args ); |
| 138 |
|
| 139 |
if ( $use_template ) { |
| 140 |
$content = sprintf( |
| 141 |
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 142 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 143 |
<head> |
| 144 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 145 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| 146 |
<title>%s</title> |
| 147 |
<style>%s</style> |
| 148 |
</head> |
| 149 |
<body> |
| 150 |
%s |
| 151 |
</body> |
| 152 |
</html>', |
| 153 |
$subject, |
| 154 |
$this->get_email_style( $email, $form ), |
| 155 |
$content |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
// Headers |
| 161 |
$headers = array(); |
| 162 |
|
| 163 |
// HTML content type |
| 164 |
$content_type = 'text/html; charset=UTF-8'; |
| 165 |
$content_type = apply_filters( 'af/form/email/content_type', $content_type, $email, $form, $args ); |
| 166 |
$content_type = apply_filters( 'af/form/email/content_type/id=' . $form['post_id'], $content_type, $email, $form, $args ); |
| 167 |
$content_type = apply_filters( 'af/form/email/content_type/key=' . $form['key'], $content_type, $email, $form, $args ); |
| 168 |
$headers[] = sprintf( 'Content-type: %s', $content_type ); |
| 169 |
|
| 170 |
// From header |
| 171 |
$from = af_resolve_merge_tags( $email['from'], $fields ); |
| 172 |
$headers[] = 'From:' . $from; |
| 173 |
|
| 174 |
$headers = apply_filters( 'af/form/email/headers', $headers, $email, $form, $fields ); |
| 175 |
$headers = apply_filters( 'af/form/email/headers/id=' . $form['post_id'], $headers, $email, $form, $fields ); |
| 176 |
$headers = apply_filters( 'af/form/email/headers/key=' . $form['key'], $headers, $email, $form, $fields ); |
| 177 |
|
| 178 |
|
| 179 |
// Attachments |
| 180 |
$attachments = array(); |
| 181 |
|
| 182 |
$attachments = apply_filters( 'af/form/email/attachments', $attachments, $email, $form, $fields ); |
| 183 |
$attachments = apply_filters( 'af/form/email/attachments/id=' . $form['post_id'], $attachments, $email, $form, $fields ); |
| 184 |
$attachments = apply_filters( 'af/form/email/attachments/key=' . $form['key'], $attachments, $email, $form, $fields ); |
| 185 |
|
| 186 |
|
| 187 |
do_action( 'af/email/before_send', $email, $form ); |
| 188 |
do_action( 'af/email/before_send/id=' . $form['post_id'], $email, $form ); |
| 189 |
do_action( 'af/email/before_send/key=' . $form['key'], $email, $form ); |
| 190 |
|
| 191 |
// Arrayify recipients |
| 192 |
if ( is_array( $recipient ) ) { |
| 193 |
$recipients = array_unique( $recipient ); |
| 194 |
} else { |
| 195 |
$recipients = array( af_resolve_merge_tags( $recipient, $fields ) ); |
| 196 |
} |
| 197 |
|
| 198 |
// Send separate emails to all recipients |
| 199 |
foreach ( $recipients as $recipient ) { |
| 200 |
wp_mail( $recipient, $subject, $content, $headers, $attachments ); |
| 201 |
} |
| 202 |
|
| 203 |
do_action( 'af/email/after_send', $email, $form ); |
| 204 |
do_action( 'af/email/after_send/id=' . $form['post_id'], $email, $form ); |
| 205 |
do_action( 'af/email/after_send/key=' . $form['key'], $email, $form ); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Returns styles for email |
| 212 |
* |
| 213 |
* @since 1.2.0 |
| 214 |
* |
| 215 |
*/ |
| 216 |
function get_email_style( $email, $form ) { |
| 217 |
|
| 218 |
ob_start(); |
| 219 |
?> |
| 220 |
|
| 221 |
body { |
| 222 |
font-family: sans-serif; |
| 223 |
} |
| 224 |
|
| 225 |
table { |
| 226 |
border-collapse: collapse; |
| 227 |
width: 100%; |
| 228 |
max-width: 500px; |
| 229 |
text-align: left; |
| 230 |
} |
| 231 |
|
| 232 |
th, |
| 233 |
td { |
| 234 |
border: 1px solid #ccc; |
| 235 |
} |
| 236 |
|
| 237 |
th { |
| 238 |
background-color: #fafafa; |
| 239 |
padding: 10px; |
| 240 |
} |
| 241 |
|
| 242 |
td { |
| 243 |
padding: 15px 20px; |
| 244 |
} |
| 245 |
|
| 246 |
.af-field-include-repeater td { |
| 247 |
padding: 10px; |
| 248 |
} |
| 249 |
|
| 250 |
<?php |
| 251 |
$styles = ob_get_clean(); |
| 252 |
|
| 253 |
$styles = apply_filters( 'af/form/email/styles', $styles, $email, $form ); |
| 254 |
$styles = apply_filters( 'af/form/email/styles/id=' . $form['post_id'], $styles, $email, $form ); |
| 255 |
$styles = apply_filters( 'af/form/email/styles/key=' . $form['key'], $styles, $email, $form ); |
| 256 |
|
| 257 |
return $styles; |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
return new AF_Core_Emails(); |
| 264 |
|