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

895 lines 19.9 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 ( isset( $atts['entry'] ) && 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 } elseif ( ! empty( $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', 'entry' );
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 } elseif ( $atts['format'] === 'json' ) {
185
186 $this->format = 'json';
187
188 } elseif ( $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 } elseif ( $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' );
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 } elseif ( $this->format === 'array' ) {
371 $content = $this->prepare_array();
372
373 } elseif ( $this->format === 'table' ) {
374 $content = $this->prepare_html_table();
375
376 } elseif ( $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
488 $output[ $this->get_key_or_id( $field_value ) ] = $displayed_value;
489
490 $has_separate_value = (bool) $field_value->get_field_option( 'separate_value' );
491 if ( $has_separate_value || $displayed_value !== $field_value->get_saved_value() ) {
492 $output[ $this->get_key_or_id( $field_value ) . '-value' ] = $field_value->get_saved_value();
493 }
494 }
495 }
496
497 /**
498 * Add a row of values to the plain text content
499 *
500 * @since 2.04
501 *
502 * @param string $label
503 * @param mixed $display_value
504 * @param string $content
505 */
506 protected function add_plain_text_row( $label, $display_value, &$content ) {
507 $display_value = $this->prepare_display_value_for_plain_text_content( $display_value );
508
509 if ( 'rtl' == $this->direction ) {
510 $content .= wp_kses_post( $display_value . ' :' . $label ) . "\r\n";
511 } else {
512 $content .= wp_kses_post( $label . ': ' . $display_value ) . "\r\n";
513 }
514 }
515
516 /**
517 * Add a field value to the HTML table or plain text content
518 *
519 * @since 2.04
520 *
521 * @param FrmFieldValue $field_value
522 * @param string $content
523 */
524 protected function add_field_value_to_content( $field_value, &$content ) {
525 if ( $this->is_extra_field( $field_value ) ) {
526 $this->add_row_for_extra_field( $field_value, $content );
527
528 } else {
529 $this->add_row_for_standard_field( $field_value, $content );
530 }
531 }
532
533 /**
534 * Add an extra field to plain text or html table content
535 *
536 * @since 3.0
537 *
538 * @param FrmFieldValue $field_value
539 * @param string $content
540 */
541 protected function add_row_for_extra_field( $field_value, &$content ) {
542 if ( ! $this->include_field_in_content( $field_value ) ) {
543 return;
544 }
545
546 if ( $this->format === 'plain_text_block' ) {
547 $this->add_plain_text_row_for_included_extra( $field_value, $content );
548 } elseif ( $this->format === 'table' ) {
549 $this->add_html_row_for_included_extra( $field_value, $content );
550 }
551 }
552
553 /**
554 * Add a standard row to plain text or html table content
555 *
556 * @since 3.0
557 *
558 * @param FrmFieldValue $field_value
559 * @param string $content
560 */
561 protected function add_row_for_standard_field( $field_value, &$content ) {
562 if ( ! $this->include_field_in_content( $field_value ) ) {
563 return;
564 }
565
566 if ( $this->format === 'plain_text_block' ) {
567 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
568 } elseif ( $this->format === 'table' ) {
569 $value_args = $this->package_value_args( $field_value );
570 $this->add_html_row( $value_args, $content );
571 }
572 }
573
574 /**
575 * Add a row to table for included extra
576 *
577 * @since 3.0
578 *
579 * @param FrmFieldValue $field_value
580 * @param string $content
581 */
582 protected function add_html_row_for_included_extra( $field_value, &$content ) {
583 $this->prepare_html_display_value_for_extra_fields( $field_value, $display_value );
584
585 if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
586 $this->add_single_cell_html_row( $display_value, $content );
587 } else {
588 $value_args = $this->package_value_args( $field_value );
589 $this->add_html_row( $value_args, $content );
590 }
591 }
592
593 /**
594 * Add a plain text row for included extra
595 *
596 * @since 3.0
597 *
598 * @param FrmFieldValue $field_value
599 * @param string $content
600 */
601 protected function add_plain_text_row_for_included_extra( $field_value, &$content ) {
602 $this->prepare_plain_text_display_value_for_extra_fields( $field_value, $display_value );
603
604 if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
605 $this->add_single_value_plain_text_row( $display_value, $content );
606 } else {
607 $this->add_plain_text_row( $field_value->get_field_label(), $display_value, $content );
608 }
609 }
610
611 /**
612 * Add a single cell row to an HTML table
613 *
614 * @since 3.0
615 *
616 * @param string $display_value
617 * @param string $content
618 */
619 protected function add_single_cell_html_row( $display_value, &$content ) {
620 // TODO: maybe move to FrmFieldValue
621 $display_value = $this->prepare_display_value_for_html_table( $display_value );
622
623 $content .= $this->table_generator->generate_single_cell_table_row( $display_value );
624 }
625
626 /**
627 * Add a single value plain text row
628 *
629 * @since 3.0
630 *
631 * @param string $display_value
632 * @param string $content
633 */
634 protected function add_single_value_plain_text_row( $display_value, &$content ) {
635 $content .= $this->prepare_display_value_for_plain_text_content( $display_value );
636 }
637
638 /**
639 * Prepare the display value for extra fields an HTML table
640 *
641 * @since 3.0
642 *
643 * @param FrmFieldValue $field_value
644 * @param mixed $display_value
645 */
646 protected function prepare_html_display_value_for_extra_fields( $field_value, &$display_value ) {
647 $display_value = $field_value->get_displayed_value();
648 }
649
650 /**
651 * Prepare a plain text value for extra fields
652 *
653 * @since 3.0
654 *
655 * @param FrmFieldValue $field_value
656 * @param mixed $display_value
657 */
658 protected function prepare_plain_text_display_value_for_extra_fields( $field_value, &$display_value ) {
659 $display_value = $field_value->get_displayed_value() . "\r\n";
660 }
661
662 /**
663 * Add a standard row to plain text or html table content
664 *
665 * @since 2.04
666 *
667 * @param FrmProFieldValue $field_value
668 * @param string $content
669 */
670 protected function add_standard_row( $field_value, &$content ) {
671 if ( $this->format === 'plain_text_block' ) {
672 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
673 } elseif ( $this->format === 'table' ) {
674 $value_args = $this->package_value_args( $field_value );
675 $this->add_html_row( $value_args, $content );
676 }
677 }
678
679 /**
680 * Package the value arguments for an HTML row
681 *
682 * @since 2.04
683 *
684 * @param FrmFieldValue $field_value
685 *
686 * @return array
687 */
688 protected function package_value_args( $field_value ) {
689 return array(
690 'label' => $field_value->get_field_label(),
691 'value' => $field_value->get_displayed_value(),
692 'field_type' => $field_value->get_field_type(),
693 );
694 }
695
696 /**
697 * Add user info to an HTML table
698 *
699 * @since 2.04
700 *
701 * @param string $content
702 */
703 protected function add_user_info_to_html_table( &$content ) {
704 if ( $this->include_user_info ) {
705
706 foreach ( $this->entry_values->get_user_info() as $user_info ) {
707
708 $value_args = array(
709 'label' => $user_info['label'],
710 'value' => $user_info['value'],
711 'field_type' => 'none',
712 );
713
714 $this->add_html_row( $value_args, $content );
715 }
716 }
717 }
718
719 /**
720 * Add user info to plain text content
721 *
722 * @since 2.04
723 *
724 * @param string $content
725 */
726 protected function add_user_info_to_plain_text_content( &$content ) {
727 if ( $this->include_user_info ) {
728
729 foreach ( $this->entry_values->get_user_info() as $user_info ) {
730 $this->add_plain_text_row( $user_info['label'], $user_info['value'], $content );
731 }
732 }
733 }
734
735 /**
736 * Check if a field should be included in the content
737 *
738 * @since 2.04
739 *
740 * @param FrmFieldValue $field_value
741 *
742 * @return bool
743 */
744 protected function include_field_in_content( $field_value ) {
745 $include = true;
746
747 if ( $this->is_extra_field( $field_value ) ) {
748 $include = $this->is_extra_field_included( $field_value );
749 } elseif ( FrmAppHelper::is_empty_value( $field_value->get_displayed_value() ) && ! $this->include_blank ) {
750 $include = false;
751 }
752
753 return apply_filters( 'frm_include_field_in_content', $include, $field_value );
754 }
755
756 /**
757 * Check if a field is normally a skipped type
758 *
759 * @since 2.04
760 *
761 * @param FrmFieldValue $field_value
762 *
763 * @return bool
764 */
765 protected function is_extra_field( $field_value ) {
766 return in_array( $field_value->get_field_type(), $this->skip_fields() );
767 }
768
769 /**
770 * Check if an extra field is included
771 *
772 * @since 2.04
773 *
774 * @param FrmFieldValue $field_value
775 *
776 * @return bool
777 */
778 protected function is_extra_field_included( $field_value ) {
779 return in_array( $field_value->get_field_type(), $this->include_extras );
780 }
781
782 /**
783 * Add a row in an HTML table
784 *
785 * @since 2.04
786 *
787 * @param array $value_args
788 * $value_args = [
789 * 'label' => (string) The label. Required
790 * 'value' => (mixed) The value to add. Required
791 * 'field_type' => (string) The field type. Blank string if not a field.
792 * ]
793 * @param string $content
794 */
795 protected function add_html_row( $value_args, &$content ) {
796 $display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] );
797
798 $content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value );
799 }
800
801 /**
802 * Prepare the displayed value for an array
803 *
804 * @since 2.04
805 *
806 * @param mixed $value
807 *
808 * @return mixed|string
809 */
810 protected function prepare_display_value_for_array( $value ) {
811 return $this->strip_html( $value );
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 if ( ! isset( $this->atts['line_breaks'] ) || ! empty( $this->atts['line_breaks'] ) ) {
827 $display_value = str_replace( array( "\r\n", "\n" ), '<br/>', $display_value );
828 }
829
830 return $display_value;
831 }
832
833 /**
834 * Prepare a field's display value for plain text content
835 *
836 * @since 2.04
837 *
838 * @param mixed $display_value
839 *
840 * @return string|int
841 */
842 protected function prepare_display_value_for_plain_text_content( $display_value ) {
843 $display_value = $this->flatten_array( $display_value );
844 $display_value = $this->strip_html( $display_value );
845
846 return $display_value;
847 }
848
849 /**
850 * Flatten an array
851 *
852 * @since 2.04
853 *
854 * @param array|string|int $value
855 *
856 * @return string|int
857 */
858 protected function flatten_array( $value ) {
859 if ( is_array( $value ) ) {
860 $value = implode( ', ', $value );
861 }
862
863 return $value;
864 }
865
866 /**
867 * Strip HTML if from email value if plain text is selected
868 *
869 * @since 2.0.21
870 *
871 * @param mixed $value
872 *
873 * @return mixed
874 */
875 protected function strip_html( $value ) {
876
877 if ( $this->is_plain_text ) {
878
879 if ( is_array( $value ) ) {
880 foreach ( $value as $key => $single_value ) {
881 $value[ $key ] = $this->strip_html( $single_value );
882 }
883 } elseif ( $this->is_plain_text && ! is_array( $value ) ) {
884 if ( strpos( $value, '<img' ) !== false ) {
885 $value = str_replace( array( '<img', 'src=', '/>', '"' ), '', $value );
886 $value = trim( $value );
887 }
888 $value = strip_tags( $value );
889 }
890 }
891
892 return $value;
893 }
894 }
895