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

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