PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.7.1
GiveWP – Donation Plugin and Fundraising Platform v2.7.1
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-donors.php
give / includes / admin / tools / export Last commit date
class-batch-export-donors.php 7 years ago class-batch-export.php 6 years ago class-core-settings-export.php 7 years ago class-export-earnings.php 6 years ago class-export.php 6 years ago class-give-export-donations.php 6 years ago export-actions.php 6 years ago export-functions.php 6 years ago give-export-donations-exporter.php 6 years ago give-export-donations-functions.php 6 years ago pdf-reports.php 6 years ago
class-batch-export-donors.php
478 lines
1 <?php
2 /**
3 * Batch Donors Export Class
4 *
5 * This class handles donor export.
6 *
7 * @package Give
8 * @subpackage Admin/Reports
9 * @copyright Copyright (c) 2016, GiveWP
10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 * @since 1.5
12 */
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Give_Batch_Donors_Export Class
21 *
22 * @since 1.5
23 */
24 class Give_Batch_Donors_Export extends Give_Batch_Export {
25
26 /**
27 * Our export type. Used for export-type specific filters/actions.
28 *
29 * @var string
30 * @since 1.5
31 */
32 public $export_type = 'donors';
33
34 /**
35 * Form submission data
36 *
37 * @var array
38 * @since 1.5
39 */
40 private $data = array();
41
42 /**
43 * Array of donor ids which is already included in csv file.
44 *
45 * @since 1.8
46 * @var array
47 */
48 private $donor_ids = array();
49
50 /**
51 * Array of payment stats which is already included in csv file.
52 *
53 * @since 1.8.9
54 * @var array
55 */
56 private $payment_stats = array();
57
58 /**
59 * Export query id.
60 *
61 * @since 1.8
62 * @var string
63 */
64 private $query_id = '';
65
66 /**
67 * Give_Batch_Export constructor.
68 *
69 * @param int $_step
70 *
71 * @since 2.1.0
72 */
73 public function __construct( $_step = 1 ) {
74
75 parent::__construct( $_step );
76
77 // Filter to change the filename.
78 add_filter( 'give_export_filename', array( $this, 'give_export_filename' ), 10, 2 );
79 }
80
81 /**
82 * Function to change the filename
83 *
84 * @param string $filename File name.
85 * @param string $export_type export type.
86 *
87 * @return string $filename file name.
88 * @since 2.1.0
89 */
90 public function give_export_filename( $filename, $export_type ) {
91
92 if ( $this->export_type !== $export_type ) {
93 return $filename;
94 }
95
96 $forms = empty( $_GET['forms'] ) ? 0 : absint( $_GET['forms'] );
97
98 if ( $forms ) {
99 $slug = get_post_field( 'post_name', get_post( $forms ) );
100 $filename = 'give-export-donors-' . $slug . '-' . date( 'm-d-Y' );
101 } else {
102 $filename = 'give-export-donors-all-forms-' . date( 'm-d-Y' );
103 }
104
105 return $filename;
106 }
107
108 /**
109 * Set the properties specific to the donors export.
110 *
111 * @param array $request The Form Data passed into the batch processing
112 *
113 * @since 1.5
114 */
115 public function set_properties( $request ) {
116
117 // Set data from form submission
118 if ( isset( $_POST['form'] ) ) {
119 parse_str( $_POST['form'], $this->data );
120 }
121
122 $this->form = $this->data['forms'];
123
124 // Setup donor ids cache.
125 if ( ! empty( $this->form ) ) {
126 // Cache donor ids to output unique list of donor.
127 $this->query_id = give_clean( $_REQUEST['give_export_option']['query_id'] );
128 $this->cache_donor_ids();
129 }
130
131 $this->price_id = give_clean( $request['give_price_option'] );
132 $this->price_id = isset( $request['give_price_option'] ) && ! in_array( $this->price_id, array( 'all', '' ) )
133 ? absint( $request['give_price_option'] )
134 : null;
135 }
136
137 /**
138 * Cache donor ids.
139 *
140 * @since 1.8.9
141 * @access private
142 */
143 private function cache_donor_ids() {
144 // Fetch already cached donor ids.
145 $donor_ids = $this->donor_ids;
146
147 if ( $cached_donor_ids = Give_Cache::get( $this->query_id, true ) ) {
148 $donor_ids = array_unique( array_merge( $cached_donor_ids, $this->donor_ids ) );
149 }
150
151 $donor_ids = array_values( $donor_ids );
152 Give_Cache::set( $this->query_id, $donor_ids, HOUR_IN_SECONDS, true );
153 }
154
155 /**
156 * Set the CSV columns.
157 *
158 * @access public
159 * @return array|bool $cols All the columns.
160 * @since 1.5
161 */
162 public function csv_cols() {
163
164 $columns = give_export_donors_get_default_columns();
165 $cols = $this->get_cols( $columns );
166
167 return $cols;
168 }
169
170 /**
171 * CSV file columns.
172 *
173 * @param array $columns
174 *
175 * @return array
176 */
177 private function get_cols( $columns ) {
178
179 $cols = array();
180
181 foreach ( $columns as $key => $value ) {
182
183 switch ( $key ) {
184
185 case 'address':
186 $cols['address_line1'] = esc_html__( 'Address', 'give' );
187 $cols['address_line2'] = esc_html__( 'Address 2', 'give' );
188 $cols['address_city'] = esc_html__( 'City', 'give' );
189 $cols['address_state'] = esc_html__( 'State', 'give' );
190 $cols['address_zip'] = esc_html__( 'Zip', 'give' );
191 $cols['address_country'] = esc_html__( 'Country', 'give' );
192 break;
193
194 default:
195 $cols[ $key ] = $value;
196 break;
197 }
198 }
199
200 return $cols;
201
202 }
203
204
205 /**
206 * Get donation query arguments
207 *
208 * @return array
209 * @since 2.4.5
210 */
211 private function get_donation_query_args() {
212 // Export donors for a specific donation form and also within specified time frame.
213 $args = array(
214 'output' => 'payments',
215 'post_type' => array( 'give_payment' ),
216 'number' => 30,
217 'paged' => $this->step,
218 'status' => 'publish',
219 'meta_key' => '_give_payment_form_id',
220 'meta_value' => absint( $this->form ),
221 );
222
223 // Check for date option filter.
224 if ( ! empty( $this->data['donor_export_start_date'] ) || ! empty( $this->data['donor_export_end_date'] ) ) {
225 // Start date.
226 $start_date = ! empty( $this->data['donor_export_start_date'] ) ? sanitize_text_field( $this->data['donor_export_start_date'] ) : '';
227 if ( ! empty( $start_date ) ) {
228 $start_date = date( 'Y-m-d', strtotime( $start_date ) );
229 $args['start_date'] = $start_date;
230 }
231
232 // End date.
233 $end_date = ! empty( $this->data['donor_export_end_date'] )
234 ? date( 'Y-m-d', strtotime( sanitize_text_field( $this->data['donor_export_end_date'] ) ) )
235 : date( 'Y-m-d', current_time( 'timestamp' ) );
236 $end_date = "{$end_date} 23:59:59";
237 $args['end_date'] = $end_date;
238 }
239
240 // Check for price option.
241 if ( null !== $this->price_id ) {
242 $args['meta_query'] = array(
243 array(
244 'key' => '_give_payment_price_id',
245 'value' => (int) $this->price_id,
246 ),
247 );
248 }
249
250 return $args;
251 }
252
253 /**
254 * Get the Export Data
255 *
256 * @access public
257 * @return array $data The data for the CSV file.
258 * @since 1.0
259 */
260 public function get_data() {
261 $i = 0;
262
263 $data = array();
264 $cached_donor_ids = Give_Cache::get( $this->query_id, true );
265
266 if ( ! empty( $this->form ) ) {
267 $args = $this->get_donation_query_args();
268
269 $payments_query = new Give_Payments_Query( $args );
270 $payments = $payments_query->get_payments();
271
272 if ( $payments ) {
273 /* @var Give_Payment $payment */
274 foreach ( $payments as $payment ) {
275 // Set donation sum.
276 $this->payment_stats[ $payment->customer_id ]['donation_sum'] = isset( $this->payment_stats[ $payment->customer_id ]['donation_sum'] ) ?
277 $this->payment_stats[ $payment->customer_id ]['donation_sum'] :
278 0;
279 $this->payment_stats[ $payment->customer_id ]['donation_sum'] += $payment->total;
280
281 // Set donation count.
282 $this->payment_stats[ $payment->customer_id ]['donations'] = isset( $this->payment_stats[ $payment->customer_id ]['donations'] ) ?
283 ++ $this->payment_stats[ $payment->customer_id ]['donations'] :
284 1;
285
286 // Set donation form name.
287 $this->payment_stats[ $payment->customer_id ]['form_title'] = $payment->form_title;
288
289 // Continue if donor already included.
290 if ( empty( $payment->customer_id ) ||
291 in_array( $payment->customer_id, $cached_donor_ids )
292 ) {
293 continue;
294 }
295
296 $this->donor_ids[] = $cached_donor_ids[] = $payment->customer_id;
297
298 $i ++;
299 }
300
301 if ( ! empty( $this->donor_ids ) ) {
302 foreach ( $this->donor_ids as $donor_id ) {
303 $donor = Give()->donors->get_donor_by( 'id', $donor_id );
304 $donor->purchase_count = $this->payment_stats[ $donor_id ]['donations'];
305 $donor->purchase_value = $this->payment_stats[ $donor_id ]['donation_sum'];
306 $data[] = $this->set_donor_data( $i, $data, $donor );
307 }
308
309 // Cache donor ids only if admin export donor for specific form.
310 $this->cache_donor_ids();
311 }
312 } // End if().
313 } else {
314
315 // Export all donors.
316 $offset = 30 * ( $this->step - 1 );
317
318 $args = array(
319 'number' => 30,
320 'offset' => $offset,
321 );
322
323 // Check for date option filter.
324 if (
325 ! empty( $this->data['donor_export_start_date'] )
326 || ! empty( $this->data['donor_export_end_date'] )
327 ) {
328
329 // Start date.
330 $start_date = ! empty( $this->data['donor_export_start_date'] ) ? sanitize_text_field( $this->data['donor_export_start_date'] ) : '';
331 if ( ! empty( $start_date ) ) {
332 $start_date = date( 'Y-m-d', strtotime( $start_date ) );
333 $args['date']['start'] = $start_date;
334 }
335
336 // End date.
337 $end_date = ! empty( $this->data['donor_export_end_date'] )
338 ? date( 'Y-m-d', strtotime( sanitize_text_field( $this->data['donor_export_end_date'] ) ) )
339 : date( 'Y-m-d', current_time( 'timestamp' ) );
340 $end_date = "{$end_date} 23:59:59";
341 $args['date']['end'] = $end_date;
342
343 }
344
345 $donors = Give()->donors->get_donors( $args );
346
347 foreach ( $donors as $donor ) {
348
349 // Continue if donor already included.
350 if ( empty( $donor->id ) || empty( $donor->payment_ids ) ) {
351 continue;
352 }
353
354 $data[] = $this->set_donor_data( $i, $data, $donor );
355 $i ++;
356 }
357 }// End if().
358
359 $data = apply_filters( 'give_export_get_data', $data );
360 $data = apply_filters( "give_export_get_data_{$this->export_type}", $data );
361
362 return $data;
363 }
364
365 /**
366 * Return the calculated completion percentage.
367 *
368 * @return int
369 * @since 1.5
370 */
371 public function get_percentage_complete() {
372
373 $percentage = 0;
374
375 // We can't count the number when getting donors for a specific form.
376 if ( empty( $this->form ) ) {
377
378 $total = Give()->donors->count();
379
380 if ( $total > 0 ) {
381
382 $percentage = ( ( 30 * $this->step ) / $total ) * 100;
383
384 }
385 } else {
386 // Calculate donations if form id set
387 $args = $this->get_donation_query_args();
388 $donations = new Give_Payments_Query( $args );
389
390 if ( empty( $donations->get_payments() ) ) {
391 $percentage = 100;
392 } else {
393 $tmp_number = $args['number'];
394 $tmp_paged = $args['paged'];
395
396 unset( $args['paged'] );
397 $args['number'] = - 1;
398 $total_donations = new Give_Payments_Query( $args );
399 $total_donations = count( $total_donations->get_payments() );
400 $percentage = ( ( $tmp_number * $tmp_paged ) / $total_donations ) * 100;
401 }
402 }
403
404 if ( $percentage > 100 ) {
405 $percentage = 100;
406 }
407
408 return $percentage;
409 }
410
411 /**
412 * Set Donor Data
413 *
414 * @param int $i CSV line.
415 * @param array $data Donor CSV data.
416 * @param object $donor Donor data.
417 *
418 * @return mixed
419 */
420 private function set_donor_data( $i, $data, $donor ) {
421
422 $columns = $this->csv_cols();
423
424 // Set address variable.
425 $address = '';
426 if ( isset( $donor->id ) && $donor->id > 0 ) {
427 $address = give_get_donor_address( $donor->id );
428 }
429
430 // Set columns.
431 if ( ! empty( $columns['full_name'] ) ) {
432 $donor_name = give_get_donor_name_by( $donor->id, 'donor' );
433 $data[ $i ]['full_name'] = $donor_name;
434 }
435 if ( ! empty( $columns['email'] ) ) {
436 $data[ $i ]['email'] = $donor->email;
437 }
438 if ( ! empty( $columns['address_line1'] ) ) {
439
440 $data[ $i ]['address_line1'] = isset( $address['line1'] ) ? $address['line1'] : '';
441 $data[ $i ]['address_line2'] = isset( $address['line2'] ) ? $address['line2'] : '';
442 $data[ $i ]['address_city'] = isset( $address['city'] ) ? $address['city'] : '';
443 $data[ $i ]['address_state'] = isset( $address['state'] ) ? $address['state'] : '';
444 $data[ $i ]['address_zip'] = isset( $address['zip'] ) ? $address['zip'] : '';
445 $data[ $i ]['address_country'] = isset( $address['country'] ) ? $address['country'] : '';
446 }
447 if ( ! empty( $columns['userid'] ) ) {
448 $data[ $i ]['userid'] = ! empty( $donor->user_id ) ? $donor->user_id : '';
449 }
450 if ( ! empty( $columns['donor_created_date'] ) ) {
451 $data[ $i ]['donor_created_date'] = date_i18n( give_date_format(), strtotime( $donor->date_created ) );
452 }
453 if ( ! empty( $columns['donations'] ) ) {
454 $data[ $i ]['donations'] = $donor->purchase_count;
455 }
456 if ( ! empty( $columns['donation_sum'] ) ) {
457 $data[ $i ]['donation_sum'] = give_format_amount( $donor->purchase_value, array( 'sanitize' => false ) );
458 }
459
460 $data[ $i ] = apply_filters( 'give_export_set_donor_data', $data[ $i ], $donor );
461
462 return $data[ $i ];
463
464 }
465
466 /**
467 * Unset the properties specific to the donors export.
468 *
469 * @param array $request
470 * @param Give_Batch_Export $export
471 */
472 public function unset_properties( $request, $export ) {
473 if ( $export->done ) {
474 Give_Cache::delete( "give_cache_{$this->query_id}" );
475 }
476 }
477 }
478