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

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

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