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