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