PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.55
E2Pdf – Export Pdf Tool for WordPress v1.32.55
1.32.55 1.32.56 1.32.53 1.32.51 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 All 76 releases
e2pdf / classes / extension / e2pdf-everest.php

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

1,304 lines 58.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-everest.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_Everest extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = array(
18 'key' => 'everest',
19 'title' => 'Everest 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_EVEREST_EXTENSION') || $this->helper->load('extension')->is_plugin_active('everest-forms/everest-forms.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 $this->set('cached_form_data', false);
51 $form = get_post($this->get('item'));
52 if (function_exists('evf_decode') && $form && !empty($form->post_content)) {
53 $this->set('cached_form', $form);
54 $this->set('cached_form_data', evf_decode($this->get('cached_form')->post_content));
55 }
56 break;
57 case 'dataset':
58 $this->set('cached_entry', false);
59 $this->set('cached_fields', false);
60 if (function_exists('evf_decode') && function_exists('evf_get_entry') && $this->get('cached_form') && $this->get('dataset')) {
61 $entry = evf_get_entry($this->get('dataset'), false);
62 if (!empty($entry->form_id) && $entry->form_id == $this->get('cached_form')->ID) {
63 $this->set('cached_entry', $entry);
64 $this->set('cached_fields', evf_decode($entry->fields));
65 }
66 }
67 break;
68 default:
69 break;
70 }
71 return true;
72 }
73
74 // get
75 public function get($key) {
76 if (isset($this->options->$key)) {
77 $value = $this->options->$key;
78 } else {
79 switch ($key) {
80 case 'args':
81 $value = array();
82 break;
83 default:
84 $value = false;
85 break;
86 }
87 }
88 return $value;
89 }
90
91 // load actions
92 public function load_actions() {
93 add_action('everest_forms_email_send_after', array($this, 'action_everest_forms_email_send_after'));
94 add_action('everest_forms_entry_details_sidebar_details', array($this, 'hook_everest_entry_view'), 10);
95 }
96
97 // load filters
98 public function load_filters() {
99 add_filter('everest_forms_add_success', array($this, 'filter_everest_forms_add_success'));
100 add_filter('everest_forms_email_attachments', array($this, 'filter_everest_forms_email_attachments'), 10, 2);
101 add_filter('everest_forms_email_message', array($this, 'filter_everest_forms_email_message'), 10);
102 add_filter('everest_forms_email_template_message', array($this, 'filter_everest_forms_email_message'), 10);
103 add_filter('everest_forms_entry_table_actions', array($this, 'hook_everest_row_actions'), 10, 2);
104 add_filter('everest_forms_after_success_ajax_message', array($this, 'filter_everest_forms_after_success_ajax_message'), 99, 3);
105
106 // extension hooks
107 add_filter('e2pdf_extension_hooks', function ($hooks) {
108 $hooks[$this->info['key']] = [
109 'hook_everest_entry_view' => __('WP Admin Entry View', 'e2pdf'),
110 'hook_everest_row_actions' => __('WP Admin Entry Row Actions', 'e2pdf')
111 ];
112 return $hooks;
113 });
114 }
115
116 // items
117 public function items() {
118 $items = array();
119 $forms = get_posts(
120 array(
121 'post_status' => 'publish',
122 'posts_per_page' => -1,
123 'post_type' => 'everest_form',
124 )
125 );
126 if ($forms) {
127 foreach ($forms as $key => $form) {
128 $items[] = $this->item($form->ID);
129 }
130 }
131 return $items;
132 }
133
134 // item
135 public function item($item_id = false) {
136 if (!$item_id && $this->get('item')) {
137 $item_id = $this->get('item');
138 }
139 $item = new stdClass();
140 $form = get_post($item_id);
141 if ($form) {
142 $item->id = (string) $item_id;
143 $item->name = $form->post_title ? $form->post_title : $item_id;
144 $item->url = $this->helper->get_url(
145 array(
146 'page' => 'evf-builder',
147 'tab' => 'fields',
148 'form_id' => $item_id,
149 'action' => 'edit',
150 ), 'admin.php?'
151 );
152 } else {
153 $item->id = '';
154 $item->name = '';
155 $item->url = '';
156 }
157 return $item;
158 }
159
160 // datasets
161 public function datasets($item_id = false, $name = false) {
162 $datasets = array();
163 if (function_exists('evf_get_entries_ids')) {
164 $entries = array_reverse(evf_get_entries_ids($item_id));
165 $this->set('item', $item_id);
166 foreach ($entries as $key => $entry) {
167 $this->set('dataset', $entry);
168 $entry_title = $this->render($name);
169 if (!$entry_title) {
170 $entry_title = $entry;
171 }
172 $datasets[] = array(
173 'key' => $entry,
174 'value' => $entry_title,
175 );
176 }
177 }
178 return $datasets;
179 }
180
181 // get dataset actions
182 public function get_dataset_actions($dataset_id = false) {
183 $dataset_id = (int) $dataset_id;
184 if (!$dataset_id) {
185 return false;
186 }
187 $item = evf_get_entry($dataset_id);
188 $actions = new stdClass();
189 $actions->view = $this->helper->get_url(
190 array(
191 'page' => 'evf-entries',
192 'form_id' => !empty($item->form_id) ? $item->form_id : '0',
193 'view-entry' => $dataset_id,
194 )
195 );
196 $actions->delete = false;
197 return $actions;
198 }
199
200 // render
201 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
202 $value = $this->render_shortcodes($value, $field);
203 if (!$raw) {
204 $value = $this->strip_shortcodes($value);
205 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
206 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
207 }
208 return $value;
209 }
210
211 // render shortcodes
212 public function render_shortcodes($value, $field = array()) {
213 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
214 if ($this->verify()) {
215 if (false !== strpos($value, '[')) {
216 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
217 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
218 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
219 }
220 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
221
222 add_filter('everest_forms_process_smart_tags', array($this, 'filter_everest_forms_process_smart_tags'), 0, 3);
223 $value = apply_filters('everest_forms_process_smart_tags', $value, $this->get('cached_form_data'), $this->get('cached_fields'), $this->get('dataset'));
224 remove_filter('everest_forms_process_smart_tags', array($this, 'filter_everest_forms_process_smart_tags'), 99);
225
226 $value = $this->helper->load('field')->render(
227 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
228 $this,
229 $field
230 );
231 }
232 return apply_filters(
233 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
234 );
235 }
236
237 // strip shortcodes
238 public function strip_shortcodes($value) {
239 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
240 $value = preg_replace('~a\:\d+\:{[^}]*}(*SKIP)(*FAIL)|{[^}]*}~', '', $value);
241 return $value;
242 }
243
244 // strip math
245 public function strip_math($value, &$replacements) {
246 if ($value) {
247 preg_match_all('/\{[^}]+\}/', $value, $matches);
248 foreach ($matches[0] as $match) {
249 $index = count($replacements) + 1;
250 $replacements[] = $match;
251 $value = str_replace($match, '%' . $index . '$s', $value);
252 }
253 }
254 return $value;
255 }
256
257 // convert shortcodes
258 public function convert_shortcodes($value, $to = false, $html = false) {
259 if ($value) {
260 if ($to) {
261 $value = stripslashes_deep($value);
262 $value = str_replace('&#91;', '[', $value);
263 if (!$html) {
264 $value = wp_specialchars_decode($value, ENT_QUOTES);
265 }
266 } else {
267 $value = str_replace('[', '&#91;', $value);
268 }
269 }
270 return $value;
271 }
272
273 // verify
274 public function verify() {
275 if ($this->get('cached_entry')) {
276 return true;
277 }
278 return false;
279 }
280
281 // visual mapper
282 public function visual_mapper() {
283 $source = '';
284 $html = '';
285 if ($this->get('item')) {
286 $source = do_shortcode('[everest_form id="' . $this->get('item') . '"]');
287 if ($source) {
288 $dom = new DOMDocument();
289 $html = $this->helper->load('convert')->load_html($source, $dom, true);
290 }
291 if (!$source) {
292 return '<div class="e2pdf-vm-error">' . __("The form source is empty or doesn't exist", 'e2pdf') . '</div>';
293 } elseif (!$html) {
294 return '<div class="e2pdf-vm-error">' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '</div>';
295 } else {
296 $form = get_post($this->get('item'));
297 $form_fields = array();
298 if (function_exists('evf_decode') && $form && !empty($form->post_content)) {
299 $post_content = evf_decode($form->post_content);
300 if (!empty($post_content['form_fields'])) {
301 $form_fields = $post_content['form_fields'];
302 }
303 }
304 $xml = $this->helper->load('xml');
305 $xpath = new DomXPath($dom);
306
307 // remove by name
308 $remove_by_name = array(
309 'everest_forms[id]',
310 'everest_forms[author]',
311 '_wpnonce6090',
312 '_wp_http_referer',
313 );
314 foreach ($remove_by_name as $key => $name) {
315 $elements = $xpath->query('//*[@name="' . $name . '"]');
316 foreach ($elements as $element) {
317 $element->parentNode->removeChild($element);
318 }
319 }
320
321 $elements = $xpath->query("//*[contains(@class, 'dropzone-input')]");
322 foreach ($elements as $element) {
323 $field_id = preg_replace('/everest_forms_(\d+)_([^\]\.*]+)/', '$2', $xml->get_node_value($element, 'name'));
324 if (isset($form_fields[$field_id])) {
325 $field_id = $this->get_field_id_for_smarttags($form_fields[$field_id]);
326 $xml->set_node_value($element, 'name', '{field_id="' . $field_id . '"}');
327 $parent = $element->parentNode;
328 if ($parent && strpos($parent->getAttribute('class'), 'evf-field-image-upload') !== false) {
329 $dropzone = $xpath->query(".//*[contains(@class, 'everest-forms-uploader')]", $parent)->item(0);
330 $max_file_number = (int) $xml->get_node_value($dropzone, 'data-max-file-number');
331 if ($max_file_number > 1) {
332 $xml->set_node_value($element, 'name', '[e2pdf-format-output explode=", " output="{0}"]' . $xml->get_node_value($element, 'name') . '[/e2pdf-format-output]');
333 }
334 }
335 }
336 }
337
338 $elements = $xpath->query("//*[contains(@data-field-type, 'repeater-fields')]");
339 foreach ($elements as $element) {
340 $sub_element = $xpath->query(".//div[contains(@class, 'repeater_button_add_remove_label')]", $element)->item(0);
341 if ($sub_element) {
342 $input = $element->ownerDocument->createElement('input');
343 $input->setAttribute('type', 'text');
344 $field_id = $xml->get_node_value($element, 'data-repeater-field-id');
345 if (isset($form_fields[$field_id])) {
346 $field_id = $this->get_field_id_for_smarttags($form_fields[$field_id]);
347 $input->setAttribute('name', '{field_id="' . $field_id . '"}');
348 $sub_element->appendChild($input);
349 }
350 }
351 }
352
353 $elements = $xpath->query("//*[contains(@class, 'evf-signature-input') or contains(@class, 'evf-payment-price') or contains(@class, 'evf-payment-subtotal') or contains(@class, 'evf-payment-total')]");
354 foreach ($elements as $element) {
355 $xml->set_node_value($element, 'type', 'text');
356 }
357
358 $elements = $xpath->query('//input|//textarea|//select');
359 foreach ($elements as $element) {
360 $field_id = preg_replace('/everest_forms\[form_fields\]\[([^\]\.*]+)\].*/', '$1', $xml->get_node_value($element, 'name'));
361 if (isset($form_fields[$field_id])) {
362 $field_id = $this->get_field_id_for_smarttags($form_fields[$field_id]);
363 $xml->set_node_value($element, 'name', '{field_id="' . $field_id . '"}');
364 }
365 }
366 $submit_buttons = $xpath->query("//input[@type='submit']|//button[@type='submit']");
367 foreach ($submit_buttons as $element) {
368 $element->parentNode->removeChild($element);
369 }
370
371 if (defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD')) {
372 return str_replace(array('<html>', '</html>'), '', $dom->saveHTML());
373 } else {
374 return $dom->saveHTML();
375 }
376 }
377 }
378 return false;
379 }
380
381 // auto
382 public function auto() {
383 $response = array();
384 $elements = array();
385 $fields = array();
386
387 $form = evf()->form->get(
388 $this->get('item'),
389 array(
390 'content_only' => true,
391 )
392 );
393 $fields = $form['form_fields'];
394
395 foreach ($fields as $field) {
396
397 $field_value = '{field_id="' . $this->get_field_id_for_smarttags($field) . '"}';
398 $field_label = isset($field['label']) ? $field['label'] : '';
399
400 switch ($field['type']) {
401 case 'text':
402 case 'email':
403 case 'first-name':
404 case 'last-name':
405 case 'number':
406 case 'url':
407 case 'phone':
408 case 'date-time':
409 case 'rating':
410 case 'scale-rating':
411 case 'payment-quantity':
412 $elements[] = $this->auto_field(
413 $field,
414 array(
415 'type' => 'e2pdf-html',
416 'block' => true,
417 'float' => true,
418 'properties' => array(
419 'top' => '20',
420 'left' => '20',
421 'right' => '20',
422 'width' => '100%',
423 'height' => 'auto',
424 'value' => $field_label,
425 ),
426 )
427 );
428 $elements[] = $this->auto_field(
429 $field,
430 array(
431 'type' => 'e2pdf-input',
432 'properties' => array(
433 'top' => '5',
434 'width' => '100%',
435 'height' => 'auto',
436 'value' => $field_value,
437 ),
438 )
439 );
440 break;
441 case 'select':
442 $elements[] = $this->auto_field(
443 $field,
444 array(
445 'type' => 'e2pdf-html',
446 'block' => true,
447 'float' => true,
448 'properties' => array(
449 'top' => '20',
450 'left' => '20',
451 'right' => '20',
452 'width' => '100%',
453 'height' => 'auto',
454 'value' => $field_label,
455 ),
456 )
457 );
458 $field_options = array();
459 foreach ($field['choices'] as $option) {
460 $option_value = isset($option['label']) ? $option['label'] : '';
461 $field_options[] = $option_value;
462 }
463 $elements[] = $this->auto_field(
464 $field,
465 array(
466 'type' => 'e2pdf-select',
467 'properties' => array(
468 'top' => '5',
469 'width' => '100%',
470 'height' => 'auto',
471 'options' => implode("\n", $field_options),
472 'value' => $field_value,
473 ),
474 )
475 );
476 break;
477 case 'yes-no':
478 case 'radio':
479 $elements[] = $this->auto_field(
480 $field,
481 array(
482 'type' => 'e2pdf-html',
483 'block' => true,
484 'float' => true,
485 'properties' => array(
486 'top' => '20',
487 'left' => '20',
488 'right' => '20',
489 'width' => '100%',
490 'height' => 'auto',
491 'value' => $field_label,
492 ),
493 )
494 );
495
496 if ($field['type'] == 'yes-no') {
497 $field['choices'] = array(
498 array(
499 'label' => $field['yes_label'],
500 'value' => $field['yes_value'],
501 ),
502 array(
503 'label' => $field['no_label'],
504 'value' => $field['no_value'],
505 ),
506 );
507 }
508
509 foreach ($field['choices'] as $opt_key => $option) {
510 if ($field['type'] == 'yes-no') {
511 $option_value = isset($option['value']) ? $option['value'] : '';
512 } else {
513 $option_value = isset($option['label']) ? $option['label'] : '';
514 }
515 $elements[] = $this->auto_field(
516 $field,
517 array(
518 'type' => 'e2pdf-radio',
519 'properties' => array(
520 'top' => '5',
521 'width' => 'auto',
522 'height' => 'auto',
523 'value' => $field_value,
524 'option' => $option_value,
525 'group' => $field_value,
526 ),
527 )
528 );
529 $elements[] = $this->auto_field(
530 $field,
531 array(
532 'type' => 'e2pdf-html',
533 'float' => true,
534 'properties' => array(
535 'left' => '5',
536 'width' => '100%',
537 'height' => 'auto',
538 'value' => $option['label'],
539 ),
540 )
541 );
542 }
543 break;
544 case 'checkbox':
545 $elements[] = $this->auto_field(
546 $field,
547 array(
548 'type' => 'e2pdf-html',
549 'block' => true,
550 'float' => true,
551 'properties' => array(
552 'top' => '20',
553 'left' => '20',
554 'right' => '20',
555 'width' => '100%',
556 'height' => 'auto',
557 'value' => $field_label,
558 ),
559 )
560 );
561 foreach ($field['choices'] as $opt_key => $option) {
562 $option_value = isset($option['label']) ? $option['label'] : '';
563 $elements[] = $this->auto_field(
564 $field,
565 array(
566 'type' => 'e2pdf-checkbox',
567 'properties' => array(
568 'top' => '5',
569 'width' => 'auto',
570 'height' => 'auto',
571 'value' => $field_value,
572 'option' => $option_value,
573 ),
574 )
575 );
576 $elements[] = $this->auto_field(
577 $field,
578 array(
579 'type' => 'e2pdf-html',
580 'float' => true,
581 'properties' => array(
582 'left' => '5',
583 'width' => '100%',
584 'height' => 'auto',
585 'value' => $option['label'],
586 ),
587 )
588 );
589 }
590 break;
591 case 'image-upload':
592 $elements[] = $this->auto_field(
593 $field,
594 array(
595 'type' => 'e2pdf-html',
596 'block' => true,
597 'float' => true,
598 'properties' => array(
599 'top' => '20',
600 'left' => '20',
601 'right' => '20',
602 'width' => '100%',
603 'height' => 'auto',
604 'value' => $field_label,
605 ),
606 )
607 );
608
609 $max_file_number = 1;
610 if (!empty($field['max_file_number'])) {
611 $max_file_number = (int) $field['max_file_number'];
612 }
613
614 for ($i = 0; $i < $max_file_number; $i++) {
615 $elements[] = $this->auto_field(
616 $field,
617 array(
618 'type' => 'e2pdf-image',
619 'float' => true,
620 'block' => true,
621 'properties' => array(
622 'top' => '5',
623 'left' => '20',
624 'right' => '0',
625 'width' => '170',
626 'height' => '150',
627 'value' => $max_file_number > 1 ? '[e2pdf-format-output explode=", " output="{' . $i . '}"]' . $field_value . '[/e2pdf-format-output]' : $field_value,
628 ),
629 )
630 );
631 }
632 break;
633 case 'textarea':
634 case 'file-upload':
635 case 'address':
636 case 'country':
637 case 'likert':
638 $elements[] = $this->auto_field(
639 $field,
640 array(
641 'type' => 'e2pdf-html',
642 'block' => true,
643 'float' => true,
644 'properties' => array(
645 'top' => '20',
646 'left' => '20',
647 'right' => '20',
648 'width' => '100%',
649 'height' => 'auto',
650 'value' => $field_label,
651 ),
652 )
653 );
654
655 $elements[] = $this->auto_field(
656 $field,
657 array(
658 'type' => 'e2pdf-textarea',
659 'properties' => array(
660 'top' => '5',
661 'width' => '100%',
662 'height' => 'auto',
663 'value' => $field_value,
664 ),
665 )
666 );
667 break;
668 case 'privacy-policy':
669 $elements[] = $this->auto_field(
670 $field,
671 array(
672 'type' => 'e2pdf-html',
673 'block' => true,
674 'float' => true,
675 'properties' => array(
676 'top' => '20',
677 'left' => '20',
678 'right' => '20',
679 'width' => '100%',
680 'height' => 'auto',
681 'value' => $field_label,
682 ),
683 )
684 );
685
686 $elements[] = $this->auto_field(
687 $field,
688 array(
689 'type' => 'e2pdf-checkbox',
690 'properties' => array(
691 'top' => '5',
692 'width' => 'auto',
693 'height' => 'auto',
694 'value' => $field_value,
695 'option' => isset($field['consent_message']) ? $field['consent_message'] : '',
696 ),
697 )
698 );
699
700 $elements[] = $this->auto_field(
701 $field,
702 array(
703 'type' => 'e2pdf-html',
704 'float' => true,
705 'properties' => array(
706 'left' => '5',
707 'width' => '100%',
708 'height' => 'auto',
709 'value' => $field['consent_message'],
710 ),
711 )
712 );
713 break;
714 case 'signature':
715 $elements[] = $this->auto_field(
716 $field,
717 array(
718 'type' => 'e2pdf-html',
719 'block' => true,
720 'float' => true,
721 'properties' => array(
722 'top' => '20',
723 'left' => '20',
724 'right' => '20',
725 'width' => '100%',
726 'height' => 'auto',
727 'value' => isset($field['settings']['label']) ? $field['settings']['label'] : '',
728 ),
729 )
730 );
731
732 $elements[] = $this->auto_field(
733 $field,
734 array(
735 'type' => 'e2pdf-signature',
736 'properties' => array(
737 'top' => '5',
738 'width' => '100%',
739 'height' => '150',
740 'dimension' => '1',
741 'block_dimension' => '1',
742 'value' => $field_value,
743 ),
744 )
745 );
746 break;
747 case 'wysiwyg':
748 $elements[] = $this->auto_field(
749 $field,
750 array(
751 'type' => 'e2pdf-html',
752 'block' => true,
753 'float' => true,
754 'properties' => array(
755 'top' => '20',
756 'left' => '20',
757 'right' => '20',
758 'width' => '100%',
759 'height' => 'auto',
760 'value' => $field_label,
761 ),
762 )
763 );
764
765 $elements[] = $this->auto_field(
766 $field,
767 array(
768 'type' => 'e2pdf-html',
769 'properties' => array(
770 'top' => '5',
771 'width' => '100%',
772 'height' => '150',
773 'value' => $field_value,
774 ),
775 )
776 );
777 break;
778 case 'html':
779 $elements[] = $this->auto_field(
780 $field,
781 array(
782 'type' => 'e2pdf-html',
783 'block' => true,
784 'float' => true,
785 'properties' => array(
786 'top' => '20',
787 'left' => '20',
788 'right' => '20',
789 'width' => '100%',
790 'height' => 'auto',
791 'value' => isset($field['code']) ? $field['code'] : '',
792 ),
793 )
794 );
795 break;
796 case 'title':
797 $elements[] = $this->auto_field(
798 $field,
799 array(
800 'type' => 'e2pdf-html',
801 'block' => true,
802 'float' => true,
803 'properties' => array(
804 'top' => '20',
805 'left' => '20',
806 'right' => '20',
807 'width' => '100%',
808 'height' => 'auto',
809 'value' => $field_label,
810 ),
811 )
812 );
813 if (!empty($field['description'])) {
814 $elements[] = $this->auto_field(
815 $field,
816 array(
817 'type' => 'e2pdf-html',
818 'block' => true,
819 'float' => true,
820 'properties' => array(
821 'top' => '5',
822 'left' => '20',
823 'right' => '20',
824 'width' => '100%',
825 'height' => 'auto',
826 'value' => isset($field['description']) ? $field['description'] : '',
827 ),
828 )
829 );
830 }
831 break;
832 case 'payment-single':
833 $elements[] = $this->auto_field(
834 $field,
835 array(
836 'type' => 'e2pdf-html',
837 'block' => true,
838 'float' => true,
839 'properties' => array(
840 'top' => '20',
841 'left' => '20',
842 'right' => '20',
843 'width' => '100%',
844 'height' => 'auto',
845 'value' => $field_label,
846 ),
847 )
848 );
849 if (!empty($field['description'])) {
850 $elements[] = $this->auto_field(
851 $field,
852 array(
853 'type' => 'e2pdf-html',
854 'block' => true,
855 'float' => true,
856 'properties' => array(
857 'top' => '5',
858 'left' => '20',
859 'right' => '20',
860 'width' => '100%',
861 'height' => 'auto',
862 'value' => isset($field['description']) ? $field['description'] : '',
863 ),
864 )
865 );
866 }
867 $elements[] = $this->auto_field(
868 $field,
869 array(
870 'type' => 'e2pdf-html',
871 'properties' => array(
872 'top' => '5',
873 'width' => '100%',
874 'height' => 'auto',
875 'value' => $field_value,
876 ),
877 )
878 );
879
880 break;
881 case 'payment-subtotal':
882 case 'payment-total':
883 $elements[] = $this->auto_field(
884 $field,
885 array(
886 'type' => 'e2pdf-html',
887 'block' => true,
888 'float' => true,
889 'properties' => array(
890 'top' => '20',
891 'left' => '20',
892 'right' => '20',
893 'width' => '100%',
894 'height' => 'auto',
895 'value' => $field_label,
896 ),
897 )
898 );
899 $elements[] = $this->auto_field(
900 $field,
901 array(
902 'type' => 'e2pdf-html',
903 'properties' => array(
904 'top' => '5',
905 'width' => '100%',
906 'height' => 'auto',
907 'value' => $field_value,
908 ),
909 )
910 );
911 break;
912 case 'divider':
913 case 'hidden':
914 case 'repeater-fields':
915 break;
916 default:
917 break;
918 }
919 }
920
921 $response['page'] = array(
922 'bottom' => '20',
923 'top' => '20',
924 );
925 $response['elements'] = $elements;
926 return $response;
927 }
928
929 // auto field
930 public function auto_field($field = false, $element = array()) {
931 if (!$field) {
932 return false;
933 }
934 if (!isset($element['block'])) {
935 $element['block'] = false;
936 }
937 if (!isset($element['float'])) {
938 $element['float'] = false;
939 }
940 return $element;
941 }
942
943 // styles
944 public function styles($item_id = false) {
945 $styles = array();
946 if (function_exists('evf')) {
947 $styles[] = evf()->plugin_url() . '/assets/css/everest-forms.css?v=' . time();
948 }
949 $styles[] = plugins_url('css/extension/everest.css?v=' . time(), $this->helper->get('plugin_file_path'));
950 return $styles;
951 }
952
953 // get field id for smarttags
954 public function get_field_id_for_smarttags($field) {
955 $field_id = isset($field['id']) ? $field['id'] : '';
956 $field_label = isset($field['label']) ? $field['label'] : '';
957 $field_type = isset($field['type']) ? $field['type'] : '';
958 if ('fullname' !== $field_id && 'email' !== $field_id && 'subject' !== $field_id && 'message' !== $field_id) {
959 $field_label = preg_split('/[\s\-\_]/', $field_label);
960 foreach ($field_label as $key => $value) {
961 if (0 === $key) {
962 $field_label[$key] = strtolower($value);
963 } else {
964 $field_label[$key] = ucfirst($value);
965 }
966 }
967 $field_label = implode('', $field_label);
968 $field_id = $field_label . '_' . $field_id;
969 } else {
970 $field_id = $field_id;
971 }
972 if ($field_type != 'repeater-fields' && isset($field['repeater-fields']) && $field['repeater-fields'] == 'yes') {
973 $field_id .= '_1';
974 }
975 return $field_id;
976 }
977
978 // get merge tag value
979 public function get_merge_tag_value($value) {
980 if (isset($value['type'])) {
981 switch ($value['type']) {
982 case 'checkbox':
983 case 'payment-checkbox':
984 $value = isset($value['label']) ? implode(', ', $value['label']) : '';
985 break;
986 case 'country':
987 $value = isset($value['country_code']) ? $value['country_code'] : '';
988 break;
989 case 'image-upload':
990 case 'file-upload':
991 $files = [];
992 $uploads = wp_upload_dir();
993 if (!empty($value['value_raw'])) {
994 foreach ($value['value_raw'] as $files_key => $files_value) {
995 if (!is_array($files_value['value']) && false !== strpos($files_value['value'], $uploads['basedir'])) {
996 $value = trailingslashit(content_url()) . str_replace(str_replace('uploads', '', $uploads['basedir']), '', $files_value['value']);
997 }
998 if (!empty($value)) {
999 $files[] = $files_value['value'];
1000 }
1001 }
1002 }
1003 $value = implode(', ', $files);
1004 break;
1005 }
1006 return $value;
1007 }
1008 return '';
1009 }
1010
1011 // email send after action
1012 public function action_everest_forms_email_send_after() {
1013 $files = $this->helper->get('tmp_everest_attachments');
1014 if (is_array($files) && !empty($files)) {
1015 foreach ($files as $key => $file) {
1016 $this->helper->delete_dir(dirname($file) . '/');
1017 }
1018 $this->helper->deset('tmp_everest_attachments');
1019 $this->helper->deset('everest_attachments');
1020 }
1021 }
1022
1023 // before template part action
1024 public function action_everest_forms_before_template_part($template_name) {
1025 if ($template_name == 'notices/success.php') {
1026 ob_start();
1027 }
1028 }
1029
1030 // after template part action
1031 public function action_everest_forms_after_template_part($template_name) {
1032 if ($template_name == 'notices/success.php') {
1033 $content = ob_get_clean();
1034 $content = $this->filter_message($content);
1035 echo $content;
1036 remove_action('everest_forms_before_template_part', array($this, 'action_everest_forms_before_template_part'), 99);
1037 remove_action('everest_forms_after_template_part', array($this, 'action_everest_forms_after_template_part'), 99);
1038 }
1039 }
1040
1041 // after success ajax message filter
1042 public function filter_everest_forms_after_success_ajax_message($response_data, $form_data, $entry) {
1043 if (!empty($response_data['message'])) {
1044 $response_data['message'] = $this->filter_message($response_data['message']);
1045 }
1046 return $response_data;
1047 }
1048
1049 // email attachments filter
1050 public function filter_everest_forms_email_attachments($attachments, $mail) {
1051 $files = $this->helper->get('everest_attachments');
1052 if (is_array($files) && !empty($files)) {
1053 $attachments = $this->helper->load('convert')->is_string_array($attachments);
1054 foreach ($files as $key => $file) {
1055 $attachments[] = $file;
1056 }
1057 }
1058 return $attachments;
1059 }
1060
1061 // email message filter
1062 public function filter_everest_forms_email_message($message) {
1063 $message = $this->filter_message($message, 'mail');
1064 return $message;
1065 }
1066
1067 // add success filter
1068 public function filter_everest_forms_add_success($message) {
1069 if (is_string($message) && false !== strpos($message, '[e2pdf-')) {
1070 add_action('everest_forms_before_template_part', array($this, 'action_everest_forms_before_template_part'), 99);
1071 add_action('everest_forms_after_template_part', array($this, 'action_everest_forms_after_template_part'), 99);
1072 }
1073 return $message;
1074 }
1075
1076 // process smart tags filter
1077 public function filter_everest_forms_process_smart_tags($content, $form_data, $fields = '') {
1078
1079 preg_match_all('/\{field_id="(.+?)"\}/', $content, $ids);
1080
1081 if (!empty($ids[1]) && !empty($fields)) {
1082 foreach ($ids[1] as $key => $field_id) {
1083 $mixed_field_id = explode('_', $field_id);
1084 $sub_field_id = isset($mixed_field_id[1]) ? $mixed_field_id['1'] : '';
1085
1086 $repeater_index = false;
1087 if (isset($mixed_field_id[2])) {
1088 $field_id = $mixed_field_id[0] . '_' . $sub_field_id;
1089 $repeater_index = $mixed_field_id[2];
1090 }
1091
1092 if ('fullname' !== $field_id && 'email' !== $field_id && 'subject' !== $field_id && 'message' !== $field_id) {
1093 $value = isset($fields[$sub_field_id]['value']) && !empty($fields[$sub_field_id]['value']) ? evf_sanitize_textarea_field($fields[$sub_field_id]['value']) : '';
1094 } else {
1095 $value = isset($fields[$field_id]['value']) && !empty($fields[$field_id]['value']) ? evf_sanitize_textarea_field($fields[$field_id]['value']) : '';
1096 }
1097 $value = apply_filters('everest_forms_smart_tags_value', $value, $field_id, $fields, $form_data);
1098
1099 // repeater
1100 if (!isset($fields[$sub_field_id])) {
1101 if (!empty($form_data['form_fields'])) {
1102 foreach ($form_data['form_fields'] as $fid => $field) {
1103 if ('repeater-fields' === $field['type']) {
1104 if (!empty($fields[$field['id']])) {
1105 $value = '';
1106 $repeater_rows = $fields[$field['id']]['value_raw'];
1107 if ($repeater_index) {
1108 if (isset($repeater_rows[$repeater_index]) && array_key_exists($sub_field_id, $repeater_rows[$repeater_index])) {
1109 $value = isset($repeater_rows[$repeater_index][$sub_field_id]['value']) && !empty($repeater_rows[$repeater_index][$sub_field_id]['value']) ? evf_sanitize_textarea_field($repeater_rows[$repeater_index][$sub_field_id]['value']) : '';
1110 if (isset($repeater_rows[$repeater_index][$sub_field_id]['type']) && ('image-upload' === $repeater_rows[$repeater_index][$sub_field_id]['type'] || 'file-upload' === $repeater_rows[$repeater_index][$sub_field_id]['type'])) {
1111 $value = $repeater_rows[$repeater_index][$sub_field_id];
1112 }
1113 if (isset($value['type']) && in_array($value['type'], array('country', 'checkbox', 'payment-checkbox', 'image-upload', 'file-upload'), true)) {
1114 $value = $this->get_merge_tag_value($value);
1115 }
1116 }
1117 $content = str_replace('{field_id="' . $field_id . '_' . $repeater_index . '"}', $value, $content);
1118 } else {
1119 $values = array();
1120 foreach ($repeater_rows as $row_id => $row) {
1121 if (array_key_exists($sub_field_id, $row)) {
1122 if (isset($row[$sub_field_id]['type']) && ('image-upload' === $row[$sub_field_id]['type'] || 'file-upload' === $row[$sub_field_id]['type'])) {
1123 $value = $row[$sub_field_id];
1124 }
1125 if (isset($value['type']) && in_array($value['type'], array('country', 'checkbox', 'payment-checkbox', 'image-upload', 'file-upload'), true)) {
1126 $values[] = $this->get_merge_tag_value($value['value']);
1127 } else {
1128 $values[] = $row[$sub_field_id]['value'];
1129 }
1130 }
1131 }
1132 $value = implode(',', array_filter($values));
1133 $content = str_replace('{field_id="' . $field_id . '"}', $value, $content);
1134 break;
1135 }
1136 }
1137 }
1138 }
1139 }
1140 } else {
1141 if (isset($fields[$sub_field_id]['type']) && ($fields[$sub_field_id]['type'] === 'image-upload' || $fields[$sub_field_id]['type'] === 'file-upload' || ($fields[$sub_field_id]['type'] === 'repeater-fields' && apply_filters('e2pdf_for_do_shortcode_data_process', false)))) {
1142 $value = $fields[$sub_field_id];
1143 }
1144 if (isset($value['type'])) {
1145 if ($value['type'] === 'repeater-fields') {
1146 $repeaters = array();
1147 if (!empty($value['value_raw']) && is_array($repeaters)) {
1148 $repeaters = $value['value_raw'];
1149 }
1150 $content = str_replace('{field_id="' . $field_id . '"}', serialize($repeaters), $content); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
1151 } elseif (in_array($value['type'], array('country', 'checkbox', 'payment-checkbox', 'image-upload', 'file-upload'), true)) {
1152 $content = str_replace('{field_id="' . $field_id . '"}', $this->get_merge_tag_value($value), $content);
1153 }
1154 }
1155 }
1156 }
1157 }
1158 return $content;
1159 }
1160
1161 // message filter
1162 public function filter_message($message = '', $type = 'message') {
1163 if (false !== strpos($message, '[')) {
1164 $shortcode_tags = array(
1165 'e2pdf-download',
1166 'e2pdf-save',
1167 'e2pdf-view',
1168 'e2pdf-adobesign',
1169 'e2pdf-zapier',
1170 'e2pdf-attachment',
1171 );
1172 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1173 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1174 if (!empty($tagnames)) {
1175 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
1176 foreach ($shortcodes[0] as $key => $shortcode_value) {
1177 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1178 $atts = shortcode_parse_atts($shortcode[3]);
1179 if (!isset($atts['dataset']) && isset($atts['id'])) {
1180 $template = new Model_E2pdf_Template();
1181 $template->load($atts['id']);
1182 if ($template->get('extension') === 'everest') {
1183 $atts['dataset'] = evf()->task->entry_id;
1184 $shortcode[3] .= ' dataset="' . evf()->task->entry_id . '"';
1185 }
1186 }
1187 if (!isset($atts['apply'])) {
1188 $shortcode[3] .= ' apply="true"';
1189 }
1190 if ($type == 'message' && !isset($atts['iframe_download'])) {
1191 $shortcode[3] .= ' iframe_download="true"';
1192 }
1193 $file = false;
1194 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1195 if ($type == 'mail') {
1196 $file = do_shortcode_tag($shortcode);
1197 if ($file) {
1198 $tmp = false;
1199 if (substr($file, 0, 4) === 'tmp:') {
1200 $file = substr($file, 4);
1201 $tmp = true;
1202 }
1203 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
1204 if ($tmp) {
1205 $this->helper->add('tmp_everest_attachments', $file);
1206 }
1207 } else {
1208 $this->helper->add('tmp_everest_attachments', $file);
1209 }
1210 $this->helper->add('everest_attachments', $file);
1211 }
1212 }
1213 $message = str_replace($shortcode_value, '', $message);
1214 } else {
1215 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
1216 }
1217 }
1218 }
1219 }
1220 return $message;
1221 }
1222
1223 // row actions hook
1224 public function hook_everest_row_actions($actions, $entry) {
1225 if (!empty($entry->form_id)) {
1226 $hooks = $this->helper->load('hooks')->get('everest', 'hook_everest_row_actions', $entry->form_id);
1227 if (!empty($hooks)) {
1228 foreach ($hooks as $hook) {
1229 if ($this->helper->load('hooks')->process_hook(
1230 $hook,
1231 [
1232 'dataset' => $entry->entry_id,
1233 ],
1234 'hook_everest_row_actions'
1235 )
1236 ) {
1237 $action = apply_filters(
1238 'e2pdf_hook_action_button',
1239 array(
1240 'html' => '<a class="e2pdf-download-hook" target="_blank" href="%s">%s</a>',
1241 'url' => $this->helper->get_url(
1242 array(
1243 'page' => 'e2pdf',
1244 'action' => 'export',
1245 'id' => $hook,
1246 'dataset' => $entry->entry_id,
1247 ), 'admin.php?'
1248 ),
1249 'title' => 'PDF #' . $hook,
1250 ), 'hook_everest_row_actions', $hook, $entry->entry_id
1251 );
1252 if (!empty($action)) {
1253 $actions['e2pdf_' . $hook] = sprintf(
1254 $action['html'], $action['url'], $action['title']
1255 );
1256 }
1257 }
1258 }
1259 }
1260 }
1261 return $actions;
1262 }
1263
1264 // entry view hook
1265 public function hook_everest_entry_view($entry) {
1266 if (!empty($entry->form_id)) {
1267 $hooks = $this->helper->load('hooks')->get('everest', 'hook_everest_entry_view', $entry->form_id);
1268 if (!empty($hooks)) {
1269 foreach ($hooks as $hook) {
1270 if ($this->helper->load('hooks')->process_hook(
1271 $hook,
1272 [
1273 'dataset' => $entry->entry_id,
1274 ],
1275 'hook_everest_entry_view'
1276 )
1277 ) {
1278 $action = apply_filters(
1279 'e2pdf_hook_action_button',
1280 array(
1281 'html' => '<p><a class="e2pdf-download-hook" target="_blank" title="%2$s" href="%1$s"><span class="dashicons dashicons-pdf"></span> %2$s</a></p>',
1282 'url' => $this->helper->get_url(
1283 array(
1284 'page' => 'e2pdf',
1285 'action' => 'export',
1286 'id' => $hook,
1287 'dataset' => $entry->entry_id,
1288 ), 'admin.php?'
1289 ),
1290 'title' => 'PDF #' . $hook,
1291 ), 'hook_everest_entry_view', $hook, $entry->entry_id
1292 );
1293 if (!empty($action)) {
1294 echo sprintf(
1295 $action['html'], $action['url'], $action['title']
1296 );
1297 }
1298 }
1299 }
1300 }
1301 }
1302 }
1303 }
1304