| @@ -5,121 +5,94 @@ | ||
| 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' ); |
| 112 | - | |
| 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 | 95 | $formats = apply_filters( 'frm_csv_format_options', $formats ); |
| 123 | 96 | |
| 124 | 97 | return $formats; |
| 125 | 98 | } |
| @@ -125,10 +98,9 @@ | ||
| 125 | 98 | } |
| 126 | 99 | |
| 127 | 100 | /** |
| 128 | 101 | * @param array $atts |
| 129 | - * | |
| 130 | - * @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'. | |
| 131 | 103 | */ |
| 132 | 104 | public static function generate_csv( $atts ) { |
| 133 | 105 | global $frm_vars; |
| 134 | 106 | $frm_vars['prevent_caching'] = true; |
| @@ -142,13 +114,13 @@ | ||
| 142 | 114 | self::set_class_parameters(); |
| 143 | 115 | self::set_has_parent_id( $atts['form'] ); |
| 144 | 116 | |
| 145 | 117 | $filename = self::generate_csv_filename( $atts['form'] ); |
| 118 | + unset( $atts['form'], $atts['form_cols'] ); | |
| 146 | 119 | |
| 147 | 120 | if ( 'file' === self::$mode ) { |
| 148 | 121 | $filepath = get_temp_dir() . $filename; |
| 149 | 122 | self::$fp = @fopen( $filepath, 'w' ); |
| 150 | - | |
| 151 | 123 | if ( ! self::$fp ) { |
| 152 | 124 | return false; |
| 153 | 125 | } |
| 154 | 126 | } elseif ( 'echo' === self::$mode ) { |
| @@ -161,9 +133,9 @@ | ||
| 161 | 133 | 'frm_item_metas', |
| 162 | 134 | array( |
| 163 | 135 | 'item_id' => $atts['entry_ids'], |
| 164 | 136 | 'field_id' => 0, |
| 165 | - 'meta_value like' => 's:7:"comment";', | |
| 137 | + 'meta_value like' => '{', | |
| 166 | 138 | ), |
| 167 | 139 | array( |
| 168 | 140 | 'group_by' => 'item_id', |
| 169 | 141 | 'order_by' => 'count(*) DESC', |
| @@ -173,62 +145,23 @@ | ||
| 173 | 145 | self::$comment_count = $comment_count; |
| 174 | 146 | |
| 175 | 147 | self::prepare_csv_headings(); |
| 176 | 148 | |
| 177 | - /** | |
| 178 | - * For a target form with 50k entries (Locations List, with 7 fields), batch sizes of 100 | |
| 179 | - * were significantly faster and used less memory, so a filter was added. | |
| 180 | - * | |
| 181 | - * @since 6.17 | |
| 182 | - * | |
| 183 | - * @param int $batch_size | |
| 184 | - * @param int $form_id | |
| 185 | - */ | |
| 186 | - $csv_export_batch_size = (int) apply_filters( 'frm_csv_export_batch_size', 20, self::$form_id ); | |
| 187 | - | |
| 188 | - // Fetch posts in batches rather than loading the entire table into memory. | |
| 189 | - 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 ) ) { | |
| 190 | 151 | self::prepare_next_csv_rows( $next_set ); |
| 191 | 152 | } |
| 192 | 153 | |
| 193 | - self::after_generate_csv( $atts ); | |
| 194 | - | |
| 195 | - unset( $atts['form'], $atts['form_cols'] ); | |
| 196 | - | |
| 197 | 154 | if ( 'file' === self::$mode ) { |
| 198 | 155 | fclose( self::$fp ); |
| 199 | 156 | return $filepath; |
| 200 | 157 | } |
| 201 | - | |
| 202 | - return null; | |
| 203 | 158 | } |
| 204 | 159 | |
| 205 | 160 | /** |
| 206 | - * @since 6.8.4 | |
| 207 | - * | |
| 208 | - * @param array $atts | |
| 209 | - * | |
| 210 | - * @return void | |
| 211 | - */ | |
| 212 | - private static function after_generate_csv( $atts ) { | |
| 213 | - /** | |
| 214 | - * @since 6.8.4 | |
| 215 | - * | |
| 216 | - * @param array $atts { | |
| 217 | - * | |
| 218 | - * @type object $form | |
| 219 | - * @type array $entry_ids | |
| 220 | - * @type array $form_cols | |
| 221 | - * } | |
| 222 | - */ | |
| 223 | - do_action( 'frm_after_generate_csv', $atts ); | |
| 224 | - } | |
| 225 | - | |
| 226 | - /** | |
| 227 | 161 | * @since 5.0.16 |
| 228 | 162 | * |
| 229 | 163 | * @param stdClass $form |
| 230 | - * | |
| 231 | 164 | * @return string |
| 232 | 165 | */ |
| 233 | 166 | private static function generate_csv_filename( $form ) { |
| 234 | 167 | $filename = gmdate( 'ymdHis', time() ) . '_' . sanitize_title_with_dashes( $form->name ) . '_formidable_entries.csv'; |
| @@ -246,11 +179,8 @@ | ||
| 246 | 179 | 'meta' => self::$meta, |
| 247 | 180 | ); |
| 248 | 181 | } |
| 249 | 182 | |
| 250 | - /** | |
| 251 | - * @return void | |
| 252 | - */ | |
| 253 | 183 | private static function set_class_parameters() { |
| 254 | 184 | $args = self::get_standard_filter_args(); |
| 255 | 185 | self::$separator = apply_filters( 'frm_csv_sep', self::$separator, $args ); |
| 256 | 186 | self::$line_break = apply_filters( 'frm_csv_line_break', self::$line_break, $args ); |
| @@ -262,22 +192,12 @@ | ||
| 262 | 192 | |
| 263 | 193 | self::$column_separator = apply_filters( 'frm_csv_column_sep', $col_sep, $args ); |
| 264 | 194 | } |
| 265 | 195 | |
| 266 | - /** | |
| 267 | - * @param object $form | |
| 268 | - * | |
| 269 | - * @return void | |
| 270 | - */ | |
| 271 | 196 | private static function set_has_parent_id( $form ) { |
| 272 | 197 | self::$has_parent_id = $form->parent_form_id > 0; |
| 273 | 198 | } |
| 274 | 199 | |
| 275 | - /** | |
| 276 | - * @param string $filename | |
| 277 | - * | |
| 278 | - * @return void | |
| 279 | - */ | |
| 280 | 200 | private static function print_file_headers( $filename ) { |
| 281 | 201 | header( 'Content-Description: File Transfer' ); |
| 282 | 202 | header( 'Content-Disposition: attachment; filename="' . esc_attr( $filename ) . '"' ); |
| 283 | 203 | header( 'Content-Type: text/csv; charset=' . self::$charset, true ); |
| @@ -285,12 +205,8 @@ | ||
| 285 | 205 | header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' ); |
| 286 | 206 | header( 'Cache-Control: no-cache, must-revalidate' ); |
| 287 | 207 | header( 'Pragma: no-cache' ); |
| 288 | 208 | |
| 289 | - if ( self::$include_bom ) { | |
| 290 | - echo esc_html( chr( 239 ) . chr( 187 ) . chr( 191 ) ); | |
| 291 | - } | |
| 292 | - | |
| 293 | 209 | do_action( |
| 294 | 210 | 'frm_csv_headers', |
| 295 | 211 | array( |
| 296 | 212 | 'form_id' => self::$form_id, |
| @@ -298,28 +214,14 @@ | ||
| 298 | 214 | ) |
| 299 | 215 | ); |
| 300 | 216 | } |
| 301 | 217 | |
| 302 | - /** | |
| 303 | - * Check the POST request data for the CSV format to use. | |
| 304 | - * | |
| 305 | - * @return void | |
| 306 | - */ | |
| 307 | 218 | public static function get_csv_format() { |
| 308 | - $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' ); | |
| 309 | - | |
| 310 | - if ( 'UTF-8 with BOM' === $csv_format ) { | |
| 311 | - self::$include_bom = true; | |
| 312 | - $csv_format = 'UTF-8'; | |
| 313 | - } | |
| 314 | - | |
| 219 | + $csv_format = FrmAppHelper::get_post_param( 'csv_format', 'UTF-8', 'sanitize_text_field' ); | |
| 315 | 220 | $csv_format = apply_filters( 'frm_csv_format', $csv_format, self::get_standard_filter_args() ); |
| 316 | 221 | self::$to_encoding = $csv_format; |
| 317 | 222 | } |
| 318 | 223 | |
| 319 | - /** | |
| 320 | - * @return void | |
| 321 | - */ | |
| 322 | 224 | private static function prepare_csv_headings() { |
| 323 | 225 | $headings = array(); |
| 324 | 226 | self::csv_headings( $headings ); |
| 325 | 227 | $headings = apply_filters( |
| @@ -335,34 +237,21 @@ | ||
| 335 | 237 | |
| 336 | 238 | self::print_csv_row( $headings ); |
| 337 | 239 | } |
| 338 | 240 | |
| 339 | - /** | |
| 340 | - * @param object $col | |
| 341 | - * | |
| 342 | - * @return array | |
| 343 | - */ | |
| 344 | 241 | private static function field_headings( $col ) { |
| 345 | 242 | $field_type_obj = FrmFieldFactory::get_field_factory( $col ); |
| 346 | - | |
| 347 | - if ( ! empty( $field_type_obj->is_combo_field ) ) { | |
| 348 | - // This is combo field. | |
| 243 | + if ( ! empty( $field_type_obj->is_combo_field ) ) { // This is combo field. | |
| 349 | 244 | return $field_type_obj->get_export_headings(); |
| 350 | 245 | } |
| 351 | 246 | |
| 352 | 247 | $field_headings = array(); |
| 353 | 248 | $separate_values = array( 'user_id', 'file', 'data', 'date' ); |
| 354 | - | |
| 355 | 249 | if ( ! empty( $col->field_options['separate_value'] ) && ! in_array( $col->type, $separate_values, true ) ) { |
| 356 | 250 | $field_headings[ $col->id . '_label' ] = strip_tags( $col->name . ' ' . __( '(label)', 'formidable' ) ); |
| 357 | 251 | } |
| 358 | 252 | |
| 359 | - if ( ! empty( $field_headings[ $col->id . '_label' ] ) ) { | |
| 360 | - $field_headings[ $col->id ] = strip_tags( $col->name . ' ' . __( '(value)', 'formidable' ) ); | |
| 361 | - } else { | |
| 362 | - $field_headings[ $col->id ] = strip_tags( $col->name ); | |
| 363 | - } | |
| 364 | - | |
| 253 | + $field_headings[ $col->id ] = strip_tags( $col->name ); | |
| 365 | 254 | $field_headings = apply_filters( |
| 366 | 255 | 'frm_csv_field_columns', |
| 367 | 256 | $field_headings, |
| 368 | 257 | array_merge( |
| @@ -373,13 +262,8 @@ | ||
| 373 | 262 | |
| 374 | 263 | return $field_headings; |
| 375 | 264 | } |
| 376 | 265 | |
| 377 | - /** | |
| 378 | - * @param array $headings | |
| 379 | - * | |
| 380 | - * @return void | |
| 381 | - */ | |
| 382 | 266 | private static function csv_headings( &$headings ) { |
| 383 | 267 | $fields_by_repeater_id = array(); |
| 384 | 268 | $repeater_ids = array(); |
| 385 | 269 | |
| @@ -384,11 +268,10 @@ | ||
| 384 | 268 | $repeater_ids = array(); |
| 385 | 269 | |
| 386 | 270 | foreach ( self::$fields as $col ) { |
| 387 | 271 | if ( self::is_the_child_of_a_repeater( $col ) ) { |
| 388 | - $repeater_id = $col->field_options['in_section']; | |
| 389 | - // Set a placeholder to maintain order for repeater fields. | |
| 390 | - $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 | |
| 391 | 274 | |
| 392 | 275 | if ( ! isset( $fields_by_repeater_id[ $repeater_id ] ) ) { |
| 393 | 276 | $fields_by_repeater_id[ $repeater_id ] = array(); |
| 394 | 277 | $repeater_ids[] = $repeater_id; |
| @@ -419,20 +302,18 @@ | ||
| 419 | 302 | } |
| 420 | 303 | unset( $start, $end, $length, $row, $repeater_meta, $where ); |
| 421 | 304 | |
| 422 | 305 | $flat = array(); |
| 423 | - | |
| 424 | 306 | foreach ( $headings as $key => $heading ) { |
| 425 | 307 | if ( is_array( $heading ) ) { |
| 426 | 308 | $repeater_id = str_replace( 'repeater', '', $key ); |
| 427 | 309 | |
| 428 | 310 | $repeater_headings = array(); |
| 429 | - | |
| 430 | 311 | foreach ( $fields_by_repeater_id[ $repeater_id ] as $col ) { |
| 431 | 312 | $repeater_headings += self::field_headings( $col ); |
| 432 | 313 | } |
| 433 | 314 | |
| 434 | - for ( $i = 0; $i < $max[ $repeater_id ]; $i++ ) { | |
| 315 | + for ( $i = 0; $i < $max[ $repeater_id ]; $i ++ ) { | |
| 435 | 316 | foreach ( $repeater_headings as $repeater_key => $repeater_name ) { |
| 436 | 317 | $flat[ $repeater_key . '[' . $i . ']' ] = $repeater_name; |
| 437 | 318 | } |
| 438 | 319 | } |
| @@ -446,12 +327,12 @@ | ||
| 446 | 327 | unset( $key, $heading, $max, $repeater_headings, $repeater_id, $fields_by_repeater_id ); |
| 447 | 328 | |
| 448 | 329 | $headings = $flat; |
| 449 | 330 | unset( $flat ); |
| 450 | - }//end if | |
| 331 | + } | |
| 451 | 332 | |
| 452 | 333 | if ( self::$comment_count ) { |
| 453 | - for ( $i = 0; $i < self::$comment_count; $i++ ) { | |
| 334 | + for ( $i = 0; $i < self::$comment_count; $i ++ ) { | |
| 454 | 335 | $headings[ 'comment' . $i ] = __( 'Comment', 'formidable' ); |
| 455 | 336 | $headings[ 'comment_user_id' . $i ] = __( 'Comment User', 'formidable' ); |
| 456 | 337 | $headings[ 'comment_created_at' . $i ] = __( 'Comment Date', 'formidable' ); |
| 457 | 338 | } |
| @@ -465,9 +346,8 @@ | ||
| 465 | 346 | $headings['is_draft'] = __( 'Entry Status', 'formidable' ); |
| 466 | 347 | $headings['ip'] = __( 'IP', 'formidable' ); |
| 467 | 348 | $headings['id'] = __( 'ID', 'formidable' ); |
| 468 | 349 | $headings['item_key'] = __( 'Key', 'formidable' ); |
| 469 | - | |
| 470 | 350 | if ( self::has_parent_id() ) { |
| 471 | 351 | $headings['parent_id'] = __( 'Parent ID', 'formidable' ); |
| 472 | 352 | } |
| 473 | 353 | |
| @@ -475,9 +355,8 @@ | ||
| 475 | 355 | } |
| 476 | 356 | |
| 477 | 357 | /** |
| 478 | 358 | * @param object $field |
| 479 | - * | |
| 480 | 359 | * @return bool |
| 481 | 360 | */ |
| 482 | 361 | private static function is_the_child_of_a_repeater( $field ) { |
| 483 | 362 | if ( $field->form_id === self::$form_id || empty( $field->field_options['in_section'] ) ) { |
| @@ -493,41 +372,22 @@ | ||
| 493 | 372 | |
| 494 | 373 | return FrmField::is_repeating_field( $section ); |
| 495 | 374 | } |
| 496 | 375 | |
| 497 | - /** | |
| 498 | - * @return bool | |
| 499 | - */ | |
| 500 | 376 | private static function has_parent_id() { |
| 501 | 377 | return self::$has_parent_id; |
| 502 | 378 | } |
| 503 | 379 | |
| 504 | - /** | |
| 505 | - * @param array $next_set | |
| 506 | - * | |
| 507 | - * @return void | |
| 508 | - */ | |
| 509 | 380 | private static function prepare_next_csv_rows( $next_set ) { |
| 510 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 511 | - $where = array( | |
| 512 | - 'or' => 1, | |
| 513 | - 'id' => $next_set, | |
| 514 | - 'parent_item_id' => $next_set, | |
| 515 | - ); | |
| 516 | - // Order by parent_item_id so children will be first. | |
| 517 | - $order_by = ' ORDER BY parent_item_id DESC'; | |
| 518 | - } else { | |
| 519 | - // When Pro is not installed, only query for direct ID matches only as we do not expect parent item id | |
| 520 | - // matches and the more simplified query is faster. | |
| 521 | - $where = array( | |
| 522 | - 'id' => $next_set, | |
| 523 | - ); | |
| 524 | - $order_by = ''; | |
| 525 | - } | |
| 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 ); | |
| 526 | 388 | |
| 527 | - $entries = FrmEntry::getAll( $where, $order_by, '', true, false ); | |
| 528 | - | |
| 529 | - foreach ( $entries as $entry ) { | |
| 389 | + foreach ( $entries as $k => $entry ) { | |
| 530 | 390 | self::$entry = $entry; |
| 531 | 391 | unset( $entry ); |
| 532 | 392 | |
| 533 | 393 | if ( self::$entry->form_id !== self::$form_id ) { |
| @@ -537,11 +397,8 @@ | ||
| 537 | 397 | } |
| 538 | 398 | } |
| 539 | 399 | } |
| 540 | 400 | |
| 541 | - /** | |
| 542 | - * @return void | |
| 543 | - */ | |
| 544 | 401 | private static function prepare_csv_row() { |
| 545 | 402 | $row = array(); |
| 546 | 403 | self::add_field_values_to_csv( $row ); |
| 547 | 404 | self::add_entry_data_to_csv( $row ); |
| @@ -557,13 +414,8 @@ | ||
| 557 | 414 | ); |
| 558 | 415 | self::print_csv_row( $row ); |
| 559 | 416 | } |
| 560 | 417 | |
| 561 | - /** | |
| 562 | - * @param array $entries | |
| 563 | - * | |
| 564 | - * @return void | |
| 565 | - */ | |
| 566 | 418 | private static function add_repeat_field_values_to_csv( &$entries ) { |
| 567 | 419 | if ( isset( self::$entry->metas ) ) { |
| 568 | 420 | // add child entries to the parent |
| 569 | 421 | foreach ( self::$entry->metas as $meta_id => $meta_value ) { |
| @@ -581,19 +433,19 @@ | ||
| 581 | 433 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) { |
| 582 | 434 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = array(); |
| 583 | 435 | } elseif ( ! is_array( $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] ) ) { |
| 584 | 436 | // if the data is here, it should be an array but if this field has collected data |
| 585 | - // both while inside and outside of the repeating section, it's possible this is a string. | |
| 437 | + // both while inside and outside of the repeating section, it's possible this is a string | |
| 586 | 438 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ] = (array) $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ]; |
| 587 | 439 | } |
| 588 | 440 | |
| 589 | - // Add the repeated values. | |
| 441 | + //add the repeated values | |
| 590 | 442 | $entries[ self::$entry->parent_item_id ]->metas[ $meta_id ][] = $meta_value; |
| 591 | - }//end foreach | |
| 443 | + } | |
| 592 | 444 | |
| 593 | 445 | self::$entry->metas = self::fill_missing_repeater_metas( self::$entry->metas, $entries ); |
| 594 | 446 | $entries[ self::$entry->parent_item_id ]->metas += self::$entry->metas; |
| 595 | - }//end if | |
| 447 | + } | |
| 596 | 448 | |
| 597 | 449 | // add the embedded form id |
| 598 | 450 | if ( ! isset( $entries[ self::$entry->parent_item_id ]->embedded_fields ) ) { |
| 599 | 451 | $entries[ self::$entry->parent_item_id ]->embedded_fields = array(); |
| @@ -606,9 +458,8 @@ | ||
| 606 | 458 | * The export needs all of the meta to be filled in, so we put blank strings for every missing repeater child |
| 607 | 459 | * |
| 608 | 460 | * @param array $metas |
| 609 | 461 | * @param array $entries |
| 610 | - * | |
| 611 | 462 | * @return array |
| 612 | 463 | */ |
| 613 | 464 | private static function fill_missing_repeater_metas( $metas, &$entries ) { |
| 614 | 465 | $field_ids = array_keys( $metas ); |
| @@ -619,9 +470,8 @@ | ||
| 619 | 470 | return $metas; |
| 620 | 471 | } |
| 621 | 472 | |
| 622 | 473 | $repeater_id = $field->field_options['in_section']; |
| 623 | - | |
| 624 | 474 | if ( ! isset( self::$fields_by_repeater_id[ $repeater_id ] ) ) { |
| 625 | 475 | return $metas; |
| 626 | 476 | } |
| 627 | 477 | |
| @@ -639,16 +489,10 @@ | ||
| 639 | 489 | |
| 640 | 490 | return $metas; |
| 641 | 491 | } |
| 642 | 492 | |
| 643 | - /** | |
| 644 | - * @param int|string $field_id | |
| 645 | - * | |
| 646 | - * @return false|object | |
| 647 | - */ | |
| 648 | 493 | private static function get_field( $field_id ) { |
| 649 | 494 | $field_id = (int) $field_id; |
| 650 | - | |
| 651 | 495 | foreach ( self::$fields as $field ) { |
| 652 | 496 | if ( $field_id === (int) $field->id ) { |
| 653 | 497 | return $field; |
| 654 | 498 | } |
| @@ -655,16 +499,11 @@ | ||
| 655 | 499 | } |
| 656 | 500 | return false; |
| 657 | 501 | } |
| 658 | 502 | |
| 659 | - /** | |
| 660 | - * @param array $row | |
| 661 | - * | |
| 662 | - * @return void | |
| 663 | - */ | |
| 664 | 503 | private static function add_field_values_to_csv( &$row ) { |
| 665 | 504 | foreach ( self::$fields as $col ) { |
| 666 | - $field_value = self::$entry->metas[ $col->id ] ?? false; | |
| 505 | + $field_value = isset( self::$entry->metas[ $col->id ] ) ? self::$entry->metas[ $col->id ] : false; | |
| 667 | 506 | |
| 668 | 507 | FrmFieldsHelper::prepare_field_value( $field_value, $col->type ); |
| 669 | 508 | self::add_array_values_to_columns( $row, compact( 'col', 'field_value' ) ); |
| 670 | 509 | |
| @@ -680,9 +519,8 @@ | ||
| 680 | 519 | ); |
| 681 | 520 | |
| 682 | 521 | if ( ! empty( $col->field_options['separate_value'] ) ) { |
| 683 | 522 | $label_key = $col->id . '_label'; |
| 684 | - | |
| 685 | 523 | if ( self::is_the_child_of_a_repeater( $col ) ) { |
| 686 | 524 | $row[ $label_key ] = array(); |
| 687 | 525 | |
| 688 | 526 | if ( is_array( $field_value ) ) { |
| @@ -698,9 +536,9 @@ | ||
| 698 | 536 | |
| 699 | 537 | $row[ $col->id ] = $field_value; |
| 700 | 538 | |
| 701 | 539 | unset( $col, $field_value ); |
| 702 | - }//end foreach | |
| 540 | + } | |
| 703 | 541 | } |
| 704 | 542 | |
| 705 | 543 | /** |
| 706 | 544 | * @since 5.0.06 |
| @@ -706,9 +544,8 @@ | ||
| 706 | 544 | * @since 5.0.06 |
| 707 | 545 | * |
| 708 | 546 | * @param mixed $field_value |
| 709 | 547 | * @param stdClass $field |
| 710 | - * | |
| 711 | 548 | * @return string |
| 712 | 549 | */ |
| 713 | 550 | private static function get_separate_value_label( $field_value, $field ) { |
| 714 | 551 | return FrmEntriesHelper::display_value( |
| @@ -719,9 +556,9 @@ | ||
| 719 | 556 | 'post_id' => self::$entry->post_id, |
| 720 | 557 | 'show_icon' => false, |
| 721 | 558 | 'entry_id' => self::$entry->id, |
| 722 | 559 | 'sep' => self::$separator, |
| 723 | - '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, | |
| 560 | + 'embedded_field_id' => ( isset( self::$entry->embedded_fields ) && isset( self::$entry->embedded_fields[ self::$entry->id ] ) ) ? 'form' . self::$entry->embedded_fields[ self::$entry->id ] : 0, | |
| 724 | 561 | ) |
| 725 | 562 | ); |
| 726 | 563 | } |
| 727 | 564 | |
| @@ -726,13 +563,8 @@ | ||
| 726 | 563 | } |
| 727 | 564 | |
| 728 | 565 | /** |
| 729 | 566 | * @since 2.0.23 |
| 730 | - * | |
| 731 | - * @param array $row | |
| 732 | - * @param array $atts | |
| 733 | - * | |
| 734 | - * @return void | |
| 735 | 567 | */ |
| 736 | 568 | private static function add_array_values_to_columns( &$row, $atts ) { |
| 737 | 569 | if ( is_array( $atts['field_value'] ) ) { |
| 738 | 570 | foreach ( $atts['field_value'] as $key => $sub_value ) { |
| @@ -739,9 +571,8 @@ | ||
| 739 | 571 | if ( is_array( $sub_value ) ) { |
| 740 | 572 | // This is combo field inside repeater. The heading key has this format: [86_first[0]]. |
| 741 | 573 | foreach ( $sub_value as $sub_key => $sub_sub_value ) { |
| 742 | 574 | $column_key = $atts['col']->id . '_' . $sub_key . '[' . $key . ']'; |
| 743 | - | |
| 744 | 575 | if ( ! is_numeric( $sub_key ) && isset( self::$headings[ $column_key ] ) ) { |
| 745 | 576 | $row[ $column_key ] = $sub_sub_value; |
| 746 | 577 | } |
| 747 | 578 | } |
| @@ -749,21 +580,15 @@ | ||
| 749 | 580 | continue; |
| 750 | 581 | } |
| 751 | 582 | |
| 752 | 583 | $column_key = $atts['col']->id . '_' . $key; |
| 753 | - | |
| 754 | 584 | if ( ! is_numeric( $key ) && isset( self::$headings[ $column_key ] ) ) { |
| 755 | 585 | $row[ $column_key ] = $sub_value; |
| 756 | 586 | } |
| 757 | 587 | } |
| 758 | - }//end if | |
| 588 | + } | |
| 759 | 589 | } |
| 760 | 590 | |
| 761 | - /** | |
| 762 | - * @param array $row | |
| 763 | - * | |
| 764 | - * @return void | |
| 765 | - */ | |
| 766 | 591 | private static function add_entry_data_to_csv( &$row ) { |
| 767 | 592 | $row['created_at'] = FrmAppHelper::get_formatted_time( self::$entry->created_at, self::$wp_date_format, ' ' ); |
| 768 | 593 | $row['updated_at'] = FrmAppHelper::get_formatted_time( self::$entry->updated_at, self::$wp_date_format, ' ' ); |
| 769 | 594 | $row['user_id'] = self::$entry->user_id; |
| @@ -771,19 +596,13 @@ | ||
| 771 | 596 | $row['is_draft'] = self::$entry->is_draft; |
| 772 | 597 | $row['ip'] = self::$entry->ip; |
| 773 | 598 | $row['id'] = self::$entry->id; |
| 774 | 599 | $row['item_key'] = self::$entry->item_key; |
| 775 | - | |
| 776 | 600 | if ( self::has_parent_id() ) { |
| 777 | 601 | $row['parent_id'] = self::$entry->parent_item_id; |
| 778 | 602 | } |
| 779 | 603 | } |
| 780 | 604 | |
| 781 | - /** | |
| 782 | - * @param array $rows | |
| 783 | - * | |
| 784 | - * @return void | |
| 785 | - */ | |
| 786 | 605 | private static function print_csv_row( $rows ) { |
| 787 | 606 | $sep = ''; |
| 788 | 607 | $echo = 'echo' === self::$mode; |
| 789 | 608 | |
| @@ -791,13 +610,12 @@ | ||
| 791 | 610 | if ( isset( $rows[ $k ] ) ) { |
| 792 | 611 | $row = $rows[ $k ]; |
| 793 | 612 | } else { |
| 794 | 613 | $row = ''; |
| 795 | - | |
| 796 | 614 | // array indexed data is not at $rows[ $k ] |
| 797 | 615 | if ( $k[ strlen( $k ) - 1 ] === ']' ) { |
| 798 | 616 | $start = strrpos( $k, '[' ); |
| 799 | - $key = substr( $k, 0, $start++ ); | |
| 617 | + $key = substr( $k, 0, $start ++ ); | |
| 800 | 618 | $index = substr( $k, $start, strlen( $k ) - 1 - $start ); |
| 801 | 619 | |
| 802 | 620 | if ( isset( $rows[ $key ] ) && isset( $rows[ $key ][ $index ] ) ) { |
| 803 | 621 | $row = $rows[ $key ][ $index ]; |
| @@ -812,9 +630,8 @@ | ||
| 812 | 630 | $row = implode( self::$separator, FrmAppHelper::array_flatten( $row, 'reset' ) ); |
| 813 | 631 | } |
| 814 | 632 | |
| 815 | 633 | $val = self::encode_value( $row ); |
| 816 | - | |
| 817 | 634 | if ( 'return' !== self::$line_break ) { |
| 818 | 635 | $val = str_replace( array( "\r\n", "\r", "\n" ), self::$line_break, $val ); |
| 819 | 636 | } |
| 820 | 637 | |
| @@ -822,14 +639,12 @@ | ||
| 822 | 639 | echo $sep . '"' . $val . '"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 823 | 640 | } else { |
| 824 | 641 | fwrite( self::$fp, $sep . '"' . $val . '"' ); |
| 825 | 642 | } |
| 826 | - | |
| 827 | 643 | $sep = self::$column_separator; |
| 828 | 644 | |
| 829 | 645 | unset( $k, $row ); |
| 830 | - }//end foreach | |
| 831 | - | |
| 646 | + } | |
| 832 | 647 | if ( $echo ) { |
| 833 | 648 | echo "\n"; |
| 834 | 649 | } else { |
| 835 | 650 | fwrite( self::$fp, "\n" ); |
| @@ -835,13 +650,8 @@ | ||
| 835 | 650 | fwrite( self::$fp, "\n" ); |
| 836 | 651 | } |
| 837 | 652 | } |
| 838 | 653 | |
| 839 | - /** | |
| 840 | - * @param string $line | |
| 841 | - * | |
| 842 | - * @return string | |
| 843 | - */ | |
| 844 | 654 | public static function encode_value( $line ) { |
| 845 | 655 | if ( '' === $line ) { |
| 846 | 656 | return $line; |
| 847 | 657 | } |
| @@ -851,9 +661,9 @@ | ||
| 851 | 661 | switch ( self::$to_encoding ) { |
| 852 | 662 | case 'macintosh': |
| 853 | 663 | // this map was derived from the differences between the MacRoman and UTF-8 Charsets |
| 854 | 664 | // Reference: |
| 855 | - // http://www.alanwood.net/demos/macroman.html. | |
| 665 | + // - http://www.alanwood.net/demos/macroman.html | |
| 856 | 666 | $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 ); |
| 857 | 667 | break; |
| 858 | 668 | case 'ISO-8859-1': |
| 859 | 669 | $convmap = array( 256, 10000, 0, 0xffff ); |
| @@ -873,11 +683,9 @@ | ||
| 873 | 683 | /** |
| 874 | 684 | * Escape a " in a csv with another " |
| 875 | 685 | * |
| 876 | 686 | * @since 2.0 |
| 877 | - * | |
| 878 | 687 | * @param mixed $value |
| 879 | - * | |
| 880 | 688 | * @return mixed |
| 881 | 689 | */ |
| 882 | 690 | public static function escape_csv( $value ) { |
| 883 | 691 | if ( ! is_string( $value ) ) { |
| @@ -887,9 +695,8 @@ | ||
| 887 | 695 | if ( '=' === $value[0] ) { |
| 888 | 696 | // escape the = to prevent vulnerability |
| 889 | 697 | $value = "'" . $value; |
| 890 | 698 | } |
| 891 | - | |
| 892 | 699 | $value = str_replace( '"', '""', $value ); |
| 893 | 700 | |
| 894 | 701 | return $value; |
| 895 | 702 | } |