public static function export($form_id, array $sub_ids = array(), $return = FALSE) { //TODO: Set Date Format from Plugin Settings $date_format = 'm/d/Y'; /* * Labels */ $field_labels = array('_seq_num' => '#', '_date_submitted' => __('Date Submitted', 'ninja-forms')); // Legacy Filter from 2.9.* $field_labels = apply_filters('nf_subs_csv_label_array_before_fields', $field_labels, $sub_ids); $fields = Ninja_Forms()->form($form_id)->get_fields(); $hidden_field_types = apply_filters('nf_sub_hidden_field_types', array()); foreach ($fields as $field) { if (in_array($field->get_setting('type'), $hidden_field_types)) { continue; } $field_labels[$field->get_id()] = $field->get_setting('label'); } /* * Submissions */ $value_array = array(); $subs = Ninja_Forms()->form($form_id)->get_subs(); foreach ($subs as $sub) { if (!in_array($sub->get_id(), $sub_ids)) { continue; } $value['_seq_num'] = $sub->get_seq_num(); $value['_date_submitted'] = $sub->get_sub_date($date_format); foreach ($field_labels as $field_id => $label) { if (!is_int($field_id)) { continue; } $value[$field_id] = $sub->get_field_value($field_id); } $value_array[] = $value; } $value_array = WPN_Helper::stripslashes($value_array); // Legacy Filter from 2.9.* $value_array = apply_filters('nf_subs_csv_value_array', $value_array, $sub_ids); $csv_array[0][] = $field_labels; $csv_array[1][] = $value_array; $today = date($date_format, current_time('timestamp')); $filename = apply_filters('nf_subs_csv_filename', 'nf_subs_' . $today); $filename = $filename . ".csv"; if ($return) { return WPN_Helper::str_putcsv($csv_array, apply_filters('nf_sub_csv_delimiter', ','), apply_filters('nf_sub_csv_enclosure', '"'), apply_filters('nf_sub_csv_terminator', "\n")); } else { header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Pragma: no-cache'); header('Expires: 0'); echo apply_filters('nf_sub_csv_bom', ""); // Byte Order Mark echo WPN_Helper::str_putcsv($csv_array, apply_filters('nf_sub_csv_delimiter', ','), apply_filters('nf_sub_csv_enclosure', '"'), apply_filters('nf_sub_csv_terminator', "\n")); die; } }
private function _create_csv($fields) { $csv_array = array(); foreach ($fields as $field) { if (!isset($field['label'])) { continue; } $csv_array[0][] = $field['label']; $csv_array[1][] = WPN_Helper::stripslashes($field['value']); } $csv_content = WPN_Helper::str_putcsv($csv_array, apply_filters('ninja_forms_sub_csv_delimiter', ','), apply_filters('ninja_forms_sub_csv_enclosure', '"'), apply_filters('ninja_forms_sub_csv_terminator', "\n")); $upload_dir = wp_upload_dir(); $path = trailingslashit($upload_dir['path']); // create temporary file $path = tempnam($path, 'Sub'); $temp_file = fopen($path, 'r+'); // write to temp file fwrite($temp_file, $csv_content); fclose($temp_file); // find the directory we will be using for the final file $path = pathinfo($path); $dir = $path['dirname']; $basename = $path['basename']; // create name for file $new_name = apply_filters('ninja_forms_submission_csv_name', 'ninja-forms-submission'); // remove a file if it already exists if (file_exists($dir . '/' . $new_name . '.csv')) { unlink($dir . '/' . $new_name . '.csv'); } // move file rename($dir . '/' . $basename, $dir . '/' . $new_name . '.csv'); return $dir . '/' . $new_name . '.csv'; }