-
Notifications
You must be signed in to change notification settings - Fork 2
/
wplf-export.php
221 lines (176 loc) · 6.94 KB
/
wplf-export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Plugin name: WPLF Export
* Plugin URI: https://github.com/k1sul1/wplf-export
* Description: Export form data from WP Libre Form
* Version: 1.0.1
* Author: @k1sul1
* Author URI: https://github.com/k1sul1
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl.html
* Text Domain: wplf-export
*/
/** Copyright 2017 Christian Nikkanen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 3, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class WPLF_Export {
public static $instance;
public static function instance() {
if (is_null(self::$instance)) {
self::$instance = new WPLF_Export();
}
return self::$instance;
}
public function __construct() {
add_filter('bulk_actions-edit-wplf-submission', array($this, 'bulkAction'));
add_filter('handle_bulk_actions-edit-wplf-submission', array($this, 'handleBulk'), 10, 3);
add_action('template_redirect', array($this, 'handleDownload'));
add_action('admin_notices', array($this, 'noticeNag'));
}
public function bulkAction($options) {
$options['wplf_export'] = __('Export', 'wplf-export');
return $options;
}
public function handleBulk($redirect_to, $action, $post_ids) {
if ($action !== 'wplf_export') {
return $redirect_to;
}
// Export form id.
$efid = !empty($_GET['form']) ? (int) $_GET['form'] : NULL;
$filter_fn = function($field_name) {
return $field_name[0] !== '_';
};
if (has_action("wplf_export_form_{$efid}_filter")) {
$filter = apply_filters("wplf_export_form_{$efid}_filter", $filter_fn);
} else {
$filter = apply_filters('wplf_export_form_filter', $filter_fn);
}
$header = array();
$rows = array();
$errors = array();
$queryvars = array();
for ($i = 0; $i < count($post_ids); $i++) {
// Should handle uploaded files fine.
$id = $post_ids[$i];
$meta = get_post_meta($id);
$fields = array_filter(array_keys($meta), $filter);
if ($i === 0) {
$header = $fields;
}
$row = array();
foreach ($fields as $field) {
$row[] = $meta[$field][0];
}
if (count($row) !== count($header)) {
$queryvars['wplf_export_error'] = 'ERR_HEADER_ROW_MISMATCH';
$errors[] = array($id => 'ERR_HEADER_ROW_MISMATCH');
}
$rows[] = $row;
}
if (count($errors) > 0) {
error_log(print_r($errors, true));
}
if (has_action("wplf_export_form_{$efid}_delimiter")) {
$delimiter = apply_filters("wplf_export_form_{$efid}_delimiter", ',');
} else {
$delimiter = apply_filters('wplf_export_form_delimiter', ',');
}
$filename = 'wplf_export_' . date('U') . '.csv';
if (has_action("wplf_export_form_{$efid}_filename")) {
$filename = apply_filters("wplf_export_form_{$efid}_filename", $filename);
} else {
$filename = apply_filters('wplf_export_form_filename', $filename);
}
$csvpath = $this->generateCSV($filename, $header, $rows, $delimiter);
$queryvars['wplf_export_path'] = $csvpath;
$queryvars['wplf_exported_posts'] = count($post_ids);
$redirect_to = add_query_arg($queryvars, $redirect_to);
return $redirect_to;
}
public function generateCSV($filename = 'wplf_export.csv', $header = array(), $rows = array(), $delimiter = ',') {
$uploads = wp_upload_dir('wplf'); // meant for date, but works with a string too
$path = $uploads['basedir'] . DIRECTORY_SEPARATOR . 'wplf' . DIRECTORY_SEPARATOR . $filename;
$handle = fopen($path, 'w+');
fputcsv($handle, $header, $delimiter);
foreach ($rows as $row) {
fputcsv($handle, $row, $delimiter);
}
return $path;
}
public function getCSV($filepath) {
$path = $filepath;
if (!file_exists($path)) {
return false;
}
$handle = fopen($path, 'r');
$csv = '';
while(!feof($handle)) {
$csv .= fread($handle, 8192);
}
fclose($handle);
return $csv;
}
public function handleDownload() {
if (parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) === '/wplf-export') {
$allowed = current_user_can(
apply_filters('wplf_export_capability', 'edit_others_posts')
);
$uploads = wp_upload_dir();
$filename = basename($_REQUEST['wplf-export-download']);
// Do not trust the parameter! Only allow files from this folder.
$filepath = $uploads['basedir'] . DIRECTORY_SEPARATOR . 'wplf' . DIRECTORY_SEPARATOR . $filename;
if ($allowed && $filepath) {
$data = $this->getCSV($filepath);
if ($data) {
header("Content-Type: application/csv", true, 200);
header("Content-Disposition: attachment; filename=$filename");
echo $data;
// Remove the file after download
ignore_user_abort(true);
register_shutdown_function('unlink', $filepath); // Never pass unsanitized filepath!
die();
}
}
}
}
public function noticeNag() {
$r = $_REQUEST;
$html = "";
$fpath = !empty($_REQUEST['wplf_export_path']) ? urlencode($_REQUEST['wplf_export_path']) : null;
if (!empty($r['wplf_export_error'])
&& $r['wplf_export_error'] === 'ERR_HEADER_ROW_MISMATCH') {
$html .= "<div id='wplf_export_nag' class='notice notice-error is-dismissible'>";
$html .= "<p>Row item count was different from header item count.
This is usually caused by selecting multiple different forms for
one export, or modifying the form fields after it has submissions.</p>
<p>If the imported file seems to be broken, select only one type
of form and try exporting again.</p>";
$html .= "<p>The download should start automatically.
<a href='/wplf-export?wplf-export-download=$fpath' id='wplf_export_save' target='_blank'>
If it doesn't click here.
</a></p>";
$html .= "</div>";
} else if (!empty($r['wplf_exported_posts'])) {
$count = (int) $r['wplf_exported_posts'];
$html .= "<div id='wplf_export_nag' class='notice notice-success is-dismissible'>";
$html .= "<p>Exported $count submissions.</p>";
$html .= "<p>The download should start automatically.
<a href='/wplf-export?wplf-export-download=$fpath' id='wplf_export_save' target='_blank'>
If it doesn't click here.
</a></p>";
$html .= "</div>";
$html .= "<script>setTimeout(function(){document.getElementById('wplf_export_save').click();},100);</script>";
}
echo $html;
}
}
WPLF_Export::instance();