Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FrmEntryFormatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmEntryFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | if ( $displayed_value !== $field_value->get_saved_value() ) { |
||
| 490 | $output[ $this->get_key_or_id( $field_value ) . '-value' ] = $field_value->get_saved_value(); |
||
| 491 | } |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add a row of values to the plain text content |
||
| 497 | * |
||
| 498 | * @since 2.04 |
||
| 499 | * |
||
| 500 | * @param string $label |
||
| 501 | * @param mixed $display_value |
||
| 502 | * @param string $content |
||
| 503 | */ |
||
| 504 | protected function add_plain_text_row( $label, $display_value, &$content ) { |
||
| 505 | $display_value = $this->prepare_display_value_for_plain_text_content( $display_value ); |
||
| 506 | |||
| 507 | if ( 'rtl' == $this->direction ) { |
||
| 508 | $content .= $display_value . ' :' . $label . "\r\n"; |
||
| 509 | } else { |
||
| 510 | $content .= $label . ': ' . $display_value . "\r\n"; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Add a field value to the HTML table or plain text content |
||
| 516 | * |
||
| 517 | * @since 2.04 |
||
| 518 | * |
||
| 519 | * @param FrmFieldValue $field_value |
||
| 520 | * @param string $content |
||
| 521 | */ |
||
| 522 | protected function add_field_value_to_content( $field_value, &$content ) { |
||
| 523 | if ( $this->is_extra_field( $field_value ) ) { |
||
| 524 | $this->add_row_for_extra_field( $field_value, $content ); |
||
| 525 | |||
| 526 | } else { |
||
| 527 | $this->add_row_for_standard_field( $field_value, $content ); |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Add an extra field to plain text or html table content |
||
| 533 | * |
||
| 534 | * @since 3.0 |
||
| 535 | * |
||
| 536 | * @param FrmFieldValue $field_value |
||
| 537 | * @param string $content |
||
| 538 | */ |
||
| 539 | protected function add_row_for_extra_field( $field_value, &$content ) { |
||
| 540 | if ( ! $this->include_field_in_content( $field_value ) ) { |
||
| 541 | return; |
||
| 542 | } |
||
| 543 | |||
| 544 | if ( $this->format === 'plain_text_block' ) { |
||
| 545 | $this->add_plain_text_row_for_included_extra( $field_value, $content ); |
||
| 546 | } else if ( $this->format === 'table' ) { |
||
| 547 | $this->add_html_row_for_included_extra( $field_value, $content ); |
||
| 548 | } |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Add a standard row to plain text or html table content |
||
| 553 | * |
||
| 554 | * @since 3.0 |
||
| 555 | * |
||
| 556 | * @param FrmFieldValue $field_value |
||
| 557 | * @param string $content |
||
| 558 | */ |
||
| 559 | protected function add_row_for_standard_field( $field_value, &$content ) { |
||
| 560 | if ( ! $this->include_field_in_content( $field_value ) ) { |
||
| 561 | return; |
||
| 562 | } |
||
| 563 | |||
| 564 | if ( $this->format === 'plain_text_block' ) { |
||
| 565 | $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content ); |
||
| 566 | } else if ( $this->format === 'table' ) { |
||
| 567 | $value_args = $this->package_value_args( $field_value ); |
||
| 568 | $this->add_html_row( $value_args, $content ); |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Add a row to table for included extra |
||
| 574 | * |
||
| 575 | * @since 3.0 |
||
| 576 | * |
||
| 577 | * @param FrmFieldValue $field_value |
||
| 578 | * @param string $content |
||
| 579 | */ |
||
| 580 | protected function add_html_row_for_included_extra( $field_value, &$content ) { |
||
| 581 | $this->prepare_html_display_value_for_extra_fields( $field_value, $display_value ); |
||
| 582 | |||
| 583 | if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) { |
||
| 584 | $this->add_single_cell_html_row( $display_value, $content ); |
||
| 585 | } else { |
||
| 586 | $value_args = $this->package_value_args( $field_value ); |
||
| 587 | $this->add_html_row( $value_args, $content ); |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Add a plain text row for included extra |
||
| 593 | * |
||
| 594 | * @since 3.0 |
||
| 595 | * |
||
| 596 | * @param FrmFieldValue $field_value |
||
| 597 | * @param string $content |
||
| 598 | */ |
||
| 599 | protected function add_plain_text_row_for_included_extra( $field_value, &$content ) { |
||
| 600 | $this->prepare_plain_text_display_value_for_extra_fields( $field_value, $display_value ); |
||
| 601 | |||
| 602 | if ( in_array( $field_value->get_field_type(), $this->single_cell_fields ) ) { |
||
| 603 | $this->add_single_value_plain_text_row( $display_value, $content ); |
||
| 604 | } else { |
||
| 605 | $this->add_plain_text_row( $field_value->get_field_label(), $display_value, $content ); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Add a single cell row to an HTML table |
||
| 611 | * |
||
| 612 | * @since 3.0 |
||
| 613 | * |
||
| 614 | * @param string $display_value |
||
| 615 | * @param string $content |
||
| 616 | */ |
||
| 617 | protected function add_single_cell_html_row( $display_value, &$content ) { |
||
| 618 | // TODO: maybe move to FrmFieldValue |
||
| 619 | $display_value = $this->prepare_display_value_for_html_table( $display_value ); |
||
| 620 | |||
| 621 | $content .= $this->table_generator->generate_single_cell_table_row( $display_value ); |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Add a single value plain text row |
||
| 626 | * |
||
| 627 | * @since 3.0 |
||
| 628 | * |
||
| 629 | * @param string $display_value |
||
| 630 | * @param string $content |
||
| 631 | */ |
||
| 632 | protected function add_single_value_plain_text_row( $display_value, &$content ) { |
||
| 633 | $content .= $this->prepare_display_value_for_plain_text_content( $display_value ); |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Prepare the display value for extra fields an HTML table |
||
| 638 | * |
||
| 639 | * @since 3.0 |
||
| 640 | * |
||
| 641 | * @param FrmFieldValue $field_value |
||
| 642 | * @param mixed $display_value |
||
| 643 | */ |
||
| 644 | protected function prepare_html_display_value_for_extra_fields( $field_value, &$display_value ) { |
||
| 645 | $display_value = $field_value->get_displayed_value(); |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Prepare a plain text value for extra fields |
||
| 650 | * |
||
| 651 | * @since 3.0 |
||
| 652 | * |
||
| 653 | * @param FrmFieldValue $field_value |
||
| 654 | * @param mixed $display_value |
||
| 655 | */ |
||
| 656 | protected function prepare_plain_text_display_value_for_extra_fields( $field_value, &$display_value ) { |
||
| 657 | $display_value = $field_value->get_displayed_value() . "\r\n"; |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Add a standard row to plain text or html table content |
||
| 662 | * |
||
| 663 | * @since 2.04 |
||
| 664 | * |
||
| 665 | * @param FrmProFieldValue $field_value |
||
| 666 | * @param string $content |
||
| 667 | */ |
||
| 668 | protected function add_standard_row( $field_value, &$content ) { |
||
| 669 | if ( $this->format === 'plain_text_block' ) { |
||
| 670 | $this->add_plain_text_row( $field_value->get_field_label(), $field_value->get_displayed_value(), $content ); |
||
| 671 | } else if ( $this->format === 'table' ) { |
||
| 672 | $value_args = $this->package_value_args( $field_value ); |
||
| 673 | $this->add_html_row( $value_args, $content ); |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Package the value arguments for an HTML row |
||
| 679 | * |
||
| 680 | * @since 2.04 |
||
| 681 | * |
||
| 682 | * @param FrmFieldValue $field_value |
||
| 683 | * |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | protected function package_value_args( $field_value ) { |
||
| 687 | return array( |
||
| 688 | 'label' => $field_value->get_field_label(), |
||
| 689 | 'value' => $field_value->get_displayed_value(), |
||
| 690 | 'field_type' => $field_value->get_field_type(), |
||
| 691 | ); |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Add user info to an HTML table |
||
| 696 | * |
||
| 697 | * @since 2.04 |
||
| 698 | * |
||
| 699 | * @param string $content |
||
| 700 | */ |
||
| 701 | protected function add_user_info_to_html_table( &$content ) { |
||
| 702 | if ( $this->include_user_info ) { |
||
| 703 | |||
| 704 | foreach ( $this->entry_values->get_user_info() as $user_info ) { |
||
| 705 | |||
| 706 | $value_args = array( |
||
| 707 | 'label' => $user_info['label'], |
||
| 708 | 'value' => $user_info['value'], |
||
| 709 | 'field_type' => 'none', |
||
| 710 | ); |
||
| 711 | |||
| 712 | $this->add_html_row( $value_args, $content ); |
||
| 713 | } |
||
| 714 | } |
||
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Add user info to plain text content |
||
| 719 | * |
||
| 720 | * @since 2.04 |
||
| 721 | * |
||
| 722 | * @param string $content |
||
| 723 | */ |
||
| 724 | protected function add_user_info_to_plain_text_content( &$content ) { |
||
| 725 | if ( $this->include_user_info ) { |
||
| 726 | |||
| 727 | foreach ( $this->entry_values->get_user_info() as $user_info ) { |
||
| 728 | $this->add_plain_text_row( $user_info['label'], $user_info['value'], $content ); |
||
| 729 | } |
||
| 730 | } |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Check if a field should be included in the content |
||
| 735 | * |
||
| 736 | * @since 2.04 |
||
| 737 | * |
||
| 738 | * @param FrmFieldValue $field_value |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | protected function include_field_in_content( $field_value ) { |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Check if a field is normally a skipped type |
||
| 756 | * |
||
| 757 | * @since 2.04 |
||
| 758 | * |
||
| 759 | * @param FrmFieldValue $field_value |
||
| 760 | * |
||
| 761 | * @return bool |
||
| 762 | */ |
||
| 763 | protected function is_extra_field( $field_value ) { |
||
| 764 | return in_array( $field_value->get_field_type(), $this->skip_fields() ); |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Check if an extra field is included |
||
| 769 | * |
||
| 770 | * @since 2.04 |
||
| 771 | * |
||
| 772 | * @param FrmFieldValue $field_value |
||
| 773 | * |
||
| 774 | * @return bool |
||
| 775 | */ |
||
| 776 | protected function is_extra_field_included( $field_value ) { |
||
| 777 | return in_array( $field_value->get_field_type(), $this->include_extras ); |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Add a row in an HTML table |
||
| 782 | * |
||
| 783 | * @since 2.04 |
||
| 784 | * |
||
| 785 | * @param array $value_args |
||
| 786 | * $value_args = [ |
||
| 787 | * 'label' => (string) The label. Required |
||
| 788 | * 'value' => (mixed) The value to add. Required |
||
| 789 | * 'field_type' => (string) The field type. Blank string if not a field. |
||
| 790 | * ] |
||
| 791 | * @param string $content |
||
| 792 | */ |
||
| 793 | protected function add_html_row( $value_args, &$content ) { |
||
| 794 | $display_value = $this->prepare_display_value_for_html_table( $value_args['value'], $value_args['field_type'] ); |
||
| 795 | |||
| 796 | $content .= $this->table_generator->generate_two_cell_table_row( $value_args['label'], $display_value ); |
||
| 797 | } |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Prepare the displayed value for an array |
||
| 801 | * |
||
| 802 | * @since 2.04 |
||
| 803 | * |
||
| 804 | * @param mixed $value |
||
| 805 | * |
||
| 806 | * @return mixed|string |
||
| 807 | */ |
||
| 808 | protected function prepare_display_value_for_array( $value ) { |
||
| 809 | return $this->strip_html( $value ); |
||
| 810 | } |
||
| 811 | |||
| 812 | |||
| 813 | /** |
||
| 814 | * Prepare a field's display value for an HTML table |
||
| 815 | * |
||
| 816 | * @since 2.04 |
||
| 817 | * |
||
| 818 | * @param mixed $display_value |
||
| 819 | * @param string $field_type |
||
| 820 | * |
||
| 821 | * @return mixed|string |
||
| 822 | */ |
||
| 823 | protected function prepare_display_value_for_html_table( $display_value, $field_type = '' ) { |
||
| 824 | $display_value = $this->flatten_array( $display_value ); |
||
| 825 | $display_value = str_replace( array( "\r\n", "\n" ), '<br/>', $display_value ); |
||
| 826 | |||
| 827 | return $display_value; |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Prepare a field's display value for plain text content |
||
| 832 | * |
||
| 833 | * @since 2.04 |
||
| 834 | * |
||
| 835 | * @param mixed $display_value |
||
| 836 | * |
||
| 837 | * @return string|int |
||
| 838 | */ |
||
| 839 | protected function prepare_display_value_for_plain_text_content( $display_value ) { |
||
| 840 | $display_value = $this->flatten_array( $display_value ); |
||
| 841 | $display_value = $this->strip_html( $display_value ); |
||
| 842 | |||
| 843 | return $display_value; |
||
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Flatten an array |
||
| 848 | * |
||
| 849 | * @since 2.04 |
||
| 850 | * |
||
| 851 | * @param array|string|int $value |
||
| 852 | * |
||
| 853 | * @return string|int |
||
| 854 | */ |
||
| 855 | protected function flatten_array( $value ) { |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Strip HTML if from email value if plain text is selected |
||
| 865 | * |
||
| 866 | * @since 2.0.21 |
||
| 867 | * |
||
| 868 | * @param mixed $value |
||
| 869 | * |
||
| 870 | * @return mixed |
||
| 871 | */ |
||
| 872 | protected function strip_html( $value ) { |
||
| 890 | } |
||
| 891 | } |
||
| 892 |