Helpers
11 months ago
Integrations
1 week ago
RestApi
2 weeks ago
abilities
2 weeks ago
abstracts
2 weeks ago
admin
2 weeks ago
blocks
1 year ago
elementor
2 years ago
export
2 months ago
fields
2 weeks ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
1 year ago
shortcodes
2 weeks ago
stats
5 months ago
templates
3 months ago
traits
2 weeks ago
class-everest-forms.php
1 week ago
class-evf-addon-upsell.php
2 weeks ago
class-evf-ajax.php
2 weeks ago
class-evf-autoloader.php
7 years ago
class-evf-background-process-import-entries.php
1 year ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
2 months ago
class-evf-cron.php
2 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-email-entries-report.php
3 months ago
class-evf-emails.php
2 weeks ago
class-evf-fields.php
2 weeks ago
class-evf-form-handler.php
2 weeks ago
class-evf-form-task.php
2 weeks ago
class-evf-forms-features.php
2 weeks ago
class-evf-frontend-scripts.php
2 weeks ago
class-evf-install.php
2 months ago
class-evf-integrations.php
3 months ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
1 year ago
class-evf-privacy.php
6 years ago
class-evf-report-cron.php
2 months ago
class-evf-reporting.php
2 months ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
1 year ago
class-evf-smart-tags.php
9 months ago
class-evf-template-loader.php
1 year ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 weeks ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
4 months ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-emails.php
683 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class handles all (notification) emails sent by Everest Forms. |
| 4 | * |
| 5 | * Heavily influenced by the great AffiliateWP plugin by Pippin Williamson. |
| 6 | * https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/emails/class-affwp-emails.php |
| 7 | * |
| 8 | * @package EverestForms\Classes\Emails |
| 9 | * @version 1.2.0 |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Email class. |
| 17 | */ |
| 18 | class EVF_Emails { |
| 19 | |
| 20 | /** |
| 21 | * Holds the from address. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | private $from_address; |
| 26 | |
| 27 | /** |
| 28 | * Holds the from name. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $from_name; |
| 33 | |
| 34 | /** |
| 35 | * Holds the reply-to address. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | private $reply_to = false; |
| 40 | |
| 41 | /** |
| 42 | * Holds the carbon copy addresses. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | private $cc = false; |
| 47 | |
| 48 | /** |
| 49 | * Holds the blind carbon copy addresses. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | private $bcc = false; |
| 54 | |
| 55 | /** |
| 56 | * Holds the email content type. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | private $content_type; |
| 61 | |
| 62 | /** |
| 63 | * Holds the email headers. |
| 64 | * |
| 65 | * @var string |
| 66 | */ |
| 67 | private $headers; |
| 68 | |
| 69 | /** |
| 70 | * Holds the email attachments. |
| 71 | * |
| 72 | * @var string |
| 73 | */ |
| 74 | public $attachments = ''; |
| 75 | |
| 76 | /** |
| 77 | * Whether to send email in HTML. |
| 78 | * |
| 79 | * @var bool |
| 80 | */ |
| 81 | private $html = true; |
| 82 | |
| 83 | /** |
| 84 | * The email template to use. |
| 85 | * |
| 86 | * @var string |
| 87 | */ |
| 88 | private $template; |
| 89 | |
| 90 | /** |
| 91 | * Form data. |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | public $form_data = array(); |
| 96 | |
| 97 | /** |
| 98 | * Fields, formatted, and sanitized. |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | public $fields = array(); |
| 103 | |
| 104 | /** |
| 105 | * Entry ID. |
| 106 | * |
| 107 | * @var int |
| 108 | */ |
| 109 | public $entry_id = ''; |
| 110 | |
| 111 | /** |
| 112 | * Constructor. |
| 113 | */ |
| 114 | public function __construct() { |
| 115 | if ( 'none' === $this->get_template() ) { |
| 116 | $this->html = false; |
| 117 | } |
| 118 | |
| 119 | // Hooks. |
| 120 | add_action( 'everest_forms_email_send_before', array( $this, 'send_before' ) ); |
| 121 | add_action( 'everest_forms_email_send_after', array( $this, 'send_after' ) ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set a property. |
| 126 | * |
| 127 | * @param string $key Object property key. |
| 128 | * @param mixed $value Object property value. |
| 129 | */ |
| 130 | public function __set( $key, $value ) { |
| 131 | $this->$key = $value; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the email from name. |
| 136 | * |
| 137 | * @return string The email from name. |
| 138 | */ |
| 139 | public function get_from_name() { |
| 140 | if ( ! empty( $this->from_name ) ) { |
| 141 | $this->from_name = $this->process_tag( $this->from_name ); |
| 142 | } else { |
| 143 | $this->from_name = get_bloginfo( 'name' ); |
| 144 | } |
| 145 | |
| 146 | return apply_filters( 'everest_forms_email_from_name', wp_specialchars_decode( $this->from_name ), $this ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get the email from address. |
| 151 | * |
| 152 | * @return string The email from address. |
| 153 | */ |
| 154 | public function get_from_address() { |
| 155 | if ( ! empty( $this->from_address ) ) { |
| 156 | $this->from_address = $this->process_tag( $this->from_address ); |
| 157 | } else { |
| 158 | $this->from_address = get_option( 'admin_email' ); |
| 159 | } |
| 160 | |
| 161 | return apply_filters( 'everest_forms_email_from_address', $this->from_address, $this ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get the email reply-to. |
| 166 | * |
| 167 | * @return string The email reply-to address. |
| 168 | */ |
| 169 | public function get_reply_to() { |
| 170 | if ( ! empty( $this->reply_to ) ) { |
| 171 | $this->reply_to = $this->process_tag( $this->reply_to ); |
| 172 | |
| 173 | if ( ! is_email( $this->reply_to ) ) { |
| 174 | $this->reply_to = false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return apply_filters( 'everest_forms_email_reply_to', $this->reply_to, $this ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the email carbon copy addresses. |
| 183 | * |
| 184 | * @return string The email carbon copy addresses. |
| 185 | */ |
| 186 | public function get_cc() { |
| 187 | if ( ! empty( $this->cc ) ) { |
| 188 | $this->cc = $this->process_tag( $this->cc ); |
| 189 | $addresses = array_map( 'trim', explode( ',', $this->cc ) ); |
| 190 | |
| 191 | foreach ( $addresses as $key => $address ) { |
| 192 | if ( ! is_email( $address ) ) { |
| 193 | unset( $addresses[ $key ] ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | $this->cc = implode( ',', $addresses ); |
| 198 | } |
| 199 | |
| 200 | return apply_filters( 'everest_forms_email_cc', $this->cc, $this ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get the email blind carbon copy addresses. |
| 205 | * |
| 206 | * @return string The email blind carbon copy addresses. |
| 207 | */ |
| 208 | public function get_bcc() { |
| 209 | if ( ! empty( $this->bcc ) ) { |
| 210 | $this->bcc = $this->process_tag( $this->bcc ); |
| 211 | $addresses = array_map( 'trim', explode( ',', $this->bcc ) ); |
| 212 | |
| 213 | foreach ( $addresses as $key => $address ) { |
| 214 | if ( ! is_email( $address ) ) { |
| 215 | unset( $addresses[ $key ] ); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | $this->bcc = implode( ',', $addresses ); |
| 220 | } |
| 221 | |
| 222 | return apply_filters( 'everest_forms_email_bcc', $this->bcc, $this ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the email content type. |
| 227 | * |
| 228 | * @return string The email content type. |
| 229 | */ |
| 230 | public function get_content_type() { |
| 231 | if ( ! $this->content_type && $this->html ) { |
| 232 | $this->content_type = apply_filters( 'everest_forms_email_default_content_type', 'text/html', $this ); |
| 233 | } elseif ( ! $this->html ) { |
| 234 | $this->content_type = 'text/plain'; |
| 235 | } |
| 236 | |
| 237 | return apply_filters( 'everest_forms_email_content_type', $this->content_type, $this ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get the email headers. |
| 242 | * |
| 243 | * @return string The email headers. |
| 244 | */ |
| 245 | public function get_headers() { |
| 246 | if ( ! $this->headers ) { |
| 247 | $this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n"; |
| 248 | if ( $this->get_reply_to() ) { |
| 249 | $this->headers .= "Reply-To: {$this->get_reply_to()}\r\n"; |
| 250 | } |
| 251 | if ( $this->get_cc() ) { |
| 252 | $this->headers .= "Cc: {$this->get_cc()}\r\n"; |
| 253 | } |
| 254 | if ( $this->get_bcc() ) { |
| 255 | $this->headers .= "Bcc: {$this->get_bcc()}\r\n"; |
| 256 | } |
| 257 | $this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n"; |
| 258 | } |
| 259 | |
| 260 | return apply_filters( 'everest_forms_email_headers', $this->headers, $this ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Build the email. |
| 265 | * |
| 266 | * @param string $message The email message. |
| 267 | * @return string |
| 268 | */ |
| 269 | public function build_email( $message ) { |
| 270 | if ( false === $this->html ) { |
| 271 | $message = $this->process_tag( $message, false, true ); |
| 272 | $message = str_replace( '{all_fields}', $this->everest_forms_html_field_value( false ), $message ); |
| 273 | |
| 274 | return apply_filters( 'everest_forms_email_message', $message, $this ); |
| 275 | } |
| 276 | |
| 277 | ob_start(); |
| 278 | |
| 279 | evf_get_template( 'emails/header-' . $this->get_template() . '.php' ); |
| 280 | |
| 281 | // Hooks into the email header. |
| 282 | do_action( 'everest_forms_email_header', $this ); |
| 283 | |
| 284 | evf_get_template( 'emails/body-' . $this->get_template() . '.php' ); |
| 285 | |
| 286 | // Hooks into the email body. |
| 287 | do_action( 'everest_forms_email_body', $this ); |
| 288 | |
| 289 | evf_get_template( 'emails/footer-' . $this->get_template() . '.php' ); |
| 290 | |
| 291 | // Hooks into the email footer. |
| 292 | do_action( 'everest_forms_email_footer', $this ); |
| 293 | |
| 294 | $message = $this->process_tag( $message, false ); |
| 295 | |
| 296 | /** |
| 297 | * Filter to modify the email message format before line break conversion. |
| 298 | * |
| 299 | * @since xx.xx.xx |
| 300 | */ |
| 301 | $message = apply_filters( 'everest_forms_email_message_pre_format', $message, $this ); |
| 302 | |
| 303 | $message = nl2br( $message ); |
| 304 | |
| 305 | $body = ob_get_clean(); |
| 306 | $message = str_replace( '{email}', $message, $body ); |
| 307 | $message = str_replace( '{all_fields}', $this->everest_forms_html_field_value( true ), $message ); |
| 308 | $message = make_clickable( $message ); |
| 309 | |
| 310 | return apply_filters( 'everest_forms_email_message', $message, $this ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Send the email. |
| 315 | * |
| 316 | * @param string $to The To address. |
| 317 | * @param string $subject The subject line of the email. |
| 318 | * @param string $message The body of the email. |
| 319 | * @param array $attachments Attachments to the email. |
| 320 | * @param string $connection_id Connection ID of the email. |
| 321 | * |
| 322 | * @return bool |
| 323 | */ |
| 324 | public function send( $to, $subject, $message, $attachments = '', $connection_id = '' ) { |
| 325 | if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) { |
| 326 | evf_doing_it_wrong( __FUNCTION__, __( 'You cannot send emails with EVF_Emails until init/admin_init has been reached', 'everest-forms' ), null ); |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Don't send anything if emails have been disabled. |
| 331 | if ( $this->is_email_disabled() ) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // Don't send if email address is invalid. |
| 336 | if ( ! is_email( $to ) ) { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | // Hooks before email is sent. |
| 341 | do_action( 'everest_forms_email_send_before', $this ); |
| 342 | |
| 343 | $message = apply_filters( 'everest_forms_entry_email__message', str_replace( '{entry_id}', absint( $this->entry_id ), $message ), $this ); |
| 344 | |
| 345 | // Email Template Enabled or not checked. |
| 346 | $email_template_included = ! empty( $this->form_data['settings']['email'][ $connection_id ]['choose_template'] ) ? true : false; |
| 347 | $save_and_continue_email_template_included = ! empty( $this->form_data['settings']['email']['connection_save_and_continue']['choose_template'] ) ? true : false; |
| 348 | $save_and_continue_enabled = ! empty( $this->form_data['settings']['email']['connection_save_and_continue']['enable_email_notification'] ) ? true : false; |
| 349 | |
| 350 | if ( $email_template_included && true === $this->html ) { |
| 351 | $message = apply_filters( 'everest_forms_email_template_message', $message, $this, $connection_id ); |
| 352 | } elseif ( ( $save_and_continue_email_template_included && true === $this->html ) && ( true === $save_and_continue_enabled ) ) { |
| 353 | $message = apply_filters( 'everest_forms_email_template_message', $message, $this, 'connection_save_and_continue' ); |
| 354 | } else { |
| 355 | $message = $this->build_email( $message ); |
| 356 | } |
| 357 | $this->attachments = apply_filters( 'everest_forms_email_attachments', $this->attachments, $this ); |
| 358 | $subject = evf_decode_string( $this->process_tag( $subject ) ); |
| 359 | |
| 360 | // Capture runtime mail transport only during first form submission email send. |
| 361 | $mailer_transport = ''; |
| 362 | $capture_mailer = static function ( $phpmailer ) use ( &$mailer_transport ) { |
| 363 | $mailer_transport = strtolower( (string) $phpmailer->Mailer ); |
| 364 | }; |
| 365 | $should_capture = ! get_option( 'everest_forms_first_form_smtp_checked', false ); |
| 366 | |
| 367 | if ( $should_capture ) { |
| 368 | add_action( 'phpmailer_init', $capture_mailer, 999 ); |
| 369 | } |
| 370 | |
| 371 | // Let's do this. |
| 372 | $sent = wp_mail( $to, $subject, $message, $this->get_headers(), $this->attachments ); |
| 373 | |
| 374 | if ( $should_capture ) { |
| 375 | remove_action( 'phpmailer_init', $capture_mailer, 999 ); |
| 376 | update_option( 'everest_forms_runtime_smtp_active', 'smtp' === $mailer_transport ); |
| 377 | update_option( 'everest_forms_first_form_smtp_checked', true ); |
| 378 | } |
| 379 | |
| 380 | if ( ! $sent ) { |
| 381 | $error_message = apply_filters( 'everest_forms_email_send_failed_message', '' ); |
| 382 | $failed_data = get_transient( 'everest_forms_mail_send_failed_count' ); |
| 383 | $failed_count = $failed_data && isset( $failed_data['failed_count'] ) ? $failed_data['failed_count'] : 0; |
| 384 | ++$failed_count; |
| 385 | set_transient( |
| 386 | 'everest_forms_mail_send_failed_count', |
| 387 | array( |
| 388 | 'failed_count' => $failed_count, |
| 389 | 'error_message' => $error_message, |
| 390 | ) |
| 391 | ); |
| 392 | } |
| 393 | // Hooks after the email is sent. |
| 394 | do_action( 'everest_forms_email_send_after', $this ); |
| 395 | |
| 396 | update_option( 'everest_forms_last_form_email_status', $sent ? 'success' : 'failed' ); |
| 397 | |
| 398 | return $sent; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Add filters/actions before the email is sent. |
| 403 | */ |
| 404 | public function send_before() { |
| 405 | add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 406 | add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 407 | add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Remove filters/actions after the email is sent. |
| 412 | */ |
| 413 | public function send_after() { |
| 414 | remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 415 | remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 416 | remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Converts text formatted HTML. This is primarily for turning line breaks |
| 421 | * into <p> and <br/> tags. |
| 422 | * |
| 423 | * @param string $message Text to convert. |
| 424 | * @return string |
| 425 | */ |
| 426 | public function text_to_html( $message ) { |
| 427 | if ( 'text/html' === $this->content_type || true === $this->html ) { |
| 428 | $message = wpautop( $message ); |
| 429 | } |
| 430 | |
| 431 | return $message; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Processes a smart tag. |
| 436 | * |
| 437 | * @param string $string String that may contain tags. |
| 438 | * @param bool $sanitize Toggle to maybe sanitize. |
| 439 | * @param bool $linebreaks Toggle to process linebreaks. |
| 440 | * |
| 441 | * @return string |
| 442 | */ |
| 443 | public function process_tag( $string = '', $sanitize = true, $linebreaks = false ) { |
| 444 | $tag = apply_filters( 'everest_forms_process_smart_tags', $string, $this->form_data, $this->fields, $this->entry_id ); |
| 445 | $tag = evf_decode_string( $tag ); |
| 446 | |
| 447 | if ( $sanitize ) { |
| 448 | if ( $linebreaks ) { |
| 449 | $tag = evf_sanitize_textarea_field( $tag ); |
| 450 | } else { |
| 451 | $tag = sanitize_text_field( $tag ); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return $tag; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Process the all fields smart tag if present. |
| 460 | * |
| 461 | * @param bool $html Toggle to use HTML or plaintext. |
| 462 | * @return string |
| 463 | */ |
| 464 | public function everest_forms_html_field_value( $html = true ) { |
| 465 | if ( empty( $this->fields ) ) { |
| 466 | return ''; |
| 467 | } |
| 468 | |
| 469 | // Make sure we have an entry id. |
| 470 | if ( ! empty( $this->entry_id ) ) { |
| 471 | $this->form_data['entry_id'] = (int) $this->entry_id; |
| 472 | } |
| 473 | |
| 474 | $message = ''; |
| 475 | |
| 476 | if ( $html ) { |
| 477 | |
| 478 | /* |
| 479 | * HTML emails. |
| 480 | */ |
| 481 | ob_start(); |
| 482 | |
| 483 | // Hooks into the email field. |
| 484 | do_action( 'everest_forms_email_field', $this ); |
| 485 | |
| 486 | evf_get_template( 'emails/field-' . $this->get_template() . '.php' ); |
| 487 | |
| 488 | $field_template = ob_get_clean(); |
| 489 | $empty_message = '<em>' . __( '(empty)', 'everest-forms' ) . '</em>'; |
| 490 | |
| 491 | $field_iterator = 1; |
| 492 | foreach ( $this->fields as $meta_id => $field ) { |
| 493 | |
| 494 | if ( isset( $this->form_data['settings']['disabled_entries'] ) && '1' === $this->form_data['settings']['disabled_entries'] ) { |
| 495 | $types_to_remove = array( 'image-upload', 'file-upload', 'signature' ); |
| 496 | if ( isset( $field['type'] ) && in_array( $field['type'], $types_to_remove, true ) ) { |
| 497 | continue; |
| 498 | } |
| 499 | } |
| 500 | if ( |
| 501 | ! apply_filters( 'everest_forms_email_display_empty_fields', false ) && |
| 502 | ( empty( $field['value'] ) && '0' !== $field['value'] ) |
| 503 | ) { |
| 504 | continue; |
| 505 | } |
| 506 | |
| 507 | // If empty value is provided for select field, don't send email. |
| 508 | if ( 'select' === $field['type'] && empty( $field['value'][0] ) ) { |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | if ( ( 'radio' === $field['type'] && empty( $field['value']['label'] ) ) || ( 'payment-multiple' === $field['type'] && empty( $field['value']['label'] ) ) ) { |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | if ( ( 'checkbox' === $field['type'] && empty( $field['value']['label'][0] ) ) || ( 'payment-checkbox' === $field['type'] && empty( $field['value']['label'] ) ) ) { |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | // If there's the export data filter, utilize that and re-loop promptly. |
| 521 | if ( has_filter( "everest_forms_field_exporter_{$field['type']}" ) ) { |
| 522 | $formatted_string = apply_filters( "everest_forms_field_exporter_{$field['type']}", $field, 'email-html', 2 ); |
| 523 | $formatted_string['value'] = false === $formatted_string['value'] ? $empty_message : $formatted_string['value']; |
| 524 | |
| 525 | $field_item = $field_template; |
| 526 | if ( 1 === $field_iterator ) { |
| 527 | $field_item = str_replace( 'border-top:1px solid #dddddd;', '', $field_item ); |
| 528 | } |
| 529 | |
| 530 | // Inject the label and value into the email template. |
| 531 | $field_item = str_replace( '{field_name}', $formatted_string['label'], $field_item ); |
| 532 | $field_item = str_replace( '{field_value}', $formatted_string['value'], $field_item ); |
| 533 | |
| 534 | $message .= wpautop( $field_item ); |
| 535 | |
| 536 | // For BW compatibility reasons. |
| 537 | ++$field_iterator; |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | $field_val = empty( $field['value'] ) && '0' !== $field['value'] ? $empty_message : $field['value']; |
| 542 | $field_name = isset( $field_val['name'] ) ? $field_val['name'] : $field['name']; |
| 543 | $field_label = ! empty( $field_val['label'] ) ? $field_val['label'] : $field_val; |
| 544 | $field_type = $field['type']; |
| 545 | |
| 546 | // If empty label is provided for choice field, don't store their data nor send email. |
| 547 | if ( in_array( $field_type, array( 'radio', 'payment-multiple' ), true ) ) { |
| 548 | if ( isset( $field_val['label'] ) && '' === $field_val['label'] ) { |
| 549 | continue; |
| 550 | } |
| 551 | } elseif ( in_array( $field_type, array( 'checkbox', 'payment-checkbox' ), true ) ) { |
| 552 | if ( isset( $field_val['label'] ) && ( empty( $field_val['label'] ) || '' === current( $field_val['label'] ) ) ) { |
| 553 | continue; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if ( isset( $field['value'], $field['value_raw'] ) && is_string( $field['value'] ) && in_array( $field_type, array( 'image-upload', 'file-upload' ), true ) ) { |
| 558 | $field['value'] = $field; |
| 559 | } |
| 560 | |
| 561 | if ( isset( $field_val['type'] ) && in_array( $field['type'], array( 'image-upload', 'file-upload', 'rating' ), true ) ) { |
| 562 | if ( 'rating' === $field_val['type'] ) { |
| 563 | $value = ! empty( $field_val['value'] ) ? $field_val['value'] : 0; |
| 564 | $number_of_stars = ! empty( $field_val['number_of_rating'] ) ? $field_val['number_of_rating'] : 5; |
| 565 | $field_val = $value . '/' . $number_of_stars; |
| 566 | } else { |
| 567 | $field_val = empty( $field_val['file_url'] ) ? $empty_message : $field_val; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if ( 'rating' !== $field_type ) { |
| 572 | if ( is_array( $field_label ) ) { |
| 573 | $field_html = array(); |
| 574 | foreach ( $field_label as $meta_val ) { |
| 575 | $field_html[] = esc_html( $meta_val ); |
| 576 | } |
| 577 | $field_val = implode( ', ', $field_html ); |
| 578 | } else { |
| 579 | $field_val = esc_html( $field_label ); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | if ( empty( $field_name ) ) { |
| 584 | $field_name = sprintf( |
| 585 | /* translators: %d - field ID. */ |
| 586 | esc_html__( 'Field ID #%d', 'everest-forms' ), |
| 587 | absint( $field['id'] ) |
| 588 | ); |
| 589 | } |
| 590 | |
| 591 | $field_item = $field_template; |
| 592 | if ( 1 === $field_iterator ) { |
| 593 | $field_item = str_replace( 'border-top:1px solid #dddddd;', '', $field_item ); |
| 594 | } |
| 595 | |
| 596 | $field_item = str_replace( '{field_name}', $field_name, $field_item ); |
| 597 | $field_value = apply_filters( 'everest_forms_html_field_value', evf_decode_string( $field_val ), $field['value'], $this->form_data, 'email-html', $field ); |
| 598 | $field_item = str_replace( '{field_value}', $field_value, $field_item ); |
| 599 | |
| 600 | $message .= wpautop( $field_item ); |
| 601 | ++$field_iterator; |
| 602 | } |
| 603 | } else { |
| 604 | /* |
| 605 | * Plain Text emails. |
| 606 | */ |
| 607 | foreach ( $this->fields as $field ) { |
| 608 | |
| 609 | if ( isset( $this->form_data['settings']['disabled_entries'] ) && '1' === $this->form_data['settings']['disabled_entries'] ) { |
| 610 | $types_to_remove = array( 'image-upload', 'file-upload', 'signature' ); |
| 611 | if ( isset( $field['type'] ) && in_array( $field['type'], $types_to_remove, true ) ) { |
| 612 | continue; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if ( ! apply_filters( 'everest_forms_email_display_empty_fields', false ) && ( empty( $field['value'] ) && '0' !== $field['value'] ) ) { |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | $field_val = empty( $field['value'] ) && '0' !== $field['value'] ? esc_html__( '(empty)', 'everest-forms' ) : $field['value']; |
| 621 | $field_name = isset( $field['name'] ) ? $field['name'] : ''; |
| 622 | |
| 623 | if ( is_array( $field_val ) ) { |
| 624 | $field_html = array(); |
| 625 | |
| 626 | foreach ( $field_val as $meta_val ) { |
| 627 | $field_html[] = $meta_val; |
| 628 | } |
| 629 | |
| 630 | if ( ! empty( $field_html ) && is_array( $field_html ) ) { |
| 631 | $field_val = implode( ', ', $field_html ); |
| 632 | } else { |
| 633 | $field_val = ''; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if ( empty( $field_name ) ) { |
| 638 | $field_name = sprintf( |
| 639 | /* translators: %d - field ID. */ |
| 640 | esc_html__( 'Field ID #%d', 'everest-forms' ), |
| 641 | absint( $field['id'] ) |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | $message .= '--- ' . evf_decode_string( $field_name ) . " ---\r\n\r\n"; |
| 646 | $field_value = evf_decode_string( $field_val ) . "\r\n\r\n"; |
| 647 | $message .= apply_filters( 'everest_forms_plaintext_field_value', $field_value, $field['value'], $this->form_data, 'email-plain' ); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if ( empty( $message ) ) { |
| 652 | $empty_message = esc_html__( 'An empty form was submitted.', 'everest-forms' ); |
| 653 | $message = $html ? wpautop( $empty_message ) : $empty_message; |
| 654 | } |
| 655 | |
| 656 | return $message; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Email kill switch if needed. |
| 661 | * |
| 662 | * @return bool |
| 663 | */ |
| 664 | public function is_email_disabled() { |
| 665 | return (bool) apply_filters( 'everest_forms_disable_all_emails', false, $this ); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Get the enabled email template. |
| 670 | * |
| 671 | * @todo Email template. |
| 672 | * |
| 673 | * @return string When filtering return 'none' to switch to text/plain email. |
| 674 | */ |
| 675 | public function get_template() { |
| 676 | if ( ! $this->template ) { |
| 677 | $this->template = get_option( 'everest_forms_email_template', 'default' ); |
| 678 | } |
| 679 | |
| 680 | return apply_filters( 'everest_forms_email_template', $this->template ); |
| 681 | } |
| 682 | } |
| 683 |