PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
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 3.06.06, at classes/models/FrmEntryFormatter.php

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