PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
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-metform.php

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

1,276 lines 55.0 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-metform.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_Metform extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = array(
18 'key' => 'metform',
19 'title' => 'MetForm',
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_METFORM_EXTENSION') || $this->helper->load('extension')->is_plugin_active('metform/metform.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', get_post($this->get('item')));
50 break;
51 case 'dataset':
52 $this->set('cached_entry', false);
53 if ($this->get('cached_form') && $this->get('dataset')) {
54 if (get_post_meta($this->get('dataset'), 'metform_entries__form_id', true) == $this->get('cached_form')->ID) {
55 $form_data = get_post_meta($this->get('dataset'), 'metform_entries__form_data', true);
56 if (is_array($form_data)) {
57 foreach ($form_data as $data_key => $data) {
58 if (is_array($data) && $this->is_repeater($data)) {
59 $raw = array();
60 foreach ($data as $sub_data_key => $sub_data) {
61 $form_data[$data_key . '-' . $sub_data_key] = $sub_data;
62 if (preg_match('/-(\d+)$/', $sub_data_key, $matches)) {
63 $raw[$matches[1]][$sub_data_key] = $sub_data;
64 }
65 }
66 $form_data[$data_key] = serialize($raw); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
67 }
68 }
69 $uploads_data = get_post_meta($this->get('dataset'), 'metform_entries__file_upload_new', true);
70 if (is_array($uploads_data)) {
71 foreach ($uploads_data as $data_key => $data) {
72 $uploads = array();
73 if (is_array($data)) {
74 foreach ($data as $upload) {
75 $uploads[] = $upload['url'];
76 }
77 }
78 $form_data[$data_key] = implode(',', $uploads);
79 }
80 }
81 if (!isset($form_data['mf_id'])) {
82 $form_data['mf_id'] = $this->get('dataset');
83 }
84 $post = get_post($this->get('dataset'));
85 if ($post) {
86 if (!isset($form_data['mf_post_date'])) {
87 $form_data['mf_post_date'] = $post->post_date;
88 }
89 if (!isset($form_data['mf_post_date_gmt'])) {
90 $form_data['mf_post_date_gmt'] = $post->post_date_gmt;
91 }
92 if (!isset($form_data['mf_post_modified'])) {
93 $form_data['mf_post_modified'] = $post->post_modified;
94 }
95 if (!isset($form_data['mf_post_modified_gmt'])) {
96 $form_data['mf_post_modified_gmt'] = $post->post_modified_gmt;
97 }
98 }
99 if (!isset($form_data['mf_form_name'])) {
100 $form_data['mf_form_name'] = get_the_title($this->get('cached_form')->ID);
101 }
102 if (!isset($form_data['mf_user_id'])) {
103 $form_data['mf_user_id'] = get_post_meta($this->get('dataset'), 'metform_entries__user_id', true);
104 }
105 if (!isset($form_data['mf_payment_status'])) {
106 $form_data['mf_payment_status'] = get_post_meta($this->get('dataset'), 'metform_entries__payment_status', true);
107 }
108 $browser_data = get_post_meta($this->get('dataset'), 'metform_form__entry_browser_data', true);
109 if (is_array($browser_data)) {
110 foreach ($browser_data as $browser_key => $browser) {
111 if (!isset($form_data['mf_browser_' . $browser_key])) {
112 $form_data['mf_browser_' . $browser_key] = $browser;
113 }
114 }
115 }
116 $cached_entry = \MetForm\Core\Entries\Metform_Shortcode::instance();
117 $cached_entry->set_values($form_data);
118 $this->set('cached_entry', $cached_entry);
119 }
120 }
121 }
122 break;
123 default:
124 break;
125 }
126 return true;
127 }
128
129 // get
130 public function get($key) {
131 if (isset($this->options->$key)) {
132 $value = $this->options->$key;
133 } else {
134 switch ($key) {
135 case 'args':
136 $value = array();
137 break;
138 default:
139 $value = false;
140 break;
141 }
142 }
143 return $value;
144 }
145
146 // load actions
147 public function load_actions() {
148 add_action('metform_after_store_form_data', array($this, 'action_metform_after_store_form_data'));
149 add_action('add_meta_boxes', array($this, 'hook_metform_entry_view'));
150 }
151
152 // load filters
153 public function load_filters() {
154 add_filter('post_row_actions', array($this, 'hook_metform_row_actions'), 10, 2);
155 add_filter('manage_metform-entry_posts_columns', array($this, 'hook_metform_entry_row_column'), 11);
156 }
157
158 // items
159 public function items() {
160 $items = array();
161 $forms = get_posts(
162 array(
163 'post_status' => 'publish',
164 'posts_per_page' => -1,
165 'post_type' => 'metform-form',
166 )
167 );
168 if ($forms) {
169 foreach ($forms as $key => $form) {
170 $items[] = $this->item($form->ID);
171 }
172 }
173 return $items;
174 }
175
176 // item
177 public function item($item_id = false) {
178 if (!$item_id && $this->get('item')) {
179 $item_id = $this->get('item');
180 }
181
182 $item = new stdClass();
183 $form = get_post($item_id);
184 if ($form) {
185 $item->id = (string) $item_id;
186 $item->name = $form->post_title ? $form->post_title : $item_id;
187 $item->url = $this->helper->get_url(
188 array(
189 'post' => $item_id,
190 'action' => 'elementor',
191 ), 'post.php?'
192 );
193 } else {
194 $item->id = '';
195 $item->name = '';
196 $item->url = '';
197 }
198 return $item;
199 }
200
201 // datasets
202 public function datasets($item_id = false, $name = false) {
203 global $wpdb;
204
205 $datasets = array();
206 $entries = $wpdb->get_results($wpdb->prepare('SELECT `post_id` FROM `' . $wpdb->prefix . "postmeta` WHERE `meta_key` = 'metform_entries__form_id' AND `meta_value` = %d ORDER BY `post_id` DESC", $item_id), OBJECT);
207 $this->set('item', $item_id);
208 foreach ($entries as $key => $entry) {
209 $this->set('dataset', $entry->post_id);
210 $entry_title = $this->render($name);
211 if (!$entry_title) {
212 $entry_title = $entry->post_id;
213 }
214 $datasets[] = array(
215 'key' => $entry->post_id,
216 'value' => $entry_title,
217 );
218 }
219
220 return $datasets;
221 }
222
223 // get dataset actions
224 public function get_dataset_actions($dataset_id = false) {
225 $dataset_id = (int) $dataset_id;
226 if (!$dataset_id) {
227 return false;
228 }
229 $actions = new stdClass();
230 $actions->view = $this->helper->get_url(
231 array(
232 'post' => $dataset_id,
233 'action' => 'edit',
234 ), 'post.php?'
235 );
236 $actions->delete = false;
237 return $actions;
238 }
239
240 // render
241 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
242 $value = $this->render_shortcodes($value, $field);
243 if (!$raw) {
244 $value = $this->strip_shortcodes($value);
245 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
246 $value = $this->helper->load('field')->render_checkbox($value, $this, $field, ',');
247 $value = $this->helper->load('field')->render_select_multiline($value, $this, $field, ',');
248 }
249 return $value;
250 }
251
252 // render shortcodes
253 public function render_shortcodes($value, $field = array()) {
254 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
255 if ($this->verify()) {
256 if (false !== strpos($value, '[')) {
257 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
258 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
259 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
260 }
261
262 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
263
264 $value = $this->get('cached_entry')->get_process_shortcode($value);
265
266 $value = $this->helper->load('field')->render(
267 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
268 $this,
269 $field
270 );
271 }
272 return apply_filters(
273 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
274 );
275 }
276
277 // strip shortcodes
278 public function strip_shortcodes($value) {
279 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
280 return $value;
281 }
282
283 // convert shortcodes
284 public function convert_shortcodes($value, $to = false, $html = false) {
285 if ($value) {
286 if ($to) {
287 $value = stripslashes_deep($value);
288 $value = str_replace('&#91;', '[', $value);
289 if (!$html) {
290 $value = wp_specialchars_decode($value, ENT_QUOTES);
291 }
292 } else {
293 $value = str_replace('[', '&#91;', $value);
294 }
295 }
296 return $value;
297 }
298
299 // verify
300 public function verify() {
301 if ($this->get('cached_entry')) {
302 return true;
303 }
304 return false;
305 }
306
307 // visual mapper
308 public function visual_mapper() {
309 $html = '';
310 if ($this->get('item')) {
311 $form = $this->auto();
312 $html = '<div class="metform-form">';
313 $float = false;
314 foreach ($form['elements'] as $element) {
315 switch ($element['type']) {
316 case 'e2pdf-html':
317 $html .= '<div>' . $element['properties']['value'] . '</div>';
318 if ($float) {
319 $html .= '<div class="clearfix"></div>';
320 $float = false;
321 }
322 break;
323 case 'e2pdf-input':
324 $html .= '<div><input type="text" name="' . $element['properties']['value'] . '"></div>';
325 break;
326 case 'e2pdf-image':
327 case 'e2pdf-signature':
328 case 'e2pdf-textarea':
329 $html .= '<div><textarea name="' . $element['properties']['value'] . '"></textarea></div>';
330 break;
331 case 'e2pdf-select':
332 $html .= '<div>';
333 $html .= '<select name="' . $element['properties']['value'] . '">';
334 $options = explode("\n", $element['properties']['options']);
335 foreach ($options as $option) {
336 $html .= '<option>' . $option . '</option>';
337 }
338 $html .= '</select>';
339 $html .= '</div>';
340 break;
341 case 'e2pdf-checkbox':
342 $html .= '<div class="metform-checkbox"><input type="checkbox" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
343 $float = true;
344 break;
345 case 'e2pdf-radio':
346 $html .= '<div class="metform-radio"><input type="radio" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
347 $float = true;
348 break;
349 default:
350 break;
351 }
352 }
353 $html .= '</div>';
354 return $html;
355 }
356 return false;
357 }
358
359 // auto
360 public function auto() {
361 $response = array();
362 $elements = array();
363 $fields = array();
364
365 $fields = \MetForm\Core\Entries\Action::instance()->get_fields($this->get('item'));
366 if (is_array($fields)) {
367 foreach ($fields as $field) {
368 $elements = $this->auto_fields($elements, $field);
369 }
370 }
371 $response['page'] = array(
372 'bottom' => '20',
373 'top' => '20',
374 );
375 $response['elements'] = $elements;
376 return $response;
377 }
378
379 // auto fields
380 public function auto_fields($elements = array(), $field = false, $repeater = '') {
381 if ($repeater) {
382 $field_value = '[' . $repeater->mf_input_name . '-' . $field->mf_input_repeater_name . '-1]';
383 $field_label = isset($field->mf_input_repeater_label) ? $field->mf_input_repeater_label : '';
384 $field_type = isset($field->mf_input_repeater_type) ? 'mf-' . $field->mf_input_repeater_type : 'mf-text';
385
386 if ($field_type == 'mf-radio' || $field_type == 'mf-checkbox' || $field_type == 'mf-select') {
387 $field->mf_input_list = array();
388 if (!empty($field->mf_input_repeater_option)) {
389 $options = preg_split('/\r?\n/', $field->mf_input_repeater_option);
390 foreach ($options as $option) {
391 $option_label_value = explode('|', $option);
392 if (count($option_label_value) == 2) {
393 $opt = new \stdClass();
394 $opt->mf_input_option_value = $option_label_value[1];
395 $opt->mf_input_option_text = $option_label_value[0];
396 $field->mf_input_list[] = $opt;
397 }
398 }
399 }
400 } elseif ($field_type == 'mf-switch') {
401 $field->mf_swtich_enable_text = '1';
402 }
403 } else {
404 $field_value = '[' . $field->mf_input_name . ']';
405 $field_label = isset($field->mf_input_label) ? $field->mf_input_label : '';
406 $field_type = isset($field->widgetType) ? $field->widgetType : 'mf-text';
407 }
408 switch ($field_type) {
409 case 'mf-text':
410 case 'mf-email':
411 case 'mf-number':
412 case 'mf-telephone':
413 case 'mf-date':
414 case 'mf-time':
415 case 'mf-range':
416 case 'mf-url':
417 case 'mf-password':
418 case 'mf-listing-fname':
419 case 'mf-listing-lname':
420 case 'mf-rating':
421 case 'mf-mobile':
422 case 'mf-calculation':
423 case 'mf-color-picker':
424 case 'mf-payment-method':
425 case 'mf-credit-card':
426 $elements[] = $this->auto_field(
427 $field,
428 array(
429 'type' => 'e2pdf-html',
430 'block' => true,
431 'float' => true,
432 'properties' => array(
433 'top' => '20',
434 'left' => '20',
435 'right' => '20',
436 'width' => '100%',
437 'height' => 'auto',
438 'value' => $field_label,
439 ),
440 )
441 );
442 $elements[] = $this->auto_field(
443 $field,
444 array(
445 'type' => 'e2pdf-input',
446 'properties' => array(
447 'top' => '5',
448 'width' => '100%',
449 'height' => 'auto',
450 'value' => $field_value,
451 ),
452 )
453 );
454 break;
455 case 'mf-select':
456 case 'mf-multi-select':
457 case 'mf-image-select':
458 case 'mf-toggle-select':
459 $elements[] = $this->auto_field(
460 $field,
461 array(
462 'type' => 'e2pdf-html',
463 'block' => true,
464 'float' => true,
465 'properties' => array(
466 'top' => '20',
467 'left' => '20',
468 'right' => '20',
469 'width' => '100%',
470 'height' => 'auto',
471 'value' => $field_label,
472 ),
473 )
474 );
475 $field_options = array();
476 foreach ($field->mf_input_list as $option) {
477 if ($field_type == 'mf-multi-select') {
478 $option_value = isset($option->value) ? $option->value : '';
479 } else {
480 $option_value = isset($option->mf_input_option_value) ? $option->mf_input_option_value : '';
481 }
482 $field_options[] = $option_value;
483 }
484
485 $elements[] = $this->auto_field(
486 $field,
487 array(
488 'type' => 'e2pdf-select',
489 'properties' => array(
490 'top' => '5',
491 'width' => '100%',
492 'height' => 'auto',
493 'options' => implode("\n", $field_options),
494 'value' => $field_value,
495 'height' => $field_type == 'mf-multi-select' ? '80' : 'auto',
496 'multiline' => $field_type == 'mf-multi-select' ? '1' : '0',
497 ),
498 )
499 );
500 break;
501 case 'mf-radio':
502 case 'mf-switch':
503 case 'mf-like-dislike':
504 $elements[] = $this->auto_field(
505 $field,
506 array(
507 'type' => 'e2pdf-html',
508 'block' => true,
509 'float' => true,
510 'properties' => array(
511 'top' => '20',
512 'left' => '20',
513 'right' => '20',
514 'width' => '100%',
515 'height' => 'auto',
516 'value' => $field_label,
517 ),
518 )
519 );
520
521 if ($field_type == 'mf-switch') {
522 if (isset($field->mf_swtich_enable_text)) {
523 $elements[] = $this->auto_field(
524 $field,
525 array(
526 'type' => 'e2pdf-radio',
527 'properties' => array(
528 'top' => '5',
529 'width' => 'auto',
530 'height' => 'auto',
531 'value' => $field_value,
532 'option' => $field->mf_swtich_enable_text,
533 'group' => $field_value,
534 ),
535 )
536 );
537 $elements[] = $this->auto_field(
538 $field,
539 array(
540 'type' => 'e2pdf-html',
541 'float' => true,
542 'properties' => array(
543 'left' => '5',
544 'width' => '100%',
545 'height' => 'auto',
546 'value' => 'Yes',
547 ),
548 )
549 );
550 }
551
552 if (isset($field->mf_swtich_disable_text)) {
553 $elements[] = $this->auto_field(
554 $field,
555 array(
556 'type' => 'e2pdf-radio',
557 'properties' => array(
558 'top' => '5',
559 'width' => 'auto',
560 'height' => 'auto',
561 'value' => $field_value,
562 'option' => $field->mf_swtich_disable_text,
563 'group' => $field_value,
564 ),
565 )
566 );
567 $elements[] = $this->auto_field(
568 $field,
569 array(
570 'type' => 'e2pdf-html',
571 'float' => true,
572 'properties' => array(
573 'left' => '5',
574 'width' => '100%',
575 'height' => 'auto',
576 'value' => 'No',
577 ),
578 )
579 );
580 }
581 } elseif ($field_type == 'mf-like-dislike') {
582 $elements[] = $this->auto_field(
583 $field,
584 array(
585 'type' => 'e2pdf-radio',
586 'properties' => array(
587 'top' => '5',
588 'width' => 'auto',
589 'height' => 'auto',
590 'value' => $field_value,
591 'option' => '1',
592 'group' => $field_value,
593 ),
594 )
595 );
596 $elements[] = $this->auto_field(
597 $field,
598 array(
599 'type' => 'e2pdf-html',
600 'float' => true,
601 'properties' => array(
602 'left' => '5',
603 'width' => '100%',
604 'height' => 'auto',
605 'value' => 'Like',
606 ),
607 )
608 );
609 $elements[] = $this->auto_field(
610 $field,
611 array(
612 'type' => 'e2pdf-radio',
613 'properties' => array(
614 'top' => '5',
615 'width' => 'auto',
616 'height' => 'auto',
617 'value' => $field_value,
618 'option' => '0',
619 'group' => $field_value,
620 ),
621 )
622 );
623 $elements[] = $this->auto_field(
624 $field,
625 array(
626 'type' => 'e2pdf-html',
627 'float' => true,
628 'properties' => array(
629 'left' => '5',
630 'width' => '100%',
631 'height' => 'auto',
632 'value' => 'Dislike',
633 ),
634 )
635 );
636 } else {
637 foreach ($field->mf_input_list as $option) {
638 $option_value = isset($option->mf_input_option_value) ? $option->mf_input_option_value : '';
639 $option_label = isset($option->mf_input_option_text) ? $option->mf_input_option_text : '';
640 $elements[] = $this->auto_field(
641 $field,
642 array(
643 'type' => 'e2pdf-radio',
644 'properties' => array(
645 'top' => '5',
646 'width' => 'auto',
647 'height' => 'auto',
648 'value' => $field_value,
649 'option' => $option_value,
650 'group' => $field_value,
651 ),
652 )
653 );
654 $elements[] = $this->auto_field(
655 $field,
656 array(
657 'type' => 'e2pdf-html',
658 'float' => true,
659 'properties' => array(
660 'left' => '5',
661 'width' => '100%',
662 'height' => 'auto',
663 'value' => $option_label,
664 ),
665 )
666 );
667 }
668 }
669 break;
670 case 'mf-checkbox':
671 $elements[] = $this->auto_field(
672 $field,
673 array(
674 'type' => 'e2pdf-html',
675 'block' => true,
676 'float' => true,
677 'properties' => array(
678 'top' => '20',
679 'left' => '20',
680 'right' => '20',
681 'width' => '100%',
682 'height' => 'auto',
683 'value' => $field_label,
684 ),
685 )
686 );
687
688 foreach ($field->mf_input_list as $option) {
689 $option_value = isset($option->mf_input_option_value) ? $option->mf_input_option_value : '';
690 $option_label = isset($option->mf_input_option_text) ? $option->mf_input_option_text : '';
691 $elements[] = $this->auto_field(
692 $field,
693 array(
694 'type' => 'e2pdf-checkbox',
695 'properties' => array(
696 'top' => '5',
697 'width' => 'auto',
698 'height' => 'auto',
699 'value' => $field_value,
700 'option' => $option_value,
701 ),
702 )
703 );
704 $elements[] = $this->auto_field(
705 $field,
706 array(
707 'type' => 'e2pdf-html',
708 'float' => true,
709 'properties' => array(
710 'left' => '5',
711 'width' => '100%',
712 'height' => 'auto',
713 'value' => $option_label,
714 ),
715 )
716 );
717 }
718 break;
719 case 'mf-file-upload':
720 $elements[] = $this->auto_field(
721 $field,
722 array(
723 'type' => 'e2pdf-html',
724 'block' => true,
725 'float' => true,
726 'properties' => array(
727 'top' => '20',
728 'left' => '20',
729 'right' => '20',
730 'width' => '100%',
731 'height' => 'auto',
732 'value' => $field_label,
733 ),
734 )
735 );
736 $elements[] = $this->auto_field(
737 $field,
738 array(
739 'type' => 'e2pdf-image',
740 'float' => true,
741 'block' => true,
742 'properties' => array(
743 'top' => '5',
744 'left' => '20',
745 'right' => '0',
746 'width' => '170',
747 'height' => '150',
748 'value' => isset($field->mf_input_multiple_file) && $field->mf_input_multiple_file == 'multiple' ? '[e2pdf-format-output explode="," output="{0}"]' . $field_value . '[/e2pdf-format-output]' : $field_value,
749 ),
750 )
751 );
752 break;
753 case 'mf-textarea':
754 case 'mf-map-location':
755 $elements[] = $this->auto_field(
756 $field,
757 array(
758 'type' => 'e2pdf-html',
759 'block' => true,
760 'float' => true,
761 'properties' => array(
762 'top' => '20',
763 'left' => '20',
764 'right' => '20',
765 'width' => '100%',
766 'height' => 'auto',
767 'value' => $field_label,
768 ),
769 )
770 );
771 $elements[] = $this->auto_field(
772 $field,
773 array(
774 'type' => 'e2pdf-textarea',
775 'properties' => array(
776 'top' => '5',
777 'width' => '100%',
778 'height' => 'auto',
779 'value' => $field_value,
780 ),
781 )
782 );
783 break;
784 case 'mf-listing-optin':
785 case 'mf-gdpr-consent':
786 $elements[] = $this->auto_field(
787 $field,
788 array(
789 'type' => 'e2pdf-html',
790 'block' => true,
791 'float' => true,
792 'properties' => array(
793 'top' => '20',
794 'left' => '20',
795 'right' => '20',
796 'width' => '100%',
797 'height' => 'auto',
798 'value' => $field_label,
799 ),
800 )
801 );
802
803 if ($field_type == 'mf-gdpr-consent') {
804 $option_label = isset($field->mf_gdpr_consent_option_text) ? $field->mf_gdpr_consent_option_text : '';
805 } else {
806 $option_label = isset($field->mf_listing_optin_option_text) ? $field->mf_listing_optin_option_text : '';
807 }
808
809 $elements[] = $this->auto_field(
810 $field,
811 array(
812 'type' => 'e2pdf-checkbox',
813 'properties' => array(
814 'top' => '5',
815 'width' => 'auto',
816 'height' => 'auto',
817 'value' => $field_value,
818 'option' => 'Accepted',
819 ),
820 )
821 );
822 $elements[] = $this->auto_field(
823 $field,
824 array(
825 'type' => 'e2pdf-html',
826 'float' => true,
827 'properties' => array(
828 'left' => '5',
829 'width' => '100%',
830 'height' => 'auto',
831 'value' => $option_label,
832 ),
833 )
834 );
835 break;
836 case 'mf-signature':
837 $elements[] = $this->auto_field(
838 $field,
839 array(
840 'type' => 'e2pdf-html',
841 'block' => true,
842 'float' => true,
843 'properties' => array(
844 'top' => '20',
845 'left' => '20',
846 'right' => '20',
847 'width' => '100%',
848 'height' => 'auto',
849 'value' => $field_label,
850 ),
851 )
852 );
853 $elements[] = $this->auto_field(
854 $field,
855 array(
856 'type' => 'e2pdf-signature',
857 'properties' => array(
858 'top' => '5',
859 'width' => '100%',
860 'height' => '150',
861 'dimension' => '1',
862 'block_dimension' => '1',
863 'value' => $field_value,
864 ),
865 )
866 );
867 break;
868 case 'mf-text-editor':
869 $elements[] = $this->auto_field(
870 $field,
871 array(
872 'type' => 'e2pdf-html',
873 'block' => true,
874 'float' => true,
875 'properties' => array(
876 'top' => '20',
877 'left' => '20',
878 'right' => '20',
879 'width' => '100%',
880 'height' => 'auto',
881 'value' => $field_label,
882 ),
883 )
884 );
885 $elements[] = $this->auto_field(
886 $field,
887 array(
888 'type' => 'e2pdf-html',
889 'properties' => array(
890 'top' => '5',
891 'width' => '100%',
892 'height' => '150',
893 'value' => $field_value,
894 ),
895 )
896 );
897 break;
898 case 'mf-simple-repeater':
899 $elements[] = $this->auto_field(
900 $field,
901 array(
902 'type' => 'e2pdf-html',
903 'block' => true,
904 'float' => true,
905 'properties' => array(
906 'top' => '20',
907 'left' => '20',
908 'right' => '20',
909 'width' => '100%',
910 'height' => 'auto',
911 'value' => $field_label,
912 ),
913 )
914 );
915 $subfields = array();
916 if (!empty($field->mf_input_repeater)) {
917 foreach ($field->mf_input_repeater as $subfield) {
918 $elements = $this->auto_fields($elements, $subfield, $field);
919 $subfields[] = '[' . $field->mf_input_name . '-' . $subfield->mf_input_repeater_name . '-[e2pdf-for-key]]';
920 }
921 }
922 $elements[] = $this->auto_field(
923 $field,
924 array(
925 'type' => 'e2pdf-html',
926 'block' => true,
927 'float' => true,
928 'properties' => array(
929 'top' => '20',
930 'left' => '20',
931 'right' => '20',
932 'width' => '100%',
933 'height' => 'auto',
934 'value' => $field_label . ' (Iteration)',
935 ),
936 )
937 );
938 $elements[] = $this->auto_field(
939 $field,
940 array(
941 'type' => 'e2pdf-textarea',
942 'properties' => array(
943 'top' => '5',
944 'width' => '100%',
945 'height' => 'auto',
946 'text_auto_font_size' => '1',
947 'value' => '[e2pdf-for]'
948 . '[e2pdf-for-data]' . $field_value . '[/e2pdf-for-data]' . "\n"
949 . '[e2pdf-for-do]' . implode(' - ', $subfields) . '' . "\n"
950 . '[/e2pdf-for-do]' . "\n"
951 . '[/e2pdf-for]',
952 ),
953 )
954 );
955 break;
956 default:
957 break;
958 }
959 return $elements;
960 }
961
962 // auto field
963 public function auto_field($field = false, $element = array()) {
964 if (!$field) {
965 return false;
966 }
967 if (!isset($element['block'])) {
968 $element['block'] = false;
969 }
970 if (!isset($element['float'])) {
971 $element['float'] = false;
972 }
973 return $element;
974 }
975
976 // styles
977 public function styles($item_id = false) {
978 $styles = array();
979 $styles[] = plugins_url('css/extension/metform.css?v=' . time(), $this->helper->get('plugin_file_path'));
980 return $styles;
981 }
982
983 // is repeater
984 public function is_repeater($value) {
985 if (is_array($value)) {
986 $key = array_key_first($value);
987 if (false !== strpos($key, 'repeater-')) {
988 return true;
989 }
990 }
991 return false;
992 }
993
994 // store form data action
995 public function action_metform_after_store_form_data() {
996 if (class_exists('ReflectionProperty')) {
997
998 $submission = \MetForm\Core\Entries\Action::instance();
999
1000 $reflection = new ReflectionProperty(get_class($submission), 'entry_id');
1001 $reflection->setAccessible(true);
1002 $dataset = $reflection->getValue($submission);
1003
1004 $reflection = new ReflectionProperty(get_class($submission), 'form_settings');
1005 $reflection->setAccessible(true);
1006 $form_settings = $reflection->getValue($submission);
1007 $form_settings['success_message'] = $this->filter_message($form_settings['success_message'], $dataset);
1008
1009 if (isset($form_settings['enable_user_notification']) && $form_settings['enable_user_notification'] == 1) {
1010 $form_settings['user_email_body'] = $this->filter_message($form_settings['user_email_body'], $dataset, 'mail');
1011 }
1012
1013 if (isset($form_settings['enable_admin_notification']) && $form_settings['enable_admin_notification'] == 1) {
1014 $form_settings['admin_email_body'] = $this->filter_message($form_settings['admin_email_body'], $dataset, 'mail');
1015 }
1016 $reflection->setValue($submission, $form_settings);
1017 }
1018 }
1019
1020 // filter message
1021 public function filter_message($message = '', $dataset = '', $type = 'message') {
1022 if (false !== strpos($message, '[')) {
1023 $shortcode_tags = array(
1024 'e2pdf-download',
1025 'e2pdf-save',
1026 'e2pdf-view',
1027 'e2pdf-adobesign',
1028 'e2pdf-zapier',
1029 'e2pdf-attachment',
1030 );
1031 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1032 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1033 if (!empty($tagnames)) {
1034 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
1035 foreach ($shortcodes[0] as $key => $shortcode_value) {
1036 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1037 $atts = shortcode_parse_atts($shortcode[3]);
1038 if (!isset($atts['dataset']) && isset($atts['id'])) {
1039 $template = new Model_E2pdf_Template();
1040 $template->load($atts['id']);
1041 if ($template->get('extension') === 'metform') {
1042 $atts['dataset'] = $dataset;
1043 $shortcode[3] .= ' dataset="' . $dataset . '"';
1044 }
1045 }
1046 if (!isset($atts['apply'])) {
1047 $shortcode[3] .= ' apply="true"';
1048 }
1049 if (!isset($atts['iframe_download']) && $type == 'message') {
1050 $shortcode[3] .= ' iframe_download="true"';
1051 }
1052 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1053 if ($type == 'mail') {
1054 add_filter('wp_mail', array($this, 'filter_wp_mail'), 11);
1055 $message = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $message);
1056 } else {
1057 $message = str_replace($shortcode_value, '', $message);
1058 }
1059 } else {
1060 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
1061 }
1062 }
1063 }
1064 }
1065 return $message;
1066 }
1067
1068 // wp mail filter
1069 public function filter_wp_mail($args) {
1070 if (false !== strpos($args['message'], '[')) {
1071 $shortcode_tags = array(
1072 'e2pdf-save',
1073 'e2pdf-attachment',
1074 );
1075 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $args['message'], $matches);
1076 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1077 if (!empty($tagnames)) {
1078 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $args['message'], $shortcodes);
1079 foreach ($shortcodes[0] as $key => $shortcode_value) {
1080 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1081 $atts = shortcode_parse_atts($shortcode[3]);
1082 $file = false;
1083 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1084 $file = do_shortcode_tag($shortcode);
1085 if ($file) {
1086 $tmp = false;
1087 if (substr($file, 0, 4) === 'tmp:') {
1088 $file = substr($file, 4);
1089 $tmp = true;
1090 }
1091 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
1092 if ($tmp) {
1093 $this->helper->add('metform_attachments', $file);
1094 }
1095 } else {
1096 $this->helper->add('metform_attachments', $file);
1097 }
1098 $args['attachments'][] = $file;
1099 }
1100 $args['message'] = str_replace($shortcode_value, '', $args['message']);
1101 }
1102 }
1103 $files = $this->helper->get('metform_attachments');
1104 if (is_array($files) && !empty($files)) {
1105 add_action('wp_mail_succeeded', array($this, 'after_mail_sent'));
1106 add_action('wp_mail_failed', array($this, 'after_mail_sent'));
1107 }
1108 }
1109 }
1110 $wp_mail = array(
1111 'to' => $args['to'],
1112 'subject' => $args['subject'],
1113 'message' => $args['message'],
1114 'headers' => $args['headers'],
1115 'attachments' => $args['attachments'],
1116 );
1117 return $wp_mail;
1118 }
1119
1120 // after mail sent
1121 public function after_mail_sent() {
1122 remove_filter('wp_mail', array($this, 'filter_wp_mail'), 30);
1123 $files = $this->helper->get('metform_attachments');
1124 if (is_array($files) && !empty($files)) {
1125 foreach ($files as $key => $file) {
1126 $this->helper->delete_dir(dirname($file) . '/');
1127 }
1128 $this->helper->deset('metform_attachments');
1129 }
1130 }
1131
1132 // row actions hook
1133 public function hook_metform_row_actions($actions, $post) {
1134 if (!empty($post->post_type) && $post->post_type == 'metform-entry' && !empty($post->ID)) {
1135 $hooks = $this->helper->load('hooks')->get('metform', 'hook_metform_row_actions', get_post_meta($post->ID, 'metform_entries__form_id', true));
1136 foreach ($hooks as $hook) {
1137 if ($this->helper->load('hooks')->process_hook(
1138 $hook,
1139 [
1140 'dataset' => $post->ID,
1141 ],
1142 'hook_metform_row_actions'
1143 )
1144 ) {
1145 $action = apply_filters(
1146 'e2pdf_hook_action_button',
1147 array(
1148 'html' => '<a class="e2pdf-download-hook" target="_blank" href="%s">%s</a>',
1149 'url' => $this->helper->get_url(
1150 array(
1151 'page' => 'e2pdf',
1152 'action' => 'export',
1153 'id' => $hook,
1154 'dataset' => $post->ID,
1155 ), 'admin.php?'
1156 ),
1157 'title' => 'PDF #' . $hook,
1158 ), 'hook_metform_row_actions', $hook, $post->ID
1159 );
1160 if (!empty($action)) {
1161 $actions['e2pdf_' . $hook] = sprintf(
1162 $action['html'], $action['url'], $action['title']
1163 );
1164 }
1165 }
1166 }
1167 }
1168 return $actions;
1169 }
1170
1171 // entry view hook
1172 public function hook_metform_entry_view() {
1173 $items = $this->helper->load('hooks')->get_items('metform', 'hook_metform_entry_view');
1174 if (!empty($items)) {
1175 add_meta_box(
1176 'e2pdf',
1177 apply_filters('e2pdf_hook_section_title', __('E2Pdf Actions', 'e2pdf'), 'hook_metform_entry_view'),
1178 array($this, 'hook_metform_entry_view_callback'),
1179 array((new \MetForm\Core\Entries\Cpt())->get_name()),
1180 'side',
1181 'default'
1182 );
1183 }
1184 }
1185
1186 // entry view callback hook
1187 public function hook_metform_entry_view_callback($post) {
1188 if (!empty($post->post_type) && $post->post_type == 'metform-entry') {
1189 $hooks = $this->helper->load('hooks')->get('metform', 'hook_metform_entry_view', get_post_meta($post->ID, 'metform_entries__form_id', true));
1190 if (!empty($hooks)) {
1191 foreach ($hooks as $hook) {
1192 if ($this->helper->load('hooks')->process_hook(
1193 $hook,
1194 [
1195 'dataset' => $post->ID,
1196 ],
1197 'hook_metform_entry_view'
1198 )
1199 ) {
1200 $action = apply_filters(
1201 'e2pdf_hook_action_button',
1202 array(
1203 '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>',
1204 'url' => $this->helper->get_url(
1205 array(
1206 'page' => 'e2pdf',
1207 'action' => 'export',
1208 'id' => $hook,
1209 'dataset' => $post->ID,
1210 ), 'admin.php?'
1211 ),
1212 'title' => 'PDF #' . $hook,
1213 ), 'hook_wordpress_page_edit', $hook, $post->ID
1214 );
1215 if (!empty($action)) {
1216 echo sprintf(
1217 $action['html'], $action['url'], $action['title']
1218 );
1219 }
1220 }
1221 }
1222 } else {
1223 echo __('None', 'e2pdf');
1224 }
1225 }
1226 }
1227
1228 // row column hook
1229 public function hook_metform_entry_row_column($columns) {
1230 $items = $this->helper->load('hooks')->get_items('metform', 'hook_metform_entry_row_column');
1231 if (!empty($items)) {
1232 $columns['e2pdf_hook_metform_entry_row_column'] = apply_filters('e2pdf_hook_section_title', __('E2Pdf Actions', 'e2pdf'), 'hook_metform_entry_row_column');
1233 add_action('manage_metform-entry_posts_custom_column', array($this, 'hook_metform_entry_row_column_callback'), 10, 2);
1234 }
1235 return $columns;
1236 }
1237
1238 // row column collback hook
1239 public function hook_metform_entry_row_column_callback($column, $post_id) {
1240 if ($column == 'e2pdf_hook_metform_entry_row_column') {
1241 $hooks = $this->helper->load('hooks')->get('metform', 'hook_metform_entry_row_column', get_post_meta($post_id, 'metform_entries__form_id', true));
1242 foreach ($hooks as $hook) {
1243 if ($this->helper->load('hooks')->process_hook(
1244 $hook,
1245 [
1246 'dataset' => $post_id,
1247 ],
1248 'hook_metform_entry_row_column'
1249 )
1250 ) {
1251 $action = apply_filters(
1252 'e2pdf_hook_action_button',
1253 array(
1254 'html' => '<a class="button e2pdf-download-hook e2pdf-download-hook-icon-button" target="_blank" title="%2$s" href="%1$s">%2$s</a> ',
1255 'url' => $this->helper->get_url(
1256 array(
1257 'page' => 'e2pdf',
1258 'action' => 'export',
1259 'id' => $hook,
1260 'dataset' => $post_id,
1261 ), 'admin.php?'
1262 ),
1263 'title' => 'PDF #' . $hook,
1264 ), 'hook_metform_entry_row_column', $hook, $post_id
1265 );
1266 if (!empty($action)) {
1267 echo sprintf(
1268 $action['html'], $action['url'], $action['title']
1269 );
1270 }
1271 }
1272 }
1273 }
1274 }
1275 }
1276