PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.5.6
HTML Forms – Simple WordPress Forms Plugin v1.5.6
1.7.0 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 All 67 releases
html-forms / src / actions / class-email.php

class-email.php in HTML Forms – Simple WordPress Forms Plugin 1.5.6, at src/actions/class-email.php

137 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $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 $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 $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 $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 $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 $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 $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 $to = hf_replace_data_variables( $settings['to'], $submission, 'strip_tags' );
110 $to = apply_filters( 'hf_action_email_to', $to, $submission );
111
112 $subject = ! empty( $settings['subject'] ) ? hf_replace_data_variables( $settings['subject'], $submission, 'strip_tags' ) : '';
113 $subject = apply_filters( 'hf_action_email_subject', $subject, $submission );
114
115 $message = hf_replace_data_variables( $settings['message'], $submission, $html_email ? 'esc_html' : null );
116 $message = ! $html_email ? str_replace( '<br />', '', $message ) : $message;
117 $message = apply_filters( 'hf_action_email_message', $message, $submission );
118
119 // parse additional email headers from settings
120 $headers = array();
121 if ( ! empty( $settings['headers'] ) ) {
122 $headers = explode( PHP_EOL, hf_replace_data_variables( $settings['headers'], $submission, 'strip_tags' ) );
123 }
124
125 $content_type = $html_email ? 'text/html' : 'text/plain';
126 $charset = get_bloginfo( 'charset' );
127 $headers[] = sprintf( 'Content-Type: %s; charset=%s', $content_type, $charset );
128
129 if ( ! empty( $settings['from'] ) ) {
130 $from = apply_filters( 'hf_action_email_from', hf_replace_data_variables( $settings['from'], $submission, 'strip_tags' ), $submission );
131 $headers[] = sprintf( 'From: %s', $from );
132 }
133
134 return wp_mail( $to, $subject, $message, $headers );
135 }
136 }
137