PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.49
E2Pdf – Export Pdf Tool for WordPress v1.32.49
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / extension / e2pdf-ninja.php

e2pdf-ninja.php in E2Pdf – Export Pdf Tool for WordPress 1.32.49, at classes/extension/e2pdf-ninja.php

1,094 lines 50.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * File: /extension/e2pdf-ninja.php
5 *
6 * @package E2Pdf
7 * @license GPLv3
8 * @link https://e2pdf.com
9 */
10 if (!defined('ABSPATH')) {
11 die('Access denied.');
12 }
13
14 class Extension_E2pdf_Ninja extends Model_E2pdf_Model {
15
16 private $options;
17 private $info = array(
18 'key' => 'ninja',
19 'title' => 'Ninja Forms',
20 );
21
22 // info
23 public function info($key = false) {
24 if ($key && isset($this->info[$key])) {
25 return $this->info[$key];
26 } else {
27 return array(
28 $this->info['key'] => $this->info['title'],
29 );
30 }
31 }
32
33 // active
34 public function active() {
35 if (defined('E2PDF_NINJA_EXTENSION') || $this->helper->load('extension')->is_plugin_active('ninja-forms/ninja-forms.php')) {
36 return true;
37 }
38 return false;
39 }
40
41 // set
42 public function set($key, $value) {
43 if (!isset($this->options)) {
44 $this->options = new stdClass();
45 }
46 $this->options->$key = $value;
47
48 switch ($key) {
49 case 'item':
50 $this->set('cached_form', false);
51 if ($this->get('item') && function_exists('Ninja_Forms')) {
52 $form = Ninja_Forms()->form($this->get('item'))->get();
53 if ($form->get_setting('objectType')) {
54 $this->set('cached_form', $form);
55 }
56 }
57 break;
58 case 'dataset':
59 $this->set('cached_entry', false);
60 if ($this->get('cached_form') && $this->get('dataset')) {
61 $entry = Ninja_Forms()->form()->get_sub($this->get('dataset'));
62 if ($entry->get_form_id() == $this->get('cached_form')->get_id()) {
63 $this->set('cached_entry', $entry);
64
65 $fields_merge_tag_object = Ninja_Forms()->merge_tags['fields'];
66 $fields_merge_tag_object->set_form_id($this->get('item'));
67 $fields = Ninja_Forms()->form($this->get('item'))->get_fields();
68
69 if (class_exists('NF_Adapters_SubmissionsSubmission')) {
70 $fields = new NF_Adapters_SubmissionsSubmission($fields, $this->get('item'), $this->get('cached_entry'));
71 foreach ($fields as $field) {
72 // Fix PHP warnings
73 if (isset($field['type']) && isset($field['value'])) {
74 switch ($field['type']) {
75 case 'checkbox':
76 if (!isset($field['settings'])) {
77 $field['settings'] = array();
78 }
79 if (!isset($field['settings']['unchecked_value'])) {
80 $field['settings']['unchecked_value'] = '';
81 }
82 if (!isset($field['unchecked_calc_value'])) {
83 $field['unchecked_calc_value'] = '';
84 }
85 break;
86 case 'file_upload':
87 $_GET['sub_id'] = '';
88 break;
89 default:
90 break;
91 }
92 }
93 $fields_merge_tag_object->add_field($field);
94 if (isset($field['type']) && isset($field['value'])) {
95 switch ($field['type']) {
96 case 'repeater':
97 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
98 $list_fields_types = array('listcheckbox', 'listmultiselect', 'listradio', 'listselect');
99 $field['value_label'] = $field['value'];
100 foreach ($form_field->get_setting('fields') as $sub_field) {
101 if (in_array($sub_field['type'], $list_fields_types, false)) {
102 foreach ($field['value_label'] as $sub_value_key => $sub_value) {
103 if (0 === strpos($sub_value['id'], $sub_field['id'] . '_')) {
104 $sub_field['value'] = $sub_value['value'];
105 $field['value_label'][$sub_value_key]['value'] = $fields_merge_tag_object->get_list_labels($sub_field);
106 }
107 }
108 }
109 }
110
111 $repeater_data_labels = Ninja_Forms()->fieldsetRepeater->extractSubmissions($field['id'], $field['value_label'], $form_field->get_settings());
112 $rows = array();
113 foreach ($repeater_data_labels as $repeater_key => $repeater) {
114 $row = array();
115 foreach ($repeater as $sub_repeater_key => $sub_repeater) {
116 $sub_value = is_array($sub_repeater['value']) ? implode(', ', $sub_repeater['value']) : $sub_repeater['value'];
117 $fields_merge_tag_object->add('field_' . $field['key'] . '_' . $repeater_key . '_' . $sub_repeater_key . '_label', $field['key'], '{field:' . $field['key'] . ':' . $repeater_key . '_' . $sub_repeater_key . ':label}', $sub_value);
118 $row[] = $sub_repeater['label'] . ': ' . $sub_value;
119 }
120 if (!empty($row)) {
121 $rows[] = implode(', ', $row);
122 }
123 }
124 $fields_merge_tag_object->add('field_' . $field['key'] . '_raw_label', $field['key'], '{field:' . $field['key'] . ':raw:label}', str_replace(array('{', '}'), array('&#123;', '&#125;'), serialize($repeater_data_labels))); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
125 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', implode("\r\n", $rows));
126
127 $repeater_data = Ninja_Forms()->fieldsetRepeater->extractSubmissions($field['id'], $field['value'], $form_field->get_settings());
128 $rows = array();
129 foreach ($repeater_data as $repeater_key => $repeater) {
130 $row = array();
131 foreach ($repeater as $sub_repeater_key => $sub_repeater) {
132 $sub_value = is_array($sub_repeater['value']) ? implode(', ', $sub_repeater['value']) : $sub_repeater['value'];
133 $fields_merge_tag_object->add('field_' . $field['key'] . '_' . $repeater_key . '_' . $sub_repeater_key, $field['key'], '{field:' . $field['key'] . ':' . $repeater_key . '_' . $sub_repeater_key . '}', $sub_value);
134 $row[] = $sub_repeater['label'] . ': ' . $sub_value;
135 }
136 if (!empty($row)) {
137 $rows[] = implode(', ', $row);
138 }
139 }
140 $fields_merge_tag_object->add('field_' . $field['key'] . '_raw', $field['key'], '{field:' . $field['key'] . ':raw}', str_replace(array('{', '}'), array('&#123;', '&#125;'), serialize($repeater_data))); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
141 $fields_merge_tag_object->add('field_' . $field['key'], $field['key'], '{field:' . $field['key'] . '}', implode("\r\n", $rows));
142 break;
143 case 'listcountry':
144 case 'liststate':
145 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
146 $options = apply_filters('ninja_forms_render_options', $form_field->get_setting('options'), $form_field->get_settings());
147 $options = apply_filters('ninja_forms_render_options_' . $form_field->get_setting('type'), $options, $form_field->get_settings());
148 if (isset($field['value']) && $field['value']) {
149 $key = array_search(
150 $field['value'], array_map(
151 function ($v) {
152 return $v['value'];
153 }, $options
154 ),
155 false
156 );
157
158 $label = isset($options[$key]['label']) ? $options[$key]['label'] : '';
159 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', $label);
160 }
161 break;
162 case 'checkbox':
163 $form_field = Ninja_Forms()->form($this->get('item'))->get_field($field['id']);
164 $checked = '';
165 if (null == $form_field->get_setting('checked_value') && (1 == $field['value'] || 'on' == $field['value'])) {
166 $checked = esc_html__('Checked', 'ninja-forms');
167 } elseif (null == $form_field->get_setting('unchecked_value') && 0 == $field['value']) {
168 $checked = esc_html__('Unchecked', 'ninja-forms');
169 }
170 if ($checked == '') {
171 if (1 == $field['value'] || 'on' == $field['value']) {
172 $checked = $form_field->get_setting('checked_value');
173 } elseif ($form_field->get_setting('checked_value') != $field['value']) {
174 $checked = $form_field->get_setting('unchecked_value');
175 }
176 }
177 $fields_merge_tag_object->add('field_' . $field['key'] . '_label', $field['key'], '{field:' . $field['key'] . ':label}', $checked);
178 break;
179 case 'file_upload':
180 if (is_array($field['value'])) {
181 $fields_merge_tag_object->add('field_' . $field['key'], $field['key'], '{field:' . $field['key'] . '}', implode(', ', $field['value']));
182 }
183 break;
184 default:
185 break;
186 }
187 }
188 }
189 $fields_merge_tag_object->include_all_fields_merge_tags();
190 add_filter('ninja_forms_merge_tag_value_signature', [$this, 'filter_ninja_forms_merge_tag_value_signature'], 999);
191 foreach ($fields as $field) {
192 if (isset($field['type']) && ($field['type'] === 'repeater' || $field['type'] === 'file_upload')) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
193 } else {
194 // Fix PHP warnings
195 if (isset($field['type']) && isset($field['value'])) {
196 switch ($field['type']) {
197 case 'checkbox':
198 if (!isset($field['settings'])) {
199 $field['settings'] = array();
200 }
201 if (!isset($field['settings']['unchecked_value'])) {
202 $field['settings']['unchecked_value'] = '';
203 }
204 if (!isset($field['unchecked_calc_value'])) {
205 $field['unchecked_calc_value'] = '';
206 }
207 break;
208 default:
209 break;
210 }
211 }
212 $fields_merge_tag_object->add_field($field); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
213 }
214 }
215 remove_filter('ninja_forms_merge_tag_value_signature', [$this, 'filter_ninja_forms_merge_tag_value_signature'], 999);
216 }
217 }
218 }
219 break;
220 default:
221 break;
222 }
223 return true;
224 }
225
226 // get
227 public function get($key) {
228 if (isset($this->options->$key)) {
229 $value = $this->options->$key;
230 } else {
231 switch ($key) {
232 case 'args':
233 $value = array();
234 break;
235 default:
236 $value = false;
237 break;
238 }
239 }
240 return $value;
241 }
242
243 // items
244 public function items() {
245 $items = array();
246 if (function_exists('Ninja_Forms')) {
247 $forms = Ninja_Forms()->form()->get_forms();
248 if (!empty($forms)) {
249 foreach ($forms as $form) {
250 $items[] = $this->item($form->get_id());
251 }
252 }
253 }
254 return $items;
255 }
256
257 // item
258 public function item($item_id = false) {
259 $item_id = (int) $item_id;
260 if (!$item_id && $this->get('item')) {
261 $item_id = $this->get('item');
262 }
263 $form = false;
264 if (function_exists('Ninja_Forms')) {
265 $form = Ninja_Forms()->form($item_id)->get();
266 }
267 $item = new stdClass();
268 if ($form) {
269 $item->id = (string) $item_id;
270 $item->url = $this->helper->get_url(
271 array(
272 'page' => 'ninja-forms',
273 'form_id' => $item_id,
274 )
275 );
276 $item->name = $form->get_setting('title');
277 } else {
278 $item->id = '';
279 $item->url = '';
280 $item->name = '';
281 }
282 return $item;
283 }
284
285 // datasets
286 public function datasets($item_id = false, $name = false) {
287 $item_id = (int) $item_id;
288 $datasets = array();
289 if (function_exists('Ninja_Forms')) {
290 $entries = Ninja_Forms()->form($item_id)->get_subs();
291 if ($entries) {
292 $this->set('item', $item_id);
293 foreach ($entries as $key => $entry) {
294 $this->set('dataset', $entry->get_id());
295 $entry_title = $this->render($name);
296 if (!$entry_title) {
297 $entry_title = $entry->get_id();
298 }
299 $datasets[] = array(
300 'key' => $entry->get_id(),
301 'value' => $entry_title,
302 );
303 }
304 }
305 }
306 return $datasets;
307 }
308
309 // get dataset actions
310 public function get_dataset_actions($dataset_id = false) {
311 $dataset_id = (int) $dataset_id;
312 if (!$dataset_id) {
313 return false;
314 }
315 $actions = new stdClass();
316 $actions->view = $this->helper->get_url(
317 array(
318 'post' => $dataset_id,
319 'action' => 'edit',
320 ),
321 'post.php?'
322 );
323 $actions->delete = false;
324 return $actions;
325 }
326
327 // get template actions
328 public function get_template_actions($template = false) {
329 $template = (int) $template;
330 if (!$template) {
331 return;
332 }
333 $actions = new stdClass();
334 $actions->delete = false;
335 return $actions;
336 }
337
338 // render
339 public function render($value, $field = array(), $convert_shortcodes = true, $raw = false) {
340 $value = $this->render_shortcodes($value, $field);
341 if (!$raw) {
342 $value = $this->strip_shortcodes($value);
343 $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
344 $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
345 }
346 return $value;
347 }
348
349 // load actions
350 public function load_actions() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
351 }
352
353 // load filters
354 public function load_filters() {
355 add_filter('ninja_forms_run_action_settings', [$this, 'filter_ninja_forms_run_action_settings'], 10, 4);
356 add_filter('ninja_forms_post_run_action_type_save', [$this, 'filter_ninja_forms_post_run_action_type_save']);
357 add_filter('ninja_forms_post_run_action_type_email', [$this, 'filter_ninja_forms_post_run_action_type_email']);
358 add_filter('ninja_forms_action_email_attachments', [$this, 'filter_ninja_forms_action_email_attachments'], 10, 3);
359 add_filter('ninja_forms_action_email_message', [$this, 'filter_ninja_forms_action_email_message'], 10, 3);
360 // 1.25.14 trigger email action fix
361 add_filter('rest_dispatch_request', [$this, 'filter_rest_dispatch_request'], 10, 3);
362 }
363
364 // run action settings filter
365 public function filter_ninja_forms_run_action_settings($settings, $form_id, $action_id, $form_data) {
366 $type = isset($settings['type']) ? $settings['type'] : false;
367 if ($type == 'successmessage' && isset($settings['success_msg'])) {
368 $settings['success_msg'] = $this->filter_content($settings['success_msg'], $this->get('dataset'), false, 'message');
369 }
370 return $settings;
371 }
372
373 // post run action type save filter
374 public function filter_ninja_forms_post_run_action_type_save($data) {
375 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
376 if ($dataset_id) {
377 $this->set('dataset', $dataset_id);
378 }
379 return $data;
380 }
381
382 // post run action type email filter
383 public function filter_ninja_forms_post_run_action_type_email($data) {
384 $files = $this->helper->get('ninja_attachments');
385 if (is_array($files) && !empty($files)) {
386 foreach ($files as $key => $file) {
387 $this->helper->delete_dir(dirname($file) . '/');
388 }
389 $this->helper->deset('ninja_attachments');
390 }
391 return $data;
392 }
393
394 // email attachments filter
395 public function filter_ninja_forms_action_email_attachments($attachments, $data, $action_settings) {
396 $message = isset($action_settings['email_message']) ? $action_settings['email_message'] : '';
397 if (false !== strpos($message, '[')) {
398 $shortcode_tags = array(
399 'e2pdf-attachment',
400 'e2pdf-save',
401 );
402 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
403 $tagnames = array_intersect($shortcode_tags, $matches[1]);
404 if (!empty($tagnames)) {
405 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
406 foreach ($shortcodes[0] as $key => $shortcode_value) {
407 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
408 $atts = shortcode_parse_atts($shortcode[3]);
409 $file = false;
410 if (!isset($atts['dataset']) && isset($atts['id'])) {
411 $template = new Model_E2pdf_Template();
412 $template->load($atts['id']);
413 if ($template->get('extension') === 'ninja') {
414 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
415 if (!$dataset_id) {
416 $dataset_id = isset($action_settings['sub_id']) ? $action_settings['sub_id'] : false;
417 }
418 if ($dataset_id) {
419 $atts['dataset'] = $dataset_id;
420 $shortcode[3] .= ' dataset="' . $dataset_id . '"';
421 }
422 }
423 }
424 if (!isset($atts['apply'])) {
425 $shortcode[3] .= ' apply="true"';
426 }
427 if (!isset($atts['filter'])) {
428 $shortcode[3] .= ' filter="true"';
429 }
430 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
431 $file = do_shortcode_tag($shortcode);
432 if ($file) {
433 $tmp = false;
434 if (substr($file, 0, 4) === 'tmp:') {
435 $file = substr($file, 4);
436 $tmp = true;
437 }
438 if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
439 if ($tmp) {
440 $this->helper->add('ninja_attachments', $file);
441 }
442 } else {
443 $this->helper->add('ninja_attachments', $file);
444 }
445 $attachments[] = $file;
446 }
447 }
448 }
449 }
450 }
451 return $attachments;
452 }
453
454 // email message filter
455 public function filter_ninja_forms_action_email_message($message, $data, $action_settings) {
456 $dataset_id = isset($data['actions']['save']['sub_id']) ? $data['actions']['save']['sub_id'] : false;
457 if (!$dataset_id) {
458 $dataset_id = isset($action_settings['sub_id']) ? $action_settings['sub_id'] : false;
459 }
460 $message = $this->filter_content($message, $dataset_id, true);
461 return $message;
462 }
463
464 // rest dispatch request filter
465 public function filter_rest_dispatch_request($result, $request, $route) {
466 if ($route && false !== strpos($route, '/ninja-forms-submissions/email-action')) {
467 $data = json_decode($request->get_body());
468 if (!empty($data->submission) && !empty($data->action_settings)) {
469 $data->action_settings->sub_id = $data->submission;
470 $request->set_body(json_encode($data)); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
471 }
472 }
473 return $result;
474 }
475
476 // filter ninja forms signature merge tag
477 public function filter_ninja_forms_merge_tag_value_signature($value) {
478 if (empty($value)) {
479 return '';
480 }
481 if (!is_string($value)) {
482 return $value;
483 }
484 return strip_tags($value, '<img>');
485 }
486
487 // filter content
488 public function filter_content($message, $dataset_id, $filter, $type = '') {
489 if (false !== strpos($message, '[')) {
490 $shortcode_tags = array(
491 'e2pdf-download',
492 'e2pdf-save',
493 'e2pdf-attachment',
494 'e2pdf-view',
495 'e2pdf-adobesign',
496 'e2pdf-zapier',
497 );
498 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
499 $tagnames = array_intersect($shortcode_tags, $matches[1]);
500 if (!empty($tagnames)) {
501 preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
502 foreach ($shortcodes[0] as $key => $shortcode_value) {
503 $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
504 $atts = shortcode_parse_atts($shortcode[3]);
505 if (!isset($atts['dataset']) && isset($atts['id'])) {
506 $template = new Model_E2pdf_Template();
507 $template->load($atts['id']);
508 if ($template->get('extension') === 'ninja') {
509 $atts['dataset'] = $dataset_id;
510 $shortcode[3] .= ' dataset="' . $dataset_id . '"';
511 }
512 }
513 if (!isset($atts['apply'])) {
514 $shortcode[3] .= ' apply="true"';
515 }
516 if (!isset($atts['filter']) && $filter) {
517 $shortcode[3] .= ' filter="true"';
518 }
519 if (!isset($atts['iframe_download']) && $type == 'message') {
520 $shortcode[3] .= ' iframe_download="true"';
521 }
522 if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
523 $message = str_replace($shortcode_value, '', $message);
524 } else {
525 $message = str_replace($shortcode_value, do_shortcode_tag($shortcode), $message);
526 }
527 }
528 }
529 }
530 return $message;
531 }
532
533 // render shortcodes
534 public function render_shortcodes($value, $field = array()) {
535 $element_id = isset($field['element_id']) ? $field['element_id'] : false;
536 if ($this->verify()) {
537 if (false !== strpos($value, '[')) {
538 $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
539 $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
540 $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
541 }
542 $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
543 $value = apply_filters('ninja_forms_merge_tags', $value);
544 $value = $this->helper->load('field')->render(
545 apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
546 $this,
547 $field
548 );
549 }
550 return apply_filters(
551 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
552 );
553 }
554
555 // strip shortcodes
556 public function strip_shortcodes($value) {
557 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
558 $value = preg_replace('~a\:\d+\:{[^}]*}(*SKIP)(*FAIL)|{[^}]*}~', '', $value);
559 return $value;
560 }
561
562 // strip math
563 public function strip_math($value, &$replacements) {
564 if ($value) {
565 preg_match_all('/\{[^}]+\}/', $value, $matches);
566 foreach ($matches[0] as $match) {
567 $index = count($replacements) + 1;
568 $replacements[] = $match;
569 $value = str_replace($match, '%' . $index . '$s', $value);
570 }
571 }
572 return $value;
573 }
574
575 // convert shortcodes
576 public function convert_shortcodes($value, $to = false, $html = false) {
577 if ($value) {
578 if ($to) {
579 $search = array('&#91;', '&#93;', '&#091;', '&#093;', '&#123;', '&#125;');
580 $replace = array('[', ']', '[', ']', '{', '}');
581 $value = str_replace($search, $replace, $value);
582 if (!$html) {
583 $value = wp_specialchars_decode($value, ENT_QUOTES);
584 }
585 } else {
586 $search = array('[', ']', '&#091;', '&#093;');
587 $replace = array('&#91;', '&#93;', '&#91;', '&#93;');
588 $value = str_replace($search, $replace, $value);
589 }
590 }
591 return $value;
592 }
593
594 // auto
595 public function auto() {
596 $elements = array();
597 if ($this->get('cached_form')) {
598 $form_fields = Ninja_Forms()->form($this->get('item'))->get_fields();
599 $width = '100';
600 foreach ($form_fields as $form_field) {
601 $field = $form_field->get_settings();
602 switch ($field['type']) {
603 case 'repeater':
604 $elements[] = $this->auto_field(
605 $field,
606 array(
607 'type' => 'e2pdf-html',
608 'block' => true,
609 'float' => true,
610 'properties' => array(
611 'top' => '20',
612 'left' => '20',
613 'right' => '20',
614 'width' => $width . '%',
615 'height' => 'auto',
616 'value' => $field['label'],
617 ),
618 )
619 );
620
621 $elements[] = $this->auto_field(
622 $field,
623 array(
624 'type' => 'e2pdf-html',
625 'block' => true,
626 'float' => true,
627 'properties' => array(
628 'top' => '20',
629 'left' => '20',
630 'right' => '20',
631 'width' => $width . '%',
632 'height' => 'auto',
633 'value' => '<hr>',
634 ),
635 )
636 );
637 foreach ($field['fields'] as $sub_field) {
638 list($field_id, $sub_field_id) = explode('.', $sub_field['id']);
639 $sub_field['key'] = $field['key'] . ':0_' . $sub_field_id;
640 $elements = $this->auto_fields($elements, $sub_field);
641 }
642 $elements[] = $this->auto_field(
643 $field,
644 array(
645 'type' => 'e2pdf-html',
646 'block' => true,
647 'float' => true,
648 'properties' => array(
649 'top' => '20',
650 'left' => '20',
651 'right' => '20',
652 'width' => $width . '%',
653 'height' => 'auto',
654 'value' => '<hr>',
655 ),
656 )
657 );
658 break;
659 default:
660 $elements = $this->auto_fields($elements, $field);
661 break;
662 }
663 }
664 }
665
666 $response = array();
667 $response['page'] = array(
668 'bottom' => '20',
669 'top' => '20',
670 'left' => '20',
671 'right' => '20',
672 );
673
674 $response['elements'] = $elements;
675 return $response;
676 }
677
678 // auto fields
679 public function auto_fields($elements, $field) {
680 $width = '100';
681 switch ($field['type']) {
682 case 'textbox':
683 case 'number':
684 case 'date':
685 case 'city':
686 case 'email':
687 case 'firstname':
688 case 'lastname':
689 case 'phone':
690 case 'zip':
691 case 'hidden':
692 case 'starrating':
693 $elements[] = $this->auto_field(
694 $field,
695 array(
696 'type' => 'e2pdf-html',
697 'block' => true,
698 'float' => true,
699 'properties' => array(
700 'top' => '20',
701 'left' => '20',
702 'right' => '20',
703 'width' => $width . '%',
704 'height' => 'auto',
705 'value' => $field['label'],
706 ),
707 )
708 );
709
710 $elements[] = $this->auto_field(
711 $field,
712 array(
713 'type' => 'e2pdf-input',
714 'properties' => array(
715 'top' => '5',
716 'width' => '100%',
717 'height' => 'auto',
718 'value' => '{field:' . $field['key'] . '}',
719 ),
720 )
721 );
722 break;
723 case 'listselect':
724 case 'listmultiselect':
725 case 'listcountry':
726 case 'liststate':
727 if ($field['type'] == 'listcountry' || $field['type'] == 'liststate') {
728 $options = apply_filters('ninja_forms_render_options', $field['options'], $field);
729 $options = apply_filters('ninja_forms_render_options_' . $field['type'], $options, $field);
730 } else {
731 $options = $field['options'];
732 }
733
734 $elements[] = $this->auto_field(
735 $field,
736 array(
737 'type' => 'e2pdf-html',
738 'block' => true,
739 'float' => true,
740 'properties' => array(
741 'top' => '20',
742 'left' => '20',
743 'right' => '20',
744 'width' => $width . '%',
745 'height' => 'auto',
746 'value' => $field['label'],
747 ),
748 )
749 );
750 $options_tmp = array();
751 foreach ($options as $option) {
752 $options_tmp[] = $option['label'];
753 }
754
755 if ($field['type'] == 'listmultiselect') {
756 $elements[] = $this->auto_field(
757 $field,
758 array(
759 'type' => 'e2pdf-select',
760 'properties' => array(
761 'top' => '5',
762 'width' => '100%',
763 'height' => '44',
764 'multiline' => '1',
765 'options' => implode("\n", $options_tmp),
766 'value' => '{field:' . $field['key'] . ':label}',
767 ),
768 )
769 );
770 } else {
771 $elements[] = $this->auto_field(
772 $field,
773 array(
774 'type' => 'e2pdf-select',
775 'properties' => array(
776 'top' => '5',
777 'width' => '100%',
778 'height' => 'auto',
779 'options' => implode("\n", $options_tmp),
780 'value' => '{field:' . $field['key'] . ':label}',
781 ),
782 )
783 );
784 }
785 break;
786 case 'textarea':
787 case 'address':
788 case 'listimage':
789 case 'file_upload':
790 $elements[] = $this->auto_field(
791 $field,
792 array(
793 'type' => 'e2pdf-html',
794 'block' => true,
795 'float' => true,
796 'properties' => array(
797 'top' => '20',
798 'left' => '20',
799 'right' => '20',
800 'width' => $width . '%',
801 'height' => 'auto',
802 'value' => $field['label'],
803 ),
804 )
805 );
806
807 $elements[] = $this->auto_field(
808 $field,
809 array(
810 'type' => 'e2pdf-textarea',
811 'properties' => array(
812 'top' => '5',
813 'width' => '100%',
814 'height' => 'auto',
815 'value' => '{field:' . $field['key'] . '}',
816 ),
817 )
818 );
819 break;
820 case 'html':
821 $elements[] = $this->auto_field(
822 $field,
823 array(
824 'type' => 'e2pdf-html',
825 'block' => true,
826 'float' => true,
827 'properties' => array(
828 'top' => '20',
829 'left' => '20',
830 'right' => '20',
831 'width' => $width . '%',
832 'height' => 'auto',
833 'value' => $field['label'],
834 ),
835 )
836 );
837
838 $elements[] = $this->auto_field(
839 $field,
840 array(
841 'type' => 'e2pdf-html',
842 'properties' => array(
843 'top' => '5',
844 'width' => '100%',
845 'height' => 'auto',
846 'value' => $field['default'],
847 ),
848 )
849 );
850 break;
851 case 'listradio':
852 $elements[] = $this->auto_field(
853 $field,
854 array(
855 'type' => 'e2pdf-html',
856 'block' => true,
857 'float' => true,
858 'properties' => array(
859 'top' => '20',
860 'left' => '20',
861 'right' => '20',
862 'width' => $width . '%',
863 'height' => 'auto',
864 'value' => $field['label'],
865 ),
866 )
867 );
868
869 foreach ($field['options'] as $opt_key => $option) {
870 $elements[] = $this->auto_field(
871 $field,
872 array(
873 'type' => 'e2pdf-radio',
874 'properties' => array(
875 'top' => '5',
876 'width' => 'auto',
877 'height' => 'auto',
878 'value' => '{field:' . $field['key'] . ':label}',
879 'option' => $option['label'],
880 'group' => '{field:' . $field['key'] . '}',
881 ),
882 )
883 );
884 $elements[] = $this->auto_field(
885 $field,
886 array(
887 'type' => 'e2pdf-html',
888 'float' => true,
889 'properties' => array(
890 'left' => '5',
891 'width' => '100%',
892 'height' => 'auto',
893 'value' => $option['label'],
894 ),
895 )
896 );
897 }
898 break;
899 case 'listcheckbox':
900 case 'checkbox':
901 $elements[] = $this->auto_field(
902 $field,
903 array(
904 'type' => 'e2pdf-html',
905 'block' => true,
906 'float' => true,
907 'properties' => array(
908 'top' => '20',
909 'left' => '20',
910 'right' => '20',
911 'width' => $width . '%',
912 'height' => 'auto',
913 'value' => $field['label'],
914 ),
915 )
916 );
917
918 if ($field['type'] == 'checkbox') {
919 $elements[] = $this->auto_field(
920 $field,
921 array(
922 'type' => 'e2pdf-checkbox',
923 'properties' => array(
924 'top' => '5',
925 'width' => 'auto',
926 'height' => 'auto',
927 'value' => '{field:' . $field['key'] . ':label}',
928 'option' => !isset($field['checked_value']) ? esc_html__('Checked', 'ninja-forms') : $field['checked_value'],
929 ),
930 )
931 );
932 $elements[] = $this->auto_field(
933 $field,
934 array(
935 'type' => 'e2pdf-html',
936 'float' => true,
937 'properties' => array(
938 'left' => '5',
939 'width' => '100%',
940 'height' => 'auto',
941 'value' => $field['label'],
942 ),
943 )
944 );
945 } else {
946 foreach ($field['options'] as $opt_key => $option) {
947 $elements[] = $this->auto_field(
948 $field,
949 array(
950 'type' => 'e2pdf-checkbox',
951 'properties' => array(
952 'top' => '5',
953 'width' => 'auto',
954 'height' => 'auto',
955 'value' => '{field:' . $field['key'] . ':label}',
956 'option' => $option['label'],
957 ),
958 )
959 );
960 $elements[] = $this->auto_field(
961 $field,
962 array(
963 'type' => 'e2pdf-html',
964 'float' => true,
965 'properties' => array(
966 'left' => '5',
967 'width' => '100%',
968 'height' => 'auto',
969 'value' => $option['label'],
970 ),
971 )
972 );
973 }
974 }
975 break;
976 case 'signature':
977 $elements[] = $this->auto_field(
978 $field,
979 array(
980 'type' => 'e2pdf-html',
981 'block' => true,
982 'float' => true,
983 'properties' => array(
984 'top' => '20',
985 'left' => '20',
986 'right' => '20',
987 'width' => $width . '%',
988 'height' => 'auto',
989 'value' => $field['label'],
990 ),
991 )
992 );
993 $elements[] = $this->auto_field(
994 $field,
995 array(
996 'type' => 'e2pdf-signature',
997 'properties' => array(
998 'top' => '5',
999 'width' => '100%',
1000 'height' => '150',
1001 'dimension' => '1',
1002 'block_dimension' => '1',
1003 'value' => '{field:' . $field['key'] . '}',
1004 ),
1005 )
1006 );
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 return $elements;
1013 }
1014
1015 // auto field
1016 public function auto_field($field = false, $element = array()) {
1017 if (!$field) {
1018 return false;
1019 }
1020 return $element;
1021 }
1022
1023 // verify
1024 public function verify() {
1025 if ($this->get('cached_form') && $this->get('cached_entry')) {
1026 return true;
1027 }
1028 return false;
1029 }
1030
1031 // visual mapper
1032 public function visual_mapper() {
1033 $html = '';
1034 if ($this->get('item')) {
1035 $form = $this->auto();
1036 $html = '<div class="ninja-form">';
1037 $float = false;
1038 foreach ($form['elements'] as $element) {
1039 switch ($element['type']) {
1040 case 'e2pdf-html':
1041 $html .= '<div>' . $element['properties']['value'] . '</div>';
1042 if ($float) {
1043 $html .= '<div class="clearfix"></div>';
1044 $float = false;
1045 }
1046 break;
1047 case 'e2pdf-input':
1048 $html .= '<div><input type="text" name="' . $element['properties']['value'] . '"></div>';
1049 break;
1050 case 'e2pdf-image':
1051 case 'e2pdf-signature':
1052 case 'e2pdf-textarea':
1053 $html .= '<div><textarea name="' . $element['properties']['value'] . '"></textarea></div>';
1054 break;
1055 case 'e2pdf-select':
1056 $html .= '<div>';
1057 $html .= '<select name="' . $element['properties']['value'] . '">';
1058 $options = explode("\n", $element['properties']['options']);
1059 foreach ($options as $option) {
1060 $html .= '<option>' . $option . '</option>';
1061 }
1062 $html .= '</select>';
1063 $html .= '</div>';
1064 break;
1065 case 'e2pdf-checkbox':
1066 $html .= '<div class="ninja-checkbox"><input type="checkbox" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
1067 $float = true;
1068 break;
1069 case 'e2pdf-radio':
1070 $html .= '<div class="ninja-radio"><input type="radio" name="' . $element['properties']['value'] . '" value="' . $element['properties']['option'] . '"></div>';
1071 $float = true;
1072 break;
1073 default:
1074 break;
1075 }
1076 }
1077 $html .= '</div>';
1078 return $html;
1079 }
1080 return false;
1081 }
1082
1083 // load shortcodes
1084 public function load_shortcodes() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
1085 }
1086
1087 // styles
1088 public function styles($item_id = false) {
1089 $styles = array();
1090 $styles[] = plugins_url('css/extension/ninja.css?v=' . time(), $this->helper->get('plugin_file_path'));
1091 return $styles;
1092 }
1093 }
1094