PluginProbe
Database Addon For WPForms ( wpforms entries ) – WPFormsDB / trunk
Database Addon For WPForms ( wpforms entries ) – WPFormsDB vtrunk
trunk 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.8.1 1.0.9 1.1.0
database-for-wpforms / inc / class-export-csv.php

class-export-csv.php in Database Addon For WPForms ( wpforms entries ) – WPFormsDB trunk, at inc/class-export-csv.php

132 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPFormsDB csv
4 */
5
6 if (!defined( 'ABSPATH')) exit;
7
8 class WPFormsDB_Export_CSV{
9
10 /**
11 * Download csv file
12 * @param String $filename
13 * @return file
14 */
15 public function download_send_headers( $filename ) {
16 // disable caching
17 $now = gmdate("D, d M Y H:i:s");
18 header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
19 header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
20 header("Last-Modified: {$now} GMT");
21
22 // force download
23 header("Content-Type: application/force-download");
24 header("Content-Type: application/octet-stream");
25 header("Content-Type: application/download");
26
27 // disposition / encoding on response body
28 header("Content-Disposition: attachment;filename={$filename}");
29 header("Content-Transfer-Encoding: binary");
30
31 }
32 /**
33 * Convert array to csv format
34 * @param array &$array
35 * @return file csv format
36 */
37 public function array2csv(array &$array, $df){
38
39 if (count($array) == 0) {
40 return null;
41 }
42
43 $array_keys = array_keys($array);
44 $heading = array();
45 $unwanted = array('WPFormsDB_', 'your-');
46
47 foreach ( $array_keys as $aKeys ) {
48 $tmp = str_replace( $unwanted, '', $aKeys );
49 $heading[] = ucfirst( $tmp );
50 }
51 fputcsv( $df, $heading );
52
53 foreach ( $array['form_id'] as $line => $form_id ) {
54 $line_values = array();
55 foreach($array_keys as $array_key ) {
56 $val = isset( $array[ $array_key ][ $line ] ) ? $array[ $array_key ][ $line ] : '';
57 $line_values[ $array_key ] = $val;
58 }
59 fputcsv($df, $line_values);
60 }
61 }
62 /**
63 * Download file
64 * @return csv file
65 */
66 public function download_csv_file(){
67
68 global $wpdb;
69 $wpforms = apply_filters( 'WPFormsDB_database', $wpdb );
70 $table_name = $wpforms->prefix.'wpforms_db';
71
72 if( isset($_REQUEST['wpforms-csv']) && isset( $_REQUEST['nonce'] ) ){
73
74 if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'dnonce')) {
75
76 wp_die( 'Not Valid.. Download nonce..!! ' );
77 }
78 $fid = (int)$_REQUEST['fid'];
79 $heading_row = $wpforms->get_results("SELECT form_id, form_value, form_date FROM $table_name
80 WHERE form_post_id = '$fid' ORDER BY form_id DESC LIMIT 1",OBJECT);
81
82 $heading_row = reset( $heading_row );
83 $heading_row = unserialize( $heading_row->form_value, ['allowed_classes' => false] );
84 $heading_key = array_keys( $heading_row );
85
86 $total_rows = $wpforms->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = '$fid' ");
87 $per_query = 1000;
88 $total_query = ( $total_rows / $per_query );
89
90 $this->download_send_headers( "WPFormsDB-" . gmdate("Y-m-d") . ".csv" );
91 $df = fopen("php://output", 'w');
92 ob_start();
93
94 for( $p = 0; $p <= $total_query; $p++ ){
95
96 $offset = $p * $per_query;
97 $results = $wpforms->get_results("SELECT form_id, form_value, form_date FROM $table_name
98 WHERE form_post_id = '$fid' ORDER BY form_id DESC LIMIT $offset, $per_query",OBJECT);
99
100 $data = array();
101 $i = 0;
102 foreach ($results as $result) :
103
104 $i++;
105 $data['form_id'][$i] = $result->form_id;
106 $data['form_date'][$i] = $result->form_date;
107 $resultTmp = unserialize( $result->form_value );
108
109 foreach ($resultTmp as $key => $value):
110 if ( ! in_array( $key, $heading_key ) ) continue;
111 if ( is_array($value) ){
112
113 $data[$key][$i] = implode(', ', $value);
114 continue;
115 }
116
117 $data[$key][$i] = str_replace( array('&quot;','&#039;','&#047;','&#092;')
118 , array('"',"'",'/','\\'), $value );
119
120 endforeach;
121
122 endforeach;
123
124 echo $this->array2csv( $data, $df );
125
126 }
127 echo ob_get_clean();
128 fclose( $df ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
129 die();
130 }
131 }
132 }