PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / admin / reports / class-charitable-reports-download.php

class-charitable-reports-download.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at includes/admin/reports/class-charitable-reports-download.php

754 lines 25.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Reports Download class.
4 *
5 * @package Charitable/Classes/Charitable_Reports_Download
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8.1
10 * @version 1.8.1
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Reports_Download' ) ) :
19
20 /**
21 * Charitable_Reports_Download
22 *
23 * @final
24 * @since 1.8.1
25 */
26 class Charitable_Reports_Download {
27
28 /**
29 * The single instance of this class.
30 *
31 * @var Charitable_Reports_Download|null
32 */
33 private static $instance = null;
34
35 /**
36 * Keeps track of column data..
37 *
38 * @var array
39 */
40 public $columns = array();
41
42 /**
43 * Create object instance.
44 *
45 * @since 1.8.1
46 */
47 public function __construct() {
48 }
49
50 /**
51 * Run things upon init.
52 *
53 * @since 1.8.1
54 */
55 public function init() {
56 }
57
58 /**
59 * Master function to generate the PDF.
60 *
61 * @since 1.8.1
62 */
63 public function generate_pdf() {
64
65 // basic checks.
66 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) || empty( $_POST['charitable_report_action'] ) ) {
67 return;
68 }
69
70 // check nonce.
71 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
72 return;
73 }
74
75 // check for export action.
76 if ( strpos( sanitize_title( $_POST['charitable_report_action'] ), 'charitable_report_', 0 ) === false ) { // phpcs:ignore
77 return;
78 }
79
80 $charitable_report_action = wp_unslash( sanitize_text_field( $_POST['charitable_report_action'] ) ); // phpcs:ignore
81
82 $action = strpos( $charitable_report_action, 'download_pdf', 0 ) ? 'download' : 'print';
83
84 // get the last word after the last underline in the string.
85 $report_type = substr( $charitable_report_action, strrpos( $charitable_report_action, '_' ) + 1 );
86
87 switch ( $report_type ) {
88 case 'dashboard':
89 $this->generate_dashboard_content( $action );
90 break;
91
92 case 'overview':
93 $this->generate_overview_content( $action );
94 break;
95
96 default:
97 break;
98 }
99
100 exit;
101 }
102
103 /**
104 * Generate a PDF report for the dashboard.
105 *
106 * @param string $action The action to take. Download or print.
107 *
108 * @since 1.8.1
109 */
110 public function generate_dashboard_content( $action = '' ) {
111
112 $include_icons = $action === 'download' ? false : true;
113 $show_support_box = false;
114 $show_recommended_addons = false;
115
116 $start_date = empty( $_POST['start_date'] ) ? false : esc_html( $_POST['start_date'] ); // phpcs:ignore
117 $end_date = empty( $_POST['end_date'] ) ? false : esc_html( $_POST['end_date'] ); // phpcs:ignore
118 $days = empty( $_POST['days'] ) ? false : intval( $_POST['days'] ); // phpcs:ignore
119
120 $init_vars = array(
121 'start_date' => $start_date,
122 'end_date' => $end_date,
123 'days' => $days,
124 'show_support_box' => $show_support_box,
125 'include_icons' => $include_icons,
126 'action' => $action,
127 'show_recommended_addons' => $show_recommended_addons,
128 'show_notifications' => false,
129 'use_cache' => 'no',
130 );
131
132 $charitable_dashboard = new Charitable_Dashboard_Legacy( $init_vars );
133 $html = $charitable_dashboard->generate_dashboard_report_html( $init_vars ); // phpcs:ignore
134
135 $assets_dir = charitable()->get_path( 'assets', true );
136 $currency_helper = charitable_get_currency_helper();
137
138 ob_start();
139
140 $page_args = array(
141 'charitable_admin_2_0_css' => $assets_dir . 'css/admin/charitable-admin-report-' . $action . '.css', // phpcs:ignore
142 'action' => $action,
143 'charitable_cards' => $html['charitable_cards'], // phpcs:ignore
144 'charitable_reports' => $html['charitable_reports'], // phpcs:ignore
145 'headline_chart_options' => array(
146 'donation_axis' => (array) $charitable_dashboard->donation_axis,
147 'date_axis' => (array) $charitable_dashboard->date_axis,
148 ),
149 'currency_symbol' => $currency_helper->get_currency_symbol(),
150 'start_date' => $start_date,
151 'end_date' => $end_date,
152 'charitable_chart_js' => charitable()->get_path( 'assets', false ) . 'js/libraries/apexcharts.min.js', // phpcs:ignore
153 );
154
155 charitable_template(
156 'print/dashboard.php',
157 $page_args
158 );
159
160 if ( 'print' === $action ) {
161
162 $html = ob_get_clean();
163 echo $html; // phpcs:ignore
164 exit;
165
166 } else {
167
168 die( 'Non-print generation not yet implemented.' );
169
170 }
171
172 exit;
173 }
174
175 /**
176 * Generate a PDF report for the overview.
177 *
178 * @param string $action The action to take. Download or print.
179 *
180 * @since 1.8.1
181 */
182 public function generate_overview_content( $action = '' ) {
183
184 /*
185 * Site-local, not UTC: post_date is stored in site-local time, so a
186 * bound built from bare UTC excludes part of today's donations on any
187 * site whose timezone is ahead of UTC.
188 *
189 * @since 1.8.13
190 */
191 $start_date = gmdate( 'Y/m/d', current_time( 'timestamp', 0 ) - ( 7 * DAY_IN_SECONDS ) );
192 $end_date = gmdate( 'Y/m/d', current_time( 'timestamp', 0 ) );
193 $status = false;
194
195 $start_date = empty( $_POST['start_date'] ) ? $start_date : esc_html( $_POST['start_date'] ); // phpcs:ignore
196 $end_date = empty( $_POST['end_date'] ) ? $end_date : esc_html( $_POST['end_date'] ); // phpcs:ignore
197 $campaign_id = empty( $_POST['campaign_id'] ) ? -1 : intval( $_POST['campaign_id'] ); // phpcs:ignore
198 $category_id = empty( $_POST['category_id'] ) ? 0 : intval( $_POST['category_id'] ); // phpcs:ignore
199 $category_term_name = $category_id > 0 ? get_term_by( 'id', $category_id, 'campaign_category' )->name : '';
200
201 $charitable_reports = Charitable_Reports::get_instance();
202
203 $args = array(
204 'start_date' => $start_date,
205 'end_date' => $end_date,
206 'status' => $status,
207 'campaign_id' => $campaign_id,
208 'category_id' => $category_id,
209 );
210
211 $charitable_reports->init_with_array( 'overview', $args );
212
213 // main activity.
214 $report_activity = $charitable_reports->get_activity();
215
216 // donations.
217 $donations = $charitable_reports->get_donations();
218 $total_count_donations = count( $donations );
219 $total_amount_donations = $charitable_reports->get_donations_total();
220 $donation_breakdown = $charitable_reports->get_donations_by_day();
221 $payment_breakdown = $charitable_reports->get_donations_by_payment();
222 $donation_average = ( $total_amount_donations > 0 && $total_amount_donations > 0 ) ? charitable_format_money( $total_amount_donations / $total_count_donations ) : 0;
223 $donation_total = charitable_format_money( $total_amount_donations );
224
225 // donors.
226 $top_donors = $charitable_reports->get_top_donors_overview();
227 $total_count_donors = $charitable_reports->get_donors_count();
228
229 // refunds.
230 $refunds_data = $charitable_reports->get_refunds();
231 $total_amount_refunds = $refunds_data['total_amount'];
232 $total_count_refunds = $refunds_data['total_count'];
233
234 $args = false;
235 $top_campaigns = Charitable_Campaigns::ordered_by_amount( $args );
236
237 $assets_dir = charitable()->get_path( 'assets', true );
238 $currency_helper = charitable_get_currency_helper();
239
240 $include_icons = $action === 'download' ? false : true;
241 $campaign = $campaign_id > 0 ? charitable_get_campaign( $campaign_id ) : false;
242
243 ob_start();
244
245 charitable_template(
246 'print/overview.php',
247 array(
248 'charitable_admin_2_0_css' => $assets_dir . 'css/admin/charitable-admin-report-' . $action . '.css',
249 'action' => $action,
250 'donations' => $donations,
251 'total_count_donations' => $total_count_donations,
252 'total_amount_donations' => $total_amount_donations,
253 'donation_breakdown' => $donation_breakdown,
254 'payment_breakdown' => $payment_breakdown,
255 'donation_average' => $donation_average,
256 'donation_total' => $donation_total,
257 'total_count_donors' => $total_count_donors,
258 'total_amount_refunds' => $total_amount_refunds,
259 'total_count_refunds' => $total_count_refunds,
260 'donation_breakdown_html' => $charitable_reports->generate_donations_breakdown_rows( $donation_breakdown, $refunds_data ),
261 'activity_list' => $charitable_reports->generate_activity_list( $report_activity, $include_icons ),
262 'top_donors' => $charitable_reports->generate_top_donors( $top_donors, $include_icons ),
263 'top_campaigns' => $charitable_reports->generate_top_campaigns( $top_campaigns, $include_icons ),
264 'payment_methods_list' => $charitable_reports->generate_payment_methods_list( $payment_breakdown, $include_icons ),
265 'headline_chart_options' => array(
266 'donation_axis' => (array) $charitable_reports->donation_axis,
267 'date_axis' => (array) $charitable_reports->date_axis,
268 ),
269 'payment_methods_chart_options' => array(
270 'payment_percentages' => (array) array_reverse( $charitable_reports->payment_percentages ),
271 'payment_labels' => (array) array_reverse( $charitable_reports->payment_labels ),
272 ),
273 'currency_symbol' => $currency_helper->get_currency_symbol(),
274 'start_date' => $start_date,
275 'end_date' => $end_date,
276 'campaign_id' => $campaign_id,
277 'campaign' => $campaign,
278 'category_id' => $category_id,
279 'category_term_name' => $category_term_name,
280 'charitable_chart_js' => charitable()->get_path( 'assets', false ) . 'js/libraries/apexcharts.min.js', // phpcs:ignore
281 )
282 );
283
284 if ( 'print' === $action ) {
285
286 $html = ob_get_clean();
287 echo $html; // phpcs:ignore
288 exit;
289
290 } else {
291
292 die( 'Non-print generation not yet implemented.' );
293
294 }
295
296 exit;
297 }
298
299 /**
300 * Print the CSV document headers.
301 *
302 * @param string $report_type The type of report being exported.
303 * @since 1.8.1
304 *
305 * @return void
306 */
307 protected function print_headers( $report_type = '' ) {
308 ignore_user_abort( true );
309
310 if ( ! charitable_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
311 set_time_limit( 0 ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
312 }
313
314 if ( '' !== $report_type ) {
315 $report_type = '-' . $report_type;
316 }
317
318 nocache_headers();
319 header( 'Content-Type: text/csv; charset=utf-8' );
320 header( 'Content-Disposition: attachment; filename=charitable-report-download' . $report_type . '-' . gmdate( 'm-d-Y' ) . '.csv' );
321 header( 'Expires: 0' );
322 }
323
324 /**
325 * Export the CSV file.
326 *
327 * @since 1.8.1
328 *
329 * @param array $data The data to export.
330 * @param string $report_type The type of report being exported.
331 *
332 * @return void
333 */
334 protected function export( $data = array(), $report_type = '' ) {
335
336 // if $data is an object, convert to array.
337 if ( is_object( $data ) ) {
338 $data = get_object_vars( $data );
339 }
340
341 if ( empty( $data ) ) {
342 die( 'There is no data to export or download.' );
343 }
344
345 $data = array_map( array( $this, 'map_data' ), $data );
346 $data = array_values( $data ); // reset keys.
347
348 $this->print_headers( $report_type );
349
350 /* Create a file pointer connected to the output stream */
351 $output = fopen( 'php://output', 'w' );
352
353 /* Print first row headers. */
354 fputcsv( $output, array_values( $this->columns ) );
355
356 /* Print the data */
357 if ( is_array( $data ) && ! empty( $data ) ) {
358 foreach ( $data as $row ) {
359 fputcsv( $output, $row );
360 }
361 }
362
363 fclose( $output ); // phpcs:ignore
364
365 exit();
366 }
367
368 /**
369 * Map the data to the columns.
370 *
371 * @since 1.8.1
372 * @param array $data The data to map.
373 *
374 * @return array
375 */
376 public function map_data( $data ) {
377 $row = array();
378
379 foreach ( $this->columns as $column_slug => $column_label ) {
380 $row[ $column_slug ] = isset( $data[ $column_slug ] ) ? $data[ $column_slug ] : '';
381 }
382
383 return $row;
384 }
385
386 /**
387 * Generate the HTML for a breakdown of donations, required for some test output.
388 *
389 * @since 1.8.1
390 *
391 * @return void
392 */
393 public function download_donations_breakdown() {
394
395 // check for nonce in $_POST and charitable_report_action variable.
396 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
397 return;
398 }
399
400 // check nonce.
401 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
402 return;
403 }
404
405 // check for admin.
406 if ( ! current_user_can( 'manage_options' ) ) {
407 return;
408 }
409
410 // check for export action.
411 if ( 'charitable_report_download_donation_breakdown' !== $_POST['charitable_report_action'] ) {
412 return;
413 }
414
415 $args = array(
416 'start_date' => isset( $_POST['start_date'] ) ? esc_html( $_POST['start_date'] ) : false, // phpcs:ignore
417 'end_date' => isset( $_POST['end_date'] ) ? esc_html( $_POST['end_date'] ) : false, // phpcs:ignore
418 'campaign_id' => isset( $_POST['campaign_id'] ) ? esc_html( $_POST['campaign_id'] ) : false, // phpcs:ignore
419 'category_id' => isset( $_POST['category_id'] ) ? esc_html( $_POST['category_id'] ) : false, // phpcs:ignore
420 'status' => isset( $_POST['status'] ) ? esc_html( $_POST['status'] ) : false, // phpcs:ignore
421 );
422
423 $defaults = array(
424 'start_date' => false,
425 'end_date' => false,
426 'campaign_id' => false,
427 'category_id' => false,
428 'status' => false,
429 );
430
431 $args = wp_parse_args( $args, $defaults );
432
433 $this->columns = array(
434 'label' => __( 'Date', 'charitable' ),
435 'amount' => __( 'Donations', 'charitable' ),
436 'donors' => __( 'No. of Donors', 'charitable' ),
437 'refunds' => __( 'Refunds', 'charitable' ),
438 'net' => __( 'Net', 'charitable' ),
439 );
440
441 $charitable_reports = Charitable_Reports::get_instance();
442
443 $charitable_reports->init_with_array( 'overview', $args );
444
445 $donation_breakdown = $charitable_reports->get_donations_by_day( true );
446
447 $this->export( $donation_breakdown );
448
449 exit;
450 }
451
452 /**
453 * Generate the HTML for a breakdown of activities.
454 *
455 * @since 1.8.1
456 *
457 * @return void
458 */
459 public function download_activities() {
460
461 // check for nonce in $_POST and charitable_report_action variable.
462 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
463 return;
464 }
465
466 // check nonce.
467 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
468 return;
469 }
470
471 // check for admin.
472 if ( ! current_user_can( 'manage_options' ) ) {
473 return;
474 }
475
476 // check for export action.
477 if ( 'charitable_report_download_activity' !== $_POST['charitable_report_action'] ) {
478 return;
479 }
480
481 $args = array(
482 'start_date' => isset( $_POST['start_date'] ) ? esc_html( $_POST['start_date'] ) : false, // phpcs:ignore
483 'end_date' => isset( $_POST['end_date'] ) ? esc_html( $_POST['end_date'] ) : false, // phpcs:ignore
484 'campaign_id' => isset( $_POST['campaign_id'] ) ? esc_html( $_POST['campaign_id'] ) : false, // phpcs:ignore
485 'activity_type' => isset( $_POST['activity_type'] ) ? esc_html( $_POST['activity_type'] ) : false, // phpcs:ignore
486 );
487
488 $defaults = array(
489 'start_date' => false,
490 'end_date' => false,
491 'campaign_id' => false,
492 'activity_type' => false,
493 );
494
495 $args = wp_parse_args( $args, $defaults );
496
497 $this->columns = array(
498 'action_label' => __( 'Action', 'charitable' ), // "Donation Made"
499 'amount' => __( 'Amount', 'charitable' ), // "$55.00"
500 'campaign_title' => __( 'Campaign', 'charitable' ), // "XYZ Campaign"
501 'donor_id' => __( 'Donor ID', 'charitable' ), // "123"
502 'donor_name' => __( 'Donor Name', 'charitable' ), // "John Doe"
503 'created_by' => __( 'Created By User ID', 'charitable' ), // 1
504 'created_by_name' => __( 'Created By User Name', 'charitable' ), // "John Doe"
505 'date_recorded' => __( 'Date', 'charitable' ), // "March 1, 2023"
506 );
507
508 $charitable_reports = Charitable_Reports::get_instance();
509 $charitable_reports->init_with_array( 'activity', $args );
510 // $charitable_reports->init( 'activity', $args['start_date'], $args['end_date'], false, $args['campaign_id'] );
511 $report_activity = $charitable_reports->get_activity( $args['activity_type'] );
512
513 // convert arrays of objects to arrays of arrays.
514 if ( is_array( $report_activity ) ) {
515 foreach ( $report_activity as $key => $value ) {
516 $report_activity[ $key ] = get_object_vars( $value );
517
518 // format action labels.
519 $report_activity[ $key ]['action_label'] = ( ! empty( $report_activity[ $key ]['type'] ) && 'donation' === $report_activity[ $key ]['type'] && ! empty( $report_activity[ $key ]['item_id_prefix'] ) && ! empty( $report_activity[ $key ]['item_id'] ) ) ? str_replace( '%id', $report_activity[ $key ]['item_id_prefix'] . $report_activity[ $key ]['item_id'], $report_activity[ $key ]['action_label'] ) : $report_activity[ $key ]['action_label'];
520 $report_activity[ $key ]['action_label'] = ( ! empty( $report_activity[ $key ]['type'] ) && 'campaign' === $report_activity[ $key ]['type'] && ! empty( $report_activity[ $key ]['campaign_id'] ) ) ? str_replace( '%id', $report_activity[ $key ]['campaign_id'], $report_activity[ $key ]['action_label'] ) : $report_activity[ $key ]['action_label'];
521
522 // convert to date format example: March 31, 2023 1:30 pm.
523 if ( ! empty( $report_activity[ $key ]['date_recorded'] ) ) :
524 $date_format = empty( $date_format ) ? get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) : $date_format;
525 $report_activity[ $key ]['date_recorded'] = gmdate( $date_format, strtotime( $report_activity[ $key ]['date_recorded'] ) );
526 endif;
527
528 // format money amounts.
529 if ( ! empty( $report_activity[ $key ]['amount'] ) ) :
530 $report_activity[ $key ]['amount'] = html_entity_decode( trim( charitable_format_money( $report_activity[ $key ]['amount'] ) ) );
531 endif;
532
533 }
534
535 }
536
537 $this->export( $report_activity, 'activity' );
538
539 exit;
540 }
541
542 /**
543 * Generate the HTML for a breakdown of donors, required for some reports.
544 *
545 * @since 1.8.1
546 *
547 * @return void
548 */
549 public function download_donors() {
550
551 // check for nonce in $_POST and charitable_report_action variable.
552 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
553 return;
554 }
555
556 // check nonce.
557 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
558 return;
559 }
560
561 // check for admin.
562 if ( ! current_user_can( 'manage_options' ) ) {
563 return;
564 }
565
566 // check for export action.
567 if ( 'charitable_report_download_donors' !== $_POST['charitable_report_action'] ) {
568 return;
569 }
570
571 $args = array(
572 'report_type' => isset( $_POST['report_type'] ) ? esc_html( $_POST['report_type'] ) : false, // phpcs:ignore
573 );
574
575 $defaults = array(
576 'report_type' => 'donors-top',
577 );
578
579 $args = wp_parse_args( $args, $defaults );
580
581 $this->columns = array(
582 'first_name' => __( 'First Name', 'charitable' ),
583 'last_name' => __( 'Last Name', 'charitable' ),
584 'email' => __( 'Email', 'charitable' ),
585 'donor_id' => __( 'Donor ID', 'charitable' ),
586 'user_id' => __( 'User ID', 'charitable' ),
587 'date_joined' => __( 'Date Joined', 'charitable' ),
588 'total_amount' => __( 'Total Donations', 'charitable' ),
589 'average' => __( 'Average Donations', 'charitable' ),
590 'total_count_donations' => __( 'No. of Donations', 'charitable' ),
591 'total_count_campaigns' => __( 'Number of Campaigns', 'charitable' ),
592 'amount' => __( 'Last Donation Amount', 'charitable' ),
593 'last_campaign_title' => __( 'Last Donation Campaign', 'charitable' ),
594 'last_donation_date' => __( 'Last Donation Date', 'charitable' ),
595 );
596
597 $charitable_reports = Charitable_Reports::get_instance();
598 $charitable_reports->init_with_array( $args['report_type'] );
599
600 $donor_data = $charitable_reports->get_donor_report_by_type( $args['report_type'] );
601
602 // convert arrays of objects to arrays of arrays.
603 if ( is_array( $donor_data ) ) {
604 foreach ( $donor_data as $key => $value ) {
605 $donor_data[ $key ] = get_object_vars( $value );
606 }
607 }
608
609 $donor_data = $this->format_donors_download_data( $donor_data );
610
611 $this->export( $donor_data, $args['report_type'] );
612
613 exit;
614 }
615
616 /**
617 * Format the donor data for download.
618 *
619 * @since 1.8.1
620 *
621 * @param array $donor_data The donor data to format.
622 *
623 * @return array
624 */
625 public function format_donors_download_data( $donor_data = false ) {
626
627 if ( ! empty( $donor_data ) ) {
628 $donor_data = charitable_reports_format_money( $donor_data, array( 'total_amount', 'average', 'amount' ), false, false, '', true, true );
629 $donor_data = charitable_reports_format_date( $donor_data, array( 'date_joined', 'last_donation_date' ), false );
630 }
631
632 return $donor_data;
633 }
634
635 /**
636 * Generate the HTML for a breakdown of an advanced report.
637 *
638 * @since 1.8.1
639 *
640 * @return void
641 */
642 public function download_advanced() {
643
644 // check for nonce in $_POST and charitable_report_action variable.
645 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
646 return;
647 }
648
649 // check nonce.
650 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
651 return;
652 }
653
654 // check for admin.
655 if ( ! current_user_can( 'manage_options' ) ) {
656 return;
657 }
658
659 // check for export action.
660 if ( 'charitable_report_download_advanced' !== $_POST['charitable_report_action'] ) {
661 return;
662 }
663
664 $args = array(
665 'report_type' => isset( $_POST['report_type'] ) ? sanitize_title( $_POST['report_type'] ) : false, // phpcs:ignore
666 );
667
668 $defaults = array(
669 'report_type' => 'lybunt',
670 );
671
672 $args = wp_parse_args( $args, $defaults );
673
674 $report_args = charitable_reports_get_advanced_report_args();
675
676 $this->columns = array(
677 'donor_first_name' => __( 'First Name', 'charitable' ),
678 'donor_last_name' => __( 'Last Name', 'charitable' ),
679 'donor_email' => __( 'Email', 'charitable' ),
680 'donor_id' => __( 'Donor ID', 'charitable' ),
681 'user_id' => __( 'User ID', 'charitable' ),
682 'start_date_compare_from' => __( 'Start Date Compare From', 'charitable' ),
683 'end_date_compare_from' => __( 'End Date Compare From', 'charitable' ),
684 'total_donation_count_compare_from' => __( 'Donation Count Compare From', 'charitable' ),
685 'total_donation_amount_compare_from' => __( 'Donation Total Compare From', 'charitable' ),
686 'start_date_compare_to' => __( 'Start Date Compare To', 'charitable' ),
687 'end_date_compare_to' => __( 'End Date Compare To', 'charitable' ),
688 'total_donation_count_compare_to' => __( 'Donation Count Compare To', 'charitable' ),
689 'total_donation_amount_compare_to' => __( 'Donation Total Compare To', 'charitable' ),
690 'last_donation_amount' => __( 'Last Donation Amount', 'charitable' ),
691 'last_donation_campaigns' => __( 'Last Donated Campaign(s)', 'charitable' ),
692 'total_life_amount' => __( 'Lifetime Total', 'charitable' ),
693 );
694
695 $charitable_reports = Charitable_Reports::get_instance();
696 $charitable_reports->init_with_array( $args['report_type'] );
697 $advanced_report_data = $charitable_reports->get_advanced_data_by_report_type( $report_args );
698
699 // convert arrays of objects to arrays of arrays.
700 if ( is_array( $advanced_report_data ) ) {
701 foreach ( $advanced_report_data as $key => $value ) {
702 $advanced_report_data[ $key ] = get_object_vars( $value );
703 }
704 }
705
706 foreach ( $advanced_report_data as $key => $value ) {
707 // merge $report_args with every element of $advanced_report_data.
708 $advanced_report_data[ $key ] = array_merge( $report_args, $value );
709 $advanced_report_data[ $key ]['last_donation_campaigns'] = wp_strip_all_tags( implode( ', ', $value['last_donation_campaign_links'] ) );
710 }
711
712 $advanced_report_data = $this->format_advanced_download_data( $advanced_report_data );
713
714 $this->export( $advanced_report_data, $args['report_type'] );
715
716 exit;
717 }
718
719 /**
720 * Format the donor data for download.
721 *
722 * @since 1.8.1
723 *
724 * @param array $advanced_data The donor data to format.
725 *
726 * @return array
727 */
728 public function format_advanced_download_data( $advanced_data = false ) {
729
730 if ( ! empty( $advanced_data ) ) {
731 $advanced_data = charitable_reports_format_money( $advanced_data, array( 'total_donation_amount_compare_from', 'total_donation_amount_compare_to', 'last_donation_amount', 'total_life_amount' ), false, false, '', true, true );
732 $advanced_data = charitable_reports_format_date( $advanced_data, array( 'start_date_compare_from', 'end_date_compare_from', 'start_date_compare_to', 'end_date_compare_to' ), 'F j, Y' );
733 }
734
735 return $advanced_data;
736 }
737
738 /**
739 * Returns and/or create the single instance of this class.
740 *
741 * @since 1.8.1
742 *
743 * @return Charitable_Reports_Download
744 */
745 public static function get_instance() {
746 if ( is_null( self::$instance ) ) {
747 self::$instance = new self();
748 }
749
750 return self::$instance;
751 }
752 }
753 endif;
754