PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.14
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.14
51.1.87 51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 All 41 releases
king-addons / includes / widgets / Form_Builder / helpers / Send_Email.php

Send_Email.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.14, at includes/widgets/Form_Builder/helpers/Send_Email.php

318 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons;
4
5 use King_Addons\Core;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 class Send_Email
12 {
13
14 public function __construct()
15 {
16 add_action('wp_ajax_king_addons_form_builder_email', [$this, 'send_email']);
17 add_action('wp_ajax_nopriv_king_addons_form_builder_email', [$this, 'send_email']);
18 }
19
20 public function send_email()
21 {
22
23 $nonce = $_POST['nonce'];
24
25 if (!wp_verify_nonce($nonce, 'king-addons-js')) {
26 return;
27 }
28
29 $message_body = [];
30
31 foreach ($_POST['form_content'] as $field) {
32 if ($field[0] === 'email') {
33 if (!is_email($field[1])) {
34
35
36 wp_send_json_error(array(
37 'action' => 'king_addons_form_builder_email',
38 'message' => esc_html__('Email provided is invalid', 'king-addons'),
39 'status' => 'error'
40 ));
41 }
42 }
43 }
44
45
46 $content_type = get_option('king_addons_email_content_type_' . $_POST['king_addons_form_id']);
47
48 $line_break = 'html' === $content_type ? '<br>' : "\n";
49
50 $email_fields = trim(get_option('king_addons_email_fields_' . $_POST['king_addons_form_id']));
51
52 if ($email_fields === '[all-fields]' || str_contains($email_fields, '[all-fields]')) {
53
54
55 $replace_shortcode_with_value = function ($matches) {
56 $field_id = $matches[1];
57 foreach ($_POST['form_content'] as $key => $value) {
58 $key_parts = explode('-', $key);
59 $last_part = end($key_parts);
60 if ($last_part === $field_id) {
61 return is_array($value[1]) ? implode("\n", $value[1]) : $value[1];
62 }
63 }
64 return '';
65 };
66
67
68 $all_fields_content = [];
69
70 foreach ($_POST['form_content'] as $key => $value) {
71 $all_fields_content[] = is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
72 }
73 $all_fields_content = implode("\n", $all_fields_content);
74
75
76 $processed_message = str_replace('[all-fields]', $all_fields_content, $email_fields);
77
78 $processed_message = preg_replace_callback(
79 '/\[id="([^"]+)"\]/',
80 $replace_shortcode_with_value,
81 $processed_message
82 );
83 } else {
84
85
86 $replace_shortcode_with_value = function ($matches) {
87 $field_id = $matches[1];
88 foreach ($_POST['form_content'] as $key => $value) {
89 $key_parts = explode('-', $key);
90 $last_part = end($key_parts);
91 if ($last_part === $field_id) {
92 return is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
93 }
94 }
95 return '';
96 };
97
98
99 $processed_message = preg_replace_callback(
100 '/\[id="([^"]+)"\]/',
101 $replace_shortcode_with_value,
102 $email_fields
103 );
104 }
105
106 $meta_keys = get_option('king_addons_meta_keys_' . $_POST['king_addons_form_id']);
107 $meta_fields = [];
108
109 foreach ($meta_keys as $metadata_type) {
110 switch ($metadata_type) {
111 case 'date':
112 $meta_fields['date'] = [
113 'title' => esc_html__('Date', 'king-addons'),
114 'value' => date_i18n(get_option('date_format')),
115 ];
116 break;
117
118 case 'time':
119 $meta_fields['time'] = [
120 'title' => esc_html__('Time', 'king-addons'),
121 'value' => date_i18n(get_option('time_format')),
122 ];
123 break;
124
125 case 'page_url':
126 $meta_fields['page_url'] = [
127 'title' => esc_html__('Page URL', 'king-addons'),
128
129 'value' => get_option('king_addons_referrer_' . $_POST['king_addons_form_id']) ? get_option('king_addons_referrer_' . $_POST['king_addons_form_id']) : '',
130 ];
131 break;
132
133 case 'page_title':
134 $meta_fields['page_title'] = [
135 'title' => esc_html__('Page Title', 'king-addons'),
136
137 'value' => get_option('king_addons_referrer_title_' . $_POST['king_addons_form_id']) ? get_option('king_addons_referrer_title_' . $_POST['king_addons_form_id']) : '',
138 ];
139 break;
140
141 case 'user_agent':
142 $meta_fields['user_agent'] = [
143 'title' => esc_html__('User Agent', 'king-addons'),
144 'value' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_textarea_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '',
145 ];
146 break;
147
148 case 'remote_ip':
149 $meta_fields['remote_ip'] = [
150 'title' => esc_html__('Remote IP', 'king-addons'),
151 'value' => Core::getClientIP(),
152 ];
153 break;
154
155 case 'credit':
156 $meta_fields['credit'] = [
157 'title' => esc_html__('Powered by', 'king-addons'),
158 'value' => esc_html__('King Addons', 'king-addons'),
159 ];
160 break;
161 }
162 }
163
164 $email_meta = [];
165
166 foreach ($meta_fields as $key => $value) {
167 $email_meta[] = $value['title'] . ': ' . $value['value'];
168 }
169
170 $to = get_option('king_addons_email_to_' . $_POST['king_addons_form_id']);
171
172 $to = preg_replace_callback(
173 '/\[id="(\w+)"\]/',
174 function ($matches) {
175 return $this->get_field_value($matches[1]);
176 },
177 $to
178 );
179
180 $subject = get_option('king_addons_email_subject_' . $_POST['king_addons_form_id']);
181
182 $subject = preg_replace_callback(
183 '/\[id="(\w+)"\]/',
184 function ($matches) {
185 return $this->get_field_value($matches[1]);
186 },
187 $subject
188 );
189
190 if ($processed_message) {
191 $message_body[] = $processed_message;
192 }
193
194
195 if ($content_type === 'html') {
196
197 foreach ($message_body as &$item) {
198 $item = nl2br($item);
199 }
200 unset($item);
201 }
202
203 $body = implode($line_break, $message_body) . $line_break . '-----' . $line_break . implode($line_break, $email_meta);
204
205 $cc_header = '';
206 if (!empty(get_option('king_addons_cc_header_' . $_POST['king_addons_form_id']))) {
207 $cc_header = 'Cc: ' . get_option('king_addons_cc_header_' . $_POST['king_addons_form_id']);
208
209 $cc_header = preg_replace_callback(
210 '/\[id="(\w+)"\]/',
211 function ($matches) {
212 return $this->get_field_value($matches[1]);
213 },
214 $cc_header
215 );
216 }
217
218 $bcc_header = '';
219 if (!empty(get_option('king_addons_bcc_header_' . $_POST['king_addons_form_id']))) {
220 $bcc_header = 'Bcc: ' . get_option('king_addons_bcc_header_' . $_POST['king_addons_form_id']);
221
222 $bcc_header = preg_replace_callback(
223 '/\[id="([^\"]+)"\]/',
224 function ($matches) {
225 return $this->get_field_value($matches[1]);
226 },
227 $bcc_header
228 );
229 }
230
231 // Initialize reply-to and email-from variables to avoid undefined variable warnings
232 $reply_to_address = '';
233 $email_from_name = '';
234 $email_from_mail = '';
235 $reply_to = '';
236
237 if (!empty(get_option('king_addons_reply_to_' . $_POST['king_addons_form_id'])) && !empty(get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id'])) && !empty(get_option('king_addons_email_from_' . $_POST['king_addons_form_id']))) {
238
239 preg_match_all('/id="([^"]+)"/', get_option('king_addons_reply_to_' . $_POST['king_addons_form_id']), $matche);
240 $reply_to_field_id = $matche[1];
241
242 preg_match_all('/id="([^"]+)"/', get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id']), $matche);
243 $email_from_name_field_id = $matche[1];
244
245 preg_match_all('/id="([^"]+)"/', get_option('king_addons_email_from_' . $_POST['king_addons_form_id']), $matche);
246 $email_from_field_id = $matche[1];
247
248 foreach ($_POST['form_content'] as $key => $value) {
249 $key_parts = explode('-', $key);
250 $last_part = end($key_parts);
251
252 if (in_array($last_part, $reply_to_field_id)) {
253 $reply_to_address = $value[1];
254 }
255
256 if (in_array($last_part, $email_from_name_field_id)) {
257 $email_from_name = $value[1];
258 }
259
260 if (in_array($last_part, $email_from_field_id)) {
261 $email_from_mail = $value[1];
262 }
263 }
264
265 if (!$reply_to_address) {
266 $reply_to_address = get_option('king_addons_reply_to_' . $_POST['king_addons_form_id']);
267 }
268
269 if (!$email_from_name) {
270 $email_from_name = get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id']);
271 }
272
273 if (!$email_from_mail) {
274 $email_from_mail = get_option('king_addons_email_from_' . $_POST['king_addons_form_id']);
275 }
276
277 $reply_to = 'Reply-To: ' . $reply_to_address;
278 }
279
280 $email_from = sprintf('From: %s <%s>' . "\r\n", $email_from_name, $email_from_mail);
281
282 $headers = array('Content-Type: text/' . $content_type . '; charset=UTF-8', $email_from, $cc_header, $bcc_header, $reply_to);
283
284
285 $sent = wp_mail($to, $subject, $body, $headers);
286
287 if ($sent) {
288 wp_send_json_success(array(
289 'action' => 'king_addons_form_builder_email',
290 'message' => esc_html__('Message sent successfully', 'king-addons'),
291 'status' => 'success',
292 'details' => json_encode($message_body)
293 ));
294 } else {
295 wp_send_json_error(array(
296 'action' => 'king_addons_form_builder_email',
297 'message' => esc_html__('Message could not be sent', 'king-addons'),
298 'status' => 'error',
299 'details' => json_encode($message_body)
300 ));
301 }
302 }
303
304 public function get_field_value($field_id)
305 {
306 foreach ($_POST['form_content'] as $key => $field) {
307 $key_parts = explode('-', $key);
308 $last_part = end($key_parts);
309
310 if ($last_part === $field_id) {
311 return $field[1];
312 }
313 }
314 return '';
315 }
316 }
317
318 new Send_Email();