| 1 |
<?php |
| 2 |
|
| 3 |
namespace HTML_Forms\Actions; |
| 4 |
|
| 5 |
use HTML_Forms\Form; |
| 6 |
use HTML_Forms\Submission; |
| 7 |
|
| 8 |
class Email extends Action { |
| 9 |
|
| 10 |
public $type = 'email'; |
| 11 |
public $label = 'Send Email'; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
$this->label = __( 'Send Email', 'html-forms' ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
private function get_default_settings() { |
| 21 |
return array( |
| 22 |
'from' => get_option( 'admin_email' ), |
| 23 |
'to' => get_option( 'admin_email' ), |
| 24 |
'subject' => '', |
| 25 |
'message' => '', |
| 26 |
'headers' => '', |
| 27 |
'content_type' => 'text/html', |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param array $settings |
| 33 |
* @param string|int $index |
| 34 |
*/ |
| 35 |
public function page_settings( $settings, $index ) { |
| 36 |
$settings = array_merge( $this->get_default_settings(), $settings ); |
| 37 |
?> |
| 38 |
<span class="hf-action-summary"><?php printf( 'From %s. To %s.', esc_html($settings['from']), esc_html($settings['to']) ); ?></span> |
| 39 |
<input type="hidden" name="form[settings][actions][<?php echo esc_attr($index); ?>][type]" value="<?php echo $this->type; ?>" /> |
| 40 |
|
| 41 |
<p class="description"> |
| 42 |
<?php _e( 'Send out an email notification whenever this form is successfully submitted.', 'html-forms' ); ?> |
| 43 |
<a target="_blank" tabindex="-1" class="html-forms-help" href="https://htmlformsplugin.com/kb/sending-email-notifications/"><span class="dashicons dashicons-editor-help"></span></a> |
| 44 |
</p> |
| 45 |
|
| 46 |
<table class="form-table"> |
| 47 |
<tr> |
| 48 |
<th><label><?php echo __( 'From', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 49 |
<td> |
| 50 |
<input name="form[settings][actions][<?php echo esc_attr($index); ?>][from]" value="<?php echo esc_attr( $settings['from'] ); ?>" type="text" class="regular-text" placeholder="jane@email.com" required /> |
| 51 |
</td> |
| 52 |
</tr> |
| 53 |
<tr> |
| 54 |
<th><label><?php echo __( 'To', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 55 |
<td> |
| 56 |
<input name="form[settings][actions][<?php echo esc_attr($index); ?>][to]" value="<?php echo esc_attr( $settings['to'] ); ?>" type="text" class="regular-text" placeholder="john@email.com" required /> |
| 57 |
</td> |
| 58 |
</tr> |
| 59 |
<tr> |
| 60 |
<th><label><?php echo __( 'Subject', 'html-forms' ); ?></label></th> |
| 61 |
<td> |
| 62 |
<input name="form[settings][actions][<?php echo esc_attr($index); ?>][subject]" value="<?php echo esc_attr( $settings['subject'] ); ?>" type="text" class="regular-text" placeholder="<?php echo esc_attr( __( 'Your email subject', 'html-forms' ) ); ?>" /> |
| 63 |
</td> |
| 64 |
</tr> |
| 65 |
|
| 66 |
<tr> |
| 67 |
<th><label><?php echo __( 'Message', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 68 |
<td> |
| 69 |
<textarea name="form[settings][actions][<?php echo esc_attr($index); ?>][message]" rows="8" class="widefat" placeholder="<?php echo esc_attr( __( 'Your email message', 'html-forms' ) ); ?>" required><?php echo esc_textarea( $settings['message'] ); ?></textarea> |
| 70 |
<p class="help"><?php _e( 'You can use the following variables (in all fields): ', 'html-forms' ); ?><br /><span class="hf-field-names"></span></p> |
| 71 |
</td> |
| 72 |
</tr> |
| 73 |
|
| 74 |
<tr> |
| 75 |
<th><label><?php echo __( 'Content Type', 'html-forms' ); ?></label></th> |
| 76 |
<td> |
| 77 |
<select name="form[settings][actions][<?php echo esc_attr($index); ?>][content_type]" required> |
| 78 |
<option <?php selected( $settings['content_type'], 'text/plain' ); ?>>text/plain</option> |
| 79 |
<option <?php selected( $settings['content_type'], 'text/html' ); ?>>text/html</option> |
| 80 |
</select> |
| 81 |
</td> |
| 82 |
</tr> |
| 83 |
|
| 84 |
<tr> |
| 85 |
<th><label><?php echo __( 'Additional Headers', 'html-forms' ); ?></label></th> |
| 86 |
<td> |
| 87 |
<textarea name="form[settings][actions][<?php echo esc_attr($index); ?>][headers]" rows="4" class="widefat" placeholder="<?php echo esc_attr( 'Reply-To: [NAME] <[EMAIL]>' ); ?>"><?php echo esc_textarea( $settings['headers'] ); ?></textarea> |
| 88 |
</td> |
| 89 |
</tr> |
| 90 |
</table> |
| 91 |
<?php |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Processes this action |
| 96 |
* |
| 97 |
* @param array $settings |
| 98 |
* @param Submission $submission |
| 99 |
* @param Form $form |
| 100 |
*/ |
| 101 |
public function process( array $settings, Submission $submission, Form $form ) { |
| 102 |
if ( empty( $settings['to'] ) || empty( $settings['message'] ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
$settings = array_merge( $this->get_default_settings(), $settings ); |
| 107 |
$html_email = $settings['content_type'] === 'text/html'; |
| 108 |
|
| 109 |
// Strip newlines and tags from substituted values |
| 110 |
$sanitize_header_value = function( $value ) { |
| 111 |
return str_replace( array( "\r", "\n" ), '', strip_tags( $value ) ); |
| 112 |
}; |
| 113 |
|
| 114 |
$to = hf_replace_data_variables( $settings['to'], $submission, $sanitize_header_value ); |
| 115 |
$to = apply_filters( 'hf_action_email_to', $to, $submission ); |
| 116 |
|
| 117 |
$subject = ! empty( $settings['subject'] ) ? hf_replace_data_variables( $settings['subject'], $submission, $sanitize_header_value ) : ''; |
| 118 |
$subject = apply_filters( 'hf_action_email_subject', $subject, $submission ); |
| 119 |
|
| 120 |
$message = hf_replace_data_variables( $settings['message'], $submission, $html_email ? 'esc_html' : null ); |
| 121 |
$message = ! $html_email ? str_replace( '<br />', '', $message ) : $message; |
| 122 |
$message = apply_filters( 'hf_action_email_message', $message, $submission ); |
| 123 |
|
| 124 |
// parse additional email headers from settings |
| 125 |
$headers = array(); |
| 126 |
if ( ! empty( $settings['headers'] ) ) { |
| 127 |
$headers = explode( PHP_EOL, hf_replace_data_variables( $settings['headers'], $submission, $sanitize_header_value ) ); |
| 128 |
} |
| 129 |
|
| 130 |
$content_type = $html_email ? 'text/html' : 'text/plain'; |
| 131 |
$charset = get_bloginfo( 'charset' ); |
| 132 |
$headers[] = sprintf( 'Content-Type: %s; charset=%s', $content_type, $charset ); |
| 133 |
|
| 134 |
if ( ! empty( $settings['from'] ) ) { |
| 135 |
$from = apply_filters( 'hf_action_email_from', hf_replace_data_variables( $settings['from'], $submission, $sanitize_header_value ), $submission ); |
| 136 |
$headers[] = sprintf( 'From: %s', $from ); |
| 137 |
} |
| 138 |
|
| 139 |
return wp_mail( $to, $subject, $message, $headers ); |
| 140 |
} |
| 141 |
} |
| 142 |
|