PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.32
E2Pdf – Export Pdf Tool for WordPress v1.32.32
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.32, at classes/extension/e2pdf-divi.php

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