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

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