PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.12
HTML Forms – Simple WordPress Forms Plugin v1.3.12
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 1.3.17 All 66 releases
html-forms / src / Actions / Email.php

Email.php in HTML Forms – Simple WordPress Forms Plugin 1.3.12, at src/Actions/Email.php

127 lines 5.6 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 $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/plain' ); ?>>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 $html_email = $settings['content_type'] === 'text/html';
103
104 $to = apply_filters( 'hf_action_email_to', hf_replace_data_variables( $settings['to'], $submission->data, 'strip_tags' ), $submission );
105 $subject = ! empty( $settings['subject'] ) ? hf_replace_data_variables( $settings['subject'], $submission->data, 'strip_tags' ) : '';
106 $subject = apply_filters( 'hf_action_email_subject', $subject, $submission );
107 $message = apply_filters( 'hf_action_email_message', hf_replace_data_variables( $settings['message'], $submission->data, $html_email ? null : 'strip_tags' ), $submission );
108
109 // parse additional email headers from settings
110 $headers = array();
111 if( ! empty( $settings['headers'] ) ) {
112 $headers = explode( PHP_EOL, hf_replace_data_variables( $settings['headers'], $submission->data, 'strip_tags' ) );
113 }
114
115 $content_type = $html_email ? 'text/html' : 'text/plain';
116 $charset = get_bloginfo('charset');
117 $headers[] = sprintf('Content-Type: %s; charset=%s', $content_type, $charset);
118
119 if( ! empty( $settings['from'] ) ) {
120 $from = apply_filters( 'hf_action_email_from', hf_replace_data_variables( $settings['from'], $submission->data, 'strip_tags' ), $submission );
121 $headers[] = sprintf( 'From: %s', $from );
122 }
123
124 return wp_mail( $to, $subject, $message, $headers );
125 }
126 }
127