PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.4
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.4
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
← All changes | classes/helpers/FrmCSVExportHelper.php +112 -308 6.295.4 View file →
@@ -5,128 +5,102 @@
5 5
6 6 class FrmCSVExportHelper {
7 7
8 8 /**
9 - * @var string
9 + * @var string $separator
10 10 */
11 11 protected static $separator = ', ';
12 12
13 13 /**
14 - * @var string
14 + * @var string $column_separator
15 15 */
16 16 protected static $column_separator = ',';
17 17
18 18 /**
19 - * @var string
19 + * @var string $line_break
20 20 */
21 21 protected static $line_break = 'return';
22 22
23 23 /**
24 - * @var string
24 + * @var string $charset
25 25 */
26 26 protected static $charset = 'UTF-8';
27 27
28 28 /**
29 - * @var string
29 + * @var string $to_encoding
30 30 */
31 31 protected static $to_encoding = 'UTF-8';
32 32
33 33 /**
34 - * @var string
34 + * @var string $wp_date_format
35 35 */
36 36 protected static $wp_date_format = 'Y-m-d H:i:s';
37 37
38 38 /**
39 - * @var int
39 + * @var int $comment_count
40 40 */
41 41 protected static $comment_count = 0;
42 42
43 43 /**
44 - * @var int
44 + * @var int $form_id
45 45 */
46 46 protected static $form_id = 0;
47 47
48 48 /**
49 - * @var array
49 + * @var array $headings
50 50 */
51 51 protected static $headings = array();
52 52
53 53 /**
54 - * @var array
54 + * @var array $fields
55 55 */
56 56 protected static $fields = array();
57 57
58 58 /**
59 - * @var stdClass|null
59 + * @var stdClass|null $entry
60 60 */
61 61 protected static $entry;
62 62
63 63 /**
64 - * @var bool|null
64 + * @var bool|null $has_parent_id
65 65 */
66 66 protected static $has_parent_id;
67 67
68 68 /**
69 - * @var array|null
69 + * @var array|null $fields_by_repeater_id
70 70 */
71 71 protected static $fields_by_repeater_id;
72 72
73 73 /**
74 - * @var string either 'echo' or 'file' are supported.
74 + * @var string $mode either 'echo' or 'file' are supported.
75 75 */
76 76 protected static $mode = 'echo';
77 77
78 78 /**
79 - * @var resource|null used to write a CSV file in file mode.
79 + * @var resource|null $fp used to write a CSV file in file mode.
80 80 */
81 81 protected static $fp;
82 82
83 83 /**
84 - * @var string the context of the CSV being generated. Possible values include 'email' when used as an email attachment, or 'default'.
84 + * @var string $context the context of the CSV being generated. Possible values include 'email' when used as an email attachment, or 'default'.
85 85 */
86 86 protected static $context = 'default';
87 87
88 88 /**
89 - * @var array
89 + * @var array $meta
90 90 */
91 91 protected static $meta = array();
92 92
93 - /**
94 - * Whether to include the BOM (Byte Order Mark) in the CSV file.
95 - * Only applicable for UTF-8 exports.
96 - *
97 - * @since 6.17
98 - *
99 - * @var bool
100 - */
101 - private static $include_bom = false;
102 -
103 - /**
104 - * Get all options for the CSV export format dropdown.
105 - *
106 - * @since 6.17 The UTF-8 with BOM option was added.
107 - *
108 - * @return array
109 - */
110 93 public static function csv_format_options() {
111 94 $formats = array( 'UTF-8', 'ISO-8859-1', 'windows-1256', 'windows-1251', 'macintosh' );
95 + $formats = apply_filters( 'frm_csv_format_options', $formats );
112 96
113 - // Do not add the UTF-8 with BOM option if this function is called on Global Settings.
114 - // This is to improve compatibility with the Export View as CSV add-on (v1.10).
115 - // Otherwise, the option will appear twice since it is added in the add-on as well.
116 - $on_global_settings_page = 'formidable-settings' === FrmAppHelper::get_param( 'page' );
117 -
118 - if ( ! $on_global_settings_page ) {
119 - array_splice( $formats, 1, 0, 'UTF-8 with BOM' );
120 - }
121 -
122 - return apply_filters( 'frm_csv_format_options', $formats );
97 + return $formats;
123 98 }
124 99
125 100 /**
126 101 * @param array $atts
127 - *
128 - * @return false|string|null returns a string file path or false if $atts['mode'] is set to 'file'.
102 + * @return string|false|void returns a string file path or false if $atts['mode'] is set to 'file'.
129 103 */
130 104 public static function generate_csv( $atts ) {
131 105 global $frm_vars;
132 106 $frm_vars['prevent_caching'] = true;
@@ -140,13 +114,13 @@
140 114 self::set_class_parameters();
141 115 self::set_has_parent_id( $atts['form'] );
142 116
143 117 $filename = self::generate_csv_filename( $atts['form'] );
118 + unset( $atts['form'], $atts['form_cols'] );
144 119
145 120 if ( 'file' === self::$mode ) {
146 121 $filepath = get_temp_dir() . $filename;
147 122 self::$fp = @fopen( $filepath, 'w' );
148 -
149 123 if ( ! self::$fp ) {
150 124 return false;
151 125 }
152 126 } elseif ( 'echo' === self::$mode ) {
@@ -154,14 +128,14 @@
154 128 }
155 129
156 130 unset( $filename );
157 131
158 - self::$comment_count = FrmDb::get_count(
132 + $comment_count = FrmDb::get_count(
159 133 'frm_item_metas',
160 134 array(
161 135 'item_id' => $atts['entry_ids'],
162 136 'field_id' => 0,
163 - 'meta_value like' => 's:7:"comment";',
137 + 'meta_value like' => '{',
164 138 ),
165 139 array(
166 140 'group_by' => 'item_id',
167 141 'order_by' => 'count(*) DESC',
@@ -167,65 +141,27 @@
167 141 'order_by' => 'count(*) DESC',
168 142 'limit' => 1,
169 143 )
170 144 );
145 + self::$comment_count = $comment_count;
171 146
172 147 self::prepare_csv_headings();
173 148
174 - /**
175 - * For a target form with 50k entries (Locations List, with 7 fields), batch sizes of 100
176 - * were significantly faster and used less memory, so a filter was added.
177 - *
178 - * @since 6.17
179 - *
180 - * @param int $batch_size
181 - * @param int $form_id
182 - */
183 - $csv_export_batch_size = (int) apply_filters( 'frm_csv_export_batch_size', 20, self::$form_id );
184 -
185 - // Fetch posts in batches rather than loading the entire table into memory.
186 - while ( $next_set = array_splice( $atts['entry_ids'], 0, $csv_export_batch_size ) ) {
149 + // fetch 20 posts at a time rather than loading the entire table into memory
150 + while ( $next_set = array_splice( $atts['entry_ids'], 0, 20 ) ) {
187 151 self::prepare_next_csv_rows( $next_set );
188 152 }
189 153
190 - self::after_generate_csv( $atts );
191 -
192 - unset( $atts['form'], $atts['form_cols'] );
193 -
194 154 if ( 'file' === self::$mode ) {
195 155 fclose( self::$fp );
196 156 return $filepath;
197 157 }
198 -
199 - return null;
200 158 }
201 159
202 160 /**
203 - * @since 6.8.4
204 - *
205 - * @param array $atts
206 - *
207 - * @return void
208 - */
209 - private static function after_generate_csv( $atts ) {
210 - /**
211 - * @since 6.8.4
212 - *
213 - * @param array $atts {
214 - *
215 - * @type object $form
216 - * @type array $entry_ids
217 - * @type array $form_cols
218 - * }
219 - */
220 - do_action( 'frm_after_generate_csv', $atts );
221 - }
222 -
223 - /**
224 161 * @since 5.0.16
225 162 *
226 163 * @param stdClass $form
227 - *
228 164 * @return string
229 165 */
230 166 private static function generate_csv_filename( $form ) {
231 167 $filename = gmdate( 'ymdHis', time() ) . '_' . sanitize_title_with_dashes( $form->name ) . '_formidable_entries.csv';
@@ -243,11 +179,8 @@
243 179 'meta' => self::$meta,
244 180 );
245 181 }
246 182
247 - /**
248 - * @return void
249 - */
250 183 private static function set_class_parameters() {
251 184 $args = self::get_standard_filter_args();
252 185 self::$separator = apply_filters( 'frm_csv_sep', self::$separator, $args );
253 186 self::$line_break = apply_filters( 'frm_csv_line_break', self::$line_break, $args );
@@ -254,28 +187,17 @@
254 187 self::$wp_date_format = apply_filters( 'frm_csv_date_format', self::$wp_date_format, $args );
255 188 self::get_csv_format();
256 189 self::$charset = get_option( 'blog_charset' );
257 190
258 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
259 - $col_sep = ! empty( $_POST['csv_col_sep'] ) ? sanitize_text_field( wp_unslash( $_POST['csv_col_sep'] ) ) : self::$column_separator;
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
260 192
261 193 self::$column_separator = apply_filters( 'frm_csv_column_sep', $col_sep, $args );
262 194 }
263 195
264 - /**
265 - * @param object $form
266 - *
267 - * @return void
268 - */
269 196 private static function set_has_parent_id( $form ) {
270 197 self::$has_parent_id = $form->parent_form_id > 0;
271 198 }
272 199
273 - /**
274 - * @param string $filename
275 - *
276 - * @return void
277 - */
278 200 private static function print_file_headers( $filename ) {
279 201 header( 'Content-Description: File Transfer' );
280 202 header( 'Content-Disposition: attachment; filename="' . esc_attr( $filename ) . '"' );
281 203 header( 'Content-Type: text/csv; charset=' . self::$charset, true );
@@ -283,12 +205,8 @@
283 205 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
284 206 header( 'Cache-Control: no-cache, must-revalidate' );
285 207 header( 'Pragma: no-cache' );
286 208
287 - if ( self::$include_bom ) {
288 - echo esc_html( chr( 239 ) . chr( 187 ) . chr( 191 ) );
289 - }
290 -
291 209 do_action(
292 210 'frm_csv_headers',
293 211 array(
294 212 'form_id' => self::$form_id,
@@ -296,28 +214,14 @@
296 214 )
297 215 );
298 216 }
299 217
300 - /**
301 - * Check the POST request data for the CSV format to use.
302 - *
303 - * @return void
304 - */
305 218 public static function get_csv_format() {
306 - $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' );
307 -
308 - if ( 'UTF-8 with BOM' === $csv_format ) {
309 - self::$include_bom = true;
310 - $csv_format = 'UTF-8';
311 - }
312 -
219 + $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' );
313 220 $csv_format = apply_filters( 'frm_csv_format', $csv_format, self::get_standard_filter_args() );
314 221 self::$to_encoding = $csv_format;
315 222 }
316 223
317 - /**
318 - * @return void
319 - */
320 224 private static function prepare_csv_headings() {
321 225 $headings = array();
322 226 self::csv_headings( $headings );
323 227 $headings = apply_filters(
@@ -333,35 +237,22 @@
333 237
334 238 self::print_csv_row( $headings );
335 239 }
336 240
337 - /**
338 - * @param object $col
339 - *
340 - * @return array
341 - */
342 241 private static function field_headings( $col ) {
343 242 $field_type_obj = FrmFieldFactory::get_field_factory( $col );
344 -
345 - if ( ! empty( $field_type_obj->is_combo_field ) ) {
346 - // This is combo field.
243 + if ( ! empty( $field_type_obj->is_combo_field ) ) { // This is combo field.
347 244 return $field_type_obj->get_export_headings();
348 245 }
349 246
350 247 $field_headings = array();
351 248 $separate_values = array( 'user_id', 'file', 'data', 'date' );
352 -
353 249 if ( ! empty( $col->field_options['separate_value'] ) && ! in_array( $col->type, $separate_values, true ) ) {
354 250 $field_headings[ $col->id . '_label' ] = strip_tags( $col->name . ' ' . __( '(label)', 'formidable' ) );
355 251 }
356 252
357 - if ( ! empty( $field_headings[ $col->id . '_label' ] ) ) {
358 - $field_headings[ $col->id ] = strip_tags( $col->name . ' ' . __( '(value)', 'formidable' ) );
359 - } else {
360 - $field_headings[ $col->id ] = strip_tags( $col->name );
361 - }
362 -
363 - return apply_filters(
253 + $field_headings[ $col->id ] = strip_tags( $col->name );
254 + $field_headings = apply_filters(
364 255 'frm_csv_field_columns',
365 256 $field_headings,
366 257 array_merge(
367 258 self::get_standard_filter_args(),
@@ -367,24 +258,20 @@
367 258 self::get_standard_filter_args(),
368 259 array( 'field' => $col )
369 260 )
370 261 );
262 +
263 + return $field_headings;
371 264 }
372 265
373 - /**
374 - * @param array $headings
375 - *
376 - * @return void
377 - */
378 - private static function csv_headings( &$headings ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
266 + private static function csv_headings( &$headings ) {
379 267 $fields_by_repeater_id = array();
380 268 $repeater_ids = array();
381 269
382 270 foreach ( self::$fields as $col ) {
383 271 if ( self::is_the_child_of_a_repeater( $col ) ) {
384 - $repeater_id = $col->field_options['in_section'];
385 - // Set a placeholder to maintain order for repeater fields.
386 - $headings[ 'repeater' . $repeater_id ] = array();
272 + $repeater_id = $col->field_options['in_section'];
273 + $headings[ 'repeater' . $repeater_id ] = array(); // set a placeholder to maintain order for repeater fields
387 274
388 275 if ( ! isset( $fields_by_repeater_id[ $repeater_id ] ) ) {
389 276 $fields_by_repeater_id[ $repeater_id ] = array();
390 277 $repeater_ids[] = $repeater_id;
@@ -415,26 +302,24 @@
415 302 }
416 303 unset( $start, $end, $length, $row, $repeater_meta, $where );
417 304
418 305 $flat = array();
419 -
420 306 foreach ( $headings as $key => $heading ) {
421 - if ( ! is_array( $heading ) ) {
422 - $flat[ $key ] = $heading;
423 - continue;
424 - }
307 + if ( is_array( $heading ) ) {
308 + $repeater_id = str_replace( 'repeater', '', $key );
425 309
426 - $repeater_id = str_replace( 'repeater', '', $key );
427 - $repeater_headings = array();
310 + $repeater_headings = array();
311 + foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) {
312 + $repeater_headings += self::field_headings( $col );
313 + }
428 314
429 - foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) {
430 - $repeater_headings += self::field_headings( $col );
431 - }
432 -
433 - for ( $i = 0; $i < $max[ $repeater_id ]; $i++ ) {
434 - foreach ( $repeater_headings as $repeater_key => $repeater_name ) {
435 - $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name;
315 + for ( $i = 0; $i < $max[ $repeater_id ]; $i ++ ) {
316 + foreach ( $repeater_headings as $repeater_key => $repeater_name ) {
317 + $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name;
318 + }
436 319 }
320 + } else {
321 + $flat[ $key ] = $heading;
437 322 }
438 323 }
439 324
440 325 self::$fields_by_repeater_id = $fields_by_repeater_id;
@@ -442,12 +327,12 @@
442 327 unset( $key, $heading, $max, $repeater_headings, $repeater_id, $fields_by_repeater_id );
443 328
444 329 $headings = $flat;
445 330 unset( $flat );
446 - }//end if
331 + }
447 332
448 333 if ( self::$comment_count ) {
449 - for ( $i = 0; $i < self::$comment_count; $i++ ) {
334 + for ( $i = 0; $i < self::$comment_count; $i ++ ) {
450 335 $headings[ 'comment' . $i ] = __( 'Comment', 'formidable' );
451 336 $headings[ 'comment_user_id' . $i ] = __( 'Comment User', 'formidable' );
452 337 $headings[ 'comment_created_at' . $i ] = __( 'Comment Date', 'formidable' );
453 338 }
@@ -457,13 +342,12 @@
457 342 $headings['created_at'] = __( 'Timestamp', 'formidable' );
458 343 $headings['updated_at'] = __( 'Last Updated', 'formidable' );
459 344 $headings['user_id'] = __( 'Created By', 'formidable' );
460 345 $headings['updated_by'] = __( 'Updated By', 'formidable' );
461 - $headings['is_draft'] = __( 'Entry Status', 'formidable' );
346 + $headings['is_draft'] = __( 'Draft', 'formidable' );
462 347 $headings['ip'] = __( 'IP', 'formidable' );
463 348 $headings['id'] = __( 'ID', 'formidable' );
464 349 $headings['item_key'] = __( 'Key', 'formidable' );
465 -
466 350 if ( self::has_parent_id() ) {
467 351 $headings['parent_id'] = __( 'Parent ID', 'formidable' );
468 352 }
469 353
@@ -471,9 +355,8 @@
471 355 }
472 356
473 357 /**
474 358 * @param object $field
475 - *
476 359 * @return bool
477 360 */
478 361 private static function is_the_child_of_a_repeater( $field ) {
479 362 if ( $field->form_id === self::$form_id || empty( $field->field_options['in_section'] ) ) {
@@ -482,58 +365,40 @@
482 365
483 366 $section_id = $field->field_options['in_section'];
484 367 $section = FrmField::getOne( $section_id );
485 368
486 - return $section && FrmField::is_repeating_field( $section );
369 + if ( ! $section ) {
370 + return false;
371 + }
372 +
373 + return FrmField::is_repeating_field( $section );
487 374 }
488 375
489 - /**
490 - * @return bool
491 - */
492 376 private static function has_parent_id() {
493 377 return self::$has_parent_id;
494 378 }
495 379
496 - /**
497 - * @param array $next_set
498 - *
499 - * @return void
500 - */
501 380 private static function prepare_next_csv_rows( $next_set ) {
502 - if ( FrmAppHelper::pro_is_installed() ) {
503 - $where = array(
504 - 'or' => 1,
505 - 'id' => $next_set,
506 - 'parent_item_id' => $next_set,
507 - );
508 - // Order by parent_item_id so children will be first.
509 - $order_by = ' ORDER BY parent_item_id DESC';
510 - } else {
511 - // When Pro is not installed, only query for direct ID matches only as we do not expect parent item id
512 - // Matches and the more simplified query is faster.
513 - $where = array(
514 - 'id' => $next_set,
515 - );
516 - $order_by = '';
517 - }
381 + // order by parent_item_id so children will be first
382 + $where = array(
383 + 'or' => 1,
384 + 'id' => $next_set,
385 + 'parent_item_id' => $next_set,
386 + );
387 + $entries = FrmEntry::getAll( $where, ' ORDER BY parent_item_id DESC', '', true, false );
518 388
519 - $entries = FrmEntry::getAll( $where, $order_by, '', true, false );
520 -
521 - foreach ( $entries as $entry ) {
389 + foreach ( $entries as $k => $entry ) {
522 390 self::$entry = $entry;
523 391 unset( $entry );
524 392
525 - if ( self::$entry->form_id === self::$form_id ) {
393 + if ( self::$entry->form_id !== self::$form_id ) {
394 + self::add_repeat_field_values_to_csv( $entries );
395 + } else {
526 396 self::prepare_csv_row();
527 - } else {
528 - self::add_repeat_field_values_to_csv( $entries );
529 397 }
530 398 }
531 399 }
532 400
533 - /**
534 - * @return void
535 - */
536 401 private static function prepare_csv_row() {
537 402 $row = array();
538 403 self::add_field_values_to_csv( $row );
539 404 self::add_entry_data_to_csv( $row );
@@ -549,20 +414,15 @@
549 414 );
550 415 self::print_csv_row( $row );
551 416 }
552 417
553 - /**
554 - * @param array $entries
555 - *
556 - * @return void
557 - */
558 418 private static function add_repeat_field_values_to_csv( &$entries ) {
559 419 if ( isset( self::$entry->metas ) ) {
560 - // Add child entries to the parent
420 + // add child entries to the parent
561 421 foreach ( self::$entry->metas as $meta_id => $meta_value ) {
562 422 if ( ! is_numeric( $meta_id ) || '' === $meta_value ) {
563 - // If the hook is being used to include field keys in the metas array,
564 - // We need to skip the keys and only process field ids
423 + // if the hook is being used to include field keys in the metas array,
424 + // we need to skip the keys and only process field ids
565 425 continue;
566 426 }
567 427
568 428 if ( ! isset( $entries[ self::$entry->parent_item_id ] ) ) {
@@ -572,26 +432,25 @@
572 432
573 433 if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) {
574 434 $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = array();
575 435 } elseif ( ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) {
576 - // If the data is here, it should be an array but if this field has collected data
577 - // Both while inside and outside of the repeating section, it's possible this is a string.
436 + // if the data is here, it should be an array but if this field has collected data
437 + // both while inside and outside of the repeating section, it's possible this is a string
578 438 $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = (array) $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ];
579 439 }
580 440
581 - // Add the repeated values.
441 + //add the repeated values
582 442 $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ][] = $meta_value;
583 - }//end foreach
443 + }
584 444
585 445 self::$entry->metas = self::fill_missing_repeater_metas( self::$entry->metas, $entries );
586 446 $entries[ self::$entry->parent_item_id ]->metas += self::$entry->metas;
587 - }//end if
447 + }
588 448
589 - // Add the embedded form id
449 + // add the embedded form id
590 450 if ( ! isset( $entries[ self::$entry->parent_item_id ]->embedded_fields ) ) {
591 451 $entries[ self::$entry->parent_item_id ]->embedded_fields = array();
592 452 }
593 -
594 453 $entries[ self::$entry->parent_item_id ]->embedded_fields[ self::$entry->id ] = self::$entry->form_id;
595 454 }
596 455
597 456 /**
@@ -599,14 +458,14 @@
599 458 * The export needs all of the meta to be filled in, so we put blank strings for every missing repeater child
600 459 *
601 460 * @param array $metas
602 461 * @param array $entries
603 - *
604 462 * @return array
605 463 */
606 464 private static function fill_missing_repeater_metas( $metas, &$entries ) {
607 - $field_id = array_key_last( $metas );
608 - $field = self::get_field( $field_id );
465 + $field_ids = array_keys( $metas );
466 + $field_id = end( $field_ids );
467 + $field = self::get_field( $field_id );
609 468
610 469 if ( ! $field || empty( $field->field_options['in_section'] ) ) {
611 470 return $metas;
612 471 }
@@ -611,57 +470,37 @@
611 470 return $metas;
612 471 }
613 472
614 473 $repeater_id = $field->field_options['in_section'];
615 -
616 474 if ( ! isset( self::$fields_by_repeater_id[ $repeater_id ] ) ) {
617 475 return $metas;
618 476 }
619 477
620 478 foreach ( self::$fields_by_repeater_id[ $repeater_id ] as $repeater_child ) {
621 - if ( isset( $metas[ $repeater_child->id ] ) ) {
622 - continue;
479 + if ( ! isset( $metas[ $repeater_child->id ] ) ) {
480 + $metas[ $repeater_child->id ] = '';
481 + $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = '';
623 482 }
624 -
625 - $metas[ $repeater_child->id ] = '';
626 -
627 - if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) || ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
628 - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] = array();
629 - }
630 -
631 - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = '';
632 483 }
633 484
634 485 return $metas;
635 486 }
636 487
637 - /**
638 - * @param int|string $field_id
639 - *
640 - * @return false|object
641 - */
642 488 private static function get_field( $field_id ) {
643 489 $field_id = (int) $field_id;
644 -
645 490 foreach ( self::$fields as $field ) {
646 491 if ( $field_id === (int) $field->id ) {
647 492 return $field;
648 493 }
649 494 }
650 -
651 495 return false;
652 496 }
653 497
654 - /**
655 - * @param array $row
656 - *
657 - * @return void
658 - */
659 498 private static function add_field_values_to_csv( &$row ) {
660 499 foreach ( self::$fields as $col ) {
661 - $field_value = self::$entry->metas[ $col->id ] ?? false;
500 + $field_value = isset( self::$entry->metas[ $col->id ] ) ? self::$entry->metas[ $col->id ] : false;
662 501
663 - FrmFieldsHelper::prepare_field_value( $field_value, $col->type );
502 + FrmAppHelper::unserialize_or_decode( $field_value );
664 503 self::add_array_values_to_columns( $row, compact( 'col', 'field_value' ) );
665 504
666 505 $field_value = apply_filters(
667 506 'frm_csv_value',
@@ -675,16 +514,12 @@
675 514 );
676 515
677 516 if ( ! empty( $col->field_options['separate_value'] ) ) {
678 517 $label_key = $col->id . '_label';
679 -
680 518 if ( self::is_the_child_of_a_repeater( $col ) ) {
681 519 $row[ $label_key ] = array();
682 -
683 - if ( is_array( $field_value ) ) {
684 - foreach ( $field_value as $value ) {
685 - $row[ $label_key ][] = self::get_separate_value_label( $value, $col );
686 - }
520 + foreach ( $field_value as $value ) {
521 + $row[ $label_key ][] = self::get_separate_value_label( $value, $col );
687 522 }
688 523 } else {
689 524 $row[ $label_key ] = self::get_separate_value_label( $field_value, $col );
690 525 }
@@ -693,9 +528,9 @@
693 528
694 529 $row[ $col->id ] = $field_value;
695 530
696 531 unset( $col, $field_value );
697 - }//end foreach
532 + }
698 533 }
699 534
700 535 /**
701 536 * @since 5.0.06
@@ -701,9 +536,8 @@
701 536 * @since 5.0.06
702 537 *
703 538 * @param mixed $field_value
704 539 * @param stdClass $field
705 - *
706 540 * @return string
707 541 */
708 542 private static function get_separate_value_label( $field_value, $field ) {
709 543 return FrmEntriesHelper::display_value(
@@ -714,9 +548,9 @@
714 548 'post_id' => self::$entry->post_id,
715 549 'show_icon' => false,
716 550 'entry_id' => self::$entry->id,
717 551 'sep' => self::$separator,
718 - '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, // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
552 + '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,
719 553 )
720 554 );
721 555 }
722 556
@@ -721,66 +555,46 @@
721 555 }
722 556
723 557 /**
724 558 * @since 2.0.23
725 - *
726 - * @param array $row
727 - * @param array $atts
728 - *
729 - * @return void
730 559 */
731 560 private static function add_array_values_to_columns( &$row, $atts ) {
732 - if ( ! is_array( $atts['field_value'] ) ) {
733 - return;
734 - }
561 + if ( is_array( $atts['field_value'] ) ) {
562 + foreach ( $atts['field_value'] as $key => $sub_value ) {
563 + if ( is_array( $sub_value ) ) {
564 + // This is combo field inside repeater. The heading key has this format: [86_first[0]].
565 + foreach ( $sub_value as $sub_key => $sub_sub_value ) {
566 + $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']';
567 + if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) {
568 + $row[ $column_key ] = $sub_sub_value;
569 + }
570 + }
735 571
736 - foreach ( $atts['field_value'] as $key => $sub_value ) {
737 - if ( is_array( $sub_value ) ) {
738 - // This is combo field inside repeater. The heading key has this format: [86_first[0]].
739 - foreach ( $sub_value as $sub_key => $sub_sub_value ) {
740 - $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']';
572 + continue;
573 + }
741 574
742 - if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) {
743 - $row[ $column_key ] = $sub_sub_value;
744 - }
575 + $column_key = $atts['col']->id . '_' . $key;
576 + if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) {
577 + $row[ $column_key ] = $sub_value;
745 578 }
746 -
747 - continue;
748 579 }
749 -
750 - $column_key = $atts['col']->id . '_' . $key;
751 -
752 - if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) {
753 - $row[ $column_key ] = $sub_value;
754 - }
755 580 }
756 581 }
757 582
758 - /**
759 - * @param array $row
760 - *
761 - * @return void
762 - */
763 583 private static function add_entry_data_to_csv( &$row ) {
764 584 $row['created_at'] = FrmAppHelper::get_formatted_time( self::$entry->created_at, self::$wp_date_format, ' ' );
765 585 $row['updated_at'] = FrmAppHelper::get_formatted_time( self::$entry->updated_at, self::$wp_date_format, ' ' );
766 586 $row['user_id'] = self::$entry->user_id;
767 587 $row['updated_by'] = self::$entry->updated_by;
768 - $row['is_draft'] = self::$entry->is_draft;
588 + $row['is_draft'] = self::$entry->is_draft ? '1' : '0';
769 589 $row['ip'] = self::$entry->ip;
770 590 $row['id'] = self::$entry->id;
771 591 $row['item_key'] = self::$entry->item_key;
772 -
773 592 if ( self::has_parent_id() ) {
774 593 $row['parent_id'] = self::$entry->parent_item_id;
775 594 }
776 595 }
777 596
778 - /**
779 - * @param array $rows
780 - *
781 - * @return void
782 - */
783 597 private static function print_csv_row( $rows ) {
784 598 $sep = '';
785 599 $echo = 'echo' === self::$mode;
786 600
@@ -788,13 +602,12 @@
788 602 if ( isset( $rows[ $k ] ) ) {
789 603 $row = $rows[ $k ];
790 604 } else {
791 605 $row = '';
792 -
793 606 // array indexed data is not at $rows[ $k ]
794 607 if ( $k[ strlen( $k ) - 1 ] === ']' ) {
795 608 $start = strrpos( $k, '[' );
796 - $key = substr( $k, 0, $start++ );
609 + $key = substr( $k, 0, $start ++ );
797 610 $index = substr( $k, $start, strlen( $k ) - 1 - $start );
798 611
799 612 if ( isset( $rows[ $key ] ) && isset( $rows[ $key ][ $index ] ) ) {
800 613 $row = $rows[ $key ][ $index ];
@@ -804,14 +617,13 @@
804 617 }
805 618 }
806 619
807 620 if ( is_array( $row ) ) {
808 - // Implode the repeated field values
621 + // implode the repeated field values
809 622 $row = implode( self::$separator, FrmAppHelper::array_flatten( $row, 'reset' ) );
810 623 }
811 624
812 625 $val = self::encode_value( $row );
813 -
814 626 if ( 'return' !== self::$line_break ) {
815 627 $val = str_replace( array( "\r\n", "\r", "\n" ), self::$line_break, $val );
816 628 }
817 629
@@ -819,14 +631,12 @@
819 631 echo $sep . '"' . $val . '"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
820 632 } else {
821 633 fwrite( self::$fp, $sep . '"' . $val . '"' );
822 634 }
823 -
824 635 $sep = self::$column_separator;
825 636
826 637 unset( $k, $row );
827 - }//end foreach
828 -
638 + }
829 639 if ( $echo ) {
830 640 echo "\n";
831 641 } else {
832 642 fwrite( self::$fp, "\n" );
@@ -832,13 +642,8 @@
832 642 fwrite( self::$fp, "\n" );
833 643 }
834 644 }
835 645
836 - /**
837 - * @param string $line
838 - *
839 - * @return string
840 - */
841 646 public static function encode_value( $line ) {
842 647 if ( '' === $line ) {
843 648 return $line;
844 649 }
@@ -846,12 +651,12 @@
846 651 $convmap = false;
847 652
848 653 switch ( self::$to_encoding ) {
849 654 case 'macintosh':
850 - // This map was derived from the differences between the MacRoman and UTF-8 Charsets
655 + // this map was derived from the differences between the MacRoman and UTF-8 Charsets
851 656 // Reference:
852 - // http://www.alanwood.net/demos/macroman.html.
853 - $convmap = array( 256, 304, 0, 0xffff, 306, 337, 0, 0xffff, 340, 375, 0, 0xffff, 377, 401, 0, 0xffff, 403, 709, 0, 0xffff, 712, 727, 0, 0xffff, 734, 936, 0, 0xffff, 938, 959, 0, 0xffff, 961, 8210, 0, 0xffff, 8213, 8215, 0, 0xffff, 8219, 8219, 0, 0xffff, 8227, 8229, 0, 0xffff, 8231, 8239, 0, 0xffff, 8241, 8248, 0, 0xffff, 8251, 8259, 0, 0xffff, 8261, 8363, 0, 0xffff, 8365, 8481, 0, 0xffff, 8483, 8705, 0, 0xffff, 8707, 8709, 0, 0xffff, 8711, 8718, 0, 0xffff, 8720, 8720, 0, 0xffff, 8722, 8729, 0, 0xffff, 8731, 8733, 0, 0xffff, 8735, 8746, 0, 0xffff, 8748, 8775, 0, 0xffff, 8777, 8799, 0, 0xffff, 8801, 8803, 0, 0xffff, 8806, 9673, 0, 0xffff, 9675, 63742, 0, 0xffff, 63744, 64256, 0, 0xffff ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
657 + // - http://www.alanwood.net/demos/macroman.html
658 + $convmap = array( 256, 304, 0, 0xffff, 306, 337, 0, 0xffff, 340, 375, 0, 0xffff, 377, 401, 0, 0xffff, 403, 709, 0, 0xffff, 712, 727, 0, 0xffff, 734, 936, 0, 0xffff, 938, 959, 0, 0xffff, 961, 8210, 0, 0xffff, 8213, 8215, 0, 0xffff, 8219, 8219, 0, 0xffff, 8227, 8229, 0, 0xffff, 8231, 8239, 0, 0xffff, 8241, 8248, 0, 0xffff, 8251, 8259, 0, 0xffff, 8261, 8363, 0, 0xffff, 8365, 8481, 0, 0xffff, 8483, 8705, 0, 0xffff, 8707, 8709, 0, 0xffff, 8711, 8718, 0, 0xffff, 8720, 8720, 0, 0xffff, 8722, 8729, 0, 0xffff, 8731, 8733, 0, 0xffff, 8735, 8746, 0, 0xffff, 8748, 8775, 0, 0xffff, 8777, 8799, 0, 0xffff, 8801, 8803, 0, 0xffff, 8806, 9673, 0, 0xffff, 9675, 63742, 0, 0xffff, 63744, 64256, 0, 0xffff );
854 659 break;
855 660 case 'ISO-8859-1':
856 661 $convmap = array( 256, 10000, 0, 0xffff );
857 662 }
@@ -870,11 +675,9 @@
870 675 /**
871 676 * Escape a " in a csv with another "
872 677 *
873 678 * @since 2.0
874 - *
875 679 * @param mixed $value
876 - *
877 680 * @return mixed
878 681 */
879 682 public static function escape_csv( $value ) {
880 683 if ( ! is_string( $value ) ) {
@@ -884,8 +687,9 @@
884 687 if ( '=' === $value[0] ) {
885 688 // escape the = to prevent vulnerability
886 689 $value = "'" . $value;
887 690 }
691 + $value = str_replace( '"', '""', $value );
888 692
889 - return str_replace( '"', '""', $value );
693 + return $value;
890 694 }
891 695 }