← All changes
|
includes/admin/tools/export/class-batch-export-donors.php
+108
-70
2.3.1
→
4.18.0
View file →
| @@ -36,9 +36,9 @@ | ||
| 36 | 36 | * |
| 37 | 37 | * @var array |
| 38 | 38 | * @since 1.5 |
| 39 | 39 | */ |
| 40 | - private $data = array(); | |
| 40 | + private $data = []; | |
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Array of donor ids which is already included in csv file. |
| 44 | 44 | * |
| @@ -44,9 +44,9 @@ | ||
| 44 | 44 | * |
| 45 | 45 | * @since 1.8 |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - private $donor_ids = array(); | |
| 48 | + private $donor_ids = []; | |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Array of payment stats which is already included in csv file. |
| 52 | 52 | * |
| @@ -52,9 +52,9 @@ | ||
| 52 | 52 | * |
| 53 | 53 | * @since 1.8.9 |
| 54 | 54 | * @var array |
| 55 | 55 | */ |
| 56 | - private $payment_stats = array(); | |
| 56 | + private $payment_stats = []; | |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * Export query id. |
| 60 | 60 | * |
| @@ -65,29 +65,29 @@ | ||
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Give_Batch_Export constructor. |
| 68 | 68 | * |
| 69 | + * @since 2.9.0 add filename to support parent constructor | |
| 69 | 70 | * @since 2.1.0 |
| 70 | 71 | * |
| 71 | - * @param int $_step | |
| 72 | + * @param null $filename | |
| 73 | + * @param int $_step | |
| 72 | 74 | */ |
| 73 | - public function __construct( $_step = 1 ) { | |
| 75 | + public function __construct( $_step = 1, $filename = null ) { | |
| 76 | + parent::__construct( $_step, $filename ); | |
| 74 | 77 | |
| 75 | - parent::__construct( $_step ); | |
| 76 | - | |
| 77 | 78 | // Filter to change the filename. |
| 78 | - add_filter( 'give_export_filename', array( $this, 'give_export_filename' ), 10, 2 ); | |
| 79 | + add_filter( 'give_export_filename', [ $this, 'give_export_filename' ], 10, 2 ); | |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | 82 | /** |
| 82 | 83 | * Function to change the filename |
| 83 | 84 | * |
| 84 | - * @since 2.1.0 | |
| 85 | - * | |
| 86 | - * @param string $filename File name. | |
| 85 | + * @param string $filename File name. | |
| 87 | 86 | * @param string $export_type export type. |
| 88 | 87 | * |
| 89 | 88 | * @return string $filename file name. |
| 89 | + * @since 2.1.0 | |
| 90 | 90 | */ |
| 91 | 91 | public function give_export_filename( $filename, $export_type ) { |
| 92 | 92 | |
| 93 | 93 | if ( $this->export_type !== $export_type ) { |
| @@ -108,11 +108,11 @@ | ||
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * Set the properties specific to the donors export. |
| 111 | 111 | * |
| 112 | + * @param array $request The Form Data passed into the batch processing | |
| 113 | + * | |
| 112 | 114 | * @since 1.5 |
| 113 | - * | |
| 114 | - * @param array $request The Form Data passed into the batch processing | |
| 115 | 115 | */ |
| 116 | 116 | public function set_properties( $request ) { |
| 117 | 117 | |
| 118 | 118 | // Set data from form submission |
| @@ -128,9 +128,12 @@ | ||
| 128 | 128 | $this->query_id = give_clean( $_REQUEST['give_export_option']['query_id'] ); |
| 129 | 129 | $this->cache_donor_ids(); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - $this->price_id = ! empty( $request['give_price_option'] ) && 'all' !== $request['give_price_option'] ? absint( $request['give_price_option'] ) : null; | |
| 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; | |
| 133 | 136 | } |
| 134 | 137 | |
| 135 | 138 | /** |
| 136 | 139 | * Cache donor ids. |
| @@ -153,10 +156,10 @@ | ||
| 153 | 156 | /** |
| 154 | 157 | * Set the CSV columns. |
| 155 | 158 | * |
| 156 | 159 | * @access public |
| 160 | + * @return array|bool $cols All the columns. | |
| 157 | 161 | * @since 1.5 |
| 158 | - * @return array|bool $cols All the columns. | |
| 159 | 162 | */ |
| 160 | 163 | public function csv_cols() { |
| 161 | 164 | |
| 162 | 165 | $columns = give_export_donors_get_default_columns(); |
| @@ -173,15 +176,15 @@ | ||
| 173 | 176 | * @return array |
| 174 | 177 | */ |
| 175 | 178 | private function get_cols( $columns ) { |
| 176 | 179 | |
| 177 | - $cols = array(); | |
| 180 | + $cols = []; | |
| 178 | 181 | |
| 179 | 182 | foreach ( $columns as $key => $value ) { |
| 180 | 183 | |
| 181 | 184 | switch ( $key ) { |
| 182 | 185 | |
| 183 | - case 'address' : | |
| 186 | + case 'address': | |
| 184 | 187 | $cols['address_line1'] = esc_html__( 'Address', 'give' ); |
| 185 | 188 | $cols['address_line2'] = esc_html__( 'Address 2', 'give' ); |
| 186 | 189 | $cols['address_city'] = esc_html__( 'City', 'give' ); |
| 187 | 190 | $cols['address_state'] = esc_html__( 'State', 'give' ); |
| @@ -198,60 +201,73 @@ | ||
| 198 | 201 | return $cols; |
| 199 | 202 | |
| 200 | 203 | } |
| 201 | 204 | |
| 205 | + | |
| 202 | 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 | + /** | |
| 203 | 255 | * Get the Export Data |
| 204 | 256 | * |
| 205 | 257 | * @access public |
| 258 | + * @return array $data The data for the CSV file. | |
| 206 | 259 | * @since 1.0 |
| 207 | - * | |
| 208 | - * @return array $data The data for the CSV file. | |
| 209 | 260 | */ |
| 210 | 261 | public function get_data() { |
| 211 | 262 | $i = 0; |
| 212 | 263 | |
| 213 | - $data = array(); | |
| 264 | + $data = []; | |
| 214 | 265 | $cached_donor_ids = Give_Cache::get( $this->query_id, true ); |
| 215 | 266 | |
| 216 | 267 | if ( ! empty( $this->form ) ) { |
| 268 | + $args = $this->get_donation_query_args(); | |
| 217 | 269 | |
| 218 | - // Export donors for a specific donation form and also within specified timeframe. | |
| 219 | - $args = array( | |
| 220 | - 'output' => 'payments', | |
| 221 | - 'post_type' => array( 'give_payment' ), | |
| 222 | - 'number' => 30, | |
| 223 | - 'paged' => $this->step, | |
| 224 | - 'status' => 'publish', | |
| 225 | - 'meta_key' => '_give_payment_form_id', | |
| 226 | - 'meta_value' => absint( $this->form ), | |
| 227 | - ); | |
| 228 | - | |
| 229 | - // Check for date option filter. | |
| 230 | - if ( ! empty( $this->data['donor_export_start_date'] ) || ! empty( $this->data['donor_export_end_date'] ) ) { | |
| 231 | - // Start date. | |
| 232 | - $start_date = ! empty( $this->data['donor_export_start_date'] ) ? sanitize_text_field( $this->data['donor_export_start_date'] ) : ''; | |
| 233 | - if ( ! empty( $start_date ) ) { | |
| 234 | - $start_date = give_get_formatted_date( $start_date ); | |
| 235 | - $args['start_date'] = $start_date; | |
| 236 | - } | |
| 237 | - | |
| 238 | - // End date. | |
| 239 | - $end_date = ! empty( $this->data['donor_export_end_date'] ) ? sanitize_text_field( $this->data['donor_export_end_date'] ) : date( give_date_format(), current_time( 'timestamp' ) ); | |
| 240 | - $end_date = give_get_formatted_date( $end_date ); | |
| 241 | - $args['end_date'] = $end_date; | |
| 242 | - } | |
| 243 | - | |
| 244 | - // Check for price option. | |
| 245 | - if ( null !== $this->price_id ) { | |
| 246 | - $args['meta_query'] = array( | |
| 247 | - array( | |
| 248 | - 'key' => '_give_payment_price_id', | |
| 249 | - 'value' => (int) $this->price_id, | |
| 250 | - ), | |
| 251 | - ); | |
| 252 | - } | |
| 253 | - | |
| 254 | 270 | $payments_query = new Give_Payments_Query( $args ); |
| 255 | 271 | $payments = $payments_query->get_payments(); |
| 256 | 272 | |
| 257 | 273 | if ( $payments ) { |
| @@ -257,9 +273,9 @@ | ||
| 257 | 273 | if ( $payments ) { |
| 258 | 274 | /* @var Give_Payment $payment */ |
| 259 | 275 | foreach ( $payments as $payment ) { |
| 260 | 276 | // Set donation sum. |
| 261 | - $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'] = isset( $this->payment_stats[ $payment->customer_id ]['donation_sum'] ) ? | |
| 262 | 278 | $this->payment_stats[ $payment->customer_id ]['donation_sum'] : |
| 263 | 279 | 0; |
| 264 | 280 | $this->payment_stats[ $payment->customer_id ]['donation_sum'] += $payment->total; |
| 265 | 281 | |
| @@ -272,9 +288,9 @@ | ||
| 272 | 288 | $this->payment_stats[ $payment->customer_id ]['form_title'] = $payment->form_title; |
| 273 | 289 | |
| 274 | 290 | // Continue if donor already included. |
| 275 | 291 | if ( empty( $payment->customer_id ) || |
| 276 | - in_array( $payment->customer_id, $cached_donor_ids ) | |
| 292 | + in_array( $payment->customer_id, $cached_donor_ids ) | |
| 277 | 293 | ) { |
| 278 | 294 | continue; |
| 279 | 295 | } |
| 280 | 296 | |
| @@ -299,26 +315,31 @@ | ||
| 299 | 315 | |
| 300 | 316 | // Export all donors. |
| 301 | 317 | $offset = 30 * ( $this->step - 1 ); |
| 302 | 318 | |
| 303 | - $args = array( | |
| 319 | + $args = [ | |
| 304 | 320 | 'number' => 30, |
| 305 | 321 | 'offset' => $offset, |
| 306 | - ); | |
| 322 | + ]; | |
| 307 | 323 | |
| 308 | 324 | // Check for date option filter. |
| 309 | - if ( ! empty( $this->data['donor_export_start_date'] ) || ! empty( $this->data['donor_export_end_date'] ) ) { | |
| 325 | + if ( | |
| 326 | + ! empty( $this->data['donor_export_start_date'] ) | |
| 327 | + || ! empty( $this->data['donor_export_end_date'] ) | |
| 328 | + ) { | |
| 310 | 329 | |
| 311 | 330 | // Start date. |
| 312 | 331 | $start_date = ! empty( $this->data['donor_export_start_date'] ) ? sanitize_text_field( $this->data['donor_export_start_date'] ) : ''; |
| 313 | 332 | if ( ! empty( $start_date ) ) { |
| 314 | - $start_date = give_get_formatted_date( $start_date ); | |
| 333 | + $start_date = date( 'Y-m-d', strtotime( $start_date ) ); | |
| 315 | 334 | $args['date']['start'] = $start_date; |
| 316 | 335 | } |
| 317 | 336 | |
| 318 | 337 | // End date. |
| 319 | - $end_date = ! empty( $this->data['donor_export_end_date'] ) ? sanitize_text_field( $this->data['donor_export_end_date'] ) : date( give_date_format(), current_time( 'timestamp' ) ); | |
| 320 | - $end_date = give_get_formatted_date( $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"; | |
| 321 | 342 | $args['date']['end'] = $end_date; |
| 322 | 343 | |
| 323 | 344 | } |
| 324 | 345 | |
| @@ -344,16 +365,16 @@ | ||
| 344 | 365 | |
| 345 | 366 | /** |
| 346 | 367 | * Return the calculated completion percentage. |
| 347 | 368 | * |
| 369 | + * @return int | |
| 348 | 370 | * @since 1.5 |
| 349 | - * @return int | |
| 350 | 371 | */ |
| 351 | 372 | public function get_percentage_complete() { |
| 352 | 373 | |
| 353 | 374 | $percentage = 0; |
| 354 | 375 | |
| 355 | - // We can't count the number when getting them for a specific form. | |
| 376 | + // We can't count the number when getting donors for a specific form. | |
| 356 | 377 | if ( empty( $this->form ) ) { |
| 357 | 378 | |
| 358 | 379 | $total = Give()->donors->count(); |
| 359 | 380 | |
| @@ -361,8 +382,25 @@ | ||
| 361 | 382 | |
| 362 | 383 | $percentage = ( ( 30 * $this->step ) / $total ) * 100; |
| 363 | 384 | |
| 364 | 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 | + } | |
| 365 | 403 | } |
| 366 | 404 | |
| 367 | 405 | if ( $percentage > 100 ) { |
| 368 | 406 | $percentage = 100; |
| @@ -373,10 +411,10 @@ | ||
| 373 | 411 | |
| 374 | 412 | /** |
| 375 | 413 | * Set Donor Data |
| 376 | 414 | * |
| 377 | - * @param int $i CSV line. | |
| 378 | - * @param array $data Donor CSV data. | |
| 415 | + * @param int $i CSV line. | |
| 416 | + * @param array $data Donor CSV data. | |
| 379 | 417 | * @param object $donor Donor data. |
| 380 | 418 | * |
| 381 | 419 | * @return mixed |
| 382 | 420 | */ |
| @@ -416,9 +454,9 @@ | ||
| 416 | 454 | if ( ! empty( $columns['donations'] ) ) { |
| 417 | 455 | $data[ $i ]['donations'] = $donor->purchase_count; |
| 418 | 456 | } |
| 419 | 457 | if ( ! empty( $columns['donation_sum'] ) ) { |
| 420 | - $data[ $i ]['donation_sum'] = give_format_amount( $donor->purchase_value, array( 'sanitize' => false ) ); | |
| 458 | + $data[ $i ]['donation_sum'] = give_format_amount( $donor->purchase_value, [ 'sanitize' => false ] ); | |
| 421 | 459 | } |
| 422 | 460 | |
| 423 | 461 | $data[ $i ] = apply_filters( 'give_export_set_donor_data', $data[ $i ], $donor ); |
| 424 | 462 | |
| @@ -428,9 +466,9 @@ | ||
| 428 | 466 | |
| 429 | 467 | /** |
| 430 | 468 | * Unset the properties specific to the donors export. |
| 431 | 469 | * |
| 432 | - * @param array $request | |
| 470 | + * @param array $request | |
| 433 | 471 | * @param Give_Batch_Export $export |
| 434 | 472 | */ |
| 435 | 473 | public function unset_properties( $request, $export ) { |
| 436 | 474 | if ( $export->done ) { |