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