PluginProbe
Hello Plus / 1.2.0
Hello Plus v1.2.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / forms / actions / email.php

email.php in Hello Plus 1.2.0, at modules/forms/actions/email.php

386 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace HelloPlus\Modules\Forms\Actions;
3
4 use Elementor\Controls_Manager;
5 use HelloPlus\Modules\Forms\Classes\Action_Base;
6 use HelloPlus\Modules\Forms\Classes\Form_Record;
7 use HelloPlus\Modules\Forms\Components\Ajax_Handler;
8 use HelloPlus\Modules\Forms\Module;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Email extends Action_Base {
15
16 public function get_name(): string {
17 return 'ehp-email';
18 }
19
20 public function get_label(): string {
21 return esc_html__( 'Email', 'hello-plus' );
22 }
23
24 public function register_settings_section( $widget ) {
25 $widget->start_controls_section(
26 $this->get_control_id( 'section_email' ),
27 [
28 'label' => $this->get_label(),
29 'tab' => Controls_Manager::TAB_CONTENT,
30 'condition' => [
31 'submit_actions' => $this->get_name(),
32 ],
33 ]
34 );
35
36 $widget->add_control(
37 $this->get_control_id( 'email_to' ),
38 [
39 'label' => esc_html__( 'To', 'hello-plus' ),
40 'type' => Controls_Manager::TEXT,
41 'default' => get_option( 'admin_email' ),
42 'ai' => [
43 'active' => false,
44 ],
45 'placeholder' => get_option( 'admin_email' ),
46 'label_block' => true,
47 'title' => esc_html__( 'Separate emails with commas', 'hello-plus' ),
48 'render_type' => 'none',
49 'dynamic' => [
50 'active' => true,
51 ],
52 ]
53 );
54
55 /* translators: %s: Site title. */
56 $default_message = sprintf( esc_html__( 'New message from [%s]', 'hello-plus' ), get_bloginfo( 'name' ) );
57
58 $widget->add_control(
59 $this->get_control_id( 'email_subject' ),
60 [
61 'label' => esc_html__( 'Subject', 'hello-plus' ),
62 'type' => Controls_Manager::TEXT,
63 'default' => $default_message,
64 'ai' => [
65 'active' => false,
66 ],
67 'placeholder' => $default_message,
68 'label_block' => true,
69 'render_type' => 'none',
70 'dynamic' => [
71 'active' => true,
72 ],
73 ]
74 );
75
76 $widget->add_control(
77 $this->get_control_id( 'email_content' ),
78 [
79 'label' => esc_html__( 'Message', 'hello-plus' ),
80 'type' => Controls_Manager::TEXTAREA,
81 'default' => '[all-fields]',
82 'ai' => [
83 'active' => false,
84 ],
85 'placeholder' => '[all-fields]',
86 'description' => sprintf(
87 /* translators: %s: The [all-fields] shortcode. */
88 esc_html__( 'By default, all form fields are sent via %s shortcode. To customize sent fields, copy the shortcode that appears inside each field and paste it above.', 'hello-plus' ),
89 '<code>[all-fields]</code>'
90 ),
91 'render_type' => 'none',
92 'dynamic' => [
93 'active' => true,
94 ],
95 ]
96 );
97
98 $site_domain = Module::get_site_domain();
99
100 $widget->add_control(
101 $this->get_control_id( 'email_from' ),
102 [
103 'label' => esc_html__( 'From Email', 'hello-plus' ),
104 'type' => Controls_Manager::TEXT,
105 'default' => 'email@' . $site_domain,
106 'ai' => [
107 'active' => false,
108 ],
109 'render_type' => 'none',
110 'dynamic' => [
111 'active' => true,
112 ],
113 ]
114 );
115
116 $widget->add_control(
117 $this->get_control_id( 'email_from_name' ),
118 [
119 'label' => esc_html__( 'From Name', 'hello-plus' ),
120 'type' => Controls_Manager::TEXT,
121 'default' => get_bloginfo( 'name' ),
122 'ai' => [
123 'active' => false,
124 ],
125 'render_type' => 'none',
126 'dynamic' => [
127 'active' => true,
128 ],
129 ]
130 );
131
132 $widget->add_control(
133 $this->get_control_id( 'email_reply_to' ),
134 [
135 'label' => esc_html__( 'Reply-To', 'hello-plus' ),
136 'type' => Controls_Manager::SELECT,
137 'options' => [
138 '' => '',
139 ],
140 'render_type' => 'none',
141 ]
142 );
143
144 $widget->add_control(
145 $this->get_control_id( 'email_to_cc' ),
146 [
147 'label' => esc_html__( 'Cc', 'hello-plus' ),
148 'type' => Controls_Manager::TEXT,
149 'default' => '',
150 'ai' => [
151 'active' => false,
152 ],
153 'title' => esc_html__( 'Separate emails with commas', 'hello-plus' ),
154 'render_type' => 'none',
155 'dynamic' => [
156 'active' => true,
157 ],
158 ]
159 );
160
161 $widget->add_control(
162 $this->get_control_id( 'email_to_bcc' ),
163 [
164 'label' => esc_html__( 'Bcc', 'hello-plus' ),
165 'type' => Controls_Manager::TEXT,
166 'default' => '',
167 'ai' => [
168 'active' => false,
169 ],
170 'title' => esc_html__( 'Separate emails with commas', 'hello-plus' ),
171 'render_type' => 'none',
172 'dynamic' => [
173 'active' => true,
174 ],
175 ]
176 );
177
178 $widget->add_control(
179 $this->get_control_id( 'form_metadata' ),
180 [
181 'label' => esc_html__( 'Meta Data', 'hello-plus' ),
182 'type' => Controls_Manager::SELECT2,
183 'multiple' => true,
184 'label_block' => true,
185 'separator' => 'before',
186 'default' => [
187 'date',
188 'time',
189 'page_url',
190 'user_agent',
191 'remote_ip',
192 'credit',
193 ],
194 'options' => [
195 'date' => esc_html__( 'Date', 'hello-plus' ),
196 'time' => esc_html__( 'Time', 'hello-plus' ),
197 'page_url' => esc_html__( 'Page URL', 'hello-plus' ),
198 'user_agent' => esc_html__( 'User Agent', 'hello-plus' ),
199 'remote_ip' => esc_html__( 'Remote IP', 'hello-plus' ),
200 'credit' => esc_html__( 'Credit', 'hello-plus' ),
201 ],
202 'render_type' => 'none',
203 ]
204 );
205
206 $widget->add_control(
207 $this->get_control_id( 'email_content_type' ),
208 [
209 'label' => esc_html__( 'Send As', 'hello-plus' ),
210 'type' => Controls_Manager::SELECT,
211 'default' => 'html',
212 'render_type' => 'none',
213 'options' => [
214 'html' => esc_html__( 'HTML', 'hello-plus' ),
215 'plain' => esc_html__( 'Plain', 'hello-plus' ),
216 ],
217 ]
218 );
219
220 $widget->end_controls_section();
221 }
222
223 public function on_export( $element ) {
224 $controls_to_unset = [
225 'email_to',
226 'email_from',
227 'email_from_name',
228 'email_subject',
229 'email_reply_to',
230 'email_to_cc',
231 'email_to_bcc',
232 ];
233
234 foreach ( $controls_to_unset as $base_id ) {
235 $control_id = $this->get_control_id( $base_id );
236 unset( $element['settings'][ $control_id ] );
237 }
238
239 return $element;
240 }
241
242 public function run( $record, $ajax_handler ) {
243 $settings = $record->get( 'form_settings' );
244 $send_html = 'plain' !== $settings[ $this->get_control_id( 'email_content_type' ) ];
245 $line_break = $send_html ? '<br>' : "\n";
246
247 $fields = [
248 'email_to' => get_option( 'admin_email' ),
249 /* translators: %s: Site title. */
250 'email_subject' => sprintf( esc_html__( 'New message from [%s]', 'hello-plus' ), get_bloginfo( 'name' ) ),
251 'email_content' => '[all-fields]',
252 'email_from_name' => get_bloginfo( 'name' ),
253 'email_from' => get_bloginfo( 'admin_email' ),
254 'email_reply_to' => 'noreply@' . Module::get_site_domain(),
255 'email_to_cc' => '',
256 'email_to_bcc' => '',
257 ];
258
259 foreach ( $fields as $key => $default ) {
260 $setting = trim( $settings[ $this->get_control_id( $key ) ] );
261 $setting = $record->replace_setting_shortcodes( $setting );
262 if ( ! empty( $setting ) ) {
263 $fields[ $key ] = $setting;
264 }
265 }
266
267 $email_reply_to = $this->get_reply_to( $record, $fields );
268
269 $fields['email_content'] = $this->replace_content_shortcodes( $fields['email_content'], $record, $line_break );
270
271 $email_meta = '';
272
273 $form_metadata_settings = $settings[ $this->get_control_id( 'form_metadata' ) ];
274
275 foreach ( $record->get( 'meta' ) as $id => $field ) {
276 if ( in_array( $id, $form_metadata_settings, true ) ) {
277 $email_meta .= $this->field_formatted( $field ) . $line_break;
278 }
279 }
280
281 if ( ! empty( $email_meta ) ) {
282 $fields['email_content'] .= $line_break . '---' . $line_break . $line_break . $email_meta;
283 }
284
285 $headers = sprintf( 'From: %s <%s>' . "\r\n", $fields['email_from_name'], $fields['email_from'] );
286 $headers .= sprintf( 'Reply-To: %s' . "\r\n", $email_reply_to );
287
288 if ( $send_html ) {
289 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
290 }
291
292 $cc_header = '';
293 if ( ! empty( $fields['email_to_cc'] ) ) {
294 $cc_header = 'Cc: ' . $fields['email_to_cc'] . "\r\n";
295 }
296
297 $email_sent = wp_mail(
298 $fields['email_to'],
299 $fields['email_subject'],
300 $fields['email_content'],
301 $headers . $cc_header,
302 );
303
304 if ( ! empty( $fields['email_to_bcc'] ) ) {
305 $bcc_emails = explode( ',', $fields['email_to_bcc'] );
306 foreach ( $bcc_emails as $bcc_email ) {
307 wp_mail(
308 trim( $bcc_email ),
309 $fields['email_subject'],
310 $fields['email_content'],
311 $headers,
312 );
313 }
314 }
315
316 if ( ! $email_sent ) {
317 $message = Ajax_Handler::get_default_message( Ajax_Handler::SERVER_ERROR, $settings );
318
319 $ajax_handler->add_error_message( $message );
320
321 throw new \Exception( esc_html( $message ) );
322 }
323 }
324
325 private function field_formatted( $field ) {
326 $formatted = '';
327 if ( ! empty( $field['title'] ) ) {
328 $formatted = sprintf( '%s: %s', $field['title'], $field['value'] );
329 } elseif ( ! empty( $field['value'] ) ) {
330 $formatted = sprintf( '%s', $field['value'] );
331 }
332
333 return $formatted;
334 }
335
336 // Allow overwrite the control_id with a prefix, @see Email2
337 protected function get_control_id( $control_id ) {
338 return $control_id;
339 }
340
341 protected function get_reply_to( $record, $fields ) {
342 $email_reply_to = '';
343
344 if ( ! empty( $fields['email_reply_to'] ) ) {
345 $sent_data = $record->get( 'sent_data' );
346 foreach ( $record->get( 'fields' ) as $field_index => $field ) {
347 if ( $field_index === $fields['email_reply_to'] && ! empty( $sent_data[ $field_index ] ) && is_email( $sent_data[ $field_index ] ) ) {
348 $email_reply_to = $sent_data[ $field_index ];
349 break;
350 }
351 }
352 }
353
354 return $email_reply_to;
355 }
356
357 /**
358 * @param string $email_content
359 * @param Form_Record $record
360 *
361 * @return string
362 */
363 private function replace_content_shortcodes( $email_content, $record, $line_break ) {
364 $email_content = do_shortcode( $email_content );
365 $all_fields_shortcode = '[all-fields]';
366
367 if ( false !== strpos( $email_content, $all_fields_shortcode ) ) {
368 $text = '';
369 foreach ( $record->get( 'fields' ) as $field ) {
370
371 $formatted = $this->field_formatted( $field );
372 if ( ( 'textarea' === $field['type'] ) && ( '<br>' === $line_break ) ) {
373 $formatted = str_replace( [ "\r\n", "\n", "\r" ], '<br />', $formatted );
374 }
375
376 $text .= $formatted . $line_break;
377 }
378
379 $email_content = str_replace( $all_fields_shortcode, $text, $email_content );
380
381 }
382
383 return $email_content;
384 }
385 }
386