PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.33
HTML Forms – Simple WordPress Forms Plugin v1.3.33
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.3.33, at src/actions/class-email.php

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