get_dir_path( $folder_name ); // check csv dir exists if ( ! file_exists( $upload_dir_path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir mkdir( $upload_dir_path, 0755, true ); } // empty csv dir $existing_files = glob( $upload_dir_path . '/*' ); if ( ! empty( $existing_files ) ) { array_map( 'unlink', $existing_files ); } // One CSV, one row per application, every column on every row, so it // imports straight into a spreadsheet or CRM. No zip. if ( ! empty( $data ) ) { $name = 1 === count( $data ) ? self::file_label( reset( $data )->ID ) : '1003-applications-' . wp_date( 'm-d-Y' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $output = fopen( $upload_dir_path . $name . '.csv', 'w' ); fputcsv( $output, $this->file_header ); foreach ( $data as $value ) { $application_id = $value->ID; $application_fields = get_post_meta( $application_id, '', true ); $row = array(); foreach ( (array) $this->data_key as $key ) { if ( isset( $application_fields[ $key ] ) && in_array( $key, array( 'ss_number', 'co_ss_number' ), true ) ) { $row[] = escape_csv_value( class_exists( '\Mapp\Security\Crypto' ) ? \Mapp\Security\Crypto::read_ssn( $application_fields[ $key ][0] ) : mapp_decrypt_ssn( $application_fields[ $key ][0] ) ); } elseif ( isset( $application_fields[ $key ] ) ) { // 3.0 stores lists (employers, assets, multi-selects) as arrays. $cell = maybe_unserialize( $application_fields[ $key ][0] ); $qdef = class_exists( '\Mapp\Schema\Schema' ) ? \Mapp\Schema\Schema::question( $key ) : null; // Lists, and dates (MM-DD-YYYY), through the schema's display format. if ( is_array( $cell ) || ( $qdef && 'date' === $qdef['type'] ) ) { $cell = class_exists( '\Mapp\Schema\Schema' ) ? \Mapp\Schema\Schema::display_value( $key, $cell ) : wp_json_encode( $cell ); } $row[] = escape_csv_value( $cell ); } else { $row[] = ''; } } fputcsv( $output, $row ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $output ); } $this->deliver( $upload_dir_path, '1003-applications-' . wp_date( 'm-d-Y' ) . '.zip' ); } }