PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.23
E2Pdf – Export Pdf Tool for WordPress v1.32.23
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-elementor.php

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

1,198 lines 51.6 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-elementor.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_Elementor extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = array(
18 'key' => 'elementor',
19 'title' => 'Elementor 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 array(
28 $this->info['key'] => $this->info['title'],
29 );
30 }
31 }
32
33 // active
34 public function active() {
35 if (defined('E2PDF_ELEMENTOR_EXTENSION') || $this->helper->load('extension')->is_plugin_active('elementor-pro/elementor-pro.php') || $this->helper->load('extension')->is_plugin_active('pro-elements/pro-elements.php')) {
36 return true;
37 }
38 return false;
39 }
40
41 // set
42 public function set($key, $value) {
43 if (!isset($this->options)) {
44 $this->options = new stdClass();
45 }
46 $this->options->$key = $value;
47 switch ($key) {
48 case 'item':
49 $this->set('cached_form', false);
50 if ($this->get('item')) {
51 $this->set('cached_form', $this->get_form($this->get('item')));
52 }
53 break;
54 case 'dataset':
55 $this->set('cached_entry', false);
56 $this->set('cached_meta', []);
57 $this->set('cached_data', []);
58 if ($this->get('cached_form') && $this->get('dataset') && class_exists('ElementorPro\Modules\Forms\Submissions\Database\Query') && class_exists('ElementorPro\Modules\Forms\Classes\Form_Record')) {
59 $this->set('cached_meta', ElementorPro\Modules\Forms\Submissions\Database\Query::get_instance()->get_submission($this->get('dataset')));
60 $labels = [];
61 if (!empty($this->get('cached_meta')['data']['form']['fields'])) {
62 foreach ($this->get('cached_meta')['data']['form']['fields'] as $field) {
63 if (isset($field['id']) && !empty($field['options']) && is_array($field['options'])) {
64 $labels[$field['id']] = [];
65 foreach ($field['options'] as $option) {
66 $parts = explode('|', $option);
67 $label = reset($parts);
68 $value = end($parts);
69 if ($value) {
70 $labels[$field['id']][$value] = $label;
71 }
72 }
73 }
74 }
75 }
76 $cached_data = [];
77 if (!empty($this->get('cached_meta')['data']['values'])) {
78 foreach ($this->get('cached_meta')['data']['values'] as $data) {
79 if (isset($data['key'])) {
80 $value = isset($data['value']) ? $data['value'] : '';
81 $cached_data[$data['key']] = $value;
82 // process labels
83 if ($value && is_string($value) && isset($labels[$data['key']])) {
84 if (isset($labels[$data['key']][$value])) {
85 $cached_data[$data['key'] . ':label'] = $labels[$data['key']][$value];
86 } elseif (false !== strpos($value, ', ')) {
87 $sub_labels = [];
88 $values = explode(', ', $value);
89 foreach ($values as $sub_value) {
90 if (isset($labels[$data['key']][$sub_value])) {
91 $sub_labels[] = $labels[$data['key']][$sub_value];
92 } else {
93 $sub_labels[] = $sub_value;
94 }
95 }
96 $cached_data[$data['key'] . ':label'] = implode(', ', $sub_labels);
97 }
98 }
99 }
100 }
101 }
102
103 $this->set('cached_data', $cached_data);
104 $this->set('cached_entry', new ElementorPro\Modules\Forms\Classes\Form_Record($this->get('cached_data'), $this->get('cached_form')));
105 }
106 break;
107 default:
108 break;
109 }
110 return true;
111 }
112
113 // get
114 public function get($key) {
115 if (isset($this->options->$key)) {
116 $value = $this->options->$key;
117 } else {
118 switch ($key) {
119 case 'cached_meta':
120 case 'args':
121 $value = array();
122 break;
123 default:
124 $value = false;
125 break;
126 }
127 }
128 return $value;
129 }
130
131 // load actions
132 public function load_actions() {
133 add_action('elementor_pro/forms/new_record', array($this, 'action_elementor_pro_forms_new_record'), 10, 2);
134 add_action('elementor_pro/forms/actions/after_run', array($this, 'action_elementor_pro_forms_actions_after_run'));
135 add_action('elementor_pro/forms/mail_sent', array($this, 'action_elementor_pro_forms_mail_sent'), 10, 2);
136 }
137
138 // load filters
139 public function load_filters() {
140 add_filter('elementor_pro/forms/wp_mail_message', array($this, 'filter_elementor_pro_forms_wp_mail_message'));
141 add_filter('elementor_pro/forms/record/actions_before', array($this, 'filter_elementor_pro_forms_record_actions_before'));
142 add_filter('wpnotif_filter_elementor_message', array($this, 'filter_wpnotif_filter_elementor_message'), 10, 2);
143 }
144
145 // items
146 public function items() {
147 $listed = array();
148 $content = array();
149 $postforms = $this->get_post_forms();
150 foreach ($postforms as $postform) {
151 $listed[] = $postform;
152 }
153 $snapshots = ElementorPro\Modules\Forms\Submissions\Database\Repositories\Form_Snapshot_Repository::instance()->all();
154 foreach ($snapshots as $snapshot) {
155 if (!in_array($snapshot->get_key(), $listed, true)) {
156 $listed[] = $snapshot->get_key();
157 }
158 }
159 foreach ($listed as $form) {
160 $content[] = $this->item($form);
161 }
162 return $content;
163 }
164
165 // get post forms
166 public function get_post_forms() {
167 global $wpdb;
168
169 $content = array();
170 $result = $wpdb->get_results(
171 $wpdb->prepare(
172 'SELECT pm.post_id, pm.meta_value FROM `' . $wpdb->postmeta . '` pm INNER JOIN `' . $wpdb->posts . '` pp ON pp.ID = pm.post_id WHERE pm.meta_key = %s AND pp.post_type NOT IN ("revision")',
173 '_elementor_data'
174 )
175 );
176 foreach ($result as $post) {
177 if (is_string($post->meta_value) && !empty($post->meta_value) && false !== strpos($post->meta_value, 'form_fields')) {
178 $meta = json_decode($post->meta_value, true);
179 if (empty($meta)) {
180 $meta = [];
181 }
182 $forms = $this->parse_forms_meta($meta);
183 foreach ($forms as $key => $form) {
184 $content[] = $post->post_id . '_' . $form;
185 }
186 }
187 }
188
189 return $content;
190 }
191
192 // parse forms meta
193 public function parse_forms_meta($elements) {
194 $forms = array();
195 foreach ($elements as $element) {
196 if (isset($element['settings']['form_fields'])) {
197 $forms[] = $element['id'];
198 }
199 if (!empty($element['elements'])) {
200 $sub_forms = $this->parse_forms_meta($element['elements']);
201 if (!empty($sub_forms)) {
202 $forms = array_merge($forms, $sub_forms);
203 }
204 }
205 }
206 return $forms;
207 }
208
209 // get form
210 public function get_form($item_id = false) {
211 if ($item_id) {
212 $form = false;
213 list($post_id, $form_id) = explode('_', $item_id);
214 $elementor = ElementorPro\Plugin::elementor();
215 $document = $elementor->documents->get($post_id);
216 if ($document) {
217 $form = ElementorPro\Modules\Forms\Module::find_element_recursive($document->get_elements_data(), $form_id);
218 if ($form && is_array($form)) {
219 $form['post_id'] = $post_id;
220 } else {
221 $snapshot = ElementorPro\Modules\Forms\Submissions\Database\Repositories\Form_Snapshot_Repository::instance()->find($post_id, $form_id);
222 if ($snapshot) {
223 $fields = array();
224 foreach ($snapshot->fields as $field) {
225 $fields[] = array(
226 'custom_id' => $field['id'],
227 'field_type' => $field['type'],
228 'required' => !empty($field['required']),
229 'field_label' => $field['label'],
230 'field_options' => '',
231 'placeholder' => '',
232 '_id' => '',
233 );
234 }
235 $form = array(
236 'id' => $snapshot->id,
237 'elType' => 'widget',
238 'post_id' => $snapshot->post_id,
239 'widgetType' => 'form',
240 'settings' => array(
241 'form_name' => $snapshot->name,
242 'form_fields' => $fields,
243 'form_metadata' => array(),
244 ),
245 );
246 }
247 }
248 }
249 if ($form) {
250 foreach ($form['settings']['form_fields'] as $key => $form_field) {
251 if (!isset($form_field['field_type'])) {
252 $form['settings']['form_fields'][$key]['field_type'] = 'text';
253 }
254 if (!isset($form_field['field_label'])) {
255 $form['settings']['form_fields'][$key]['field_label'] = '';
256 }
257 }
258 if (!isset($form['settings']['form_metadata'])) {
259 $form['settings']['form_metadata'] = array();
260 }
261 }
262 return $form;
263 } else {
264 return false;
265 }
266 }
267
268 // item
269 public function item($item_id = false) {
270 if (!$item_id && $this->get('item')) {
271 $item_id = $this->get('item');
272 }
273 $form = $this->get_form($item_id);
274 $item = new stdClass();
275 if ($form) {
276 $item->id = $item_id;
277 $item->url = $this->helper->get_url(
278 array(
279 'post' => $form['post_id'],
280 'action' => 'elementor',
281 )
282 );
283 $post_name = '';
284 if (!empty($form['post_id'])) {
285 $post_name .= ' | ' . get_the_title($form['post_id']);
286 }
287
288 $item->name = $form['settings']['form_name'] . ' (' . $form['id'] . ')' . $post_name;
289 } else {
290 $item->id = '';
291 $item->url = '';
292 $item->name = '';
293 }
294 return $item;
295 }
296
297 // datasets
298 public function datasets($item_id = false, $name = false) {
299 $datasets = array();
300 if ($item_id) {
301 $entries = ElementorPro\Modules\Forms\Submissions\Database\Query::get_instance()->get_submissions(
302 array(
303 'page' => 1,
304 'per_page' => 999999,
305 'filters' => array(
306 'form' => array(
307 'value' => $item_id,
308 ),
309 ),
310 'order' => [
311 'order' => 'desc',
312 'by' => 'id',
313 ],
314 )
315 );
316 if (!empty($entries['data'])) {
317 $this->set('item', $item_id);
318 foreach ($entries['data'] as $key => $entry) {
319 $this->set('dataset', $entry['id']);
320 $entry_title = $this->render($name);
321 if (!$entry_title) {
322 $entry_title = $entry['id'];
323 }
324 $datasets[] = array(
325 'key' => $entry['id'],
326 'value' => $entry_title,
327 );
328 }
329 }
330 }
331 return $datasets;
332 }
333
334 // get dataset actions
335 public function get_dataset_actions($dataset_id = false) {
336 $dataset_id = (int) $dataset_id;
337 if (!$dataset_id) {
338 return false;
339 }
340 $actions = new stdClass();
341 $actions->view = $this->helper->get_url(array('page' => 'e-form-submissions')) . '#/' . $dataset_id;
342 $actions->delete = false;
343 return $actions;
344 }
345
346 // get template actions
347 public function get_template_actions($template = false) {
348 $template = (int) $template;
349 if (!$template) {
350 return;
351 }
352 $actions = new stdClass();
353 $actions->delete = false;
354 return $actions;
355 }
356
357 // render
358 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
359 $value = $this->render_shortcodes($value, $field);
360 if (!$raw) {
361 $value = $this->strip_shortcodes($value);
362 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
363 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
364 }
365 return $value;
366 }
367
368 // render shortcodes
369 public function render_shortcodes($value, $field = array()) {
370 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
371 if ($this->verify()) {
372 if (false !== strpos($value, '[')) {
373 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
374 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
375 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
376 }
377 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
378 $value = $this->replace_meta_shortcodes($value, $this->get('cached_meta'));
379
380 /**
381 * Compatibility fix for Signature field for Elementor Forms
382 * https://wordpress.org/plugins/signature-field-for-elementor-forms/
383 */
384 if (class_exists('Superaddons_Elementor_Signature_Field')) {
385 $signatures = $this->get('cached_entry')->get_field(
386 array(
387 'type' => 'signature',
388 )
389 );
390 foreach ($signatures as $signature) {
391 $this->get('cached_entry')->update_field($signature['id'], 'value', $signature['raw_value']);
392 }
393 }
394
395 /**
396 * Compatibility fix Repeater Fields for Elementor Forms
397 * https://wordpress.org/plugins/repeater-for-elementor/
398 */
399 if (class_exists('Rednumber_Repeater_Custom_Validation')) {
400 $repeaters = $this->get('cached_entry')->get_field(
401 array(
402 'type' => 'repeater',
403 )
404 );
405 foreach ($repeaters as $repeater) {
406 $this->get('cached_entry')->update_field($repeater['id'], 'value', $this->repeater_field_value($repeater['raw_value']));
407 }
408 }
409
410 $value = $this->get('cached_entry')->replace_setting_shortcodes($value);
411 $value = $this->replace_label_shortcodes($value);
412 $value = $this->helper->load('field')->render(
413 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
414 $this,
415 $field
416 );
417 }
418 return apply_filters(
419 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
420 );
421 }
422
423 // convert shortcodes
424 public function convert_shortcodes($value, $to = false, $html = false) {
425 if ($value) {
426 if ($to) {
427 $value = str_replace('&#91;', '[', $value);
428 if (!$html) {
429 $value = wp_specialchars_decode($value, ENT_QUOTES);
430 }
431 } else {
432 $value = str_replace('[', '&#91;', $value);
433 }
434 }
435 return $value;
436 }
437
438 // strip shortcodes
439 public function strip_shortcodes($value) {
440 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
441 return $value;
442 }
443
444 // verify
445 public function verify() {
446 if (class_exists('ElementorPro\Modules\Forms\Submissions\Database\Query') && $this->get('cached_form') && $this->get('cached_meta')) {
447 $post_id = isset($this->get('cached_meta')['data']['post']['id']) ? $this->get('cached_meta')['data']['post']['id'] : false;
448 $form_id = isset($this->get('cached_meta')['data']['form']['element_id']) ? $this->get('cached_meta')['data']['form']['element_id'] : false;
449 if ($post_id && $form_id && $this->get('item') == $post_id . '_' . $form_id) {
450 return true;
451 }
452 }
453 return false;
454 }
455
456 // visual mapper
457 public function visual_mapper() {
458
459 $item = $this->get('item');
460 $html = '';
461 $source = '';
462
463 if ($item) {
464 ob_start();
465 $form = $this->get_form($item);
466 if ($form) {
467 $elementor = ElementorPro\Plugin::elementor();
468 $document = $elementor->documents->get($form['post_id']);
469 $elementor->documents->switch_to_document($document);
470 $widget = $elementor->elements_manager->create_element_instance($form);
471 $widget->print_element();
472 $source = ob_get_contents();
473 }
474 if (ob_get_length() > 0) {
475 while (@ob_end_clean()); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
476 }
477 if ($source) {
478 $dom = new DOMDocument();
479 $html = $this->helper->load('convert')->load_html($source, $dom, true);
480 }
481 if (!$source) {
482 return '<div class="e2pdf-vm-error">' . __("The form source is empty or doesn't exist", 'e2pdf') . '</div>';
483 } elseif (!$html) {
484 return '<div class="e2pdf-vm-error">' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '</div>';
485 } else {
486 $xml = $this->helper->load('xml');
487 $xpath = new DomXPath($dom);
488
489 // remove by name
490 $remove_by_name = array(
491 'post_id',
492 'form_id',
493 'referer_title',
494 );
495 foreach ($remove_by_name as $key => $name) {
496 $elements = $xpath->query('//*[@name="' . $name . '"]');
497 foreach ($elements as $element) {
498 $element->parentNode->removeChild($element);
499 }
500 }
501
502 // remove by class
503 $remove_by_class = array(
504 'dce-conditions-js-error-notice',
505 'repeater-field-button-add',
506 'elementor-field-type-submit',
507 'select-caret-down-wrapper',
508 );
509 foreach ($remove_by_class as $key => $class) {
510 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
511 foreach ($elements as $element) {
512 $element->parentNode->removeChild($element);
513 }
514 }
515
516 $elements = $xpath->query('//input|//textarea|//select');
517 foreach ($elements as $element) {
518 if ($xml->get_node_value($element, 'type') == 'checkbox' || $xml->get_node_value($element, 'type') == 'file') {
519 $xml->set_node_value($element, 'name', str_replace('[]', '', $xml->get_node_value($element, 'name')));
520 }
521 $xml->set_node_value($element, 'name', str_replace(array('form_fields[', ']'), array('[field id="', '"]'), $xml->get_node_value($element, 'name')));
522 }
523
524 $elements = $xpath->query("//input[@type='submit']");
525 foreach ($elements as $element) {
526 $element->parentNode->removeChild($element);
527 }
528
529 $elements = $xpath->query("//input[@type='dce_form_signature']");
530 foreach ($elements as $element) {
531 if ($xml->get_node_value($element, 'type') == 'checkbox' || $xml->get_node_value($element, 'type') == 'file') {
532 $xml->set_node_value($element, 'name', str_replace('[]', '', $xml->get_node_value($element, 'name')));
533 }
534 $xml->set_node_value($element, 'type', 'text');
535 }
536
537 $elements = $xpath->query("//*[contains(@class, 'elementor-field-repeater-data')]");
538 foreach ($elements as $element) {
539 $xml->set_node_value($element, 'value', $xml->get_node_value($element, 'name'));
540 $xml->set_node_value($element, 'type', 'text');
541 }
542
543 if (defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD')) {
544 return str_replace(array('<html>', '</html>'), '', $dom->saveHTML());
545 } else {
546 return $dom->saveHTML();
547 }
548 }
549 }
550 return false;
551 }
552
553 // auto
554 public function auto() {
555
556 $response = array();
557 $elements = array();
558
559 $form = $this->get_form($this->get('item'));
560
561 foreach ($form['settings']['form_fields'] as $key => $field) {
562 $id = isset($field['custom_id']) ? $field['custom_id'] : $field['_id'];
563 $field_type = isset($field['field_type']) ? $field['field_type'] : 'text';
564 $label = isset($field['field_label']) ? $field['field_label'] : '';
565 switch ($field_type) {
566 case 'text':
567 case 'email':
568 case 'url':
569 case 'tel':
570 case 'number':
571 case 'date':
572 case 'time':
573 case 'password':
574 $elements[] = $this->auto_field(
575 $field,
576 array(
577 'type' => 'e2pdf-html',
578 'block' => true,
579 'properties' => array(
580 'top' => '20',
581 'left' => '20',
582 'right' => '20',
583 'width' => '100%',
584 'height' => 'auto',
585 'value' => $label,
586 ),
587 )
588 );
589 $elements[] = $this->auto_field(
590 $field,
591 array(
592 'type' => 'e2pdf-input',
593 'properties' => array(
594 'top' => '5',
595 'width' => '100%',
596 'height' => 'auto',
597 'pass' => $field_type === 'password' ? '1' : '0',
598 'value' => '[field id="' . $id . '"]',
599 ),
600 )
601 );
602 break;
603 case 'upload':
604 case 'textarea':
605 $elements[] = $this->auto_field(
606 $field,
607 array(
608 'type' => 'e2pdf-html',
609 'block' => true,
610 'properties' => array(
611 'top' => '20',
612 'left' => '20',
613 'right' => '20',
614 'width' => '100%',
615 'height' => 'auto',
616 'value' => $label,
617 ),
618 )
619 );
620 $elements[] = $this->auto_field(
621 $field,
622 array(
623 'type' => 'e2pdf-textarea',
624 'properties' => array(
625 'top' => '5',
626 'width' => '100%',
627 'height' => '150',
628 'text_auto_font_size' => '1',
629 'value' => '[field id="' . $id . '"]',
630 ),
631 )
632 );
633 break;
634 case 'select':
635 $elements[] = $this->auto_field(
636 $field,
637 array(
638 'type' => 'e2pdf-html',
639 'block' => true,
640 'properties' => array(
641 'top' => '20',
642 'left' => '20',
643 'right' => '20',
644 'width' => '100%',
645 'height' => 'auto',
646 'value' => $label,
647 ),
648 )
649 );
650
651 $options = [];
652 if (isset($field['field_options'])) {
653 $options = array_map(
654 function ($item) {
655 $parts = explode('|', $item);
656 return end($parts);
657 }, preg_split("/\\r\\n|\\r|\\n/", $field['field_options'])
658 );
659 }
660
661 $elements[] = $this->auto_field(
662 $field,
663 array(
664 'type' => 'e2pdf-select',
665 'properties' => array(
666 'top' => '5',
667 'width' => '100%',
668 'height' => 'auto',
669 'multiline' => isset($field['is_multiple']) && $field['is_multiple'] ? '1' : '0',
670 'options' => implode("\n", $options),
671 'value' => '[field id="' . $id . '"]',
672 'height' => 'auto',
673 ),
674 )
675 );
676 break;
677 case 'checkbox':
678 $elements[] = $this->auto_field(
679 $field,
680 array(
681 'type' => 'e2pdf-html',
682 'block' => true,
683 'properties' => array(
684 'top' => '20',
685 'left' => '20',
686 'right' => '20',
687 'width' => '100%',
688 'height' => 'auto',
689 'value' => $label,
690 ),
691 )
692 );
693
694 if (isset($field['field_options'])) {
695 foreach (preg_split('/(\r\n|\n|\r)/', $field['field_options']) as $checkbox) {
696 $options = explode('|', $checkbox);
697 $option = end($options);
698 $elements[] = $this->auto_field(
699 $field,
700 array(
701 'type' => 'e2pdf-checkbox',
702 'properties' => array(
703 'top' => '5',
704 'width' => 'auto',
705 'height' => 'auto',
706 'value' => '[field id="' . $id . '"]',
707 'option' => $option ? $option : '',
708 ),
709 )
710 );
711
712 $lables = explode('|', $checkbox);
713 $label = reset($lables);
714 $elements[] = $this->auto_field(
715 $field,
716 array(
717 'type' => 'e2pdf-html',
718 'float' => true,
719 'properties' => array(
720 'left' => '5',
721 'width' => '100%',
722 'height' => 'auto',
723 'value' => $label ? $label : '',
724 ),
725 )
726 );
727 }
728 }
729 break;
730 case 'radio':
731 $elements[] = $this->auto_field(
732 $field,
733 array(
734 'type' => 'e2pdf-html',
735 'block' => true,
736 'properties' => array(
737 'top' => '20',
738 'left' => '20',
739 'right' => '20',
740 'width' => '100%',
741 'height' => 'auto',
742 'value' => $label,
743 ),
744 )
745 );
746 if (isset($field['field_options'])) {
747 foreach (preg_split('/(\r\n|\n|\r)/', $field['field_options']) as $radio) {
748 $options = explode('|', $radio);
749 $option = end($options);
750 $elements[] = $this->auto_field(
751 $field,
752 array(
753 'type' => 'e2pdf-radio',
754 'properties' => array(
755 'top' => '5',
756 'width' => 'auto',
757 'height' => 'auto',
758 'value' => '[field id="' . $id . '"]',
759 'option' => $option ? $option : '',
760 'group' => '[field id="' . $id . '"]',
761 ),
762 )
763 );
764
765 $lables = explode('|', $radio);
766 $label = reset($lables);
767 $elements[] = $this->auto_field(
768 $field,
769 array(
770 'type' => 'e2pdf-html',
771 'float' => true,
772 'properties' => array(
773 'left' => '5',
774 'width' => '100%',
775 'height' => 'auto',
776 'value' => $label,
777 ),
778 )
779 );
780 }
781 }
782 break;
783 case 'acceptance':
784 $elements[] = $this->auto_field(
785 $field,
786 array(
787 'type' => 'e2pdf-html',
788 'block' => true,
789 'properties' => array(
790 'top' => '20',
791 'left' => '20',
792 'right' => '20',
793 'width' => '100%',
794 'height' => 'auto',
795 'value' => isset($field['acceptance_text']) ? $field['acceptance_text'] : '',
796 ),
797 )
798 );
799 $elements[] = $this->auto_field(
800 $field,
801 array(
802 'type' => 'e2pdf-checkbox',
803 'properties' => array(
804 'top' => '5',
805 'width' => 'auto',
806 'height' => 'auto',
807 'value' => '[field id="' . $id . '"]',
808 'option' => 'on',
809 ),
810 )
811 );
812 $elements[] = $this->auto_field(
813 $field,
814 array(
815 'type' => 'e2pdf-html',
816 'float' => true,
817 'properties' => array(
818 'left' => '5',
819 'width' => '100%',
820 'height' => 'auto',
821 'value' => $label,
822 ),
823 )
824 );
825 break;
826 case 'html':
827 $elements[] = $this->auto_field(
828 $field,
829 array(
830 'type' => 'e2pdf-html',
831 'block' => true,
832 'properties' => array(
833 'top' => '20',
834 'left' => '20',
835 'right' => '20',
836 'width' => '100%',
837 'height' => 'auto',
838 'value' => isset($field['field_html']) ? $field['field_html'] : '',
839 ),
840 )
841 );
842 break;
843 case 'signature':
844 case 'dce_form_signature':
845 $elements[] = $this->auto_field(
846 $field,
847 array(
848 'type' => 'e2pdf-signature',
849 'properties' => array(
850 'top' => '5',
851 'width' => '100%',
852 'height' => '150',
853 'dimension' => '1',
854 'block_dimension' => '1',
855 'value' => '[field id="' . $id . '"]',
856 ),
857 )
858 );
859 break;
860 case 'repeater':
861 if (class_exists('Rednumber_Repeater_Custom_Validation')) {
862 $elements[] = $this->auto_field(
863 $field,
864 array(
865 'type' => 'e2pdf-html',
866 'block' => true,
867 'properties' => array(
868 'top' => '20',
869 'left' => '20',
870 'right' => '20',
871 'width' => '100%',
872 'height' => 'auto',
873 'value' => isset($field['repeater_title']) ? $field['repeater_title'] : '',
874 ),
875 )
876 );
877 $elements[] = $this->auto_field(
878 $field,
879 array(
880 'type' => 'e2pdf-textarea',
881 'properties' => array(
882 'top' => '5',
883 'width' => '100%',
884 'height' => '150',
885 'text_auto_font_size' => '1',
886 'value' => '[field id="' . $id . '"]',
887 ),
888 )
889 );
890 }
891 break;
892 default:
893 break;
894 }
895 }
896 $response['page'] = array(
897 'bottom' => '20',
898 'top' => '20',
899 'right' => '20',
900 'left' => '20',
901 );
902 $response['elements'] = $elements;
903 return $response;
904 }
905
906 // auto field
907 public function auto_field($field = false, $element = array()) {
908 if (!$field) {
909 return false;
910 }
911 if (!isset($element['block'])) {
912 $element['block'] = false;
913 }
914 if (!isset($element['float'])) {
915 $element['float'] = false;
916 }
917 if (isset($element['block']) && $element['block'] && isset($field['width']) && $field['width'] !== '100') {
918 $element['float'] = true;
919 $element['properties']['width'] = $field['width'] . '%';
920 }
921
922 return $element;
923 }
924
925 // styles
926 public function styles($item_id = false) {
927 $styles = array();
928 if (defined('ELEMENTOR_ASSETS_URL')) {
929 $styles[] = ELEMENTOR_ASSETS_URL . 'css/frontend.css';
930 }
931 if (defined('ELEMENTOR_PRO_URL')) {
932 $styles[] = ELEMENTOR_PRO_URL . 'assets/css/frontend.css';
933 }
934 $styles[] = $this->helper->get_wp_upload_dir('baseurl') . '/elementor/css/global.css';
935 $styles[] = plugins_url('css/extension/elementor.css?v=' . time(), $this->helper->get('plugin_file_path'));
936 return $styles;
937 }
938
939 // replace meta shortcodes
940 public function replace_meta_shortcodes($value, $submission) {
941 $meta_keys = array(
942 'id',
943 'referer',
944 'referer_title',
945 'user_id',
946 'user_agent',
947 'user_ip',
948 'user_name',
949 'created_at_gmt',
950 'updated_at_gmt',
951 'created_at',
952 'updated_at',
953 'status',
954 );
955 $meta = array();
956 foreach ($meta_keys as $meta_key) {
957 if (isset($submission['data'][$meta_key])) {
958 $meta['[' . $meta_key . ']'] = $submission['data'][$meta_key];
959 } else {
960 $meta['[' . $meta_key . ']'] = '';
961 }
962 }
963 return str_replace(array_keys($meta), $meta, $value);
964 }
965
966 // after run action
967 public function action_elementor_pro_forms_actions_after_run($action) {
968 if ($action->get_name() == 'save-to-database' && class_exists('ReflectionProperty')) {
969 $reflection = new ReflectionProperty(get_class($action), 'submission_id');
970 $reflection->setAccessible(true);
971 $reflection->getValue($action);
972 $this->set('dataset', $reflection->getValue($action));
973 }
974 }
975
976 // new record action
977 public function action_elementor_pro_forms_new_record($record, $handler) {
978 if ($handler->is_success && class_exists('ReflectionProperty')) {
979 $form = $handler->get_current_form();
980 $message = $this->filter_success_message($form['settings']['success_message']);
981 if ($form['settings']['success_message'] != $message) {
982 $form['settings']['success_message'] = $message;
983 $reflection = new ReflectionProperty(get_class($handler), 'current_form');
984 $reflection->setAccessible(true);
985 $reflection->setValue($handler, $form);
986 }
987 }
988 }
989
990 // forms mail sent action
991 public function action_elementor_pro_forms_mail_sent($settings, $record) {
992 remove_filter('wp_mail', array($this, 'filter_wp_mail'), 30);
993 $files = $this->helper->get('elementor_attachments');
994 if (is_array($files) && !empty($files)) {
995 foreach ($files as $key => $file) {
996 $this->helper->delete_dir(dirname($file) . '/');
997 }
998 $this->helper->deset('elementor_attachments');
999 }
1000 }
1001
1002 // filter form record actions before
1003 public function filter_elementor_pro_forms_record_actions_before($record) {
1004 $form_settings = $record->get('form_settings');
1005 if (!empty($form_settings['email_content'])) {
1006 $form_settings['email_content'] = preg_replace('/(\[)((e2pdf-download|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]]*?)(\])/', '{{$2}}', $form_settings['email_content']);
1007 }
1008 if (!empty($form_settings['email_content_2'])) {
1009 $form_settings['email_content_2'] = preg_replace('/(\[)((e2pdf-download|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]]*?)(\])/', '{{$2}}', $form_settings['email_content_2']);
1010 }
1011 if (!empty($form_settings['dce_form_email_repeater'])) {
1012 foreach ($form_settings['dce_form_email_repeater'] as $key => $email_content) {
1013 if (!empty($email_content['dce_form_email_content'])) {
1014 $form_settings['dce_form_email_repeater'][$key]['dce_form_email_content'] = preg_replace('/(\[)((e2pdf-download|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]]*?)(\])/', '{{$2}}', $email_content['dce_form_email_content']);
1015 }
1016 }
1017 }
1018 $record->set('form_settings', $form_settings);
1019 return $record;
1020 }
1021
1022 // filter wpnotif message
1023 public function filter_wpnotif_filter_elementor_message($message, $settings) {
1024 $message = $this->filter_success_message($message);
1025 return $message;
1026 }
1027
1028 // filter elementor pro wp mail message
1029 public function filter_elementor_pro_forms_wp_mail_message($message) {
1030 $message = preg_replace('/(\{\{)((e2pdf-download|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\}]*?)(\}\})/', '[$2]', $message);
1031 if (false !== strpos($message, '[')) {
1032 $shortcode_tags = array(
1033 'e2pdf-download',
1034 'e2pdf-save',
1035 'e2pdf-adobesign',
1036 'e2pdf-zapier',
1037 'e2pdf-attachment',
1038 );
1039 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $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) . '/', $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 ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1047 if (!isset($atts['dataset']) && isset($atts['id'])) {
1048 $template = new Model_E2pdf_Template();
1049 $template->load($atts['id']);
1050 if ($template->get('extension') === 'elementor') {
1051 $atts['dataset'] = $this->get('dataset');
1052 $shortcode[3] .= ' dataset="' . $this->get('dataset') . '"';
1053 }
1054 }
1055 if (!isset($atts['apply'])) {
1056 $shortcode[3] .= ' apply="true"';
1057 }
1058 $file = do_shortcode_tag($shortcode);
1059 if ($file) {
1060 $tmp = false;
1061 if (substr($file, 0, 4) === 'tmp:') {
1062 $file = substr($file, 4);
1063 $tmp = true;
1064 }
1065 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
1066 if ($tmp) {
1067 $this->helper->add('elementor_attachments', $file);
1068 }
1069 } else {
1070 $this->helper->add('elementor_attachments', $file);
1071 }
1072 }
1073 $message = str_replace($shortcode_value, '', $message);
1074 } else {
1075 if (!isset($atts['dataset']) && isset($atts['id'])) {
1076 $template = new Model_E2pdf_Template();
1077 $template->load($atts['id']);
1078
1079 if ($template->get('extension') === 'elementor') {
1080 $atts['dataset'] = $this->get('dataset');
1081 $shortcode[3] .= ' dataset="' . $this->get('dataset') . '"';
1082 }
1083 }
1084 if (!isset($atts['apply'])) {
1085 $shortcode[3] .= ' apply="true"';
1086 }
1087 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
1088 }
1089 }
1090 add_filter('wp_mail', array($this, 'filter_wp_mail'), 30);
1091 }
1092 }
1093 return $message;
1094 }
1095
1096 // filter success message
1097 public function filter_success_message($message) {
1098 if (false !== strpos($message, '[')) {
1099 $shortcode_tags = array(
1100 'e2pdf-download',
1101 'e2pdf-save',
1102 'e2pdf-view',
1103 'e2pdf-adobesign',
1104 'e2pdf-zapier',
1105 );
1106 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1107 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1108 if (!empty($tagnames)) {
1109 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
1110 foreach ($shortcodes[0] as $key => $shortcode_value) {
1111 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1112 $atts = shortcode_parse_atts($shortcode[3]);
1113 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
1114 } else {
1115 if (!isset($atts['dataset']) && isset($atts['id'])) {
1116 $template = new Model_E2pdf_Template();
1117 $template->load($atts['id']);
1118
1119 if ($template->get('extension') === 'elementor') {
1120 $atts['dataset'] = $this->get('dataset');
1121 $shortcode[3] .= ' dataset="' . $this->get('dataset') . '"';
1122 }
1123 }
1124 if (!isset($atts['apply'])) {
1125 $shortcode[3] .= ' apply="true"';
1126 }
1127 if (!isset($atts['iframe_download'])) {
1128 $shortcode[3] .= ' iframe_download="true"';
1129 }
1130 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
1131 }
1132 }
1133 }
1134 }
1135 return $message;
1136 }
1137
1138 // filter wp mail
1139 public function filter_wp_mail($args = array()) {
1140 $files = $this->helper->get('elementor_attachments');
1141 if (is_array($files) && !empty($files)) {
1142 foreach ($files as $file) {
1143 $args['attachments'][] = $file;
1144 }
1145 }
1146 $wp_mail = array(
1147 'to' => $args['to'],
1148 'subject' => $args['subject'],
1149 'message' => $args['message'],
1150 'headers' => $args['headers'],
1151 'attachments' => $args['attachments'],
1152 );
1153
1154 return $wp_mail;
1155 }
1156
1157 // render field value
1158 public function repeater_field_value($value) {
1159 $items = [];
1160 $dom = new DOMDocument();
1161 $html = $this->helper->load('convert')->load_html($value, $dom);
1162 if ($html) {
1163 $ol = $dom->getElementsByTagName('ol')->item(0);
1164 if ($ol) {
1165 foreach ($ol->getElementsByTagName('li') as $li) {
1166 $nestedUl = $li->getElementsByTagName('ul')->item(0);
1167 if ($nestedUl) {
1168 $data = [];
1169 foreach ($nestedUl->getElementsByTagName('li') as $innerLi) {
1170 $parts = explode(' : ', $innerLi->textContent, 2);
1171 if (count($parts) === 2) {
1172 $key = trim($parts[0]);
1173 $value = trim($parts[1]);
1174 $data[$key] = $value;
1175 }
1176 }
1177 $items[] = $data;
1178 }
1179 }
1180 }
1181 }
1182 return serialize($items); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
1183 }
1184
1185 // replace label shortcodes
1186 public function replace_label_shortcodes($setting, $urlencode = false) {
1187 return preg_replace_callback(
1188 '/(\[field[^]]*id="((\w+):label)"[^]]*\])/', function ($matches) use ($urlencode) {
1189 $value = '';
1190 if (isset($this->get('cached_data')[$matches[2]])) {
1191 $value = sanitize_text_field($this->get('cached_data')[$matches[2]]);
1192 }
1193 return $value;
1194 }, $setting
1195 );
1196 }
1197 }
1198