PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.05.03
E2Pdf – Export Pdf Tool for WordPress v1.05.03
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.05.03, at classes/extension/e2pdf-caldera.php

1,426 lines 61.8 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 $template->set('pages', $pages);
617 }
618 }
619 return $template;
620 }
621
622 private function random() {
623 return round((float) rand() / (float) getrandmax() * 10000000);
624 }
625
626 public function visual_mapper() {
627
628 $item = $this->get('item');
629 $forms = array();
630 $source = '';
631 $html = '';
632
633 if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) {
634
635 $form = Caldera_Forms_Forms::get_form($item);
636 if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) {
637 foreach ($form['node'] as $node) {
638 if (isset($node['form']) && $node['form']) {
639 $source .= Caldera_Forms::render_form($node['form']);
640 $forms[] = Caldera_Forms_Forms::get_form($node['form']);
641 }
642 }
643 } else {
644 $source = Caldera_Forms::render_form($item);
645 }
646
647 if ($source) {
648 libxml_use_internal_errors(true);
649 $dom = new DOMDocument();
650 if (function_exists('mb_convert_encoding')) {
651 $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
652 } else {
653 $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
654 }
655 libxml_clear_errors();
656 }
657
658 if (!$source) {
659 return __('Form source is empty', 'e2pdf');
660 } elseif (!$html) {
661 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
662 } else {
663
664 $xpath = new DomXPath($dom);
665
666 $remove_by_class = array(
667 'live-gravatar',
668 );
669 foreach ($remove_by_class as $key => $class) {
670 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
671 foreach ($elements as $element) {
672 $element->parentNode->removeChild($element);
673 }
674 }
675
676 $remove_by_tag = array(
677 'script'
678 );
679 foreach ($remove_by_tag as $key => $tag) {
680 $elements = $xpath->query("//{$tag}");
681 foreach ($elements as $element) {
682 $element->parentNode->removeChild($element);
683 }
684 }
685
686 // Replace names
687 $fields = $xpath->query("//*[contains(@name, 'fld_')]");
688 foreach ($fields as $element) {
689 if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox') {
690 preg_match("/(.*?)\[(.*?)\]/i", $element->attributes->getNamedItem("name")->nodeValue, $matches);
691 if (isset($matches[1])) {
692 $field_id = $matches[1];
693 }
694 } else {
695 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
696 }
697
698 // Modify Star field
699 if ($element->attributes->getNamedItem("data-type")->nodeValue == 'star') {
700 if ($element->attributes->getNamedItem("style")) {
701 $element->attributes->getNamedItem("style")->nodeValue = '';
702 }
703 }
704
705 // Modify Range field
706 if ($element->attributes->getNamedItem("type")->nodeValue == 'range') {
707
708 if ($element->parentNode->attributes->getNamedItem("style")) {
709 $element->parentNode->attributes->getNamedItem("style")->nodeValue = '';
710 }
711
712 if ($element->parentNode->attributes->getNamedItem("class")) {
713 $element->parentNode->attributes->getNamedItem("class")->nodeValue = 'col-xs-12';
714 }
715
716 $next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0);
717 $next_element->parentNode->removeChild($next_element);
718 }
719
720 if (!empty($form['is_connected_form'])) {
721 foreach ($forms as $sub_form) {
722 $field = Caldera_Forms_Field_Util::get_field($field_id, $sub_form);
723 if ($field && isset($field['slug'])) {
724 if ($element->attributes->getNamedItem("name")) {
725 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
726 }
727 break;
728 }
729 }
730 } else {
731 $field = Caldera_Forms_Field_Util::get_field($field_id, $form);
732 if ($field && isset($field['slug'])) {
733 if ($element->attributes->getNamedItem("name")) {
734 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
735 }
736 }
737 }
738 }
739
740 // Convert Calculation Fields
741 $calculation_fields = $xpath->query("//input[@data-type='calculation']");
742 foreach ($calculation_fields as $calculation_field) {
743
744 if ($calculation_field->attributes->getNamedItem("type")) {
745 $calculation_field->attributes->getNamedItem("type")->nodeValue = 'text';
746 }
747
748 $calculation_text = $xpath->query(".//h3", $calculation_field->parentNode)->item(0);
749 $calculation_field->parentNode->removeChild($calculation_text);
750 }
751
752 // Modify Toggle Fields
753 $toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]");
754 foreach ($toggle_gorups as $toogle_group) {
755 $toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $toogle_group);
756 foreach ($toggle_elements as $toggle_element) {
757 $toggle_id = $toggle_element->attributes->getNamedItem("id")->nodeValue;
758
759 $radio = $toggle_elements = $xpath->query(".//input[@id='{$toggle_id}']", $toogle_group)->item(0);
760
761 if ($radio) {
762 $radio_cloned = $radio->cloneNode(true);
763
764 $attr = $dom->createAttribute('style');
765 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
766 $radio_cloned->appendChild($attr);
767
768 $toggle_element->appendChild($radio_cloned);
769 }
770 }
771 }
772
773 // Modify Advanced File Upload
774 $advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]");
775 foreach ($advanced_uploaders as $advanced_uploader) {
776 $input = $xpath->query(".//input[@type='hidden']", $advanced_uploader->parentNode)->item(0);
777
778 $attr = $dom->createAttribute('style');
779 $attr->value = 'position: relative;';
780 $advanced_uploader->appendChild($attr);
781
782 if ($input) {
783 $input_cloned = $input->cloneNode(true);
784 $attr = $dom->createAttribute('style');
785 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
786 $input_cloned->appendChild($attr);
787
788 if ($input_cloned->attributes->getNamedItem('type')) {
789 $input_cloned->attributes->getNamedItem('type')->nodeValue = 'text';
790 }
791
792 $advanced_uploader->appendChild($input_cloned);
793 }
794 }
795
796 // Remove unecessary elements
797 $submit_buttons = $xpath->query("//input[@type='submit']");
798 foreach ($submit_buttons as $submit_button) {
799 $submit_button->parentNode->removeChild($submit_button);
800 }
801 return $dom->saveHTML();
802 }
803 }
804 return false;
805 }
806
807 /**
808 * Auto Generate of Template for this extension
809 *
810 * @return array - List of elements
811 */
812 public function auto() {
813
814 $response = array();
815 $elements = array();
816 $fields = array();
817 $forms = array();
818
819 $item = $this->get('item');
820
821 if (class_exists("Caldera_Forms_Forms")) {
822 $form = Caldera_Forms_Forms::get_form($item);
823 if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) {
824 foreach ($form['node'] as $node) {
825 if (isset($node['form']) && $node['form']) {
826 $sub_form = Caldera_Forms_Forms::get_form($node['form']);
827 $forms[] = $sub_form;
828 if ($sub_form && isset($sub_form['fields']) && is_array($sub_form['fields'])) {
829 $fields = array_merge($fields, $sub_form['fields']);
830 }
831 }
832 }
833 } else {
834 $forms[] = $form;
835 $fields = $form['fields'];
836 }
837
838 foreach ($forms as $sub_form) {
839 if ($sub_form && isset($sub_form['layout_grid']['structure']) && isset($sub_form['layout_grid']['fields'])) {
840 $struct = explode('|', $sub_form['layout_grid']['structure']);
841 foreach ($struct as $row_num => $row) {
842
843 $float = false;
844 $columns = explode(':', $row);
845 foreach ($columns as $column_num => $column) {
846 $column_fields = array_keys($sub_form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1));
847
848 $width = 100 / (12 / $column) . "%";
849
850 foreach ($column_fields as $column_field_key) {
851 if (isset($fields[$column_field_key])) {
852 $field = $fields[$column_field_key];
853
854 switch ($field['type']) {
855 case 'text':
856 case 'email':
857 case 'phone_better':
858 case 'phone_better':
859 case 'number':
860 case 'phone':
861 case 'url':
862 case 'date_picker':
863 case 'color_picker':
864 case 'credit_card_number':
865 case 'credit_card_exp':
866 case 'credit_card_cvc':
867 case 'calculation':
868 case 'range_slider':
869 case 'star_rating':
870 case 'utm':
871 case 'file':
872 case 'advanced_file':
873 $elements[] = $this->auto_field($field, array(
874 'type' => 'e2pdf-html',
875 'block' => true,
876 'float' => $float,
877 'properties' => array(
878 'top' => '20',
879 'left' => '20',
880 'right' => '20',
881 'width' => $width,
882 'height' => 'auto',
883 'value' => $field['label'],
884 )
885 ));
886
887 $elements[] = $this->auto_field($field, array(
888 'type' => 'e2pdf-input',
889 'properties' => array(
890 'top' => '5',
891 'width' => '100%',
892 'height' => 'auto',
893 'value' => "%{$field['slug']}%",
894 )
895 ));
896 break;
897
898 case 'paragraph':
899 $elements[] = $this->auto_field($field, array(
900 'type' => 'e2pdf-html',
901 'block' => true,
902 'float' => $float,
903 'properties' => array(
904 'top' => '20',
905 'left' => '20',
906 'right' => '20',
907 'width' => $width,
908 'height' => 'auto',
909 'value' => $field['label'],
910 )
911 ));
912
913 $elements[] = $this->auto_field($field, array(
914 'type' => 'e2pdf-textarea',
915 'properties' => array(
916 'top' => '5',
917 'width' => '100%',
918 'height' => '150',
919 'value' => "%{$field['slug']}%",
920 )
921 ));
922 break;
923
924 case 'wysiwyg':
925 case 'html':
926 case 'summary':
927 $elements[] = $this->auto_field($field, array(
928 'type' => 'e2pdf-html',
929 'block' => true,
930 'float' => $float,
931 'properties' => array(
932 'top' => '20',
933 'left' => '20',
934 'right' => '20',
935 'width' => $width,
936 'height' => 'auto',
937 'value' => $field['label'],
938 )
939 ));
940
941 $elements[] = $this->auto_field($field, array(
942 'type' => 'e2pdf-html',
943 'properties' => array(
944 'top' => '5',
945 'width' => '100%',
946 'height' => '150',
947 'value' => "%{$field['slug']}%",
948 )
949 ));
950 break;
951
952 case 'dropdown':
953 case 'filtered_select2':
954 case 'states':
955 $elements[] = $this->auto_field($field, array(
956 'type' => 'e2pdf-html',
957 'block' => true,
958 'float' => $float,
959 'properties' => array(
960 'top' => '20',
961 'left' => '20',
962 'right' => '20',
963 'width' => $width,
964 'height' => 'auto',
965 'value' => $field['label'],
966 )
967 ));
968
969 $options_tmp = array();
970
971 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
972 foreach ($field['config']['option'] as $opt_key => $option) {
973 if (is_array($option)) {
974 $options_tmp[] = $option['value'];
975 }
976 }
977 }
978
979 if ($field['type'] == 'states' && defined("CFCORE_PATH")) {
980 if (file_exists(CFCORE_PATH . "fields/states/field.php")) {
981 $contents = file_get_contents(CFCORE_PATH . "fields/states/field.php");
982 preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches);
983 if (isset($matches[1])) {
984 foreach ($matches[1] as $state) {
985 $options_tmp[] = $state;
986 }
987 }
988 }
989 }
990
991 $elements[] = $this->auto_field($field, array(
992 'type' => 'e2pdf-select',
993 'properties' => array(
994 'top' => '5',
995 'width' => '100%',
996 'height' => 'auto',
997 'options' => implode("\n", $options_tmp),
998 'value' => "%{$field['slug']}%",
999 )
1000 ));
1001 break;
1002
1003 case 'checkbox':
1004 $elements[] = $this->auto_field($field, array(
1005 'type' => 'e2pdf-html',
1006 'block' => true,
1007 'float' => $float,
1008 'properties' => array(
1009 'top' => '20',
1010 'left' => '20',
1011 'right' => '20',
1012 'width' => $width,
1013 'height' => 'auto',
1014 'value' => $field['label'],
1015 )
1016 ));
1017
1018 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
1019 foreach ($field['config']['option'] as $opt_key => $option) {
1020 if (is_array($option)) {
1021 $elements[] = $this->auto_field($field, array(
1022 'type' => 'e2pdf-checkbox',
1023 'properties' => array(
1024 'top' => '5',
1025 'width' => 'auto',
1026 'height' => 'auto',
1027 'value' => "%{$field['slug']}%",
1028 'option' => $option['label']
1029 )
1030 ));
1031 $elements[] = $this->auto_field($field, array(
1032 'type' => 'e2pdf-html',
1033 'float' => true,
1034 'properties' => array(
1035 'left' => '5',
1036 'width' => '100%',
1037 'height' => 'auto',
1038 'value' => $option['label']
1039 )
1040 ));
1041 }
1042 }
1043 }
1044 break;
1045
1046 case 'radio':
1047 case 'toggle_switch':
1048 $elements[] = $this->auto_field($field, array(
1049 'type' => 'e2pdf-html',
1050 'block' => true,
1051 'float' => $float,
1052 'properties' => array(
1053 'top' => '20',
1054 'left' => '20',
1055 'right' => '20',
1056 'width' => $width,
1057 'height' => 'auto',
1058 'value' => $field['label'],
1059 )
1060 ));
1061
1062 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
1063 foreach ($field['config']['option'] as $opt_key => $option) {
1064 if (is_array($option)) {
1065 $elements[] = $this->auto_field($field, array(
1066 'type' => 'e2pdf-radio',
1067 'properties' => array(
1068 'top' => '5',
1069 'width' => 'auto',
1070 'height' => 'auto',
1071 'value' => "%{$field['slug']}%",
1072 'option' => $option['label'],
1073 'group' => "group_" . $field['slug'],
1074 )
1075 ));
1076 $elements[] = $this->auto_field($field, array(
1077 'type' => 'e2pdf-html',
1078 'float' => true,
1079 'properties' => array(
1080 'left' => '5',
1081 'width' => '100%',
1082 'height' => 'auto',
1083 'value' => $option['label']
1084 )
1085 ));
1086 }
1087 }
1088 }
1089 break;
1090 default:
1091 /*
1092 * Unsupported fields: section_break, live_gravatar, button
1093 */
1094 break;
1095 }
1096 }
1097 }
1098
1099 $float = true;
1100 }
1101 }
1102 }
1103 }
1104 }
1105
1106 $response['page'] = array(
1107 'bottom' => '20',
1108 'top' => '20',
1109 );
1110
1111 $response['elements'] = $elements;
1112 return $response;
1113 }
1114
1115 public function auto_field($field = false, $element = array()) {
1116
1117 if (!$field) {
1118 return false;
1119 }
1120
1121 if (!isset($element['block'])) {
1122 $element['block'] = false;
1123 }
1124
1125 if (!isset($element['float'])) {
1126 $element['float'] = false;
1127 }
1128
1129 return $element;
1130 }
1131
1132 /**
1133 * Load actions for this extension
1134 */
1135 public function load_actions() {
1136 add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3);
1137 add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3);
1138 }
1139
1140 /**
1141 * Delete attachments that were sent by email
1142 */
1143 public function action_caldera_forms_mailer_complete() {
1144
1145 $files = $this->helper->get('caldera_attachments');
1146
1147 if (is_array($files) && !empty($files)) {
1148 foreach ($files as $key => $file) {
1149 $this->helper->delete_dir(dirname($file) . '/');
1150 }
1151 $this->helper->deset('caldera_attachments');
1152 }
1153 }
1154
1155 /**
1156 * Convert file and advanced_file field type to not rendered
1157 *
1158 * @param array $field - Field
1159 * @param array $form - Current form
1160 *
1161 @return array - Updated field
1162 */
1163 public function filter_caldera_forms_render_get_field($field, $form) {
1164 if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) {
1165 $field['config']['media_lib'] = false;
1166 }
1167 return $field;
1168 }
1169
1170 /**
1171 * Fix for {entry_id}
1172 *
1173 * @param array $form - Current form
1174 * @param int $entry_id - Current entry_id
1175 *
1176 @return array - Updated form
1177 */
1178 public function filter_caldera_forms_magic_form($form, $entry_id) {
1179 if (class_exists("Caldera_Forms") && $entry_id) {
1180 Caldera_Forms::set_field_data('_entry_id', $entry_id, $form);
1181 }
1182 return $form;
1183 }
1184
1185 /**
1186 * Render file and advanced_file field types to url instead image and link
1187 *
1188 * @param string $value - Text value
1189 * @param array $matches - List of matches of magic tags
1190 * @param int $entry_id - Current entry
1191 * @param array $form - Current form
1192 *
1193 * @return string - Updated value
1194 */
1195 public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) {
1196
1197 if (class_exists('Caldera_Forms') && class_exists('Caldera_Forms_Field_Util')) {
1198 if (isset($matches[1])) {
1199 foreach ($matches[1] as $key => $tag) {
1200
1201 $part_tags = explode(':', $tag);
1202 if (!empty($part_tags[1])) {
1203 $tag = $part_tags[0];
1204 }
1205 $entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id);
1206
1207 $field = false;
1208 if ($entry !== null) {
1209 $field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form);
1210 }
1211
1212 if (Caldera_Forms_Field_Util::is_file_field($field, $form)) {
1213 if (filter_var($entry, FILTER_VALIDATE_URL)) {
1214 $value = $entry;
1215 }
1216 continue;
1217 }
1218 $value = str_replace($matches[0][$key], $entry, $value);
1219 }
1220 }
1221 }
1222
1223 return $value;
1224 }
1225
1226 /**
1227 * [e2pdf-view] shortcode fix
1228 *
1229 * @param array $out - Array with response
1230 * @param array $form - Current form
1231 *
1232 @return array - Updated response
1233 */
1234 public function filter_caldera_forms_ajax_return($out, $form) {
1235
1236 if (isset($out['html'])) {
1237
1238 if (false === strpos($out['html'], '[')) {
1239 return $out;
1240 }
1241
1242 $shortcode_tags = array(
1243 'e2pdf-view'
1244 );
1245
1246 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
1247
1248 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1249
1250 if (!empty($tagnames)) {
1251 $pattern = get_shortcode_regex($tagnames);
1252
1253 preg_match_all("/$pattern/", $out['html'], $shortcodes);
1254 foreach ($shortcodes[0] as $key => $shortcode_value) {
1255
1256 $shortcode = array();
1257 $shortcode[1] = $shortcodes[1][$key];
1258 $shortcode[2] = $shortcodes[2][$key];
1259 $shortcode[3] = $shortcodes[3][$key];
1260 $shortcode[4] = $shortcodes[4][$key];
1261 $shortcode[5] = $shortcodes[5][$key];
1262 $shortcode[6] = $shortcodes[6][$key];
1263
1264 $out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']);
1265 }
1266 }
1267 }
1268
1269 return $out;
1270 }
1271
1272 public function action_caldera_forms_submit_complete($form, $referrer, $process_id) {
1273 global $form, $transdata;
1274
1275 if (isset($form['success'])) {
1276
1277 if (false === strpos($form['success'], '[')) {
1278 return;
1279 }
1280
1281 $shortcode_tags = array(
1282 'e2pdf-download',
1283 'e2pdf-save',
1284 'e2pdf-view',
1285 'e2pdf-adobesign'
1286 );
1287
1288 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
1289
1290 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1291
1292 if (!empty($tagnames)) {
1293 $pattern = get_shortcode_regex($tagnames);
1294
1295 preg_match_all("/$pattern/", $form['success'], $shortcodes);
1296 foreach ($shortcodes[0] as $key => $shortcode_value) {
1297
1298 $shortcode = array();
1299 $shortcode[1] = $shortcodes[1][$key];
1300 $shortcode[2] = $shortcodes[2][$key];
1301 $shortcode[3] = $shortcodes[3][$key];
1302 $shortcode[4] = $shortcodes[4][$key];
1303 $shortcode[5] = $shortcodes[5][$key];
1304 $shortcode[6] = $shortcodes[6][$key];
1305
1306 $atts = shortcode_parse_atts($shortcode[3]);
1307
1308 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1309 $template = new Model_E2pdf_Template();
1310 $template->load($atts['id']);
1311 if ($template->get('extension') === 'caldera') {
1312 $dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false;
1313 if ($dataset_id) {
1314 $atts['dataset'] = $dataset_id;
1315 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1316 }
1317 }
1318 }
1319
1320 if ($shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1321 $shortcode[3] .= " apply=\"true\"";
1322 }
1323
1324 if ($shortcode[2] === 'e2pdf-view') {
1325 $new_shortcode = $shortcode[2] . $shortcode[3];
1326 $form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']);
1327 } else {
1328 $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
1329 }
1330 }
1331 }
1332 }
1333 }
1334
1335 /**
1336 * Load filters for this extension
1337 */
1338 public function load_filters() {
1339 add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 30, 3);
1340 add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 30, 2);
1341 }
1342
1343 public function filter_caldera_forms_mailer($mail, $data, $form) {
1344
1345 if ($mail && isset($mail['message'])) {
1346 if (false !== strpos($mail['message'], '[')) {
1347 $shortcode_tags = array(
1348 'e2pdf-download',
1349 'e2pdf-save',
1350 'e2pdf-attachment',
1351 'e2pdf-adobesign'
1352 );
1353
1354 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches);
1355 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1356
1357 if (!empty($tagnames)) {
1358 $pattern = get_shortcode_regex($tagnames);
1359
1360 preg_match_all("/$pattern/", $mail['message'], $shortcodes);
1361
1362 foreach ($shortcodes[0] as $key => $shortcode_value) {
1363 $shortcode = array();
1364 $shortcode[1] = $shortcodes[1][$key];
1365 $shortcode[2] = $shortcodes[2][$key];
1366 $shortcode[3] = $shortcodes[3][$key];
1367 $shortcode[4] = $shortcodes[4][$key];
1368 $shortcode[5] = $shortcodes[5][$key];
1369 $shortcode[6] = $shortcodes[6][$key];
1370
1371 $atts = shortcode_parse_atts($shortcode[3]);
1372
1373 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1374 $template = new Model_E2pdf_Template();
1375 $template->load($atts['id']);
1376 if ($template->get('extension') === 'caldera') {
1377 $dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false;
1378 if ($dataset_id) {
1379 $atts['dataset'] = $dataset_id;
1380 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1381 }
1382 }
1383 }
1384
1385 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1386 $shortcode[3] .= " apply=\"true\"";
1387 }
1388
1389 $shortcode[3] .= " filter=\"true\"";
1390
1391 if ($shortcode[2] === 'e2pdf-attachment') {
1392 $file = do_shortcode_tag($shortcode);
1393 if ($file) {
1394 $this->helper->add('caldera_attachments', $file);
1395 $mail['attachments'][] = $file;
1396 }
1397 $mail['message'] = str_replace($shortcode_value, '', $mail['message']);
1398 } else {
1399 $mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']);
1400 }
1401 }
1402 }
1403 }
1404 }
1405
1406 return $mail;
1407 }
1408
1409 /**
1410 * Get styles for generating Map Field function
1411 *
1412 * @return array - List of css files to load
1413 */
1414 public function styles() {
1415
1416 $styles = array();
1417 if (class_exists('Caldera_Forms_Render_Assets')) {
1418 $url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false);
1419 $styles[] = $url;
1420 }
1421
1422 return $styles;
1423 }
1424
1425 }
1426