PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.0.2
GiveWP – Donation Plugin and Fundraising Platform v3.0.2
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / tools / export / class-batch-export.php
give / includes / admin / tools / export Last commit date
class-batch-export-donors.php 5 years ago class-batch-export.php 4 years ago class-core-settings-export.php 7 years ago class-export-earnings.php 3 years ago class-export.php 3 years ago class-give-export-donations.php 5 years ago export-actions.php 3 years ago export-functions.php 4 years ago give-export-donations-exporter.php 3 years ago give-export-donations-functions.php 5 years ago pdf-reports.php 5 years ago
class-batch-export.php
362 lines
1 <?php
2 /**
3 * Batch Export Class
4 *
5 * This is the base class for all batch export methods. Each data export type (donors, payments, etc) extend this class.
6 *
7 * @package Give
8 * @since 1.5
9 * @copyright Copyright (c) 2016, GiveWP
10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 * @subpackage Admin/Export
12 */
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Give_Batch_Export Class
21 *
22 * @since 1.5
23 */
24 class Give_Batch_Export extends Give_Export {
25
26 /**
27 * The file the data is stored in.
28 *
29 * @since 1.5
30 */
31 private $file;
32
33 /**
34 * The name of the file the data is stored in.
35 *
36 * @since 1.5
37 */
38 public $filename;
39
40 /**
41 * The file type, typically .csv
42 *
43 * @since 1.5
44 */
45 public $filetype;
46
47 /**
48 * The current step being processed.
49 *
50 * @since 1.5
51 */
52 public $step;
53
54 /**
55 * Start date, Y-m-d H:i:s
56 *
57 * @since 1.5
58 */
59 public $start;
60
61 /**
62 * End date, Y-m-d H:i:s
63 *
64 * @since 1.5
65 */
66 public $end;
67
68 /**
69 * Status to export.
70 *
71 * @since 1.5
72 */
73 public $status;
74
75 /**
76 * Form to export data for.
77 *
78 * @since 1.5
79 */
80 public $form = null;
81
82 /**
83 * Form Price ID to export data for.
84 *
85 * @since 1.5
86 */
87 public $price_id = null;
88
89 /**
90 * Is the export file writable.
91 *
92 * @since 1.5
93 */
94 public $is_writable = true;
95
96 /**
97 * Is the export file empty.
98 *
99 * @since 1.5
100 */
101 public $is_empty = false;
102
103 /**
104 *
105 * @since 1.8.9
106 */
107 public $is_void = false;
108
109 /**
110 * Is the export file complete.
111 *
112 * @since 1.8.9
113 */
114 public $done = false;
115
116 /**
117 * Give_Batch_Export constructor.
118 *
119 * @since 2.21.0 Create only csv file.
120 * @since 2.9.0 add hash to filename to avoid collisions
121 * @since 1.5
122 *
123 * @param int $_step
124 * @param string|null $filename
125 */
126 public function __construct( $_step = 1, $filename = null ) {
127 $upload_dir = wp_upload_dir();
128 $this->filetype = '.csv';
129
130 if ( null === $filename ) {
131 $hash = uniqid();
132 $this->filename = "give-{$hash}-{$this->export_type}{$this->filetype}";
133 } else {
134 $this->filename = "{$filename}{$this->filetype}";
135 }
136
137 $this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename;
138
139 if ( ! is_writable( $upload_dir['basedir'] ) ) {
140 $this->is_writable = false;
141 }
142
143 $this->step = $_step;
144 $this->done = false;
145 }
146
147 /**
148 * Process a step.
149 *
150 * @since 1.5
151 * @return bool
152 */
153 public function process_step() {
154
155 if ( ! $this->can_export() ) {
156 wp_die(
157 esc_html__( 'You do not have permission to export data.', 'give' ),
158 esc_html__( 'Error', 'give' ),
159 [
160 'response' => 403,
161 ]
162 );
163 }
164
165 if ( $this->step < 2 ) {
166
167 // Make sure we start with a fresh file on step 1.
168 @unlink( $this->file );
169 $this->print_csv_cols();
170 }
171
172 $this->print_csv_rows();
173
174 return 100 !== $this->get_percentage_complete();
175 }
176
177 /**
178 * Output the CSV columns.
179 *
180 * @access public
181 * @since 1.5
182 * @return string
183 * @uses Give_Export::get_csv_cols()
184 */
185 public function print_csv_cols() {
186
187 $col_data = '';
188 $cols = $this->get_csv_cols();
189 $i = 1;
190 foreach ( $cols as $col_id => $column ) {
191 $col_data .= '"' . addslashes( $column ) . '"';
192 $col_data .= $i == count( $cols ) ? '' : ',';
193 $i ++;
194 }
195 $col_data .= "\r\n";
196
197 $this->stash_step_data( $col_data );
198
199 return $col_data;
200
201 }
202
203 /**
204 * Print the CSV rows for the current step.
205 *
206 * @access public
207 * @since 1.5
208 * @return string|false
209 */
210 public function print_csv_rows() {
211
212 $row_data = '';
213 $data = $this->get_data();
214 $cols = $this->get_csv_cols();
215
216 if ( $data ) {
217
218 // Output each row
219 foreach ( $data as $row ) {
220 $i = 1;
221 foreach ( $row as $col_id => $column ) {
222 // Make sure the column is valid
223 if ( array_key_exists( $col_id, $cols ) ) {
224 $row_data .= '"' . addslashes( preg_replace( '/"/', "'", $column ) ) . '"';
225 $row_data .= $i == count( $cols ) ? '' : ',';
226 $i ++;
227 }
228 }
229 $row_data .= "\r\n";
230 }
231
232 $this->stash_step_data( $row_data );
233
234 return $row_data;
235 }
236
237 return false;
238 }
239
240 /**
241 * Return the calculated completion percentage.
242 *
243 * @since 1.5
244 * @return int
245 */
246 public function get_percentage_complete() {
247 return 100;
248 }
249
250 /**
251 * Retrieve the file data is written to.
252 *
253 * @since 1.5
254 * @return string
255 */
256 protected function get_file() {
257
258 $file = '';
259
260 if ( @file_exists( $this->file ) ) {
261
262 if ( ! is_writable( $this->file ) ) {
263 $this->is_writable = false;
264 }
265
266 $file = @file_get_contents( $this->file );
267
268 } else {
269
270 @file_put_contents( $this->file, '' );
271 @chmod( $this->file, 0664 );
272
273 }
274
275 return $file;
276 }
277
278 /**
279 * Append data to export file.
280 *
281 * @since 1.5
282 *
283 * @param $data string The data to add to the file.
284 *
285 * @return void
286 */
287 protected function stash_step_data( $data = '' ) {
288
289 $file = $this->get_file();
290 $file .= $data;
291 @file_put_contents( $this->file, $file );
292
293 // If we have no rows after this step, mark it as an empty export.
294 $file_rows = file( $this->file, FILE_SKIP_EMPTY_LINES );
295 $default_cols = $this->get_csv_cols();
296 $default_cols = empty( $default_cols ) ? 0 : 1;
297
298 $this->is_empty = count( $file_rows ) == $default_cols ? true : false;
299
300 }
301
302 /**
303 * Perform the export.
304 *
305 * @access public
306 * @since 1.5
307 * @return void
308 */
309 public function export() {
310
311 // Set headers
312 $this->headers();
313
314 $file = $this->get_file();
315
316 @unlink( $this->file );
317
318 echo $file;
319
320 /**
321 * Fire action after file output.
322 *
323 * @since 1.8
324 */
325 do_action( 'give_file_export_complete', $_REQUEST );
326
327 give_die();
328 }
329
330 /**
331 * Set the properties specific to the export.
332 *
333 * @since 1.5
334 *
335 * @param array $request The Form Data passed into the batch processing.
336 */
337 public function set_properties( $request ) {
338 }
339
340 /**
341 * Unset the properties specific to the export.
342 *
343 * @since 1.8.9
344 *
345 * @param array $request The Form Data passed into the batch processing.
346 * @param Give_Batch_Export $export
347 */
348 public function unset_properties( $request, $export ) {
349 }
350
351 /**
352 * Allow for pre-fetching of data for the remainder of the exporter.
353 *
354 * @access public
355 * @since 1.5
356 * @return void
357 */
358 public function pre_fetch() {
359 }
360
361 }
362