| @@ -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 | |
| @@ -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; |
| @@ -342,9 +223,9 @@ | ||
| 342 | 223 | $headings['created_at'] = __( 'Timestamp', 'formidable' ); |
| 343 | 224 | $headings['updated_at'] = __( 'Last Updated', 'formidable' ); |
| 344 | 225 | $headings['user_id'] = __( 'Created By', 'formidable' ); |
| 345 | 226 | $headings['updated_by'] = __( 'Updated By', 'formidable' ); |
| 346 | - $headings['is_draft'] = __( 'Entry Status', 'formidable' ); | |
| 227 | + $headings['is_draft'] = __( 'Draft', 'formidable' ); | |
| 347 | 228 | $headings['ip'] = __( 'IP', 'formidable' ); |
| 348 | 229 | $headings['id'] = __( 'ID', 'formidable' ); |
| 349 | 230 | $headings['item_key'] = __( 'Key', 'formidable' ); |
| 350 | 231 | if ( self::has_parent_id() ) { |
| @@ -408,9 +289,8 @@ | ||
| 408 | 289 | array( |
| 409 | 290 | 'entry' => self::$entry, |
| 410 | 291 | 'date_format' => self::$wp_date_format, |
| 411 | 292 | 'comment_count' => self::$comment_count, |
| 412 | - 'context' => self::$context, | |
| 413 | 293 | ) |
| 414 | 294 | ); |
| 415 | 295 | self::print_csv_row( $row ); |
| 416 | 296 | } |
| @@ -476,14 +356,9 @@ | ||
| 476 | 356 | } |
| 477 | 357 | |
| 478 | 358 | foreach ( self::$fields_by_repeater_id[ $repeater_id ] as $repeater_child ) { |
| 479 | 359 | 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 | - | |
| 360 | + $metas[ $repeater_child->id ] = ''; | |
| 486 | 361 | $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; |
| 487 | 362 | } |
| 488 | 363 | } |
| 489 | 364 | |
| @@ -503,9 +378,9 @@ | ||
| 503 | 378 | private static function add_field_values_to_csv( &$row ) { |
| 504 | 379 | foreach ( self::$fields as $col ) { |
| 505 | 380 | $field_value = isset( self::$entry->metas[ $col->id ] ) ? self::$entry->metas[ $col->id ] : false; |
| 506 | 381 | |
| 507 | - FrmFieldsHelper::prepare_field_value( $field_value, $col->type ); | |
| 382 | + FrmAppHelper::unserialize_or_decode( $field_value ); | |
| 508 | 383 | self::add_array_values_to_columns( $row, compact( 'col', 'field_value' ) ); |
| 509 | 384 | |
| 510 | 385 | $field_value = apply_filters( |
| 511 | 386 | 'frm_csv_value', |
| @@ -513,9 +388,8 @@ | ||
| 513 | 388 | array( |
| 514 | 389 | 'field' => $col, |
| 515 | 390 | 'entry' => self::$entry, |
| 516 | 391 | 'separator' => self::$separator, |
| 517 | - 'context' => self::$context, | |
| 518 | 392 | ) |
| 519 | 393 | ); |
| 520 | 394 | |
| 521 | 395 | if ( ! empty( $col->field_options['separate_value'] ) ) { |
| @@ -521,13 +395,10 @@ | ||
| 521 | 395 | if ( ! empty( $col->field_options['separate_value'] ) ) { |
| 522 | 396 | $label_key = $col->id . '_label'; |
| 523 | 397 | if ( self::is_the_child_of_a_repeater( $col ) ) { |
| 524 | 398 | $row[ $label_key ] = array(); |
| 525 | - | |
| 526 | - if ( is_array( $field_value ) ) { | |
| 527 | - foreach ( $field_value as $value ) { | |
| 528 | - $row[ $label_key ][] = self::get_separate_value_label( $value, $col ); | |
| 529 | - } | |
| 399 | + foreach ( $field_value as $value ) { | |
| 400 | + $row[ $label_key ][] = self::get_separate_value_label( $value, $col ); | |
| 530 | 401 | } |
| 531 | 402 | } else { |
| 532 | 403 | $row[ $label_key ] = self::get_separate_value_label( $field_value, $col ); |
| 533 | 404 | } |
| @@ -592,9 +463,9 @@ | ||
| 592 | 463 | $row['created_at'] = FrmAppHelper::get_formatted_time( self::$entry->created_at, self::$wp_date_format, ' ' ); |
| 593 | 464 | $row['updated_at'] = FrmAppHelper::get_formatted_time( self::$entry->updated_at, self::$wp_date_format, ' ' ); |
| 594 | 465 | $row['user_id'] = self::$entry->user_id; |
| 595 | 466 | $row['updated_by'] = self::$entry->updated_by; |
| 596 | - $row['is_draft'] = self::$entry->is_draft; | |
| 467 | + $row['is_draft'] = self::$entry->is_draft ? '1' : '0'; | |
| 597 | 468 | $row['ip'] = self::$entry->ip; |
| 598 | 469 | $row['id'] = self::$entry->id; |
| 599 | 470 | $row['item_key'] = self::$entry->item_key; |
| 600 | 471 | if ( self::has_parent_id() ) { |
| @@ -602,10 +473,9 @@ | ||
| 602 | 473 | } |
| 603 | 474 | } |
| 604 | 475 | |
| 605 | 476 | private static function print_csv_row( $rows ) { |
| 606 | - $sep = ''; | |
| 607 | - $echo = 'echo' === self::$mode; | |
| 477 | + $sep = ''; | |
| 608 | 478 | |
| 609 | 479 | foreach ( self::$headings as $k => $heading ) { |
| 610 | 480 | if ( isset( $rows[ $k ] ) ) { |
| 611 | 481 | $row = $rows[ $k ]; |
| @@ -634,22 +504,14 @@ | ||
| 634 | 504 | if ( 'return' !== self::$line_break ) { |
| 635 | 505 | $val = str_replace( array( "\r\n", "\r", "\n" ), self::$line_break, $val ); |
| 636 | 506 | } |
| 637 | 507 | |
| 638 | - if ( $echo ) { | |
| 639 | - echo $sep . '"' . $val . '"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 640 | - } else { | |
| 641 | - fwrite( self::$fp, $sep . '"' . $val . '"' ); | |
| 642 | - } | |
| 508 | + echo $sep . '"' . $val . '"'; // WPCS: XSS ok. | |
| 643 | 509 | $sep = self::$column_separator; |
| 644 | 510 | |
| 645 | 511 | unset( $k, $row ); |
| 646 | 512 | } |
| 647 | - if ( $echo ) { | |
| 648 | - echo "\n"; | |
| 649 | - } else { | |
| 650 | - fwrite( self::$fp, "\n" ); | |
| 651 | - } | |
| 513 | + echo "\n"; | |
| 652 | 514 | } |
| 653 | 515 | |
| 654 | 516 | public static function encode_value( $line ) { |
| 655 | 517 | if ( '' === $line ) { |