PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7.2
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
← All changes | classes/models/FrmEntryFormatter.php +58 -118 6.276.7.2 View file →
@@ -8,24 +8,21 @@
8 8 */
9 9 class FrmEntryFormatter {
10 10
11 11 /**
12 + * @var stdClass
12 13 * @since 2.04
13 - *
14 - * @var stdClass|null
15 14 */
16 - protected $entry;
15 + protected $entry = null;
17 16
18 17 /**
19 - * @var FrmEntryValues|null
20 - *
18 + * @var FrmEntryValues
21 19 * @since 2.04
22 20 */
23 - protected $entry_values;
21 + protected $entry_values = null;
24 22
25 23 /**
26 24 * @var bool
27 - *
28 25 * @since 2.04
29 26 */
30 27 protected $is_plain_text = false;
31 28
@@ -30,9 +27,8 @@
30 27 protected $is_plain_text = false;
31 28
32 29 /**
33 30 * @var bool
34 - *
35 31 * @since 2.04
36 32 */
37 33 protected $include_user_info = false;
38 34
@@ -37,9 +33,8 @@
37 33 protected $include_user_info = false;
38 34
39 35 /**
40 36 * @var bool
41 - *
42 37 * @since 2.04
43 38 */
44 39 protected $include_blank = false;
45 40
@@ -44,9 +39,8 @@
44 39 protected $include_blank = false;
45 40
46 41 /**
47 42 * @var string
48 - *
49 43 * @since 2.04
50 44 */
51 45 protected $format = 'text';
52 46
@@ -51,9 +45,8 @@
51 45 protected $format = 'text';
52 46
53 47 /**
54 48 * @var string
55 - *
56 49 * @since 2.05
57 50 */
58 51 protected $array_key = 'key';
59 52
@@ -58,23 +51,20 @@
58 51 protected $array_key = 'key';
59 52
60 53 /**
61 54 * @var string
62 - *
63 55 * @since 2.04
64 56 */
65 57 protected $direction = 'ltr';
66 58
67 59 /**
68 - * @var FrmTableHTMLGenerator|null
69 - *
60 + * @var FrmTableHTMLGenerator
70 61 * @since 2.04
71 62 */
72 - protected $table_generator;
63 + protected $table_generator = null;
73 64
74 65 /**
75 66 * @var bool
76 - *
77 67 * @since 2.04
78 68 */
79 69 protected $is_clickable = false;
80 70
@@ -79,9 +69,8 @@
79 69 protected $is_clickable = false;
80 70
81 71 /**
82 72 * @var array
83 - *
84 73 * @since 2.04
85 74 */
86 75 protected $include_extras = array();
87 76
@@ -86,9 +75,8 @@
86 75 protected $include_extras = array();
87 76
88 77 /**
89 78 * @var array
90 - *
91 79 * @since 3.0
92 80 */
93 81 protected $single_cell_fields = array();
94 82
@@ -93,9 +81,8 @@
93 81 protected $single_cell_fields = array();
94 82
95 83 /**
96 84 * @var array
97 - *
98 85 * @since 3.0
99 86 */
100 87 protected $atts = array();
101 88
@@ -136,14 +123,17 @@
136 123 *
137 124 * @since 2.04
138 125 *
139 126 * @param array $atts
140 - *
141 - * @return void
142 127 */
143 128 protected function init_entry( $atts ) {
144 129 if ( isset( $atts['entry'] ) && is_object( $atts['entry'] ) ) {
145 - $this->entry = isset( $atts['entry']->metas ) ? $atts['entry'] : FrmEntry::getOne( $atts['entry']->id, true );
130 +
131 + if ( isset( $atts['entry']->metas ) ) {
132 + $this->entry = $atts['entry'];
133 + } else {
134 + $this->entry = FrmEntry::getOne( $atts['entry']->id, true );
135 + }
146 136 } elseif ( ! empty( $atts['id'] ) ) {
147 137 $this->entry = FrmEntry::getOne( $atts['id'], true );
148 138 }
149 139 }
@@ -153,10 +143,8 @@
153 143 *
154 144 * @since 2.04
155 145 *
156 146 * @param array $atts
157 - *
158 - * @return void
159 147 */
160 148 protected function init_entry_values( $atts ) {
161 149 $entry_atts = $this->prepare_entry_attributes( $atts );
162 150 $this->entry_values = new FrmEntryValues( $this->entry->id, $entry_atts );
@@ -171,11 +159,11 @@
171 159 *
172 160 * @return array
173 161 */
174 162 protected function prepare_entry_attributes( $atts ) {
175 - $entry_atts = array();
163 + $entry_atts = array();
164 +
176 165 $conditionally_add = array( 'include_fields', 'fields', 'exclude_fields', 'entry' );
177 -
178 166 foreach ( $conditionally_add as $index ) {
179 167 if ( isset( $atts[ $index ] ) ) {
180 168 $entry_atts[ $index ] = $atts[ $index ];
181 169 }
@@ -189,18 +177,25 @@
189 177 *
190 178 * @since 2.04
191 179 *
192 180 * @param array $atts
193 - *
194 - * @return void
195 181 */
196 182 protected function init_format( $atts ) {
197 183 if ( $atts['format'] === 'array' ) {
184 +
198 185 $this->format = 'array';
186 +
199 187 } elseif ( $atts['format'] === 'json' ) {
188 +
200 189 $this->format = 'json';
190 +
201 191 } elseif ( $atts['format'] === 'text' ) {
202 - $this->format = $this->is_plain_text === true ? 'plain_text_block' : 'table';
192 +
193 + if ( $this->is_plain_text === true ) {
194 + $this->format = 'plain_text_block';
195 + } else {
196 + $this->format = 'table';
197 + }
203 198 }
204 199
205 200 /**
206 201 * Allows modifying the format property of FrmEntryFormatter object.
@@ -226,13 +221,11 @@
226 221 *
227 222 * @since 2.05
228 223 *
229 224 * @param array $atts
230 - *
231 - * @return void
232 225 */
233 226 protected function init_array_key( $atts ) {
234 - if ( isset( $atts['array_key'] ) && $atts['array_key'] === 'id' ) {
227 + if ( isset( $atts['array_key'] ) && $atts['array_key'] == 'id' ) {
235 228 $this->array_key = 'id';
236 229 }
237 230 }
238 231
@@ -241,13 +234,11 @@
241 234 *
242 235 * @since 2.04
243 236 *
244 237 * @param array $atts
245 - *
246 - * @return void
247 238 */
248 239 protected function init_is_plain_text( $atts ) {
249 - if ( ! empty( $atts['plain_text'] ) ) {
240 + if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
250 241 $this->is_plain_text = true;
251 242 } elseif ( $atts['format'] !== 'text' ) {
252 243 $this->is_plain_text = true;
253 244 }
@@ -258,13 +249,11 @@
258 249 *
259 250 * @since 2.04
260 251 *
261 252 * @param array $atts
262 - *
263 - * @return void
264 253 */
265 254 protected function init_include_blank( $atts ) {
266 - if ( ! empty( $atts['include_blank'] ) ) {
255 + if ( isset( $atts['include_blank'] ) && $atts['include_blank'] ) {
267 256 $this->include_blank = true;
268 257 }
269 258 }
270 259
@@ -273,10 +262,8 @@
273 262 *
274 263 * @since 2.04
275 264 *
276 265 * @param array $atts
277 - *
278 - * @return void
279 266 */
280 267 protected function init_direction( $atts ) {
281 268 if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
282 269 $this->direction = 'rtl';
@@ -288,13 +275,11 @@
288 275 *
289 276 * @since 2.04
290 277 *
291 278 * @param array $atts
292 - *
293 - * @return void
294 279 */
295 280 protected function init_include_user_info( $atts ) {
296 - if ( ! empty( $atts['user_info'] ) ) {
281 + if ( isset( $atts['user_info'] ) && $atts['user_info'] ) {
297 282 $this->include_user_info = true;
298 283 }
299 284 }
300 285
@@ -301,13 +286,11 @@
301 286 /**
302 287 * Which fields to skip by default
303 288 *
304 289 * @since 3.0
305 - *
306 - * @return array
307 290 */
308 291 protected function skip_fields() {
309 - return array( 'captcha', 'html', FrmSubmitHelper::FIELD_TYPE );
292 + return array( 'captcha', 'html' );
310 293 }
311 294
312 295 /**
313 296 * Set the include_extras property
@@ -314,13 +297,11 @@
314 297 *
315 298 * @since 3.0
316 299 *
317 300 * @param array $atts
318 - *
319 - * @return void
320 301 */
321 302 protected function init_include_extras( $atts ) {
322 - if ( ! empty( $atts['include_extras'] ) ) {
303 + if ( isset( $atts['include_extras'] ) && $atts['include_extras'] ) {
323 304 $this->include_extras = array_map( 'strtolower', array_map( 'trim', explode( ',', $atts['include_extras'] ) ) );
324 305 }
325 306 }
326 307
@@ -327,10 +308,8 @@
327 308 /**
328 309 * Initialize the single_cell_fields property
329 310 *
330 311 * @since 3.0
331 - *
332 - * @return void
333 312 */
334 313 protected function init_single_cell_fields() {
335 314 $this->single_cell_fields = array( 'html' );
336 315 }
@@ -340,10 +319,8 @@
340 319 *
341 320 * @since 2.04
342 321 *
343 322 * @param array $atts
344 - *
345 - * @return void
346 323 */
347 324 protected function init_table_generator( $atts ) {
348 325 $this->table_generator = new FrmTableHTMLGenerator( 'entry', $atts );
349 326 }
@@ -353,13 +330,11 @@
353 330 *
354 331 * @since 2.04
355 332 *
356 333 * @param array $atts
357 - *
358 - * @return void
359 334 */
360 335 protected function init_is_clickable( $atts ) {
361 - if ( ! empty( $atts['clickable'] ) ) {
336 + if ( isset( $atts['clickable'] ) && $atts['clickable'] ) {
362 337 $this->is_clickable = true;
363 338 }
364 339 }
365 340
@@ -367,12 +342,8 @@
367 342 * Save the passed atts for other calls. Exclude some attributes to prevent
368 343 * interaction with processing field values like time format.
369 344 *
370 345 * @since 3.0
371 - *
372 - * @param array $atts
373 - *
374 - * @return void
375 346 */
376 347 protected function init_atts( $atts ) {
377 348 $atts['source'] = 'entry_formatter';
378 349 $atts['wpautop'] = false;
@@ -378,9 +349,8 @@
378 349 $atts['wpautop'] = false;
379 350 $atts['return_array'] = true;
380 351
381 352 $unset = array( 'id', 'form_id', 'format' );
382 -
383 353 foreach ( $unset as $param ) {
384 354 if ( isset( $atts[ $param ] ) ) {
385 355 unset( $atts[ $param ] );
386 356 }
@@ -396,12 +366,12 @@
396 366 * @since 2.05
397 367 *
398 368 * @param FrmFieldValue $field_value
399 369 *
400 - * @return int|string
370 + * @return string|int
401 371 */
402 372 protected function get_key_or_id( $field_value ) {
403 - return $this->array_key === 'key' ? $field_value->get_field_key() : $field_value->get_field_id();
373 + return $this->array_key == 'key' ? $field_value->get_field_key() : $field_value->get_field_id();
404 374 }
405 375
406 376 /**
407 377 * Package and return the formatted entry values
@@ -478,13 +448,12 @@
478 448 *
479 449 * @since 2.05
480 450 *
481 451 * @param string $content
482 - *
483 - * @return void
484 452 */
485 453 protected function add_field_values_to_content( &$content ) {
486 - foreach ( $this->entry_values->get_field_values() as $field_value ) {
454 + foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
455 +
487 456 /**
488 457 * @var FrmFieldValue $field_value
489 458 */
490 459 $field_value->prepare_displayed_value( $this->atts );
@@ -529,10 +498,8 @@
529 498 * @since 2.04
530 499 *
531 500 * @param array $field_values
532 501 * @param array $output
533 - *
534 - * @return void
535 502 */
536 503 protected function push_field_values_to_array( $field_values, &$output ) {
537 504 foreach ( $field_values as $field_value ) {
538 505 /**
@@ -549,17 +516,17 @@
549 516 * @since 2.04
550 517 *
551 518 * @param FrmFieldValue $field_value
552 519 * @param array $output
553 - *
554 - * @return void
555 520 */
556 521 protected function push_single_field_to_array( $field_value, &$output ) {
557 522 if ( $this->include_field_in_content( $field_value ) ) {
558 - $displayed_value = $this->prepare_display_value_for_array( $field_value->get_displayed_value() );
523 +
524 + $displayed_value = $this->prepare_display_value_for_array( $field_value->get_displayed_value() );
525 +
559 526 $output[ $this->get_key_or_id( $field_value ) ] = $displayed_value;
560 - $has_separate_value = (bool) $field_value->get_field_option( 'separate_value' );
561 527
528 + $has_separate_value = (bool) $field_value->get_field_option( 'separate_value' );
562 529 if ( $has_separate_value || $displayed_value !== $field_value->get_saved_value() ) {
563 530 $output[ $this->get_key_or_id( $field_value ) . '-value' ] = $field_value->get_saved_value();
564 531 }
565 532 }
@@ -572,15 +539,13 @@
572 539 *
573 540 * @param string $label
574 541 * @param mixed $display_value
575 542 * @param string $content
576 - *
577 - * @return void
578 543 */
579 544 protected function add_plain_text_row( $label, $display_value, &$content ) {
580 545 $display_value = $this->prepare_display_value_for_plain_text_content( $display_value );
581 546
582 - if ( 'rtl' === $this->direction ) {
547 + if ( 'rtl' == $this->direction ) {
583 548 $content .= wp_kses_post( $display_value . ' :' . $label ) . "\r\n";
584 549 } else {
585 550 $content .= wp_kses_post( $label . ': ' . $display_value ) . "\r\n";
586 551 }
@@ -592,10 +557,8 @@
592 557 * @since 2.04
593 558 *
594 559 * @param FrmFieldValue $field_value
595 560 * @param string $content
596 - *
597 - * @return void
598 561 */
599 562 protected function add_field_value_to_content( $field_value, &$content ) {
600 563 if ( $this->is_extra_field( $field_value ) ) {
601 564 $this->add_row_for_extra_field( $field_value, $content );
@@ -611,10 +574,8 @@
611 574 * @since 3.0
612 575 *
613 576 * @param FrmFieldValue $field_value
614 577 * @param string $content
615 - *
616 - * @return void
617 578 */
618 579 protected function add_row_for_extra_field( $field_value, &$content ) {
619 580 if ( ! $this->include_field_in_content( $field_value ) ) {
620 581 return;
@@ -633,10 +594,8 @@
633 594 * @since 3.0
634 595 *
635 596 * @param FrmFieldValue $field_value
636 597 * @param string $content
637 - *
638 - * @return void
639 598 */
640 599 protected function add_row_for_standard_field( $field_value, &$content ) {
641 600 if ( ! $this->include_field_in_content( $field_value ) ) {
642 601 return;
@@ -656,15 +615,13 @@
656 615 * @since 3.0
657 616 *
658 617 * @param FrmFieldValue $field_value
659 618 * @param string $content
660 - *
661 - * @return void
662 619 */
663 620 protected function add_html_row_for_included_extra( $field_value, &$content ) {
664 621 $this->prepare_html_display_value_for_extra_fields( $field_value, $display_value );
665 622
666 - if ( in_array( $field_value->get_field_type(), $this->single_cell_fields, true ) ) {
623 + if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
667 624 $this->add_single_cell_html_row( $display_value, $content );
668 625 } else {
669 626 $value_args = $this->package_value_args( $field_value );
670 627 $this->add_html_row( $value_args, $content );
@@ -677,15 +634,13 @@
677 634 * @since 3.0
678 635 *
679 636 * @param FrmFieldValue $field_value
680 637 * @param string $content
681 - *
682 - * @return void
683 638 */
684 639 protected function add_plain_text_row_for_included_extra( $field_value, &$content ) {
685 640 $this->prepare_plain_text_display_value_for_extra_fields( $field_value, $display_value );
686 641
687 - if ( in_array( $field_value->get_field_type(), $this->single_cell_fields, true ) ) {
642 + if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) {
688 643 $this->add_single_value_plain_text_row( $display_value, $content );
689 644 } else {
690 645 $this->add_plain_text_row( $field_value->get_field_label(), $display_value, $content );
691 646 }
@@ -697,10 +652,8 @@
697 652 * @since 3.0
698 653 *
699 654 * @param string $display_value
700 655 * @param string $content
701 - *
702 - * @return void
703 656 */
704 657 protected function add_single_cell_html_row( $display_value, &$content ) {
705 658 // TODO: maybe move to FrmFieldValue
706 659 $display_value = $this->prepare_display_value_for_html_table( $display_value );
@@ -714,10 +667,8 @@
714 667 * @since 3.0
715 668 *
716 669 * @param string $display_value
717 670 * @param string $content
718 - *
719 - * @return void
720 671 */
721 672 protected function add_single_value_plain_text_row( $display_value, &$content ) {
722 673 $content .= $this->prepare_display_value_for_plain_text_content( $display_value );
723 674 }
@@ -728,10 +679,8 @@
728 679 * @since 3.0
729 680 *
730 681 * @param FrmFieldValue $field_value
731 682 * @param mixed $display_value
732 - *
733 - * @return void
734 683 */
735 684 protected function prepare_html_display_value_for_extra_fields( $field_value, &$display_value ) {
736 685 $display_value = $field_value->get_displayed_value();
737 686 }
@@ -742,10 +691,8 @@
742 691 * @since 3.0
743 692 *
744 693 * @param FrmFieldValue $field_value
745 694 * @param mixed $display_value
746 - *
747 - * @return void
748 695 */
749 696 protected function prepare_plain_text_display_value_for_extra_fields( $field_value, &$display_value ) {
750 697 $display_value = $field_value->get_displayed_value() . "\r\n";
751 698 }
@@ -756,10 +703,8 @@
756 703 * @since 2.04
757 704 *
758 705 * @param FrmFieldValue $field_value
759 706 * @param string $content
760 - *
761 - * @return void
762 707 */
763 708 protected function add_standard_row( $field_value, &$content ) {
764 709 if ( $this->format === 'plain_text_block' ) {
765 710 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
@@ -791,14 +736,14 @@
791 736 *
792 737 * @since 2.04
793 738 *
794 739 * @param string $content
795 - *
796 - * @return void
797 740 */
798 741 protected function add_user_info_to_html_table( &$content ) {
799 742 if ( $this->include_user_info ) {
743 +
800 744 foreach ( $this->entry_values->get_user_info() as $user_info ) {
745 +
801 746 $value_args = array(
802 747 'label' => $user_info['label'],
803 748 'value' => $user_info['value'],
804 749 'field_type' => 'none',
@@ -814,13 +759,12 @@
814 759 *
815 760 * @since 2.04
816 761 *
817 762 * @param string $content
818 - *
819 - * @return void
820 763 */
821 764 protected function add_user_info_to_plain_text_content( &$content ) {
822 765 if ( $this->include_user_info ) {
766 +
823 767 foreach ( $this->entry_values->get_user_info() as $user_info ) {
824 768 $this->add_plain_text_row( $user_info['label'], $user_info['value'], $content );
825 769 }
826 770 }
@@ -856,9 +800,9 @@
856 800 *
857 801 * @return bool
858 802 */
859 803 protected function is_extra_field( $field_value ) {
860 - return in_array( $field_value->get_field_type(), $this->skip_fields(), true );
804 + return in_array( $field_value->get_field_type(), $this->skip_fields() );
861 805 }
862 806
863 807 /**
864 808 * Check if an extra field is included
@@ -869,9 +813,9 @@
869 813 *
870 814 * @return bool
871 815 */
872 816 protected function is_extra_field_included( $field_value ) {
873 - return in_array( $field_value->get_field_type(), $this->include_extras, true );
817 + return in_array( $field_value->get_field_type(), $this->include_extras );
874 818 }
875 819
876 820 /**
877 821 * Add a row in an HTML table
@@ -884,19 +828,13 @@
884 828 * 'value' => (mixed) The value to add. Required
885 829 * 'field_type' => (string) The field type. Blank string if not a field.
886 830 * ]
887 831 * @param string $content
888 - *
889 - * @return void
890 832 */
891 833 protected function add_html_row( $value_args, &$content ) {
892 834 $display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] );
893 835
894 - $content .= $this->table_generator->generate_two_cell_table_row(
895 - $value_args['label'],
896 - $display_value,
897 - array( 'field_type' => $value_args['field_type'] )
898 - );
836 + $content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value );
899 837 }
900 838
901 839 /**
902 840 * Prepare the displayed value for an array
@@ -922,9 +860,8 @@
922 860 * @return mixed|string
923 861 */
924 862 protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) {
925 863 $display_value = $this->flatten_array( $display_value );
926 -
927 864 if ( ! isset( $this->atts['line_breaks'] ) || ! empty( $this->atts['line_breaks'] ) ) {
928 865 $display_value = str_replace( array( "\r\n", "\n" ), '<br/>', $display_value );
929 866 }
930 867
@@ -937,13 +874,15 @@
937 874 * @since 2.04
938 875 *
939 876 * @param mixed $display_value
940 877 *
941 - * @return int|string
878 + * @return string|int
942 879 */
943 880 protected function prepare_display_value_for_plain_text_content( $display_value ) {
944 881 $display_value = $this->flatten_array( $display_value );
945 - return $this->strip_html( $display_value );
882 + $display_value = $this->strip_html( $display_value );
883 +
884 + return $display_value;
946 885 }
947 886
948 887 /**
949 888 * Flatten an array
@@ -949,15 +888,15 @@
949 888 * Flatten an array
950 889 *
951 890 * @since 2.04
952 891 *
953 - * @param array|int|string $value
892 + * @param array|string|int $value
954 893 *
955 - * @return int|string
894 + * @return string|int
956 895 */
957 896 protected function flatten_array( $value ) {
958 897 if ( is_array( $value ) ) {
959 - $separator = $this->atts['array_separator'] ?? ', ';
898 + $separator = isset( $this->atts['array_separator'] ) ? $this->atts['array_separator'] : ', ';
960 899 $value = implode( $separator, $value );
961 900 }
962 901
963 902 return $value;
@@ -972,19 +911,20 @@
972 911 *
973 912 * @return mixed
974 913 */
975 914 protected function strip_html( $value ) {
915 +
976 916 if ( $this->is_plain_text ) {
917 +
977 918 if ( is_array( $value ) ) {
978 919 foreach ( $value as $key => $single_value ) {
979 920 $value[ $key ] = $this->strip_html( $single_value );
980 921 }
981 922 } elseif ( $this->is_plain_text && ! is_array( $value ) ) {
982 - if ( str_contains( $value, '<img' ) ) {
923 + if ( strpos( $value, '<img' ) !== false ) {
983 924 $value = str_replace( array( '<img', 'src=', '/>', '"' ), '', $value );
984 925 $value = trim( $value );
985 926 }
986 -
987 927 $value = strip_tags( $value );
988 928 }
989 929 }
990 930