| @@ -89,43 +89,17 @@ | ||
| 89 | 89 | * @var array |
| 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 | 102 | * @return false|string|null 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; |
| @@ -144,9 +118,8 @@ | ||
| 144 | 118 | |
| 145 | 119 | if ( 'file' === self::$mode ) { |
| 146 | 120 | $filepath = get_temp_dir() . $filename; |
| 147 | 121 | self::$fp = @fopen( $filepath, 'w' ); |
| 148 | - | |
| 149 | 122 | if ( ! self::$fp ) { |
| 150 | 123 | return false; |
| 151 | 124 | } |
| 152 | 125 | } elseif ( 'echo' === self::$mode ) { |
| @@ -154,14 +127,14 @@ | ||
| 154 | 127 | } |
| 155 | 128 | |
| 156 | 129 | unset( $filename ); |
| 157 | 130 | |
| 158 | - self::$comment_count = FrmDb::get_count( | |
| 131 | + $comment_count = FrmDb::get_count( | |
| 159 | 132 | 'frm_item_metas', |
| 160 | 133 | array( |
| 161 | 134 | 'item_id' => $atts['entry_ids'], |
| 162 | 135 | 'field_id' => 0, |
| 163 | - 'meta_value like' => 's:7:"comment";', | |
| 136 | + 'meta_value like' => '{', | |
| 164 | 137 | ), |
| 165 | 138 | array( |
| 166 | 139 | 'group_by' => 'item_id', |
| 167 | 140 | 'order_by' => 'count(*) DESC', |
| @@ -167,24 +140,14 @@ | ||
| 167 | 140 | 'order_by' => 'count(*) DESC', |
| 168 | 141 | 'limit' => 1, |
| 169 | 142 | ) |
| 170 | 143 | ); |
| 144 | + self::$comment_count = $comment_count; | |
| 171 | 145 | |
| 172 | 146 | self::prepare_csv_headings(); |
| 173 | 147 | |
| 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 ) ) { | |
| 148 | + // fetch 20 posts at a time rather than loading the entire table into memory | |
| 149 | + while ( $next_set = array_splice( $atts['entry_ids'], 0, 20 ) ) { | |
| 187 | 150 | self::prepare_next_csv_rows( $next_set ); |
| 188 | 151 | } |
| 189 | 152 | |
| 190 | 153 | self::after_generate_csv( $atts ); |
| @@ -202,9 +165,8 @@ | ||
| 202 | 165 | /** |
| 203 | 166 | * @since 6.8.4 |
| 204 | 167 | * |
| 205 | 168 | * @param array $atts |
| 206 | - * | |
| 207 | 169 | * @return void |
| 208 | 170 | */ |
| 209 | 171 | private static function after_generate_csv( $atts ) { |
| 210 | 172 | /** |
| @@ -210,9 +172,8 @@ | ||
| 210 | 172 | /** |
| 211 | 173 | * @since 6.8.4 |
| 212 | 174 | * |
| 213 | 175 | * @param array $atts { |
| 214 | - * | |
| 215 | 176 | * @type object $form |
| 216 | 177 | * @type array $entry_ids |
| 217 | 178 | * @type array $form_cols |
| 218 | 179 | * } |
| @@ -223,9 +184,8 @@ | ||
| 223 | 184 | /** |
| 224 | 185 | * @since 5.0.16 |
| 225 | 186 | * |
| 226 | 187 | * @param stdClass $form |
| 227 | - * | |
| 228 | 188 | * @return string |
| 229 | 189 | */ |
| 230 | 190 | private static function generate_csv_filename( $form ) { |
| 231 | 191 | $filename = gmdate( 'ymdHis', time() ) . '_' . sanitize_title_with_dashes( $form->name ) . '_formidable_entries.csv'; |
| @@ -243,11 +203,8 @@ | ||
| 243 | 203 | 'meta' => self::$meta, |
| 244 | 204 | ); |
| 245 | 205 | } |
| 246 | 206 | |
| 247 | - /** | |
| 248 | - * @return void | |
| 249 | - */ | |
| 250 | 207 | private static function set_class_parameters() { |
| 251 | 208 | $args = self::get_standard_filter_args(); |
| 252 | 209 | self::$separator = apply_filters( 'frm_csv_sep', self::$separator, $args ); |
| 253 | 210 | self::$line_break = apply_filters( 'frm_csv_line_break', self::$line_break, $args ); |
| @@ -254,28 +211,17 @@ | ||
| 254 | 211 | self::$wp_date_format = apply_filters( 'frm_csv_date_format', self::$wp_date_format, $args ); |
| 255 | 212 | self::get_csv_format(); |
| 256 | 213 | self::$charset = get_option( 'blog_charset' ); |
| 257 | 214 | |
| 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; | |
| 215 | + $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 | 216 | |
| 261 | 217 | self::$column_separator = apply_filters( 'frm_csv_column_sep', $col_sep, $args ); |
| 262 | 218 | } |
| 263 | 219 | |
| 264 | - /** | |
| 265 | - * @param object $form | |
| 266 | - * | |
| 267 | - * @return void | |
| 268 | - */ | |
| 269 | 220 | private static function set_has_parent_id( $form ) { |
| 270 | 221 | self::$has_parent_id = $form->parent_form_id > 0; |
| 271 | 222 | } |
| 272 | 223 | |
| 273 | - /** | |
| 274 | - * @param string $filename | |
| 275 | - * | |
| 276 | - * @return void | |
| 277 | - */ | |
| 278 | 224 | private static function print_file_headers( $filename ) { |
| 279 | 225 | header( 'Content-Description: File Transfer' ); |
| 280 | 226 | header( 'Content-Disposition: attachment; filename="' . esc_attr( $filename ) . '"' ); |
| 281 | 227 | header( 'Content-Type: text/csv; charset=' . self::$charset, true ); |
| @@ -283,12 +229,8 @@ | ||
| 283 | 229 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); |
| 284 | 230 | header( 'Cache-Control: no-cache, must-revalidate' ); |
| 285 | 231 | header( 'Pragma: no-cache' ); |
| 286 | 232 | |
| 287 | - if ( self::$include_bom ) { | |
| 288 | - echo esc_html( chr( 239 ) . chr( 187 ) . chr( 191 ) ); | |
| 289 | - } | |
| 290 | - | |
| 291 | 233 | do_action( |
| 292 | 234 | 'frm_csv_headers', |
| 293 | 235 | array( |
| 294 | 236 | 'form_id' => self::$form_id, |
| @@ -296,28 +238,14 @@ | ||
| 296 | 238 | ) |
| 297 | 239 | ); |
| 298 | 240 | } |
| 299 | 241 | |
| 300 | - /** | |
| 301 | - * Check the POST request data for the CSV format to use. | |
| 302 | - * | |
| 303 | - * @return void | |
| 304 | - */ | |
| 305 | 242 | 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 | - | |
| 243 | + $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' ); | |
| 313 | 244 | $csv_format = apply_filters( 'frm_csv_format', $csv_format, self::get_standard_filter_args() ); |
| 314 | 245 | self::$to_encoding = $csv_format; |
| 315 | 246 | } |
| 316 | 247 | |
| 317 | - /** | |
| 318 | - * @return void | |
| 319 | - */ | |
| 320 | 248 | private static function prepare_csv_headings() { |
| 321 | 249 | $headings = array(); |
| 322 | 250 | self::csv_headings( $headings ); |
| 323 | 251 | $headings = apply_filters( |
| @@ -333,16 +261,10 @@ | ||
| 333 | 261 | |
| 334 | 262 | self::print_csv_row( $headings ); |
| 335 | 263 | } |
| 336 | 264 | |
| 337 | - /** | |
| 338 | - * @param object $col | |
| 339 | - * | |
| 340 | - * @return array | |
| 341 | - */ | |
| 342 | 265 | private static function field_headings( $col ) { |
| 343 | 266 | $field_type_obj = FrmFieldFactory::get_field_factory( $col ); |
| 344 | - | |
| 345 | 267 | if ( ! empty( $field_type_obj->is_combo_field ) ) { |
| 346 | 268 | // This is combo field. |
| 347 | 269 | return $field_type_obj->get_export_headings(); |
| 348 | 270 | } |
| @@ -348,20 +270,14 @@ | ||
| 348 | 270 | } |
| 349 | 271 | |
| 350 | 272 | $field_headings = array(); |
| 351 | 273 | $separate_values = array( 'user_id', 'file', 'data', 'date' ); |
| 352 | - | |
| 353 | 274 | if ( ! empty( $col->field_options['separate_value'] ) && ! in_array( $col->type, $separate_values, true ) ) { |
| 354 | 275 | $field_headings[ $col->id . '_label' ] = strip_tags( $col->name . ' ' . __( '(label)', 'formidable' ) ); |
| 355 | 276 | } |
| 356 | 277 | |
| 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( | |
| 278 | + $field_headings[ $col->id ] = strip_tags( $col->name ); | |
| 279 | + $field_headings = apply_filters( | |
| 364 | 280 | 'frm_csv_field_columns', |
| 365 | 281 | $field_headings, |
| 366 | 282 | array_merge( |
| 367 | 283 | self::get_standard_filter_args(), |
| @@ -367,16 +283,13 @@ | ||
| 367 | 283 | self::get_standard_filter_args(), |
| 368 | 284 | array( 'field' => $col ) |
| 369 | 285 | ) |
| 370 | 286 | ); |
| 287 | + | |
| 288 | + return $field_headings; | |
| 371 | 289 | } |
| 372 | 290 | |
| 373 | - /** | |
| 374 | - * @param array $headings | |
| 375 | - * | |
| 376 | - * @return void | |
| 377 | - */ | |
| 378 | - private static function csv_headings( &$headings ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 291 | + private static function csv_headings( &$headings ) { | |
| 379 | 292 | $fields_by_repeater_id = array(); |
| 380 | 293 | $repeater_ids = array(); |
| 381 | 294 | |
| 382 | 295 | foreach ( self::$fields as $col ) { |
| @@ -415,26 +328,24 @@ | ||
| 415 | 328 | } |
| 416 | 329 | unset( $start, $end, $length, $row, $repeater_meta, $where ); |
| 417 | 330 | |
| 418 | 331 | $flat = array(); |
| 419 | - | |
| 420 | 332 | foreach ( $headings as $key => $heading ) { |
| 421 | - if ( ! is_array( $heading ) ) { | |
| 422 | - $flat[ $key ] = $heading; | |
| 423 | - continue; | |
| 424 | - } | |
| 333 | + if ( is_array( $heading ) ) { | |
| 334 | + $repeater_id = str_replace( 'repeater', '', $key ); | |
| 425 | 335 | |
| 426 | - $repeater_id = str_replace( 'repeater', '', $key ); | |
| 427 | - $repeater_headings = array(); | |
| 336 | + $repeater_headings = array(); | |
| 337 | + foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) { | |
| 338 | + $repeater_headings += self::field_headings( $col ); | |
| 339 | + } | |
| 428 | 340 | |
| 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; | |
| 341 | + for ( $i = 0; $i < $max[ $repeater_id ]; $i++ ) { | |
| 342 | + foreach ( $repeater_headings as $repeater_key => $repeater_name ) { | |
| 343 | + $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name; | |
| 344 | + } | |
| 436 | 345 | } |
| 346 | + } else { | |
| 347 | + $flat[ $key ] = $heading; | |
| 437 | 348 | } |
| 438 | 349 | } |
| 439 | 350 | |
| 440 | 351 | self::$fields_by_repeater_id = $fields_by_repeater_id; |
| @@ -461,9 +372,8 @@ | ||
| 461 | 372 | $headings['is_draft'] = __( 'Entry Status', 'formidable' ); |
| 462 | 373 | $headings['ip'] = __( 'IP', 'formidable' ); |
| 463 | 374 | $headings['id'] = __( 'ID', 'formidable' ); |
| 464 | 375 | $headings['item_key'] = __( 'Key', 'formidable' ); |
| 465 | - | |
| 466 | 376 | if ( self::has_parent_id() ) { |
| 467 | 377 | $headings['parent_id'] = __( 'Parent ID', 'formidable' ); |
| 468 | 378 | } |
| 469 | 379 | |
| @@ -471,9 +381,8 @@ | ||
| 471 | 381 | } |
| 472 | 382 | |
| 473 | 383 | /** |
| 474 | 384 | * @param object $field |
| 475 | - * | |
| 476 | 385 | * @return bool |
| 477 | 386 | */ |
| 478 | 387 | private static function is_the_child_of_a_repeater( $field ) { |
| 479 | 388 | if ( $field->form_id === self::$form_id || empty( $field->field_options['in_section'] ) ) { |
| @@ -482,58 +391,40 @@ | ||
| 482 | 391 | |
| 483 | 392 | $section_id = $field->field_options['in_section']; |
| 484 | 393 | $section = FrmField::getOne( $section_id ); |
| 485 | 394 | |
| 486 | - return $section && FrmField::is_repeating_field( $section ); | |
| 395 | + if ( ! $section ) { | |
| 396 | + return false; | |
| 397 | + } | |
| 398 | + | |
| 399 | + return FrmField::is_repeating_field( $section ); | |
| 487 | 400 | } |
| 488 | 401 | |
| 489 | - /** | |
| 490 | - * @return bool | |
| 491 | - */ | |
| 492 | 402 | private static function has_parent_id() { |
| 493 | 403 | return self::$has_parent_id; |
| 494 | 404 | } |
| 495 | 405 | |
| 496 | - /** | |
| 497 | - * @param array $next_set | |
| 498 | - * | |
| 499 | - * @return void | |
| 500 | - */ | |
| 501 | 406 | 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 | - } | |
| 407 | + // order by parent_item_id so children will be first | |
| 408 | + $where = array( | |
| 409 | + 'or' => 1, | |
| 410 | + 'id' => $next_set, | |
| 411 | + 'parent_item_id' => $next_set, | |
| 412 | + ); | |
| 413 | + $entries = FrmEntry::getAll( $where, ' ORDER BY parent_item_id DESC', '', true, false ); | |
| 518 | 414 | |
| 519 | - $entries = FrmEntry::getAll( $where, $order_by, '', true, false ); | |
| 520 | - | |
| 521 | 415 | foreach ( $entries as $entry ) { |
| 522 | 416 | self::$entry = $entry; |
| 523 | 417 | unset( $entry ); |
| 524 | 418 | |
| 525 | - if ( self::$entry->form_id === self::$form_id ) { | |
| 419 | + if ( self::$entry->form_id !== self::$form_id ) { | |
| 420 | + self::add_repeat_field_values_to_csv( $entries ); | |
| 421 | + } else { | |
| 526 | 422 | self::prepare_csv_row(); |
| 527 | - } else { | |
| 528 | - self::add_repeat_field_values_to_csv( $entries ); | |
| 529 | 423 | } |
| 530 | 424 | } |
| 531 | 425 | } |
| 532 | 426 | |
| 533 | - /** | |
| 534 | - * @return void | |
| 535 | - */ | |
| 536 | 427 | private static function prepare_csv_row() { |
| 537 | 428 | $row = array(); |
| 538 | 429 | self::add_field_values_to_csv( $row ); |
| 539 | 430 | self::add_entry_data_to_csv( $row ); |
| @@ -549,20 +440,15 @@ | ||
| 549 | 440 | ); |
| 550 | 441 | self::print_csv_row( $row ); |
| 551 | 442 | } |
| 552 | 443 | |
| 553 | - /** | |
| 554 | - * @param array $entries | |
| 555 | - * | |
| 556 | - * @return void | |
| 557 | - */ | |
| 558 | 444 | private static function add_repeat_field_values_to_csv( &$entries ) { |
| 559 | 445 | if ( isset( self::$entry->metas ) ) { |
| 560 | - // Add child entries to the parent | |
| 446 | + // add child entries to the parent | |
| 561 | 447 | foreach ( self::$entry->metas as $meta_id => $meta_value ) { |
| 562 | 448 | 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 | |
| 449 | + // if the hook is being used to include field keys in the metas array, | |
| 450 | + // we need to skip the keys and only process field ids | |
| 565 | 451 | continue; |
| 566 | 452 | } |
| 567 | 453 | |
| 568 | 454 | if ( ! isset( $entries[ self::$entry->parent_item_id ] ) ) { |
| @@ -572,10 +458,10 @@ | ||
| 572 | 458 | |
| 573 | 459 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) { |
| 574 | 460 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = array(); |
| 575 | 461 | } 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. | |
| 462 | + // if the data is here, it should be an array but if this field has collected data | |
| 463 | + // both while inside and outside of the repeating section, it's possible this is a string. | |
| 578 | 464 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = (array) $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ]; |
| 579 | 465 | } |
| 580 | 466 | |
| 581 | 467 | // Add the repeated values. |
| @@ -585,13 +471,12 @@ | ||
| 585 | 471 | self::$entry->metas = self::fill_missing_repeater_metas( self::$entry->metas, $entries ); |
| 586 | 472 | $entries[ self::$entry->parent_item_id ]->metas += self::$entry->metas; |
| 587 | 473 | }//end if |
| 588 | 474 | |
| 589 | - // Add the embedded form id | |
| 475 | + // add the embedded form id | |
| 590 | 476 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->embedded_fields ) ) { |
| 591 | 477 | $entries[ self::$entry->parent_item_id ]->embedded_fields = array(); |
| 592 | 478 | } |
| 593 | - | |
| 594 | 479 | $entries[ self::$entry->parent_item_id ]->embedded_fields[ self::$entry->id ] = self::$entry->form_id; |
| 595 | 480 | } |
| 596 | 481 | |
| 597 | 482 | /** |
| @@ -599,14 +484,14 @@ | ||
| 599 | 484 | * The export needs all of the meta to be filled in, so we put blank strings for every missing repeater child |
| 600 | 485 | * |
| 601 | 486 | * @param array $metas |
| 602 | 487 | * @param array $entries |
| 603 | - * | |
| 604 | 488 | * @return array |
| 605 | 489 | */ |
| 606 | 490 | private static function fill_missing_repeater_metas( $metas, &$entries ) { |
| 607 | - $field_id = array_key_last( $metas ); | |
| 608 | - $field = self::get_field( $field_id ); | |
| 491 | + $field_ids = array_keys( $metas ); | |
| 492 | + $field_id = end( $field_ids ); | |
| 493 | + $field = self::get_field( $field_id ); | |
| 609 | 494 | |
| 610 | 495 | if ( ! $field || empty( $field->field_options['in_section'] ) ) { |
| 611 | 496 | return $metas; |
| 612 | 497 | } |
| @@ -611,55 +496,40 @@ | ||
| 611 | 496 | return $metas; |
| 612 | 497 | } |
| 613 | 498 | |
| 614 | 499 | $repeater_id = $field->field_options['in_section']; |
| 615 | - | |
| 616 | 500 | if ( ! isset( self::$fields_by_repeater_id[ $repeater_id ] ) ) { |
| 617 | 501 | return $metas; |
| 618 | 502 | } |
| 619 | 503 | |
| 620 | 504 | foreach ( self::$fields_by_repeater_id[ $repeater_id ] as $repeater_child ) { |
| 621 | - if ( isset( $metas[ $repeater_child->id ] ) ) { | |
| 622 | - continue; | |
| 623 | - } | |
| 505 | + if ( ! isset( $metas[ $repeater_child->id ] ) ) { | |
| 506 | + $metas[ $repeater_child->id ] = ''; | |
| 624 | 507 | |
| 625 | - $metas[ $repeater_child->id ] = ''; | |
| 508 | + if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) || ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] ) ) { | |
| 509 | + $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ] = array(); | |
| 510 | + } | |
| 626 | 511 | |
| 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(); | |
| 512 | + $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; | |
| 629 | 513 | } |
| 630 | - | |
| 631 | - $entries[ self::$entry->parent_item_id ]->metas[ $repeater_child->id ][] = ''; | |
| 632 | 514 | } |
| 633 | 515 | |
| 634 | 516 | return $metas; |
| 635 | 517 | } |
| 636 | 518 | |
| 637 | - /** | |
| 638 | - * @param int|string $field_id | |
| 639 | - * | |
| 640 | - * @return false|object | |
| 641 | - */ | |
| 642 | 519 | private static function get_field( $field_id ) { |
| 643 | 520 | $field_id = (int) $field_id; |
| 644 | - | |
| 645 | 521 | foreach ( self::$fields as $field ) { |
| 646 | 522 | if ( $field_id === (int) $field->id ) { |
| 647 | 523 | return $field; |
| 648 | 524 | } |
| 649 | 525 | } |
| 650 | - | |
| 651 | 526 | return false; |
| 652 | 527 | } |
| 653 | 528 | |
| 654 | - /** | |
| 655 | - * @param array $row | |
| 656 | - * | |
| 657 | - * @return void | |
| 658 | - */ | |
| 659 | 529 | private static function add_field_values_to_csv( &$row ) { |
| 660 | 530 | foreach ( self::$fields as $col ) { |
| 661 | - $field_value = self::$entry->metas[ $col->id ] ?? false; | |
| 531 | + $field_value = isset( self::$entry->metas[ $col->id ] ) ? self::$entry->metas[ $col->id ] : false; | |
| 662 | 532 | |
| 663 | 533 | FrmFieldsHelper::prepare_field_value( $field_value, $col->type ); |
| 664 | 534 | self::add_array_values_to_columns( $row, compact( 'col', 'field_value' ) ); |
| 665 | 535 | |
| @@ -675,9 +545,8 @@ | ||
| 675 | 545 | ); |
| 676 | 546 | |
| 677 | 547 | if ( ! empty( $col->field_options['separate_value'] ) ) { |
| 678 | 548 | $label_key = $col->id . '_label'; |
| 679 | - | |
| 680 | 549 | if ( self::is_the_child_of_a_repeater( $col ) ) { |
| 681 | 550 | $row[ $label_key ] = array(); |
| 682 | 551 | |
| 683 | 552 | if ( is_array( $field_value ) ) { |
| @@ -701,9 +570,8 @@ | ||
| 701 | 570 | * @since 5.0.06 |
| 702 | 571 | * |
| 703 | 572 | * @param mixed $field_value |
| 704 | 573 | * @param stdClass $field |
| 705 | - * | |
| 706 | 574 | * @return string |
| 707 | 575 | */ |
| 708 | 576 | private static function get_separate_value_label( $field_value, $field ) { |
| 709 | 577 | return FrmEntriesHelper::display_value( |
| @@ -714,9 +582,9 @@ | ||
| 714 | 582 | 'post_id' => self::$entry->post_id, |
| 715 | 583 | 'show_icon' => false, |
| 716 | 584 | 'entry_id' => self::$entry->id, |
| 717 | 585 | '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 | |
| 586 | + '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 | 587 | ) |
| 720 | 588 | ); |
| 721 | 589 | } |
| 722 | 590 | |
| @@ -721,46 +589,32 @@ | ||
| 721 | 589 | } |
| 722 | 590 | |
| 723 | 591 | /** |
| 724 | 592 | * @since 2.0.23 |
| 725 | - * | |
| 726 | - * @param array $row | |
| 727 | - * @param array $atts | |
| 728 | - * | |
| 729 | - * @return void | |
| 730 | 593 | */ |
| 731 | 594 | private static function add_array_values_to_columns( &$row, $atts ) { |
| 732 | - if ( ! is_array( $atts['field_value'] ) ) { | |
| 733 | - return; | |
| 734 | - } | |
| 595 | + if ( is_array( $atts['field_value'] ) ) { | |
| 596 | + foreach ( $atts['field_value'] as $key => $sub_value ) { | |
| 597 | + if ( is_array( $sub_value ) ) { | |
| 598 | + // This is combo field inside repeater. The heading key has this format: [86_first[0]]. | |
| 599 | + foreach ( $sub_value as $sub_key => $sub_sub_value ) { | |
| 600 | + $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']'; | |
| 601 | + if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 602 | + $row[ $column_key ] = $sub_sub_value; | |
| 603 | + } | |
| 604 | + } | |
| 735 | 605 | |
| 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 . ']'; | |
| 606 | + continue; | |
| 607 | + } | |
| 741 | 608 | |
| 742 | - if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 743 | - $row[ $column_key ] = $sub_sub_value; | |
| 744 | - } | |
| 609 | + $column_key = $atts['col']->id . '_' . $key; | |
| 610 | + if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) { | |
| 611 | + $row[ $column_key ] = $sub_value; | |
| 745 | 612 | } |
| 746 | - | |
| 747 | - continue; | |
| 748 | 613 | } |
| 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 | 614 | } |
| 756 | 615 | } |
| 757 | 616 | |
| 758 | - /** | |
| 759 | - * @param array $row | |
| 760 | - * | |
| 761 | - * @return void | |
| 762 | - */ | |
| 763 | 617 | private static function add_entry_data_to_csv( &$row ) { |
| 764 | 618 | $row['created_at'] = FrmAppHelper::get_formatted_time( self::$entry->created_at, self::$wp_date_format, ' ' ); |
| 765 | 619 | $row['updated_at'] = FrmAppHelper::get_formatted_time( self::$entry->updated_at, self::$wp_date_format, ' ' ); |
| 766 | 620 | $row['user_id'] = self::$entry->user_id; |
| @@ -768,19 +622,13 @@ | ||
| 768 | 622 | $row['is_draft'] = self::$entry->is_draft; |
| 769 | 623 | $row['ip'] = self::$entry->ip; |
| 770 | 624 | $row['id'] = self::$entry->id; |
| 771 | 625 | $row['item_key'] = self::$entry->item_key; |
| 772 | - | |
| 773 | 626 | if ( self::has_parent_id() ) { |
| 774 | 627 | $row['parent_id'] = self::$entry->parent_item_id; |
| 775 | 628 | } |
| 776 | 629 | } |
| 777 | 630 | |
| 778 | - /** | |
| 779 | - * @param array $rows | |
| 780 | - * | |
| 781 | - * @return void | |
| 782 | - */ | |
| 783 | 631 | private static function print_csv_row( $rows ) { |
| 784 | 632 | $sep = ''; |
| 785 | 633 | $echo = 'echo' === self::$mode; |
| 786 | 634 | |
| @@ -788,9 +636,8 @@ | ||
| 788 | 636 | if ( isset( $rows[ $k ] ) ) { |
| 789 | 637 | $row = $rows[ $k ]; |
| 790 | 638 | } else { |
| 791 | 639 | $row = ''; |
| 792 | - | |
| 793 | 640 | // array indexed data is not at $rows[ $k ] |
| 794 | 641 | if ( $k[ strlen( $k ) - 1 ] === ']' ) { |
| 795 | 642 | $start = strrpos( $k, '[' ); |
| 796 | 643 | $key = substr( $k, 0, $start++ ); |
| @@ -804,14 +651,13 @@ | ||
| 804 | 651 | } |
| 805 | 652 | } |
| 806 | 653 | |
| 807 | 654 | if ( is_array( $row ) ) { |
| 808 | - // Implode the repeated field values | |
| 655 | + // implode the repeated field values | |
| 809 | 656 | $row = implode( self::$separator, FrmAppHelper::array_flatten( $row, 'reset' ) ); |
| 810 | 657 | } |
| 811 | 658 | |
| 812 | 659 | $val = self::encode_value( $row ); |
| 813 | - | |
| 814 | 660 | if ( 'return' !== self::$line_break ) { |
| 815 | 661 | $val = str_replace( array( "\r\n", "\r", "\n" ), self::$line_break, $val ); |
| 816 | 662 | } |
| 817 | 663 | |
| @@ -819,14 +665,12 @@ | ||
| 819 | 665 | echo $sep . '"' . $val . '"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 820 | 666 | } else { |
| 821 | 667 | fwrite( self::$fp, $sep . '"' . $val . '"' ); |
| 822 | 668 | } |
| 823 | - | |
| 824 | 669 | $sep = self::$column_separator; |
| 825 | 670 | |
| 826 | 671 | unset( $k, $row ); |
| 827 | 672 | }//end foreach |
| 828 | - | |
| 829 | 673 | if ( $echo ) { |
| 830 | 674 | echo "\n"; |
| 831 | 675 | } else { |
| 832 | 676 | fwrite( self::$fp, "\n" ); |
| @@ -832,13 +676,8 @@ | ||
| 832 | 676 | fwrite( self::$fp, "\n" ); |
| 833 | 677 | } |
| 834 | 678 | } |
| 835 | 679 | |
| 836 | - /** | |
| 837 | - * @param string $line | |
| 838 | - * | |
| 839 | - * @return string | |
| 840 | - */ | |
| 841 | 680 | public static function encode_value( $line ) { |
| 842 | 681 | if ( '' === $line ) { |
| 843 | 682 | return $line; |
| 844 | 683 | } |
| @@ -846,12 +685,12 @@ | ||
| 846 | 685 | $convmap = false; |
| 847 | 686 | |
| 848 | 687 | switch ( self::$to_encoding ) { |
| 849 | 688 | case 'macintosh': |
| 850 | - // This map was derived from the differences between the MacRoman and UTF-8 Charsets | |
| 689 | + // this map was derived from the differences between the MacRoman and UTF-8 Charsets | |
| 851 | 690 | // Reference: |
| 852 | 691 | // 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 | |
| 692 | + $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 | 693 | break; |
| 855 | 694 | case 'ISO-8859-1': |
| 856 | 695 | $convmap = array( 256, 10000, 0, 0xffff ); |
| 857 | 696 | } |
| @@ -870,11 +709,9 @@ | ||
| 870 | 709 | /** |
| 871 | 710 | * Escape a " in a csv with another " |
| 872 | 711 | * |
| 873 | 712 | * @since 2.0 |
| 874 | - * | |
| 875 | 713 | * @param mixed $value |
| 876 | - * | |
| 877 | 714 | * @return mixed |
| 878 | 715 | */ |
| 879 | 716 | public static function escape_csv( $value ) { |
| 880 | 717 | if ( ! is_string( $value ) ) { |
| @@ -884,8 +721,9 @@ | ||
| 884 | 721 | if ( '=' === $value[0] ) { |
| 885 | 722 | // escape the = to prevent vulnerability |
| 886 | 723 | $value = "'" . $value; |
| 887 | 724 | } |
| 725 | + $value = str_replace( '"', '""', $value ); | |
| 888 | 726 | |
| 889 | - return str_replace( '"', '""', $value ); | |
| 727 | + return $value; | |
| 890 | 728 | } |
| 891 | 729 | } |