PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.14
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.14
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmEntryFormatter.php

FrmEntryFormatter.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 5.0.14, at classes/models/FrmEntryFormatter.php

899 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @since 2.04
8 */
9 class FrmEntryFormatter {
10
11 /**
12 * @var stdClass
13 * @since 2.04
14 */
15 protected $entry = null;
16
17 /**
18 * @var FrmEntryValues
19 * @since 2.04
20 */
21 protected $entry_values = null;
22
23 /**
24 * @var bool
25 * @since 2.04
26 */
27 protected $is_plain_text = false;
28
29 /**
30 * @var bool
31 * @since 2.04
32 */
33 protected $include_user_info = false;
34
35 /**
36 * @var bool
37 * @since 2.04
38 */
39 protected $include_blank = false;
40
41 /**
42 * @var string
43 * @since 2.04
44 */
45 protected $format = 'text';
46
47 /**
48 * @var string
49 * @since 2.05
50 */
51 protected $array_key = 'key';
52
53 /**
54 * @var string
55 * @since 2.04
56 */
57 protected $direction = 'ltr';
58
59 /**
60 * @var FrmTableHTMLGenerator
61 * @since 2.04
62 */
63 protected $table_generator = null;
64
65 /**
66 * @var bool
67 * @since 2.04
68 */
69 protected $is_clickable = false;
70
71 /**
72 * @var array
73 * @since 2.04
74 */
75 protected $include_extras = array();
76
77 /**
78 * @var array
79 * @since 3.0
80 */
81 protected $single_cell_fields = array();
82
83 /**
84 * @var array
85 * @since 3.0
86 */
87 protected $atts = array();
88
89 /**
90 * FrmEntryFormat constructor
91 *
92 * @since 2.04
93 *
94 * @param $atts
95 */
96 public function __construct( $atts ) {
97 $this->init_entry( $atts );
98
99 if ( $this->entry === null || $this->entry === false ) {
100 return;
101 }
102
103 $this->init_is_plain_text( $atts );
104 $this->init_format( $atts );
105 $this->init_array_key( $atts );
106 $this->init_include_blank( $atts );
107 $this->init_direction( $atts );
108 $this->init_include_user_info( $atts );
109 $this->init_include_extras( $atts );
110 $this->init_single_cell_fields();
111 $this->init_entry_values( $atts );
112
113 if ( $this->format === 'table' ) {
114 $this->init_table_generator( $atts );
115 $this->init_is_clickable( $atts );
116 }
117
118 $this->init_atts( $atts );
119 }
120
121 /**
122 * Set the entry property
123 *
124 * @since 2.04
125 *
126 * @param array $atts
127 */
128 protected function init_entry( $atts ) {
129 if ( isset( $atts['entry'] ) && is_object( $atts['entry'] ) ) {
130
131 if ( isset( $atts['entry']->metas ) ) {
132 $this->entry = $atts['entry'];
133 } else {
134 $this->entry = FrmEntry::getOne( $atts['entry']->id, true );
135 }
136 } elseif ( ! empty( $atts['id'] ) ) {
137 $this->entry = FrmEntry::getOne( $atts['id'], true );
138 }
139 }
140
141 /**
142 * Set the entry values property
143 *
144 * @since 2.04
145 *
146 * @param array $atts
147 */
148 protected function init_entry_values( $atts ) {
149 $entry_atts = $this->prepare_entry_attributes( $atts );
150 $this->entry_values = new FrmEntryValues( $this->entry->id, $entry_atts );
151 }
152
153 /**
154 * Prepare attributes array for FrmEntryValues constructor
155 *
156 * @since 2.05
157 *
158 * @param array $atts
159 *
160 * @return array
161 */
162 protected function prepare_entry_attributes( $atts ) {
163 $entry_atts = array();
164
165 $conditionally_add = array( 'include_fields', 'fields', 'exclude_fields', 'entry' );
166 foreach ( $conditionally_add as $index ) {
167 if ( isset( $atts[ $index ] ) ) {
168 $entry_atts[ $index ] = $atts[ $index ];
169 }
170 }
171
172 return $entry_atts;
173 }
174
175 /**
176 * Set the format property
177 *
178 * @since 2.04
179 *
180 * @param array $atts
181 */
182 protected function init_format( $atts ) {
183 if ( $atts['format'] === 'array' ) {
184
185 $this->format = 'array';
186
187 } elseif ( $atts['format'] === 'json' ) {
188
189 $this->format = 'json';
190
191 } elseif ( $atts['format'] === 'text' ) {
192
193 if ( $this->is_plain_text === true ) {
194 $this->format = 'plain_text_block';
195 } else {
196 $this->format = 'table';
197 }
198 }
199 }
200
201 /**
202 * Set the array_key property that sets whether the keys in the
203 * returned array are field keys or ids
204 *
205 * @since 2.05
206 *
207 * @param array $atts
208 */
209 protected function init_array_key( $atts ) {
210 if ( isset( $atts['array_key'] ) && $atts['array_key'] == 'id' ) {
211 $this->array_key = 'id';
212 }
213 }
214
215 /**
216 * Set the is_plain_text property
217 *
218 * @since 2.04
219 *
220 * @param array $atts
221 */
222 protected function init_is_plain_text( $atts ) {
223 if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
224 $this->is_plain_text = true;
225 } elseif ( $atts['format'] !== 'text' ) {
226 $this->is_plain_text = true;
227 }
228 }
229
230 /**
231 * Set the include_blank property
232 *
233 * @since 2.04
234 *
235 * @param array $atts
236 */
237 protected function init_include_blank( $atts ) {
238 if ( isset( $atts['include_blank'] ) && $atts['include_blank'] ) {
239 $this->include_blank = true;
240 }
241 }
242
243 /**
244 * Set the direction property
245 *
246 * @since 2.04
247 *
248 * @param array $atts
249 */
250 protected function init_direction( $atts ) {
251 if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
252 $this->direction = 'rtl';
253 }
254 }
255
256 /**
257 * Set the include_user_info property
258 *
259 * @since 2.04
260 *
261 * @param array $atts
262 */
263 protected function init_include_user_info( $atts ) {
264 if ( isset( $atts['user_info'] ) && $atts['user_info'] ) {
265 $this->include_user_info = true;
266 }
267 }
268
269 /**
270 * Which fields to skip by default
271 *
272 * @since 3.0
273 */
274 protected function skip_fields() {
275 return array( 'captcha', 'html' );
276 }
277
278 /**
279 * Set the include_extras property
280 *
281 * @since 3.0
282 *
283 * @param array $atts
284 */
285 protected function init_include_extras( $atts ) {
286 if ( isset( $atts['include_extras'] ) && $atts['include_extras'] ) {
287 $this->include_extras = array_map( 'strtolower', array_map( 'trim', explode( ',', $atts['include_extras'] ) ) );
288 }
289 }
290
291 /**
292 * Initialize the single_cell_fields property
293 *
294 * @since 3.0
295 */
296 protected function init_single_cell_fields() {
297 $this->single_cell_fields = array( 'html' );
298 }
299
300 /**
301 * Set the table_generator property
302 *
303 * @since 2.04
304 *
305 * @param array $atts
306 */
307 protected function init_table_generator( $atts ) {
308 $this->table_generator = new FrmTableHTMLGenerator( 'entry', $atts );
309 }
310
311 /**
312 * Set the is_clickable property
313 *
314 * @since 2.04
315 *
316 * @param array $atts
317 */
318 protected function init_is_clickable( $atts ) {
319 if ( isset( $atts['clickable'] ) && $atts['clickable'] ) {
320 $this->is_clickable = true;
321 }
322 }
323
324 /**
325 * Save the passed atts for other calls. Exclude some attributes to prevent
326 * interaction with processing field values like time format.
327 *
328 * @since 3.0
329 */
330 protected function init_atts( $atts ) {
331 $atts['source'] = 'entry_formatter';
332 $atts['wpautop'] = false;
333 $atts['return_array'] = true;
334
335 $unset = array( 'id', 'entry', 'form_id', 'format' );
336 foreach ( $unset as $param ) {
337 if ( isset( $atts[ $param ] ) ) {
338 unset( $atts[ $param ] );
339 }
340 }
341
342 $this->atts = $atts;
343 }
344
345 /**
346 * Get the field key or ID, depending on array_key property
347 *
348 * @since 2.05
349 *
350 * @param FrmFieldValue $field_value
351 *
352 * @return string|int
353 */
354 protected function get_key_or_id( $field_value ) {
355 return $this->array_key == 'key' ? $field_value->get_field_key() : $field_value->get_field_id();
356 }
357
358 /**
359 * Package and return the formatted entry values
360 *
361 * @since 2.04
362 *
363 * @return array|string
364 */
365 public function get_formatted_entry_values() {
366 if ( $this->entry === null || $this->entry === false ) {
367 return '';
368 }
369
370 if ( $this->format === 'json' ) {
371 $content = json_encode( $this->prepare_array() );
372
373 } elseif ( $this->format === 'array' ) {
374 $content = $this->prepare_array();
375
376 } elseif ( $this->format === 'table' ) {
377 $content = $this->prepare_html_table();
378
379 } elseif ( $this->format === 'plain_text_block' ) {
380 $content = $this->prepare_plain_text_block();
381
382 } else {
383 $content = '';
384 }
385
386 return $content;
387 }
388
389 /**
390 * Return the formatted HTML table with entry values
391 *
392 * @since 2.04
393 *
394 * @return string
395 */
396 protected function prepare_html_table() {
397 $content = $this->table_generator->generate_table_header();
398
399 $this->add_field_values_to_content( $content );
400 $this->add_user_info_to_html_table( $content );
401
402 $content .= $this->table_generator->generate_table_footer();
403
404 if ( $this->is_clickable ) {
405 $content = make_clickable( $content );
406 }
407
408 return $content;
409 }
410
411 /**
412 * Add field values to table or plain text content
413 *
414 * @since 2.05
415 *
416 * @param string $content
417 */
418 protected function add_field_values_to_content( &$content ) {
419 foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
420
421 /**
422 * @var FrmFieldValue $field_value
423 */
424 $field_value->prepare_displayed_value( $this->atts );
425 $this->add_field_value_to_content( $field_value, $content );
426 }
427 }
428
429 /**
430 * Return the formatted plain text content
431 *
432 * @since 2.04
433 *
434 * @return string
435 */
436 protected function prepare_plain_text_block() {
437 $content = '';
438
439 $this->add_field_values_to_content( $content );
440 $this->add_user_info_to_plain_text_content( $content );
441
442 return $content;
443 }
444
445 /**
446 * Prepare the array output
447 *
448 * @since 2.04
449 *
450 * @return array
451 */
452 protected function prepare_array() {
453 $array_output = array();
454
455 $this->push_field_values_to_array( $this->entry_values->get_field_values(), $array_output );
456
457 return $array_output;
458 }
459
460 /**
461 * Push field values to array content
462 *
463 * @since 2.04
464 *
465 * @param array $field_values
466 * @param array $output
467 */
468 protected function push_field_values_to_array( $field_values, &$output ) {
469 foreach ( $field_values as $field_value ) {
470 /**
471 * @var FrmFieldValue $field_value
472 */
473 $field_value->prepare_displayed_value( $this->atts );
474 $this->push_single_field_to_array( $field_value, $output );
475 }
476 }
477
478 /**
479 * Push a single field to the array content
480 *
481 * @since 2.04
482 *
483 * @param FrmFieldValue $field_value
484 * @param array $output
485 */
486 protected function push_single_field_to_array( $field_value, &$output ) {
487 if ( $this->include_field_in_content( $field_value ) ) {
488
489 $displayed_value = $this->prepare_display_value_for_array( $field_value->get_displayed_value() );
490
491 $output[ $this->get_key_or_id( $field_value ) ] = $displayed_value;
492
493 $has_separate_value = (bool) $field_value->get_field_option( 'separate_value' );
494 if ( $has_separate_value || $displayed_value !== $field_value->get_saved_value() ) {
495 $output[ $this->get_key_or_id( $field_value ) . '-value' ] = $field_value->get_saved_value();
496 }
497 }
498 }
499
500 /**
501 * Add a row of values to the plain text content
502 *
503 * @since 2.04
504 *
505 * @param string $label
506 * @param mixed $display_value
507 * @param string $content
508 */
509 protected function add_plain_text_row( $label, $display_value, &$content ) {
510 $display_value = $this->prepare_display_value_for_plain_text_content( $display_value );
511
512 if ( 'rtl' == $this->direction ) {
513 $content .= wp_kses_post( $display_value . ' :' . $label ) . "\r\n";
514 } else {
515 $content .= wp_kses_post( $label . ': ' . $display_value ) . "\r\n";
516 }
517 }
518
519 /**
520 * Add a field value to the HTML table or plain text content
521 *
522 * @since 2.04
523 *
524 * @param FrmFieldValue $field_value
525 * @param string $content
526 */
527 protected function add_field_value_to_content( $field_value, &$content ) {
528 if ( $this->is_extra_field( $field_value ) ) {
529 $this->add_row_for_extra_field( $field_value, $content );
530
531 } else {
532 $this->add_row_for_standard_field( $field_value, $content );
533 }
534 }
535
536 /**
537 * Add an extra field to plain text or html table content
538 *
539 * @since 3.0
540 *
541 * @param FrmFieldValue $field_value
542 * @param string $content
543 */
544 protected function add_row_for_extra_field( $field_value, &$content ) {
545 if ( ! $this->include_field_in_content( $field_value ) ) {
546 return;
547 }
548
549 if ( $this->format === 'plain_text_block' ) {
550 $this->add_plain_text_row_for_included_extra( $field_value, $content );
551 } elseif ( $this->format === 'table' ) {
552 $this->add_html_row_for_included_extra( $field_value, $content );
553 }
554 }
555
556 /**
557 * Add a standard row to plain text or html table content
558 *
559 * @since 3.0
560 *
561 * @param FrmFieldValue $field_value
562 * @param string $content
563 */
564 protected function add_row_for_standard_field( $field_value, &$content ) {
565 if ( ! $this->include_field_in_content( $field_value ) ) {
566 return;
567 }
568
569 if ( $this->format === 'plain_text_block' ) {
570 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
571 } elseif ( $this->format === 'table' ) {
572 $value_args = $this->package_value_args( $field_value );
573 $this->add_html_row( $value_args, $content );
574 }
575 }
576
577 /**
578 * Add a row to table for included extra
579 *
580 * @since 3.0
581 *
582 * @param FrmFieldValue $field_value
583 * @param string $content
584 */
585 protected function add_html_row_for_included_extra( $field_value, &$content ) {
586 $this->prepare_html_display_value_for_extra_fields( $field_value, $display_value );
587
588 if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
589 $this->add_single_cell_html_row( $display_value, $content );
590 } else {
591 $value_args = $this->package_value_args( $field_value );
592 $this->add_html_row( $value_args, $content );
593 }
594 }
595
596 /**
597 * Add a plain text row for included extra
598 *
599 * @since 3.0
600 *
601 * @param FrmFieldValue $field_value
602 * @param string $content
603 */
604 protected function add_plain_text_row_for_included_extra( $field_value, &$content ) {
605 $this->prepare_plain_text_display_value_for_extra_fields( $field_value, $display_value );
606
607 if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
608 $this->add_single_value_plain_text_row( $display_value, $content );
609 } else {
610 $this->add_plain_text_row( $field_value->get_field_label(), $display_value, $content );
611 }
612 }
613
614 /**
615 * Add a single cell row to an HTML table
616 *
617 * @since 3.0
618 *
619 * @param string $display_value
620 * @param string $content
621 */
622 protected function add_single_cell_html_row( $display_value, &$content ) {
623 // TODO: maybe move to FrmFieldValue
624 $display_value = $this->prepare_display_value_for_html_table( $display_value );
625
626 $content .= $this->table_generator->generate_single_cell_table_row( $display_value );
627 }
628
629 /**
630 * Add a single value plain text row
631 *
632 * @since 3.0
633 *
634 * @param string $display_value
635 * @param string $content
636 */
637 protected function add_single_value_plain_text_row( $display_value, &$content ) {
638 $content .= $this->prepare_display_value_for_plain_text_content( $display_value );
639 }
640
641 /**
642 * Prepare the display value for extra fields an HTML table
643 *
644 * @since 3.0
645 *
646 * @param FrmFieldValue $field_value
647 * @param mixed $display_value
648 */
649 protected function prepare_html_display_value_for_extra_fields( $field_value, &$display_value ) {
650 $display_value = $field_value->get_displayed_value();
651 }
652
653 /**
654 * Prepare a plain text value for extra fields
655 *
656 * @since 3.0
657 *
658 * @param FrmFieldValue $field_value
659 * @param mixed $display_value
660 */
661 protected function prepare_plain_text_display_value_for_extra_fields( $field_value, &$display_value ) {
662 $display_value = $field_value->get_displayed_value() . "\r\n";
663 }
664
665 /**
666 * Add a standard row to plain text or html table content
667 *
668 * @since 2.04
669 *
670 * @param FrmProFieldValue $field_value
671 * @param string $content
672 */
673 protected function add_standard_row( $field_value, &$content ) {
674 if ( $this->format === 'plain_text_block' ) {
675 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
676 } elseif ( $this->format === 'table' ) {
677 $value_args = $this->package_value_args( $field_value );
678 $this->add_html_row( $value_args, $content );
679 }
680 }
681
682 /**
683 * Package the value arguments for an HTML row
684 *
685 * @since 2.04
686 *
687 * @param FrmFieldValue $field_value
688 *
689 * @return array
690 */
691 protected function package_value_args( $field_value ) {
692 return array(
693 'label' => $field_value->get_field_label(),
694 'value' => $field_value->get_displayed_value(),
695 'field_type' => $field_value->get_field_type(),
696 );
697 }
698
699 /**
700 * Add user info to an HTML table
701 *
702 * @since 2.04
703 *
704 * @param string $content
705 */
706 protected function add_user_info_to_html_table( &$content ) {
707 if ( $this->include_user_info ) {
708
709 foreach ( $this->entry_values->get_user_info() as $user_info ) {
710
711 $value_args = array(
712 'label' => $user_info['label'],
713 'value' => $user_info['value'],
714 'field_type' => 'none',
715 );
716
717 $this->add_html_row( $value_args, $content );
718 }
719 }
720 }
721
722 /**
723 * Add user info to plain text content
724 *
725 * @since 2.04
726 *
727 * @param string $content
728 */
729 protected function add_user_info_to_plain_text_content( &$content ) {
730 if ( $this->include_user_info ) {
731
732 foreach ( $this->entry_values->get_user_info() as $user_info ) {
733 $this->add_plain_text_row( $user_info['label'], $user_info['value'], $content );
734 }
735 }
736 }
737
738 /**
739 * Check if a field should be included in the content
740 *
741 * @since 2.04
742 *
743 * @param FrmFieldValue $field_value
744 *
745 * @return bool
746 */
747 protected function include_field_in_content( $field_value ) {
748 $include = true;
749
750 if ( $this->is_extra_field( $field_value ) ) {
751 $include = $this->is_extra_field_included( $field_value );
752 } elseif ( FrmAppHelper::is_empty_value( $field_value->get_displayed_value() ) && ! $this->include_blank ) {
753 $include = false;
754 }
755
756 return apply_filters( 'frm_include_field_in_content', $include, $field_value );
757 }
758
759 /**
760 * Check if a field is normally a skipped type
761 *
762 * @since 2.04
763 *
764 * @param FrmFieldValue $field_value
765 *
766 * @return bool
767 */
768 protected function is_extra_field( $field_value ) {
769 return in_array( $field_value->get_field_type(), $this->skip_fields() );
770 }
771
772 /**
773 * Check if an extra field is included
774 *
775 * @since 2.04
776 *
777 * @param FrmFieldValue $field_value
778 *
779 * @return bool
780 */
781 protected function is_extra_field_included( $field_value ) {
782 return in_array( $field_value->get_field_type(), $this->include_extras );
783 }
784
785 /**
786 * Add a row in an HTML table
787 *
788 * @since 2.04
789 *
790 * @param array $value_args
791 * $value_args = [
792 * 'label' => (string) The label. Required
793 * 'value' => (mixed) The value to add. Required
794 * 'field_type' => (string) The field type. Blank string if not a field.
795 * ]
796 * @param string $content
797 */
798 protected function add_html_row( $value_args, &$content ) {
799 $display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] );
800
801 $content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value );
802 }
803
804 /**
805 * Prepare the displayed value for an array
806 *
807 * @since 2.04
808 *
809 * @param mixed $value
810 *
811 * @return mixed|string
812 */
813 protected function prepare_display_value_for_array( $value ) {
814 return $this->strip_html( $value );
815 }
816
817 /**
818 * Prepare a field's display value for an HTML table
819 *
820 * @since 2.04
821 *
822 * @param mixed $display_value
823 * @param string $field_type
824 *
825 * @return mixed|string
826 */
827 protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) {
828 $display_value = $this->flatten_array( $display_value );
829 if ( ! isset( $this->atts['line_breaks'] ) || ! empty( $this->atts['line_breaks'] ) ) {
830 $display_value = str_replace( array( "\r\n", "\n" ), '<br/>', $display_value );
831 }
832
833 return $display_value;
834 }
835
836 /**
837 * Prepare a field's display value for plain text content
838 *
839 * @since 2.04
840 *
841 * @param mixed $display_value
842 *
843 * @return string|int
844 */
845 protected function prepare_display_value_for_plain_text_content( $display_value ) {
846 $display_value = $this->flatten_array( $display_value );
847 $display_value = $this->strip_html( $display_value );
848
849 return $display_value;
850 }
851
852 /**
853 * Flatten an array
854 *
855 * @since 2.04
856 *
857 * @param array|string|int $value
858 *
859 * @return string|int
860 */
861 protected function flatten_array( $value ) {
862 if ( is_array( $value ) ) {
863 $separator = isset( $this->atts['array_separator'] ) ? $this->atts['array_separator'] : ', ';
864 $value = implode( $separator, $value );
865 }
866
867 return $value;
868 }
869
870 /**
871 * Strip HTML if from email value if plain text is selected
872 *
873 * @since 2.0.21
874 *
875 * @param mixed $value
876 *
877 * @return mixed
878 */
879 protected function strip_html( $value ) {
880
881 if ( $this->is_plain_text ) {
882
883 if ( is_array( $value ) ) {
884 foreach ( $value as $key => $single_value ) {
885 $value[ $key ] = $this->strip_html( $single_value );
886 }
887 } elseif ( $this->is_plain_text && ! is_array( $value ) ) {
888 if ( strpos( $value, '<img' ) !== false ) {
889 $value = str_replace( array( '<img', 'src=', '/>', '"' ), '', $value );
890 $value = trim( $value );
891 }
892 $value = strip_tags( $value );
893 }
894 }
895
896 return $value;
897 }
898 }
899