| @@ -4,93 +4,22 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmCSVExportHelper { |
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @var string $separator | |
| 10 | - */ | |
| 11 | - protected static $separator = ', '; | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * @var string $column_separator | |
| 15 | - */ | |
| 8 | + protected static $separator = ', '; | |
| 16 | 9 | protected static $column_separator = ','; |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var string $line_break | |
| 20 | - */ | |
| 21 | - protected static $line_break = 'return'; | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * @var string $charset | |
| 25 | - */ | |
| 26 | - protected static $charset = 'UTF-8'; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * @var string $to_encoding | |
| 30 | - */ | |
| 31 | - protected static $to_encoding = 'UTF-8'; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * @var string $wp_date_format | |
| 35 | - */ | |
| 36 | - protected static $wp_date_format = 'Y-m-d H:i:s'; | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * @var int $comment_count | |
| 40 | - */ | |
| 41 | - protected static $comment_count = 0; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @var int $form_id | |
| 45 | - */ | |
| 46 | - protected static $form_id = 0; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * @var array $headings | |
| 50 | - */ | |
| 51 | - protected static $headings = array(); | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * @var array $fields | |
| 55 | - */ | |
| 56 | - protected static $fields = array(); | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * @var stdClass|null $entry | |
| 60 | - */ | |
| 10 | + protected static $line_break = 'return'; | |
| 11 | + protected static $charset = 'UTF-8'; | |
| 12 | + protected static $to_encoding = 'UTF-8'; | |
| 13 | + protected static $wp_date_format = 'Y-m-d H:i:s'; | |
| 14 | + protected static $comment_count = 0; | |
| 15 | + protected static $form_id = 0; | |
| 16 | + protected static $headings = array(); | |
| 17 | + protected static $fields = array(); | |
| 61 | 18 | protected static $entry; |
| 62 | - | |
| 63 | - /** | |
| 64 | - * @var bool|null $has_parent_id | |
| 65 | - */ | |
| 66 | 19 | protected static $has_parent_id; |
| 67 | - | |
| 68 | - /** | |
| 69 | - * @var array|null $fields_by_repeater_id | |
| 70 | - */ | |
| 71 | 20 | protected static $fields_by_repeater_id; |
| 72 | 21 | |
| 73 | - /** | |
| 74 | - * @var string $mode either 'echo' or 'file' are supported. | |
| 75 | - */ | |
| 76 | - protected static $mode = 'echo'; | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * @var resource|null $fp used to write a CSV file in file mode. | |
| 80 | - */ | |
| 81 | - protected static $fp; | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * @var string $context the context of the CSV being generated. Possible values include 'email' when used as an email attachment, or 'default'. | |
| 85 | - */ | |
| 86 | - protected static $context = 'default'; | |
| 87 | - | |
| 88 | - /** | |
| 89 | - * @var array $meta | |
| 90 | - */ | |
| 91 | - protected static $meta = array(); | |
| 92 | - | |
| 93 | 22 | public static function csv_format_options() { |
| 94 | 23 | $formats = array( 'UTF-8', 'ISO-8859-1', 'windows-1256', 'windows-1251', 'macintosh' ); |
| 95 | 24 | $formats = apply_filters( 'frm_csv_format_options', $formats ); |
| 96 | 25 | |
| @@ -96,12 +25,8 @@ | ||
| 96 | 25 | |
| 97 | 26 | return $formats; |
| 98 | 27 | } |
| 99 | 28 | |
| 100 | - /** | |
| 101 | - * @param array $atts | |
| 102 | - * @return string|false|void returns a string file path or false if $atts['mode'] is set to 'file'. | |
| 103 | - */ | |
| 104 | 29 | public static function generate_csv( $atts ) { |
| 105 | 30 | global $frm_vars; |
| 106 | 31 | $frm_vars['prevent_caching'] = true; |
| 107 | 32 | |
| @@ -106,28 +31,15 @@ | ||
| 106 | 31 | $frm_vars['prevent_caching'] = true; |
| 107 | 32 | |
| 108 | 33 | self::$fields = $atts['form_cols']; |
| 109 | 34 | self::$form_id = $atts['form']->id; |
| 110 | - self::$mode = ! empty( $atts['mode'] ) && 'file' === $atts['mode'] ? 'file' : 'echo'; | |
| 111 | - self::$context = ! empty( $atts['context'] ) ? $atts['context'] : 'default'; | |
| 112 | - self::$meta = ! empty( $atts['meta'] ) ? $atts['meta'] : array(); | |
| 113 | - | |
| 114 | - self::set_class_parameters(); | |
| 35 | + self::set_class_paramters(); | |
| 115 | 36 | self::set_has_parent_id( $atts['form'] ); |
| 116 | 37 | |
| 117 | - $filename = self::generate_csv_filename( $atts['form'] ); | |
| 38 | + $filename = apply_filters( 'frm_csv_filename', gmdate( 'ymdHis', time() ) . '_' . sanitize_title_with_dashes( $atts['form']->name ) . '_formidable_entries.csv', $atts['form'] ); | |
| 118 | 39 | unset( $atts['form'], $atts['form_cols'] ); |
| 119 | 40 | |
| 120 | - if ( 'file' === self::$mode ) { | |
| 121 | - $filepath = get_temp_dir() . $filename; | |
| 122 | - self::$fp = @fopen( $filepath, 'w' ); | |
| 123 | - if ( ! self::$fp ) { | |
| 124 | - return false; | |
| 125 | - } | |
| 126 | - } elseif ( 'echo' === self::$mode ) { | |
| 127 | - self::print_file_headers( $filename ); | |
| 128 | - } | |
| 129 | - | |
| 41 | + self::print_file_headers( $filename ); | |
| 130 | 42 | unset( $filename ); |
| 131 | 43 | |
| 132 | 44 | $comment_count = FrmDb::get_count( |
| 133 | 45 | 'frm_item_metas', |
| @@ -149,49 +61,20 @@ | ||
| 149 | 61 | // fetch 20 posts at a time rather than loading the entire table into memory |
| 150 | 62 | while ( $next_set = array_splice( $atts['entry_ids'], 0, 20 ) ) { |
| 151 | 63 | self::prepare_next_csv_rows( $next_set ); |
| 152 | 64 | } |
| 153 | - | |
| 154 | - if ( 'file' === self::$mode ) { | |
| 155 | - fclose( self::$fp ); | |
| 156 | - return $filepath; | |
| 157 | - } | |
| 158 | 65 | } |
| 159 | 66 | |
| 160 | - /** | |
| 161 | - * @since 5.0.16 | |
| 162 | - * | |
| 163 | - * @param stdClass $form | |
| 164 | - * @return string | |
| 165 | - */ | |
| 166 | - private static function generate_csv_filename( $form ) { | |
| 167 | - $filename = gmdate( 'ymdHis', time() ) . '_' . sanitize_title_with_dashes( $form->name ) . '_formidable_entries.csv'; | |
| 168 | - return apply_filters( 'frm_csv_filename', $filename, $form, self::get_standard_filter_args() ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * @since 5.0.16 | |
| 173 | - * | |
| 174 | - * @return array | |
| 175 | - */ | |
| 176 | - private static function get_standard_filter_args() { | |
| 177 | - return array( | |
| 178 | - 'context' => self::$context, | |
| 179 | - 'meta' => self::$meta, | |
| 180 | - ); | |
| 181 | - } | |
| 182 | - | |
| 183 | - private static function set_class_parameters() { | |
| 184 | - $args = self::get_standard_filter_args(); | |
| 185 | - self::$separator = apply_filters( 'frm_csv_sep', self::$separator, $args ); | |
| 186 | - self::$line_break = apply_filters( 'frm_csv_line_break', self::$line_break, $args ); | |
| 187 | - self::$wp_date_format = apply_filters( 'frm_csv_date_format', self::$wp_date_format, $args ); | |
| 67 | + private static function set_class_paramters() { | |
| 68 | + self::$separator = apply_filters( 'frm_csv_sep', self::$separator ); | |
| 69 | + self::$line_break = apply_filters( 'frm_csv_line_break', self::$line_break ); | |
| 70 | + self::$wp_date_format = apply_filters( 'frm_csv_date_format', self::$wp_date_format ); | |
| 188 | 71 | self::get_csv_format(); |
| 189 | 72 | self::$charset = get_option( 'blog_charset' ); |
| 190 | 73 | |
| 191 | - $col_sep = ! empty( $_POST['csv_col_sep'] ) ? sanitize_text_field( wp_unslash( $_POST['csv_col_sep'] ) ) : self::$column_separator; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 74 | + $col_sep = ( isset( $_POST['csv_col_sep'] ) && ! empty( $_POST['csv_col_sep'] ) ) ? sanitize_text_field( wp_unslash( $_POST['csv_col_sep'] ) ) : self::$column_separator; | |
| 192 | 75 | |
| 193 | - self::$column_separator = apply_filters( 'frm_csv_column_sep', $col_sep, $args ); | |
| 76 | + self::$column_separator = apply_filters( 'frm_csv_column_sep', $col_sep ); | |
| 194 | 77 | } |
| 195 | 78 | |
| 196 | 79 | private static function set_has_parent_id( $form ) { |
| 197 | 80 | self::$has_parent_id = $form->parent_form_id > 0; |
| @@ -216,9 +99,9 @@ | ||
| 216 | 99 | } |
| 217 | 100 | |
| 218 | 101 | public static function get_csv_format() { |
| 219 | 102 | $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' ); |
| 220 | - $csv_format = apply_filters( 'frm_csv_format', $csv_format, self::get_standard_filter_args() ); | |
| 103 | + $csv_format = apply_filters( 'frm_csv_format', $csv_format ); | |
| 221 | 104 | self::$to_encoding = $csv_format; |
| 222 | 105 | } |
| 223 | 106 | |
| 224 | 107 | private static function prepare_csv_headings() { |
| @@ -227,11 +110,10 @@ | ||
| 227 | 110 | $headings = apply_filters( |
| 228 | 111 | 'frm_csv_columns', |
| 229 | 112 | $headings, |
| 230 | 113 | self::$form_id, |
| 231 | - array_merge( | |
| 232 | - self::get_standard_filter_args(), | |
| 233 | - array( 'fields' => self::$fields ) | |
| 114 | + array( | |
| 115 | + 'fields' => self::$fields, | |
| 234 | 116 | ) |
| 235 | 117 | ); |
| 236 | 118 | self::$headings = $headings; |
| 237 | 119 | |
| @@ -245,9 +127,9 @@ | ||
| 245 | 127 | } |
| 246 | 128 | |
| 247 | 129 | $field_headings = array(); |
| 248 | 130 | $separate_values = array( 'user_id', 'file', 'data', 'date' ); |
| 249 | - if ( ! empty( $col->field_options['separate_value'] ) && ! in_array( $col->type, $separate_values, true ) ) { | |
| 131 | + if ( isset( $col->field_options['separate_value'] ) && $col->field_options['separate_value'] && ! in_array( $col->type, $separate_values, true ) ) { | |
| 250 | 132 | $field_headings[ $col->id . '_label' ] = strip_tags( $col->name . ' ' . __( '(label)', 'formidable' ) ); |
| 251 | 133 | } |
| 252 | 134 | |
| 253 | 135 | $field_headings[ $col->id ] = strip_tags( $col->name ); |
| @@ -253,11 +135,10 @@ | ||
| 253 | 135 | $field_headings[ $col->id ] = strip_tags( $col->name ); |
| 254 | 136 | $field_headings = apply_filters( |
| 255 | 137 | 'frm_csv_field_columns', |
| 256 | 138 | $field_headings, |
| 257 | - array_merge( | |
| 258 | - self::get_standard_filter_args(), | |
| 259 | - array( 'field' => $col ) | |
| 139 | + array( | |
| 140 | + 'field' => $col, | |
| 260 | 141 | ) |
| 261 | 142 | ); |
| 262 | 143 | |
| 263 | 144 | return $field_headings; |
| @@ -349,10 +230,8 @@ | ||
| 349 | 230 | $headings['item_key'] = __( 'Key', 'formidable' ); |
| 350 | 231 | if ( self::has_parent_id() ) { |
| 351 | 232 | $headings['parent_id'] = __( 'Parent ID', 'formidable' ); |
| 352 | 233 | } |
| 353 | - | |
| 354 | - $headings = apply_filters( 'frm_export_csv_headings', $headings ); | |
| 355 | 234 | } |
| 356 | 235 | |
| 357 | 236 | /** |
| 358 | 237 | * @param object $field |
| @@ -408,9 +287,8 @@ | ||
| 408 | 287 | array( |
| 409 | 288 | 'entry' => self::$entry, |
| 410 | 289 | 'date_format' => self::$wp_date_format, |
| 411 | 290 | 'comment_count' => self::$comment_count, |
| 412 | - 'context' => self::$context, | |
| 413 | 291 | ) |
| 414 | 292 | ); |
| 415 | 293 | self::print_csv_row( $row ); |
| 416 | 294 | } |
| @@ -476,14 +354,9 @@ | ||
| 476 | 354 | } |
| 477 | 355 | |
| 478 | 356 | foreach ( self::$fields_by_repeater_id[ $repeater_id ] as $repeater_child ) { |
| 479 | 357 | if ( ! isset( $metas[ $repeater_child->id ] ) ) { |
| 480 | - $metas[ $repeater_child->id ] = ''; | |
| 481 | - | |
| 482 | - if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) || ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) ) { | |
| 483 | - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] = array(); | |
| 484 | - } | |
| 485 | - | |
| 358 | + $metas[ $repeater_child->id ] = ''; | |
| 486 | 359 | $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; |
| 487 | 360 | } |
| 488 | 361 | } |
| 489 | 362 | |
| @@ -503,9 +376,9 @@ | ||
| 503 | 376 | private static function add_field_values_to_csv( &$row ) { |
| 504 | 377 | foreach ( self::$fields as $col ) { |
| 505 | 378 | $field_value = isset( self::$entry->metas[ $col->id ] ) ? self::$entry->metas[ $col->id ] : false; |
| 506 | 379 | |
| 507 | - FrmFieldsHelper::prepare_field_value( $field_value, $col->type ); | |
| 380 | + FrmAppHelper::unserialize_or_decode( $field_value ); | |
| 508 | 381 | self::add_array_values_to_columns( $row, compact( 'col', 'field_value' ) ); |
| 509 | 382 | |
| 510 | 383 | $field_value = apply_filters( |
| 511 | 384 | 'frm_csv_value', |
| @@ -513,26 +386,27 @@ | ||
| 513 | 386 | array( |
| 514 | 387 | 'field' => $col, |
| 515 | 388 | 'entry' => self::$entry, |
| 516 | 389 | 'separator' => self::$separator, |
| 517 | - 'context' => self::$context, | |
| 518 | 390 | ) |
| 519 | 391 | ); |
| 520 | 392 | |
| 521 | 393 | if ( ! empty( $col->field_options['separate_value'] ) ) { |
| 522 | - $label_key = $col->id . '_label'; | |
| 523 | - if ( self::is_the_child_of_a_repeater( $col ) ) { | |
| 524 | - $row[ $label_key ] = array(); | |
| 394 | + $sep_value = FrmEntriesHelper::display_value( | |
| 395 | + $field_value, | |
| 396 | + $col, | |
| 397 | + array( | |
| 398 | + 'type' => $col->type, | |
| 399 | + 'post_id' => self::$entry->post_id, | |
| 400 | + 'show_icon' => false, | |
| 401 | + 'entry_id' => self::$entry->id, | |
| 402 | + 'sep' => self::$separator, | |
| 403 | + 'embedded_field_id' => ( isset( self::$entry->embedded_fields ) && isset( self::$entry->embedded_fields[ self::$entry->id ] ) ) ? 'form' . self::$entry->embedded_fields[ self::$entry->id ] : 0, | |
| 404 | + ) | |
| 405 | + ); | |
| 525 | 406 | |
| 526 | - if ( is_array( $field_value ) ) { | |
| 527 | - foreach ( $field_value as $value ) { | |
| 528 | - $row[ $label_key ][] = self::get_separate_value_label( $value, $col ); | |
| 529 | - } | |
| 530 | - } | |
| 531 | - } else { | |
| 532 | - $row[ $label_key ] = self::get_separate_value_label( $field_value, $col ); | |
| 533 | - } | |
| 534 | - unset( $label_key ); | |
| 407 | + $row[ $col->id . '_label' ] = $sep_value; | |
| 408 | + unset( $sep_value ); | |
| 535 | 409 | } |
| 536 | 410 | |
| 537 | 411 | $row[ $col->id ] = $field_value; |
| 538 | 412 | |
| @@ -540,30 +414,8 @@ | ||
| 540 | 414 | } |
| 541 | 415 | } |
| 542 | 416 | |
| 543 | 417 | /** |
| 544 | - * @since 5.0.06 | |
| 545 | - * | |
| 546 | - * @param mixed $field_value | |
| 547 | - * @param stdClass $field | |
| 548 | - * @return string | |
| 549 | - */ | |
| 550 | - private static function get_separate_value_label( $field_value, $field ) { | |
| 551 | - return FrmEntriesHelper::display_value( | |
| 552 | - $field_value, | |
| 553 | - $field, | |
| 554 | - array( | |
| 555 | - 'type' => $field->type, | |
| 556 | - 'post_id' => self::$entry->post_id, | |
| 557 | - 'show_icon' => false, | |
| 558 | - 'entry_id' => self::$entry->id, | |
| 559 | - 'sep' => self::$separator, | |
| 560 | - 'embedded_field_id' => ( isset( self::$entry->embedded_fields ) && isset( self::$entry->embedded_fields[ self::$entry->id ] ) ) ? 'form' . self::$entry->embedded_fields[ self::$entry->id ] : 0, | |
| 561 | - ) | |
| 562 | - ); | |
| 563 | - } | |
| 564 | - | |
| 565 | - /** | |
| 566 | 418 | * @since 2.0.23 |
| 567 | 419 | */ |
| 568 | 420 | private static function add_array_values_to_columns( &$row, $atts ) { |
| 569 | 421 | if ( is_array( $atts['field_value'] ) ) { |
| @@ -602,10 +454,9 @@ | ||
| 602 | 454 | } |
| 603 | 455 | } |
| 604 | 456 | |
| 605 | 457 | private static function print_csv_row( $rows ) { |
| 606 | - $sep = ''; | |
| 607 | - $echo = 'echo' === self::$mode; | |
| 458 | + $sep = ''; | |
| 608 | 459 | |
| 609 | 460 | foreach ( self::$headings as $k => $heading ) { |
| 610 | 461 | if ( isset( $rows[ $k ] ) ) { |
| 611 | 462 | $row = $rows[ $k ]; |
| @@ -634,22 +485,14 @@ | ||
| 634 | 485 | if ( 'return' !== self::$line_break ) { |
| 635 | 486 | $val = str_replace( array( "\r\n", "\r", "\n" ), self::$line_break, $val ); |
| 636 | 487 | } |
| 637 | 488 | |
| 638 | - if ( $echo ) { | |
| 639 | - echo $sep . '"' . $val . '"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 640 | - } else { | |
| 641 | - fwrite( self::$fp, $sep . '"' . $val . '"' ); | |
| 642 | - } | |
| 489 | + echo $sep . '"' . $val . '"'; // WPCS: XSS ok. | |
| 643 | 490 | $sep = self::$column_separator; |
| 644 | 491 | |
| 645 | 492 | unset( $k, $row ); |
| 646 | 493 | } |
| 647 | - if ( $echo ) { | |
| 648 | - echo "\n"; | |
| 649 | - } else { | |
| 650 | - fwrite( self::$fp, "\n" ); | |
| 651 | - } | |
| 494 | + echo "\n"; | |
| 652 | 495 | } |
| 653 | 496 | |
| 654 | 497 | public static function encode_value( $line ) { |
| 655 | 498 | if ( '' === $line ) { |