PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.23
E2Pdf – Export Pdf Tool for WordPress v1.32.23
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / extension / e2pdf-formidable.php

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

4,273 lines 207.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * E2Pdf Formidable Extension
5 * @copyright Copyright 2017 https://e2pdf.com
6 * @license GPLv3
7 * @version 1
8 * @link https://e2pdf.com
9 * @since 0.00.01
10 */
11 if (!defined('ABSPATH')) {
12 die('Access denied.');
13 }
14
15 class Extension_E2pdf_Formidable extends Model_E2pdf_Model {
16
17 private $options;
18 private $info = array(
19 'key' => 'formidable',
20 'title' => 'Formidable Forms',
21 );
22
23 /**
24 * Get info about extension
25 * @param string $key - Key to get assigned extension info value
26 * @return array|string - Extension Key and Title or Assigned extension info value
27 */
28 public function info($key = false) {
29 if ($key && isset($this->info[$key])) {
30 return $this->info[$key];
31 } else {
32 return array(
33 $this->info['key'] => $this->info['title'],
34 );
35 }
36 }
37
38 /**
39 * Check if needed plugin active
40 * @return bool - Activated/Not Activated plugin
41 */
42 public function active() {
43 if (defined('E2PDF_FORMIDABLE_EXTENSION') || $this->helper->load('extension')->is_plugin_active('formidable/formidable.php')) {
44 return true;
45 }
46 return false;
47 }
48
49 /**
50 * Set option
51 * @param string $key - Key of option
52 * @param string $value - Value of option
53 * @return bool - Status of setting option
54 */
55 public function set($key, $value) {
56 if (!isset($this->options)) {
57 $this->options = new stdClass();
58 }
59 $this->options->$key = $value;
60 switch ($key) {
61 case 'item':
62 $this->set('cached_form', false);
63 if ($this->get('item') && $this->get('item') != '-2' && class_exists('FrmForm')) {
64 $form = FrmForm::getOne($this->get('item'));
65 if (isset($form->id)) {
66 $this->set('cached_form', $form);
67 }
68 }
69 break;
70 case 'item1':
71 $this->set('cached_form', false);
72 if ($this->get('item1') && class_exists('FrmForm')) {
73 $form = FrmForm::getOne($this->get('item1'));
74 if (isset($form->id)) {
75 $this->set('cached_form', $form);
76 }
77 }
78 break;
79 case 'item2':
80 $this->set('cached_form2', false);
81 if ($this->get('item2') && class_exists('FrmForm')) {
82 $form = FrmForm::getOne($this->get('item2'));
83 if (isset($form->id)) {
84 $this->set('cached_form2', $form);
85 }
86 }
87 break;
88 case 'dataset':
89 $this->set('cached_entry', false);
90 if ($this->get('cached_form') && $this->get('dataset') && class_exists('FrmEntry')) {
91 if (substr($this->get('dataset'), 0, 4) === 'tmp_') {
92 $this->set('cached_entry', get_transient('e2pdf_' . $this->get('dataset')));
93 } else {
94 $entry = FrmEntry::getOne($this->get('dataset'));
95 if ($entry && isset($entry->form_id) && $entry->form_id == $this->get('cached_form')->id) {
96 $this->set('cached_entry', $entry);
97 }
98 }
99 }
100 break;
101 case 'dataset2':
102 if ($this->get('cached_form2') && $this->get('dataset2') && class_exists('FrmEntry')) {
103 $entry = FrmEntry::getOne($this->get('dataset2'));
104 if ($entry && isset($entry->form_id) && $entry->form_id == $this->get('cached_form2')->id) {
105 $this->set('cached_entry2', $entry);
106 }
107 }
108 break;
109 default:
110 break;
111 }
112 return true;
113 }
114
115 /**
116 * Get option by key
117 * @param string $key - Key to get assigned option value
118 * @return mixed
119 */
120 public function get($key) {
121 if (isset($this->options->$key)) {
122 $value = $this->options->$key;
123 } else {
124 switch ($key) {
125 case 'args':
126 $value = array();
127 break;
128 default:
129 $value = false;
130 break;
131 }
132 }
133 return $value;
134 }
135
136 /**
137 * Get items to work with
138 * @return array() - List of available items
139 */
140 public function items() {
141 $items = array();
142 if (class_exists('FrmForm')) {
143 $where = array(
144 'is_template' => 0,
145 'status' => 'published',
146 );
147 $forms = FrmForm::getAll($where, 'name');
148 if ($forms) {
149 foreach ($forms as $key => $form) {
150 $items[] = $this->item($form->id);
151 }
152 }
153 }
154 return $items;
155 }
156
157 /**
158 * Get entries for export
159 * @param int $item - Form ID
160 * @param string $name - Entries names
161 * @return array() - Entries list
162 */
163 public function datasets($item_id = false, $name = false) {
164 $item_id = (int) $item_id;
165 $datasets = array();
166 if (class_exists('FrmEntry') && $item_id) {
167 $where = array(
168 'it.form_id' => $item_id,
169 );
170 $entries = FrmEntry::getAll($where, ' ORDER BY id DESC');
171 if ($entries) {
172 $this->set('item', $item_id);
173 if (class_exists('FrmFieldsHelper') && class_exists('FrmFormsController') && method_exists('FrmFormsController', 'replace_form_name_shortcodes')) {
174 $this->set('cached_shortcodes', FrmFieldsHelper::get_shortcodes($name, $item_id));
175 }
176 foreach ($entries as $key => $entry) {
177 $this->set('dataset', $entry->id);
178 $entry_title = $this->render($name);
179 if (!$entry_title) {
180 $entry_title = $entry->item_key;
181 }
182 $datasets[] = array(
183 'key' => $entry->id,
184 'value' => $entry_title,
185 );
186 }
187 $this->set('cached_shortcodes', false);
188 }
189 }
190 return $datasets;
191 }
192
193 /**
194 * Get Dataset Actions
195 * @param int $dataset_id - Dataset ID
196 * @return object
197 */
198 public function get_dataset_actions($dataset_id = false) {
199 $dataset_id = (int) $dataset_id;
200 if (!$dataset_id) {
201 return false;
202 }
203 $actions = new stdClass();
204 $actions->view = $this->helper->get_url(
205 array(
206 'page' => 'formidable-entries',
207 'frm_action' => 'show',
208 'id' => $dataset_id,
209 )
210 );
211 $actions->delete = false;
212 return $actions;
213 }
214
215 /**
216 * Get Template Actions
217 * @param int $template - Template ID
218 * @return object
219 */
220 public function get_template_actions($template = false) {
221 $template = (int) $template;
222 if (!$template) {
223 return;
224 }
225 $actions = new stdClass();
226 $actions->delete = false;
227 return $actions;
228 }
229
230 /**
231 * Get item
232 * @param int $item_id - Item ID
233 * @return object - Item
234 */
235 public function item($item_id = false) {
236 $item_id = (int) $item_id;
237 if (!$item_id && $this->get('item')) {
238 $item_id = $this->get('item');
239 }
240 $form = false;
241 if (class_exists('FrmForm')) {
242 $form = FrmForm::getOne($item_id);
243 }
244 $item = new stdClass();
245 if ($form) {
246 $item->id = (string) $item_id;
247 $item->url = $this->helper->get_url(
248 array(
249 'page' => 'formidable',
250 'frm_action' => 'edit',
251 'id' => $item_id,
252 )
253 );
254 $item->name = esc_html('' === $form->name ? __('(no title)', 'formidable') : $form->name . ($form->parent_form_id ? __(' (child)', 'formidable') : ''));
255 } else {
256 $item->id = '';
257 $item->url = '';
258 $item->name = '';
259 }
260 return $item;
261 }
262
263 /**
264 * Render value according to content
265 * @param string $value - Content
266 * @param string $type - Type of rendering value
267 * @param array $field - Field details
268 * @return string - Fully rendered value
269 */
270 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
271 $value = $this->render_shortcodes($value, $field);
272 if (!$raw) {
273 $value = $this->strip_shortcodes($value);
274 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
275 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
276 }
277 return $value;
278 }
279
280 /**
281 * Load actions for this extension
282 */
283 public function load_actions() {
284 add_action('frm_notification', array($this, 'action_frm_notification'), 30, 3);
285 add_action('check_ajax_referer', array($this, 'action_check_ajax_referer'), 10, 2);
286 add_action('frm_after_create_entry', array($this, 'action_frm_default_value'), 0, 2);
287 add_action('frm_after_update_entry', array($this, 'action_frm_default_value'), 0, 2);
288 add_action('frm_success_action', array($this, 'action_frm_success_action'));
289
290 /* Hooks */
291 add_action('frm_show_entry_sidebar', array($this, 'hook_formidable_entry_view'));
292 add_action('frm_edit_entry_sidebar', array($this, 'hook_formidable_entry_edit'));
293 }
294
295 /**
296 * Load filters for this extension
297 */
298 public function load_filters() {
299
300 if (!get_option('e2pdf_formidable_disable_filter', '0')) {
301 add_filter('frm_display_entry_content', array(new Model_E2pdf_Filter(), 'pre_filter'), 9);
302 add_filter('frm_display_entry_content', array(new Model_E2pdf_Filter(), 'filter'), 25);
303 add_filter('frm_display_entry_content', array($this, 'filter_frm_display_entry_content'), 30);
304 add_filter('frm_content', array(new Model_E2pdf_Filter(), 'pre_filter'));
305 add_filter('frm_content', array(new Model_E2pdf_Filter(), 'filter'), 25);
306 }
307
308 add_filter('frm_content', array($this, 'filter_frm_content'), 30, 3);
309 add_filter('frm_image_html_array', array($this, 'filter_frm_image_html_array'), 10, 2);
310 add_filter('frm_notification_attachment', array($this, 'filter_frm_notification_attachment'), 30, 3);
311 add_filter('e2pdf_model_options_get_options_options', array($this, 'filter_e2pdf_model_options_get_options_options'));
312 add_filter('frm_main_feedback', array($this, 'filter_frm_main_feedback'));
313
314 // replace shortcodes on backup
315 add_filter('e2pdf_controller_templates_backup_options', array($this, 'filter_e2pdf_controller_templates_backup_options'), 10, 3);
316 add_filter('e2pdf_controller_templates_backup_pages', array($this, 'filter_e2pdf_controller_templates_backup_pages'), 10, 4);
317 add_filter('e2pdf_controller_templates_backup_actions', array($this, 'filter_e2pdf_controller_templates_backup_actions'), 10, 4);
318 add_filter('e2pdf_controller_templates_backup_replace_shortcodes', array($this, 'filter_e2pdf_controller_templates_backup_replace_shortcodes'), 10, 4);
319
320 // replace shortcodes on import
321 add_filter('e2pdf_controller_templates_import_options', array($this, 'filter_e2pdf_controller_templates_import_options'), 10, 3);
322 add_filter('e2pdf_controller_templates_import_pages', array($this, 'filter_e2pdf_controller_templates_import_pages'), 10, 5);
323 add_filter('e2pdf_controller_templates_import_actions', array($this, 'filter_e2pdf_controller_templates_import_actions'), 10, 5);
324 add_filter('e2pdf_controller_templates_import_replace_shortcodes', array($this, 'filter_e2pdf_controller_templates_import_replace_shortcodes'), 10, 5);
325
326 // hooks
327 add_filter('frm_row_actions', array($this, 'hook_formidable_row_actions'), 10, 2);
328
329 // frm-graph
330 add_filter('frm_graph_shortcode_custom_html', array($this, 'filter_frm_graph_shortcode_custom_html'), 99, 2);
331 }
332
333 public function action_frm_success_action() {
334 $this->set('iframe_download', true);
335 }
336
337 public function filter_frm_main_feedback($message) {
338 $this->set('iframe_download', false);
339 return $message;
340 }
341
342 /**
343 * Render shortcodes which available in this extension
344 * @param string $value - Content
345 * @param string $type - Type of rendering value
346 * @param array $field - Field details
347 * @return string - Value with rendered shortcodes
348 */
349 public function render_shortcodes($value, $field = array()) {
350 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
351 $checkbox_separated = false;
352 $foreach_wrapper_shortcodes = false;
353 $foreach_inner_shortcodes = false;
354 $signature = false;
355 if ($this->verify()) {
356 if (false !== strpos($value, '[')) {
357 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
358 if (class_exists('FrmProEntry')) {
359 $shortcode_tags = array();
360 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
361 $tagnames = array_intersect($shortcode_tags, $matches[1]);
362 foreach ($matches[1] as $key => $shortcode) {
363 if (false !== strpos($shortcode, ':')) {
364 $shortcode_tags[] = $shortcode;
365 }
366 }
367 $tagnames = array_intersect($shortcode_tags, $matches[1]);
368 if (!empty($tagnames)) {
369 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes);
370 foreach ($shortcodes[0] as $key => $shortcode_value) {
371 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
372 $shortcode_details = explode(':', $shortcode[2]);
373 $field_id = $shortcode_details['0'];
374 $child_id = $shortcode_details['1'];
375 $child_entry = 0;
376 $child_field = false;
377 $child_form = false;
378 if (class_exists('FrmField')) {
379 $child_field = FrmField::getOne($field_id);
380 if ($child_field) {
381 $child_form = $child_field->form_id;
382 }
383 }
384 $child_entries = array();
385 if ($this->get('cached_entry')) {
386 $childs = FrmEntry::getAll(array('parent_item_id' => $this->get('cached_entry')->id), ' ORDER BY it.id ASC', '', true, false);
387 if ($childs && $child_form) {
388 foreach ($childs as $child) {
389 if ($child->form_id == $child_form) {
390 $child_entries[] = $child->id;
391 }
392 }
393 }
394 }
395 if ($this->get('item') == '-2') {
396 if ($this->get('cached_entry2')) {
397 $childs = FrmEntry::getAll(array('parent_item_id' => $this->get('cached_entry2')->id), ' ORDER BY it.id ASC', '', true, false);
398 if ($childs && $child_form) {
399 foreach ($childs as $child) {
400 if ($child->form_id == $child_form) {
401 $child_entries[] = $child->id;
402 }
403 }
404 }
405 }
406 }
407 $new_shortcode = '';
408 if (!empty($child_entries) && count($child_entries) >= $child_id) {
409 $start = 1;
410 foreach ($child_entries as $key => $sub_entry) {
411 if ($start == $child_id) {
412 $child_entry = $sub_entry;
413 break;
414 }
415 $start++;
416 }
417
418 $shortcode[2] = 'frm-field-value field_id=' . $field_id . ' entry=' . $child_entry;
419 $new_shortcode = '[' . $shortcode[2] . $shortcode[3] . ']';
420 }
421 $checkbox_separated = true;
422 $value = str_replace($shortcode_value, $new_shortcode, $value);
423 }
424 }
425 }
426
427 // Foreach Inner Shortcodes
428 preg_match_all('/\[foreach[^\]]*?\]((?:(?!\[\/foreach).)+)(e2pdf-if|e2pdf-for)((?:(?!\[\/foreach).)+)\[\/foreach[^\]]*?\]/s', $value, $matches);
429 if (!empty($matches[0])) {
430 foreach ($matches[0] as $match) {
431 $new_match = preg_replace('/(\[)(\/?(e2pdf-if|e2pdf-for)[^\]]*?)(\])/', '{{$2}}', $match);
432 $if_shortcodes = $this->helper->load('if')->get_shortcodes();
433 $if_shortcodes_new = array_map(
434 function ($str) {
435 return str_replace(array('[', ']'), array('{{', '}}'), $str);
436 },
437 $if_shortcodes
438 );
439 $value = str_replace($match, str_replace($if_shortcodes, $if_shortcodes_new, $new_match), $value);
440 }
441 $foreach_inner_shortcodes = true;
442 }
443
444 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
445
446 /* Default shortcodes support */
447 $shortcode_tags = array(
448 'e2pdf-frm-lookup-values',
449 'e2pdf-frm-data-values',
450 'default-message',
451 'default_message',
452 'default-message2',
453 'default_message2',
454 );
455 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
456 $tagnames = array_intersect($shortcode_tags, $matches[1]);
457 if (!empty($tagnames)) {
458 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $value, $shortcodes);
459 foreach ($shortcodes[0] as $key => $shortcode_value) {
460 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
461 $atts = shortcode_parse_atts($shortcode[3]);
462 if ($shortcode[2] === 'e2pdf-frm-lookup-values' || $shortcode[2] === 'e2pdf-frm-data-values') {
463 if (!isset($atts['user_id']) && $this->get('user_id')) {
464 $shortcode[3] .= ' user_id="' . $this->get('user_id') . '"';
465 $value = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $value);
466 }
467 } elseif (($shortcode[2] === 'default-message' || $shortcode[2] === 'default_message') && $this->get('cached_entry') && class_exists('FrmEntriesHelper')) {
468 $default_message = FrmEntriesHelper::replace_default_message(
469 $shortcode_value,
470 array(
471 'id' => $this->get('cached_entry')->id,
472 'entry' => clone $this->get('cached_entry'),
473 'plain_text' => isset($atts['is_plain_text']) && $atts['is_plain_text'] == 'true' ? true : false,
474 'user_info' => isset($atts['user_info']) && $atts['user_info'] == 'true' ? true : false,
475 )
476 );
477 $value = str_replace($shortcode_value, $default_message, $value);
478 } elseif (($shortcode[2] === 'default-message2' || $shortcode[2] == 'default_message2') && $this->get('cached_entry2') && class_exists('FrmEntriesHelper')) {
479 $default_message = FrmEntriesHelper::replace_default_message(
480 str_replace(array('default-message2', 'default_message2'), array('default-message', 'default_message'), $shortcode_value),
481 array(
482 'id' => $this->get('cached_entry2')->id,
483 'entry' => clone $this->get('cached_entry2'),
484 'plain_text' => isset($atts['is_plain_text']) && $atts['is_plain_text'] == 'true' ? true : false,
485 'user_info' => isset($atts['user_info']) && $atts['user_info'] == 'true' ? true : false,
486 )
487 );
488 $value = str_replace($shortcode_value, $default_message, $value);
489 }
490 }
491 }
492
493 // Foreach Wrapper Shortcodes
494 preg_match_all('/\[foreach[^\]]*?\]((?:(?!\[\/foreach).)+)(e2pdf-format-number|e2pdf-format-date|e2pdf-format-output|e2pdf-math)((?:(?!\[\/foreach).)+)\[\/foreach[^\]]*?\]/s', $value, $matches);
495 if (!empty($matches[0])) {
496 foreach ($matches[0] as $match) {
497 $new_match = preg_replace('/(\[)(\/?(e2pdf-format-number|e2pdf-format-date|e2pdf-format-output|e2pdf-math)[^\]]*?)(\])/', '{{$2}}', $match);
498 $value = str_replace($match, $new_match, $value);
499 $foreach_wrapper_shortcodes = true;
500 }
501 }
502 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
503
504 if (false !== strpos($value, '[')) {
505 if (strpos($value, '[foreach') !== false) {
506 if ($this->get('cached_entry')) {
507 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[id\]/is', $this->get('cached_entry')->id, $value);
508 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[key\]/is', $this->get('cached_entry')->item_key, $value);
509 } else {
510 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[id\]/is', '', $value);
511 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[key\]/is', '', $value);
512 }
513
514 if ($this->get('cached_entry2')) {
515 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[id2\]/is', $this->get('cached_entry2')->id, $value);
516 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[key2\]/is', $this->get('cached_entry2')->item_key, $value);
517 } else {
518 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[id2\]/is', '', $value);
519 $value = preg_replace('/\[foreach[^\]]*?\](.*?)\[\/foreach(*SKIP)(*FAIL)|\[key2\]/is', '', $value);
520 }
521 } else {
522 $replace = array(
523 '[id]' => $this->get('cached_entry') ? $this->get('cached_entry')->id : '',
524 '[key]' => $this->get('cached_entry') ? $this->get('cached_entry')->item_key : '',
525 '[id2]' => $this->get('cached_entry2') ? $this->get('cached_entry2')->id : '',
526 '[key2]' => $this->get('cached_entry2') ? $this->get('cached_entry2')->item_key : '',
527 );
528 $value = str_replace(array_keys($replace), $replace, $value);
529 }
530 }
531 }
532
533 /*
534 * Checkboxes separatable fix filter add
535 * @since 0.01.43
536 */
537 if ($checkbox_separated) {
538 add_filter('frm_display_value_custom', array($this, 'filter_frm_display_value_custom'), 0, 3);
539 }
540
541 /* [display-frm-data] shortcode support */
542 if (false !== strpos($value, '[')) {
543 preg_match_all('/(?=\[display-frm-data\b)(\[(?:[^\[\]]+|(?1))+\])/', $value, $matches);
544 if (!empty($matches[1])) {
545 add_filter('frmpro_fields_replace_shortcodes', array($this, 'filter_frmpro_fields_replace_shortcodes'), 20, 4);
546 foreach ($matches[1] as $match) {
547 if ($this->get('item') == '-2') {
548 if ($this->get('cached_entry') && $this->get('cached_entry2')) {
549 $new_match = apply_filters('frm_content', $match, $this->get('cached_form'), $this->get('cached_entry'));
550 $new_match = apply_filters('frm_content', $new_match, $this->get('cached_form2'), $this->get('cached_entry2'));
551 } elseif ($this->get('cached_entry')) {
552 $new_match = apply_filters('frm_content', $match, $this->get('cached_form'), $this->get('cached_entry'));
553 if ($this->get('cached_form2')) {
554 $new_match = apply_filters('frm_content', $new_match, $this->get('cached_form2'), $this->get('cached_entry'));
555 }
556 } elseif ($this->get('cached_entry2')) {
557 $new_match = apply_filters('frm_content', $match, $this->get('cached_form2'), $this->get('cached_entry2'));
558 if ($this->get('cached_form')) {
559 $new_match = apply_filters('frm_content', $new_match, $this->get('cached_form'), $this->get('cached_entry2'));
560 }
561 }
562 } else {
563 $new_match = apply_filters('frm_content', $match, $this->get('cached_form'), $this->get('cached_entry'));
564 }
565 $value = str_replace($match, $new_match, $value);
566 }
567 remove_filter('frmpro_fields_replace_shortcodes', array($this, 'filter_frmpro_fields_replace_shortcodes'), 20);
568 }
569 }
570
571 add_filter('frm_filter_view', array($this, 'filter_frm_filter_view'));
572 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
573 remove_filter('frm_filter_view', array($this, 'filter_frm_filter_view'));
574
575 /*
576 * Checkboxes separatable fix filter remove
577 * @since 0.01.43
578 */
579 if ($checkbox_separated) {
580 remove_filter('frm_display_value_custom', array($this, 'filter_frm_display_value_custom'), 0);
581 }
582
583 /* Bug fix for Formidable Forms Signature (2.0.1) */
584 if (class_exists('FrmFieldFactory')) {
585 $signature_field = FrmFieldFactory::get_field_type('signature');
586 if ($signature_field->get_display_value('signature') == '') {
587 $signature = true;
588 }
589 }
590 if ($signature) {
591 remove_filter('frm_get_signature_display_value', 'FrmSigAppController::display_signature');
592 remove_filter('frmpro_fields_replace_shortcodes', 'FrmSigAppController::custom_display_signature');
593 add_filter('frm_keep_signature_value_array', array($this, 'filter_frm_keep_signature_value_array'), 10, 2);
594 }
595 add_filter('frmpro_fields_replace_shortcodes', array($this, 'filter_frmpro_fields_replace_shortcodes'), 20, 4);
596
597 $text_signature = !empty($field['type']) && ($field['type'] == 'e2pdf-image' || $field['type'] == 'e2pdf-signature') ? true : false;
598 if ($text_signature) {
599 add_filter('frm_get_signature_display_value', array($this, 'filter_frm_get_signature_display_value'), 20, 3);
600 }
601 if ($this->get('item') == '-2') {
602 if ($this->get('cached_entry') && $this->get('cached_entry2')) {
603 $value = apply_filters('frm_content', $value, $this->get('cached_form'), $this->get('cached_entry'));
604 $value = apply_filters('frm_content', $value, $this->get('cached_form2'), $this->get('cached_entry2'));
605 } elseif ($this->get('cached_entry')) {
606 $value = apply_filters('frm_content', $value, $this->get('cached_form'), $this->get('cached_entry'));
607 if ($this->get('cached_form2')) {
608 $value = apply_filters('frm_content', $value, $this->get('cached_form2'), $this->get('cached_entry'));
609 }
610 } elseif ($this->get('cached_entry2')) {
611 $value = apply_filters('frm_content', $value, $this->get('cached_form2'), $this->get('cached_entry2'));
612 if ($this->get('cached_form')) {
613 $value = apply_filters('frm_content', $value, $this->get('cached_form'), $this->get('cached_entry2'));
614 }
615 }
616 } else {
617 if ($this->get('cached_shortcodes')) {
618 remove_filter('frm_content', 'FrmFormsController::filter_content', 10, 3);
619 add_filter('frm_content', array($this, 'filter_filter_content'), 10, 3);
620 }
621 $value = apply_filters('frm_content', $value, $this->get('cached_form'), $this->get('cached_entry'));
622 if ($this->get('cached_shortcodes')) {
623 remove_filter('frm_content', array($this, 'filter_filter_content'), 10, 3);
624 add_filter('frm_content', 'FrmFormsController::filter_content', 10, 3);
625 }
626 }
627
628 // Foreach Wrapper Shortcodes Reverse
629 if ($foreach_wrapper_shortcodes) {
630 $value = preg_replace('/(\{\{)(\/?(e2pdf-format-number|e2pdf-format-date|e2pdf-format-output|e2pdf-math)[^\}]*?)(\}\})/', '[$2]', $value);
631 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field, true);
632 }
633
634 // Foreach Inner Shortcodes Reverse
635 if ($foreach_inner_shortcodes) {
636 preg_match_all('/\{\{(e2pdf-if|e2pdf-for)\}\}(.*?)+\{\{\/(e2pdf-if|e2pdf-for)\}\}/s', $value, $matches);
637 if (!empty($matches[0])) {
638 foreach ($matches[0] as $match) {
639 $new_match = preg_replace('/(\{\{)(\/?(e2pdf-if|e2pdf-for)[^\}]*?)(\}\})/', '[$2]', $match);
640 $value = str_replace($match, str_replace($if_shortcodes_new, $if_shortcodes, $new_match), $value);
641 }
642 }
643 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
644 }
645
646 // Text Signature
647 if ($text_signature) {
648 remove_filter('frm_get_signature_display_value', array($this, 'filter_frm_get_signature_display_value'), 20);
649 if ($value && false !== strpos($value, ':e2pdf-text-filter:')) {
650 $value = str_replace(':e2pdf-text-filter:', '', $value);
651 $field['properties']['text'] = '1';
652 }
653 }
654 remove_filter('frmpro_fields_replace_shortcodes', array($this, 'filter_frmpro_fields_replace_shortcodes'), 20);
655 if ($signature) {
656 remove_filter('frm_keep_signature_value_array', array($this, 'filter_frm_keep_signature_value_array'));
657 add_filter('frm_get_signature_display_value', 'FrmSigAppController::display_signature', 10, 3);
658 add_filter('frmpro_fields_replace_shortcodes', 'FrmSigAppController::custom_display_signature', 10, 4);
659 }
660 $value = $this->replace_meta_shortcodes($value);
661 $value = $this->helper->load('field')->render(
662 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), $this->get('item2'), $this->get('dataset2')),
663 $this,
664 $field
665 );
666 }
667 return apply_filters(
668 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), $this->get('item2'), $this->get('dataset2')
669 );
670 }
671
672 /**
673 * Strip unused shortcodes
674 * @param string $value - Content
675 * @return string - Value with removed unused shortcodes
676 */
677 public function strip_shortcodes($value) {
678 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
679 return $value;
680 }
681
682 /**
683 * Convert "shortcodes" inside value string
684 * @param string $value - Value string
685 * @param bool $to - Convert From/To
686 * @return string - Converted value
687 */
688 public function convert_shortcodes($value, $to = false, $html = false) {
689 if ($value) {
690 if ($to) {
691 $search = array('&#91;', '&#93;', '&#091;', '&#093;');
692 $replace = array('[', ']', '[', ']');
693 $value = str_replace($search, $replace, $value);
694 if (!$html) {
695 $value = wp_specialchars_decode($value, ENT_QUOTES);
696 }
697 } else {
698 $search = array('[', ']', '&#091;', '&#093;');
699 $replace = array('&#91;', '&#93;', '&#91;', '&#93;');
700 $value = str_replace($search, $replace, $value);
701 }
702 }
703 return $value;
704 }
705
706 public function filter_frm_filter_view($view) {
707 if (isset($view->frm_before_content) && $view->frm_before_content) {
708 $view->frm_before_content = str_replace('[e2pdf-exclude]', '[e2pdf-exclude apply="true"]', $view->frm_before_content);
709 }
710 if (isset($view->post_content) && $view->post_content) {
711 $view->post_content = str_replace('[e2pdf-exclude]', '[e2pdf-exclude apply="true"]', $view->post_content);
712 }
713 if (isset($view->frm_after_content) && $view->frm_after_content) {
714 $view->frm_after_content = str_replace('[e2pdf-exclude]', '[e2pdf-exclude apply="true"]', $view->frm_after_content);
715 }
716 return $view;
717 }
718
719 public function filter_frm_lookup_is_current_user_filter_needed($is_filter_needed, $field_id, $field_options) {
720 if (FrmField::is_option_true_in_array($field_options, 'lookup_filter_current_user')) {
721 return true;
722 }
723 return $is_filter_needed;
724 }
725
726 public function filter_frm_keep_signature_value_array($keep_array, $atts) {
727 return true;
728 }
729
730 /**
731 * Search and update shortcodes for this extension inside content
732 * Auto set of dataset id
733 * @param string $content - Content
734 * @param int $form - ID of form
735 * @param int $dataset_id - ID of dataset
736 * @return string - Content with updates shortcodes
737 */
738 public function filter_frm_content($content, $form, $dataset_id) {
739
740 if (false === strpos($content, '[')) {
741 return $content;
742 }
743 $shortcode_tags = array(
744 'e2pdf-download',
745 'e2pdf-save',
746 'e2pdf-view',
747 'e2pdf-adobesign',
748 'e2pdf-zapier',
749 );
750 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
751 $tagnames = array_intersect($shortcode_tags, $matches[1]);
752 if (!empty($tagnames)) {
753 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
754 foreach ($shortcodes[0] as $key => $shortcode_value) {
755 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
756 $atts = shortcode_parse_atts($shortcode[3]);
757 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
758 } else {
759 if (!isset($atts['dataset']) && isset($atts['id'])) {
760 $template = new Model_E2pdf_Template();
761 $template->load($atts['id']);
762 if ($template->get('extension') === 'formidable') {
763 $entry_id = is_object($dataset_id) ? $dataset_id->id : $dataset_id;
764 if ($entry_id) {
765 if ($template->get('item') == '-2') {
766 if ($template->get('item1') == $form->id) {
767 $atts['dataset'] = $entry_id;
768 $shortcode[3] .= ' dataset="' . $entry_id . '"';
769 } elseif (!isset($atts['dataset2']) && $template->get('item2') == $form->id) {
770 $atts['dataset2'] = $entry_id;
771 $shortcode[3] .= ' dataset2="' . $entry_id . '"';
772 }
773 } else {
774 $atts['dataset'] = $entry_id;
775 $shortcode[3] .= ' dataset="' . $entry_id . '"';
776 }
777 }
778 }
779 }
780 if (!isset($atts['apply'])) {
781 $shortcode[3] .= ' apply="true"';
782 }
783 if (!isset($atts['filter'])) {
784 $shortcode[3] .= ' filter="true"';
785 }
786 if (!isset($atts['iframe_download']) && $this->get('iframe_download')) {
787 $shortcode[3] .= ' iframe_download="true"';
788 }
789 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
790 }
791 }
792 }
793
794 return $content;
795 }
796
797 public function filter_frm_display_entry_content($content) {
798
799 if (false === strpos($content, '[')) {
800 return $content;
801 }
802 $shortcode_tags = array(
803 'e2pdf-download',
804 'e2pdf-save',
805 'e2pdf-view',
806 'e2pdf-adobesign',
807 'e2pdf-zapier',
808 );
809 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
810 $tagnames = array_intersect($shortcode_tags, $matches[1]);
811 if (!empty($tagnames)) {
812 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
813 foreach ($shortcodes[0] as $key => $shortcode_value) {
814 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
815 $atts = shortcode_parse_atts($shortcode[3]);
816 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
817 } else {
818 if (!isset($atts['apply'])) {
819 $shortcode[3] .= ' apply="true"';
820 }
821 if (!isset($atts['filter'])) {
822 $shortcode[3] .= ' filter="true"';
823 }
824 if (!isset($atts['iframe_download'])) {
825 $shortcode[3] .= ' iframe_download="true"';
826 }
827 $content = str_replace($shortcode_value, do_shortcode_tag($shortcode), $content);
828 }
829 }
830 }
831
832 return $content;
833 }
834
835 /**
836 * Filter for checkbox repeatable Label/Value fix
837 * @param array $value - Value
838 * @param obj $field - Field
839 * @param array $atts - Shortcode Attributes
840 * @return mixed - Updated value
841 */
842 public function filter_frm_display_value_custom($value, $field, $atts = array()) {
843 if (class_exists('FrmProEntriesController')) {
844 $defaults = array(
845 'html' => 0,
846 'type' => $field->type,
847 'keepjs' => 0,
848 );
849 $atts = array_merge($defaults, $atts);
850 if ($atts['type'] === 'checkbox') {
851 $value = FrmProEntriesController::get_option_label_for_saved_value($value, $field, $atts);
852 }
853 }
854 return $value;
855 }
856
857 /**
858 * Filter for Signature
859 * @param array $value - Value
860 * @param obj $field - Field
861 * @param array $atts - Shortcode Attributes
862 * @return mixed - Updated value
863 */
864 public function filter_frm_get_signature_display_value($value, $field, $atts = array()) {
865 if ($value && !empty($value['format']) && $value['format'] == 'typed' && !empty($value['content'])) {
866 $value['content'] = ':e2pdf-text-filter:' . $value['content'];
867 }
868 return $value;
869 }
870
871 public function filter_frmpro_fields_replace_shortcodes($replace_with, $tag, $atts, $field) {
872
873 /* Backward compatibility with Formidable Forms PRO < 5.0 */
874 if (class_exists('FrmProDb') && property_exists('FrmProDb', 'plug_version') && version_compare(FrmProDb::$plug_version, '5.0', '<')) {
875 if ($this->get('item') == '-2' && $this->get('cached_entry2') && $field->form_id == $this->get('cached_entry2')->form_id) {
876 $atts['entry_id'] = $this->get('cached_entry2')->id;
877 $replace_with = FrmProEntryMetaHelper::get_post_or_meta_value($this->get('cached_entry2'), $field, $atts);
878 }
879 }
880
881 if ($field->type === 'checkbox' && is_array($replace_with)) {
882 if (isset($atts['key']) && $atts['key']) {
883 if (isset($replace_with[$atts['key']])) {
884 $replace_with = $replace_with[$atts['key']];
885 } else {
886 $replace_with = '';
887 }
888 }
889 }
890
891 if ($field->type === 'radio') {
892 if (isset($atts['key']) && $atts['key'] === 'other') {
893 if (isset($field->options) && is_array($field->options)) {
894 $field_key = array_search($replace_with, array_column($field->options, 'value'));
895 if ($field_key !== false) {
896 $replace_with = '';
897 }
898 }
899 }
900 }
901
902 return $replace_with;
903 }
904
905 /**
906 * Generate attachments according shortcodes in Email template
907 * @param array $attachments - List of attachments
908 * @param int $form - ID of form
909 * @param array $args - Arguments
910 * @return array - Updated list of attachments
911 */
912 public function filter_frm_notification_attachment($attachments, $form, $args) {
913 if (!isset($args['email_key'])) {
914 return $attachments;
915 }
916 $form_actions = FrmFormAction::get_action_for_form($form->id);
917 $dataset_id = $args['entry']->id;
918 $email_key = $args['email_key'];
919
920 $shortcode_tags = array(
921 'e2pdf-attachment',
922 'e2pdf-save',
923 );
924
925 foreach ($form_actions as $key => $action) {
926 if ($action->ID === $email_key) {
927 $content = $action->post_content['email_message'];
928 if (false === strpos($content, '[')) {
929 return $attachments;
930 }
931
932 remove_filter('frm_content', array($this, 'filter_frm_content'), 30);
933 $content = apply_filters('frm_content', $content, $form, $args['entry']);
934 add_filter('frm_content', array($this, 'filter_frm_content'), 30, 3);
935
936 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
937 $tagnames = array_intersect($shortcode_tags, $matches[1]);
938 if (!empty($tagnames)) {
939 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
940 foreach ($shortcodes[0] as $key => $shortcode_value) {
941 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
942 $atts = shortcode_parse_atts($shortcode[3]);
943 $file = false;
944 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
945 if (!isset($atts['dataset']) && isset($atts['id'])) {
946 $template = new Model_E2pdf_Template();
947 $template->load($atts['id']);
948 if ($template->get('extension') === 'formidable') {
949 $entry_id = is_object($dataset_id) ? $dataset_id->id : $dataset_id;
950 if ($entry_id) {
951 if ($template->get('item') == '-2') {
952 if ($template->get('item1') == $form->id) {
953 $atts['dataset'] = $entry_id;
954 $shortcode[3] .= ' dataset="' . $entry_id . '"';
955 } elseif (!isset($atts['dataset2']) && $template->get('item2') == $form->id) {
956 $atts['dataset2'] = $entry_id;
957 $shortcode[3] .= ' dataset2="' . $entry_id . '"';
958 }
959 } else {
960 $atts['dataset'] = $entry_id;
961 $shortcode[3] .= ' dataset="' . $entry_id . '"';
962 }
963 }
964 }
965 }
966 if (!isset($atts['apply'])) {
967 $shortcode[3] .= ' apply="true"';
968 }
969 if (!isset($atts['filter'])) {
970 $shortcode[3] .= ' filter="true"';
971 }
972 $transient = false;
973 if (isset($atts['use_args_entry']) && $atts['use_args_entry'] == 'true' && $atts['dataset'] == $dataset_id) {
974 $dataset = 'tmp_' . $this->helper->load('encryption')->random_md5();
975 $transient = 'e2pdf_' . $dataset;
976 set_transient($transient, $args['entry'], 1800);
977 $atts['dataset'] = $dataset;
978 $shortcode[3] .= ' dataset="' . $dataset . '"';
979 }
980 $file = do_shortcode_tag($shortcode);
981 if ($file) {
982 $tmp = false;
983 if (substr($file, 0, 4) === 'tmp:') {
984 $file = substr($file, 4);
985 $tmp = true;
986 }
987 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
988 if ($tmp) {
989 $this->helper->add('formidable_attachments', $file);
990 }
991 } else {
992 $this->helper->add('formidable_attachments', $file);
993 }
994 $attachments[] = $file;
995 }
996 if ($transient) {
997 delete_transient($transient);
998 }
999 }
1000 }
1001 }
1002 }
1003 }
1004 return $attachments;
1005 }
1006
1007 /**
1008 * Decrypt protected uploaded file in a File Upload field
1009 * @param string $html
1010 * @param array $atts
1011 * @return string $html
1012 */
1013 public function filter_frm_image_html_array($html, $atts) {
1014
1015 if (isset($atts['decrypt']) &&
1016 $atts['decrypt'] &&
1017 class_exists('FrmProFileField') &&
1018 class_exists('FrmProFilePayloadBuilder') &&
1019 class_exists('FrmAppHelper')
1020 ) {
1021
1022 $id = $atts['media_id'];
1023 $permission = isset($atts['permission']) && $atts['permission'] ? true : false;
1024
1025 remove_filter('wp_get_attachment_url', 'FrmProFileField::filter_attachment_url');
1026 remove_filter('wp_get_attachment_image_src', 'FrmProFileField::filter_attachment_image_src');
1027
1028 $is_image = wp_attachment_is_image($id);
1029 $url = $this->get_file_url($id, $is_image ? $atts['size'] : false);
1030
1031 if (!FrmProFileField::user_has_permission($id) && !$permission) {
1032 $frm_settings = FrmAppHelper::get_settings();
1033 $html = $frm_settings->admin_permission;
1034 } else {
1035 $html = $atts['show_image'] ? wp_get_attachment_image($id, $atts['size'], !$is_image) : '';
1036
1037 /* If show_filename=1 is included */
1038 if ($atts['show_filename']) {
1039 $label = $this->get_single_file_name($id);
1040 if ($atts['show_image']) {
1041 $html .= ' <span id="frm_media_' . absint($id) . '" class="frm_upload_label">' . $label . '</span>';
1042 } else {
1043 $html .= $label;
1044 }
1045 }
1046
1047 /* If neither show_image or show_filename are included, get file URL */
1048 if (!$html) {
1049 $html = $url;
1050 }
1051
1052 /* If add_link=1 is included */
1053 if ($atts['add_link'] || (!$is_image && $atts['add_link_for_non_image'])) {
1054 $href = $is_image ? $this->get_file_url($id) : $url;
1055 $target = !empty($atts['new_tab']) ? ' target="_blank"' : '';
1056 $html = '<a href="' . esc_url($href) . '" class="frm_file_link"' . $target . '>' . $html . '</a>';
1057 }
1058
1059 if (!empty($atts['class'])) {
1060 $html = str_replace(' class="', ' class="' . esc_attr($atts['class'] . ' '), $html);
1061 }
1062
1063 add_filter('wp_get_attachment_url', 'FrmProFileField::filter_attachment_url', 10, 2);
1064 add_filter('wp_get_attachment_image_src', 'FrmProFileField::filter_attachment_image_src', 10, 4);
1065 }
1066 }
1067
1068 return $html;
1069 }
1070
1071 /**
1072 * @param int $id
1073 * @param string|int[]|bool $size
1074 * @param array $args supported keys include "url" and "leave_size_out_of_payload"
1075 * @return string unprotected url to use for our specified file id and size.
1076 */
1077 public function get_file_url($id, $size = false, $args = array()) {
1078 $url = isset($args['url']) ? $args['url'] : false;
1079 $builder = new FrmProFilePayloadBuilder($id, $size, $url);
1080 return $builder->get_url();
1081 }
1082
1083 /**
1084 * Remove Page Breaks for Visual Mapper
1085 * @param array $fields - List of fields
1086 * @return array - Updated fields
1087 */
1088 public function filter_remove_pagebreaks($fields) {
1089 foreach ((array) $fields as $field_key => $field) {
1090 if ($field->type == 'break') {
1091 unset($fields[$field_key]);
1092 }
1093 }
1094 return $fields;
1095 }
1096
1097 public function filter_frm_match_xml_form($edit_query, $form) {
1098 if (isset($edit_query['created_at'])) {
1099 $edit_query['created_at'] = date('Y-m-d H:i:s', strtotime('now'));
1100 }
1101 return $edit_query;
1102 }
1103
1104 public function filter_frm_show_new_entry_page() {
1105 return 'new';
1106 }
1107
1108 public function filter_frm_pre_display_form($form) {
1109 if (!empty($form->options['single_entry'])) {
1110 $form->options['single_entry'] = 0;
1111 }
1112 return $form;
1113 }
1114
1115 /**
1116 * Add options for Formidable extension
1117 * @param array $options - List of options
1118 * @return array - Updated options list
1119 */
1120 public function filter_e2pdf_model_options_get_options_options($options = array()) {
1121 $options['formidable_group'] = array(
1122 'name' => 'Formidable Forms',
1123 'action' => 'extension',
1124 'group' => 'formidable_group',
1125 'options' => array(
1126 array(
1127 'name' => __('Auto PDF and Visual Mapper', 'e2pdf'),
1128 'key' => 'e2pdf_formidable_use_keys',
1129 'value' => get_option('e2pdf_formidable_use_keys', '0'),
1130 'default_value' => '0',
1131 'type' => 'checkbox',
1132 'checkbox_value' => '1',
1133 'placeholder' => __('Use Field Keys instead Field IDs', 'e2pdf'),
1134 ),
1135 array(
1136 'name' => __('Filter', 'e2pdf'),
1137 'key' => 'e2pdf_formidable_disable_filter',
1138 'value' => get_option('e2pdf_formidable_disable_filter', '0'),
1139 'default_value' => '0',
1140 'type' => 'checkbox',
1141 'checkbox_value' => '1',
1142 'placeholder' => __('Disable Filter', 'e2pdf'),
1143 ),
1144 ),
1145 );
1146 return $options;
1147 }
1148
1149 public function filter_e2pdf_controller_templates_import_options($options) {
1150
1151 if (isset($options['item'])) {
1152 $options['item']['options'][] = array(
1153 'name' => 'Formidable Forms',
1154 'key' => 'options[formidable_item_new_form]',
1155 'value' => 1,
1156 'default_value' => 0,
1157 'type' => 'radio',
1158 'li' => array(
1159 'class' => 'e2pdf-import-extension-option e2pdf-hide',
1160 ),
1161 'options' => array(
1162 '1' => 'Recreate Web Form',
1163 '0' => 'Overwrite Web Form',
1164 ),
1165 );
1166 }
1167
1168 return $options;
1169 }
1170
1171 public function filter_e2pdf_controller_templates_backup_options($options, $template, $extension) {
1172
1173 if ($extension->loaded('formidable')) {
1174 $options['formidable'] = array(
1175 'name' => 'Formidable Forms',
1176 'options' => array(
1177 array(
1178 'name' => __('Force shortcodes to use', 'e2pdf'),
1179 'key' => 'options[formidable_force_shortcodes]',
1180 'value' => 0,
1181 'default_value' => 0,
1182 'type' => 'select',
1183 'options' => array(
1184 '0' => __('None', 'e2pdf'),
1185 '1' => __('Fields IDs', 'e2pdf'),
1186 '2' => __('Field Keys', 'e2pdf'),
1187 ),
1188 ),
1189 ),
1190 );
1191 }
1192
1193 return $options;
1194 }
1195
1196 public function filter_e2pdf_controller_templates_backup_pages($pages, $options, $template, $extension) {
1197
1198 if ($extension->loaded('formidable') && (isset($options['formidable_force_shortcodes']) && $options['formidable_force_shortcodes'])) {
1199 $fields = array();
1200 if ($template->get('item') == '-2') {
1201 if ($template->get('item1')) {
1202 $where = array('fi.form_id' => (int) $template->get('item1'));
1203 $item_fields = FrmField::getAll($where, 'id ASC');
1204 if ($item_fields) {
1205 $fields = array_merge($fields, $item_fields);
1206 }
1207 }
1208 if ($template->get('item2')) {
1209 $where = array('fi.form_id' => (int) $template->get('item2'));
1210 $item_fields = FrmField::getAll($where, 'id ASC');
1211 if ($item_fields) {
1212 $fields = array_merge($fields, $item_fields);
1213 }
1214 }
1215 } else {
1216 $where = array('fi.form_id' => (int) $template->get('item'));
1217 $fields = FrmField::getAll($where, 'id ASC');
1218 }
1219
1220 $search = array();
1221 $replace = array();
1222
1223 if ($options['formidable_force_shortcodes'] == '1') {
1224 foreach ($fields as $field_key => $field) {
1225 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1226 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1227 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1228 foreach ($sub_fields as $sub_field_key => $sub_field) {
1229 $search[] = $sub_field->field_key;
1230 $replace[] = $sub_field->id;
1231 }
1232 $search[] = $field->field_key;
1233 $replace[] = $field->id;
1234 } else {
1235 $search[] = $field->field_key;
1236 $replace[] = $field->id;
1237 }
1238 }
1239 } elseif ($options['formidable_force_shortcodes'] == '2') {
1240 foreach ($fields as $field_key => $field) {
1241
1242 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1243 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1244 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1245 foreach ($sub_fields as $sub_field_key => $sub_field) {
1246 $search[] = $sub_field->id;
1247 $replace[] = $sub_field->field_key;
1248 }
1249 $search[] = $field->id;
1250 $replace[] = $field->field_key;
1251 } else {
1252 $search[] = $field->id;
1253 $replace[] = $field->field_key;
1254 }
1255 }
1256 }
1257
1258 $search = array_reverse($search);
1259 $replace = array_reverse($replace);
1260 $list = array_combine($search, $replace);
1261 $pages = $this->pages_replace_shortcodes($pages, $list);
1262 }
1263
1264 return $pages;
1265 }
1266
1267 public function filter_e2pdf_controller_templates_backup_actions($actions, $options, $template, $extension) {
1268
1269 if ($extension->loaded('formidable') && (isset($options['formidable_force_shortcodes']) && $options['formidable_force_shortcodes'])) {
1270 $fields = array();
1271 if ($template->get('item') == '-2') {
1272 if ($template->get('item1')) {
1273 $where = array('fi.form_id' => (int) $template->get('item1'));
1274 $item_fields = FrmField::getAll($where, 'id ASC');
1275 if ($item_fields) {
1276 $fields = array_merge($fields, $item_fields);
1277 }
1278 }
1279 if ($template->get('item2')) {
1280 $where = array('fi.form_id' => (int) $template->get('item2'));
1281 $item_fields = FrmField::getAll($where, 'id ASC');
1282 if ($item_fields) {
1283 $fields = array_merge($fields, $item_fields);
1284 }
1285 }
1286 } else {
1287 $where = array('fi.form_id' => (int) $template->get('item'));
1288 $fields = FrmField::getAll($where, 'id ASC');
1289 }
1290
1291 $search = array();
1292 $replace = array();
1293
1294 if ($options['formidable_force_shortcodes'] == '1') {
1295 foreach ($fields as $field_key => $field) {
1296 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1297 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1298 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1299 foreach ($sub_fields as $sub_field_key => $sub_field) {
1300 $search[] = $sub_field->field_key;
1301 $replace[] = $sub_field->id;
1302 }
1303 $search[] = $field->field_key;
1304 $replace[] = $field->id;
1305 } else {
1306 $search[] = $field->field_key;
1307 $replace[] = $field->id;
1308 }
1309 }
1310 } elseif ($options['formidable_force_shortcodes'] == '2') {
1311 foreach ($fields as $field_key => $field) {
1312
1313 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1314 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1315 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1316 foreach ($sub_fields as $sub_field_key => $sub_field) {
1317 $search[] = $sub_field->id;
1318 $replace[] = $sub_field->field_key;
1319 }
1320 $search[] = $field->id;
1321 $replace[] = $field->field_key;
1322 } else {
1323 $search[] = $field->id;
1324 $replace[] = $field->field_key;
1325 }
1326 }
1327 }
1328
1329 $search = array_reverse($search);
1330 $replace = array_reverse($replace);
1331 $list = array_combine($search, $replace);
1332 $actions = $this->actions_replace_shortcodes($actions, $list);
1333 }
1334
1335 return $actions;
1336 }
1337
1338 public function filter_e2pdf_controller_templates_backup_replace_shortcodes($value, $options, $template, $extension) {
1339
1340 if ($extension->loaded('formidable') && (isset($options['formidable_force_shortcodes']) && $options['formidable_force_shortcodes'])) {
1341 $fields = array();
1342 if ($template->get('item') == '-2') {
1343 if ($template->get('item1')) {
1344 $where = array('fi.form_id' => (int) $template->get('item1'));
1345 $item_fields = FrmField::getAll($where, 'id ASC');
1346 if ($item_fields) {
1347 $fields = array_merge($fields, $item_fields);
1348 }
1349 }
1350 if ($template->get('item2')) {
1351 $where = array('fi.form_id' => (int) $template->get('item2'));
1352 $item_fields = FrmField::getAll($where, 'id ASC');
1353 if ($item_fields) {
1354 $fields = array_merge($fields, $item_fields);
1355 }
1356 }
1357 } else {
1358 $where = array('fi.form_id' => (int) $template->get('item'));
1359 $fields = FrmField::getAll($where, 'id ASC');
1360 }
1361
1362 $search = array();
1363 $replace = array();
1364
1365 if ($options['formidable_force_shortcodes'] == '1') {
1366 foreach ($fields as $field_key => $field) {
1367 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1368 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1369 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1370 foreach ($sub_fields as $sub_field_key => $sub_field) {
1371 $search[] = $sub_field->field_key;
1372 $replace[] = $sub_field->id;
1373 }
1374 $search[] = $field->field_key;
1375 $replace[] = $field->id;
1376 } else {
1377 $search[] = $field->field_key;
1378 $replace[] = $field->id;
1379 }
1380 }
1381 } elseif ($options['formidable_force_shortcodes'] == '2') {
1382 foreach ($fields as $field_key => $field) {
1383
1384 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1385 $sub_where = array('fi.form_id' => $field->field_options['form_select']);
1386 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1387 foreach ($sub_fields as $sub_field_key => $sub_field) {
1388 $search[] = $sub_field->id;
1389 $replace[] = $sub_field->field_key;
1390 }
1391 $search[] = $field->id;
1392 $replace[] = $field->field_key;
1393 } else {
1394 $search[] = $field->id;
1395 $replace[] = $field->field_key;
1396 }
1397 }
1398 }
1399
1400 $search = array_reverse($search);
1401 $replace = array_reverse($replace);
1402 $list = array_combine($search, $replace);
1403 $value = $this->replace_shortcodes($value, $list);
1404 }
1405
1406 return $value;
1407 }
1408
1409 public function filter_e2pdf_controller_templates_import_pages($pages, $options, $xml, $template, $extension) {
1410
1411 if ($extension->loaded('formidable') &&
1412 $template->get('item') &&
1413 (($template->get('item') != '-2' && $template->get('item') != (string) $xml->template->item) || ($template->get('item') == '-2' && ($template->get('item1') != (string) $xml->template->item1 || $template->get('item2') != (string) $xml->template->item2))) &&
1414 $xml->item->formidable &&
1415 $options['item'] &&
1416 class_exists('FrmXMLHelper') &&
1417 class_exists('FrmField')
1418 ) {
1419
1420 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
1421 file_put_contents($tmp, base64_decode((string) $xml->item->formidable), LOCK_EX);
1422 $dom = new DOMDocument();
1423 $success = $dom->loadXML(file_get_contents($tmp));
1424 $old_ids = array();
1425 $old_keys = array();
1426 if ($success && function_exists('simplexml_import_dom')) {
1427 $item_xml = simplexml_import_dom($dom);
1428 foreach ($item_xml->form as $form_key => $form) {
1429 if (($template->get('item') != '-2' && (string) $form->id == (string) $xml->template->item) || ($template->get('item') == '-2' && ((string) $form->id == (string) $xml->template->item1 || (string) $form->id == (string) $xml->template->item2))) {
1430 foreach ($form->field as $field_key => $field) {
1431 $field_options = @json_decode((string) $field->field_options, true); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1432 if (isset($field_options['form_select']) && $field_options['form_select']) {
1433 foreach ($item_xml->form as $sub_form_key => $sub_form) {
1434 if ((string) $sub_form->id == $field_options['form_select']) {
1435 foreach ($sub_form->field as $sub_field_key => $sub_field) {
1436 $old_ids[] = (string) $sub_field->id;
1437 $old_keys[] = (string) $sub_field->field_key;
1438 }
1439 }
1440 }
1441 $old_ids[] = (string) $field->id;
1442 $old_keys[] = (string) $field->field_key;
1443 } else {
1444 $old_ids[] = (string) $field->id;
1445 $old_keys[] = (string) $field->field_key;
1446 }
1447 }
1448 }
1449 }
1450 }
1451
1452 $fields = array();
1453 if ($template->get('item') == '-2') {
1454 if ($template->get('item1')) {
1455 $where = array('fi.form_id' => (int) $template->get('item1'));
1456 $item_fields = FrmField::getAll($where, 'id ASC');
1457 if ($item_fields) {
1458 $fields = array_merge($fields, $item_fields);
1459 }
1460 }
1461
1462 if ($template->get('item2')) {
1463 $where = array('fi.form_id' => (int) $template->get('item2'));
1464 $item_fields = FrmField::getAll($where, 'id ASC');
1465 if ($item_fields) {
1466 $fields = array_merge($fields, $item_fields);
1467 }
1468 }
1469 } else {
1470 $where = array('fi.form_id' => (int) $template->get('item'));
1471 $fields = FrmField::getAll($where, 'id ASC');
1472 }
1473
1474 $new_ids = array();
1475 $new_keys = array();
1476
1477 foreach ($fields as $field_key => $field) {
1478
1479 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1480 $sub_where = array('fi.form_id' => (int) $field->field_options['form_select']);
1481 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1482 foreach ($sub_fields as $sub_field_key => $sub_field) {
1483 $new_ids[] = $sub_field->id;
1484 $new_keys[] = $sub_field->field_key;
1485 }
1486 $new_ids[] = $field->id;
1487 $new_keys[] = $field->field_key;
1488 } else {
1489 $new_ids[] = $field->id;
1490 $new_keys[] = $field->field_key;
1491 }
1492 }
1493
1494 if (count($old_ids) === count($new_ids)) {
1495 $old_ids = array_reverse($old_ids);
1496 $new_ids = array_reverse($new_ids);
1497
1498 $old_keys = array_reverse($old_keys);
1499 $new_keys = array_reverse($new_keys);
1500
1501 $list_ids = array_combine($old_ids, $new_ids);
1502 $pages = $this->pages_replace_shortcodes($pages, $list_ids);
1503
1504 $list_keys = array_combine($old_keys, $new_keys);
1505 $pages = $this->pages_replace_shortcodes($pages, $list_keys);
1506 }
1507
1508 unset($dom);
1509 unlink($tmp);
1510 }
1511
1512 return $pages;
1513 }
1514
1515 public function filter_e2pdf_controller_templates_import_actions($actions, $options, $xml, $template, $extension) {
1516
1517 if ($extension->loaded('formidable') &&
1518 $template->get('item') &&
1519 (($template->get('item') != '-2' && $template->get('item') != (string) $xml->template->item) || ($template->get('item') == '-2' && ($template->get('item1') != (string) $xml->template->item1 || $template->get('item2') != (string) $xml->template->item2))) &&
1520 $xml->item->formidable &&
1521 $options['item'] &&
1522 class_exists('FrmXMLHelper') &&
1523 class_exists('FrmField')
1524 ) {
1525
1526 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
1527 file_put_contents($tmp, base64_decode((string) $xml->item->formidable), LOCK_EX);
1528
1529 $dom = new DOMDocument();
1530 $success = $dom->loadXML(file_get_contents($tmp));
1531
1532 $old_ids = array();
1533 $old_keys = array();
1534
1535 if ($success && function_exists('simplexml_import_dom')) {
1536 $item_xml = simplexml_import_dom($dom);
1537
1538 foreach ($item_xml->form as $form_key => $form) {
1539 if (($template->get('item') != '-2' && (string) $form->id == (string) $xml->template->item) || ($template->get('item') == '-2' && ((string) $form->id == (string) $xml->template->item1 || (string) $form->id == (string) $xml->template->item2))) {
1540 foreach ($form->field as $field_key => $field) {
1541 $field_options = @json_decode((string) $field->field_options, true); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1542 if (isset($field_options['form_select']) && $field_options['form_select']) {
1543 foreach ($item_xml->form as $sub_form_key => $sub_form) {
1544 if ((string) $sub_form->id == $field_options['form_select']) {
1545 foreach ($sub_form->field as $sub_field_key => $sub_field) {
1546 $old_ids[] = (string) $sub_field->id;
1547 $old_keys[] = (string) $sub_field->field_key;
1548 }
1549 }
1550 }
1551 $old_ids[] = (string) $field->id;
1552 $old_keys[] = (string) $field->field_key;
1553 } else {
1554 $old_ids[] = (string) $field->id;
1555 $old_keys[] = (string) $field->field_key;
1556 }
1557 }
1558 }
1559 }
1560 }
1561
1562 $fields = array();
1563 if ($template->get('item') == '-2') {
1564 if ($template->get('item1')) {
1565 $where = array('fi.form_id' => (int) $template->get('item1'));
1566 $item_fields = FrmField::getAll($where, 'id ASC');
1567 if ($item_fields) {
1568 $fields = array_merge($fields, $item_fields);
1569 }
1570 }
1571
1572 if ($template->get('item2')) {
1573 $where = array('fi.form_id' => (int) $template->get('item2'));
1574 $item_fields = FrmField::getAll($where, 'id ASC');
1575
1576 if ($item_fields) {
1577 $fields = array_merge($fields, $item_fields);
1578 }
1579 }
1580 } else {
1581 $where = array('fi.form_id' => (int) $template->get('item'));
1582 $fields = FrmField::getAll($where, 'id ASC');
1583 }
1584
1585 $new_ids = array();
1586 $new_keys = array();
1587
1588 foreach ($fields as $field_key => $field) {
1589
1590 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1591 $sub_where = array('fi.form_id' => (int) $field->field_options['form_select']);
1592 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1593 foreach ($sub_fields as $sub_field_key => $sub_field) {
1594 $new_ids[] = $sub_field->id;
1595 $new_keys[] = $sub_field->field_key;
1596 }
1597 $new_ids[] = $field->id;
1598 $new_keys[] = $field->field_key;
1599 } else {
1600 $new_ids[] = $field->id;
1601 $new_keys[] = $field->field_key;
1602 }
1603 }
1604
1605 if (count($old_ids) === count($new_ids)) {
1606 $old_ids = array_reverse($old_ids);
1607 $new_ids = array_reverse($new_ids);
1608
1609 $old_keys = array_reverse($old_keys);
1610 $new_keys = array_reverse($new_keys);
1611
1612 $list_ids = array_combine($old_ids, $new_ids);
1613 $actions = $this->actions_replace_shortcodes($actions, $list_ids);
1614
1615 $list_keys = array_combine($old_keys, $new_keys);
1616 $actions = $this->pages_replace_shortcodes($actions, $list_keys);
1617 }
1618 unset($dom);
1619 unlink($tmp);
1620 }
1621
1622 return $actions;
1623 }
1624
1625 public function filter_frm_graph_shortcode_custom_html($html, $data) {
1626 if (apply_filters('e2pdf_pdf_render', false)) {
1627 return serialize($data['graph_data']);
1628 }
1629 return $html;
1630 }
1631
1632 public function filter_e2pdf_controller_templates_import_replace_shortcodes($value, $options, $xml, $template, $extension) {
1633
1634 if ($extension->loaded('formidable') &&
1635 $template->get('item') &&
1636 (($template->get('item') != '-2' && $template->get('item') != (string) $xml->template->item) || ($template->get('item') == '-2' && ($template->get('item1') != (string) $xml->template->item1 || $template->get('item2') != (string) $xml->template->item2))) &&
1637 $xml->item->formidable &&
1638 $options['item'] &&
1639 class_exists('FrmXMLHelper') &&
1640 class_exists('FrmField')
1641 ) {
1642
1643 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
1644 file_put_contents($tmp, base64_decode((string) $xml->item->formidable), LOCK_EX);
1645
1646 $dom = new DOMDocument();
1647 $success = $dom->loadXML(file_get_contents($tmp));
1648
1649 $old_ids = array();
1650 $old_keys = array();
1651
1652 if ($success && function_exists('simplexml_import_dom')) {
1653 $item_xml = simplexml_import_dom($dom);
1654 foreach ($item_xml->form as $form_key => $form) {
1655 if (($template->get('item') != '-2' && (string) $form->id == (string) $xml->template->item) || ($template->get('item') == '-2' && ((string) $form->id == (string) $xml->template->item1 || (string) $form->id == (string) $xml->template->item2))) {
1656 foreach ($form->field as $field_key => $field) {
1657 $field_options = @json_decode((string) $field->field_options, true); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
1658 if (isset($field_options['form_select']) && $field_options['form_select']) {
1659 foreach ($item_xml->form as $sub_form_key => $sub_form) {
1660 if ((string) $sub_form->id == $field_options['form_select']) {
1661 foreach ($sub_form->field as $sub_field_key => $sub_field) {
1662 $old_ids[] = (string) $sub_field->id;
1663 $old_keys[] = (string) $sub_field->field_key;
1664 }
1665 }
1666 }
1667 $old_ids[] = (string) $field->id;
1668 $old_keys[] = (string) $field->field_key;
1669 } else {
1670 $old_ids[] = (string) $field->id;
1671 $old_keys[] = (string) $field->field_key;
1672 }
1673 }
1674 }
1675 }
1676 }
1677
1678 $fields = array();
1679 if ($template->get('item') == '-2') {
1680 if ($template->get('item1')) {
1681 $where = array('fi.form_id' => (int) $template->get('item1'));
1682 $item_fields = FrmField::getAll($where, 'id ASC');
1683 if ($item_fields) {
1684 $fields = array_merge($fields, $item_fields);
1685 }
1686 }
1687
1688 if ($template->get('item2')) {
1689 $where = array('fi.form_id' => (int) $template->get('item2'));
1690 $item_fields = FrmField::getAll($where, 'id ASC');
1691
1692 if ($item_fields) {
1693 $fields = array_merge($fields, $item_fields);
1694 }
1695 }
1696 } else {
1697 $where = array('fi.form_id' => (int) $template->get('item'));
1698 $fields = FrmField::getAll($where, 'id ASC');
1699 }
1700
1701 $new_ids = array();
1702 $new_keys = array();
1703
1704 foreach ($fields as $field_key => $field) {
1705 if (isset($field->field_options['form_select']) && $field->field_options['form_select']) {
1706 $sub_where = array('fi.form_id' => (int) $field->field_options['form_select']);
1707 $sub_fields = FrmField::getAll($sub_where, 'id ASC');
1708 foreach ($sub_fields as $sub_field_key => $sub_field) {
1709 $new_ids[] = $sub_field->id;
1710 $new_keys[] = $sub_field->field_key;
1711 }
1712 $new_ids[] = $field->id;
1713 $new_keys[] = $field->field_key;
1714 } else {
1715 $new_ids[] = $field->id;
1716 $new_keys[] = $field->field_key;
1717 }
1718 }
1719
1720 if (count($old_ids) === count($new_ids)) {
1721
1722 $old_ids = array_reverse($old_ids);
1723 $new_ids = array_reverse($new_ids);
1724
1725 $old_keys = array_reverse($old_keys);
1726 $new_keys = array_reverse($new_keys);
1727
1728 $list_ids = array_combine($old_ids, $new_ids);
1729 $value = $this->replace_shortcodes($value, $list_ids);
1730
1731 $list_keys = array_combine($old_keys, $new_keys);
1732 $value = $this->replace_shortcodes($value, $list_keys);
1733 }
1734
1735 unset($dom);
1736 unlink($tmp);
1737 }
1738 return $value;
1739 }
1740
1741 /**
1742 * Delete attachments that were sent by email
1743 */
1744 public function action_frm_notification() {
1745
1746 $files = $this->helper->get('formidable_attachments');
1747 if (is_array($files) && !empty($files)) {
1748 foreach ($files as $key => $file) {
1749 $this->helper->delete_dir(dirname($file) . '/');
1750 }
1751 $this->helper->deset('formidable_attachments');
1752 }
1753 }
1754
1755 /**
1756 * Fix to render E2Pdf shortcodes with beforeContent and afterContent inside Formidable Form View Preview
1757 */
1758 public function action_check_ajax_referer($action, $result) {
1759 if ($action == 'frm_ajax' && false !== $result && isset($_POST['action']) && $_POST['action'] == 'frm_views_process_box_preview') {
1760 if (isset($_POST['beforeContent']) && $_POST['beforeContent']) {
1761 $_POST['beforeContent'] = $this->filter_view_preview_before_after_content($_POST['beforeContent']);
1762 }
1763 if (isset($_POST['afterContent']) && $_POST['afterContent']) {
1764 $_POST['afterContent'] = $this->filter_view_preview_before_after_content($_POST['afterContent']);
1765 }
1766 }
1767 }
1768
1769 public function action_frm_default_value($entry_id, $form_id) {
1770 global $wpdb;
1771 if ($form_id) {
1772 $fields = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'frm_fields` WHERE form_id = %d AND (default_value LIKE %s OR default_value LIKE %s)', $form_id, '%[e2pdf-download%', '%[e2pdf-save%'));
1773 if (!empty($fields)) {
1774 foreach ($fields as $key => $field) {
1775 $meta = FrmEntryMeta::get_entry_meta_by_field($entry_id, $field->id);
1776 if ($meta === null) {
1777 $new_value = $this->filter_frm_content($field->default_value, FrmForm::getOne($form_id), $entry_id);
1778 $added = FrmEntryMeta::add_entry_meta($entry_id, $field->id, null, $new_value);
1779 if (!$added) {
1780 FrmEntryMeta::update_entry_meta($entry_id, $field->id, null, $new_value);
1781 }
1782 }
1783 }
1784 }
1785 }
1786 }
1787
1788 /**
1789 * Filter "get" shortcodes with beforeContent and afterContent inside Formidable Form View Preview
1790 * @param type $content
1791 * @return type
1792 */
1793 public function filter_view_preview_before_after_content($content) {
1794
1795 if (false !== strpos($content, '[get ') && (
1796 false !== strpos($content, 'e2pdf-download') ||
1797 false !== strpos($content, 'e2pdf-save') ||
1798 false !== strpos($content, 'e2pdf-view') ||
1799 false !== strpos($content, 'e2pdf-adobesign') ||
1800 false !== strpos($content, 'e2pdf-zapier') ||
1801 false !== strpos($content, 'e2pdf-attachment')
1802 )) {
1803 $shortcode_tags = array(
1804 'get',
1805 );
1806 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
1807 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1808 if (!empty($tagnames)) {
1809 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
1810 foreach ($shortcodes[0] as $key => $shortcode_value) {
1811 $content = str_replace($shortcode_value, $this->convert_shortcodes($shortcode_value), $content);
1812 }
1813 }
1814 }
1815 return $content;
1816 }
1817
1818 public function filter_filter_content($content, $form, $entry = false) {
1819 $content = FrmFormsController::replace_form_name_shortcodes($content, $form);
1820 if (!$entry) {
1821 return $content;
1822 }
1823 if (is_object($form)) {
1824 $form = $form->id;
1825 }
1826 if (!empty($entry->parent_entry)) {
1827 $form = $entry->parent_entry->form_id;
1828 }
1829 $shortcodes = $this->get('cached_shortcodes');
1830 $content = apply_filters('frm_replace_content_shortcodes', $content, $entry, $shortcodes);
1831 return $content;
1832 }
1833
1834 /**
1835 * Auto Generate of Template for this extension
1836 * @return array - List of elements
1837 */
1838 public function auto() {
1839
1840 $response = array();
1841 $elements = array();
1842
1843 $form_id = $this->get('item');
1844
1845 $fields = array();
1846 if (class_exists('FrmField')) {
1847 $fields = FrmField::get_all_for_form($form_id, '', 'include', 'include');
1848 }
1849
1850 if ($fields) {
1851 foreach ($fields as $key => $field) {
1852 $field_id = get_option('e2pdf_formidable_use_keys', '0') ? $field->field_key : $field->id;
1853
1854 if ($field->type === 'lookup' && isset($field->field_options['data_type'])) {
1855 if ($field->field_options['data_type'] == 'select') {
1856 $field->options = array(
1857 '[e2pdf-frm-lookup-values id="' . $field_id . '"]',
1858 );
1859 } elseif ($field->field_options['data_type'] == 'radio' || $field->field_options['data_type'] == 'checkbox') {
1860 $options = array();
1861 $frm_filter = false;
1862 if (class_exists('FrmProFieldsHelper') && method_exists('FrmProFieldsHelper', 'add_default_field_settings')) {
1863 if (!has_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings')) {
1864 add_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings', 10, 2);
1865 $frm_filter = true;
1866 }
1867 }
1868
1869 $args = array();
1870 $field_array = FrmAppHelper::start_field_array($field);
1871 FrmFieldsHelper::prepare_new_front_field($field_array, $field, $args);
1872 $field_array = array_merge($field->field_options, $field_array);
1873 if (isset($field_array['options']) && is_array($field_array['options'])) {
1874 $options = $field_array['options'];
1875 }
1876 if ($frm_filter) {
1877 remove_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings');
1878 }
1879 $field->options = $options;
1880 }
1881 $field->type = $field->field_options['data_type'];
1882 } elseif ($field->type === 'data' && isset($field->field_options['data_type'])) {
1883 if ($field->field_options['data_type'] == 'select') {
1884 $field->options = array(
1885 '[e2pdf-frm-data-values id="' . $field_id . '"]',
1886 );
1887 } elseif ($field->field_options['data_type'] == 'radio' || $field->field_options['data_type'] == 'checkbox') {
1888 $options = array();
1889 $frm_filter = false;
1890 if (class_exists('FrmProFieldsHelper') && method_exists('FrmProFieldsHelper', 'add_default_field_settings')) {
1891 if (!has_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings')) {
1892 add_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings', 10, 2);
1893 $frm_filter = true;
1894 }
1895 }
1896
1897 $args = array();
1898 $field->field_options['restrict'] = '0';
1899 $field_array = FrmAppHelper::start_field_array($field);
1900 FrmFieldsHelper::prepare_new_front_field($field_array, $field, $args);
1901
1902 $field_array = array_merge($field->field_options, $field_array);
1903 if (isset($field_array['options']) && is_array($field_array['options'])) {
1904 $options = $field_array['options'];
1905 }
1906
1907 if ($frm_filter) {
1908 remove_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings');
1909 }
1910 $field->options = $options;
1911 }
1912 $field->type = $field->field_options['data_type'];
1913 }
1914
1915 /*
1916 * Repeatable fields Field ID modification
1917 * @since 0.01.42
1918 */
1919 if ($field->type !== 'lookup' && $field->type !== 'data' && isset($field->form_id) && $field->form_id != $form_id) {
1920 $field_id = $field_id . ':1';
1921 }
1922
1923 switch ($field->type) {
1924 case 'divider':
1925 if ($field->name) {
1926 $elements[] = $this->auto_field(
1927 $field,
1928 array(
1929 'type' => 'e2pdf-html',
1930 'block' => true,
1931 'properties' => array(
1932 'top' => '20',
1933 'left' => '20',
1934 'right' => '20',
1935 'width' => '100%',
1936 'height' => 'auto',
1937 'value' => '<h2>' . $field->name . '</h2>',
1938 ),
1939 )
1940 );
1941 }
1942 break;
1943 case 'html':
1944 if ($field->description) {
1945 $elements[] = $this->auto_field(
1946 $field,
1947 array(
1948 'type' => 'e2pdf-html',
1949 'block' => true,
1950 'properties' => array(
1951 'top' => '20',
1952 'left' => '20',
1953 'right' => '20',
1954 'width' => '100%',
1955 'height' => 'auto',
1956 'value' => $field->description,
1957 ),
1958 )
1959 );
1960 }
1961 break;
1962 case 'signature':
1963 $elements[] = $this->auto_field(
1964 $field,
1965 array(
1966 'type' => 'e2pdf-html',
1967 'block' => true,
1968 'properties' => array(
1969 'top' => '20',
1970 'left' => '20',
1971 'right' => '20',
1972 'width' => '100%',
1973 'height' => 'auto',
1974 'value' => $field->name,
1975 ),
1976 )
1977 );
1978
1979 $elements[] = $this->auto_field(
1980 $field,
1981 array(
1982 'type' => 'e2pdf-signature',
1983 'properties' => array(
1984 'top' => '5',
1985 'width' => '100%',
1986 'height' => '150',
1987 'dimension' => '1',
1988 'block_dimension' => '1',
1989 'value' => '[' . $field_id . ']',
1990 ),
1991 )
1992 );
1993 break;
1994 case 'scale':
1995 $elements[] = $this->auto_field(
1996 $field,
1997 array(
1998 'type' => 'e2pdf-html',
1999 'block' => true,
2000 'properties' => array(
2001 'top' => '20',
2002 'left' => '20',
2003 'right' => '20',
2004 'width' => '100%',
2005 'height' => 'auto',
2006 'value' => $field->name,
2007 ),
2008 )
2009 );
2010
2011 if (isset($field->options) && is_array($field->options)) {
2012 $start = true;
2013 foreach ($field->options as $opt_key => $option) {
2014 if (is_array($option)) {
2015 $elements[] = $this->auto_field(
2016 $field,
2017 array(
2018 'type' => 'e2pdf-radio',
2019 'float' => $start ? false : true,
2020 'properties' => array(
2021 'top' => $start ? '5' : '0',
2022 'left' => $start ? '0' : '5',
2023 'width' => 'auto',
2024 'height' => 'auto',
2025 'value' => '[' . $field_id . ']',
2026 'option' => $option['value'],
2027 'group' => 'group_' . $field_id,
2028 ),
2029 )
2030 );
2031 } else {
2032 $elements[] = $this->auto_field(
2033 $field,
2034 array(
2035 'type' => 'e2pdf-radio',
2036 'float' => $start ? false : true,
2037 'properties' => array(
2038 'top' => $start ? '5' : '0',
2039 'left' => $start ? '0' : '5',
2040 'width' => 'auto',
2041 'height' => 'auto',
2042 'value' => '[' . $field_id . ']',
2043 'option' => $option,
2044 'group' => 'group_' . $field_id,
2045 ),
2046 )
2047 );
2048 }
2049
2050 $start = false;
2051 }
2052
2053 $start = true;
2054 foreach ($field->options as $opt_key => $option) {
2055 if (is_array($option)) {
2056
2057 $elements[] = $this->auto_field(
2058 $field,
2059 array(
2060 'type' => 'e2pdf-html',
2061 'float' => $start ? false : true,
2062 'properties' => array(
2063 'top' => $start ? '5' : '0',
2064 'left' => $start ? '0' : '5',
2065 'width' => 'auto',
2066 'height' => 'auto',
2067 'value' => $option['label'],
2068 'text_align' => 'center',
2069 ),
2070 )
2071 );
2072 } else {
2073 $elements[] = $this->auto_field(
2074 $field,
2075 array(
2076 'type' => 'e2pdf-html',
2077 'float' => $start ? false : true,
2078 'properties' => array(
2079 'top' => $start ? '5' : '0',
2080 'left' => $start ? '0' : '5',
2081 'width' => 'auto',
2082 'height' => 'auto',
2083 'value' => $option,
2084 'text_align' => 'center',
2085 ),
2086 )
2087 );
2088 }
2089 $start = false;
2090 }
2091 }
2092 break;
2093 case 'rte':
2094 $elements[] = $this->auto_field(
2095 $field,
2096 array(
2097 'type' => 'e2pdf-html',
2098 'block' => true,
2099 'properties' => array(
2100 'top' => '20',
2101 'left' => '20',
2102 'right' => '20',
2103 'width' => '100%',
2104 'height' => 'auto',
2105 'value' => $field->name,
2106 ),
2107 )
2108 );
2109
2110 $elements[] = $this->auto_field(
2111 $field,
2112 array(
2113 'type' => 'e2pdf-html',
2114 'properties' => array(
2115 'top' => '5',
2116 'width' => '100%',
2117 'height' => '150',
2118 'value' => '[' . $field_id . ' wpautop=0]',
2119 ),
2120 )
2121 );
2122 break;
2123 case 'text':
2124 case 'email':
2125 case 'url':
2126 case 'number':
2127 case 'phone':
2128 case 'date':
2129 case 'image':
2130 case 'tag':
2131 case 'password':
2132 case 'quiz_score':
2133 $elements[] = $this->auto_field(
2134 $field,
2135 array(
2136 'type' => 'e2pdf-html',
2137 'block' => true,
2138 'properties' => array(
2139 'top' => '20',
2140 'left' => '20',
2141 'right' => '20',
2142 'width' => '100%',
2143 'height' => 'auto',
2144 'value' => $field->name,
2145 ),
2146 )
2147 );
2148
2149 $elements[] = $this->auto_field(
2150 $field,
2151 array(
2152 'type' => 'e2pdf-input',
2153 'properties' => array(
2154 'top' => '5',
2155 'width' => '100%',
2156 'height' => 'auto',
2157 'value' => '[' . $field_id . ']',
2158 'pass' => $field->type === 'password' ? '1' : '0',
2159 ),
2160 )
2161 );
2162 break;
2163 case 'time':
2164 $elements[] = $this->auto_field(
2165 $field,
2166 array(
2167 'type' => 'e2pdf-html',
2168 'block' => true,
2169 'properties' => array(
2170 'top' => '20',
2171 'left' => '20',
2172 'right' => '20',
2173 'width' => '100%',
2174 'height' => 'auto',
2175 'value' => $field->name,
2176 ),
2177 )
2178 );
2179
2180 $options_tmp = array();
2181 if (class_exists('FrmProFieldTime')) {
2182 $frm_pro_field_time = new FrmProFieldTime($field);
2183 $options_tmp = $frm_pro_field_time->get_options($field->field_options);
2184 }
2185
2186 if (isset($field->field_options['single_time']) && $field->field_options['single_time']) {
2187 $elements[] = $this->auto_field(
2188 $field,
2189 array(
2190 'type' => 'e2pdf-select',
2191 'properties' => array(
2192 'top' => '5',
2193 'width' => '100%',
2194 'height' => 'auto',
2195 'options' => implode("\n", $options_tmp),
2196 'value' => '[' . $field_id . ']',
2197 ),
2198 )
2199 );
2200 } else {
2201
2202 $options_h = isset($options_tmp['H']) && is_array($options_tmp['H']) ? $options_tmp['H'] : array();
2203 $options_m = isset($options_tmp['m']) && is_array($options_tmp['m']) ? $options_tmp['m'] : array();
2204 $options_a = isset($options_tmp['A']) && is_array($options_tmp['A']) ? $options_tmp['A'] : array();
2205
2206 $elements[] = $this->auto_field(
2207 $field,
2208 array(
2209 'type' => 'e2pdf-select',
2210 'float' => true,
2211 'properties' => array(
2212 'top' => '5',
2213 'width' => isset($field->field_options['clock']) && $field->field_options['clock'] == '12' ? '33.3%' : '50%',
2214 'height' => 'auto',
2215 'options' => implode("\n", $options_h),
2216 'value' => isset($field->field_options['clock']) && $field->field_options['clock'] == '12' ? '[' . $field_id . ' format="g"]' : '[' . $field_id . ' format="H"]',
2217 ),
2218 )
2219 );
2220
2221 $elements[] = $this->auto_field(
2222 $field,
2223 array(
2224 'type' => 'e2pdf-select',
2225 'float' => true,
2226 'properties' => array(
2227 'top' => '5',
2228 'left' => '20',
2229 'width' => isset($field->field_options['clock']) && $field->field_options['clock'] == '12' ? '33.3%' : '50%',
2230 'height' => 'auto',
2231 'options' => implode("\n", $options_m),
2232 'value' => '[' . $field_id . ' format="i"]',
2233 ),
2234 )
2235 );
2236
2237 if (isset($field->field_options['clock']) && $field->field_options['clock'] == '12') {
2238 $elements[] = $this->auto_field(
2239 $field,
2240 array(
2241 'type' => 'e2pdf-select',
2242 'float' => true,
2243 'properties' => array(
2244 'top' => '5',
2245 'left' => '20',
2246 'width' => '33.3%',
2247 'height' => 'auto',
2248 'options' => implode("\n", $options_a),
2249 'value' => '[' . $field_id . ' format="A"]',
2250 ),
2251 )
2252 );
2253 }
2254 }
2255 break;
2256 case 'select':
2257 $elements[] = $this->auto_field(
2258 $field,
2259 array(
2260 'type' => 'e2pdf-html',
2261 'block' => true,
2262 'properties' => array(
2263 'top' => '20',
2264 'left' => '20',
2265 'right' => '20',
2266 'width' => '100%',
2267 'height' => 'auto',
2268 'value' => $field->name,
2269 ),
2270 )
2271 );
2272
2273 $options_tmp = array();
2274 if (isset($field->options) && is_array($field->options)) {
2275 foreach ($field->options as $opt_key => $option) {
2276 if (is_array($option)) {
2277 $options_tmp[] = isset($option['label']) ? $option['label'] : $option['value'];
2278 } else {
2279 $options_tmp[] = $option;
2280 }
2281 }
2282 }
2283
2284 $elements[] = $this->auto_field(
2285 $field,
2286 array(
2287 'type' => 'e2pdf-select',
2288 'properties' => array(
2289 'top' => '5',
2290 'width' => '100%',
2291 'height' => 'auto',
2292 'options' => implode("\n", $options_tmp),
2293 'value' => '[' . $field_id . ']',
2294 'height' => isset($field->field_options['multiple']) && $field->field_options['multiple'] ? '80' : 'auto',
2295 'multiline' => isset($field->field_options['multiple']) && $field->field_options['multiple'] ? '1' : '0',
2296 ),
2297 )
2298 );
2299 break;
2300 case 'credit_card':
2301 $elements[] = $this->auto_field(
2302 $field,
2303 array(
2304 'type' => 'e2pdf-html',
2305 'block' => true,
2306 'properties' => array(
2307 'top' => '20',
2308 'left' => '20',
2309 'right' => '20',
2310 'width' => '100%',
2311 'height' => 'auto',
2312 'value' => $field->name,
2313 ),
2314 )
2315 );
2316
2317 $elements[] = $this->auto_field(
2318 $field,
2319 array(
2320 'type' => 'e2pdf-input',
2321 'properties' => array(
2322 'top' => '5',
2323 'width' => '100%',
2324 'height' => 'auto',
2325 'value' => '[' . $field_id . ' show="cc"]',
2326 ),
2327 )
2328 );
2329
2330 $elements[] = $this->auto_field(
2331 $field,
2332 array(
2333 'type' => 'e2pdf-select',
2334 'properties' => array(
2335 'top' => '5',
2336 'width' => '33.3%',
2337 'left' => '0',
2338 'height' => 'auto',
2339 'options' => implode("\n", range(1, 12)),
2340 'value' => '[' . $field_id . ' show="month"]',
2341 ),
2342 )
2343 );
2344
2345 $elements[] = $this->auto_field(
2346 $field,
2347 array(
2348 'type' => 'e2pdf-select',
2349 'float' => true,
2350 'properties' => array(
2351 'top' => '5',
2352 'left' => '20',
2353 'width' => '33.3%',
2354 'height' => 'auto',
2355 'options' => implode("\n", range(date('Y'), date('Y') + 10)),
2356 'value' => '[' . $field_id . ' show="year"]',
2357 ),
2358 )
2359 );
2360
2361 $elements[] = $this->auto_field(
2362 $field,
2363 array(
2364 'type' => 'e2pdf-input',
2365 'float' => true,
2366 'properties' => array(
2367 'top' => '5',
2368 'left' => '20',
2369 'width' => '33.3%',
2370 'height' => 'auto',
2371 'value' => '[' . $field_id . ' show="cvc"]',
2372 ),
2373 )
2374 );
2375 break;
2376 case 'address':
2377 $elements[] = $this->auto_field(
2378 $field,
2379 array(
2380 'type' => 'e2pdf-html',
2381 'block' => true,
2382 'properties' => array(
2383 'top' => '20',
2384 'left' => '20',
2385 'right' => '20',
2386 'width' => '100%',
2387 'height' => 'auto',
2388 'value' => $field->name,
2389 ),
2390 )
2391 );
2392
2393 if (isset($field->field_options['line1_desc']) && $field->field_options['line1_desc']) {
2394 $elements[] = $this->auto_field(
2395 $field,
2396 array(
2397 'type' => 'e2pdf-html',
2398 'properties' => array(
2399 'top' => '5',
2400 'width' => '100%',
2401 'height' => 'auto',
2402 'value' => $field->field_options['line1_desc'],
2403 ),
2404 )
2405 );
2406 }
2407
2408 $elements[] = $this->auto_field(
2409 $field,
2410 array(
2411 'type' => 'e2pdf-input',
2412 'properties' => array(
2413 'top' => '5',
2414 'width' => '100%',
2415 'height' => 'auto',
2416 'value' => '[' . $field_id . ' show="line1"]',
2417 ),
2418 )
2419 );
2420
2421 if (isset($field->field_options['line2_desc']) && $field->field_options['line2_desc']) {
2422 $elements[] = $this->auto_field(
2423 $field,
2424 array(
2425 'type' => 'e2pdf-html',
2426 'properties' => array(
2427 'top' => '5',
2428 'width' => '100%',
2429 'height' => 'auto',
2430 'value' => $field->field_options['line2_desc'],
2431 ),
2432 )
2433 );
2434 }
2435
2436 $elements[] = $this->auto_field(
2437 $field,
2438 array(
2439 'type' => 'e2pdf-input',
2440 'properties' => array(
2441 'top' => '5',
2442 'width' => '100%',
2443 'height' => 'auto',
2444 'value' => '[' . $field_id . ' show="line2"]',
2445 ),
2446 )
2447 );
2448
2449 if (
2450 (isset($field->field_options['city_desc']) && $field->field_options['city_desc']) ||
2451 (isset($field->field_options['state_desc']) && $field->field_options['state_desc'] && isset($field->field_options['address_type']) && $field->field_options['address_type'] != 'europe') ||
2452 (isset($field->field_options['zip_desc']) && $field->field_options['zip_desc'])
2453 ) {
2454
2455 $float = false;
2456 if (isset($field->field_options['address_type']) && $field->field_options['address_type'] != 'europe') {
2457 $elements[] = $this->auto_field(
2458 $field,
2459 array(
2460 'type' => 'e2pdf-html',
2461 'float' => $float,
2462 'properties' => array(
2463 'top' => '5',
2464 'left' => $float ? '20' : '0',
2465 'width' => '33.3%',
2466 'height' => 'auto',
2467 'value' => isset($field->field_options['city_desc']) && $field->field_options['city_desc'] ? $field->field_options['city_desc'] : '',
2468 ),
2469 )
2470 );
2471 $float = true;
2472 }
2473
2474 if (isset($field->field_options['address_type']) && (
2475 $field->field_options['address_type'] == 'international' ||
2476 $field->field_options['address_type'] == 'us' ||
2477 $field->field_options['address_type'] == 'generic'
2478 )
2479 ) {
2480
2481 $elements[] = $this->auto_field(
2482 $field,
2483 array(
2484 'type' => 'e2pdf-html',
2485 'float' => $float,
2486 'properties' => array(
2487 'top' => '5',
2488 'left' => $float ? '20' : '0',
2489 'width' => '33.3%',
2490 'height' => 'auto',
2491 'value' => isset($field->field_options['state_desc']) && $field->field_options['state_desc'] ? $field->field_options['state_desc'] : '',
2492 ),
2493 )
2494 );
2495 $float = true;
2496 }
2497
2498 $elements[] = $this->auto_field(
2499 $field,
2500 array(
2501 'type' => 'e2pdf-html',
2502 'float' => $float,
2503 'properties' => array(
2504 'top' => '5',
2505 'left' => $float ? '20' : '0',
2506 'width' => '33.3%',
2507 'height' => 'auto',
2508 'value' => isset($field->field_options['zip_desc']) && $field->field_options['zip_desc'] ? $field->field_options['zip_desc'] : '',
2509 ),
2510 )
2511 );
2512 $float = true;
2513
2514 if (isset($field->field_options['address_type']) && $field->field_options['address_type'] == 'europe') {
2515
2516 $elements[] = $this->auto_field(
2517 $field,
2518 array(
2519 'type' => 'e2pdf-html',
2520 'float' => $float,
2521 'properties' => array(
2522 'top' => '5',
2523 'left' => $float ? '20' : '0',
2524 'width' => '33.3%',
2525 'height' => 'auto',
2526 'value' => isset($field->field_options['city_desc']) && $field->field_options['city_desc'] ? $field->field_options['city_desc'] : '',
2527 ),
2528 )
2529 );
2530 }
2531 }
2532
2533 $float = false;
2534 if (isset($field->field_options['address_type']) && $field->field_options['address_type'] != 'europe') {
2535 $elements[] = $this->auto_field(
2536 $field,
2537 array(
2538 'type' => 'e2pdf-input',
2539 'float' => $float,
2540 'properties' => array(
2541 'top' => '5',
2542 'left' => $float ? '20' : '0',
2543 'width' => '33.3%',
2544 'height' => 'auto',
2545 'value' => '[' . $field_id . ' show="city"]',
2546 ),
2547 )
2548 );
2549 $float = true;
2550 }
2551
2552 if (isset($field->field_options['address_type']) && (
2553 $field->field_options['address_type'] == 'international' ||
2554 $field->field_options['address_type'] == 'us' ||
2555 $field->field_options['address_type'] == 'generic'
2556 )
2557 ) {
2558 if ($field->field_options['address_type'] == 'us') {
2559 $options_tmp = array();
2560 if (class_exists('FrmFieldsHelper')) {
2561 $options_tmp = array_values(FrmFieldsHelper::get_us_states());
2562 }
2563
2564 $elements[] = $this->auto_field(
2565 $field,
2566 array(
2567 'type' => 'e2pdf-select',
2568 'float' => $float,
2569 'properties' => array(
2570 'top' => '5',
2571 'left' => $float ? '20' : '0',
2572 'width' => '33.3%',
2573 'height' => 'auto',
2574 'options' => implode("\n", $options_tmp),
2575 'value' => '[' . $field_id . ' show="state"]',
2576 ),
2577 )
2578 );
2579 } else {
2580 $elements[] = $this->auto_field(
2581 $field,
2582 array(
2583 'type' => 'e2pdf-input',
2584 'float' => $float,
2585 'properties' => array(
2586 'top' => '5',
2587 'left' => $float ? '20' : '0',
2588 'width' => '33.3%',
2589 'height' => 'auto',
2590 'value' => '[' . $field_id . ' show="state"]',
2591 ),
2592 )
2593 );
2594 }
2595 $float = true;
2596 }
2597
2598 $elements[] = $this->auto_field(
2599 $field,
2600 array(
2601 'type' => 'e2pdf-input',
2602 'float' => $float,
2603 'properties' => array(
2604 'top' => '5',
2605 'left' => $float ? '20' : '0',
2606 'width' => '33.3%',
2607 'height' => 'auto',
2608 'value' => '[' . $field_id . ' show="zip"]',
2609 ),
2610 )
2611 );
2612 $float = true;
2613
2614 if (isset($field->field_options['address_type']) && $field->field_options['address_type'] == 'europe') {
2615 $elements[] = $this->auto_field(
2616 $field,
2617 array(
2618 'type' => 'e2pdf-input',
2619 'float' => $float,
2620 'properties' => array(
2621 'top' => '5',
2622 'left' => $float ? '20' : '0',
2623 'width' => '33.3%',
2624 'height' => 'auto',
2625 'value' => '[' . $field_id . ' show="city"]',
2626 ),
2627 )
2628 );
2629 }
2630
2631 if (isset($field->field_options['address_type']) && ($field->field_options['address_type'] == 'international' || $field->field_options['address_type'] == 'europe')) {
2632 if (isset($field->field_options['country_desc']) && $field->field_options['country_desc']) {
2633 $elements[] = $this->auto_field(
2634 $field,
2635 array(
2636 'type' => 'e2pdf-html',
2637 'properties' => array(
2638 'top' => '5',
2639 'width' => '100%',
2640 'height' => 'auto',
2641 'value' => $field->field_options['country_desc'],
2642 ),
2643 )
2644 );
2645 }
2646
2647 $options_tmp = array();
2648 if (class_exists('FrmFieldsHelper')) {
2649 $options_tmp = array_values(FrmFieldsHelper::get_countries());
2650 }
2651
2652 $elements[] = $this->auto_field(
2653 $field,
2654 array(
2655 'type' => 'e2pdf-select',
2656 'properties' => array(
2657 'top' => '5',
2658 'width' => '100%',
2659 'height' => 'auto',
2660 'options' => implode("\n", $options_tmp),
2661 'value' => '[' . $field_id . ' show="country"]',
2662 ),
2663 )
2664 );
2665 }
2666 break;
2667 case 'textarea':
2668 $elements[] = $this->auto_field(
2669 $field,
2670 array(
2671 'type' => 'e2pdf-html',
2672 'block' => true,
2673 'properties' => array(
2674 'top' => '20',
2675 'left' => '20',
2676 'right' => '20',
2677 'width' => '100%',
2678 'height' => 'auto',
2679 'value' => $field->name,
2680 ),
2681 )
2682 );
2683 $elements[] = $this->auto_field(
2684 $field,
2685 array(
2686 'type' => 'e2pdf-textarea',
2687 'properties' => array(
2688 'top' => '5',
2689 'width' => '100%',
2690 'height' => '150',
2691 'value' => '[' . $field_id . ' wpautop=0]',
2692 ),
2693 )
2694 );
2695 break;
2696 case 'file':
2697 $elements[] = $this->auto_field(
2698 $field,
2699 array(
2700 'type' => 'e2pdf-html',
2701 'block' => true,
2702 'properties' => array(
2703 'top' => '20',
2704 'left' => '20',
2705 'right' => '20',
2706 'width' => '100%',
2707 'height' => 'auto',
2708 'value' => $field->name,
2709 ),
2710 )
2711 );
2712 if (strpos($field_id, ':') !== false) {
2713 $field_id .= ' size="full" show_image="0" add_link="0"';
2714 } else {
2715 $field_id .= ' size="full"';
2716 }
2717 $elements[] = $this->auto_field(
2718 $field,
2719 array(
2720 'type' => 'e2pdf-textarea',
2721 'properties' => array(
2722 'top' => '5',
2723 'width' => '100%',
2724 'height' => '150',
2725 'value' => '[' . $field_id . ']',
2726 ),
2727 )
2728 );
2729 break;
2730 case 'radio':
2731 $elements[] = $this->auto_field(
2732 $field,
2733 array(
2734 'type' => 'e2pdf-html',
2735 'block' => true,
2736 'properties' => array(
2737 'top' => '20',
2738 'left' => '20',
2739 'right' => '20',
2740 'width' => '100%',
2741 'height' => 'auto',
2742 'value' => $field->name,
2743 ),
2744 )
2745 );
2746
2747 $image_options = false;
2748 if (isset($field->field_options['image_options']) && $field->field_options['image_options']) {
2749 $image_options = true;
2750 }
2751
2752 $separate_value = false;
2753 if (isset($field->field_options['separate_value']) && $field->field_options['separate_value']) {
2754 $separate_value = true;
2755 }
2756
2757 if (isset($field->options) && is_array($field->options)) {
2758
2759 foreach ($field->options as $opt_key => $option) {
2760 if (is_array($option)) {
2761 $elements[] = $this->auto_field(
2762 $field,
2763 array(
2764 'type' => 'e2pdf-radio',
2765 'properties' => array(
2766 'top' => '5',
2767 'width' => 'auto',
2768 'height' => 'auto',
2769 'value' => $image_options ? '[' . $field_id . ' show="value"]' : '[' . $field_id . ']',
2770 'option' => $image_options && $separate_value && isset($option['value']) ? $option['value'] : $option['label'],
2771 'group' => '[' . $field_id . ']',
2772 ),
2773 )
2774 );
2775 $elements[] = $this->auto_field(
2776 $field,
2777 array(
2778 'type' => 'e2pdf-html',
2779 'float' => true,
2780 'properties' => array(
2781 'left' => '5',
2782 'width' => '100%',
2783 'height' => 'auto',
2784 'value' => $option['label'],
2785 ),
2786 )
2787 );
2788 } else {
2789
2790 $elements[] = $this->auto_field(
2791 $field,
2792 array(
2793 'type' => 'e2pdf-radio',
2794 'properties' => array(
2795 'top' => '5',
2796 'width' => 'auto',
2797 'height' => 'auto',
2798 'value' => '[' . $field_id . ']',
2799 'option' => substr($opt_key, 0, 6) === 'other_' ? '[' . $field_id . ' show="value" key="other"]' : $option,
2800 'group' => '[' . $field_id . ']',
2801 ),
2802 )
2803 );
2804 $elements[] = $this->auto_field(
2805 $field,
2806 array(
2807 'type' => 'e2pdf-html',
2808 'float' => true,
2809 'properties' => array(
2810 'left' => '5',
2811 'width' => '100%',
2812 'height' => 'auto',
2813 'value' => $option,
2814 ),
2815 )
2816 );
2817
2818 if (substr($opt_key, 0, 6) === 'other_') {
2819 $elements[] = $this->auto_field(
2820 $field,
2821 array(
2822 'type' => 'e2pdf-input',
2823 'properties' => array(
2824 'top' => '5',
2825 'width' => '100%',
2826 'height' => 'auto',
2827 'value' => '[' . $field_id . ' show="value" key="other"]',
2828 ),
2829 )
2830 );
2831 }
2832 }
2833 }
2834 }
2835 break;
2836 case 'checkbox':
2837 $elements[] = $this->auto_field(
2838 $field,
2839 array(
2840 'type' => 'e2pdf-html',
2841 'block' => true,
2842 'properties' => array(
2843 'top' => '20',
2844 'left' => '20',
2845 'right' => '20',
2846 'width' => '100%',
2847 'height' => 'auto',
2848 'value' => $field->name,
2849 ),
2850 )
2851 );
2852
2853 $image_options = false;
2854 if (isset($field->field_options['image_options']) && $field->field_options['image_options']) {
2855 $image_options = true;
2856 }
2857
2858 $separate_value = false;
2859 if (isset($field->field_options['separate_value']) && $field->field_options['separate_value']) {
2860 $separate_value = true;
2861 }
2862
2863 if (isset($field->options) && is_array($field->options)) {
2864 foreach ($field->options as $opt_key => $option) {
2865 if (is_array($option)) {
2866 $elements[] = $this->auto_field(
2867 $field,
2868 array(
2869 'type' => 'e2pdf-checkbox',
2870 'properties' => array(
2871 'top' => '5',
2872 'width' => 'auto',
2873 'height' => 'auto',
2874 'value' => $image_options ? '[' . $field_id . ' show="value"]' : '[' . $field_id . ']',
2875 'option' => $image_options && $separate_value && isset($option['value']) ? $option['value'] : $option['label'],
2876 ),
2877 )
2878 );
2879 $elements[] = $this->auto_field(
2880 $field,
2881 array(
2882 'type' => 'e2pdf-html',
2883 'float' => true,
2884 'properties' => array(
2885 'left' => '5',
2886 'width' => '100%',
2887 'height' => 'auto',
2888 'value' => $option['label'],
2889 ),
2890 )
2891 );
2892 } else {
2893
2894 $elements[] = $this->auto_field(
2895 $field,
2896 array(
2897 'type' => 'e2pdf-checkbox',
2898 'properties' => array(
2899 'top' => '5',
2900 'width' => 'auto',
2901 'height' => 'auto',
2902 'value' => '[' . $field_id . ']',
2903 'option' => substr($opt_key, 0, 6) === 'other_' ? '[' . $field_id . ' show="value" key="' . $opt_key . '"]' : $option,
2904 ),
2905 )
2906 );
2907
2908 $elements[] = $this->auto_field(
2909 $field,
2910 array(
2911 'type' => 'e2pdf-html',
2912 'float' => true,
2913 'properties' => array(
2914 'left' => '5',
2915 'width' => '100%',
2916 'height' => 'auto',
2917 'value' => $option,
2918 ),
2919 )
2920 );
2921
2922 if (substr($opt_key, 0, 6) === 'other_') {
2923 $elements[] = $this->auto_field(
2924 $field,
2925 array(
2926 'type' => 'e2pdf-input',
2927 'properties' => array(
2928 'top' => '5',
2929 'width' => '100%',
2930 'height' => 'auto',
2931 'value' => '[' . $field_id . ' show="value" key="' . $opt_key . '"]',
2932 ),
2933 )
2934 );
2935 }
2936 }
2937 }
2938 }
2939 break;
2940 default:
2941 break;
2942 }
2943 }
2944 }
2945
2946 $response['page'] = array(
2947 'bottom' => '20',
2948 'top' => '20',
2949 'right' => '20',
2950 'left' => '20',
2951 );
2952
2953 $response['elements'] = $elements;
2954 return $response;
2955 }
2956
2957 /**
2958 * Generate field for Auto PDF
2959 * @param object $field - Formidable field object
2960 * @param string $type - Field type
2961 * @param array $options - Field additional options
2962 * @return array - Prepared auto field
2963 */
2964 public function auto_field($field = false, $element = array()) {
2965
2966 if (!$field) {
2967 return false;
2968 }
2969
2970 if (!isset($element['block'])) {
2971 $element['block'] = false;
2972 }
2973
2974 if (!isset($element['float'])) {
2975 $element['float'] = false;
2976 }
2977
2978 $classes = array();
2979 if (isset($field->field_options['classes'])) {
2980 $classes = explode(' ', $field->field_options['classes']);
2981 }
2982
2983 $float_classes = array(
2984 'frm_half',
2985 'frm_third',
2986 'frm_two_thirds',
2987 'frm_fourth',
2988 'frm_three_fourths',
2989 'frm_fifth',
2990 'frm_two_fifths',
2991 'frm_sixth',
2992 'frm_seventh',
2993 'frm_eighth',
2994 );
2995
2996 $array_intersect = array_intersect($classes, $float_classes);
2997
2998 if (!empty($array_intersect) && !in_array('frm_first', $classes) && isset($element['block']) && $element['block']) {
2999 $element['float'] = true;
3000 }
3001
3002 $primary_class = false;
3003 if (!empty($array_intersect)) {
3004 $primary_class = end($array_intersect);
3005 }
3006
3007 if (isset($element['block']) && $element['block']) {
3008 switch ($primary_class) {
3009 case 'frm_half':
3010 $element['properties']['width'] = '50%';
3011 break;
3012 case 'frm_third':
3013 $element['properties']['width'] = '33.3%';
3014 break;
3015 case 'frm_two_thirds':
3016 $element['properties']['width'] = '66.67%';
3017 break;
3018 case 'frm_fourth':
3019 $element['properties']['width'] = '25%';
3020 break;
3021 case 'frm_three_fourths':
3022 $element['properties']['width'] = '75%';
3023 break;
3024 case 'frm_fifth':
3025 $element['properties']['width'] = '20%';
3026 break;
3027 case 'frm_two_fifths':
3028 $element['properties']['width'] = '80%';
3029 break;
3030 case 'frm_sixth':
3031 $element['properties']['width'] = '16.67%';
3032 break;
3033 case 'frm_seventh':
3034 $element['properties']['width'] = '14.29%';
3035 break;
3036 case 'frm_eighth':
3037 $element['properties']['width'] = '12.5%';
3038 break;
3039 default:
3040 break;
3041 }
3042 }
3043
3044 return $element;
3045 }
3046
3047 /**
3048 * Backup form action
3049 * @param object xml - XML object where to add params for saved item
3050 */
3051 public function backup($xml = false) {
3052 if (class_exists('FrmXMLController')) {
3053
3054 $ids = array();
3055 if ($this->get('item') == '-2') {
3056 $ids[] = $this->get('item1');
3057 $ids[] = $this->get('item2');
3058 } else {
3059 $ids[] = $this->get('item');
3060 }
3061
3062 $type = array();
3063 $type[] = 'forms';
3064
3065 ob_start();
3066 FrmXMLController::generate_xml($type, compact('ids'));
3067 $backup = ob_get_clean();
3068 $xml->addChildCData('formidable', base64_encode($backup));
3069 }
3070 }
3071
3072 /**
3073 * Import form action
3074 * @param object xml - XML object to parse data to import form
3075 */
3076 public function import($xml, $options = array()) {
3077
3078 $updated_items = array();
3079 $new_form = isset($options['formidable_item_new_form']) && $options['formidable_item_new_form'] ? true : false;
3080
3081 if (class_exists('FrmXMLHelper') && class_exists('FrmField') && $this->get('item')) {
3082
3083 if (isset($xml->formidable) && $xml->formidable) {
3084
3085 $tmp = tempnam($this->helper->get('tmp_dir'), 'e2pdf');
3086 file_put_contents($tmp, base64_decode((string) $xml->formidable), LOCK_EX);
3087
3088 if ($new_form) {
3089 add_filter('frm_match_xml_form', array($this, 'filter_frm_match_xml_form'), 10, 2);
3090 }
3091 $result = FrmXMLHelper::import_xml($tmp);
3092
3093 if ($new_form) {
3094 remove_filter('frm_match_xml_form', array($this, 'filter_frm_match_xml_form'));
3095 }
3096 unlink($tmp);
3097
3098 FrmXMLHelper::parse_message($result, $message, $errors);
3099
3100 if ($errors) {
3101 return array(
3102 'errors' => $errors,
3103 );
3104 } else {
3105 if ($this->get('item') == '-2') {
3106 if ($this->get('item1') && isset($result['forms'][$this->get('item1')])) {
3107 $updated_items[$this->get('item1')] = $result['forms'][$this->get('item1')];
3108 }
3109 if ($this->get('item2') && isset($result['forms'][$this->get('item2')])) {
3110 $updated_items[$this->get('item2')] = $result['forms'][$this->get('item2')];
3111 }
3112 } elseif (isset($result['forms'][$this->get('item')])) {
3113 $updated_items[$this->get('item')] = $result['forms'][$this->get('item')];
3114 }
3115 }
3116 }
3117 }
3118 return $updated_items;
3119 }
3120
3121 public function after_import($old_template_id, $new_template_id) {
3122
3123 if ($this->get('item') && $old_template_id && $new_template_id && $old_template_id != $new_template_id && class_exists('FrmForm')) {
3124 $forms = array();
3125 if ($this->get('item') == '-2') {
3126 if ($this->get('item1')) {
3127 $forms[] = $this->get('item1');
3128 }
3129 if ($this->get('item2')) {
3130 $forms[] = $this->get('item2');
3131 }
3132 } else {
3133 $forms[] = $this->get('item');
3134 }
3135
3136 foreach ($forms as $form_id) {
3137 $form = FrmForm::getOne($form_id);
3138 if ($form) {
3139 if (isset($form->options['success_msg'])) {
3140 $success_msg = $this->replace_template_id($form->options['success_msg'], $old_template_id, $new_template_id);
3141 if ($success_msg != $form->options['success_msg']) {
3142 $update = array(
3143 'options' => array(
3144 'success_msg' => $success_msg,
3145 ),
3146 );
3147 FrmForm::update($form_id, $update);
3148 }
3149 }
3150
3151 if (class_exists('FrmFormAction')) {
3152 $actions = FrmFormAction::get_action_for_form($form_id, 'email');
3153 foreach ($actions as $action) {
3154 if (isset($action->post_content['email_message'])) {
3155 $email_message = $this->replace_template_id($action->post_content['email_message'], $old_template_id, $new_template_id);
3156 if ($email_message != $action->post_content['email_message']) {
3157 $action->post_content['email_message'] = $email_message;
3158 FrmFormAction::clear_cache();
3159 FrmDb::save_settings($action, 'frm_actions');
3160 }
3161 }
3162 }
3163 }
3164 }
3165 }
3166 }
3167 return false;
3168 }
3169
3170 public function replace_template_id($message, $old_template_id, $new_template_id) {
3171 $old_template_id = (int) $old_template_id;
3172 $new_template_id = (int) $new_template_id;
3173 $message = preg_replace('/\[(e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)(.*) id=(|\'|")' . $old_template_id . '(|\'|")(.*)\]/i', '[${1}${2} id=${3}' . $new_template_id . '${4}${5}]', $message);
3174 return $message;
3175 }
3176
3177 public function replace_meta_shortcodes($value) {
3178 if (false !== strpos($value, '[')) {
3179 $replace = array(
3180 '[referer]' => '',
3181 '[browser]' => '',
3182 '[referer2]' => '',
3183 '[browser2]' => '',
3184 '[entry_num]' => '',
3185 '[entry_num2]' => '',
3186 );
3187 if ((false !== strpos($value, '[referer]') || false !== strpos($value, '[browser]')) && $this->get('cached_entry') && isset($this->get('cached_entry')->description)) {
3188 if (is_serialized($this->get('cached_entry')->description)) {
3189 $description = $this->helper->load('convert')->unserialize(trim($this->get('cached_entry')->description));
3190 } else {
3191 $description = $this->get('cached_entry')->description;
3192 }
3193 if (isset($description['referrer'])) {
3194 $replace['[referer]'] = @preg_match('/Referer +\d+\:[ \t]+([^\n\t]+)/', $description['referrer'], $m) ? $m[1] : $description['referrer']; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
3195 }
3196 $replace['[browser]'] = isset($description['browser']) ? $description['browser'] : '';
3197 }
3198 if ((false !== strpos($value, '[referer2]') || false !== strpos($value, '[browser2]')) && $this->get('cached_entry2') && isset($this->get('cached_entry2')->description)) {
3199 if (is_serialized($this->get('cached_entry2')->description)) {
3200 $description = $this->helper->load('convert')->unserialize(trim($this->get('cached_entry2')->description));
3201 } else {
3202 $description = $this->get('cached_entry2')->description;
3203 }
3204 $replace['[referer2]'] = '';
3205 if (isset($description['referrer'])) {
3206 $replace['[referer2]'] = @preg_match('/Referer +\d+\:[ \t]+([^\n\t]+)/', $description['referrer'], $m) ? $m[1] : $description['referrer']; // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
3207 }
3208 $replace['[browser2]'] = isset($description['browser']) ? $description['browser'] : '';
3209 }
3210 if (false !== strpos($value, '[entry_num]')) {
3211 $replace['[entry_num]'] = class_exists('FrmDb') && $this->get('cached_entry') ? FrmDb::get_count('frm_items', array("form_id = '{$this->get('cached_form')->id}' AND id <= '{$this->get('cached_entry')->id}' AND 1" => '1')) : '';
3212 }
3213 if (false !== strpos($value, '[entry_num2]')) {
3214 $replace['[entry_num2]'] = class_exists('FrmDb') && $this->get('cached_entry2') ? FrmDb::get_count('frm_items', array("form_id = '{$this->get('cached_form2')->id}' AND id <= '{$this->get('cached_entry2')->id}' AND 1" => '1')) : '';
3215 }
3216 $value = str_replace(array_keys($replace), $replace, $value);
3217 }
3218 return $value;
3219 }
3220
3221 /**
3222 * Replace Old Formidable shortcodes to new values on pages
3223 * @param array $pages - List of pages to replace shortcodes
3224 * @param array $shortcodes_list - List of new/old shortcodes to replace
3225 * @return array - List of updated pages
3226 */
3227 public function pages_replace_shortcodes($pages = array(), $shortcodes_list = array()) {
3228
3229 foreach ($pages as $page_key => $page) {
3230 /* Replace page actions and conditions shortcodes */
3231 if (isset($page['actions']) && !empty($page['actions'])) {
3232 foreach ($page['actions'] as $action_key => $action_value) {
3233 if (isset($action_value['change'])) {
3234 $pages[$page_key]['actions'][$action_key]['change'] = $this->replace_shortcodes($action_value['change'], $shortcodes_list);
3235 }
3236
3237 if (isset($action_value['conditions']) && !empty($action_value['conditions'])) {
3238 foreach ($action_value['conditions'] as $condition_key => $condition_value) {
3239 if (isset($condition_value['if'])) {
3240 $pages[$page_key]['actions'][$action_key]['conditions'][$condition_key]['if'] = $this->replace_shortcodes($condition_value['if'], $shortcodes_list);
3241 }
3242
3243 if (isset($condition_value['value'])) {
3244 $pages[$page_key]['actions'][$action_key]['conditions'][$condition_key]['value'] = $this->replace_shortcodes($condition_value['value'], $shortcodes_list);
3245 }
3246 }
3247 }
3248 }
3249 }
3250
3251 foreach ($page['elements'] as $element_key => $element_value) {
3252 $pages[$page_key]['elements'][$element_key]['value'] = $this->replace_shortcodes($element_value['value'], $shortcodes_list);
3253
3254 if (isset($element_value['properties']['option'])) {
3255 $pages[$page_key]['elements'][$element_key]['properties']['option'] = $this->replace_shortcodes($element_value['properties']['option'], $shortcodes_list);
3256 }
3257
3258 if (isset($element_value['properties']['options'])) {
3259 $pages[$page_key]['elements'][$element_key]['properties']['options'] = $this->replace_shortcodes($element_value['properties']['options'], $shortcodes_list);
3260 }
3261
3262 if (isset($element_value['properties']['group'])) {
3263 $pages[$page_key]['elements'][$element_key]['properties']['group'] = $this->replace_shortcodes($element_value['properties']['group'], $shortcodes_list);
3264 }
3265
3266 /* Replace element actions and conditions shortcodes */
3267 if (isset($element_value['actions']) && !empty($element_value['actions'])) {
3268 foreach ($element_value['actions'] as $action_key => $action_value) {
3269 if (isset($action_value['change'])) {
3270 $pages[$page_key]['elements'][$element_key]['actions'][$action_key]['change'] = $this->replace_shortcodes($action_value['change'], $shortcodes_list);
3271 }
3272
3273 if (isset($action_value['conditions']) && !empty($action_value['conditions'])) {
3274 foreach ($action_value['conditions'] as $condition_key => $condition_value) {
3275 if (isset($condition_value['if'])) {
3276 $pages[$page_key]['elements'][$element_key]['actions'][$action_key]['conditions'][$condition_key]['if'] = $this->replace_shortcodes($condition_value['if'], $shortcodes_list);
3277 }
3278
3279 if (isset($condition_value['value'])) {
3280 $pages[$page_key]['elements'][$element_key]['actions'][$action_key]['conditions'][$condition_key]['value'] = $this->replace_shortcodes($condition_value['value'], $shortcodes_list);
3281 }
3282 }
3283 }
3284 }
3285 }
3286 }
3287 }
3288
3289 return $pages;
3290 }
3291
3292 public function actions_replace_shortcodes($actions = array(), $shortcodes_list = array()) {
3293
3294 foreach ($actions as $action_key => $action_value) {
3295 if (isset($action_value['change'])) {
3296 $actions[$action_key]['change'] = $this->replace_shortcodes($action_value['change'], $shortcodes_list);
3297 }
3298
3299 if (isset($action_value['conditions']) && !empty($action_value['conditions'])) {
3300 foreach ($action_value['conditions'] as $condition_key => $condition_value) {
3301 if (isset($condition_value['if'])) {
3302 $actions[$action_key]['conditions'][$condition_key]['if'] = $this->replace_shortcodes($condition_value['if'], $shortcodes_list);
3303 }
3304
3305 if (isset($condition_value['value'])) {
3306 $actions[$action_key]['conditions'][$condition_key]['value'] = $this->replace_shortcodes($condition_value['value'], $shortcodes_list);
3307 }
3308 }
3309 }
3310 }
3311
3312 return $actions;
3313 }
3314
3315 /**
3316 * Replace Old Formidable shortcodes to new values
3317 * @param string $content - Value that can contains old shortcodes
3318 * @param array $shortcodes_list - List of new/old shortcodes to replace
3319 * @return string - Updated value
3320 */
3321 public function replace_shortcodes($content = '', $shortcodes_list = array()) {
3322 if (false === strpos($content, '[')) {
3323 return $content;
3324 }
3325 foreach ($shortcodes_list as $list_key => $list_value) {
3326 $content = preg_replace("/(\[|\[(?:[^\]]*?)\s|\[(?:[^\]]*?)(?:field_id|e2pdf-frm-data-values id|e2pdf-frm-lookup-values id)=(?:\'|\"|)){$list_key}(\:(?:.*?)\]|\s(?:.*?)\]|(?:\'|\")(?:.*?)\]|\])/", '${1}' . $list_value . '${2}', $content);
3327 }
3328 return $content;
3329 }
3330
3331 /**
3332 * Verify if item and dataset exists
3333 * @return bool - item and dataset exists
3334 */
3335 public function verify() {
3336 $verify = false;
3337 if ($this->get('item') == '-2') {
3338 if ($this->get('cached_entry')) {
3339 $verify = true;
3340 } else {
3341 $this->set('dataset', false);
3342 }
3343 if ($this->get('cached_entry2')) {
3344 $verify = true;
3345 } else {
3346 $this->set('dataset2', false);
3347 }
3348 } elseif ($this->get('cached_entry')) {
3349 $verify = true;
3350 }
3351 return $verify;
3352 }
3353
3354 /**
3355 * Create Form based on uploaded PDF
3356 * @param object $template - Template Object to work with
3357 * @param array $data - Settings to create labels/shortcodes
3358 * @return object - Mapped Template Object
3359 */
3360 public function auto_form($template, $data = array()) {
3361
3362 if ($template->get('ID')) {
3363
3364 $auto_form_label = isset($data['auto_form_label']) && $data['auto_form_label'] ? $data['auto_form_label'] : false;
3365 $auto_form_shortcode = isset($data['auto_form_shortcode']) ? true : false;
3366
3367 $form = array(
3368 'form_key' => '',
3369 'name' => $template->get('title'),
3370 'description' => '',
3371 'status' => 'published',
3372 'options' => array(
3373 'success_msg' => sprintf(__('Success. [e2pdf-download id="%s"]', 'e2pdf'), $template->get('ID')),
3374 ),
3375 );
3376
3377 $item = FrmForm::create($form);
3378 if ($item) {
3379 $template->set('item', $item);
3380
3381 $checkboxes = array();
3382 $radios = array();
3383
3384 $pages = $template->get('pages');
3385
3386 foreach ($pages as $page_key => $page) {
3387 if (isset($page['elements']) && !empty($page['elements'])) {
3388 foreach ($page['elements'] as $element_key => $element) {
3389 $field_values = array();
3390 if ($element['type'] == 'e2pdf-input' || ($element['type'] == 'e2pdf-signature' && !class_exists('FrmSigAppHelper'))) {
3391 $field_values = FrmFieldsHelper::setup_new_vars('text', $item);
3392 } elseif ($element['type'] == 'e2pdf-signature' && class_exists('FrmSigAppHelper')) {
3393 $field_values = FrmFieldsHelper::setup_new_vars('signature', $item);
3394 } elseif ($element['type'] == 'e2pdf-textarea') {
3395 $field_values = FrmFieldsHelper::setup_new_vars('textarea', $item);
3396 } elseif ($element['type'] == 'e2pdf-select') {
3397 $field_values = FrmFieldsHelper::setup_new_vars('select', $item);
3398 $field_values['options'] = array();
3399 if (isset($element['properties']['options'])) {
3400 $field_options = explode("\n", $element['properties']['options']);
3401 foreach ($field_options as $option) {
3402 $field_values['options'][] = $option;
3403 }
3404 }
3405 } elseif ($element['type'] == 'e2pdf-checkbox') {
3406 $field_key = array_search($element['name'], array_column($checkboxes, 'name'));
3407 if ($field_key !== false) {
3408 $checkboxes[$field_key]['options'][] = $element['properties']['option'];
3409 $pages[$page_key]['elements'][$element_key]['value'] = '[' . $checkboxes[$field_key]['element_id'] . ']';
3410 } else {
3411 $field_values = FrmFieldsHelper::setup_new_vars('checkbox', $item);
3412 $field_values['options'] = array();
3413 }
3414 } elseif ($element['type'] == 'e2pdf-radio') {
3415 if (isset($element['properties']['group']) && $element['properties']['group']) {
3416 $element['name'] = $element['properties']['group'];
3417 } else {
3418 $element['name'] = $element['element_id'];
3419 }
3420
3421 $field_key = array_search($element['name'], array_column($radios, 'name'));
3422 if ($field_key !== false) {
3423 $radios[$field_key]['options'][] = $element['properties']['option'];
3424 $pages[$page_key]['elements'][$element_key]['value'] = '[' . $radios[$field_key]['element_id'] . ']';
3425 } else {
3426 $field_values = FrmFieldsHelper::setup_new_vars('radio', $item);
3427 $field_values['options'] = array();
3428 }
3429 }
3430
3431 $field_id = !empty($field_values) ? FrmField::create($field_values) : false;
3432 if ($field_id) {
3433 $field = FrmField::getOne($field_id);
3434 $labels = array();
3435
3436 if ($auto_form_shortcode) {
3437 $labels[] = get_option('e2pdf_formidable_use_keys', '0') ? '[' . $field->field_key . ']' : '[' . $field->id . ']';
3438 }
3439
3440 if ($auto_form_label && $auto_form_label == 'value' && isset($element['value']) && $element['value']) {
3441 $labels[] = $element['value'];
3442 } elseif ($auto_form_label && $auto_form_label == 'name' && isset($element['name']) && $element['name']) {
3443 $labels[] = $element['name'];
3444 }
3445
3446 if (!empty($labels)) {
3447 $update = array(
3448 'name' => implode(' ', $labels),
3449 );
3450 FrmField::update($field_id, $update);
3451 }
3452
3453 if ($element['type'] == 'e2pdf-textarea') {
3454 $pages[$page_key]['elements'][$element_key]['value'] = get_option('e2pdf_formidable_use_keys', '0') ? '[' . $field->field_key . ' wpautop=0]' : '[' . $field->id . ' wpautop=0]';
3455 } else {
3456 $pages[$page_key]['elements'][$element_key]['value'] = get_option('e2pdf_formidable_use_keys', '0') ? '[' . $field->field_key . ']' : '[' . $field->id . ']';
3457 }
3458
3459 if (isset($element['properties']['esig'])) {
3460 unset($pages[$page_key]['elements'][$element_key]['properties']['esig']);
3461 }
3462
3463 if ($element['type'] == 'e2pdf-checkbox') {
3464 $checkboxes[] = array(
3465 'name' => $element['name'],
3466 'element_id' => $field_id,
3467 'field_id' => $field->id,
3468 'options' => array(
3469 $element['properties']['option'],
3470 ),
3471 );
3472 } elseif ($element['type'] == 'e2pdf-radio') {
3473 $radios[] = array(
3474 'name' => $element['name'],
3475 'element_id' => $field_id,
3476 'field_id' => $field->id,
3477 'options' => array(
3478 $element['properties']['option'],
3479 ),
3480 );
3481 }
3482 }
3483 }
3484 }
3485 }
3486
3487 foreach ($checkboxes as $element) {
3488 $update = array(
3489 'options' => $element['options'],
3490 );
3491 FrmField::update($element['field_id'], $update);
3492 }
3493
3494 foreach ($radios as $element) {
3495 $update = array(
3496 'options' => $element['options'],
3497 );
3498 FrmField::update($element['field_id'], $update);
3499 }
3500
3501 $template->set('pages', $pages);
3502 }
3503 }
3504
3505 return $template;
3506 }
3507
3508 /**
3509 * Init Visual Mapper data
3510 * @return bool|string - HTML data source for Visual Mapper
3511 */
3512 public function visual_mapper() {
3513
3514 $html = '';
3515 $source = '';
3516
3517 if ($this->get('item') && class_exists('FrmformsController')) {
3518 if ($this->get('cached_form') && $this->get('cached_form')->parent_form_id) {
3519 return '<div class="e2pdf-vm-error">' . __("Child form doesn't support Visual Mapper", 'e2pdf') . '</div>';
3520 }
3521 if (class_exists('FrmProFieldsHelper')) {
3522 add_filter('frm_get_paged_fields', array($this, 'filter_remove_pagebreaks'), 9);
3523 }
3524 add_filter('frm_show_new_entry_page', array($this, 'filter_frm_show_new_entry_page'), 99);
3525 add_filter('frm_pre_display_form', array($this, 'filter_frm_pre_display_form'));
3526 $source = FrmformsController::show_form($this->get('item'), '', true, true);
3527 if (class_exists('FrmProFieldsHelper')) {
3528 remove_filter('frm_get_paged_fields', array($this, 'filter_remove_pagebreaks'), 9);
3529 }
3530 remove_filter('frm_show_new_entry_page', array($this, 'filter_frm_show_new_entry_page'), 99);
3531 remove_filter('frm_pre_display_form', array($this, 'filter_frm_pre_display_form'));
3532 if ($source) {
3533 $dom = new DOMDocument();
3534 $html = $this->helper->load('convert')->load_html($source, $dom, true);
3535 }
3536 if (!$source) {
3537 return '<div class="e2pdf-vm-error">' . __("The form source is empty or doesn't exist", 'e2pdf') . '</div>';
3538 } elseif (!$html) {
3539 return '<div class="e2pdf-vm-error">' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '</div>';
3540 } else {
3541
3542 $xml = $this->helper->load('xml');
3543 $xpath = new DomXPath($dom);
3544
3545 // remove by name
3546 $remove_by_name = array(
3547 'frm_action',
3548 'form_id',
3549 'frm_hide_fields_' . $this->get('item'),
3550 'form_key',
3551 '0',
3552 'frm_submit_entry_' . $this->get('item'),
3553 '_wp_http_referer',
3554 'item_key',
3555 'frm_state',
3556 );
3557 foreach ($remove_by_name as $key => $name) {
3558 $elements = $xpath->query('//*[@name="' . $name . '"]');
3559 foreach ($elements as $element) {
3560 $element->parentNode->removeChild($element);
3561 }
3562 }
3563
3564 // remove by class
3565 $remove_by_class = array(
3566 'frm_ajax_loading',
3567 'wp-editor-tools',
3568 'quicktags-toolbar',
3569 'frm_button_submit',
3570 'frm_range_value',
3571 'star-rating',
3572 'frm_repeat_buttons',
3573 'frm_save_draft',
3574 'frm_final_submit',
3575 'frm_verify',
3576 );
3577 foreach ($remove_by_class as $key => $class) {
3578 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
3579 foreach ($elements as $element) {
3580 $element->parentNode->removeChild($element);
3581 }
3582 }
3583
3584 // remove by tag
3585 $remove_by_tag = array(
3586 'link',
3587 'style',
3588 'script',
3589 );
3590 foreach ($remove_by_tag as $key => $tag) {
3591 $elements = $xpath->query('//' . $tag);
3592 foreach ($elements as $element) {
3593 $element->parentNode->removeChild($element);
3594 }
3595 }
3596
3597 // remove classes
3598 $remove_classes = array(
3599 'wp-editor-container',
3600 'frm_logic_form',
3601 'frm_pos_none',
3602 );
3603 foreach ($remove_classes as $key => $class) {
3604 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
3605 foreach ($elements as $element) {
3606 $xml->set_node_value($element, 'class', str_replace($class, '', $xml->get_node_value($element, 'class')));
3607 }
3608 }
3609
3610 // remove styles
3611 $remove_styles = array(
3612 'frm_toggle_container',
3613 );
3614 foreach ($remove_styles as $key => $class) {
3615 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
3616 foreach ($elements as $element) {
3617 $xml->set_node_value($element, 'style', '');
3618 }
3619 }
3620
3621 /*
3622 * Metas patterns to replace field names
3623 * @since 0.01.42
3624 */
3625 $metas_pattern = array(
3626 '/item_meta\[(?:.*?)\]\[0\]\[(.*?)\](?:\[\])?/i' => '$1:1',
3627 '/item_meta\[(.*?)\](\[\])?/i' => '$1',
3628 );
3629
3630 // replace names
3631 $metas = $xpath->query("//*[contains(@name, 'item_meta')]");
3632 foreach ($metas as $element) {
3633 $field_id = preg_replace(array_keys($metas_pattern), $metas_pattern, $xml->get_node_value($element, 'name'));
3634 if (get_option('e2pdf_formidable_use_keys', '0') && class_exists('FrmField')) {
3635 if (strpos($field_id, ':') !== false) {
3636 $repeat_data = explode(':', $field_id);
3637 if (isset($repeat_data['0'])) {
3638 $field_data = FrmField::getOne($repeat_data['0']);
3639 if ($field_data) {
3640 $field_id = $field_data->field_key . ':1';
3641 }
3642 }
3643 } else {
3644 $field_data = FrmField::getOne($field_id);
3645 if ($field_data) {
3646 $field_id = $field_data->field_key;
3647 }
3648 }
3649 }
3650 $xml->set_node_value($element, 'name', $field_id);
3651 }
3652
3653 $frm_combo_inputs_container = $xpath->query("//*[contains(@class, 'frm_combo_inputs_container')]");
3654 foreach ($frm_combo_inputs_container as $element) {
3655 $inputs = $xpath->query('.//input', $element);
3656 $selects = $xpath->query('.//select', $element);
3657
3658 foreach ($selects as $key => $sub_element) {
3659 $field_data = array();
3660 preg_match('/(.*?)\[(.*?)\]/i', $xml->get_node_value($sub_element, 'name'), $field_data);
3661 if (!empty($field_data) && isset($field_data[1]) && isset($field_data[2])) {
3662 $field_id = $field_data[1] . ' show="' . $field_data[2] . '"';
3663 $xml->set_node_value($sub_element, 'name', $field_id);
3664 }
3665 }
3666
3667 foreach ($inputs as $key => $sub_element) {
3668 $field_data = array();
3669 preg_match('/(.*?)\[(.*?)\]/i', $xml->get_node_value($sub_element, 'name'), $field_data);
3670 if (!empty($field_data) && isset($field_data[1]) && isset($field_data[2])) {
3671 $field_id = $field_data[1] . ' show="' . $field_data[2] . '"';
3672 $xml->set_node_value($sub_element, 'name', $field_id);
3673 }
3674 }
3675 }
3676
3677 $frm_dropzone = $xpath->query("//*[contains(@class, 'frm_dropzone')]/parent::*");
3678 foreach ($frm_dropzone as $element) {
3679 $dropzone = $xpath->query(".//*[contains(@class, 'frm_dropzone')]", $element)->item(0);
3680 $input = $xpath->query('.//input', $element)->item(0);
3681 if ($input && $dropzone) {
3682 $input_cloned = $input->cloneNode(true);
3683
3684 $xml->set_node_value($input_cloned, 'type', 'text');
3685 $xml->set_node_value($input_cloned, 'value', __('File Upload', 'formidable'));
3686 $xml->set_node_value($input_cloned, 'style', 'width: 100%; height: 200px; text-align: center;');
3687
3688 if (strpos($xml->get_node_value($input_cloned, 'name'), ':') !== false) {
3689 $xml->set_node_value($input_cloned, 'name', $xml->get_node_value($input_cloned, 'name') . ' size="full" show_image="0" add_link="0"');
3690 } else {
3691 $xml->set_node_value($input_cloned, 'name', $xml->get_node_value($input_cloned, 'name') . ' size="full"');
3692 }
3693
3694 $input->parentNode->replaceChild($input_cloned, $input);
3695 $dropzone->parentNode->removeChild($dropzone);
3696 }
3697 }
3698
3699 // replace signature
3700 $sigpad = $xpath->query("//*[contains(@class, 'sigPad')]");
3701 foreach ($sigpad as $element) {
3702 $input = $xpath->query('.//input', $element)->item(0);
3703 if ($input) {
3704 $input_cloned = $input->cloneNode(true);
3705
3706 $field_id = preg_replace('/\[(.*?)\]/i', '', $xml->get_node_value($input_cloned, 'name'));
3707 if (get_option('e2pdf_formidable_use_keys', '0') && class_exists('FrmField')) {
3708 $field_data = FrmField::getOne($field_id);
3709 if ($field_data) {
3710 $field_id = $field_data->field_key;
3711 }
3712 }
3713
3714 $xml->set_node_value($input_cloned, 'style', 'width: 300px; height: 100px;');
3715 $xml->set_node_value($input_cloned, 'name', $field_id);
3716 $element->parentNode->replaceChild($input_cloned, $element);
3717 }
3718 }
3719
3720 // replace names
3721 $fields = $xpath->query('//input');
3722 foreach ($fields as $element) {
3723
3724 $image_options = false;
3725 $separate_value = false;
3726
3727 if (class_exists('FrmField') && ($xml->get_node_value($element, 'type') == 'checkbox' || $xml->get_node_value($element, 'type') == 'radio')) {
3728
3729 $field_id = $xml->get_node_value($element, 'name');
3730 if (false !== strpos($xml->get_node_value($element, 'name'), ':')) {
3731 $field_id = substr($xml->get_node_value($element, 'name'), 0, strpos($xml->get_node_value($element, 'name'), ':'));
3732 }
3733 if (preg_match('/\[(other(.*?[^\]]))\]/', $field_id)) {
3734 $field_id = preg_replace('/\[(other(.*?[^\]]))\]/', '', $field_id);
3735 }
3736
3737 $field_data = FrmField::getOne($field_id);
3738
3739 if (isset($field_data->field_options['image_options']) && $field_data->field_options['image_options']) {
3740 $image_options = true;
3741 }
3742
3743 if (isset($field_data->field_options['separate_value']) && $field_data->field_options['separate_value']) {
3744 $separate_value = true;
3745 }
3746
3747 if (isset($field_data->type) && $field_data->type === 'data' && isset($field_data->field_options['form_select'])) {
3748 $dynamic_form_id = false;
3749 $dynamic_field_id = false;
3750
3751 $dynamic_field_id = $field_data->field_options['form_select'];
3752 $dynamic_field_data = FrmField::getOne($dynamic_field_id);
3753 if ($dynamic_field_data) {
3754 $dynamic_form_id = $dynamic_field_data->form_id;
3755 }
3756
3757 $options = array();
3758 if (class_exists('FrmEntry') && class_exists('FrmEntryMeta')) {
3759 $where = array(
3760 'it.form_id' => $dynamic_form_id,
3761 );
3762 $entries_tmp = FrmEntry::getAll($where, ' ORDER BY id ASC');
3763 foreach ($entries_tmp as $key => $entry) {
3764 $options[] = array(
3765 'label' => FrmEntryMeta::get_meta_value($entry, $dynamic_field_id),
3766 'value' => $key,
3767 );
3768 }
3769 }
3770 $field_data->options = $options;
3771 }
3772
3773 if (isset($field_data->options) && is_array($field_data->options)) {
3774
3775 $field_options = $field_data->options;
3776 $field_value = $xml->get_node_value($element, 'value');
3777
3778 $field_key = false;
3779 foreach ($field_options as $fv_key => $fv) {
3780 if (isset($fv['value']) && $fv['value'] == $field_value) {
3781 $field_key = $fv_key;
3782 break;
3783 }
3784 }
3785
3786 if ($field_key !== false) {
3787 if ($image_options && $separate_value) {
3788 $field_label = isset($field_options[$field_key]['value']) ? $field_options[$field_key]['value'] : $field_value;
3789 } else {
3790 $field_label = isset($field_options[$field_key]['label']) ? $field_options[$field_key]['label'] : $field_value;
3791 }
3792 $xml->set_node_value($element, 'value', $field_label);
3793 } elseif ($xml->get_node_value($element, 'type') == 'radio') {
3794 $xml->set_node_value($element, 'value', '[' . $field_id . ' show="value" key="other"]');
3795 } else {
3796 $field_key = array_search($field_value, $field_options);
3797 if ($field_key !== false) {
3798 $xml->set_node_value($element, 'value', '[' . $field_id . ' show="value" key="' . $field_key . '"]');
3799 }
3800 }
3801 }
3802 }
3803
3804 $name = false;
3805 if ($xml->get_node_value($element, 'type') == 'text') {
3806 if (preg_match('/other\[(.*?[^\]])\]\[(other(.*?[^\]]))\]/', $xml->get_node_value($element, 'name'))) {
3807 $name = preg_replace('/other\[(.*?[^\]])\]\[(other(.*?[^\]]))\]/', '$1 show="value" key="$2"', $xml->get_node_value($element, 'name'));
3808 } elseif (preg_match('/other\[(.*?[^\]])\]/', $xml->get_node_value($element, 'name'))) {
3809 $name = preg_replace('/other\[(.*?[^\]])\]/', '$1 show="value" key="other"', $xml->get_node_value($element, 'name'));
3810 }
3811 } elseif ($xml->get_node_value($element, 'type') == 'checkbox') {
3812 if (preg_match('/\[(other(.*?[^\]]))\]/', $xml->get_node_value($element, 'name'))) {
3813 $name = preg_replace('/\[(other(.*?[^\]]))\]/', '', $xml->get_node_value($element, 'name'));
3814 } else {
3815 if ($image_options) {
3816 $name = $xml->get_node_value($element, 'name') . ' show="value"';
3817 }
3818 }
3819 } elseif ($xml->get_node_value($element, 'type') == 'radio') {
3820 if ($image_options) {
3821 $name = $xml->get_node_value($element, 'name') . ' show="value"';
3822 }
3823 }
3824
3825 if (!$name) {
3826 $name = $xml->get_node_value($element, 'name');
3827 }
3828
3829 $xml->set_node_value($element, 'name', '[' . $name . ']');
3830 }
3831
3832 // replace names
3833 $textareas = $xpath->query('//textarea');
3834 foreach ($textareas as $element) {
3835 $xml->set_node_value($element, 'name', '[' . $xml->get_node_value($element, 'name') . ' wpautop=0]');
3836 }
3837
3838 // replace names
3839 $selects = $xpath->query('//select');
3840 foreach ($selects as $element) {
3841
3842 if (class_exists('FrmField')) {
3843
3844 $options = $xpath->query('.//option', $element);
3845 $field_id = $xml->get_node_value($element, 'name');
3846
3847 /* Time Field names */
3848 if (false !== strpos($xml->get_node_value($element, 'name'), ':')) {
3849 $field_id = substr($xml->get_node_value($element, 'name'), 0, strpos($xml->get_node_value($element, 'name'), ':'));
3850 }
3851 $field_id = str_replace(array('[H]', '[m]', '[A]'), '', $field_id);
3852
3853 $field_data = FrmField::getOne($field_id);
3854 if (isset($field_data->type) && $field_data->type === 'lookup') {
3855 foreach ($options as $option) {
3856 $option->parentNode->removeChild($option);
3857 }
3858 $wrapper = $dom->createElement('option');
3859 $wrapper_atts = array(
3860 'value' => '[e2pdf-frm-lookup-values id="' . $field_id . '"]',
3861 );
3862 foreach ($wrapper_atts as $key => $value) {
3863 $attr = $dom->createAttribute($key);
3864 $attr->value = $value;
3865 $wrapper->appendChild($attr);
3866 }
3867 $element->appendChild($wrapper);
3868 } elseif (isset($field_data->type) && $field_data->type === 'data') {
3869 foreach ($options as $option) {
3870 $option->parentNode->removeChild($option);
3871 }
3872 $wrapper = $dom->createElement('option');
3873 $wrapper_atts = array(
3874 'value' => '[e2pdf-frm-data-values id="' . $field_id . '"]',
3875 );
3876 foreach ($wrapper_atts as $key => $value) {
3877 $attr = $dom->createAttribute($key);
3878 $attr->value = $value;
3879 $wrapper->appendChild($attr);
3880 }
3881 $element->appendChild($wrapper);
3882 } elseif (isset($field_data->type) && $field_data->type === 'time') {
3883 $replace = array(
3884 '[m]' => ' format="i"',
3885 '[A]' => ' format="A"',
3886 );
3887 if (isset($field_data->field_options['clock']) && $field_data->field_options['clock'] == '12') {
3888 $replace['[H]'] = ' format="g"';
3889 } else {
3890 $replace['[H]'] = ' format="H"';
3891 }
3892 $xml->set_node_value($element, 'name', str_replace(array_keys($replace), $replace, $xml->get_node_value($element, 'name')));
3893 }
3894
3895 if (isset($field_data->options) && is_array($field_data->options)) {
3896 $field_options = $field_data->options;
3897 foreach ($options as $option) {
3898 $field_value = $xml->get_node_value($option, 'value');
3899 foreach ($field_options as $field_option) {
3900 if (isset($field_option['value']) && $field_option['value'] === $field_value) {
3901 $field_label = isset($field_option['label']) ? $field_option['label'] : $field_value;
3902 $xml->set_node_value($option, 'value', $field_label);
3903 }
3904 }
3905 }
3906 }
3907 }
3908 $xml->set_node_value($element, 'name', '[' . $xml->get_node_value($element, 'name') . ']');
3909 }
3910
3911 // show total formatted fields
3912 $total_formatted = $xpath->query("//*[contains(@class, 'frm_total_formatted')]/parent::*");
3913 foreach ($total_formatted as $element) {
3914 $elements = $xpath->query("//*[contains(@class, 'frm_hidden')]");
3915 foreach ($elements as $element) {
3916 $xml->set_node_value($element, 'class', str_replace('frm_hidden', '', $xml->get_node_value($element, 'class')));
3917 }
3918 }
3919
3920 if (defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD')) {
3921 return str_replace(array('<html>', '</html>'), '', $dom->saveHTML());
3922 } else {
3923 return $dom->saveHTML();
3924 }
3925 }
3926 }
3927 return false;
3928 }
3929
3930 /**
3931 * Convert Field name to Value
3932 * @since 0.01.34
3933 * @param string $name - Field name
3934 * @return bool|string - Converted value or false
3935 */
3936 public function auto_map($name = false) {
3937
3938 $item = $this->get('item');
3939
3940 if ($item && class_exists('FrmField')) {
3941 $where = array(
3942 'fi.form_id' => (int) $item,
3943 array(
3944 'or' => 1,
3945 'fi.field_key' => $name,
3946 'fi.name' => $name,
3947 ),
3948 );
3949 $field = FrmField::getAll($where, 'id ASC', '1');
3950
3951 if ($field && is_object($field)) {
3952 $field_id = get_option('e2pdf_formidable_use_keys', '0') ? $field->field_key : $field->id;
3953 if ($field->type == 'textarea' || $field->type == 'rte') {
3954 return '[' . $field_id . ' wpautop=0]';
3955 } else {
3956 return '[' . $field_id . ']';
3957 }
3958 }
3959 }
3960
3961 return false;
3962 }
3963
3964 /**
3965 * Load additional shortcodes for this extension
3966 */
3967 public function load_shortcodes() {
3968 add_shortcode('e2pdf-frm-entry-values', array($this, 'shortcode_e2pdf_frm_entry_values'));
3969 add_shortcode('e2pdf-frm-lookup-values', array($this, 'shortcode_e2pdf_frm_lookup_values'));
3970 add_shortcode('e2pdf-frm-data-values', array($this, 'shortcode_e2pdf_frm_data_values'));
3971 add_shortcode('e2pdf-frm-repeatable', array($this, 'shortcode_e2pdf_frm_repeatable'));
3972 }
3973
3974 /**
3975 * [e2pdf-frm-repeatable id="entry_id' field_id="field_id"] shortcode
3976 * @param array $atts - Atributes for shortcode
3977 * @return string - Output of shortcode
3978 */
3979 public function shortcode_e2pdf_frm_repeatable($atts = array()) {
3980 $id = (int) $atts['id'];
3981 $field_id = $atts['field_id'];
3982
3983 $response = '[frm-field-value field_id="' . $field_id . '" entry="' . $id . '"]';
3984 return $response;
3985 }
3986
3987 /**
3988 * [e2pdf-frm-entry-values id='{form_id}' field_id='{field_id}' separator=''] shortcode
3989 * @param array $atts - Atributes for shortcode
3990 * @return string - Output of shortcodes
3991 */
3992 public function shortcode_e2pdf_frm_entry_values($atts = array()) {
3993 $form_id = (int) $atts['id'];
3994 $field_id = $atts['field_id'];
3995 $separator = isset($atts['separator']) ? $atts['separator'] : "\r\n";
3996
3997 $response = '';
3998 $values = array();
3999 if ($form_id && $field_id && class_exists('FrmEntry') && class_exists('FrmEntryMeta')) {
4000
4001 $where = array(
4002 'it.form_id' => $form_id,
4003 );
4004 $entries_tmp = FrmEntry::getAll($where, ' ORDER BY id ASC');
4005 foreach ($entries_tmp as $key => $entry) {
4006 $values[] = FrmEntryMeta::get_meta_value($entry, $field_id);
4007 }
4008 }
4009 $response = implode($separator, $values);
4010 return $response;
4011 }
4012
4013 /**
4014 * [e2pdf-frm-lookup-values id='{field_id}'] shortcode
4015 * @param array $atts - Atributes for shortcode
4016 * @return string - Output of shortcode
4017 */
4018 public function shortcode_e2pdf_frm_lookup_values($atts = array()) {
4019
4020 $id = isset($atts['id']) ? $atts['id'] : false;
4021 $user_id = isset($atts['user_id']) ? $atts['user_id'] : '0';
4022 $separator = isset($atts['separator']) ? $atts['separator'] : "\r\n";
4023
4024 $response = '';
4025 $values = array();
4026
4027 if ($id && class_exists('FrmField') && class_exists('FrmAppHelper') && class_exists('FrmFieldsHelper')) {
4028 $field = FrmField::getOne($id);
4029 if ($field && $field->type === 'lookup') {
4030
4031 $frm_filter = false;
4032 if (class_exists('FrmProFieldsHelper') && method_exists('FrmProFieldsHelper', 'add_default_field_settings')) {
4033 if (!has_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings')) {
4034 add_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings', 10, 2);
4035 $frm_filter = true;
4036 }
4037 }
4038
4039 $args = array();
4040 if (is_admin()) {
4041 $original_user_id = get_current_user_id();
4042 wp_set_current_user($user_id);
4043 if (!current_user_can('administrator')) {
4044 add_filter('frm_lookup_is_current_user_filter_needed', array($this, 'filter_frm_lookup_is_current_user_filter_needed'), 10, 3);
4045 }
4046 }
4047
4048 $field_array = FrmAppHelper::start_field_array($field);
4049 FrmFieldsHelper::prepare_new_front_field($field_array, $field, $args);
4050 $field_array = array_merge($field->field_options, $field_array);
4051 if (isset($field_array['options']) && is_array($field_array['options'])) {
4052 $values = $field_array['options'];
4053 }
4054
4055 if (is_admin()) {
4056 if (!current_user_can('administrator')) {
4057 remove_filter('frm_lookup_is_current_user_filter_needed', array($this, 'filter_frm_lookup_is_current_user_filter_needed'));
4058 }
4059 wp_set_current_user($original_user_id);
4060 }
4061 if ($frm_filter) {
4062 remove_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings');
4063 }
4064 }
4065 }
4066
4067 $response = implode($separator, $values);
4068 return $response;
4069 }
4070
4071 /**
4072 * [e2pdf-frm-data-values id='{field_id}'] shortcode
4073 * @param array $atts - Atributes for shortcode
4074 * @return string - Output of shortcode
4075 */
4076 public function shortcode_e2pdf_frm_data_values($atts = array()) {
4077
4078 $id = isset($atts['id']) ? $atts['id'] : false;
4079 $user_id = isset($atts['user_id']) ? $atts['user_id'] : '0';
4080 $separator = isset($atts['separator']) ? $atts['separator'] : "\r\n";
4081
4082 $response = '';
4083 $values = array();
4084
4085 if ($id && class_exists('FrmField') && class_exists('FrmAppHelper') && class_exists('FrmFieldsHelper')) {
4086 $field = FrmField::getOne($id);
4087 if ($field && $field->type === 'data') {
4088 $frm_filter = false;
4089 if (class_exists('FrmProFieldsHelper') && method_exists('FrmProFieldsHelper', 'add_default_field_settings')) {
4090 if (!has_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings')) {
4091 add_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings', 10, 2);
4092 $frm_filter = true;
4093 }
4094 }
4095
4096 $args = array();
4097 if (is_admin()) {
4098 $original_user_id = get_current_user_id();
4099 wp_set_current_user($user_id);
4100 if (current_user_can('administrator')) {
4101 $field->field_options['restrict'] = '0';
4102 }
4103 }
4104
4105 $field_array = FrmAppHelper::start_field_array($field);
4106 FrmFieldsHelper::prepare_new_front_field($field_array, $field, $args);
4107
4108 $field_array = array_merge($field->field_options, $field_array);
4109 if (isset($field_array['options']) && is_array($field_array['options'])) {
4110 $values = $field_array['options'];
4111 }
4112
4113 if (is_admin()) {
4114 wp_set_current_user($original_user_id);
4115 }
4116 if ($frm_filter) {
4117 remove_filter('frm_default_field_options', 'FrmProFieldsHelper::add_default_field_settings');
4118 }
4119 }
4120 }
4121
4122 $response = implode($separator, $values);
4123 return $response;
4124 }
4125
4126 /**
4127 * Get styles for generating Map Field function
4128 * @return array - List of css files to load
4129 */
4130 public function styles($item_id = false) {
4131
4132 $styles = array();
4133 if (class_exists('FrmStylesHelper')) {
4134 $uploads = FrmStylesHelper::get_upload_base();
4135 $saved_css_path = '/formidable/css/formidablepro.css';
4136 if (is_readable($uploads['basedir'] . $saved_css_path)) {
4137 $url = $uploads['baseurl'] . $saved_css_path;
4138 } else {
4139 $url = admin_url('admin-ajax.php?action=frmpro_css');
4140 }
4141 $styles[] = $url;
4142 $styles[] = plugins_url('css/extension/formidable.css?v=' . time(), $this->helper->get('plugin_file_path'));
4143 }
4144 return $styles;
4145 }
4146
4147 public function merged_items() {
4148 return true;
4149 }
4150
4151 public function hook_formidable_entry_view($entry) {
4152 if (!empty($entry->form_id)) {
4153 $hooks = $this->helper->load('hooks')->get('formidable', 'hook_formidable_entry_view', $entry->form_id);
4154 if (!empty($hooks)) {
4155 echo '<h3>' . apply_filters('e2pdf_hook_section_title', __('E2Pdf Actions', 'e2pdf'), 'hook_formidable_entry_view') . '</h3>';
4156 echo '<div class="inside">';
4157 foreach ($hooks as $hook) {
4158 if ($this->helper->load('hooks')->process_hook(
4159 $hook,
4160 [
4161 'dataset' => $entry->id,
4162 ],
4163 'hook_formidable_entry_view'
4164 )
4165 ) {
4166 $action = apply_filters(
4167 'e2pdf_hook_action_button',
4168 array(
4169 'html' => '<div class="misc-pub-section"><a class="e2pdf-download-hook" target="_blank" title="%2$s" href="%1$s"><span class="dashicons dashicons-pdf"></span> %2$s</a></div>',
4170 'url' => $this->helper->get_url(
4171 array(
4172 'page' => 'e2pdf',
4173 'action' => 'export',
4174 'id' => $hook,
4175 'dataset' => $entry->id,
4176 ), 'admin.php?'
4177 ),
4178 'title' => 'PDF #' . $hook,
4179 ), 'hook_formidable_entry_view', $hook, $entry->id
4180 );
4181 if (!empty($action)) {
4182 echo sprintf(
4183 $action['html'], $action['url'], $action['title']
4184 );
4185 }
4186 }
4187 }
4188 echo '</div>';
4189 }
4190 }
4191 }
4192
4193 public function hook_formidable_entry_edit($entry) {
4194 if (!empty($entry->form_id)) {
4195 $hooks = $this->helper->load('hooks')->get('formidable', 'hook_formidable_entry_edit', $entry->form_id);
4196 if (!empty($hooks)) {
4197 echo '<h3>' . apply_filters('e2pdf_hook_section_title', __('E2Pdf Actions', 'e2pdf'), 'hook_formidable_entry_view') . '</h3>';
4198 echo '<div class="inside">';
4199 foreach ($hooks as $hook) {
4200 if ($this->helper->load('hooks')->process_hook(
4201 $hook,
4202 [
4203 'dataset' => $entry->id,
4204 ],
4205 'hook_formidable_entry_edit'
4206 )
4207 ) {
4208 $action = apply_filters(
4209 'e2pdf_hook_action_button',
4210 array(
4211 'html' => '<div class="misc-pub-section"><a class="e2pdf-download-hook" target="_blank" title="%2$s" href="%1$s"><span class="dashicons dashicons-pdf"></span> %2$s</a></div>',
4212 'url' => $this->helper->get_url(
4213 array(
4214 'page' => 'e2pdf',
4215 'action' => 'export',
4216 'id' => $hook,
4217 'dataset' => $entry->id,
4218 ), 'admin.php?'
4219 ),
4220 'title' => 'PDF #' . $hook,
4221 ), 'hook_formidable_entry_edit', $hook, $entry->id
4222 );
4223 if (!empty($action)) {
4224 echo sprintf(
4225 $action['html'], $action['url'], $action['title']
4226 );
4227 }
4228 }
4229 }
4230 echo '</div>';
4231 }
4232 }
4233 }
4234
4235 public function hook_formidable_row_actions($actions, $entry) {
4236 if (!empty($entry->form_id) && !empty($entry->id)) {
4237 $hooks = $this->helper->load('hooks')->get('formidable', 'hook_formidable_row_actions', $entry->form_id);
4238 foreach ($hooks as $hook) {
4239 if ($this->helper->load('hooks')->process_hook(
4240 $hook,
4241 [
4242 'dataset' => $entry->id,
4243 ],
4244 'hook_formidable_row_actions'
4245 )
4246 ) {
4247 $action = apply_filters(
4248 'e2pdf_hook_action_button',
4249 array(
4250 'html' => '<a class="e2pdf-download-hook" target="_blank" href="%s">%s</a>',
4251 'url' => $this->helper->get_url(
4252 array(
4253 'page' => 'e2pdf',
4254 'action' => 'export',
4255 'id' => $hook,
4256 'dataset' => $entry->id,
4257 ), 'admin.php?'
4258 ),
4259 'title' => 'PDF #' . $hook,
4260 ), 'hook_formidable_row_actions', $hook, $entry->id
4261 );
4262 if (!empty($action)) {
4263 $actions['e2pdf_' . $hook] = sprintf(
4264 $action['html'], $action['url'], $action['title']
4265 );
4266 }
4267 }
4268 }
4269 }
4270 return $actions;
4271 }
4272 }
4273