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

659 lines 13.5 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 2.04
77 */
78 protected $skip_fields = array( 'captcha', 'html' );
79
80 /**
81 * FrmEntryFormat constructor
82 *
83 * @since 2.04
84 *
85 * @param $atts
86 */
87 public function __construct( $atts ) {
88 $this->init_entry( $atts );
89
90 if ( $this->entry === null || $this->entry === false ) {
91 return;
92 }
93
94 $this->init_is_plain_text( $atts );
95 $this->init_format( $atts );
96 $this->init_array_key( $atts );
97 $this->init_include_blank( $atts );
98 $this->init_direction( $atts );
99 $this->init_include_user_info( $atts );
100 $this->init_entry_values( $atts );
101
102 if ( $this->format === 'table' ) {
103 $this->init_table_generator( $atts );
104 $this->init_is_clickable( $atts );
105 }
106 }
107
108 /**
109 * Set the entry property
110 *
111 * @since 2.04
112 *
113 * @param array $atts
114 */
115 protected function init_entry( $atts ) {
116 if ( is_object( $atts['entry'] ) ) {
117
118 if ( isset( $atts['entry']->metas ) ) {
119 $this->entry = $atts['entry'];
120 } else {
121 $this->entry = FrmEntry::getOne( $atts['entry']->id, true );
122 }
123 } else if ( $atts['id'] ) {
124 $this->entry = FrmEntry::getOne( $atts['id'], true );
125 }
126 }
127
128 /**
129 * Set the entry values property
130 *
131 * @since 2.04
132 *
133 * @param array $atts
134 */
135 protected function init_entry_values( $atts ) {
136 $atts['source'] = 'entry_formatter';
137 $this->entry_values = new FrmEntryValues( $this->entry->id, $atts );
138 }
139
140 /**
141 * Set the format property
142 *
143 * @since 2.04
144 *
145 * @param array $atts
146 */
147 protected function init_format( $atts ) {
148 if ( $atts['format'] === 'array' ) {
149
150 $this->format = 'array';
151
152 } else if ( $atts['format'] === 'json' ) {
153
154 $this->format = 'json';
155
156 } else if ( $atts['format'] === 'text' ) {
157
158 if ( $this->is_plain_text === true ) {
159 $this->format = 'plain_text_block';
160 } else {
161 $this->format = 'table';
162 }
163 }
164 }
165
166 /**
167 * Set the array_key property that sets whether the keys in the
168 * returned array are field keys or ids
169 *
170 * @since 2.05
171 *
172 * @param array $atts
173 */
174 protected function init_array_key( $atts ) {
175 if ( isset( $atts['array_key'] ) && $atts['array_key'] == 'id' ) {
176 $this->array_key = 'id';
177 }
178 }
179
180 /**
181 * Set the is_plain_text property
182 *
183 * @since 2.04
184 *
185 * @param array $atts
186 */
187 protected function init_is_plain_text( $atts ) {
188 if ( isset( $atts['plain_text'] ) && $atts['plain_text'] ) {
189 $this->is_plain_text = true;
190 } else if ( $atts['format'] !== 'text' ) {
191 $this->is_plain_text = true;
192 }
193 }
194
195 /**
196 * Set the include_blank property
197 *
198 * @since 2.04
199 *
200 * @param array $atts
201 */
202 protected function init_include_blank( $atts ) {
203 if ( isset( $atts['include_blank'] ) && $atts['include_blank'] ) {
204 $this->include_blank = true;
205 }
206 }
207
208 /**
209 * Set the direction property
210 *
211 * @since 2.04
212 *
213 * @param array $atts
214 */
215 protected function init_direction( $atts ) {
216 if ( isset( $atts['direction'] ) && $atts['direction'] === 'rtl' ) {
217 $this->direction = 'rtl';
218 }
219 }
220
221 /**
222 * Set the include_user_info property
223 *
224 * @since 2.04
225 *
226 * @param array $atts
227 */
228 protected function init_include_user_info( $atts ) {
229 if ( isset( $atts['user_info'] ) && $atts['user_info'] ) {
230 $this->include_user_info = true;
231 }
232 }
233
234 /**
235 * Set the table_generator property
236 *
237 * @since 2.04
238 *
239 * @param array $atts
240 */
241 protected function init_table_generator( $atts ) {
242 $this->table_generator = new FrmTableHTMLGenerator( 'entry', $atts );
243 }
244
245 /**
246 * Set the is_clickable property
247 *
248 * @since 2.04
249 *
250 * @param array $atts
251 */
252 protected function init_is_clickable( $atts ) {
253 if ( isset( $atts['clickable'] ) && $atts['clickable'] ) {
254 $this->is_clickable = true;
255 }
256 }
257
258 /**
259 * Get the field key or ID, depending on array_key property
260 *
261 * @since 2.05
262 *
263 * @param FrmFieldValue $field_value
264 *
265 * @return string|int
266 */
267 protected function get_key_or_id( $field_value ) {
268 return $this->array_key == 'key' ? $field_value->get_field_key() : $field_value->get_field_id();
269 }
270
271 /**
272 * Package and return the formatted entry values
273 *
274 * @since 2.04
275 *
276 * @return array|string
277 */
278 public function get_formatted_entry_values() {
279 if ( $this->entry === null || $this->entry === false ) {
280 return '';
281 }
282
283 if ( $this->format === 'json' ) {
284 $content = json_encode( $this->prepare_array() );
285
286 } else if ( $this->format === 'array' ) {
287 $content = $this->prepare_array();
288
289 } else if ( $this->format === 'table' ) {
290 $content = $this->prepare_html_table();
291
292 } else if ( $this->format === 'plain_text_block' ) {
293 $content = $this->prepare_plain_text_block();
294
295 } else {
296 $content = '';
297 }
298
299 return $content;
300 }
301
302 /**
303 * Return the formatted HTML table with entry values
304 *
305 * @since 2.04
306 *
307 * @return string
308 */
309 protected function prepare_html_table() {
310 $content = $this->table_generator->generate_table_header();
311
312 foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
313 $this->add_field_value_to_content( $field_value, $content );
314 }
315
316 $this->add_user_info_to_html_table( $content );
317
318 $content .= $this->table_generator->generate_table_footer();
319
320 if ( $this->is_clickable ) {
321 $content = make_clickable( $content );
322 }
323
324 return $content;
325 }
326
327 /**
328 * Return the formatted plain text content
329 *
330 * @since 2.04
331 *
332 * @return string
333 */
334 protected function prepare_plain_text_block() {
335 $content = '';
336
337 foreach ( $this->entry_values->get_field_values() as $field_id => $field_value ) {
338 $this->add_field_value_to_content( $field_value, $content );
339 }
340
341 $this->add_user_info_to_plain_text_content( $content );
342
343 return $content;
344 }
345
346 /**
347 * Prepare the array output
348 *
349 * @since 2.04
350 *
351 * @return array
352 */
353 protected function prepare_array() {
354 $array_output = array();
355
356 $this->push_field_values_to_array( $this->entry_values->get_field_values(), $array_output );
357
358 return $array_output;
359 }
360
361 /**
362 * Push field values to array content
363 *
364 * @since 2.04
365 *
366 * @param array $field_values
367 * @param array $output
368 */
369 protected function push_field_values_to_array( $field_values, &$output ) {
370 foreach ( $field_values as $field_value ) {
371 $this->push_single_field_to_array( $field_value, $output );
372 }
373 }
374
375 /**
376 * Push a single field to the array content
377 *
378 * @since 2.04
379 *
380 * @param FrmFieldValue $field_value
381 * @param array $output
382 */
383 protected function push_single_field_to_array( $field_value, &$output ) {
384 if ( $this->include_field_in_content( $field_value ) ) {
385
386 $displayed_value = $this->prepare_display_value_for_array( $field_value->get_displayed_value() );
387 $output[ $this->get_key_or_id( $field_value ) ] = $displayed_value;
388
389 if ( $displayed_value !== $field_value->get_saved_value() ) {
390 $output[ $this->get_key_or_id( $field_value ) . '-value' ] = $field_value->get_saved_value();
391 }
392 }
393 }
394
395 /**
396 * Add a row of values to the plain text content
397 *
398 * @since 2.04
399 *
400 * @param string $label
401 * @param mixed $display_value
402 * @param string $content
403 */
404 protected function add_plain_text_row( $label, $display_value, &$content ) {
405 $display_value = $this->prepare_display_value_for_plain_text_content( $display_value );
406
407 if ( 'rtl' == $this->direction ) {
408 $content .= $display_value . ' :' . $label . "\r\n";
409 } else {
410 $content .= $label . ': ' . $display_value . "\r\n";
411 }
412 }
413
414 /**
415 * Add a field value to the HTML table or plain text content
416 *
417 * @since 2.04
418 *
419 * @param FrmFieldValue $field_value
420 * @param string $content
421 */
422 protected function add_field_value_to_content( $field_value, &$content ) {
423 if ( ! $this->include_field_in_content( $field_value ) ) {
424 return;
425 }
426
427 if ( $this->format === 'plain_text_block' ) {
428 $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content );
429 } else if ( $this->format === 'table' ) {
430 $value_args = $this->package_value_args( $field_value );
431 $this->add_html_row( $value_args, $content );
432 }
433 }
434
435 /**
436 * Package the value arguments for an HTML row
437 *
438 * @since 2.04
439 *
440 * @param FrmFieldValue $field_value
441 *
442 * @return array
443 */
444 protected function package_value_args( $field_value ) {
445 return array(
446 'label' => $field_value->get_field_label(),
447 'value' => $field_value->get_displayed_value(),
448 'field_type' => $field_value->get_field_type(),
449 );
450 }
451
452 /**
453 * Add user info to an HTML table
454 *
455 * @since 2.04
456 *
457 * @param string $content
458 */
459 protected function add_user_info_to_html_table( &$content ) {
460 if ( $this->include_user_info ) {
461
462 foreach ( $this->entry_values->get_user_info() as $user_info ) {
463
464 $value_args = array(
465 'label' => $user_info['label'],
466 'value' => $user_info['value'],
467 'field_type' => 'none',
468 );
469
470 $this->add_html_row( $value_args, $content );
471 }
472 }
473 }
474
475 /**
476 * Add user info to plain text content
477 *
478 * @since 2.04
479 *
480 * @param string $content
481 */
482 protected function add_user_info_to_plain_text_content( &$content ) {
483 if ( $this->include_user_info ) {
484
485 foreach ( $this->entry_values->get_user_info() as $user_info ) {
486 $this->add_plain_text_row( $user_info['label'], $user_info['value'], $content );
487 }
488 }
489 }
490
491 /**
492 * Check if a field should be included in the content
493 *
494 * @since 2.04
495 *
496 * @param FrmFieldValue $field_value
497 *
498 * @return bool
499 */
500 protected function include_field_in_content( $field_value ) {
501 $include = true;
502
503 if ( $this->is_extra_field( $field_value ) ) {
504
505 $include = $this->is_extra_field_included( $field_value );
506
507 } else {
508 $displayed_value = $field_value->get_displayed_value();
509
510 if ( $displayed_value === '' || ( is_array( $displayed_value ) && empty( $displayed_value ) ) ) {
511
512 if ( ! $this->include_blank ) {
513 $include = false;
514 }
515 }
516 }
517
518 return $include;
519 }
520
521 /**
522 * Check if a field is normally a skipped type
523 *
524 * @since 2.04
525 *
526 * @param FrmFieldValue $field_value
527 *
528 * @return bool
529 */
530 protected function is_extra_field( $field_value ) {
531 return in_array( $field_value->get_field_type(), $this->skip_fields );
532 }
533
534 /**
535 * Check if an extra field is included
536 *
537 * @since 2.04
538 *
539 * @param FrmFieldValue $field_value
540 *
541 * @return bool
542 */
543 protected function is_extra_field_included( $field_value ) {
544 return in_array( $field_value->get_field_type(), $this->include_extras );
545 }
546
547 /**
548 * Add a row in an HTML table
549 *
550 * @since 2.04
551 *
552 * @param array $value_args
553 * $value_args = [
554 * 'label' => (string) The label. Required
555 * 'value' => (mixed) The value to add. Required
556 * 'field_type' => (string) The field type. Blank string if not a field.
557 * ]
558 * @param string $content
559 */
560 protected function add_html_row( $value_args, &$content ) {
561 $display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] );
562
563 $content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value );
564 }
565
566 /**
567 * Prepare the displayed value for an array
568 *
569 * @since 2.04
570 *
571 * @param mixed $value
572 *
573 * @return mixed|string
574 */
575 protected function prepare_display_value_for_array( $value ) {
576 return $this->strip_html( $value );
577 }
578
579
580 /**
581 * Prepare a field's display value for an HTML table
582 *
583 * @since 2.04
584 *
585 * @param mixed $display_value
586 * @param string $field_type
587 *
588 * @return mixed|string
589 */
590 protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) {
591 $display_value = $this->flatten_array( $display_value );
592 $display_value = str_replace( "\r\n", '<br/>', $display_value );
593
594 return $display_value;
595 }
596
597 /**
598 * Prepare a field's display value for plain text content
599 *
600 * @since 2.04
601 *
602 * @param mixed $display_value
603 *
604 * @return string|int
605 */
606 protected function prepare_display_value_for_plain_text_content( $display_value ) {
607 $display_value = $this->flatten_array( $display_value );
608 $display_value = $this->strip_html( $display_value );
609
610 return $display_value;
611 }
612
613 /**
614 * Flatten an array
615 *
616 * @since 2.04
617 *
618 * @param array|string|int $value
619 *
620 * @return string|int
621 */
622 protected function flatten_array( $value ) {
623 if ( is_array( $value ) ) {
624 $value = implode( ', ', $value );
625 }
626
627 return $value;
628 }
629
630 /**
631 * Strip HTML if from email value if plain text is selected
632 *
633 * @since 2.0.21
634 *
635 * @param mixed $value
636 *
637 * @return mixed
638 */
639 protected function strip_html( $value ) {
640
641 if ( $this->is_plain_text ) {
642
643 if ( is_array( $value ) ) {
644 foreach ( $value as $key => $single_value ) {
645 $value[ $key ] = $this->strip_html( $single_value );
646 }
647 } else if ( $this->is_plain_text && ! is_array( $value ) ) {
648 if ( strpos( $value, '<img' ) !== false ) {
649 $value = str_replace( array( '<img', 'src=', '/>', '"' ), '', $value );
650 $value = trim( $value );
651 }
652 $value = strip_tags( $value );
653 }
654 }
655
656 return $value;
657 }
658 }
659