| 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 |
public function page_settings( $settings ) { |
| 18 |
$defaults = array_fill_keys( array( 'from', 'to', 'subject', 'message' ), '' ); |
| 19 |
$settings = array_merge( $defaults, $settings ); |
| 20 |
?> |
| 21 |
<span class="hf-action-summary"><?php printf( 'From %s. To %s.', $settings['from'], $settings['to'] ); ?></span> |
| 22 |
<input type="hidden" name="form[settings][actions][0][type]" value="<?php echo $this->type; ?>" /> |
| 23 |
<table class="form-table"> |
| 24 |
<tr> |
| 25 |
<th><label><?php echo __( 'From', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 26 |
<td> |
| 27 |
<input name="form[settings][actions][0][from]" value="<?php echo esc_attr( $settings['from'] ); ?>" type="text" class="regular-text" placeholder="jane@email.com" required /> |
| 28 |
</td> |
| 29 |
</tr> |
| 30 |
<tr> |
| 31 |
<th><label><?php echo __( 'To', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 32 |
<td> |
| 33 |
<input name="form[settings][actions][0][to]" value="<?php echo esc_attr( $settings['to'] ); ?>" type="text" class="regular-text" placeholder="john@email.com" required /> |
| 34 |
</td> |
| 35 |
</tr> |
| 36 |
<tr> |
| 37 |
<th><label><?php echo __( 'Subject', 'html-forms' ); ?></label></th> |
| 38 |
<td> |
| 39 |
<input name="form[settings][actions][0][subject]" value="<?php echo esc_attr( $settings['subject'] ); ?>" type="text" class="regular-text" placeholder="<?php echo esc_attr( __( 'Your email subject', 'html-forms' ) ); ?>" /> |
| 40 |
</td> |
| 41 |
</tr> |
| 42 |
<tr> |
| 43 |
<th><label><?php echo __( 'Message', 'html-forms' ); ?> <span class="hf-required">*</span></label></th> |
| 44 |
<td> |
| 45 |
<textarea name="form[settings][actions][0][message]" rows="8" class="widefat" placeholder="<?php echo esc_attr( __( 'Your email message', 'html-forms' ) ); ?>" required><?php echo esc_textarea( $settings['message'] ); ?></textarea> |
| 46 |
<p class="help"><?php _e( 'You can use the following variables: ', 'html-forms' ); ?><span class="hf-field-names"></span></p> |
| 47 |
</td> |
| 48 |
</tr> |
| 49 |
</table> |
| 50 |
<?php |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Processes this action |
| 55 |
* |
| 56 |
* @param array $settings |
| 57 |
* @param Submission $submission |
| 58 |
* @param Form $form |
| 59 |
*/ |
| 60 |
public function process( array $settings, Submission $submission, Form $form ) { |
| 61 |
if( empty( $settings['to'] ) || empty( $settings['message'] ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$to = hf_template( $settings['to'], $submission->data ); |
| 66 |
$subject = ! empty( $settings['subject'] ) ? hf_template( $settings['subject'], $submission->data ) : ''; |
| 67 |
$message = nl2br( hf_template( $settings['message'], $submission->data ) ); |
| 68 |
$headers = array( 'Content-Type: text/html' ); |
| 69 |
|
| 70 |
if( ! empty( $settings['from'] ) ) { |
| 71 |
$from = hf_template($settings['from'], $submission->data); |
| 72 |
$headers[] = sprintf( 'From: %s', $from ); |
| 73 |
} |
| 74 |
|
| 75 |
wp_mail( $to, $subject, $message, $headers ); |
| 76 |
} |
| 77 |
} |