PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.04.07
E2Pdf – Export Pdf Tool for WordPress v1.04.07
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-caldera.php

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

1,425 lines 61.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 Caldera Extension
5 *
6 * @copyright Copyright 2017 https://e2pdf.com
7 * @license GPL v2
8 * @version 1
9 * @link https://e2pdf.com
10 * @since 0.01.47
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Extension_E2pdf_Caldera extends Model_E2pdf_Model {
17
18 private $options;
19 private $info = array(
20 'key' => 'caldera',
21 'title' => 'Caldera Forms'
22 );
23
24 function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Get info about extension
30 *
31 * @param string $key - Key to get assigned extension info value
32 *
33 * @return array|string - Extension Key and Title or Assigned extension info value
34 */
35 public function info($key = false) {
36 if ($key && isset($this->info[$key])) {
37 return $this->info[$key];
38 } else {
39 return array(
40 $this->info['key'] => $this->info['title']
41 );
42 }
43 }
44
45 /**
46 * Check if needed plugin active
47 *
48 * @return bool - Activated/Not Activated plugin
49 */
50 public function active() {
51
52 if (!function_exists('is_plugin_active')) {
53 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
54 }
55
56 if (is_plugin_active('caldera-forms/caldera-core.php')) {
57 return true;
58 }
59 return false;
60 }
61
62 /**
63 * Set option
64 *
65 * @param string $key - Key of option
66 * @param string $value - Value of option
67 *
68 * @return bool - Status of setting option
69 */
70 public function set($key, $value) {
71 if (!isset($this->options)) {
72 $this->options = new stdClass();
73 }
74
75 $this->options->$key = $value;
76 return true;
77 }
78
79 /**
80 * Get option by key
81 *
82 * @param string $key - Key to get assigned option value
83 *
84 * @return mixed
85 */
86 public function get($key) {
87 if (isset($this->options->$key)) {
88 $value = $this->options->$key;
89 return $value;
90 } else {
91 return false;
92 }
93 }
94
95 /**
96 * Get items to work with
97 *
98 * @return array() - List of available items
99 */
100 public function items() {
101 $content = array();
102 if (class_exists("Caldera_Forms_Forms")) {
103 $forms = Caldera_Forms_Forms::get_forms(true);
104 if ($forms) {
105 foreach ($forms as $key => $value) {
106 $content[] = $this->item($value['ID']);
107 }
108 }
109 }
110 return $content;
111 }
112
113 /**
114 * Get entries for export
115 *
116 * @param int $item - Form ID
117 * @param string $name - Entries names
118 *
119 * @return array() - Entries list
120 */
121 public function datasets($item = false, $name = false) {
122
123 $datasets = array();
124
125 if (class_exists("Caldera_Forms_Admin") && $item) {
126
127 $form = Caldera_Forms_Admin::get_entries($item, 1, 9999999999);
128
129 if ($form && isset($form['entries']) && is_array($form['entries'])) {
130 foreach ($form['entries'] as $key => $dataset) {
131 $this->set('item', $item);
132 $this->set('dataset', $dataset['_entry_id']);
133
134 $dataset_title = $this->render($name);
135 if (!$dataset_title) {
136 $dataset_title = $dataset['_date'];
137 }
138 $datasets[] = array(
139 'key' => $dataset['_entry_id'],
140 'value' => $dataset_title
141 );
142 }
143 }
144 }
145
146
147 return $datasets;
148 }
149
150 /**
151 * Get dataset
152 *
153 * @param int $dataset - Dataset ID
154 *
155 * @return object - Dataset
156 */
157 public function dataset($dataset = false) {
158 $dataset = (int) $dataset;
159
160 if (!$dataset) {
161 return false;
162 }
163
164 $data = new stdClass();
165 $data->url = false;
166
167 return $data;
168 }
169
170 /**
171 * Get item
172 *
173 * @param string $item - Item ID
174 *
175 * @return object - Item
176 */
177 public function item($item = false) {
178
179 $form = new stdClass();
180
181 if (!$item && $this->get('item')) {
182 $item = $this->get('item');
183 }
184
185 $caldera_form = false;
186 if (class_exists('Caldera_Forms_Forms')) {
187 $caldera_form = Caldera_Forms_Forms::get_form($item);
188 }
189
190 if ($caldera_form) {
191 $form->id = (string) $caldera_form['ID'];
192 $form->url = $this->helper->get_url(array('page' => 'caldera-forms', 'edit' => $caldera_form['ID']));
193 $form->name = $caldera_form['name'];
194 } else {
195 $form->id = '';
196 $form->url = 'javascript:void(0);';
197 $form->name = '';
198 }
199 return $form;
200 }
201
202 /**
203 * Render value according to content
204 *
205 * @param string $value - Content
206 * @param string $type - Type of rendering value
207 * @param array $field - Field details
208 *
209 * @return string - Fully rendered value
210 */
211 public function render($value, $type = false, $field = array()) {
212
213 $value = $this->render_shortcodes($value, $type, $field);
214 $value = $this->strip_shortcodes($value);
215
216 if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
217 $option = $this->render($field['properties']['option']);
218 $options = explode(', ', $value);
219 $option_options = explode(', ', $option);
220 if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
221 return $option;
222 } else {
223 return "";
224 }
225 }
226
227 if ($type === 'value') {
228 $value = $this->convert_shortcodes($value, true);
229 }
230
231 return $value;
232 }
233
234 /**
235 * Render shortcodes which available in this extension
236 *
237 * @param string $value - Content
238 * @param string $type - Type of rendering value
239 * @param array $field - Field details
240 *
241 * @return string - Value with rendered shortcodes
242 */
243 public function render_shortcodes($value, $type = false, $field = array()) {
244
245 $dataset = $this->get('dataset');
246
247 if ($this->verify()) {
248 if (false !== strpos($value, '[')) {
249
250 $shortcode_tags = array(
251 'e2pdf-format-number',
252 'e2pdf-format-date',
253 'e2pdf-format-output',
254 );
255 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
256 $tagnames = array_intersect($shortcode_tags, $matches[1]);
257
258 if (!empty($tagnames)) {
259
260 $pattern = get_shortcode_regex($tagnames);
261 preg_match_all("/$pattern/", $value, $shortcodes);
262 foreach ($shortcodes[0] as $key => $shortcode_value) {
263 $shortcode = array();
264 $shortcode[1] = $shortcodes[1][$key];
265 $shortcode[2] = $shortcodes[2][$key];
266 $shortcode[3] = $shortcodes[3][$key];
267 $shortcode[4] = $shortcodes[4][$key];
268 $shortcode[5] = $shortcodes[5][$key];
269 $shortcode[6] = $shortcodes[6][$key];
270
271 if (isset($shortcode['5'])) {
272 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
273 $sub_value = $this->render($shortcode['5'], $type);
274 } else {
275 $sub_value = $this->render($shortcode['5'], $type, $field);
276 }
277 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
278 }
279 }
280 }
281 }
282
283 $value = do_shortcode($value);
284
285 add_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30, 2);
286 add_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30, 2);
287 add_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30, 4);
288
289 global $form;
290 $tmp_form = false;
291 if ($form && isset($form['ID'])) {
292 $tmp_form = $form;
293 if ($form['ID'] != $this->get('item')) {
294 $form = Caldera_Forms_Forms::get_form($this->get('item'));
295 }
296 if (!empty($form['is_connected_form'])) {
297 $form['processors']['_connected_form'] = array(
298 'type' => 'form-connector',
299 'runtimes' => array(
300 'insert' => true
301 ),
302 'config' => array()
303 );
304
305 // setup fields
306 $form_fields = array();
307 // get meta
308 $meta = Caldera_Forms::get_entry_meta((int) $dataset, $form);
309
310 if (!empty($meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'])) {
311 foreach ((array) $meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'] as $form_meta) {
312 $form_fields = array_merge($form_fields, $form_meta);
313 }
314 }
315
316 if (!empty($form['condition_points']['forms'])) {
317 $form['fields'] = array();
318 foreach ($form['condition_points']['forms'] as $connected_id => $connected_form) {
319 if (!empty($form_fields[$connected_id]) && !empty($connected_form['fields'])) {
320 $form['fields'] = array_merge($form['fields'], $connected_form['fields']);
321 }
322 }
323
324 if (function_exists('cf_form_connector_setup_processors_meta')) {
325 add_filter('caldera_forms_get_entry_detail', 'cf_form_connector_setup_processors_meta', 10, 3);
326 }
327 }
328 }
329 }
330
331 $value = Caldera_Forms::do_magic_tags($value, $dataset);
332
333 if ($tmp_form) {
334 $form = $tmp_form;
335 }
336
337 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
338 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
339 if ($esig) {
340 //process e-signature
341 $value = "";
342 } else {
343 if (!$this->helper->load('image')->get_image($value)) {
344 $value = $this->strip_shortcodes($value);
345 if (
346 $value &&
347 trim($value) != "" &&
348 extension_loaded('gd') &&
349 function_exists('imagettftext')
350 ) {
351 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
352 $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
353 } else {
354 $penColour = array(0x14, 0x53, 0x94);
355 }
356
357 $default_options = array(
358 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
359 'bgColour' => 'transparent',
360 'penColour' => $penColour
361 );
362
363 $options = array();
364 $options = array_merge($default_options, $options);
365
366 $model_e2pdf_font = new Model_E2pdf_Font();
367
368 $font = false;
369 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
370 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
371 }
372 if (!$font) {
373 $font = $model_e2pdf_font->get_font_path('Noto Sans');
374 }
375
376 $size = 150;
377 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
378 $size = $field['properties']['text_font_size'];
379 }
380
381 $model_e2pdf_signature = new Model_E2pdf_Signature();
382 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
383 } else {
384 $value = "";
385 }
386 }
387 }
388 } else {
389 $value = $this->convert_shortcodes($value);
390 }
391
392 remove_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30);
393 remove_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30);
394 remove_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30);
395 }
396
397 return $value;
398 }
399
400 /**
401 * Strip unused shortcodes
402 *
403 * @param string $value - Content
404 *
405 * @return string - Value with removed unused shortcodes
406 */
407 public function strip_shortcodes($value) {
408 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
409 $value = preg_replace('~(?:%/?)[^/%]+/?%~s', "", $value);
410 return $value;
411 }
412
413 public function convert_shortcodes($value, $to = false) {
414 if ($value) {
415 if ($to) {
416 $value = str_replace("&#91;", "[", $value);
417 //$value = str_replace("&#37;", "%", $value);
418 } else {
419 $value = str_replace("[", "&#91;", $value);
420 //$value = str_replace("%", "&#37;", $value);
421 }
422 }
423 return $value;
424 }
425
426 public function verify() {
427 $item = $this->get('item');
428 $dataset = $this->get('dataset');
429
430 if ($item && $dataset && class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms_Forms') && class_exists("Caldera_Forms")) {
431 $form = Caldera_Forms_Forms::get_form($item);
432 if ($form && isset($form['_last_updated'])) {
433 $entry = new Caldera_Forms_Entry($form, $dataset);
434 if ($entry->found()) {
435 return true;
436 }
437 }
438 }
439 return false;
440 }
441
442 public function add_form($template) {
443 if ($template->get('ID')) {
444
445 $form = array(
446 'success' => sprintf(__('Success. [e2pdf-download id="%s"]', 'e2pdf'), $template->get('ID')),
447 'name' => $template->get('title'),
448 'fields' => array(),
449 'layout_grid' => array(
450 'fields' => array(),
451 'strucutre' => ''
452 )
453 );
454
455 $pages = $template->get('pages');
456 $checkboxes = array();
457 $radios = array();
458
459 foreach ($pages as $page_key => $page) {
460 if (isset($page['elements']) && !empty($page['elements'])) {
461
462 foreach ($page['elements'] as $element_key => $element) {
463 if ($element['type'] == 'e2pdf-input' || $element['type'] == 'e2pdf-signature') {
464
465 $field_id = 'fld_' . $this->random();
466 $form['fields'][$field_id] = array(
467 'ID' => $field_id,
468 'slug' => 'element_' . $element['element_id'],
469 'type' => 'text',
470 'label' => $element['value'] ? '%element_' . $element['element_id'] . '% ' . $element['value'] : '%element_' . $element['element_id'] . '%',
471 );
472 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%';
473 if (isset($element['properties']['esig'])) {
474 unset($pages[$page_key]['elements'][$element_key]['properties']['esig']);
475 }
476 } elseif ($element['type'] == 'e2pdf-textarea') {
477
478 $field_id = 'fld_' . $this->random();
479 $form['fields'][$field_id] = array(
480 'ID' => $field_id,
481 'slug' => 'element_' . $element['element_id'],
482 'type' => 'paragraph',
483 'label' => $element['value'] ? '%element_' . $element['element_id'] . '% ' . $element['value'] : '%element_' . $element['element_id'] . '%',
484 );
485 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%';
486 } elseif ($element['type'] == 'e2pdf-select') {
487
488 $options = array();
489 $field_options = array();
490
491 if (isset($element['properties']['options'])) {
492 $field_options = explode("\n", $element['properties']['options']);
493 foreach ($field_options as $option) {
494 $option_id = 'opt' . $this->random();
495 $options[$option_id] = array(
496 'calc_value' => '',
497 'label' => $option,
498 'value' => $option
499 );
500 }
501 }
502
503 $field_id = 'fld_' . $this->random();
504 $form['fields'][$field_id] = array(
505 'ID' => $field_id,
506 'slug' => 'element_' . $element['element_id'],
507 'type' => 'dropdown',
508 'label' => '%element_' . $element['element_id'] . '%',
509 'config' => array(
510 'option' => $options
511 )
512 );
513
514 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%';
515 } elseif ($element['type'] == 'e2pdf-checkbox') {
516 $field_key = array_search($element['name'], array_column($checkboxes, 'name'));
517 if ($field_key !== false) {
518 $option_id = 'opt' . $this->random();
519 $checkboxes[$field_key]['options'][$option_id] = array(
520 'calc_value' => $element['properties']['option'],
521 'label' => $element['properties']['option'],
522 'value' => $element['properties']['option'],
523 );
524 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $checkboxes[$field_key]['element_id'] . '%';
525 } else {
526
527 $option_id = 'opt' . $this->random();
528
529 $checkboxes[] = array(
530 'name' => $element['name'],
531 'element_id' => $element['element_id'],
532 'options' => array(
533 $option_id => array(
534 'calc_value' => $element['properties']['option'],
535 'label' => $element['properties']['option'],
536 'value' => $element['properties']['option'],
537 ),
538 )
539 );
540
541 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%';
542 }
543 } elseif ($element['type'] == 'e2pdf-radio') {
544 $field_key = array_search($element['name'], array_column($radios, 'name'));
545 if ($field_key !== false) {
546 $option_id = 'opt' . $this->random();
547 $radios[$field_key]['options'][$option_id] = array(
548 'calc_value' => $element['properties']['option'],
549 'label' => $element['properties']['option'],
550 'value' => $element['properties']['option'],
551 );
552 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $radios[$field_key]['element_id'] . '%';
553 } else {
554
555 $option_id = 'opt' . $this->random();
556
557 $radios[] = array(
558 'name' => $element['name'],
559 'element_id' => $element['element_id'],
560 'options' => array(
561 $option_id => array(
562 'calc_value' => $element['properties']['option'],
563 'label' => $element['properties']['option'],
564 'value' => $element['properties']['option'],
565 ),
566 )
567 );
568
569 $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%';
570 }
571 }
572 }
573 }
574 }
575
576 foreach ($checkboxes as $element) {
577 $field_id = 'fld_' . $this->random();
578 $form['fields'][$field_id] = array(
579 'ID' => $field_id,
580 'slug' => 'element_' . $element['element_id'],
581 'type' => 'checkbox',
582 'label' => '%element_' . $element['element_id'] . '%',
583 'config' => array(
584 'option' => $element['options']
585 )
586 );
587 }
588
589 foreach ($radios as $element) {
590 $field_id = 'fld_' . $this->random();
591 $form['fields'][$field_id] = array(
592 'ID' => $field_id,
593 'slug' => 'element_' . $element['element_id'],
594 'type' => 'radio',
595 'label' => '%element_' . $element['element_id'] . '%',
596 'config' => array(
597 'option' => $element['options']
598 )
599 );
600 }
601
602 $fields = array_keys($form['fields']);
603 $x = 1;
604 foreach ($fields as $field) {
605 $form['layout_grid']['fields'][$field] = $x . ':1';
606 if ($x == 1) {
607 $form['layout_grid']['structure'] .= '12';
608 } else {
609 $form['layout_grid']['structure'] .= '|12';
610 }
611 $x++;
612 }
613
614 if ($item = Caldera_Forms_Forms::create_form($form)) {
615 $template->set('item', $item['ID']);
616 }
617 }
618 return $template;
619 }
620
621 private function random() {
622 return round((float) rand() / (float) getrandmax() * 10000000);
623 }
624
625 public function visual_mapper() {
626
627 $item = $this->get('item');
628 $forms = array();
629 $source = '';
630 $html = '';
631
632 if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) {
633
634 $form = Caldera_Forms_Forms::get_form($item);
635 if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) {
636 foreach ($form['node'] as $node) {
637 if (isset($node['form']) && $node['form']) {
638 $source .= Caldera_Forms::render_form($node['form']);
639 $forms[] = Caldera_Forms_Forms::get_form($node['form']);
640 }
641 }
642 } else {
643 $source = Caldera_Forms::render_form($item);
644 }
645
646 if ($source) {
647 libxml_use_internal_errors(true);
648 $dom = new DOMDocument();
649 if (function_exists('mb_convert_encoding')) {
650 $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
651 } else {
652 $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
653 }
654 libxml_clear_errors();
655 }
656
657 if (!$source) {
658 return __('Form source is empty', 'e2pdf');
659 } elseif (!$html) {
660 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
661 } else {
662
663 $xpath = new DomXPath($dom);
664
665 $remove_by_class = array(
666 'live-gravatar',
667 );
668 foreach ($remove_by_class as $key => $class) {
669 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
670 foreach ($elements as $element) {
671 $element->parentNode->removeChild($element);
672 }
673 }
674
675 $remove_by_tag = array(
676 'script'
677 );
678 foreach ($remove_by_tag as $key => $tag) {
679 $elements = $xpath->query("//{$tag}");
680 foreach ($elements as $element) {
681 $element->parentNode->removeChild($element);
682 }
683 }
684
685 // Replace names
686 $fields = $xpath->query("//*[contains(@name, 'fld_')]");
687 foreach ($fields as $element) {
688 if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox') {
689 preg_match("/(.*?)\[(.*?)\]/i", $element->attributes->getNamedItem("name")->nodeValue, $matches);
690 if (isset($matches[1])) {
691 $field_id = $matches[1];
692 }
693 } else {
694 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
695 }
696
697 // Modify Star field
698 if ($element->attributes->getNamedItem("data-type")->nodeValue == 'star') {
699 if ($element->attributes->getNamedItem("style")) {
700 $element->attributes->getNamedItem("style")->nodeValue = '';
701 }
702 }
703
704 // Modify Range field
705 if ($element->attributes->getNamedItem("type")->nodeValue == 'range') {
706
707 if ($element->parentNode->attributes->getNamedItem("style")) {
708 $element->parentNode->attributes->getNamedItem("style")->nodeValue = '';
709 }
710
711 if ($element->parentNode->attributes->getNamedItem("class")) {
712 $element->parentNode->attributes->getNamedItem("class")->nodeValue = 'col-xs-12';
713 }
714
715 $next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0);
716 $next_element->parentNode->removeChild($next_element);
717 }
718
719 if (!empty($form['is_connected_form'])) {
720 foreach ($forms as $sub_form) {
721 $field = Caldera_Forms_Field_Util::get_field($field_id, $sub_form);
722 if ($field && isset($field['slug'])) {
723 if ($element->attributes->getNamedItem("name")) {
724 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
725 }
726 break;
727 }
728 }
729 } else {
730 $field = Caldera_Forms_Field_Util::get_field($field_id, $form);
731 if ($field && isset($field['slug'])) {
732 if ($element->attributes->getNamedItem("name")) {
733 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
734 }
735 }
736 }
737 }
738
739 // Convert Calculation Fields
740 $calculation_fields = $xpath->query("//input[@data-type='calculation']");
741 foreach ($calculation_fields as $calculation_field) {
742
743 if ($calculation_field->attributes->getNamedItem("type")) {
744 $calculation_field->attributes->getNamedItem("type")->nodeValue = 'text';
745 }
746
747 $calculation_text = $xpath->query(".//h3", $calculation_field->parentNode)->item(0);
748 $calculation_field->parentNode->removeChild($calculation_text);
749 }
750
751 // Modify Toggle Fields
752 $toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]");
753 foreach ($toggle_gorups as $toogle_group) {
754 $toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $toogle_group);
755 foreach ($toggle_elements as $toggle_element) {
756 $toggle_id = $toggle_element->attributes->getNamedItem("id")->nodeValue;
757
758 $radio = $toggle_elements = $xpath->query(".//input[@id='{$toggle_id}']", $toogle_group)->item(0);
759
760 if ($radio) {
761 $radio_cloned = $radio->cloneNode(true);
762
763 $attr = $dom->createAttribute('style');
764 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
765 $radio_cloned->appendChild($attr);
766
767 $toggle_element->appendChild($radio_cloned);
768 }
769 }
770 }
771
772 // Modify Advanced File Upload
773 $advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]");
774 foreach ($advanced_uploaders as $advanced_uploader) {
775 $input = $xpath->query(".//input[@type='hidden']", $advanced_uploader->parentNode)->item(0);
776
777 $attr = $dom->createAttribute('style');
778 $attr->value = 'position: relative;';
779 $advanced_uploader->appendChild($attr);
780
781 if ($input) {
782 $input_cloned = $input->cloneNode(true);
783 $attr = $dom->createAttribute('style');
784 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
785 $input_cloned->appendChild($attr);
786
787 if ($input_cloned->attributes->getNamedItem('type')) {
788 $input_cloned->attributes->getNamedItem('type')->nodeValue = 'text';
789 }
790
791 $advanced_uploader->appendChild($input_cloned);
792 }
793 }
794
795 // Remove unecessary elements
796 $submit_buttons = $xpath->query("//input[@type='submit']");
797 foreach ($submit_buttons as $submit_button) {
798 $submit_button->parentNode->removeChild($submit_button);
799 }
800 return $dom->saveHTML();
801 }
802 }
803 return false;
804 }
805
806 /**
807 * Auto Generate of Template for this extension
808 *
809 * @return array - List of elements
810 */
811 public function auto() {
812
813 $response = array();
814 $elements = array();
815 $fields = array();
816 $forms = array();
817
818 $item = $this->get('item');
819
820 if (class_exists("Caldera_Forms_Forms")) {
821 $form = Caldera_Forms_Forms::get_form($item);
822 if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) {
823 foreach ($form['node'] as $node) {
824 if (isset($node['form']) && $node['form']) {
825 $sub_form = Caldera_Forms_Forms::get_form($node['form']);
826 $forms[] = $sub_form;
827 if ($sub_form && isset($sub_form['fields']) && is_array($sub_form['fields'])) {
828 $fields = array_merge($fields, $sub_form['fields']);
829 }
830 }
831 }
832 } else {
833 $forms[] = $form;
834 $fields = $form['fields'];
835 }
836
837 foreach ($forms as $sub_form) {
838 if ($sub_form && isset($sub_form['layout_grid']['structure']) && isset($sub_form['layout_grid']['fields'])) {
839 $struct = explode('|', $sub_form['layout_grid']['structure']);
840 foreach ($struct as $row_num => $row) {
841
842 $float = false;
843 $columns = explode(':', $row);
844 foreach ($columns as $column_num => $column) {
845 $column_fields = array_keys($sub_form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1));
846
847 $width = 100 / (12 / $column) . "%";
848
849 foreach ($column_fields as $column_field_key) {
850 if (isset($fields[$column_field_key])) {
851 $field = $fields[$column_field_key];
852
853 switch ($field['type']) {
854 case 'text':
855 case 'email':
856 case 'phone_better':
857 case 'phone_better':
858 case 'number':
859 case 'phone':
860 case 'url':
861 case 'date_picker':
862 case 'color_picker':
863 case 'credit_card_number':
864 case 'credit_card_exp':
865 case 'credit_card_cvc':
866 case 'calculation':
867 case 'range_slider':
868 case 'star_rating':
869 case 'utm':
870 case 'file':
871 case 'advanced_file':
872 $elements[] = $this->auto_field($field, array(
873 'type' => 'e2pdf-html',
874 'block' => true,
875 'float' => $float,
876 'properties' => array(
877 'top' => '20',
878 'left' => '20',
879 'right' => '20',
880 'width' => $width,
881 'height' => 'auto',
882 'value' => $field['label'],
883 )
884 ));
885
886 $elements[] = $this->auto_field($field, array(
887 'type' => 'e2pdf-input',
888 'properties' => array(
889 'top' => '5',
890 'width' => '100%',
891 'height' => 'auto',
892 'value' => "%{$field['slug']}%",
893 )
894 ));
895 break;
896
897 case 'paragraph':
898 $elements[] = $this->auto_field($field, array(
899 'type' => 'e2pdf-html',
900 'block' => true,
901 'float' => $float,
902 'properties' => array(
903 'top' => '20',
904 'left' => '20',
905 'right' => '20',
906 'width' => $width,
907 'height' => 'auto',
908 'value' => $field['label'],
909 )
910 ));
911
912 $elements[] = $this->auto_field($field, array(
913 'type' => 'e2pdf-textarea',
914 'properties' => array(
915 'top' => '5',
916 'width' => '100%',
917 'height' => '150',
918 'value' => "%{$field['slug']}%",
919 )
920 ));
921 break;
922
923 case 'wysiwyg':
924 case 'html':
925 case 'summary':
926 $elements[] = $this->auto_field($field, array(
927 'type' => 'e2pdf-html',
928 'block' => true,
929 'float' => $float,
930 'properties' => array(
931 'top' => '20',
932 'left' => '20',
933 'right' => '20',
934 'width' => $width,
935 'height' => 'auto',
936 'value' => $field['label'],
937 )
938 ));
939
940 $elements[] = $this->auto_field($field, array(
941 'type' => 'e2pdf-html',
942 'properties' => array(
943 'top' => '5',
944 'width' => '100%',
945 'height' => '150',
946 'value' => "%{$field['slug']}%",
947 )
948 ));
949 break;
950
951 case 'dropdown':
952 case 'filtered_select2':
953 case 'states':
954 $elements[] = $this->auto_field($field, array(
955 'type' => 'e2pdf-html',
956 'block' => true,
957 'float' => $float,
958 'properties' => array(
959 'top' => '20',
960 'left' => '20',
961 'right' => '20',
962 'width' => $width,
963 'height' => 'auto',
964 'value' => $field['label'],
965 )
966 ));
967
968 $options_tmp = array();
969
970 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
971 foreach ($field['config']['option'] as $opt_key => $option) {
972 if (is_array($option)) {
973 $options_tmp[] = $option['value'];
974 }
975 }
976 }
977
978 if ($field['type'] == 'states' && defined("CFCORE_PATH")) {
979 if (file_exists(CFCORE_PATH . "fields/states/field.php")) {
980 $contents = file_get_contents(CFCORE_PATH . "fields/states/field.php");
981 preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches);
982 if (isset($matches[1])) {
983 foreach ($matches[1] as $state) {
984 $options_tmp[] = $state;
985 }
986 }
987 }
988 }
989
990 $elements[] = $this->auto_field($field, array(
991 'type' => 'e2pdf-select',
992 'properties' => array(
993 'top' => '5',
994 'width' => '100%',
995 'height' => 'auto',
996 'options' => implode("\n", $options_tmp),
997 'value' => "%{$field['slug']}%",
998 )
999 ));
1000 break;
1001
1002 case 'checkbox':
1003 $elements[] = $this->auto_field($field, array(
1004 'type' => 'e2pdf-html',
1005 'block' => true,
1006 'float' => $float,
1007 'properties' => array(
1008 'top' => '20',
1009 'left' => '20',
1010 'right' => '20',
1011 'width' => $width,
1012 'height' => 'auto',
1013 'value' => $field['label'],
1014 )
1015 ));
1016
1017 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
1018 foreach ($field['config']['option'] as $opt_key => $option) {
1019 if (is_array($option)) {
1020 $elements[] = $this->auto_field($field, array(
1021 'type' => 'e2pdf-checkbox',
1022 'properties' => array(
1023 'top' => '5',
1024 'width' => 'auto',
1025 'height' => 'auto',
1026 'value' => "%{$field['slug']}%",
1027 'option' => $option['label']
1028 )
1029 ));
1030 $elements[] = $this->auto_field($field, array(
1031 'type' => 'e2pdf-html',
1032 'float' => true,
1033 'properties' => array(
1034 'left' => '5',
1035 'width' => '100%',
1036 'height' => 'auto',
1037 'value' => $option['label']
1038 )
1039 ));
1040 }
1041 }
1042 }
1043 break;
1044
1045 case 'radio':
1046 case 'toggle_switch':
1047 $elements[] = $this->auto_field($field, array(
1048 'type' => 'e2pdf-html',
1049 'block' => true,
1050 'float' => $float,
1051 'properties' => array(
1052 'top' => '20',
1053 'left' => '20',
1054 'right' => '20',
1055 'width' => $width,
1056 'height' => 'auto',
1057 'value' => $field['label'],
1058 )
1059 ));
1060
1061 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
1062 foreach ($field['config']['option'] as $opt_key => $option) {
1063 if (is_array($option)) {
1064 $elements[] = $this->auto_field($field, array(
1065 'type' => 'e2pdf-radio',
1066 'properties' => array(
1067 'top' => '5',
1068 'width' => 'auto',
1069 'height' => 'auto',
1070 'value' => "%{$field['slug']}%",
1071 'option' => $option['label'],
1072 'group' => "group_" . $field['slug'],
1073 )
1074 ));
1075 $elements[] = $this->auto_field($field, array(
1076 'type' => 'e2pdf-html',
1077 'float' => true,
1078 'properties' => array(
1079 'left' => '5',
1080 'width' => '100%',
1081 'height' => 'auto',
1082 'value' => $option['label']
1083 )
1084 ));
1085 }
1086 }
1087 }
1088 break;
1089 default:
1090 /*
1091 * Unsupported fields: section_break, live_gravatar, button
1092 */
1093 break;
1094 }
1095 }
1096 }
1097
1098 $float = true;
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1105 $response['page'] = array(
1106 'bottom' => '20',
1107 'top' => '20',
1108 );
1109
1110 $response['elements'] = $elements;
1111 return $response;
1112 }
1113
1114 public function auto_field($field = false, $element = array()) {
1115
1116 if (!$field) {
1117 return false;
1118 }
1119
1120 if (!isset($element['block'])) {
1121 $element['block'] = false;
1122 }
1123
1124 if (!isset($element['float'])) {
1125 $element['float'] = false;
1126 }
1127
1128 return $element;
1129 }
1130
1131 /**
1132 * Load actions for this extension
1133 */
1134 public function load_actions() {
1135 add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3);
1136 add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3);
1137 }
1138
1139 /**
1140 * Delete attachments that were sent by email
1141 */
1142 public function action_caldera_forms_mailer_complete() {
1143
1144 $files = $this->helper->get('caldera_attachments');
1145
1146 if (is_array($files) && !empty($files)) {
1147 foreach ($files as $key => $file) {
1148 $this->helper->delete_dir(dirname($file) . '/');
1149 }
1150 $this->helper->deset('caldera_attachments');
1151 }
1152 }
1153
1154 /**
1155 * Convert file and advanced_file field type to not rendered
1156 *
1157 * @param array $field - Field
1158 * @param array $form - Current form
1159 *
1160 @return array - Updated field
1161 */
1162 public function filter_caldera_forms_render_get_field($field, $form) {
1163 if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) {
1164 $field['config']['media_lib'] = false;
1165 }
1166 return $field;
1167 }
1168
1169 /**
1170 * Fix for {entry_id}
1171 *
1172 * @param array $form - Current form
1173 * @param int $entry_id - Current entry_id
1174 *
1175 @return array - Updated form
1176 */
1177 public function filter_caldera_forms_magic_form($form, $entry_id) {
1178 if (class_exists("Caldera_Forms") && $entry_id) {
1179 Caldera_Forms::set_field_data('_entry_id', $entry_id, $form);
1180 }
1181 return $form;
1182 }
1183
1184 /**
1185 * Render file and advanced_file field types to url instead image and link
1186 *
1187 * @param string $value - Text value
1188 * @param array $matches - List of matches of magic tags
1189 * @param int $entry_id - Current entry
1190 * @param array $form - Current form
1191 *
1192 * @return string - Updated value
1193 */
1194 public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) {
1195
1196 if (class_exists('Caldera_Forms') && class_exists('Caldera_Forms_Field_Util')) {
1197 if (isset($matches[1])) {
1198 foreach ($matches[1] as $key => $tag) {
1199
1200 $part_tags = explode(':', $tag);
1201 if (!empty($part_tags[1])) {
1202 $tag = $part_tags[0];
1203 }
1204 $entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id);
1205
1206 $field = false;
1207 if ($entry !== null) {
1208 $field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form);
1209 }
1210
1211 if (Caldera_Forms_Field_Util::is_file_field($field, $form)) {
1212 if (filter_var($entry, FILTER_VALIDATE_URL)) {
1213 $value = $entry;
1214 }
1215 continue;
1216 }
1217 $value = str_replace($matches[0][$key], $entry, $value);
1218 }
1219 }
1220 }
1221
1222 return $value;
1223 }
1224
1225 /**
1226 * [e2pdf-view] shortcode fix
1227 *
1228 * @param array $out - Array with response
1229 * @param array $form - Current form
1230 *
1231 @return array - Updated response
1232 */
1233 public function filter_caldera_forms_ajax_return($out, $form) {
1234
1235 if (isset($out['html'])) {
1236
1237 if (false === strpos($out['html'], '[')) {
1238 return $out;
1239 }
1240
1241 $shortcode_tags = array(
1242 'e2pdf-view'
1243 );
1244
1245 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
1246
1247 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1248
1249 if (!empty($tagnames)) {
1250 $pattern = get_shortcode_regex($tagnames);
1251
1252 preg_match_all("/$pattern/", $out['html'], $shortcodes);
1253 foreach ($shortcodes[0] as $key => $shortcode_value) {
1254
1255 $shortcode = array();
1256 $shortcode[1] = $shortcodes[1][$key];
1257 $shortcode[2] = $shortcodes[2][$key];
1258 $shortcode[3] = $shortcodes[3][$key];
1259 $shortcode[4] = $shortcodes[4][$key];
1260 $shortcode[5] = $shortcodes[5][$key];
1261 $shortcode[6] = $shortcodes[6][$key];
1262
1263 $out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']);
1264 }
1265 }
1266 }
1267
1268 return $out;
1269 }
1270
1271 public function action_caldera_forms_submit_complete($form, $referrer, $process_id) {
1272 global $form, $transdata;
1273
1274 if (isset($form['success'])) {
1275
1276 if (false === strpos($form['success'], '[')) {
1277 return;
1278 }
1279
1280 $shortcode_tags = array(
1281 'e2pdf-download',
1282 'e2pdf-save',
1283 'e2pdf-view',
1284 'e2pdf-adobesign'
1285 );
1286
1287 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
1288
1289 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1290
1291 if (!empty($tagnames)) {
1292 $pattern = get_shortcode_regex($tagnames);
1293
1294 preg_match_all("/$pattern/", $form['success'], $shortcodes);
1295 foreach ($shortcodes[0] as $key => $shortcode_value) {
1296
1297 $shortcode = array();
1298 $shortcode[1] = $shortcodes[1][$key];
1299 $shortcode[2] = $shortcodes[2][$key];
1300 $shortcode[3] = $shortcodes[3][$key];
1301 $shortcode[4] = $shortcodes[4][$key];
1302 $shortcode[5] = $shortcodes[5][$key];
1303 $shortcode[6] = $shortcodes[6][$key];
1304
1305 $atts = shortcode_parse_atts($shortcode[3]);
1306
1307 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1308 $template = new Model_E2pdf_Template();
1309 $template->load($atts['id']);
1310 if ($template->get('extension') === 'caldera') {
1311 $dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false;
1312 if ($dataset_id) {
1313 $atts['dataset'] = $dataset_id;
1314 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1315 }
1316 }
1317 }
1318
1319 if ($shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1320 $shortcode[3] .= " apply=\"true\"";
1321 }
1322
1323 if ($shortcode[2] === 'e2pdf-view') {
1324 $new_shortcode = $shortcode[2] . $shortcode[3];
1325 $form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']);
1326 } else {
1327 $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
1328 }
1329 }
1330 }
1331 }
1332 }
1333
1334 /**
1335 * Load filters for this extension
1336 */
1337 public function load_filters() {
1338 add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 30, 3);
1339 add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 30, 2);
1340 }
1341
1342 public function filter_caldera_forms_mailer($mail, $data, $form) {
1343
1344 if ($mail && isset($mail['message'])) {
1345 if (false !== strpos($mail['message'], '[')) {
1346 $shortcode_tags = array(
1347 'e2pdf-download',
1348 'e2pdf-save',
1349 'e2pdf-attachment',
1350 'e2pdf-adobesign'
1351 );
1352
1353 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches);
1354 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1355
1356 if (!empty($tagnames)) {
1357 $pattern = get_shortcode_regex($tagnames);
1358
1359 preg_match_all("/$pattern/", $mail['message'], $shortcodes);
1360
1361 foreach ($shortcodes[0] as $key => $shortcode_value) {
1362 $shortcode = array();
1363 $shortcode[1] = $shortcodes[1][$key];
1364 $shortcode[2] = $shortcodes[2][$key];
1365 $shortcode[3] = $shortcodes[3][$key];
1366 $shortcode[4] = $shortcodes[4][$key];
1367 $shortcode[5] = $shortcodes[5][$key];
1368 $shortcode[6] = $shortcodes[6][$key];
1369
1370 $atts = shortcode_parse_atts($shortcode[3]);
1371
1372 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1373 $template = new Model_E2pdf_Template();
1374 $template->load($atts['id']);
1375 if ($template->get('extension') === 'caldera') {
1376 $dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false;
1377 if ($dataset_id) {
1378 $atts['dataset'] = $dataset_id;
1379 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1380 }
1381 }
1382 }
1383
1384 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1385 $shortcode[3] .= " apply=\"true\"";
1386 }
1387
1388 $shortcode[3] .= " filter=\"true\"";
1389
1390 if ($shortcode[2] === 'e2pdf-attachment') {
1391 $file = do_shortcode_tag($shortcode);
1392 if ($file) {
1393 $this->helper->add('caldera_attachments', $file);
1394 $mail['attachments'][] = $file;
1395 }
1396 $mail['message'] = str_replace($shortcode_value, '', $mail['message']);
1397 } else {
1398 $mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']);
1399 }
1400 }
1401 }
1402 }
1403 }
1404
1405 return $mail;
1406 }
1407
1408 /**
1409 * Get styles for generating Map Field function
1410 *
1411 * @return array - List of css files to load
1412 */
1413 public function styles() {
1414
1415 $styles = array();
1416 if (class_exists('Caldera_Forms_Render_Assets')) {
1417 $url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false);
1418 $styles[] = $url;
1419 }
1420
1421 return $styles;
1422 }
1423
1424 }
1425