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