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

1,105 lines 43.3 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
20 function __construct() {
21 parent::__construct();
22 }
23
24 /**
25 * Get info about extension
26 *
27 * @return array() - Extension key and name
28 */
29 public function info() {
30 return array(
31 'caldera' => __('Caldera', 'e2pdf')
32 );
33 }
34
35 /**
36 * Check if export item function available
37 *
38 * @return bool - Item export available/not available
39 */
40 public function export() {
41 return false;
42 }
43
44 /**
45 * Check if needed plugin active
46 *
47 * @return bool - Activated/Not Activated plugin
48 */
49 public function active() {
50
51 if (!function_exists('is_plugin_active')) {
52 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
53 }
54
55 if (is_plugin_active('caldera-forms/caldera-core.php')) {
56 return true;
57 }
58 return false;
59 }
60
61 /**
62 * Set option
63 *
64 * @param string $key - Key of option
65 * @param string $value - Value of option
66 *
67 * @return bool - Status of setting option
68 */
69 public function set($key, $value) {
70 if (!isset($this->options)) {
71 $this->options = new stdClass();
72 }
73
74 $this->options->$key = $value;
75 return true;
76 }
77
78 /**
79 * Get option by key
80 *
81 * @param string $key - Key to get assigned option value
82 *
83 * @return mixed
84 */
85 public function get($key) {
86 if (isset($this->options->$key)) {
87 $value = $this->options->$key;
88 return $value;
89 } else {
90 return false;
91 }
92 }
93
94 /**
95 * Get items to work with
96 *
97 * @return array() - List of available items
98 */
99 public function get_items() {
100 $content = array();
101 if (class_exists("Caldera_Forms_Forms")) {
102 $forms = Caldera_Forms_Forms::get_forms(true);
103 if ($forms) {
104 foreach ($forms as $key => $value) {
105 $content[] = array(
106 'key' => $value['ID'],
107 'value' => $value['name']
108 );
109 }
110 }
111 }
112 return $content;
113 }
114
115 /**
116 * Get entries for export
117 *
118 * @param int $item - Form ID
119 * @param string $name - Entries names
120 *
121 * @return array() - Entries list
122 */
123 public function get_datasets($item = false, $name = false) {
124
125 $datasets = array();
126
127 if (class_exists("Caldera_Forms_Admin") && $item) {
128
129 $form = Caldera_Forms_Admin::get_entries($item, 1, 9999999999);
130
131 if ($form && isset($form['entries']) && is_array($form['entries'])) {
132 foreach ($form['entries'] as $key => $dataset) {
133 $this->set('item', $item);
134 $this->set('dataset', $dataset['_entry_id']);
135
136 $dataset_title = $this->render($name);
137 if (!$dataset_title) {
138 $dataset_title = $dataset['_date'];
139 }
140 $datasets[] = array(
141 'key' => $dataset['_entry_id'],
142 'value' => $dataset_title
143 );
144 }
145 }
146 }
147
148
149 return $datasets;
150 }
151
152 /**
153 * Get dataset
154 *
155 * @param int $dataset - Dataset ID
156 *
157 * @return object - Dataset
158 */
159 public function get_dataset($dataset = false) {
160 $dataset = (int) $dataset;
161
162 if (!$dataset) {
163 return false;
164 }
165
166 $data = new stdClass();
167 $data->url = false;
168
169 return $data;
170 }
171
172 /**
173 * Get item
174 *
175 * @param string $item - Item ID
176 *
177 * @return object - Item
178 */
179 public function get_item($item = false) {
180
181 $form = new stdClass();
182
183 $caldera_form = false;
184 if (class_exists("Caldera_Forms_Forms")) {
185 $caldera_form = Caldera_Forms_Forms::get_form($item);
186 }
187
188 if ($caldera_form) {
189 $form->url = $this->helper->get_url(array('page' => 'caldera-forms', 'edit' => $caldera_form['ID']));
190 $form->name = $caldera_form['name'];
191 } else {
192 $form->url = 'javascript:void(0);';
193 $form->name = '';
194 }
195 return $form;
196 }
197
198 /**
199 * Render value according to content
200 *
201 * @param string $value - Content
202 * @param string $type - Type of rendering value
203 * @param array $field - Field details
204 *
205 * @return string - Fully rendered value
206 */
207 public function render($value, $type = false, $field = array()) {
208
209 $value = $this->render_shortcodes($value, $type, $field);
210 $value = $this->strip_shortcodes($value);
211
212 if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
213 $option = $this->render($field['properties']['option']);
214 $options = explode(', ', $value);
215 $option_options = explode(', ', $option);
216 if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
217 return $option;
218 } else {
219 return "";
220 }
221 }
222
223 if ($type === 'value') {
224 $value = str_replace("&#91;", "[", $value);
225 }
226
227 return $value;
228 }
229
230 /**
231 * Render shortcodes which available in this extension
232 *
233 * @param string $value - Content
234 * @param string $type - Type of rendering value
235 * @param array $field - Field details
236 *
237 * @return string - Value with rendered shortcodes
238 */
239 public function render_shortcodes($value, $type = false, $field = array()) {
240
241 $dataset = $this->get('dataset');
242
243 if ($this->verify()) {
244 if (false !== strpos($value, '[')) {
245
246 $shortcode_tags = array(
247 'e2pdf-format-number',
248 'e2pdf-format-date',
249 'e2pdf-format-output',
250 );
251 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
252 $tagnames = array_intersect($shortcode_tags, $matches[1]);
253
254 if (!empty($tagnames)) {
255
256 $pattern = get_shortcode_regex($tagnames);
257 preg_match_all("/$pattern/", $value, $shortcodes);
258 foreach ($shortcodes[0] as $key => $shortcode_value) {
259 $shortcode = array();
260 $shortcode[1] = $shortcodes[1][$key];
261 $shortcode[2] = $shortcodes[2][$key];
262 $shortcode[3] = $shortcodes[3][$key];
263 $shortcode[4] = $shortcodes[4][$key];
264 $shortcode[5] = $shortcodes[5][$key];
265 $shortcode[6] = $shortcodes[6][$key];
266
267 if (isset($shortcode['5'])) {
268 $sub_value = $this->strip_shortcodes(Caldera_Forms::do_magic_tags($shortcode['5'], $dataset));
269 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
270 }
271 }
272 }
273 }
274
275 $value = do_shortcode($value);
276
277 add_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30, 2);
278 add_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30, 2);
279 add_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30, 4);
280
281 $value = Caldera_Forms::do_magic_tags($value, $dataset);
282
283 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
284 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
285 if ($esig) {
286 //process e-signature
287 $value = "";
288 } else {
289 if (!$this->helper->load('image')->get_image($value)) {
290 $value = $this->strip_shortcodes($value);
291 if (
292 $value &&
293 trim($value) != "" &&
294 extension_loaded('gd') &&
295 function_exists('imagettftext')
296 ) {
297 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
298 $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
299 } else {
300 $penColour = array(0x14, 0x53, 0x94);
301 }
302
303 $default_options = array(
304 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
305 'bgColour' => 'transparent',
306 'penColour' => $penColour
307 );
308
309 $options = array();
310 $options = array_merge($default_options, $options);
311
312 $model_e2pdf_font = new Model_E2pdf_Font();
313
314 $font = false;
315 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
316 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
317 }
318 if (!$font) {
319 $font = $model_e2pdf_font->get_font_path('Noto Sans');
320 }
321
322 $size = 150;
323 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
324 $size = $field['properties']['text_font_size'];
325 }
326
327 $model_e2pdf_signature = new Model_E2pdf_Signature();
328 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
329 } else {
330 $value = "";
331 }
332 }
333 }
334 } else {
335 $value = str_replace("[", '&#91;', $value);
336 }
337
338 remove_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30);
339 remove_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30);
340 remove_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30);
341 }
342
343 return $value;
344 }
345
346 /**
347 * Strip unused shortcodes
348 *
349 * @param string $value - Content
350 *
351 * @return string - Value with removed unused shortcodes
352 */
353 public function strip_shortcodes($value) {
354 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
355 $value = preg_replace('~(?:%/?)[^/%]+/?%~s', "", $value);
356 return $value;
357 }
358
359 public function verify() {
360 $item = $this->get('item');
361 $dataset = $this->get('dataset');
362
363 if ($item && $dataset && class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms_Forms') && class_exists("Caldera_Forms")) {
364 $form = Caldera_Forms_Forms::get_form($item);
365 if ($form && isset($form['_last_updated'])) {
366 $entry = new Caldera_Forms_Entry($form, $dataset);
367 if ($entry->found()) {
368 return true;
369 }
370 }
371 }
372 return false;
373 }
374
375 public function visual_mapper() {
376
377 $item = $this->get('item');
378
379 if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) {
380
381 $source = Caldera_Forms::render_form($item);
382
383 $form = Caldera_Forms_Forms::get_form($item);
384
385 libxml_use_internal_errors(true);
386 $dom = new DOMDocument;
387 $html = $dom->loadHTML($source);
388 libxml_clear_errors();
389
390 if (!$html) {
391 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
392 } else {
393
394 $xpath = new DomXPath($dom);
395
396
397 $remove_by_class = array(
398 'live-gravatar',
399 );
400 foreach ($remove_by_class as $key => $class) {
401 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
402 foreach ($elements as $element) {
403 $element->parentNode->removeChild($element);
404 }
405 }
406
407 $remove_by_tag = array(
408 'script'
409 );
410 foreach ($remove_by_tag as $key => $tag) {
411 $elements = $xpath->query("//{$tag}");
412 foreach ($elements as $element) {
413 $element->parentNode->removeChild($element);
414 }
415 }
416
417
418 // Replace names
419 $fields = $xpath->query("//*[contains(@name, 'fld_')]");
420 foreach ($fields as $element) {
421 if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox') {
422 preg_match("/(.*?)\[(.*?)\]/i", $element->attributes->getNamedItem("name")->nodeValue, $matches);
423 if (isset($matches[1])) {
424 $field_id = $matches[1];
425 }
426 } else {
427 $field_id = $element->attributes->getNamedItem("name")->nodeValue;
428 }
429
430
431 // Modify Star field
432 if ($element->attributes->getNamedItem("data-type")->nodeValue == 'star') {
433 $element->attributes->getNamedItem("style")->nodeValue = '';
434 }
435
436 // Modify Range field
437 if ($element->attributes->getNamedItem("type")->nodeValue == 'range') {
438 $element->parentNode->attributes->getNamedItem("style")->nodeValue = '';
439 $element->parentNode->attributes->getNamedItem("class")->nodeValue = 'col-xs-12';
440
441 $next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0);
442 $next_element->parentNode->removeChild($next_element);
443 }
444
445
446 $field = Caldera_Forms_Field_Util::get_field($field_id, $form);
447 if ($field && isset($field['slug'])) {
448 $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
449 }
450 }
451
452 // Convert Calculation Fields
453 $calculation_fields = $xpath->query("//input[@data-type='calculation']");
454 foreach ($calculation_fields as $calculation_field) {
455 $calculation_field->attributes->getNamedItem("type")->nodeValue = 'text';
456
457 $calculation_text = $xpath->query(".//h3", $calculation_field->parentNode)->item(0);
458 $calculation_field->parentNode->removeChild($calculation_text);
459 }
460
461
462 // Modify Toggle Fields
463 $toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]");
464 foreach ($toggle_gorups as $toogle_group) {
465 $toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $toogle_group);
466 foreach ($toggle_elements as $toggle_element) {
467 $toggle_id = $toggle_element->attributes->getNamedItem("id")->nodeValue;
468
469 $radio = $toggle_elements = $xpath->query(".//input[@id='{$toggle_id}']", $toogle_group)->item(0);
470
471 if ($radio) {
472 $radio_cloned = $radio->cloneNode(true);
473
474 $attr = $dom->createAttribute('style');
475 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
476 $radio_cloned->appendChild($attr);
477
478 $toggle_element->appendChild($radio_cloned);
479 }
480 }
481 }
482
483 // Modify Advanced File Upload
484 $advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]");
485 foreach ($advanced_uploaders as $advanced_uploader) {
486 $input = $xpath->query(".//input[@type='hidden']", $advanced_uploader->parentNode)->item(0);
487
488
489 $attr = $dom->createAttribute('style');
490 $attr->value = 'position: relative;';
491 $advanced_uploader->appendChild($attr);
492
493
494 if ($input) {
495 $input_cloned = $input->cloneNode(true);
496 $attr = $dom->createAttribute('style');
497 $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
498 $input_cloned->appendChild($attr);
499
500 $input_cloned->attributes->getNamedItem('type')->nodeValue = 'text';
501
502 $advanced_uploader->appendChild($input_cloned);
503 }
504 }
505
506 // Remove unecessary elements
507 $submit_buttons = $xpath->query("//input[@type='submit']");
508 foreach ($submit_buttons as $submit_button) {
509 $submit_button->parentNode->removeChild($submit_button);
510 }
511 return $dom->saveHTML();
512 }
513 }
514 return false;
515 }
516
517 /**
518 * Auto Generate of Template for this extension
519 *
520 * @return array - List of elements
521 */
522 public function auto() {
523 $response = array();
524 $elements = array();
525
526 $item = $this->get('item');
527
528 $fields = array();
529
530 if (class_exists("Caldera_Forms_Forms")) {
531 $form = Caldera_Forms_Forms::get_form($item);
532 if ($form && isset($form['fields'])) {
533 $fields = $form['fields'];
534 }
535 }
536
537 if ($form && isset($form['layout_grid']['structure']) && isset($form['layout_grid']['fields'])) {
538 $struct = explode('|', $form['layout_grid']['structure']);
539 foreach ($struct as $row_num => $row) {
540
541 $float = false;
542
543 $columns = explode(':', $row);
544 foreach ($columns as $column_num => $column) {
545 $column_fields = array_keys($form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1));
546
547 $width = 100 / (12 / $column) . "%";
548
549 foreach ($column_fields as $column_field_key) {
550 if (isset($fields[$column_field_key])) {
551 $field = $fields[$column_field_key];
552
553 switch ($field['type']) {
554 case 'text':
555 case 'email':
556 case 'phone_better':
557 case 'phone_better':
558 case 'number':
559 case 'phone':
560 case 'url':
561 case 'date_picker':
562 case 'color_picker':
563 case 'credit_card_number':
564 case 'credit_card_exp':
565 case 'credit_card_cvc':
566 case 'calculation':
567 case 'range_slider':
568 case 'star_rating':
569 case 'utm':
570 case 'file':
571 case 'advanced_file':
572 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
573 'top' => '20',
574 'left' => '20',
575 'right' => '20',
576 'block' => true,
577 'float' => $float,
578 'width' => $width,
579 'properties' => array(
580 'value' => $field['label'],
581 )
582 ));
583
584 $elements[] = $this->auto_field($field, 'e2pdf-input', array(
585 'properties' => array(
586 'value' => "%{$field['slug']}%",
587 )
588 ));
589 break;
590
591
592 case 'paragraph':
593
594 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
595 'top' => '20',
596 'left' => '20',
597 'right' => '20',
598 'block' => true,
599 'float' => $float,
600 'width' => $width,
601 'properties' => array(
602 'value' => $field['label'],
603 )
604 ));
605
606 $elements[] = $this->auto_field($field, 'e2pdf-textarea', array(
607 'properties' => array(
608 'value' => "%{$field['slug']}%",
609 )
610 ));
611 break;
612
613
614 case 'wysiwyg':
615 case 'html':
616 case 'summary':
617 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
618 'top' => '20',
619 'left' => '20',
620 'right' => '20',
621 'block' => true,
622 'float' => $float,
623 'width' => $width,
624 'properties' => array(
625 'value' => $field['label'],
626 )
627 ));
628
629 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
630 'properties' => array(
631 'height' => '100',
632 'value' => "%{$field['slug']}%",
633 )
634 ));
635 break;
636
637
638 case 'dropdown':
639 case 'filtered_select2':
640 case 'states':
641 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
642 'top' => '20',
643 'left' => '20',
644 'right' => '20',
645 'block' => true,
646 'float' => $float,
647 'width' => $width,
648 'properties' => array(
649 'value' => $field['label'],
650 )
651 ));
652
653 $options_tmp = array();
654
655 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
656 foreach ($field['config']['option'] as $opt_key => $option) {
657 if (is_array($option)) {
658 $options_tmp[] = $option['value'];
659 }
660 }
661 }
662
663 if ($field['type'] == 'states' && defined("CFCORE_PATH")) {
664 if (file_exists(CFCORE_PATH . "fields/states/field.php")) {
665 $contents = file_get_contents(CFCORE_PATH . "fields/states/field.php");
666 preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches);
667 if (isset($matches[1])) {
668 foreach ($matches[1] as $state) {
669 $options_tmp[] = $state;
670 }
671 }
672 }
673 }
674
675 $elements[] = $this->auto_field($field, 'e2pdf-select', array(
676 'properties' => array(
677 'options' => implode("\n", $options_tmp),
678 'value' => "%{$field['slug']}%",
679 )
680 ));
681 break;
682
683
684 case 'checkbox':
685 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
686 'top' => '20',
687 'left' => '20',
688 'right' => '20',
689 'block' => true,
690 'float' => $float,
691 'width' => $width,
692 'properties' => array(
693 'value' => $field['label'],
694 )
695 ));
696
697 $top = 0;
698 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
699 foreach ($field['config']['option'] as $opt_key => $option) {
700 if (is_array($option)) {
701 $elements[] = $this->auto_field($field, 'e2pdf-checkbox', array(
702 'top' => $top,
703 'properties' => array(
704 'width' => '15',
705 'height' => '15',
706 'value' => "%{$field['slug']}%",
707 'option' => $option['label']
708 )
709 ));
710 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
711 'top' => $top,
712 'left' => '20',
713 'properties' => array(
714 'value' => $option['label']
715 )
716 ));
717 }
718 $top = $top + 20;
719 }
720 }
721 break;
722
723
724 case 'radio':
725 case 'toggle_switch':
726 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
727 'top' => '20',
728 'left' => '20',
729 'right' => '20',
730 'block' => true,
731 'float' => $float,
732 'width' => $width,
733 'properties' => array(
734 'value' => $field['label'],
735 )
736 ));
737
738 $top = 0;
739 if (isset($field['config']['option']) && is_array($field['config']['option'])) {
740 foreach ($field['config']['option'] as $opt_key => $option) {
741 if (is_array($option)) {
742 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
743 'top' => $top,
744 'properties' => array(
745 'width' => '15',
746 'height' => '15',
747 'value' => "%{$field['slug']}%",
748 'option' => $option['label'],
749 'group' => "group_" . $field['slug'],
750 )
751 ));
752 $elements[] = $this->auto_field($field, 'e2pdf-html', array(
753 'top' => $top,
754 'left' => '20',
755 'properties' => array(
756 'value' => $option['label']
757 )
758 ));
759 }
760 $top = $top + 20;
761 }
762 }
763 break;
764 default:
765 /*
766 * Unsupported fields: section_break, live_gravatar, button
767 */
768 break;
769 }
770 }
771 }
772
773 $float = true;
774 }
775 }
776 }
777
778 $response['page'] = array(
779 'bottom' => '20',
780 'top' => '20',
781 );
782
783 $response['elements'] = $elements;
784 return $response;
785 }
786
787 public function auto_field($field = false, $type = false, $options = array()) {
788
789 if (!$field || !$type) {
790 return false;
791 }
792
793 $element = array(
794 'type' => $type,
795 'properties' => isset($options['properties']) ? $options['properties'] : array(),
796 );
797
798 $element['top'] = isset($options['top']) ? $options['top'] : '0';
799 $element['left'] = isset($options['left']) ? $options['left'] : '0';
800 $element['right'] = isset($options['right']) ? $options['right'] : '0';
801 $element['block'] = isset($options['block']) ? $options['block'] : false;
802 $element['float'] = isset($options['float']) ? $options['float'] : false;
803
804 if (isset($options['width']) && $element['block']) {
805 $element['width'] = $options['width'];
806 }
807 return $element;
808 }
809
810 /**
811 * Load actions for this extension
812 */
813 public function load_actions() {
814 add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3);
815 add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3);
816 }
817
818 /**
819 * Delete attachments that were sent by email
820 */
821 public function action_caldera_forms_mailer_complete() {
822
823 $files = $this->helper->get('caldera_attachments');
824
825 if (is_array($files) && !empty($files)) {
826 foreach ($files as $key => $file) {
827 $this->helper->delete_dir(dirname($file) . '/');
828 }
829 $this->helper->deset('caldera_attachments');
830 }
831 }
832
833 /**
834 * Convert file and advanced_file field type to not rendered
835 *
836 * @param array $field - Field
837 * @param array $form - Current form
838 *
839 @return array - Updated field
840 */
841 public function filter_caldera_forms_render_get_field($field, $form) {
842 if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) {
843 $field['config']['media_lib'] = false;
844 }
845 return $field;
846 }
847
848 /**
849 * Fix for {entry_id}
850 *
851 * @param array $form - Current form
852 * @param int $entry_id - Current entry_id
853 *
854 @return array - Updated form
855 */
856 public function filter_caldera_forms_magic_form($form, $entry_id) {
857 if (class_exists("Caldera_Forms") && $entry_id) {
858 Caldera_Forms::set_field_data('_entry_id', $entry_id, $form);
859 }
860 return $form;
861 }
862
863 /**
864 * Render file and advanced_file field types to url instead image and link
865 *
866 * @param string $value - Text value
867 * @param array $matches - List of matches of magic tags
868 * @param int $entry_id - Current entry
869 * @param array $form - Current form
870 *
871 * @return string - Updated value
872 */
873 public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) {
874
875 if (class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Field_Util")) {
876 if (isset($matches[1])) {
877 foreach ($matches[1] as $key => $tag) {
878
879 $part_tags = explode(':', $tag);
880 if (!empty($part_tags[1])) {
881 $tag = $part_tags[0];
882 }
883 $entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id);
884
885 $field = false;
886 if ($entry !== null) {
887 $field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form);
888 }
889
890 if (Caldera_Forms_Field_Util::is_file_field($field, $form)) {
891 if (filter_var($entry, FILTER_VALIDATE_URL)) {
892 $value = $entry;
893 }
894 continue;
895 }
896 $value = str_replace($matches[0][$key], $entry, $value);
897 }
898 }
899 }
900
901
902 return $value;
903 }
904
905 /**
906 * [e2pdf-view] shortcode fix
907 *
908 * @param array $out - Array with response
909 * @param array $form - Current form
910 *
911 @return array - Updated response
912 */
913 public function filter_caldera_forms_ajax_return($out, $form) {
914
915 if (isset($out['html'])) {
916
917 if (false === strpos($out['html'], '[')) {
918 return $out;
919 }
920
921 $shortcode_tags = array(
922 'e2pdf-view'
923 );
924
925 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
926
927 $tagnames = array_intersect($shortcode_tags, $matches[1]);
928
929 if (!empty($tagnames)) {
930 $pattern = get_shortcode_regex($tagnames);
931
932 preg_match_all("/$pattern/", $out['html'], $shortcodes);
933 foreach ($shortcodes[0] as $key => $shortcode_value) {
934
935 $shortcode = array();
936 $shortcode[1] = $shortcodes[1][$key];
937 $shortcode[2] = $shortcodes[2][$key];
938 $shortcode[3] = $shortcodes[3][$key];
939 $shortcode[4] = $shortcodes[4][$key];
940 $shortcode[5] = $shortcodes[5][$key];
941 $shortcode[6] = $shortcodes[6][$key];
942
943 $out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']);
944 }
945 }
946 }
947
948 return $out;
949 }
950
951 public function action_caldera_forms_submit_complete($form, $referrer, $process_id) {
952 global $form, $transdata;
953
954 if (isset($form['success'])) {
955
956 if (false === strpos($form['success'], '[')) {
957 return;
958 }
959
960 $shortcode_tags = array(
961 'e2pdf-download',
962 'e2pdf-save',
963 'e2pdf-view',
964 'e2pdf-adobesign'
965 );
966
967 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);
968
969 $tagnames = array_intersect($shortcode_tags, $matches[1]);
970
971 if (!empty($tagnames)) {
972 $pattern = get_shortcode_regex($tagnames);
973
974 preg_match_all("/$pattern/", $form['success'], $shortcodes);
975 foreach ($shortcodes[0] as $key => $shortcode_value) {
976
977 $shortcode = array();
978 $shortcode[1] = $shortcodes[1][$key];
979 $shortcode[2] = $shortcodes[2][$key];
980 $shortcode[3] = $shortcodes[3][$key];
981 $shortcode[4] = $shortcodes[4][$key];
982 $shortcode[5] = $shortcodes[5][$key];
983 $shortcode[6] = $shortcodes[6][$key];
984
985 $atts = shortcode_parse_atts($shortcode[3]);
986
987 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
988 $template = new Model_E2pdf_Template();
989 $template->load($atts['id']);
990 if ($template->get('extension') === 'caldera') {
991 $dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false;
992 if ($dataset_id) {
993 $atts['dataset'] = $dataset_id;
994 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
995 }
996 }
997 }
998
999 if ($shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1000 $shortcode[3] .= " apply=\"true\"";
1001 }
1002
1003 if ($shortcode[2] === 'e2pdf-view') {
1004 $new_shortcode = $shortcode[2] . $shortcode[3];
1005 $form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']);
1006 } else {
1007 $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
1008 }
1009 }
1010 }
1011 }
1012 }
1013
1014 /**
1015 * Load filters for this extension
1016 */
1017 public function load_filters() {
1018 add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 10, 3);
1019 add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 10, 2);
1020 }
1021
1022 public function filter_caldera_forms_mailer($mail, $data, $form) {
1023
1024 if ($mail && isset($mail['message'])) {
1025 if (false !== strpos($mail['message'], '[')) {
1026 $shortcode_tags = array(
1027 'e2pdf-download',
1028 'e2pdf-save',
1029 'e2pdf-attachment',
1030 'e2pdf-adobesign'
1031 );
1032
1033 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches);
1034 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1035
1036 if (!empty($tagnames)) {
1037 $pattern = get_shortcode_regex($tagnames);
1038
1039 preg_match_all("/$pattern/", $mail['message'], $shortcodes);
1040
1041 foreach ($shortcodes[0] as $key => $shortcode_value) {
1042 $shortcode = array();
1043 $shortcode[1] = $shortcodes[1][$key];
1044 $shortcode[2] = $shortcodes[2][$key];
1045 $shortcode[3] = $shortcodes[3][$key];
1046 $shortcode[4] = $shortcodes[4][$key];
1047 $shortcode[5] = $shortcodes[5][$key];
1048 $shortcode[6] = $shortcodes[6][$key];
1049
1050 $atts = shortcode_parse_atts($shortcode[3]);
1051
1052 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1053 $template = new Model_E2pdf_Template();
1054 $template->load($atts['id']);
1055 if ($template->get('extension') === 'caldera') {
1056 $dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false;
1057 if ($dataset_id) {
1058 $atts['dataset'] = $dataset_id;
1059 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1060 }
1061 }
1062 }
1063
1064 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1065 $shortcode[3] .= " apply=\"true\"";
1066 }
1067
1068 $shortcode[3] .= " filter=\"true\"";
1069
1070 if ($shortcode[2] === 'e2pdf-attachment') {
1071 $file = do_shortcode_tag($shortcode);
1072 if ($file) {
1073 $this->helper->add('caldera_attachments', $file);
1074 $mail['attachments'][] = $file;
1075 }
1076 $mail['message'] = str_replace($shortcode_value, '', $mail['message']);
1077 } else {
1078 $mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']);
1079 }
1080 }
1081 }
1082 }
1083 }
1084
1085 return $mail;
1086 }
1087
1088 /**
1089 * Get styles for generating Map Field function
1090 *
1091 * @return array - List of css files to load
1092 */
1093 public function get_styles() {
1094
1095 $styles = array();
1096 if (class_exists('Caldera_Forms_Render_Assets')) {
1097 $url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false);
1098 $styles[] = $url;
1099 }
1100
1101 return $styles;
1102 }
1103
1104 }
1105