PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.34
E2Pdf – Export Pdf Tool for WordPress v1.32.34
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / extension / e2pdf-divi.php

e2pdf-divi.php in E2Pdf – Export Pdf Tool for WordPress 1.32.34, at classes/extension/e2pdf-divi.php

1,983 lines 99.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /extension/e2pdf-divi.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Extension_E2pdf_Divi extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = [
18 'key' => 'divi',
19 'title' => 'Divi Forms',
20 ];
21
22 // info
23 public function info($key = false) {
24 if ($key && isset($this->info[$key])) {
25 return $this->info[$key];
26 } else {
27 return [
28 $this->info['key'] => $this->info['title'],
29 ];
30 }
31 }
32
33 // active
34 public function active() {
35 if (file_exists(get_template_directory() . '/et-pagebuilder/et-pagebuilder.php')) {
36 return true;
37 } else {
38 if (defined('E2PDF_DIVI_EXTENSION') || $this->helper->load('extension')->is_plugin_active('divi-builder/divi-builder.php')) {
39 return true;
40 }
41 }
42 return false;
43 }
44
45 // set
46 public function set($key, $value) {
47 if (!isset($this->options)) {
48 $this->options = new stdClass();
49 }
50 $this->options->$key = $value;
51 switch ($key) {
52 case 'dataset':
53 $this->set('cached_entry', []);
54 $this->set('cached_meta', []);
55 if ($this->get('item') && $this->get('dataset')) {
56 $entry = new Model_E2pdf_Dataset();
57 if ($entry->load($this->get('dataset'), $this->get('item'), 'divi')) {
58 $cached_entry = $entry->get('entry');
59 $this->set('cached_entry', $cached_entry);
60
61 $meta_data = [];
62 if ($cached_entry && is_array($cached_entry)) {
63 // Divi5
64 if (!empty($cached_entry['ET_CORE_VERSION'])) {
65 foreach ($cached_entry as $field_key => $field_value) {
66 $meta_data['%%' . $field_key . '%%'] = $field_value;
67 }
68 if (!isset($meta_data['%%e2pdf_entry_id%%'])) {
69 $meta_data['%%e2pdf_entry_id%%'] = (int) $this->get('dataset');
70 }
71 } else {
72 // Divi4
73 $processed_fields_values = [];
74 $et_contact_proccess = array_search('et_contact_proccess', $cached_entry);
75 $et_pb_contact_form_num = $et_contact_proccess === false ? 0 : str_replace('et_pb_contactform_submit_', '', $et_contact_proccess);
76 $current_form_fields = isset($cached_entry['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $cached_entry['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
77 if ('' !== $current_form_fields) {
78 $fields_data_json = str_replace('\\', '', $current_form_fields);
79 $fields_data_array = json_decode($fields_data_json, true);
80 if (!empty($fields_data_array)) {
81 foreach ($fields_data_array as $field_value) {
82 $processed_fields_values[$field_value['original_id']]['value'] = isset($cached_entry[$field_value['field_id']]) ? $cached_entry[$field_value['field_id']] : '';
83 $processed_fields_values[$field_value['original_id']]['label'] = $field_value['field_label'];
84
85 $is_file = isset($cached_entry[$field_value['field_id'] . '_is_file']) && $cached_entry[$field_value['field_id'] . '_is_file'] == 'yes';
86 $is_signature_pad = isset($cached_entry[$field_value['field_id'] . '_is_signature_pad']) && $cached_entry[$field_value['field_id'] . '_is_signature_pad'] == 'yes';
87
88 if (($is_signature_pad || $is_file) && $processed_fields_values[$field_value['original_id']]['value']) {
89 $subdir = isset($cached_entry['_subdir']) ? $cached_entry['_subdir'] : '';
90 if (isset($cached_entry['_save_files_to_media']) && $cached_entry['_save_files_to_media'] == 'on') {
91 if ($subdir && !file_exists(path_join(wp_upload_dir()['basedir'] . $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\/\d{4}\/\d{2}$/', $subdir)) {
92 $tmpsubdir = '/' . date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . ' -1 month'));
93 if (file_exists(path_join(wp_upload_dir()['basedir'] . $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
94 $subdir = $tmpsubdir;
95 }
96 }
97 $processed_fields_values[$field_value['original_id']]['value'] = path_join(wp_upload_dir()['baseurl'] . $subdir, $processed_fields_values[$field_value['original_id']]['value']);
98 } else {
99 $contact_form_id = isset($cached_entry['_unique_id']) ? $cached_entry['_unique_id'] : '';
100 if (function_exists('pwh_dcfh_file_helpers') && $contact_form_id) {
101 if ($subdir && !file_exists(pwh_dcfh_file_helpers()::get_form_upload_dir($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\d{4}\/\d{2}$/', $subdir)) {
102 $tmpsubdir = date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . " -1 month"));
103 if (file_exists(pwh_dcfh_file_helpers()::get_form_upload_dir($contact_form_id, $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
104 $subdir = $tmpsubdir;
105 }
106 }
107 $processed_fields_values[$field_value['original_id']]['value'] = pwh_dcfh_file_helpers()::get_form_upload_url($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value']);
108 } elseif (function_exists('ks_pac_dcfh_file_helper') && $contact_form_id) {
109 if ($subdir && !file_exists(ks_pac_dcfh_file_helper()::get_form_upload_dir($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\d{4}\/\d{2}$/', $subdir)) {
110 $tmpsubdir = date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . " -1 month"));
111 if (file_exists(ks_pac_dcfh_file_helper()::get_form_upload_dir($contact_form_id, $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
112 $subdir = $tmpsubdir;
113 }
114 }
115 $processed_fields_values[$field_value['original_id']]['value'] = ks_pac_dcfh_file_helper()::get_form_upload_url($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value']);
116 } else {
117 $processed_fields_values[$field_value['original_id']]['value'] = '';
118 }
119 }
120 }
121 }
122 }
123 }
124 if (!isset($processed_fields_values['_wp_http_referer'])) {
125 $processed_fields_values['_wp_http_referer'] = [
126 'value' => isset($cached_entry['_wp_http_referer']) ? $cached_entry['_wp_http_referer'] : '',
127 'label' => '_wp_http_referer',
128 ];
129 }
130 if (!isset($processed_fields_values['e2pdf_entry_id'])) {
131 $processed_fields_values['e2pdf_entry_id'] = [
132 'value' => (int) $this->get('dataset'),
133 'label' => 'e2pdf_entry_id',
134 ];
135 }
136 foreach ($processed_fields_values as $field_key => $field_value) {
137 $meta_data['%%' . $field_key . '%%'] = $field_value['value'];
138 }
139 }
140 }
141 $this->set('cached_meta', $meta_data);
142 }
143 }
144 break;
145 default:
146 break;
147 }
148 }
149
150 // get
151 public function get($key) {
152 if (isset($this->options->$key)) {
153 $value = $this->options->$key;
154 } else {
155 switch ($key) {
156 case 'args':
157 case 'cached_entry':
158 case 'cached_meta':
159 $value = [];
160 break;
161 default:
162 $value = false;
163 break;
164 }
165 }
166 return $value;
167 }
168
169 // items
170 public function items() {
171 global $wpdb;
172 $condition = [
173 'post_content' => [
174 'condition' => 'LIKE',
175 'value' => '%et_pb_contact_form%',
176 'type' => '%s',
177 'or' => [
178 [
179 'post_content' => [
180 'condition' => 'LIKE',
181 'value' => '%wp:divi/contact-form%',
182 'type' => '%s',
183 ],
184 ],
185 ],
186 ],
187 'post_type' => [
188 'condition' => '<>',
189 'value' => [
190 'revision',
191 'et_pb_layout',
192 ],
193 'type' => '%s',
194 ],
195 ];
196 $order_condition = [
197 'orderby' => 'id',
198 'order' => 'desc',
199 ];
200 $where = $this->helper->load('db')->prepare_where($condition);
201 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
202 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
203 $posts = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'posts`' . $where['sql'] . $orderby . '', $where['filter']));
204
205 $content = [];
206 foreach ($posts as $key => $post) {
207 foreach ($this->get_forms($post->post_content) as $form_key => $form_value) {
208 $content[] = $this->item($form_key);
209 }
210 }
211 return $content;
212 }
213
214 // get forms
215 public function get_forms($content) {
216 $forms = [];
217
218 if ($this->divi4_form($content)) {
219 $shortcode_tags = [
220 'et_pb_contact_form',
221 ];
222 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
223 $tagnames = array_intersect($shortcode_tags, $matches[1]);
224 if (!empty($tagnames)) {
225 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
226 foreach ($shortcodes[0] as $key => $shortcode_value) {
227 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
228 preg_match_all('/admin_label="(.*?)"/', $shortcode[3], $labels);
229 if (isset($labels['1'])) {
230 foreach ($labels['1'] as $label) {
231 $forms[$label] = $label;
232 }
233 }
234 }
235 }
236 } elseif ($this->divi5_form($content)) {
237 $blocks = $this->get_forms_blocks(parse_blocks($content));
238 foreach ($blocks as $block) {
239 $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
240 if ($label) {
241 $forms[$label] = $label;
242 }
243 }
244 }
245 return $forms;
246 }
247
248 // datasets
249 public function datasets($item_id = false, $name = false) {
250 global $wpdb;
251 $datasets = [];
252 if ($item_id) {
253 $condition = [
254 'extension' => [
255 'condition' => '=',
256 'value' => 'divi',
257 'type' => '%s',
258 ],
259 'item' => [
260 'condition' => '=',
261 'value' => $item_id,
262 'type' => '%s',
263 ],
264 ];
265 $order_condition = [
266 'orderby' => 'ID',
267 'order' => 'desc',
268 ];
269 $where = $this->helper->load('db')->prepare_where($condition);
270 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
271 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
272 $entries = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'e2pdf_datasets`' . $where['sql'] . $orderby . '', $where['filter']));
273
274 if ($entries) {
275 $this->set('item', $item_id);
276 foreach ($entries as $key => $entry) {
277 $this->set('dataset', $entry->ID);
278 $entry_title = $this->render($name);
279 if (!$entry_title) {
280 $entry_title = $entry->ID;
281 }
282 $datasets[] = [
283 'key' => $entry->ID,
284 'value' => $entry_title,
285 ];
286 }
287 }
288 }
289 return $datasets;
290 }
291
292 // get dataset actions
293 public function get_dataset_actions($dataset_id = false) {
294 $dataset_id = (int) $dataset_id;
295 if (!$dataset_id) {
296 return;
297 }
298 $actions = new stdClass();
299 $actions->view = false;
300 $actions->delete = true;
301 return $actions;
302 }
303
304 // get template actions
305 public function get_template_actions($template = false) {
306 $template = (int) $template;
307 if (!$template) {
308 return;
309 }
310 $actions = new stdClass();
311 $actions->delete = true;
312 return $actions;
313 }
314
315 // get forms blocks
316 public function get_forms_blocks($blocks) {
317 $forms = [];
318 foreach ($blocks as $block) {
319 if ('divi/contact-form' === $block['blockName']) {
320 $forms[] = $block;
321 }
322 if (!empty($block['innerBlocks'])) {
323 $nested_forms = $this->get_forms_blocks($block['innerBlocks']);
324 $forms = array_merge($forms, $nested_forms);
325 }
326 }
327 return $forms;
328 }
329
330 // get fields blocks
331 public function get_fields_blocks($form) {
332 $fields = [];
333 if (!empty($form['innerBlocks'])) {
334 foreach ($form['innerBlocks'] as $block) {
335 if ('divi/contact-field' === $block['blockName']) {
336 $field_type = !empty($block['attrs']['fieldItem']['advanced']['type']['desktop']['value']) ? $block['attrs']['fieldItem']['advanced']['type']['desktop']['value'] : '';
337 $field_label = !empty($block['attrs']['fieldItem']['innerContent']['desktop']['value']) ? $block['attrs']['fieldItem']['innerContent']['desktop']['value'] : '';
338 $field_id = !empty($block['attrs']['fieldItem']['advanced']['id']['desktop']['value']) ? $block['attrs']['fieldItem']['advanced']['id']['desktop']['value'] : '';
339 $fields[] = [
340 'field_type' => $field_type,
341 'field_title' => $field_label,
342 'field_id' => $field_id,
343 ];
344 }
345 if (!empty($block['innerBlocks'])) {
346 $nested_fields = $this->get_fields_blocks($block);
347 $fields = array_merge($fields, $nested_fields);
348 }
349 }
350 }
351 return $fields;
352 }
353
354 // item
355 public function item($item_id = false) {
356 if (!$item_id && $this->get('item')) {
357 $item_id = $this->get('item');
358 }
359 $item = new stdClass();
360 if ($item_id) {
361 $item->id = (string) $item_id;
362 $item->name = $item_id;
363 $form = $this->get_form($item_id);
364 if (isset($form->ID)) {
365 $item->url = $this->helper->get_url(
366 [
367 'post' => $form->ID,
368 'action' => 'edit',
369 ], 'post.php?'
370 );
371 } else {
372 $item->url = '';
373 }
374 } else {
375 $item->id = '';
376 $item->name = '';
377 $item->url = '';
378 }
379 return $item;
380 }
381
382 // get form
383 public function get_form($item_id = false) {
384 global $wpdb;
385 $item_post = false;
386 $condition = [
387 'post_content' => [
388 'condition' => 'LIKE',
389 'value' => '%admin_label="' . $item_id . '"%',
390 'type' => '%s',
391 'or' => [
392 [
393 'post_content' => [
394 'condition' => 'LIKE',
395 'value' => '%"meta":{"adminLabel":{"desktop":{"value":"' . $item_id . '"%',
396 'type' => '%s',
397 ],
398 ],
399 ],
400 ],
401 'post_type' => [
402 'condition' => '<>',
403 'value' => [
404 'revision',
405 'et_pb_layout',
406 ],
407 'type' => '%s',
408 ],
409 ];
410
411 $order_condition = [
412 'orderby' => 'id',
413 'order' => 'desc',
414 ];
415
416 $where = $this->helper->load('db')->prepare_where($condition);
417 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
418 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
419 $posts = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'posts`' . $where['sql'] . $orderby . '', $where['filter']));
420 foreach ($posts as $post) {
421 if (in_array($item_id, $this->get_forms($post->post_content))) {
422 $item_post = $post;
423 break;
424 }
425 }
426 return $item_post;
427 }
428
429 // render
430 public function render($value, $field = [], $convert_shortcodes = true, $raw = false) {
431 $value = $this->render_shortcodes($value, $field);
432 if (!$raw) {
433 $value = $this->strip_shortcodes($value);
434 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
435 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
436 }
437 return $value;
438 }
439
440 // render shortcodes
441 public function render_shortcodes($value, $field = []) {
442 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
443 if ($this->verify()) {
444 if (false !== strpos($value, '[')) {
445 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
446 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
447 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
448 }
449 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
450 if (!empty($this->get('cached_meta'))) {
451 $value = $this->helper->load('convert')->stritr($value, $this->get('cached_meta'));
452 }
453 $value = $this->helper->load('field')->render(
454 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
455 $this,
456 $field
457 );
458 }
459 return apply_filters(
460 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
461 );
462 }
463
464 // strip shortcodes
465 public function strip_shortcodes($value) {
466 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
467 $value = preg_replace('~%%[^%%]*%%~', '', $value);
468 return $value;
469 }
470
471 // convert shortcodes
472 public function convert_shortcodes($value, $to = false, $html = false) {
473 if ($value) {
474 if ($to) {
475 $value = stripslashes_deep($value);
476 $value = str_replace('&#91;', '[', $value);
477 if (!$html) {
478 $value = wp_specialchars_decode($value, ENT_QUOTES);
479 }
480 } else {
481 $value = str_replace('[', '&#91;', $value);
482 }
483 }
484 return $value;
485 }
486
487 // auto
488 public function auto() {
489
490 $elements = [];
491
492 if ($this->get('item')) {
493 $post = $this->get_form($this->get('item'));
494 if ($post && isset($post->post_content)) {
495
496 $source = '';
497 $content = $post->post_content;
498
499 if ($this->divi4_form($content)) {
500 $shortcode_tags = [
501 'et_pb_contact_form',
502 ];
503 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
504 $tagnames = array_intersect($shortcode_tags, $matches[1]);
505 if (!empty($tagnames)) {
506 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
507 foreach ($shortcodes[0] as $key => $shortcode_value) {
508 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
509 $atts = shortcode_parse_atts($shortcode[3]);
510 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
511 $this->divi4_load();
512 $source = do_shortcode($shortcode_value);
513 }
514 }
515 }
516 } elseif ($this->divi5_form($content)) {
517 $blocks = $this->get_forms_blocks(parse_blocks($content));
518 foreach ($blocks as $key => $block) {
519 $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
520 if ($label == $this->get('item')) {
521 $source = render_block($block);
522 break;
523 }
524 }
525 }
526
527 if ($source) {
528 $dom = new DOMDocument();
529 $html = $this->helper->load('convert')->load_html($source, $dom, true);
530 if ($html) {
531 $xml = $this->helper->load('xml');
532 $xpath = new DomXPath($dom);
533
534 // remove parent by class
535 $remove_parent_by_class = [
536 'et_pb_contact_captcha_question',
537 ];
538 foreach ($remove_parent_by_class as $key => $class) {
539 $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
540 foreach ($xml_elements as $xml_element) {
541 $xml_element->parentNode->removeChild($xml_element);
542 }
543 }
544
545 $blocks = $xpath->query("//*[contains(@class, 'et_pb_contact_field')]");
546 foreach ($blocks as $element) {
547
548 if ($xml->get_node_value($element, 'data-type') == 'radio') {
549
550 $label = $xpath->query('.//label', $element)->item(0);
551 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
552
553 $name = '';
554 if ($check_handler) {
555 $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
556 }
557
558 if ($label) {
559 $elements[] = $this->auto_field(
560 $element,
561 [
562 'type' => 'e2pdf-html',
563 'block' => true,
564 'class' => $xml->get_node_value($element, 'class'),
565 'properties' => [
566 'top' => '20',
567 'left' => '20',
568 'right' => '20',
569 'width' => '100%',
570 'height' => 'auto',
571 'value' => $label->nodeValue,
572 ],
573 ]
574 );
575 }
576
577 $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_radio')]", $element);
578 foreach ($fields as $field) {
579 $radio_label = $xpath->query('.//label', $field)->item(0);
580 $radio = $xpath->query(".//input[@type='radio']", $field)->item(0);
581
582 if (
583 $radio->attributes->getNamedItem('data-original_id') &&
584 $radio->attributes->getNamedItem('value')
585 ) {
586 $elements[] = $this->auto_field(
587 $field,
588 [
589 'type' => 'e2pdf-radio',
590 'class' => $xml->get_node_value($field, 'class'),
591 'properties' => [
592 'top' => '5',
593 'width' => 'auto',
594 'height' => 'auto',
595 'value' => '%%' . $xml->get_node_value($radio, 'data-original_id') . '%%',
596 'option' => $xml->get_node_value($radio, 'value'),
597 'group' => '%%' . $xml->get_node_value($radio, 'data-original_id') . '%%',
598 ],
599 ]
600 );
601 }
602
603 if ($radio_label) {
604 $elements[] = $this->auto_field(
605 $radio,
606 [
607 'type' => 'e2pdf-html',
608 'float' => true,
609 'class' => $xml->get_node_value($radio, 'class'),
610 'properties' => [
611 'left' => '5',
612 'width' => '100%',
613 'height' => 'auto',
614 'value' => $radio_label->nodeValue,
615 ],
616 ]
617 );
618 }
619 }
620 } elseif ($xml->get_node_value($element, 'data-type') == 'checkbox') {
621 $label = $xpath->query('.//label', $element)->item(0);
622 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
623
624 $name = '';
625 if ($check_handler) {
626 $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
627 }
628
629 if ($label) {
630 $elements[] = $this->auto_field(
631 $element,
632 [
633 'type' => 'e2pdf-html',
634 'block' => true,
635 'class' => $xml->get_node_value($element, 'class'),
636 'properties' => [
637 'top' => '20',
638 'left' => '20',
639 'right' => '20',
640 'width' => '100%',
641 'height' => 'auto',
642 'value' => $label->nodeValue,
643 ],
644 ]
645 );
646 }
647
648 $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_checkbox')]", $element);
649 foreach ($fields as $field) {
650 $checkbox_label = $xpath->query('.//label', $field)->item(0);
651 $checkbox = $xpath->query(".//input[@type='checkbox']", $field)->item(0);
652
653 $elements[] = $this->auto_field(
654 $field,
655 [
656 'type' => 'e2pdf-checkbox',
657 'class' => $xml->get_node_value($field, 'class'),
658 'properties' => [
659 'top' => '5',
660 'width' => 'auto',
661 'height' => 'auto',
662 'value' => $name,
663 'option' => $xml->get_node_value($checkbox, 'value'),
664 ],
665 ]
666 );
667
668 if ($checkbox_label) {
669 $elements[] = $this->auto_field(
670 $checkbox,
671 [
672 'type' => 'e2pdf-html',
673 'float' => true,
674 'class' => $xml->get_node_value($checkbox, 'class'),
675 'properties' => [
676 'left' => '5',
677 'width' => '100%',
678 'height' => 'auto',
679 'value' => $checkbox_label->nodeValue,
680 ],
681 ]
682 );
683 }
684 }
685 } else {
686 $label = $xpath->query('.//label', $element)->item(0);
687 $input_text = $xpath->query(".//input[@type='text']", $element)->item(0);
688 $select = $xpath->query('.//select', $element)->item(0);
689 $textarea = $xpath->query('.//textarea', $element)->item(0);
690
691 if ($label && ($input_text || $select || $textarea)) {
692 $elements[] = $this->auto_field(
693 $element,
694 [
695 'type' => 'e2pdf-html',
696 'block' => true,
697 'class' => $xml->get_node_value($element, 'class'),
698 'properties' => [
699 'top' => '20',
700 'left' => '20',
701 'right' => '20',
702 'width' => '100%',
703 'height' => 'auto',
704 'value' => $label->nodeValue,
705 ],
706 ]
707 );
708 }
709
710 if ($input_text) {
711 $elements[] = $this->auto_field(
712 $input_text,
713 [
714 'type' => 'e2pdf-input',
715 'class' => $xml->get_node_value($input_text, 'class'),
716 'properties' => [
717 'top' => '5',
718 'width' => '100%',
719 'height' => 'auto',
720 'value' => '%%' . $xml->get_node_value($input_text, 'data-original_id') . '%%',
721 ],
722 ]
723 );
724 } elseif ($select) {
725 $options_tmp = [];
726 $options = $xpath->query('.//option', $select);
727 foreach ($options as $option) {
728 $options_tmp[] = $xml->get_node_value($option, 'value');
729 }
730
731 $elements[] = $this->auto_field(
732 $select,
733 [
734 'type' => 'e2pdf-select',
735 'class' => $xml->get_node_value($select, 'class'),
736 'properties' => [
737 'top' => '5',
738 'width' => '100%',
739 'height' => 'auto',
740 'options' => implode("\n", $options_tmp),
741 'value' => '%%' . $xml->get_node_value($select, 'data-original_id') . '%%',
742 ],
743 ]
744 );
745 } elseif ($textarea) {
746 $elements[] = $this->auto_field(
747 $textarea,
748 [
749 'type' => 'e2pdf-textarea',
750 'class' => $xml->get_node_value($textarea, 'class'),
751 'properties' => [
752 'top' => '5',
753 'width' => '100%',
754 'height' => '150',
755 'value' => '%%' . $xml->get_node_value($textarea, 'data-original_id') . '%%',
756 ],
757 ]
758 );
759 }
760 }
761 }
762 }
763 }
764 }
765 }
766 return [
767 'page' => [
768 'bottom' => '20',
769 'top' => '20',
770 'right' => '20',
771 'left' => '20',
772 ],
773 'elements' => $elements,
774 ];
775 }
776
777 // auto map
778 public function auto_map($name = false) {
779 $item = $this->get('item');
780 if ($item) {
781 $post = $this->get_form($item);
782 if ($post && isset($post->post_content)) {
783 $content = $post->post_content;
784 if ($this->divi4_form($content)) {
785 $shortcode_tags = [
786 'et_pb_contact_form',
787 ];
788 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
789 $tagnames = array_intersect($shortcode_tags, $matches[1]);
790 if (!empty($tagnames)) {
791 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
792 foreach ($shortcodes[0] as $key => $shortcode_value) {
793 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
794 $atts = shortcode_parse_atts($shortcode[3]);
795 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
796 $field_content = $shortcode_value;
797 $field_shortcode_tags = [
798 'et_pb_contact_field',
799 ];
800 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $field_content, $field_matches);
801 $field_tagnames = array_intersect($field_shortcode_tags, $field_matches[1]);
802 if (!empty($field_tagnames)) {
803 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($field_tagnames) . '/', $field_content, $field_shortcodes);
804 foreach ($field_shortcodes[0] as $field_key => $field_shortcode_value) {
805 $field_shortcode = [];
806 $field_shortcode[3] = $field_shortcodes[3][$field_key];
807 $field = shortcode_parse_atts($field_shortcode[3]);
808 if (isset($field['field_title']) && isset($field['field_id'])) {
809 if ($field['field_title'] == $name || $field['field_id'] == $name) {
810 return '%%' . strtolower($field['field_id']) . '%%';
811 }
812 }
813 }
814 }
815 }
816 }
817 }
818 } elseif ($this->divi5_form($content)) {
819 $blocks = $this->get_forms_blocks(parse_blocks($content));
820 foreach ($blocks as $key => $block) {
821 $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
822 if ($label == $this->get('item')) {
823 $fields = $this->get_fields_blocks($block);
824 if (!empty($fields)) {
825 foreach ($fields as $field) {
826 if (isset($field['field_title']) && isset($field['field_id'])) {
827 if ($field['field_title'] == $name || $field['field_id'] == $name) {
828 return '%%' . strtolower($field['field_id']) . '%%';
829 }
830 }
831 }
832 }
833 }
834 break;
835 }
836 }
837 }
838 }
839
840 return false;
841 }
842
843 // auto field
844 public function auto_field($field = false, $element = []) {
845
846 if (!$field) {
847 return false;
848 }
849
850 if (!isset($element['block'])) {
851 $element['block'] = false;
852 }
853
854 if (!isset($element['float'])) {
855 $element['float'] = false;
856 }
857
858 $classes = [];
859 if (isset($element['class']) && $element['class']) {
860 $classes = explode(' ', $element['class']);
861 unset($element['class']);
862 }
863
864 $float_classes = [
865 'et_pb_contact_field_half',
866 ];
867 $array_intersect = array_intersect($classes, $float_classes);
868
869 if (!empty($array_intersect) && $element['block']) {
870 $element['float'] = true;
871 };
872
873 $primary_class = false;
874 if (!empty($array_intersect)) {
875 $primary_class = end($array_intersect);
876 }
877
878 if ($element['block']) {
879 switch ($primary_class) {
880 case 'et_pb_contact_field_half':
881 $element['width'] = '50%';
882 break;
883 default:
884 break;
885 }
886 }
887
888 return $element;
889 }
890
891 // verify
892 public function verify() {
893 if ($this->get('cached_entry')) {
894 return true;
895 }
896 return false;
897 }
898
899 // visual mapper
900 public function visual_mapper() {
901
902 $source = '';
903 $html = '';
904
905 if ($this->get('item')) {
906 $post = $this->get_form($this->get('item'));
907 if ($post && isset($post->post_content)) {
908 $content = $post->post_content;
909
910 if ($this->divi4_form($content)) {
911 $shortcode_tags = [
912 'et_pb_contact_form',
913 ];
914 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
915 $tagnames = array_intersect($shortcode_tags, $matches[1]);
916 if (!empty($tagnames)) {
917 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
918 foreach ($shortcodes[0] as $key => $shortcode_value) {
919 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
920 $atts = shortcode_parse_atts($shortcode[3]);
921 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
922 $this->divi4_load();
923 $source = do_shortcode($shortcode_value);
924 }
925 }
926 }
927 } elseif ($this->divi5_form($content)) {
928 $blocks = $this->get_forms_blocks(parse_blocks($content));
929 foreach ($blocks as $key => $block) {
930 $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
931 if ($label == $this->get('item')) {
932 $source = render_block($block);
933 break;
934 }
935 }
936 }
937
938 if ($source) {
939 $dom = new DOMDocument();
940 $html = $this->helper->load('convert')->load_html($source, $dom, true);
941 }
942 if (!$source) {
943 return '<div class="e2pdf-vm-error">' . __("The form source is empty or doesn't exist", 'e2pdf') . '</div>';
944 } elseif (!$html) {
945 return '<div class="e2pdf-vm-error">' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '</div>';
946 } else {
947 $xml = $this->helper->load('xml');
948 $xpath = new DomXPath($dom);
949
950 // remove by name
951 $remove_by_name = [
952 'et_pb_contactform_submit_0',
953 '_wpnonce-et-pb-contact-form-submitted-0',
954 '_wp_http_referer',
955 ];
956 foreach ($remove_by_name as $key => $name) {
957 $xml_elements = $xpath->query('//*[@name="' . $name . '"]');
958 foreach ($xml_elements as $xml_element) {
959 $xml_element->parentNode->removeChild($xml_element);
960 }
961 }
962
963 // replace names
964 $fields = $xpath->query("//*[contains(@name, 'et_pb_contact_')]");
965 foreach ($fields as $xml_element) {
966 $xml->set_node_value($xml_element, 'name', '%%' . $xml->get_node_value($xml_element, 'data-original_id') . '%%');
967 }
968
969 $checkboxes = $xpath->query("//*[contains(@class, 'et_pb_contact_field') and @data-type='checkbox']");
970 foreach ($checkboxes as $xml_element) {
971 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $xml_element)->item(0);
972 $name = '';
973 if ($check_handler) {
974 $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
975 }
976 $checks = $xpath->query(".//input[@type='checkbox']", $xml_element);
977 foreach ($checks as $check) {
978 $xml->set_node_value($check, 'name', $name);
979 }
980 }
981
982 // remove by class
983 $remove_by_class = [
984 'et_pb_contact_submit',
985 'et_pb_contactform_validate_field',
986 'et_pb_checkbox_handle',
987 ];
988 foreach ($remove_by_class as $key => $class) {
989 $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]");
990 foreach ($xml_elements as $xml_element) {
991 $xml_element->parentNode->removeChild($xml_element);
992 }
993 }
994
995 // remove parent by class
996 $remove_parent_by_class = [
997 'et_pb_contact_captcha_question',
998 ];
999 foreach ($remove_parent_by_class as $key => $class) {
1000 $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
1001 foreach ($xml_elements as $xml_element) {
1002 $xml_element->parentNode->removeChild($xml_element);
1003 }
1004 }
1005 }
1006
1007 if (defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD')) {
1008 return str_replace(['<html>', '</html>'], '', $dom->saveHTML());
1009 } else {
1010 return $dom->saveHTML();
1011 }
1012 }
1013 }
1014
1015 return false;
1016 }
1017
1018 // filter mail
1019 public function filter_wp_mail($args) {
1020
1021 if (!$this->get('process')) {
1022 return $args;
1023 }
1024
1025 if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1026 $args['message'] = preg_replace('/(\{)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\}]*?)(\})/', '[$2]', $args['message']);
1027 }
1028
1029 if (isset($args['message'])) {
1030 if (false !== strpos($args['message'], '[')) {
1031 $shortcode_tags = [
1032 'e2pdf-download',
1033 'e2pdf-save',
1034 'e2pdf-attachment',
1035 'e2pdf-adobesign',
1036 'e2pdf-zapier',
1037 ];
1038 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $args['message'], $matches);
1039 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1040 if (!empty($tagnames)) {
1041 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $args['message'], $shortcodes);
1042 foreach ($shortcodes[0] as $key => $shortcode_value) {
1043 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1044 $atts = shortcode_parse_atts($shortcode[3]);
1045 if (!isset($atts['apply'])) {
1046 $shortcode[3] .= ' apply="true"';
1047 }
1048 if (!isset($atts['filter'])) {
1049 $shortcode[3] .= ' filter="true"';
1050 }
1051 $file = false;
1052 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1053 $file = do_shortcode_tag($shortcode);
1054 if ($file) {
1055 $tmp = false;
1056 if (substr($file, 0, 4) === 'tmp:') {
1057 $file = substr($file, 4);
1058 $tmp = true;
1059 }
1060 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
1061 if ($tmp) {
1062 $this->helper->add('divi_attachments', $file);
1063 }
1064 } else {
1065 $this->helper->add('divi_attachments', $file);
1066 }
1067 $args['attachments'][] = $file;
1068 if (!has_action('wp_mail_succeeded', [$this, 'filter_wp_mail_attachments'])) {
1069 add_action('wp_mail_succeeded', [$this, 'filter_wp_mail_attachments']);
1070 }
1071 if (!has_action('wp_mail_failed', [$this, 'filter_wp_mail_attachments'])) {
1072 add_action('wp_mail_failed', [$this, 'filter_wp_mail_attachments']);
1073 }
1074 }
1075 $args['message'] = str_replace($shortcode_value, '', $args['message']);
1076 } else {
1077 if (is_array($args['headers']) && !in_array('Content-Type: text/html; charset=UTF-8', $args['headers'])) {
1078 $args['headers'][] = 'Content-Type: text/html; charset=UTF-8';
1079 }
1080 $args['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $args['message']);
1081 $args['message'] = str_replace("\r\n", '<br/>', $args['message']);
1082 }
1083 }
1084 }
1085 }
1086 }
1087 $wp_mail = [
1088 'to' => $args['to'],
1089 'subject' => $args['subject'],
1090 'message' => $args['message'],
1091 'headers' => $args['headers'],
1092 'attachments' => $args['attachments'],
1093 ];
1094 return $wp_mail;
1095 }
1096
1097 public function filter_wp_mail_attachments() {
1098 $files = $this->helper->get('divi_attachments');
1099 if (is_array($files) && !empty($files)) {
1100 foreach ($files as $key => $file) {
1101 $this->helper->delete_dir(dirname($file) . '/');
1102 }
1103 $this->helper->deset('divi_attachments');
1104 }
1105 }
1106
1107 // load actions
1108 public function load_actions() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
1109 add_action('et_pb_contact_form_submit', [$this, 'action_et_pb_contact_form_submit'], 11);
1110 }
1111
1112 public function action_et_pb_contact_form_submit() {
1113 if ($this->divi5()) {
1114 add_filter('wp_mail', [$this, 'filter_wp_mail'], 1000);
1115 }
1116 }
1117
1118 // load filters
1119 public function load_filters() {
1120 // Divi Contact Form Helper Confirmation Email compatibility fix
1121 if (defined('PWH_DCFH_PLUGIN_FILE') || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1122 add_filter('et_pb_module_shortcode_attributes', [$this, 'filter_et_pb_module_shortcode_attributes'], 9, 5);
1123 } else {
1124 add_filter('et_pb_module_shortcode_attributes', [$this, 'filter_et_pb_module_shortcode_attributes'], 30, 5);
1125 }
1126 add_filter('et_module_shortcode_output', [$this, 'filter_et_module_shortcode_output'], 30, 3);
1127
1128 // Divi 5
1129 add_filter('divi_module_library_register_module_attrs', [$this, 'filter_divi_module_library_register_module_attrs'], 11, 2);
1130 }
1131
1132 public function filter_divi_module_library_register_module_attrs($module_attrs, $filter_args) {
1133
1134 if ('divi/contact-form' !== $filter_args['name']) {
1135 return $module_attrs;
1136 }
1137
1138 if (!isset($_SERVER['REQUEST_METHOD']) || 'POST' !== $_SERVER['REQUEST_METHOD']) {
1139 return $module_attrs;
1140 }
1141
1142 $module_id = !empty($filter_args['id']) ? $filter_args['id'] : '';
1143 $store_instance = !empty($filter_args['storeInstance']) ? $filter_args['storeInstance'] : 0;
1144
1145 $module = \ET\Builder\FrontEnd\BlockParser\BlockParserStore::get($module_id, $store_instance);
1146
1147 if (!$module) {
1148 return $module_attrs;
1149 }
1150
1151 if (
1152 !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils') ||
1153 !method_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils', 'get_unique_id') ||
1154 !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormHandler') ||
1155 !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule') ||
1156 !method_exists('\ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule', 'get_field_unique_id')
1157 ) {
1158 return $module_attrs;
1159 }
1160
1161 $unique_id = \ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils::get_unique_id($module_attrs, (array) $module);
1162
1163 $submitted = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $unique_id]);
1164 if (!$submitted) {
1165 return $module_attrs;
1166 }
1167
1168 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce-et-pb-contact-form-submitted-' . $unique_id])), 'et-pb-contact-form-submit-' . $unique_id)) {
1169 return $module_attrs;
1170 }
1171
1172 $use_basic_captcha = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['useBasicCaptcha'] ?? 'on';
1173 $use_spam_service = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['enabled'] ?? 'off';
1174
1175 if ('on' === $use_basic_captcha && 'off' === $use_spam_service) {
1176 $captcha_answer = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_' . $unique_id] ?? ''));
1177 $first_digit = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_first_digit_' . $unique_id] ?? ''));
1178 $second_digit = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_second_digit_' . $unique_id] ?? ''));
1179
1180 if (!$captcha_answer || ((int) $first_digit + (int) $second_digit) !== (int) $captcha_answer) {
1181 return $module_attrs;
1182 }
1183 } elseif ('on' === $use_spam_service) {
1184 $provider = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['provider'] ?? 'recaptcha';
1185 $account = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['account'] ?? '';
1186 $min_score = (float) ($module_attrs['module']['advanced']['spamProtection']['desktop']['value']['minScore'] ?? 0.0);
1187
1188 if (empty($_POST['token']) || !SpamProtectionService::validate_token($provider, $account, $min_score)) {
1189 return $module_attrs;
1190 }
1191 }
1192
1193 $messages = [
1194 'successMessage' => isset($module_attrs['module']['advanced']['successMessage']['desktop']['value']) ? $module_attrs['module']['advanced']['successMessage']['desktop']['value'] : '',
1195 ];
1196 if (defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1197 $send_admin_email = isset($module_attrs['adminEmail']['advanced']['adminEmailEnabled']['desktop']['value']) ? $module_attrs['adminEmail']['advanced']['adminEmailEnabled']['desktop']['value'] : 'on';
1198 if ($send_admin_email === 'on') {
1199 $admin_email_rich_text = isset($module_attrs['adminEmail']['advanced']['emailRichTextEnabled']['desktop']['value']) ? $module_attrs['adminEmail']['advanced']['emailRichTextEnabled']['desktop']['value'] : 'off';
1200 if ($admin_email_rich_text === 'on') {
1201 $messages['adminEmailRichTextMessage'] = isset($module_attrs['adminEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) ? $this->decode_shortcodes($module_attrs['adminEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) : '';
1202 } else {
1203 $messages['email'] = isset($module_attrs['email']['innerContent']['desktop']['value']) ? $module_attrs['email']['innerContent']['desktop']['value'] : '';
1204 }
1205 }
1206 $send_user_email = isset($module_attrs['confirmationEmail']['advanced']['sendEmailToSubmitter']['desktop']['value']) ? $module_attrs['confirmationEmail']['advanced']['sendEmailToSubmitter']['desktop']['value'] : 'off';
1207 if ($send_user_email === 'on') {
1208 $user_email_rich_text = isset($module_attrs['confirmationEmail']['advanced']['emailMessageRichTextEnabled']['desktop']['value']) ? $module_attrs['confirmationEmail']['advanced']['emailMessageRichTextEnabled']['desktop']['value'] : 'off';
1209 if ($user_email_rich_text === 'on') {
1210 $messages['confirmationEmailRichTextMessage'] = isset($module_attrs['confirmationEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) ? $this->decode_shortcodes($module_attrs['confirmationEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) : '';
1211 } else {
1212 $messages['confirmationEmailMessage'] = isset($module_attrs['confirmationEmail']['innerContent']['emailMessage']['desktop']['value']) ? $module_attrs['confirmationEmail']['innerContent']['emailMessage']['desktop']['value'] : '';
1213 }
1214 }
1215 } else {
1216 $messages['email'] = isset($module_attrs['email']['innerContent']['desktop']['value']) ? $module_attrs['email']['innerContent']['desktop']['value'] : '';
1217 }
1218
1219 foreach ($messages as $message_key => $message) {
1220 if (false !== strpos($message, '[')) {
1221 $shortcode_tags = [
1222 'e2pdf-download',
1223 'e2pdf-save',
1224 'e2pdf-view',
1225 'e2pdf-adobesign',
1226 'e2pdf-zapier',
1227 'e2pdf-attachment',
1228 ];
1229 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1230 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1231 if (!empty($tagnames)) {
1232 $this->set('process', true);
1233 } else {
1234 unset($messages[$message_key]);
1235 }
1236 } else {
1237 unset($messages[$message_key]);
1238 }
1239 }
1240
1241 $_POST = $this->sanitize($_POST);
1242
1243 if (!$this->get('process')) {
1244 return $module_attrs;
1245 }
1246
1247 $fields_raw = [];
1248
1249 $fields = \ET\Builder\FrontEnd\BlockParser\BlockParserStore::get_children($module_id, $store_instance);
1250 foreach ($fields as $field) {
1251 // Skip nested modules (Text, Button, Divider, etc.), only process Contact Field modules.
1252 if ('divi/contact-field' !== $field->blockName) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- This is a property of the WP Core class.
1253 continue;
1254 }
1255
1256 // Use pre-filtered field attributes if provided, otherwise use field attributes from storage.
1257 $field_attrs = $field->attrs;
1258 $field_unique_id = \ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule::get_field_unique_id($field->id, $store_instance);
1259 $field_type = $field_attrs['fieldItem']['advanced']['type']['desktop']['value'] ?? 'input';
1260 $field_id = $field_attrs['fieldItem']['advanced']['id']['desktop']['value'] ?? '';
1261 $field_key = strtolower($field_id);
1262 $field_label = $field_attrs['fieldItem']['innerContent']['desktop']['value'] ?? '';
1263
1264 if (!$field_label) {
1265 $field_label = __('New Field', 'et_builder_5');
1266 }
1267
1268 $fields_raw[$field_key]['id'] = $field_id;
1269 $fields_raw[$field_key]['label'] = $field_label;
1270 $fields_raw[$field_key]['type'] = $field_type;
1271 $fields_raw[$field_key]['allowedSymbols'] = $field_attrs['fieldItem']['advanced']['allowedSymbols']['desktop']['value'] ?? '';
1272 $fields_raw[$field_key]['maxLength'] = $field_attrs['fieldItem']['advanced']['maxLength']['desktop']['value'] ?? '';
1273 $fields_raw[$field_key]['minLength'] = $field_attrs['fieldItem']['advanced']['minLength']['desktop']['value'] ?? '';
1274 $fields_raw[$field_key]['isRequired'] = 'on' === ($field_attrs['fieldItem']['advanced']['required']['desktop']['value'] ?? 'on');
1275
1276 $fields_raw[$field_key]['conditionalLogic'] = [
1277 'enabled' => 'on' === ($field_attrs['conditionalLogic']['advanced']['enable']['desktop']['value'] ?? 'off'),
1278 'matchAll' => 'on' === ($field_attrs['conditionalLogic']['advanced']['relation']['desktop']['value'] ?? 'off'),
1279 'rules' => $field_attrs['conditionalLogic']['innerContent']['desktop']['value'] ?? [],
1280 ];
1281
1282 if ('text' === $field_type) {
1283 $fields_raw[$field_key]['value'] = isset($_POST[$field_unique_id]) ? sanitize_textarea_field(wp_unslash($_POST[$field_unique_id])) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1284 } elseif ('checkbox' === $field_type) {
1285 // For checkbox fields, decode and sanitize immediately, store as array.
1286 $raw_value = isset($_POST[$field_unique_id]) ? wp_unslash($_POST[$field_unique_id]) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1287
1288 if ('' !== $raw_value) {
1289 // Split the URL-encoded comma-separated values.
1290 $parts = explode(',', $raw_value);
1291 $parts = array_map('urldecode', $parts);
1292 $parts = array_map('sanitize_text_field', $parts);
1293 $parts = array_map('trim', $parts);
1294 $fields_raw[$field_key]['value'] = $parts;
1295 } else {
1296 $fields_raw[$field_key]['value'] = [];
1297 }
1298 } else {
1299 $fields_raw[$field_key]['value'] = isset($_POST[$field_unique_id]) ? sanitize_text_field(wp_unslash($_POST[$field_unique_id])) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1300 }
1301
1302 switch ($field_type) {
1303 case 'checkbox':
1304 $options = $field_attrs['fieldItem']['advanced']['checkboxOptions']['desktop']['value'] ?? [];
1305 $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1306 function ($option) {
1307 return wp_strip_all_tags($option['value'] ?? '');
1308 },
1309 $options
1310 ) : [];
1311 break;
1312 case 'radio':
1313 $options = $field_attrs['fieldItem']['advanced']['radioOptions']['desktop']['value'] ?? [];
1314 $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1315 function ($option) {
1316 return sanitize_text_field($option['value'] ?? '');
1317 },
1318 $options
1319 ) : [];
1320 break;
1321 case 'select':
1322 $options = $field_attrs['fieldItem']['advanced']['selectOptions']['desktop']['value'] ?? [];
1323 $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1324 function ($option) {
1325 return sanitize_text_field($option['value'] ?? '');
1326 },
1327 $options
1328 ) : [];
1329 break;
1330 default:
1331 $fields_raw[$field_key]['options'] = [];
1332 break;
1333 }
1334 }
1335
1336 $refClass = new ReflectionClass('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormHandler');
1337 $obj = $refClass->newInstanceWithoutConstructor();
1338
1339 try {
1340 $refFields = $refClass->getProperty('_fields_raw');
1341 $refFields->setAccessible(true);
1342 $refFields->setValue($obj, $fields_raw);
1343
1344 $refError = $refClass->getProperty('_error');
1345 $refError->setAccessible(true);
1346 $refError->setValue($obj, new WP_Error());
1347 } catch (\ReflectionException $e) {
1348 return $module_attrs;
1349 }
1350
1351 $obj->validate_fields();
1352
1353 $errorObj = $refError->getValue($obj);
1354
1355 if ($errorObj->has_errors()) {
1356 return $module_attrs;
1357 }
1358
1359 $fields_raw = $refFields->getValue($obj);
1360
1361 $item = !empty($module_attrs['module']['meta']['adminLabel']['desktop']['value']) ? $module_attrs['module']['meta']['adminLabel']['desktop']['value'] : '';
1362 if (!$item) {
1363 return $module_attrs;
1364 }
1365
1366 $data = [
1367 'ET_CORE_VERSION' => '5',
1368 ];
1369 if (!empty($fields_raw) && is_array($fields_raw)) {
1370 foreach ($fields_raw as $field_key => $field_raw) {
1371 if (!empty($field_raw['value'])) {
1372 $data[$field_key] = $field_raw['value'];
1373 }
1374 }
1375 }
1376 if (empty($data['_wp_http_referer']) && !empty($_POST['_wp_http_referer'])) {
1377 $data['_wp_http_referer'] = esc_url_raw($_POST['_wp_http_referer']);
1378 }
1379
1380 $entry = new Model_E2pdf_Dataset();
1381 $entry->set('extension', 'divi');
1382 $entry->set('item', $item);
1383 $entry->set('entry', $data);
1384 $dataset = $entry->save();
1385
1386 foreach ($messages as $message_key => $message) {
1387 if (false !== strpos($message, '[')) {
1388 $shortcode_tags = [
1389 'e2pdf-download',
1390 'e2pdf-save',
1391 'e2pdf-view',
1392 'e2pdf-adobesign',
1393 'e2pdf-zapier',
1394 'e2pdf-attachment',
1395 ];
1396 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1397 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1398 if (!empty($tagnames)) {
1399 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
1400 foreach ($shortcodes[0] as $key => $shortcode_value) {
1401 $item = false;
1402 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1403 $atts = shortcode_parse_atts($shortcode[3]);
1404
1405 if (!isset($atts['dataset']) && isset($atts['id'])) {
1406 $template = new Model_E2pdf_Template();
1407 $template->load($atts['id']);
1408 if ($template->get('extension') === 'divi') {
1409 $item = $template->get('item');
1410 $atts['dataset'] = $dataset;
1411 $shortcode[3] .= ' dataset="' . $dataset . '"';
1412 }
1413 }
1414 if (!isset($atts['apply'])) {
1415 $shortcode[3] .= ' apply="true"';
1416 }
1417 if (!isset($atts['iframe_download'])) {
1418 $shortcode[3] .= ' iframe_download="true"';
1419 }
1420 switch ($message_key) {
1421 case 'successMessage':
1422 $module_attrs['module']['advanced']['successMessage']['desktop']['value'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $module_attrs['module']['advanced']['successMessage']['desktop']['value']);
1423 break;
1424 case 'email':
1425 $module_attrs['email']['innerContent']['desktop']['value'] = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $module_attrs['email']['innerContent']['desktop']['value']);
1426 break;
1427 case 'adminEmailRichTextMessage':
1428 case 'confirmationEmailMessage':
1429 case 'confirmationEmailRichTextMessage':
1430 $messages[$message_key] = str_replace($shortcode_value, '{' . $shortcode[2] . $shortcode[3] . '}', $messages[$message_key]);
1431 break;
1432 default:
1433 break;
1434 }
1435 }
1436 }
1437 }
1438 }
1439
1440 foreach ($messages as $message_key => $message) {
1441 switch ($message_key) {
1442 case 'adminEmailRichTextMessage':
1443 $class = '\KS_PAC_DCFH\Frontend\Controllers\ContactFormDataRepository';
1444 if (class_exists($class) && method_exists($class, 'instance')) {
1445 $contact_form_rep = $class::instance();
1446 $contact_form_rep->admin_message = $messages[$message_key];
1447 }
1448 break;
1449 case 'confirmationEmailMessage':
1450 case 'confirmationEmailRichTextMessage':
1451 $class = '\KS_PAC_DCFH\Frontend\Services\ConfirmationEmailService';
1452 if (class_exists($class) && method_exists($class, 'instance')) {
1453 $contact_form_rep = $class::instance();
1454 $contact_form_rep->email_message = $messages[$message_key];
1455 }
1456 break;
1457 default:
1458 break;
1459 }
1460 }
1461 return $module_attrs;
1462 }
1463
1464 // filter shortcode attributes
1465 public function filter_et_pb_module_shortcode_attributes($props, $attrs, $render_slug, $address, $content) {
1466
1467 if ($render_slug !== 'et_pb_contact_form') {
1468 return $props;
1469 }
1470
1471 $et_contact_proccess = array_search('et_contact_proccess', (array) $_POST);
1472 if ($et_contact_proccess === false) {
1473 return $props;
1474 }
1475
1476 $success_message = isset($props['success_message']) ? $this->decode_shortcodes($props['success_message'], true) : '';
1477
1478 if (false !== strpos($success_message, '[')) {
1479 $shortcode_tags = [
1480 'e2pdf-download',
1481 'e2pdf-save',
1482 'e2pdf-view',
1483 'e2pdf-adobesign',
1484 'e2pdf-zapier',
1485 ];
1486 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1487 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1488 if (!empty($tagnames)) {
1489 $this->set('process', true);
1490 } else {
1491 $success_message = '';
1492 }
1493 }
1494
1495 $admin_email_rich_text = isset($props['use_custom_message_richtext']) && $props['use_custom_message_richtext'] == 'on' && isset($props['custom_message_richtext']) ? true : false;
1496 if ($admin_email_rich_text) {
1497 $admin_email_message = $this->decode_shortcodes($props['custom_message_richtext']);
1498 } else {
1499 $admin_email_message = isset($props['custom_message']) ? $this->decode_shortcodes($props['custom_message']) : '';
1500 }
1501 if (false !== strpos($admin_email_message, '[')) {
1502 $shortcode_tags = [
1503 'e2pdf-download',
1504 'e2pdf-save',
1505 'e2pdf-attachment',
1506 'e2pdf-adobesign',
1507 'e2pdf-zapier',
1508 ];
1509 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $admin_email_message, $matches);
1510 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1511 if (!empty($tagnames)) {
1512 $this->set('process', true);
1513 } else {
1514 $admin_email_message = '';
1515 }
1516 }
1517
1518 /* Divi Contact Form Helper Confirmation Email and Confirmation Email RichText */
1519 $user_email_rich_text = isset($props['use_confirmation_email']) && $props['use_confirmation_email'] == 'on' && isset($props['use_confirmation_message_richtext']) && $props['use_confirmation_message_richtext'] == 'on' && isset($props['confirmation_message_richtext']) ? true : false;
1520 if ($user_email_rich_text) {
1521 $user_email_message = $this->decode_shortcodes($props['confirmation_message_richtext']);
1522 } else {
1523 $user_email_message = isset($props['use_confirmation_email']) && $props['use_confirmation_email'] == 'on' && isset($props['confirmation_email_message']) ? $this->decode_shortcodes($props['confirmation_email_message']) : '';
1524 }
1525 if (false !== strpos($user_email_message, '[')) {
1526 $shortcode_tags = [
1527 'e2pdf-download',
1528 'e2pdf-save',
1529 'e2pdf-attachment',
1530 'e2pdf-adobesign',
1531 'e2pdf-zapier',
1532 ];
1533 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $user_email_message, $matches);
1534 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1535 if (!empty($tagnames)) {
1536 $this->set('process', true);
1537 } else {
1538 $user_email_message = '';
1539 }
1540 }
1541
1542 $_POST = $this->sanitize($_POST);
1543
1544 // check if E2Pdf shortcodes exist in messages
1545 if (!$this->get('process')) {
1546 return $props;
1547 }
1548
1549 $data = $_POST;
1550 if (isset($props['_unique_id'])) {
1551 $data['_unique_id'] = $props['_unique_id'];
1552 }
1553 if (isset($props['save_files_to_media']) && $props['save_files_to_media'] == 'on') {
1554 $data['_save_files_to_media'] = $props['save_files_to_media'];
1555 $data['_subdir'] = wp_upload_dir()['subdir'];
1556 } elseif (function_exists('pwh_dcfh_file_helpers')) {
1557 $subdir = pwh_dcfh_file_helpers()::get_subdir();
1558 if (is_array($subdir)) {
1559 $data['_subdir'] = implode('/', $subdir);
1560 }
1561 } elseif (function_exists('ks_pac_dcfh_file_helper')) {
1562 $subdir = ks_pac_dcfh_file_helper()::get_subdir();
1563 if (is_array($subdir)) {
1564 $data['_subdir'] = implode('/', $subdir);
1565 }
1566 }
1567
1568 $captcha = isset($props['captcha']) ? $props['captcha'] : '';
1569 $use_spam_service = isset($props['use_spam_service']) ? $props['use_spam_service'] : 'off';
1570 $et_pb_contact_form_num = str_replace('et_pb_contactform_submit_', '', $et_contact_proccess);
1571 $et_contact_error = false;
1572 $current_form_fields = isset($data['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $data['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
1573 $contact_email = '';
1574 $nonce_result = isset($data['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($data['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1575
1576 if ($nonce_result && isset($data['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($data['et_pb_contact_et_number_' . $et_pb_contact_form_num])) {
1577 if ('' !== $current_form_fields) {
1578 $fields_data_json = str_replace('\\', '', $current_form_fields);
1579 $fields_data_array = json_decode($fields_data_json, true);
1580
1581 // check whether captcha field is not empty
1582 if ('on' === $captcha && 'off' === $use_spam_service && (!isset($data['et_pb_contact_captcha_' . $et_pb_contact_form_num]) || empty($data['et_pb_contact_captcha_' . $et_pb_contact_form_num]))) {
1583 $et_contact_error = true;
1584 } elseif ('on' === $use_spam_service) {
1585 if (class_exists('ET_Builder_Element')) {
1586 $contact_form = ET_Builder_Element::get_module('et_pb_contact_form');
1587 if ($contact_form && is_object($contact_form) && method_exists($contact_form, 'is_spam_submission')) {
1588 $contact_form->props = $props;
1589 if ($contact_form->is_spam_submission()) {
1590 if (!empty($_POST['token'])) {
1591 unset($_POST['token']);
1592 }
1593 $et_contact_error = true;
1594 } else {
1595 $props['use_spam_service'] = 'off';
1596 $props['captcha'] = 'off';
1597 }
1598 }
1599 }
1600 }
1601
1602 // check all fields on current form and generate error message if needed
1603 if (!empty($fields_data_array)) {
1604 foreach ($fields_data_array as $value) {
1605 if (isset($value['field_id']) && 'et_pb_contact_et_number_' . $et_pb_contact_form_num === $value['field_id']) {
1606 continue;
1607 }
1608 /* Check all the required fields, generate error message if required field is empty */
1609 $field_value = isset($data[$value['field_id']]) ? trim($data[$value['field_id']]) : '';
1610 if ('required' === $value['required_mark'] && empty($field_value) && !is_numeric($field_value)) {
1611 $et_contact_error = true;
1612 continue;
1613 }
1614 /* Additional check for email field */
1615 if ('email' === $value['field_type'] && 'required' === $value['required_mark'] && !empty($field_value)) {
1616 $contact_email = isset($data[$value['field_id']]) ? sanitize_email($data[$value['field_id']]) : '';
1617 if (!empty($contact_email) && !is_email($contact_email)) {
1618 $et_contact_error = true;
1619 }
1620 }
1621 }
1622 }
1623 } else {
1624 $et_contact_error = true;
1625 }
1626 } else {
1627 $et_contact_error = true;
1628 }
1629
1630 if (!$et_contact_error && $nonce_result) {
1631 // divi contact form helper 2.1.3 bug fix wp_mail filter removed at maybe_filter_wp_mail
1632 if (defined('KS_PAC_DCFH_PLUGIN_VERSION') && version_compare(KS_PAC_DCFH_PLUGIN_VERSION, '2.1.3', '>=')) {
1633 add_filter('wp_mail', function ($args) {
1634 return $args;
1635 }, 999);
1636 }
1637 add_filter('wp_mail', [$this, 'filter_wp_mail'], 1000);
1638
1639 $dataset = false;
1640 if (false !== strpos($success_message, '[')) {
1641 $shortcode_tags = [
1642 'e2pdf-download',
1643 'e2pdf-save',
1644 'e2pdf-view',
1645 'e2pdf-adobesign',
1646 'e2pdf-zapier',
1647 ];
1648 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1649 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1650 if (!empty($tagnames)) {
1651 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $success_message, $shortcodes);
1652 foreach ($shortcodes[0] as $key => $shortcode_value) {
1653 $item = false;
1654 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1655 $atts = shortcode_parse_atts($shortcode[3]);
1656 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
1657 } else {
1658 if (!isset($atts['dataset']) && isset($atts['id'])) {
1659 $template = new Model_E2pdf_Template();
1660 $template->load($atts['id']);
1661 if ($template->get('extension') === 'divi') {
1662 $item = $template->get('item');
1663 if (!$dataset) {
1664 $entry = new Model_E2pdf_Dataset();
1665 $entry->set('extension', 'divi');
1666 $entry->set('item', $item);
1667 $entry->set('entry', $data);
1668 $dataset = $entry->save();
1669 }
1670 $atts['dataset'] = $dataset;
1671 $shortcode[3] .= ' dataset="' . $dataset . '"';
1672 }
1673 }
1674 if (!isset($atts['apply'])) {
1675 $shortcode[3] .= ' apply="true"';
1676 }
1677 if (!isset($atts['iframe_download'])) {
1678 $shortcode[3] .= ' iframe_download="true"';
1679 }
1680 $props['success_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['success_message']);
1681 }
1682 }
1683 }
1684 }
1685
1686 if (false !== strpos($admin_email_message, '[')) {
1687 $shortcode_tags = [
1688 'e2pdf-download',
1689 'e2pdf-save',
1690 'e2pdf-attachment',
1691 'e2pdf-adobesign',
1692 'e2pdf-zapier',
1693 ];
1694 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $admin_email_message, $matches);
1695 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1696 if (!empty($tagnames)) {
1697 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $admin_email_message, $shortcodes);
1698 foreach ($shortcodes[0] as $key => $shortcode_value) {
1699 $item = false;
1700 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1701 $atts = shortcode_parse_atts($shortcode[3]);
1702 if (!isset($atts['dataset']) && isset($atts['id'])) {
1703 $template = new Model_E2pdf_Template();
1704 $template->load($atts['id']);
1705 if ($template->get('extension') === 'divi') {
1706 $item = $template->get('item');
1707 if (!$dataset) {
1708 $entry = new Model_E2pdf_Dataset();
1709 $entry->set('extension', 'divi');
1710 $entry->set('item', $item);
1711 $entry->set('entry', $data);
1712 $dataset = $entry->save();
1713 }
1714 $atts['dataset'] = $dataset;
1715 $shortcode[3] .= ' dataset="' . $dataset . '"';
1716 }
1717 }
1718 if ($admin_email_rich_text) {
1719 if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1720 $props['custom_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['custom_message_richtext']);
1721 } else {
1722 $props['custom_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['custom_message_richtext']);
1723 }
1724 } else {
1725 $props['custom_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['custom_message']);
1726 }
1727 }
1728 }
1729 }
1730
1731 if (false !== strpos($user_email_message, '[')) {
1732 $shortcode_tags = [
1733 'e2pdf-download',
1734 'e2pdf-save',
1735 'e2pdf-attachment',
1736 'e2pdf-adobesign',
1737 'e2pdf-zapier',
1738 ];
1739 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $user_email_message, $matches);
1740 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1741 if (!empty($tagnames)) {
1742 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $user_email_message, $shortcodes);
1743 foreach ($shortcodes[0] as $key => $shortcode_value) {
1744 $item = false;
1745 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1746 $atts = shortcode_parse_atts($shortcode[3]);
1747 if (!isset($atts['dataset']) && isset($atts['id'])) {
1748 $template = new Model_E2pdf_Template();
1749 $template->load($atts['id']);
1750 if ($template->get('extension') === 'divi') {
1751 $item = $template->get('item');
1752 if (!$dataset) {
1753 $entry = new Model_E2pdf_Dataset();
1754 $entry->set('extension', 'divi');
1755 $entry->set('item', $item);
1756 $entry->set('entry', $data);
1757 $dataset = $entry->save();
1758 }
1759 $atts['dataset'] = $dataset;
1760 $shortcode[3] .= ' dataset="' . $dataset . '"';
1761 }
1762 }
1763 if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1764 if ($user_email_rich_text) {
1765 $props['confirmation_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['confirmation_message_richtext']);
1766 } else {
1767 $props['confirmation_email_message'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['confirmation_email_message']);
1768 }
1769 } else {
1770 if ($user_email_rich_text) {
1771 $props['confirmation_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['confirmation_message_richtext']);
1772 } else {
1773 $props['confirmation_email_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['confirmation_email_message']);
1774 }
1775 }
1776 }
1777 }
1778 }
1779 }
1780
1781 return $props;
1782 }
1783
1784 // filter shortcode output
1785 public function filter_et_module_shortcode_output($output, $render_slug, $form) {
1786
1787 if ($render_slug !== 'et_pb_contact_form') {
1788 return $output;
1789 }
1790
1791 $et_contact_proccess = array_search('et_contact_proccess', (array) $_POST);
1792 if ($et_contact_proccess === false) {
1793 return $output;
1794 }
1795
1796 $et_pb_contact_form_num = str_replace(['pwh_dcfh_et_pb_contactform_submit_', 'et_pb_contactform_submit_'], ['', ''], $et_contact_proccess);
1797 $nonce_result = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1798
1799 if ($nonce_result && (
1800 (isset($_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($_POST['et_pb_contact_et_number_' . $et_pb_contact_form_num])) ||
1801 (isset($_POST['pwh_dcfh_et_pb_contactform_submit_' . $et_pb_contact_form_num]) && $_POST['pwh_dcfh_et_pb_contactform_submit_' . $et_pb_contact_form_num] == 'et_contact_proccess')
1802 )
1803 ) {
1804 $success_message = isset($form->props['success_message']) ? $this->decode_shortcodes($form->props['success_message'], true) : '';
1805 if (false !== strpos($success_message, '[')) {
1806 $shortcode_tags = [
1807 'e2pdf-download',
1808 'e2pdf-save',
1809 'e2pdf-view',
1810 'e2pdf-adobesign',
1811 'e2pdf-zapier',
1812 ];
1813 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1814 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1815 if (!empty($tagnames)) {
1816 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $success_message, $shortcodes);
1817 foreach ($shortcodes[0] as $key => $shortcode_value) {
1818 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1819 if (isset($form->props['enable_multistep']) && 'on' === $form->props['enable_multistep']) {
1820 $output = str_replace($shortcode_value, do_shortcode_tag($shortcode), $output);
1821 } else {
1822 $output = str_replace(str_replace(['"'], ['&quot;'], $shortcode_value), do_shortcode_tag($shortcode), $output);
1823 }
1824 }
1825 }
1826 }
1827 }
1828 return $output;
1829 }
1830
1831 // delete item
1832 public function delete_item($template_id = false, $dataset = false) {
1833 global $wpdb;
1834 $template = new Model_E2pdf_Template();
1835 if ($template_id && $dataset && $template->load($template_id)) {
1836 if ($template->get('extension') === 'divi' && $template->get('item')) {
1837 $item = $template->get('item');
1838 $where = [
1839 'ID' => $dataset,
1840 'item' => $item,
1841 'extension' => 'divi',
1842 ];
1843 $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1844 return true;
1845 }
1846 }
1847 return false;
1848 }
1849
1850 // delete items
1851 public function delete_items($template_id = false) {
1852 global $wpdb;
1853 $template = new Model_E2pdf_Template();
1854 if ($template_id && $template->load($template_id)) {
1855 if ($template->get('extension') === 'divi' && $template->get('item')) {
1856 $where = [
1857 'item' => $template->get('item'),
1858 'extension' => 'divi',
1859 ];
1860 $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1861 return true;
1862 }
1863 }
1864 return false;
1865 }
1866
1867 // styles
1868 public function styles($item_id = false) {
1869 $styles = [];
1870 if ($this->divi5()) {
1871 $assets_list = \ET\Builder\FrontEnd\Assets\DynamicAssetsUtils::get_assets_list(
1872 [
1873 'prefix' => \ET\Builder\FrontEnd\Assets\DynamicAssetsUtils::get_dynamic_assets_path(),
1874 'suffix' => '',
1875 'specialty_suffix' => '',
1876 ]
1877 );
1878 foreach ($assets_list as $list) {
1879 if (!empty($list['css']) && is_array($list['css'])) {
1880 foreach ($list['css'] as $css) {
1881 $styles[] = $this->wp_convert_path_to_url($css);
1882 }
1883 }
1884 }
1885 }
1886 $styles[] = plugins_url('css/extension/divi.css?v=' . time(), $this->helper->get('plugin_file_path'));
1887 return $styles;
1888 }
1889
1890 public function wp_convert_path_to_url($path) {
1891 // Standardize slashes for Windows/Linux compatibility
1892 $path = wp_normalize_path($path);
1893 $root = wp_normalize_path(ABSPATH);
1894
1895 // Replace the server root with the Site URL
1896 return str_replace($root, site_url('/'), $path);
1897 }
1898
1899 // sanitize
1900 public function sanitize($value) {
1901 if (is_array($value)) {
1902 foreach ($value as $k => $v) {
1903 $value[$k] = $this->sanitize($v);
1904 }
1905 return $value;
1906 }
1907 if (!is_string($value)) {
1908 return $value;
1909 }
1910 if (stripos($value, 'e2pdf-') === false) {
1911 return $value;
1912 }
1913 return preg_replace('/(?:\[|&#91;|\{)(e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]}\r\n]*(?:\]|&#93;|\})/i', '', $value);
1914 }
1915
1916 // decode shortcodes
1917 public function decode_shortcodes($value = '', $quote = false) {
1918 return preg_replace_callback(
1919 '/(&#91;)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^&]*?)(&#93;)/',
1920 function ($m) use ($quote) {
1921 $shortcode = $m[2];
1922 if ($quote !== false) {
1923 $shortcode = str_replace('&quot;', '"', $shortcode);
1924 }
1925
1926 return '[' . $shortcode . ']';
1927 },
1928 $value
1929 );
1930 }
1931
1932 // encode shortcodes
1933 public function encode_shortcodes($value = '', $quote = false) {
1934 return preg_replace_callback(
1935 '/(\[)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]]*?)(\])/',
1936 function ($m) use ($quote) {
1937 $shortcode = $m[2];
1938 if ($quote !== false) {
1939 $shortcode = str_replace('"', '&quot;', $shortcode);
1940 }
1941
1942 return '&#91;' . $shortcode . '&#93;';
1943 },
1944 $value
1945 );
1946 }
1947
1948 // divi4 load
1949 public function divi4_load() {
1950 if (defined('ET_BUILDER_DIR') && !$this->divi5()) {
1951 require_once ET_BUILDER_DIR . 'class-et-builder-element.php';
1952 require_once ET_BUILDER_DIR . 'functions.php';
1953 require_once ET_BUILDER_DIR . 'ab-testing.php';
1954 require_once ET_BUILDER_DIR . 'class-et-global-settings.php';
1955 if (file_exists(ET_BUILDER_DIR . 'module/type/WithSpamProtection.php')) {
1956 require_once ET_BUILDER_DIR . 'module/type/WithSpamProtection.php';
1957 }
1958 require_once ET_BUILDER_DIR . 'module/ContactForm.php';
1959 require_once ET_BUILDER_DIR . 'module/ContactFormItem.php';
1960 new ET_Builder_Module_Contact_Form();
1961 new ET_Builder_Module_Contact_Form_Item();
1962 }
1963 }
1964
1965 // is divi5
1966 public function divi5() {
1967 if (defined('ET_CORE_VERSION') && version_compare(ET_CORE_VERSION, '4.99', '>')) {
1968 return true;
1969 }
1970 return false;
1971 }
1972
1973 // is divi4 form
1974 public function divi4_form($content = '') {
1975 return (false !== strpos($content, '[et_pb_contact_form'));
1976 }
1977
1978 // is divi 5 form
1979 public function divi5_form($content = '') {
1980 return (false !== strpos($content, 'wp:divi/contact-form'));
1981 }
1982 }
1983