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