'caldera', 'title' => 'Caldera Forms', ); /** * Get info about extension * @param string $key - Key to get assigned extension info value * @return array|string - Extension Key and Title or Assigned extension info value */ public function info($key = false) { if ($key && isset($this->info[$key])) { return $this->info[$key]; } else { return array( $this->info['key'] => $this->info['title'], ); } } /** * Check if needed plugin active * @return bool - Activated/Not Activated plugin */ public function active() { if (defined('E2PDF_CALDERA_EXTENSION') || $this->helper->load('extension')->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; switch ($key) { case 'item': global $form; $this->set('cached_form', false); $this->set('cached_tmp_form', false); if (class_exists('Caldera_Forms_Forms') && $this->get('item')) { if ($form && isset($form['ID'])) { $this->set('cached_tmp_form', $form); if ($form['ID'] != $this->get('item')) { $form = Caldera_Forms_Forms::get_form($this->get('item')); } } if ($form == null) { $form = Caldera_Forms_Forms::get_form($this->get('item')); } if (isset($form['_last_updated'])) { $this->set('cached_form', $form); } } break; case 'dataset': $this->set('cached_entry', false); if (class_exists('Caldera_Forms_Entry') && class_exists('Caldera_Forms') && $this->get('cached_form') && $this->get('dataset')) { $entry = new Caldera_Forms_Entry($this->get('cached_form'), $this->get('dataset')); if ($entry->found()) { $this->set('cached_entry', $entry); } if (!empty($this->get('cached_form')['is_connected_form'])) { $form = $this->get('cached_form'); $form['processors']['_connected_form'] = array( 'type' => 'form-connector', 'runtimes' => array( 'insert' => true, ), 'config' => array(), ); $form_fields = array(); $meta = Caldera_Forms::get_entry_meta((int) $this->get('dataset'), $form); if (!empty($meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'])) { foreach ((array) $meta['form-connector']['data']['_connected_form']['entry']['form']['meta_value'] as $form_meta) { $form_fields = array_merge($form_fields, $form_meta); } } if (!empty($form['condition_points']['forms'])) { $form['fields'] = array(); foreach ($form['condition_points']['forms'] as $connected_id => $connected_form) { if (!empty($form_fields[$connected_id]) && !empty($connected_form['fields'])) { $form['fields'] = array_merge($form['fields'], $connected_form['fields']); } } if (function_exists('cf_form_connector_setup_processors_meta')) { add_filter('caldera_forms_get_entry_detail', 'cf_form_connector_setup_processors_meta', 10, 3); } } if (isset($form['node']) && function_exists('cf_from_connector_merge_fields')) { $form['fields'] = cf_from_connector_merge_fields($form); } $this->set('cached_form', $form); } } break; default: break; } 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; } else { switch ($key) { case 'args': $value = array(); break; default: $value = false; break; } } return $value; } /** * Get items to work with * @return array() - List of available items */ public function items() { $content = array(); if (class_exists('Caldera_Forms_Forms')) { $forms = Caldera_Forms_Forms::get_forms(true); if ($forms) { foreach ($forms as $key => $value) { $content[] = $this->item($value['ID']); } } } return $content; } /** * Get entries for export * @param int $item_id - Item ID * @param string $name - Entries names * @return array() - Entries list */ public function datasets($item_id = false, $name = false) { $datasets = array(); if (class_exists('Caldera_Forms_Admin') && $item_id) { $form = Caldera_Forms_Admin::get_entries($item_id, 1, 9999999999); if ($form && isset($form['entries']) && is_array($form['entries'])) { $this->set('item', $item_id); foreach ($form['entries'] as $key => $entry) { $this->set('dataset', $entry['_entry_id']); $entry_title = $this->render($name); if (!$entry_title) { $entry_title = $entry['_date']; } $datasets[] = array( 'key' => $entry['_entry_id'], 'value' => $entry_title, ); } } } return $datasets; } /** * Get Dataset Actions * @param int $dataset_id - Dataset ID * @return object */ public function get_dataset_actions($dataset_id = false) { $dataset_id = (int) $dataset_id; if (!$dataset_id) { return false; } $actions = new stdClass(); $actions->view = false; $actions->delete = false; return $actions; } /** * Get Template Actions * @param int $template - Template ID * @return object */ public function get_template_actions($template = false) { $template = (int) $template; if (!$template) { return; } $actions = new stdClass(); $actions->delete = false; return $actions; } /** * Get item * @param string $item_id - Item ID * @return object - Item */ public function item($item_id = false) { if (!$item_id && $this->get('item')) { $item_id = $this->get('item'); } $form = false; if (class_exists('Caldera_Forms_Forms')) { $form = Caldera_Forms_Forms::get_form($item_id); } $item = new stdClass(); if ($form) { $item->id = (string) $form['ID']; $item->url = $this->helper->get_url( array( 'page' => 'caldera-forms', 'edit' => $form['ID'], ) ); $item->name = isset($form['name']) ? $form['name'] : ''; } else { $item->id = ''; $item->url = ''; $item->name = ''; } return $item; } /** * 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, $field = array(), $convert_shortcodes = true, $raw = false) { $value = $this->render_shortcodes($value, $field); if (!$raw) { $value = $this->strip_shortcodes($value); $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false); $value = $this->helper->load('field')->render_checkbox($value, $this, $field); } 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, $field = array()) { $element_id = isset($field['element_id']) ? $field['element_id'] : false; if ($this->verify()) { if (false !== strpos($value, '[')) { $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field); $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field); $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field); } $value = $this->helper->load('field')->do_shortcodes($value, $this, $field); 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_pre_do_field_magic', array($this, 'filter_caldera_forms_pre_do_field_magic'), 30, 5); add_filter('caldera_forms_get_field_entry', array($this, 'filter_caldera_forms_get_field_entry'), 30, 4); add_filter('caldera_forms_pre_check_condition', array($this, 'filter_caldera_forms_pre_check_condition'), 30); global $form; if ($this->get('cached_form')) { $form = $this->get('cached_form'); } $value = Caldera_Forms::do_magic_tags($value, $this->get('dataset')); if ($this->get('cached_tmp_form')) { $form = $this->get('cached_tmp_form'); } $value = $this->helper->load('field')->render( apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false), $this, $field ); 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_pre_do_field_magic', array($this, 'filter_caldera_forms_pre_do_field_magic'), 30); remove_filter('caldera_forms_get_field_entry', array($this, 'filter_caldera_forms_get_field_entry'), 30); remove_filter('caldera_forms_pre_check_condition', array($this, 'filter_caldera_forms_pre_check_condition'), 30); } return apply_filters( 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false ); } /** * 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('~%[a-z0-9_]*%~', '', $value); return $value; } // strip math public function strip_math($value, &$replacements) { if ($value) { preg_match_all('/%[a-z0-9_]*%/', $value, $matches); foreach ($matches[0] as $match) { $index = count($replacements) + 1; $replacements[] = $match; $value = str_replace($match, '%' . $index . '$s', $value); } } return $value; } /** * Convert shortcodes inside value string * @param string $value - Value string * @param bool $to - Convert From/To * @return string - Converted value */ public function convert_shortcodes($value, $to = false, $html = false) { if ($value) { if ($to) { $value = stripslashes_deep($value); $value = str_replace('[', '[', $value); if (!$html) { $value = wp_specialchars_decode($value, ENT_QUOTES); } } else { $value = str_replace('[', '[', $value); } } return $value; } /** * Verify if item and dataset exists * @return bool - item and dataset exists */ public function verify() { if ($this->get('cached_entry')) { return true; } return false; } /** * Create Form based on uploaded PDF * @param object $template - Template Object to work with * @param array $data - Settings to create labels/shortcodes * @return object - Mapped Template Object */ public function auto_form($template, $data = array()) { if ($template->get('ID')) { $auto_form_label = isset($data['auto_form_label']) && $data['auto_form_label'] ? $data['auto_form_label'] : false; $auto_form_shortcode = isset($data['auto_form_shortcode']) ? true : false; $form = array( '_last_updated' => date('r'), 'success' => sprintf(__('Success. [e2pdf-download id="%s"]', 'e2pdf'), $template->get('ID')), 'name' => $template->get('title'), 'db_support' => '1', 'hide_form' => '1', 'form_ajax' => '1', 'custom_callback' => '', 'fields' => array(), 'layout_grid' => array( 'fields' => array(), 'strucutre' => '', ), 'settings' => array( 'responsive' => array( 'break_point' => 'sm', ), ), ); $pages = $template->get('pages'); $checkboxes = array(); $radios = array(); foreach ($pages as $page_key => $page) { if (isset($page['elements']) && !empty($page['elements'])) { foreach ($page['elements'] as $element_key => $element) { $type = false; $label = ''; if ($element['type'] == 'e2pdf-input' || $element['type'] == 'e2pdf-signature') { $type = 'text'; $label = 'Text'; } elseif ($element['type'] == 'e2pdf-textarea') { $type = 'paragraph'; $label = 'Textarea'; } elseif ($element['type'] == 'e2pdf-select') { $type = 'dropdown'; $label = 'Select'; $options = array(); $field_options = array(); if (isset($element['properties']['options'])) { $field_options = explode("\n", $element['properties']['options']); foreach ($field_options as $option) { $option_id = 'opt' . $this->random(); $options[$option_id] = array( 'calc_value' => '', 'label' => $option, 'value' => $option, ); } } } elseif ($element['type'] == 'e2pdf-checkbox') { $field_key = array_search($element['name'], array_column($checkboxes, 'name'), false); if ($field_key !== false) { $option_id = 'opt' . $this->random(); $checkboxes[$field_key]['options'][$option_id] = array( 'calc_value' => $element['properties']['option'], 'label' => $element['properties']['option'], 'value' => $element['properties']['option'], ); $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $checkboxes[$field_key]['element_id'] . '%'; } else { $type = 'checkbox'; $label = 'Checkbox'; } } elseif ($element['type'] == 'e2pdf-radio') { if (isset($element['properties']['group']) && $element['properties']['group']) { $element['name'] = $element['properties']['group']; } else { $element['name'] = $element['element_id']; } $field_key = array_search($element['name'], array_column($radios, 'name'), false); if ($field_key !== false) { $option_id = 'opt' . $this->random(); $radios[$field_key]['options'][$option_id] = array( 'calc_value' => $element['properties']['option'], 'label' => $element['properties']['option'], 'value' => $element['properties']['option'], ); $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $radios[$field_key]['element_id'] . '%'; } else { $type = 'radio'; $label = 'Radio'; } } if ($type) { $labels = array(); if ($auto_form_shortcode) { $labels[] = '%element_' . $element['element_id'] . '%'; } if ($auto_form_label && $auto_form_label == 'value' && isset($element['value']) && $element['value']) { $labels[] = $element['value']; } elseif ($auto_form_label && $auto_form_label == 'name' && isset($element['name']) && $element['name']) { $labels[] = $element['name']; } if ($type == 'checkbox' || $type == 'radio') { $field_id = 'opt' . $this->random(); $field_data = array( 'name' => $element['name'], 'element_id' => $element['element_id'], 'label' => !empty($labels) ? implode(' ', $labels) : $label, 'options' => array( $field_id => array( 'calc_value' => $element['properties']['option'], 'label' => $element['properties']['option'], 'value' => $element['properties']['option'], ), ), ); if ($type == 'checkbox') { $checkboxes[] = $field_data; } else { $radios[] = $field_data; } } else { $field_id = 'fld_' . $this->random(); $form['fields'][$field_id] = array( 'ID' => $field_id, 'slug' => 'element_' . $element['element_id'], 'type' => $type, 'label' => !empty($labels) ? implode(' ', $labels) : $label, ); if ($type == 'select') { $form['fields'][$field_id]['config'] = array( 'option' => $options, ); } } $pages[$page_key]['elements'][$element_key]['value'] = '%element_' . $element['element_id'] . '%'; if (isset($element['properties']['esig'])) { unset($pages[$page_key]['elements'][$element_key]['properties']['esig']); } } } } } foreach ($checkboxes as $element) { $field_id = 'fld_' . $this->random(); $form['fields'][$field_id] = array( 'ID' => $field_id, 'slug' => 'element_' . $element['element_id'], 'type' => 'checkbox', 'label' => $element['label'], 'config' => array( 'option' => $element['options'], ), ); } foreach ($radios as $element) { $field_id = 'fld_' . $this->random(); $form['fields'][$field_id] = array( 'ID' => $field_id, 'slug' => 'element_' . $element['element_id'], 'type' => 'radio', 'label' => $element['label'], 'config' => array( 'option' => $element['options'], ), ); } $field_id = 'fld_' . $this->random(); $form['fields'][$field_id] = array( 'ID' => $field_id, 'slug' => 'submit', 'type' => 'button', 'label' => 'Submit', 'class' => 'btn btn-default', ); $fields = array_keys($form['fields']); $x = 1; foreach ($fields as $field) { $form['layout_grid']['fields'][$field] = $x . ':1'; if ($x == 1) { $form['layout_grid']['structure'] .= '12'; } else { $form['layout_grid']['structure'] .= '|12'; } $x++; } $item = Caldera_Forms_Forms::create_form($form); if ($item) { $template->set('item', $item['ID']); $template->set('pages', $pages); } } return $template; } /** * Generate Random number for Field ID * @return int - Generated number */ private function random() { return round((float) rand() / (float) getrandmax() * 10000000); } /** * Init Visual Mapper data * @return bool|string - HTML data source for Visual Mapper */ public function visual_mapper() { $forms = array(); $source = ''; $html = ''; if ($this->get('item') && class_exists('Caldera_Forms') && class_exists('Caldera_Forms_Forms') && class_exists('Caldera_Forms_Field_Util')) { add_filter('caldera_forms_render_get_form', array($this, 'filter_caldera_forms_render_get_form')); $form = Caldera_Forms_Forms::get_form($this->get('item')); if (!empty($form['is_connected_form']) && isset($form['node']) && !empty($form['node'])) { foreach ($form['node'] as $node) { if (isset($node['form']) && $node['form']) { $source .= Caldera_Forms::render_form($node['form']); $forms[] = Caldera_Forms_Forms::get_form($node['form']); } } } else { $source = Caldera_Forms::render_form($this->get('item')); } remove_filter('caldera_forms_render_get_form', array($this, 'filter_caldera_forms_render_get_form')); if ($source) { $dom = new DOMDocument(); $html = $this->helper->load('convert')->load_html($source, $dom, true); } if (!$source) { return '
' . __("The form source is empty or doesn't exist", 'e2pdf') . '
'; } elseif (!$html) { return '
' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '
'; } else { $xml = $this->helper->load('xml'); $xpath = new DomXPath($dom); $conditional_fields = $xpath->query("//*[contains(@class, 'caldera-forms-conditional-field')]"); foreach ($conditional_fields as $element) { $conditional_field_id = $xml->get_node_value($element, 'data-field-id'); if ($conditional_field_id) { preg_match('/