| 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 |
$defaults = 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 |
return $defaults; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @param array $settings |
| 34 |
* @param string|int $index |
| 35 |
*/ |
| 36 |
public function page_settings( $settings, $index ) { |
| 37 |
$settings = array_merge( $this->get_default_settings(), $settings ); |
| 38 |
?> |
| 39 |
<span class="hf-action-summary"><?php printf( 'From %s. To %s.', $settings['from'], $settings['to'] ); ?></span> |
| 40 |
<input type="hidden" name="form[settings][actions][<?php echo $index; ?>][type]" value="<?php echo $this->type; ?>" /> |
| 41 |
<table class="form-table"> |
| 42 |
<tr> |
| 43 |
<th><label><?php echo __( 'From', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 44 |
<td> |
| 45 |
<input name="form[settings][actions][<?php echo $index; ?>][from]" value="<?php echo esc_attr( $settings['from'] ); ?>" type="text" class="regular-text" placeholder="jane@email.com" required /> |
| 46 |
</td> |
| 47 |
</tr> |
| 48 |
<tr> |
| 49 |
<th><label><?php echo __( 'To', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 50 |
<td> |
| 51 |
<input name="form[settings][actions][<?php echo $index; ?>][to]" value="<?php echo esc_attr( $settings['to'] ); ?>" type="text" class="regular-text" placeholder="john@email.com" required /> |
| 52 |
</td> |
| 53 |
</tr> |
| 54 |
<tr> |
| 55 |
<th><label><?php echo __( 'Subject', 'html-forms' ); ?></label></th> |
| 56 |
<td> |
| 57 |
<input name="form[settings][actions][<?php echo $index; ?>][subject]" value="<?php echo esc_attr( $settings['subject'] ); ?>" type="text" class="regular-text" placeholder="<?php echo esc_attr( __( 'Your email subject', 'html-forms' ) ); ?>" /> |
| 58 |
</td> |
| 59 |
</tr> |
| 60 |
|
| 61 |
<tr> |
| 62 |
<th><label><?php echo __( 'Message', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 63 |
<td> |
| 64 |
<textarea name="form[settings][actions][<?php echo $index; ?>][message]" rows="8" class="widefat" placeholder="<?php echo esc_attr( __( 'Your email message', 'html-forms' ) ); ?>" required><?php echo esc_textarea( $settings['message'] ); ?></textarea> |
| 65 |
<p class="help"><?php _e( 'You can use the following variables (in all fields): ', 'html-forms' ); ?><br /><span class="hf-field-names"></span></p> |
| 66 |
</td> |
| 67 |
</tr> |
| 68 |
|
| 69 |
<tr> |
| 70 |
<th><label><?php echo __( 'Content Type', 'html-forms' ); ?></label></th> |
| 71 |
<td> |
| 72 |
<select name="form[settings][actions][<?php echo $index; ?>][content_type]" required> |
| 73 |
<option <?php selected( $settings['content_type'], 'text/html' ); ?>>text/plain</option> |
| 74 |
<option <?php selected( $settings['content_type'], 'text/html' ); ?>>text/html</option> |
| 75 |
</select> |
| 76 |
</td> |
| 77 |
</tr> |
| 78 |
|
| 79 |
<tr> |
| 80 |
<th><label><?php echo __( 'Additional headers', 'html-forms' ); ?></label></th> |
| 81 |
<td> |
| 82 |
<textarea name="form[settings][actions][<?php echo $index; ?>][headers]" rows="4" class="widefat" placeholder="<?php echo esc_attr( 'Reply-To: [NAME] <[EMAIL]>' ); ?>"><?php echo esc_textarea( $settings['headers'] ); ?></textarea> |
| 83 |
</td> |
| 84 |
</tr> |
| 85 |
</table> |
| 86 |
<?php |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Processes this action |
| 91 |
* |
| 92 |
* @param array $settings |
| 93 |
* @param Submission $submission |
| 94 |
* @param Form $form |
| 95 |
*/ |
| 96 |
public function process( array $settings, Submission $submission, Form $form ) { |
| 97 |
if( empty( $settings['to'] ) || empty( $settings['message'] ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
$settings = array_merge( $this->get_default_settings(), $settings ); |
| 102 |
|
| 103 |
$to = hf_replace_data_variables( $settings['to'], $submission->data ); |
| 104 |
$subject = ! empty( $settings['subject'] ) ? hf_replace_data_variables( $settings['subject'], $submission->data ) : ''; |
| 105 |
$message = hf_replace_data_variables( $settings['message'], $submission->data ); |
| 106 |
|
| 107 |
// parse additional email headers from settings |
| 108 |
$headers = array(); |
| 109 |
if( ! empty( $settings['headers'] ) ) { |
| 110 |
$headers = explode( PHP_EOL, hf_replace_data_variables( $settings['headers'], $submission->data ) ); |
| 111 |
} |
| 112 |
|
| 113 |
$html_email = $settings['content_type'] === 'text/html'; |
| 114 |
if( $html_email ) { |
| 115 |
$headers[] = 'Content-Type: text/html'; |
| 116 |
$message = nl2br( $message ); |
| 117 |
} |
| 118 |
|
| 119 |
if( ! empty( $settings['from'] ) ) { |
| 120 |
$from = hf_replace_data_variables($settings['from'], $submission->data); |
| 121 |
$headers[] = sprintf( 'From: %s', $from ); |
| 122 |
} |
| 123 |
|
| 124 |
return wp_mail( $to, $subject, $message, $headers ); |
| 125 |
} |
| 126 |
} |
| 127 |
|