# e2pdf/1.01.01/classes/extension/e2pdf-caldera.php

E2Pdf – Export Pdf Tool for WordPress, version 1.01.01. 1,159 lines.

- Page: https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/extension/e2pdf-caldera.php
- Raw: https://pluginprobe.com/plugins/e2pdf/1.01.01/raw/classes/extension/e2pdf-caldera.php
- Modified: 2018-11-07T14:59:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/e2pdf/1.01.01/code/classes/extension/e2pdf-caldera.php#L10-L20`.

```php
<?php

/**
 * E2pdf Caldera Extension
 * 
 * @copyright  Copyright 2017 https://e2pdf.com
 * @license    GPL v2
 * @version    1
 * @link       https://e2pdf.com
 * @since      0.01.47
 */
if (!defined('ABSPATH')) {
    die('Access denied.');
}

class Extension_E2pdf_Caldera extends Model_E2pdf_Model {

    private $options;

    function __construct() {
        parent::__construct();
    }

    /**
     * Get info about extension
     * 
     * @return array() - Extension key and name
     */
    public function info() {
        return array(
            'caldera' => __('Caldera', 'e2pdf')
        );
    }

    /**
     * Check if export item function available
     * 
     * @return bool - Item export available/not available
     */
    public function export() {
        return false;
    }

    /**
     * Check if needed plugin active
     * 
     * @return bool - Activated/Not Activated plugin
     */
    public function active() {

        if (!function_exists('is_plugin_active')) {
            include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
        }

        if (is_plugin_active('caldera-forms/caldera-core.php')) {
            return true;
        }
        return false;
    }

    /**
     * Set option
     * 
     * @param string $key - Key of option
     * @param string $value - Value of option
     * 
     * @return bool - Status of setting option
     */
    public function set($key, $value) {
        if (!isset($this->options)) {
            $this->options = new stdClass();
        }

        $this->options->$key = $value;
        return true;
    }

    /**
     * Get option by key
     * 
     * @param string $key - Key to get assigned option value
     * 
     * @return mixed
     */
    public function get($key) {
        if (isset($this->options->$key)) {
            $value = $this->options->$key;
            return $value;
        } else {
            return false;
        }
    }

    /**
     * Get items to work with
     * 
     * @return array() - List of available items
     */
    public function get_items() {
        $content = array();
        if (class_exists("Caldera_Forms_Forms")) {
            $forms = Caldera_Forms_Forms::get_forms(true);
            if ($forms) {
                foreach ($forms as $key => $value) {
                    $content[] = array(
                        'key' => $value['ID'],
                        'value' => $value['name']
                    );
                }
            }
        }
        return $content;
    }

    /**
     * Get entries for export
     * 
     * @param int $item - Form ID
     * @param string $name - Entries names
     * 
     * @return array() - Entries list
     */
    public function get_datasets($item = false, $name = false) {

        $datasets = array();

        if (class_exists("Caldera_Forms_Admin") && $item) {

            $form = Caldera_Forms_Admin::get_entries($item, 1, 9999999999);

            if ($form && isset($form['entries']) && is_array($form['entries'])) {
                foreach ($form['entries'] as $key => $dataset) {
                    $this->set('item', $item);
                    $this->set('dataset', $dataset['_entry_id']);

                    $dataset_title = $this->render($name);
                    if (!$dataset_title) {
                        $dataset_title = $dataset['_date'];
                    }
                    $datasets[] = array(
                        'key' => $dataset['_entry_id'],
                        'value' => $dataset_title
                    );
                }
            }
        }


        return $datasets;
    }

    /**
     * Get dataset
     * 
     * @param int $dataset - Dataset ID
     * 
     * @return object - Dataset
     */
    public function get_dataset($dataset = false) {
        $dataset = (int) $dataset;

        if (!$dataset) {
            return false;
        }

        $data = new stdClass();
        $data->url = false;

        return $data;
    }

    /**
     * Get item
     * 
     * @param string $item - Item ID
     * 
     * @return object - Item
     */
    public function get_item($item = false) {

        $form = new stdClass();

        $caldera_form = false;
        if (class_exists("Caldera_Forms_Forms")) {
            $caldera_form = Caldera_Forms_Forms::get_form($item);
        }

        if ($caldera_form) {
            $form->url = $this->helper->get_url(array('page' => 'caldera-forms', 'edit' => $caldera_form['ID']));
            $form->name = $caldera_form['name'];
        } else {
            $form->url = 'javascript:void(0);';
            $form->name = '';
        }
        return $form;
    }

    /**
     * Render value according to content
     * 
     * @param string $value - Content
     * @param string $type - Type of rendering value
     * @param array $field - Field details
     * 
     * @return string - Fully rendered value
     */
    public function render($value, $type = false, $field = array()) {

        $value = $this->render_shortcodes($value, $type, $field);
        $value = $this->strip_shortcodes($value);

        if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
            $option = $this->render($field['properties']['option']);
            $options = explode(', ', $value);
            $option_options = explode(', ', $option);
            if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
                return $option;
            } else {
                return "";
            }
        }

        return $value;
    }

    /**
     * Render shortcodes which available in this extension
     * 
     * @param string $value - Content
     * @param string $type - Type of rendering value
     * @param array $field - Field details
     * 
     * @return string - Value with rendered shortcodes
     */
    public function render_shortcodes($value, $type = false, $field = array()) {

        $dataset = $this->get('dataset');

        if ($this->verify() && class_exists("Caldera_Forms_Entry") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms")) {
            if (false !== strpos($value, '[')) {

                $shortcode_tags = array(
                    'e2pdf-format-number',
                    'e2pdf-format-date',
                    'e2pdf-format-output',
                );
                preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
                $tagnames = array_intersect($shortcode_tags, $matches[1]);

                if (!empty($tagnames)) {

                    $pattern = get_shortcode_regex($tagnames);
                    preg_match_all("/$pattern/", $value, $shortcodes);
                    foreach ($shortcodes[0] as $key => $shortcode_value) {
                        $shortcode = array();
                        $shortcode[1] = $shortcodes[1][$key];
                        $shortcode[2] = $shortcodes[2][$key];
                        $shortcode[3] = $shortcodes[3][$key];
                        $shortcode[4] = $shortcodes[4][$key];
                        $shortcode[5] = $shortcodes[5][$key];
                        $shortcode[6] = $shortcodes[6][$key];

                        if (isset($shortcode['5'])) {
                            $sub_value = $this->strip_shortcodes(Caldera_Forms::do_magic_tags($shortcode['5'], $dataset));
                            $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
                        }
                    }
                }
            }

            $value = do_shortcode($value);

            add_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30, 2);
            add_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30, 2);
            add_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30, 4);

            $value = Caldera_Forms::do_magic_tags($value, $dataset);

            if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
                $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
                if ($esig) {
                    //process e-signature
                    $value = "";
                } else {
                    $helper_e2pdf_image = new Helper_E2pdf_Image();
                    if (!$helper_e2pdf_image->get_image($value)) {
                        $value = $this->strip_shortcodes($value);
                        if (
                                $value &&
                                trim($value) != "" &&
                                extension_loaded('gd') &&
                                function_exists('imagettftext')
                        ) {
                            if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
                                $penColour = $this->helper->hexColorAllocate($field['properties']['text_color']);
                            } else {
                                $penColour = array(0x14, 0x53, 0x94);
                            }

                            $default_options = array(
                                'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
                                'bgColour' => 'transparent',
                                'penColour' => $penColour
                            );

                            $options = array();
                            $options = array_merge($default_options, $options);

                            $model_e2pdf_font = new Model_E2pdf_Font();

                            $font = false;
                            if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
                                $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
                            }
                            if (!$font) {
                                $font = $model_e2pdf_font->get_font_path('Noto Sans');
                            }

                            $size = 150;
                            if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
                                $size = $field['properties']['text_font_size'];
                            }

                            $model_e2pdf_signature = new Model_E2pdf_Signature();
                            $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
                        } else {
                            $value = "";
                        }
                    }
                }
            }

            remove_filter('caldera_forms_magic_form', array($this, 'filter_caldera_forms_magic_form'), 30);
            remove_filter('caldera_forms_render_get_field', array($this, 'filter_caldera_forms_render_get_field'), 30);
            remove_filter('caldera_forms_do_field_magic_value', array($this, 'filter_caldera_forms_do_field_magic_value'), 30);
        }

        return $value;
    }

    /**
     * Strip unused shortcodes
     * 
     * @param string $value - Content
     * 
     * @return string - Value with removed unused shortcodes
     */
    public function strip_shortcodes($value) {
        $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
        $value = preg_replace('~(?:%/?)[^/%]+/?%~', "", $value);
        return $value;
    }

    public function verify() {
        $item = $this->get('item');
        $dataset = $this->get('dataset');

        if ($item && $dataset && class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms_Forms')) {
            $form = Caldera_Forms_Forms::get_form($item);
            if ($form && isset($form['_last_updated'])) {
                $entry = new Caldera_Forms_Entry($form, $dataset);
                if ($entry->found()) {
                    return true;
                }
            }
        }
        return false;
    }

    public function visual_mapper() {

        $item = $this->get('item');

        if ($item && class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Forms") && class_exists("Caldera_Forms_Field_Util")) {

            $source = Caldera_Forms::render_form($item);

            $form = Caldera_Forms_Forms::get_form($item);

            libxml_use_internal_errors(true);
            $dom = new DOMDocument;
            $html = $dom->loadHTML($source);
            libxml_clear_errors();

            if (!$html) {
                return __('Form could not be parsed due incorrect HTML', 'e2pdf');
            } else {

                $xpath = new DomXPath($dom);


                $remove_by_class = array(
                    'live-gravatar',
                );
                foreach ($remove_by_class as $key => $class) {
                    $elements = $xpath->query("//*[contains(@class, '{$class}')]");
                    foreach ($elements as $element) {
                        $element->parentNode->removeChild($element);
                    }
                }

                $remove_by_tag = array(
                    'script'
                );
                foreach ($remove_by_tag as $key => $tag) {
                    $elements = $xpath->query("//{$tag}");
                    foreach ($elements as $element) {
                        $element->parentNode->removeChild($element);
                    }
                }


                // Replace names
                $fields = $xpath->query("//*[contains(@name, 'fld_')]");
                foreach ($fields as $element) {
                    if ($element->attributes->getNamedItem("type")->nodeValue == 'checkbox') {
                        preg_match("/(.*?)\[(.*?)\]/i", $element->attributes->getNamedItem("name")->nodeValue, $matches);
                        if (isset($matches[1])) {
                            $field_id = $matches[1];
                        }
                    } else {
                        $field_id = $element->attributes->getNamedItem("name")->nodeValue;
                    }


                    // Modify Star field
                    if ($element->attributes->getNamedItem("data-type")->nodeValue == 'star') {
                        $element->attributes->getNamedItem("style")->nodeValue = '';
                    }

                    // Modify Range field
                    if ($element->attributes->getNamedItem("type")->nodeValue == 'range') {
                        $element->parentNode->attributes->getNamedItem("style")->nodeValue = '';
                        $element->parentNode->attributes->getNamedItem("class")->nodeValue = 'col-xs-12';

                        $next_element = $xpath->query(".//following-sibling::div[1]", $element->parentNode)->item(0);
                        $next_element->parentNode->removeChild($next_element);
                    }


                    $field = Caldera_Forms_Field_Util::get_field($field_id, $form);
                    if ($field && isset($field['slug'])) {
                        $element->attributes->getNamedItem("name")->nodeValue = "%" . $field['slug'] . "%";
                    }
                }

                // Convert Calculation Fields
                $calculation_fields = $xpath->query("//input[@data-type='calculation']");
                foreach ($calculation_fields as $calculation_field) {
                    $calculation_field->attributes->getNamedItem("type")->nodeValue = 'text';

                    $calculation_text = $xpath->query(".//h3", $calculation_field->parentNode)->item(0);
                    $calculation_field->parentNode->removeChild($calculation_text);
                }


                // Modify Toggle Fields
                $toggle_gorups = $xpath->query("//*[contains(@class, 'cf-toggle-switch')]");
                foreach ($toggle_gorups as $toogle_group) {
                    $toggle_elements = $xpath->query(".//*[contains(@class, 'cf-toggle-group-buttons')]//a", $toogle_group);
                    foreach ($toggle_elements as $toggle_element) {
                        $toggle_id = $toggle_element->attributes->getNamedItem("id")->nodeValue;

                        $radio = $toggle_elements = $xpath->query(".//input[@id='{$toggle_id}']", $toogle_group)->item(0);

                        if ($radio) {
                            $radio_cloned = $radio->cloneNode(true);

                            $attr = $dom->createAttribute('style');
                            $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
                            $radio_cloned->appendChild($attr);

                            $toggle_element->appendChild($radio_cloned);
                        }
                    }
                }

                // Modify Advanced File Upload
                $advanced_uploaders = $xpath->query("//*[contains(@class, 'cf-uploader-trigger')]");
                foreach ($advanced_uploaders as $advanced_uploader) {
                    $input = $xpath->query(".//input[@type='hidden']", $advanced_uploader->parentNode)->item(0);


                    $attr = $dom->createAttribute('style');
                    $attr->value = 'position: relative;';
                    $advanced_uploader->appendChild($attr);


                    if ($input) {
                        $input_cloned = $input->cloneNode(true);
                        $attr = $dom->createAttribute('style');
                        $attr->value = 'position: absolute;width: 100%;top: 0;left: 0;height: 100%; opacity: 0;';
                        $input_cloned->appendChild($attr);

                        $input_cloned->attributes->getNamedItem('type')->nodeValue = 'text';

                        $advanced_uploader->appendChild($input_cloned);
                    }
                }

                // Remove unecessary elements
                $submit_buttons = $xpath->query("//input[@type='submit']");
                foreach ($submit_buttons as $submit_button) {
                    $submit_button->parentNode->removeChild($submit_button);
                }




                return $dom->saveHTML();
            }
        }
        return false;
    }

    /**
     * Auto Generate of Template for this extension
     * 
     * @return array - List of elements
     */
    public function auto() {
        $response = array();
        $elements = array();

        $item = $this->get('item');

        $fields = array();

        if (class_exists("Caldera_Forms_Forms")) {
            $form = Caldera_Forms_Forms::get_form($item);
            if ($form && isset($form['fields'])) {
                $fields = $form['fields'];
            }
        }


        if ($form && isset($form['layout_grid']['structure']) && isset($form['layout_grid']['fields'])) {
            $struct = explode('|', $form['layout_grid']['structure']);
            foreach ($struct as $row_num => $row) {

                $float = false;

                $columns = explode(':', $row);
                foreach ($columns as $column_num => $column) {
                    $column_fields = array_keys($form['layout_grid']['fields'], ( $row_num + 1) . ':' . ( $column_num + 1));

                    $width = 100 / (12 / $column) . "%";

                    foreach ($column_fields as $column_field_key) {
                        if (isset($fields[$column_field_key])) {
                            $field = $fields[$column_field_key];


                            switch ($field['type']) {
                                case 'text':
                                case 'email':
                                case 'phone_better':
                                case 'phone_better':
                                case 'number':
                                case 'phone':
                                case 'url':
                                case 'date_picker':
                                case 'color_picker':
                                case 'credit_card_number':
                                case 'credit_card_exp':
                                case 'credit_card_cvc':
                                case 'calculation':
                                case 'range_slider':
                                case 'star_rating':
                                case 'utm':
                                case 'file':
                                case 'advanced_file':
                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));


                                    $elements[] = $this->auto_field($field, 'e2pdf-input', array(
                                        'properties' => array(
                                            'value' => "%{$field['slug']}%",
                                        )
                                    ));
                                    break;


                                case 'paragraph':

                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));

                                    $elements[] = $this->auto_field($field, 'e2pdf-textarea', array(
                                        'properties' => array(
                                            'value' => "%{$field['slug']}%",
                                        )
                                    ));
                                    break;


                                case 'wysiwyg':
                                case 'html':
                                case 'summary':
                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));

                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'properties' => array(
                                            'height' => '100',
                                            'value' => "%{$field['slug']}%",
                                        )
                                    ));
                                    break;


                                case 'dropdown':
                                case 'filtered_select2':
                                case 'states':
                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));

                                    $options_tmp = array();

                                    if (isset($field['config']['option']) && is_array($field['config']['option'])) {
                                        foreach ($field['config']['option'] as $opt_key => $option) {
                                            if (is_array($option)) {
                                                $options_tmp[] = $option['value'];
                                            }
                                        }
                                    }

                                    if ($field['type'] == 'states' && defined("CFCORE_PATH")) {
                                        if (file_exists(CFCORE_PATH . "fields/states/field.php")) {
                                            $contents = file_get_contents(CFCORE_PATH . "fields/states/field.php");
                                            preg_match_all('/<option value="(.*?)">(.*?)<\/option>/', $contents, $matches);
                                            if (isset($matches[1])) {
                                                foreach ($matches[1] as $state) {
                                                    $options_tmp[] = $state;
                                                }
                                            }
                                        }
                                    }

                                    $elements[] = $this->auto_field($field, 'e2pdf-select', array(
                                        'properties' => array(
                                            'options' => implode("\n", $options_tmp),
                                            'value' => "%{$field['slug']}%",
                                        )
                                    ));
                                    break;


                                case 'checkbox':
                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));

                                    $top = 0;



                                    if (isset($field['config']['option']) && is_array($field['config']['option'])) {
                                        foreach ($field['config']['option'] as $opt_key => $option) {
                                            if (is_array($option)) {
                                                $elements[] = $this->auto_field($field, 'e2pdf-checkbox', array(
                                                    'top' => $top,
                                                    'properties' => array(
                                                        'width' => '15',
                                                        'height' => '15',
                                                        'value' => "%{$field['slug']}%",
                                                        'option' => $option['label']
                                                    )
                                                ));
                                                $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                                    'top' => $top,
                                                    'left' => '20',
                                                    'properties' => array(
                                                        'value' => $option['label']
                                                    )
                                                ));
                                            }
                                            $top = $top + 20;
                                        }
                                    }
                                    break;


                                case 'radio':
                                case 'toggle_switch':
                                    $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                        'top' => '20',
                                        'left' => '20',
                                        'right' => '20',
                                        'block' => true,
                                        'float' => $float,
                                        'width' => $width,
                                        'properties' => array(
                                            'value' => $field['label'],
                                        )
                                    ));

                                    $top = 0;



                                    if (isset($field['config']['option']) && is_array($field['config']['option'])) {
                                        foreach ($field['config']['option'] as $opt_key => $option) {
                                            if (is_array($option)) {
                                                $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
                                                    'top' => $top,
                                                    'properties' => array(
                                                        'width' => '15',
                                                        'height' => '15',
                                                        'value' => "%{$field['slug']}%",
                                                        'option' => $option['label'],
                                                        'group' => "group_" . $field['slug'],
                                                    )
                                                ));
                                                $elements[] = $this->auto_field($field, 'e2pdf-html', array(
                                                    'top' => $top,
                                                    'left' => '20',
                                                    'properties' => array(
                                                        'value' => $option['label']
                                                    )
                                                ));
                                            }
                                            $top = $top + 20;
                                        }
                                    }
                                    break;



                                default:
                                    /*
                                     *  Unsupported fields: section_break, live_gravatar, button 
                                     */
                                    break;
                            }
                        }
                    }

                    $float = true;
                }
            }
        }


        $response['page'] = array(
            'bottom' => '20',
            'top' => '20',
        );

        $response['elements'] = $elements;
        return $response;
    }

    public function auto_field($field = false, $type = false, $options = array()) {

        if (!$field || !$type) {
            return false;
        }


        $element = array(
            'type' => $type,
            'properties' => isset($options['properties']) ? $options['properties'] : array(),
        );


        $element['top'] = isset($options['top']) ? $options['top'] : '0';
        $element['left'] = isset($options['left']) ? $options['left'] : '0';
        $element['right'] = isset($options['right']) ? $options['right'] : '0';
        $element['block'] = isset($options['block']) ? $options['block'] : false;
        $element['float'] = isset($options['float']) ? $options['float'] : false;

        if (isset($options['width']) && $element['block']) {
            $element['width'] = $options['width'];
        }



        return $element;
    }

    /**
     * Load actions for this extension
     */
    public function load_actions() {
        add_action('caldera_forms_submit_complete', array($this, 'action_caldera_forms_submit_complete'), 99, 3);
        add_action('caldera_forms_mailer_complete', array($this, 'action_caldera_forms_mailer_complete'), 99, 3);
    }

    /**
     * Delete attachments that were sent by email
     */
    public function action_caldera_forms_mailer_complete() {

        $files = $this->helper->get('caldera_attachments');

        if (is_array($files) && !empty($files)) {
            foreach ($files as $key => $file) {
                $this->helper->delete_dir(dirname($file) . '/');
            }
            $this->helper->deset('caldera_attachments');
        }
    }

    /**
     * Convert file and advanced_file field type to not rendered
     * 
     * @param array $field - Field
     * @param array $form - Current form 
     * 
      @return array - Updated field
     */
    public function filter_caldera_forms_render_get_field($field, $form) {
        if (isset($field['type']) && ($field['type'] == 'file' || $field['type'] == 'advanced_file')) {
            $field['config']['media_lib'] = false;
        }
        return $field;
    }

    /**
     * Fix for {entry_id}
     * 
     * @param array $form - Current form
     * @param int $entry_id - Current entry_id
     * 
      @return array - Updated form
     */
    public function filter_caldera_forms_magic_form($form, $entry_id) {
        if (class_exists("Caldera_Forms") && $entry_id) {
            Caldera_Forms::set_field_data('_entry_id', $entry_id, $form);
        }
        return $form;
    }

    /**
     * Render file and advanced_file field types to url instead image and link
     * 
     * @param string $value - Text value
     * @param array $matches - List of matches of magic tags
     * @param int $entry_id - Current entry
     * @param array $form - Current form  
     * 
     * @return string - Updated value
     */
    public function filter_caldera_forms_do_field_magic_value($value, $matches, $entry_id, $form) {

        if (class_exists("Caldera_Forms") && class_exists("Caldera_Forms_Field_Util")) {
            if (isset($matches[1])) {
                foreach ($matches[1] as $key => $tag) {

                    $part_tags = explode(':', $tag);
                    if (!empty($part_tags[1])) {
                        $tag = $part_tags[0];
                    }
                    $entry = Caldera_Forms::get_slug_data($tag, $form, $entry_id);

                    $field = false;
                    if ($entry !== null) {
                        $field = Caldera_Forms_Field_Util::get_field_by_slug($tag, $form);
                    }

                    if (Caldera_Forms_Field_Util::is_file_field($field, $form)) {
                        if (filter_var($entry, FILTER_VALIDATE_URL)) {
                            $value = $entry;
                        }
                        continue;
                    }
                    $value = str_replace($matches[0][$key], $entry, $value);
                }
            }
        }


        return $value;
    }

    /**
     * [e2pdf-view] shortcode fix
     * 
     * @param array $out - Array with response
     * @param array $form - Current form 
     * 
      @return array - Updated response
     */
    public function filter_caldera_forms_ajax_return($out, $form) {

        if (isset($out['html'])) {

            if (false === strpos($out['html'], '[')) {
                return $out;
            }

            $shortcode_tags = array(
                'e2pdf-view'
            );

            preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);

            $tagnames = array_intersect($shortcode_tags, $matches[1]);

            if (!empty($tagnames)) {
                $pattern = get_shortcode_regex($tagnames);

                preg_match_all("/$pattern/", $out['html'], $shortcodes);
                foreach ($shortcodes[0] as $key => $shortcode_value) {

                    $extension = false;
                    $item = false;

                    $shortcode = array();
                    $shortcode[1] = $shortcodes[1][$key];
                    $shortcode[2] = $shortcodes[2][$key];
                    $shortcode[3] = $shortcodes[3][$key];
                    $shortcode[4] = $shortcodes[4][$key];
                    $shortcode[5] = $shortcodes[5][$key];
                    $shortcode[6] = $shortcodes[6][$key];

                    $atts = shortcode_parse_atts($shortcode[3]);

                    if (array_key_exists('id', $atts)) {
                        $template = new Model_E2pdf_Template();
                        $template->load($atts['id']);
                        if ($template->get('extension') === 'caldera') {
                            $extension = $template->get('extension');
                            $item = $template->get('item');
                        }
                    }

                    $out['html'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $out['html']);
                }
            }
        }

        return $out;
    }

    public function action_caldera_forms_submit_complete($form, $referrer, $process_id) {
        global $form, $transdata;

        if (isset($form['success'])) {

            if (false === strpos($form['success'], '[')) {
                return;
            }

            $shortcode_tags = array(
                'e2pdf-download',
                'e2pdf-save',
                'e2pdf-view'
            );

            preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $form['success'], $matches);

            $tagnames = array_intersect($shortcode_tags, $matches[1]);

            if (!empty($tagnames)) {
                $pattern = get_shortcode_regex($tagnames);

                preg_match_all("/$pattern/", $form['success'], $shortcodes);
                foreach ($shortcodes[0] as $key => $shortcode_value) {

                    $extension = false;
                    $item = false;

                    $shortcode = array();
                    $shortcode[1] = $shortcodes[1][$key];
                    $shortcode[2] = $shortcodes[2][$key];
                    $shortcode[3] = $shortcodes[3][$key];
                    $shortcode[4] = $shortcodes[4][$key];
                    $shortcode[5] = $shortcodes[5][$key];
                    $shortcode[6] = $shortcodes[6][$key];

                    $atts = shortcode_parse_atts($shortcode[3]);

                    if (array_key_exists('id', $atts)) {
                        $template = new Model_E2pdf_Template();
                        $template->load($atts['id']);
                        if ($template->get('extension') === 'caldera') {
                            $extension = $template->get('extension');
                            $item = $template->get('item');
                        }
                    }

                    if ($extension) {
                        if (!array_key_exists('dataset', $atts)) {
                            $dataset_id = isset($transdata['entry_id']) ? $transdata['entry_id'] : false;
                            if ($dataset_id) {
                                $atts['dataset'] = $dataset_id;
                                $shortcode[3] .= " dataset='{$dataset_id}'";

                                if ($shortcode[2] === 'e2pdf-save') {
                                    $shortcode[3] .= " apply='true'";
                                }

                                if ($shortcode[2] === 'e2pdf-view') {
                                    $new_shortcode = $shortcode[2] . $shortcode[3];
                                    $form['success'] = str_replace($shortcode_value, "[{$new_shortcode}]", $form['success']);
                                } else {
                                    $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
                                }
                            }
                        } else {

                            if ($shortcode[2] === 'e2pdf-save') {
                                $shortcode[3] .= " apply='true'";
                            }

                            if ($shortcode[2] !== 'e2pdf-view') {
                                $form['success'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $form['success']);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Load filters for this extension
     */
    public function load_filters() {
        add_filter('caldera_forms_mailer', array($this, 'filter_caldera_forms_mailer'), 10, 3);
        add_filter('caldera_forms_ajax_return', array($this, 'filter_caldera_forms_ajax_return'), 10, 2);
    }

    public function filter_caldera_forms_mailer($mail, $data, $form) {

        if ($mail && isset($mail['message'])) {
            if (false !== strpos($mail['message'], '[')) {
                $shortcode_tags = array(
                    'e2pdf-download',
                    'e2pdf-attachment',
                    'e2pdf-save'
                );

                preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $mail['message'], $matches);
                $tagnames = array_intersect($shortcode_tags, $matches[1]);

                if (!empty($tagnames)) {
                    $pattern = get_shortcode_regex($tagnames);

                    preg_match_all("/$pattern/", $mail['message'], $shortcodes);

                    foreach ($shortcodes[0] as $key => $shortcode_value) {
                        $extension = false;
                        $item = false;

                        $shortcode = array();
                        $shortcode[1] = $shortcodes[1][$key];
                        $shortcode[2] = $shortcodes[2][$key];
                        $shortcode[3] = $shortcodes[3][$key];
                        $shortcode[4] = $shortcodes[4][$key];
                        $shortcode[5] = $shortcodes[5][$key];
                        $shortcode[6] = $shortcodes[6][$key];

                        $atts = shortcode_parse_atts($shortcode[3]);

                        if (array_key_exists('id', $atts)) {
                            $template = new Model_E2pdf_Template();
                            $template->load($atts['id']);
                            if ($template->get('extension') === 'caldera') {
                                $extension = $template->get('extension');
                                $item = $template->get('item');

                                if (!array_key_exists('dataset', $atts)) {
                                    $dataset_id = isset($data['_entry_id']) ? $data['_entry_id'] : false;
                                    if ($dataset_id) {
                                        $atts['dataset'] = $dataset_id;
                                        $shortcode[3] .= " dataset='{$dataset_id}' apply='true'";
                                    }
                                } else {
                                    $shortcode[3] .= " apply='true'";
                                }

                                if ($shortcode[2] === 'e2pdf-download' || $shortcode[2] === 'e2pdf-save') {
                                    $mail['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $mail['message']);
                                } elseif ($shortcode[2] === 'e2pdf-attachment') {
                                    $file = do_shortcode_tag($shortcode);

                                    if ($file) {
                                        $this->helper->add('caldera_attachments', $file);
                                        $mail['attachments'][] = $file;
                                    }
                                    $mail['message'] = str_replace($shortcode_value, '', $mail['message']);
                                }
                            }
                        }
                    }
                }
            }
        }


        return $mail;
    }

    /**
     * Get styles for generating Map Field function
     * 
     * @return array - List of css files to load
     */
    public function get_styles() {

        $styles = array();
        if (class_exists('Caldera_Forms_Render_Assets')) {
            $url = Caldera_Forms_Render_Assets::make_url('caldera-forms-front', false);
            $styles[] = $url;
        }

        return $styles;
    }

}

```
