__('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('/