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

1,159 lines 46.5 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 $value = Caldera_Forms::do_magic_tags($value, $dataset);
290
291 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
292 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
293 if ($esig) {
294 //process e-signature
295 $value = "";
296 } else {
297 if (!$this->helper->load('image')->get_image($value)) {
298 $value = $this->strip_shortcodes($value);
299 if (
300 $value &&
301 trim($value) != "" &&
302 extension_loaded('gd') &&
303 function_exists('imagettftext')
304 ) {
305 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
306 $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
307 } else {
308 $penColour = array(0x14, 0x53, 0x94);
309 }
310
311 $default_options = array(
312 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
313 'bgColour' => 'transparent',
314 'penColour' => $penColour
315 );
316
317 $options = array();
318 $options = array_merge($default_options, $options);
319
320 $model_e2pdf_font = new Model_E2pdf_Font();
321
322 $font = false;
323 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
324 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
325 }
326 if (!$font) {
327 $font = $model_e2pdf_font->get_font_path('Noto Sans');
328 }
329
330 $size = 150;
331 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
332 $size = $field['properties']['text_font_size'];
333 }
334
335 $model_e2pdf_signature = new Model_E2pdf_Signature();
336 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
337 } else {
338 $value = "";
339 }
340 }
341 }
342 } else {
343 $value = $this->convert_shortcodes($value);
344 }
345
346 remove_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30);
347 remove_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30);
348 remove_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30);
349 }
350
351 return $value;
352 }
353
354 /**
355 * Strip unused shortcodes
356 *
357 * @param string $value - Content
358 *
359 * @return string - Value with removed unused shortcodes
360 */
361 public function strip_shortcodes($value) {
362 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
363 $value = preg_replace('~(?:%/?)[^/%]+/?%~s', "", $value);
364 return $value;
365 }
366
367 public function convert_shortcodes($value, $to = false) {
368 if ($value) {
369 if ($to) {
370 $value = str_replace("&#91;", "[", $value);
371 //$value = str_replace("&#37;", "%", $value);
372 } else {
373 $value = str_replace("[", "&#91;", $value);
374 //$value = str_replace("%", "&#37;", $value);
375 }
376 }
377 return $value;
378 }
379
380 public function verify() {
381 $item = $this->get('item');
382 $dataset = $this->get('dataset');
383
384 if ($item && $dataset && class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms_Forms') && class_exists("Caldera_Forms")) {
385 $form = Caldera_Forms_Forms::get_form($item);
386 if ($form && isset($form['_last_updated'])) {
387 $entry = new Caldera_Forms_Entry($form, $dataset);
388 if ($entry->found()) {
389 return true;
390 }
391 }
392 }
393 return false;
394 }
395
396 public function visual_mapper() {
397
398 $item = $this->get('item');
399
400 if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) {
401
402 $source = Caldera_Forms::render_form($item);
403
404 $form = Caldera_Forms_Forms::get_form($item);
405
406 libxml_use_internal_errors(true);
407 $dom = new DOMDocument();
408 if (function_exists('mb_convert_encoding')) {
409 $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
410 } else {
411 $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
412 }
413 libxml_clear_errors();
414
415 if (!$html) {
416 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
417 } else {
418
419 $xpath = new DomXPath($dom);
420
421 $remove_by_class = array(
422 'live-gravatar',
423 );
424 foreach ($remove_by_class as $key => $class) {
425 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
426 foreach ($elements as $element) {
427 $element->parentNode->removeChild($element);
428 }
429 }
430
431 $remove_by_tag = array(
432 'script'
433 );
434 foreach ($remove_by_tag as $key => $tag) {
435 $elements = $xpath->query("//{$tag}");
436 foreach ($elements as $element) {
437 $element->parentNode->removeChild($element);
438 }
439 }
440
441 // Replace names
442 $fields = $xpath->query("//*[contains(@name, 'fld_')]");
443 foreach ($fields as $element) {
444 if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox') {
445 preg_match("/(.*?)\[(.*?)\]/i", $element->attributes->getNamedItem("name")->nodeValue, $matches);
446 if (isset($matches[1])) {
447 $field_id = $matches[1];
448 }
449 } else {
450 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
451 }
452
453 // Modify Star field
454 if ($element->attributes->getNamedItem("data-type")->nodeValue == 'star') {
455 if ($element->attributes->getNamedItem("style")) {
456 $element->attributes->getNamedItem("style")->nodeValue = '';
457 }
458 }
459
460 // Modify Range field
461 if ($element->attributes->getNamedItem("type")->nodeValue == 'range') {
462
463 if ($element->parentNode->attributes->getNamedItem("style")) {
464 $element->parentNode->attributes->getNamedItem("style")->nodeValue = '';
465 }
466
467 if ($element->parentNode->attributes->getNamedItem("class")) {
468 $element->parentNode->attributes->getNamedItem("class")->nodeValue = 'col-xs-12';
469 }
470
471 $next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0);
472 $next_element->parentNode->removeChild($next_element);
473 }
474
475
476 $field = Caldera_Forms_Field_Util::get_field($field_id, $form);
477 if ($field && isset($field['slug'])) {
478 if ($element->attributes->getNamedItem("name")) {
479 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
480 }
481 }
482 }
483
484 // Convert Calculation Fields
485 $calculation_fields = $xpath->query("//input[@data-type='calculation']");
486 foreach ($calculation_fields as $calculation_field) {
487
488 if ($calculation_field->attributes->getNamedItem("type")) {
489 $calculation_field->attributes->getNamedItem("type")->nodeValue = 'text';
490 }
491
492 $calculation_text = $xpath->query(".//h3", $calculation_field->parentNode)->item(0);
493 $calculation_field->parentNode->removeChild($calculation_text);
494 }
495
496 // Modify Toggle Fields
497 $toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]");
498 foreach ($toggle_gorups as $toogle_group) {
499 $toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $toogle_group);
500 foreach ($toggle_elements as $toggle_element) {
501 $toggle_id = $toggle_element->attributes->getNamedItem("id")->nodeValue;
502
503 $radio = $toggle_elements = $xpath->query(".//input[@id='{$toggle_id}']", $toogle_group)->item(0);
504
505 if ($radio) {
506 $radio_cloned = $radio->cloneNode(true);
507
508 $attr = $dom->createAttribute('style');
509 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
510 $radio_cloned->appendChild($attr);
511
512 $toggle_element->appendChild($radio_cloned);
513 }
514 }
515 }
516
517 // Modify Advanced File Upload
518 $advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]");
519 foreach ($advanced_uploaders as $advanced_uploader) {
520 $input = $xpath->query(".//input[@type='hidden']", $advanced_uploader->parentNode)->item(0);
521
522 $attr = $dom->createAttribute('style');
523 $attr->value = 'position: relative;';
524 $advanced_uploader->appendChild($attr);
525
526 if ($input) {
527 $input_cloned = $input->cloneNode(true);
528 $attr = $dom->createAttribute('style');
529 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
530 $input_cloned->appendChild($attr);
531
532 if ($input_cloned->attributes->getNamedItem('type')) {
533 $input_cloned->attributes->getNamedItem('type')->nodeValue = 'text';
534 }
535
536 $advanced_uploader->appendChild($input_cloned);
537 }
538 }
539
540 // Remove unecessary elements
541 $submit_buttons = $xpath->query("//input[@type='submit']");
542 foreach ($submit_buttons as $submit_button) {
543 $submit_button->parentNode->removeChild($submit_button);
544 }
545 return $dom->saveHTML();
546 }
547 }
548 return false;
549 }
550
551 /**
552 * Auto Generate of Template for this extension
553 *
554 * @return array - List of elements
555 */
556 public function auto() {
557 $response = array();
558 $elements = array();
559
560 $item = $this->get('item');
561
562 $fields = array();
563
564 if (class_exists("Caldera_Forms_Forms")) {
565 $form = Caldera_Forms_Forms::get_form($item);
566 if ($form && isset($form['fields'])) {
567 $fields = $form['fields'];
568 }
569 }
570
571 if ($form && isset($form['layout_grid']['structure']) && isset($form['layout_grid']['fields'])) {
572 $struct = explode('|', $form['layout_grid']['structure']);
573 foreach ($struct as $row_num => $row) {
574
575 $float = false;
576
577 $columns = explode(':', $row);
578 foreach ($columns as $column_num => $column) {
579 $column_fields = array_keys($form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1));
580
581 $width = 100 / (12 / $column) . "%";
582
583 foreach ($column_fields as $column_field_key) {
584 if (isset($fields[$column_field_key])) {
585 $field = $fields[$column_field_key];
586
587 switch ($field['type']) {
588 case 'text':
589 case 'email':
590 case 'phone_better':
591 case 'phone_better':
592 case 'number':
593 case 'phone':
594 case 'url':
595 case 'date_picker':
596 case 'color_picker':
597 case 'credit_card_number':
598 case 'credit_card_exp':
599 case 'credit_card_cvc':
600 case 'calculation':
601 case 'range_slider':
602 case 'star_rating':
603 case 'utm':
604 case 'file':
605 case 'advanced_file':
606 $elements[] = $this->auto_field($field, array(
607 'type' => 'e2pdf-html',
608 'block' => true,
609 'float' => $float,
610 'properties' => array(
611 'top' => '20',
612 'left' => '20',
613 'right' => '20',
614 'width' => $width,
615 'height' => 'auto',
616 'value' => $field['label'],
617 )
618 ));
619
620 $elements[] = $this->auto_field($field, array(
621 'type' => 'e2pdf-input',
622 'properties' => array(
623 'top' => '5',
624 'width' => '100%',
625 'height' => 'auto',
626 'value' => "%{$field['slug']}%",
627 )
628 ));
629 break;
630
631 case 'paragraph':
632
633 $elements[] = $this->auto_field($field, array(
634 'type' => 'e2pdf-html',
635 'block' => true,
636 'float' => $float,
637 'properties' => array(
638 'top' => '20',
639 'left' => '20',
640 'right' => '20',
641 'width' => $width,
642 'height' => 'auto',
643 'value' => $field['label'],
644 )
645 ));
646
647 $elements[] = $this->auto_field($field, array(
648 'type' => 'e2pdf-textarea',
649 'properties' => array(
650 'top' => '5',
651 'width' => '100%',
652 'height' => '150',
653 'value' => "%{$field['slug']}%",
654 )
655 ));
656 break;
657
658 case 'wysiwyg':
659 case 'html':
660 case 'summary':
661 $elements[] = $this->auto_field($field, array(
662 'type' => 'e2pdf-html',
663 'block' => true,
664 'float' => $float,
665 'properties' => array(
666 'top' => '20',
667 'left' => '20',
668 'right' => '20',
669 'width' => $width,
670 'height' => 'auto',
671 'value' => $field['label'],
672 )
673 ));
674
675 $elements[] = $this->auto_field($field, array(
676 'type' => 'e2pdf-html',
677 'properties' => array(
678 'top' => '5',
679 'width' => '100%',
680 'height' => '150',
681 'value' => "%{$field['slug']}%",
682 )
683 ));
684 break;
685
686 case 'dropdown':
687 case 'filtered_select2':
688 case 'states':
689 $elements[] = $this->auto_field($field, array(
690 'type' => 'e2pdf-html',
691 'block' => true,
692 'float' => $float,
693 'properties' => array(
694 'top' => '20',
695 'left' => '20',
696 'right' => '20',
697 'width' => $width,
698 'height' => 'auto',
699 'value' => $field['label'],
700 )
701 ));
702
703 $options_tmp = array();
704
705 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
706 foreach ($field['config']['option'] as $opt_key => $option) {
707 if (is_array($option)) {
708 $options_tmp[] = $option['value'];
709 }
710 }
711 }
712
713 if ($field['type'] == 'states' && defined("CFCORE_PATH")) {
714 if (file_exists(CFCORE_PATH . "fields/states/field.php")) {
715 $contents = file_get_contents(CFCORE_PATH . "fields/states/field.php");
716 preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches);
717 if (isset($matches[1])) {
718 foreach ($matches[1] as $state) {
719 $options_tmp[] = $state;
720 }
721 }
722 }
723 }
724
725 $elements[] = $this->auto_field($field, array(
726 'type' => 'e2pdf-select',
727 'properties' => array(
728 'top' => '5',
729 'width' => '100%',
730 'height' => 'auto',
731 'options' => implode("\n", $options_tmp),
732 'value' => "%{$field['slug']}%",
733 )
734 ));
735 break;
736
737 case 'checkbox':
738 $elements[] = $this->auto_field($field, array(
739 'type' => 'e2pdf-html',
740 'block' => true,
741 'float' => $float,
742 'properties' => array(
743 'top' => '20',
744 'left' => '20',
745 'right' => '20',
746 'width' => $width,
747 'height' => 'auto',
748 'value' => $field['label'],
749 )
750 ));
751
752
753 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
754 foreach ($field['config']['option'] as $opt_key => $option) {
755 if (is_array($option)) {
756 $elements[] = $this->auto_field($field, array(
757 'type' => 'e2pdf-checkbox',
758 'properties' => array(
759 'top' => '5',
760 'width' => 'auto',
761 'height' => 'auto',
762 'value' => "%{$field['slug']}%",
763 'option' => $option['label']
764 )
765 ));
766 $elements[] = $this->auto_field($field, array(
767 'type' => 'e2pdf-html',
768 'float' => true,
769 'properties' => array(
770 'left' => '5',
771 'width' => '100%',
772 'height' => 'auto',
773 'value' => $option['label']
774 )
775 ));
776 }
777 }
778 }
779 break;
780
781 case 'radio':
782 case 'toggle_switch':
783 $elements[] = $this->auto_field($field, array(
784 'type' => 'e2pdf-html',
785 'block' => true,
786 'float' => $float,
787 'properties' => array(
788 'top' => '20',
789 'left' => '20',
790 'right' => '20',
791 'width' => $width,
792 'height' => 'auto',
793 'value' => $field['label'],
794 )
795 ));
796
797 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
798 foreach ($field['config']['option'] as $opt_key => $option) {
799 if (is_array($option)) {
800 $elements[] = $this->auto_field($field, array(
801 'type' => 'e2pdf-radio',
802 'properties' => array(
803 'top' => '5',
804 'width' => 'auto',
805 'height' => 'auto',
806 'value' => "%{$field['slug']}%",
807 'option' => $option['label'],
808 'group' => "group_" . $field['slug'],
809 )
810 ));
811 $elements[] = $this->auto_field($field, array(
812 'type' => 'e2pdf-html',
813 'float' => true,
814 'properties' => array(
815 'left' => '5',
816 'width' => '100%',
817 'height' => 'auto',
818 'value' => $option['label']
819 )
820 ));
821 }
822 }
823 }
824 break;
825 default:
826 /*
827 * Unsupported fields: section_break, live_gravatar, button
828 */
829 break;
830 }
831 }
832 }
833
834 $float = true;
835 }
836 }
837 }
838
839 $response['page'] = array(
840 'bottom' => '20',
841 'top' => '20',
842 );
843
844 $response['elements'] = $elements;
845 return $response;
846 }
847
848 public function auto_field($field = false, $element = array()) {
849
850 if (!$field) {
851 return false;
852 }
853
854 if (!isset($element['block'])) {
855 $element['block'] = false;
856 }
857
858 if (!isset($element['float'])) {
859 $element['float'] = false;
860 }
861
862 return $element;
863 }
864
865 /**
866 * Load actions for this extension
867 */
868 public function load_actions() {
869 add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3);
870 add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3);
871 }
872
873 /**
874 * Delete attachments that were sent by email
875 */
876 public function action_caldera_forms_mailer_complete() {
877
878 $files = $this->helper->get('caldera_attachments');
879
880 if (is_array($files) && !empty($files)) {
881 foreach ($files as $key => $file) {
882 $this->helper->delete_dir(dirname($file) . '/');
883 }
884 $this->helper->deset('caldera_attachments');
885 }
886 }
887
888 /**
889 * Convert file and advanced_file field type to not rendered
890 *
891 * @param array $field - Field
892 * @param array $form - Current form
893 *
894 @return array - Updated field
895 */
896 public function filter_caldera_forms_render_get_field($field, $form) {
897 if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) {
898 $field['config']['media_lib'] = false;
899 }
900 return $field;
901 }
902
903 /**
904 * Fix for {entry_id}
905 *
906 * @param array $form - Current form
907 * @param int $entry_id - Current entry_id
908 *
909 @return array - Updated form
910 */
911 public function filter_caldera_forms_magic_form($form, $entry_id) {
912 if (class_exists("Caldera_Forms") && $entry_id) {
913 Caldera_Forms::set_field_data('_entry_id', $entry_id, $form);
914 }
915 return $form;
916 }
917
918 /**
919 * Render file and advanced_file field types to url instead image and link
920 *
921 * @param string $value - Text value
922 * @param array $matches - List of matches of magic tags
923 * @param int $entry_id - Current entry
924 * @param array $form - Current form
925 *
926 * @return string - Updated value
927 */
928 public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) {
929
930 if (class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Field_Util")) {
931 if (isset($matches[1])) {
932 foreach ($matches[1] as $key => $tag) {
933
934 $part_tags = explode(':', $tag);
935 if (!empty($part_tags[1])) {
936 $tag = $part_tags[0];
937 }
938 $entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id);
939
940 $field = false;
941 if ($entry !== null) {
942 $field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form);
943 }
944
945 if (Caldera_Forms_Field_Util::is_file_field($field, $form)) {
946 if (filter_var($entry, FILTER_VALIDATE_URL)) {
947 $value = $entry;
948 }
949 continue;
950 }
951 $value = str_replace($matches[0][$key], $entry, $value);
952 }
953 }
954 }
955
956 return $value;
957 }
958
959 /**
960 * [e2pdf-view] shortcode fix
961 *
962 * @param array $out - Array with response
963 * @param array $form - Current form
964 *
965 @return array - Updated response
966 */
967 public function filter_caldera_forms_ajax_return($out, $form) {
968
969 if (isset($out['html'])) {
970
971 if (false === strpos($out['html'], '[')) {
972 return $out;
973 }
974
975 $shortcode_tags = array(
976 'e2pdf-view'
977 );
978
979 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
980
981 $tagnames = array_intersect($shortcode_tags, $matches[1]);
982
983 if (!empty($tagnames)) {
984 $pattern = get_shortcode_regex($tagnames);
985
986 preg_match_all("/$pattern/", $out['html'], $shortcodes);
987 foreach ($shortcodes[0] as $key => $shortcode_value) {
988
989 $shortcode = array();
990 $shortcode[1] = $shortcodes[1][$key];
991 $shortcode[2] = $shortcodes[2][$key];
992 $shortcode[3] = $shortcodes[3][$key];
993 $shortcode[4] = $shortcodes[4][$key];
994 $shortcode[5] = $shortcodes[5][$key];
995 $shortcode[6] = $shortcodes[6][$key];
996
997 $out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']);
998 }
999 }
1000 }
1001
1002 return $out;
1003 }
1004
1005 public function action_caldera_forms_submit_complete($form, $referrer, $process_id) {
1006 global $form, $transdata;
1007
1008 if (isset($form['success'])) {
1009
1010 if (false === strpos($form['success'], '[')) {
1011 return;
1012 }
1013
1014 $shortcode_tags = array(
1015 'e2pdf-download',
1016 'e2pdf-save',
1017 'e2pdf-view',
1018 'e2pdf-adobesign'
1019 );
1020
1021 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
1022
1023 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1024
1025 if (!empty($tagnames)) {
1026 $pattern = get_shortcode_regex($tagnames);
1027
1028 preg_match_all("/$pattern/", $form['success'], $shortcodes);
1029 foreach ($shortcodes[0] as $key => $shortcode_value) {
1030
1031 $shortcode = array();
1032 $shortcode[1] = $shortcodes[1][$key];
1033 $shortcode[2] = $shortcodes[2][$key];
1034 $shortcode[3] = $shortcodes[3][$key];
1035 $shortcode[4] = $shortcodes[4][$key];
1036 $shortcode[5] = $shortcodes[5][$key];
1037 $shortcode[6] = $shortcodes[6][$key];
1038
1039 $atts = shortcode_parse_atts($shortcode[3]);
1040
1041 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1042 $template = new Model_E2pdf_Template();
1043 $template->load($atts['id']);
1044 if ($template->get('extension') === 'caldera') {
1045 $dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false;
1046 if ($dataset_id) {
1047 $atts['dataset'] = $dataset_id;
1048 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1049 }
1050 }
1051 }
1052
1053 if ($shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1054 $shortcode[3] .= " apply=\"true\"";
1055 }
1056
1057 if ($shortcode[2] === 'e2pdf-view') {
1058 $new_shortcode = $shortcode[2] . $shortcode[3];
1059 $form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']);
1060 } else {
1061 $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
1062 }
1063 }
1064 }
1065 }
1066 }
1067
1068 /**
1069 * Load filters for this extension
1070 */
1071 public function load_filters() {
1072 add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 10, 3);
1073 add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 10, 2);
1074 }
1075
1076 public function filter_caldera_forms_mailer($mail, $data, $form) {
1077
1078 if ($mail && isset($mail['message'])) {
1079 if (false !== strpos($mail['message'], '[')) {
1080 $shortcode_tags = array(
1081 'e2pdf-download',
1082 'e2pdf-save',
1083 'e2pdf-attachment',
1084 'e2pdf-adobesign'
1085 );
1086
1087 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches);
1088 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1089
1090 if (!empty($tagnames)) {
1091 $pattern = get_shortcode_regex($tagnames);
1092
1093 preg_match_all("/$pattern/", $mail['message'], $shortcodes);
1094
1095 foreach ($shortcodes[0] as $key => $shortcode_value) {
1096 $shortcode = array();
1097 $shortcode[1] = $shortcodes[1][$key];
1098 $shortcode[2] = $shortcodes[2][$key];
1099 $shortcode[3] = $shortcodes[3][$key];
1100 $shortcode[4] = $shortcodes[4][$key];
1101 $shortcode[5] = $shortcodes[5][$key];
1102 $shortcode[6] = $shortcodes[6][$key];
1103
1104 $atts = shortcode_parse_atts($shortcode[3]);
1105
1106 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1107 $template = new Model_E2pdf_Template();
1108 $template->load($atts['id']);
1109 if ($template->get('extension') === 'caldera') {
1110 $dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false;
1111 if ($dataset_id) {
1112 $atts['dataset'] = $dataset_id;
1113 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1114 }
1115 }
1116 }
1117
1118 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1119 $shortcode[3] .= " apply=\"true\"";
1120 }
1121
1122 $shortcode[3] .= " filter=\"true\"";
1123
1124 if ($shortcode[2] === 'e2pdf-attachment') {
1125 $file = do_shortcode_tag($shortcode);
1126 if ($file) {
1127 $this->helper->add('caldera_attachments', $file);
1128 $mail['attachments'][] = $file;
1129 }
1130 $mail['message'] = str_replace($shortcode_value, '', $mail['message']);
1131 } else {
1132 $mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']);
1133 }
1134 }
1135 }
1136 }
1137 }
1138
1139 return $mail;
1140 }
1141
1142 /**
1143 * Get styles for generating Map Field function
1144 *
1145 * @return array - List of css files to load
1146 */
1147 public function styles() {
1148
1149 $styles = array();
1150 if (class_exists('Caldera_Forms_Render_Assets')) {
1151 $url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false);
1152 $styles[] = $url;
1153 }
1154
1155 return $styles;
1156 }
1157
1158 }
1159