PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11
1.8.13.1 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 All 212 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.11, at includes/admin/reports/class-charitable-reports-download.php

747 lines 25.4 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 $start_date = gmdate( 'Y/m/d', strtotime( '-7 days' ) );
185 $end_date = gmdate( 'Y/m/d' );
186 $status = false;
187
188 $start_date = empty( $_POST['start_date'] ) ? $start_date : esc_html( $_POST['start_date'] ); // phpcs:ignore
189 $end_date = empty( $_POST['end_date'] ) ? $end_date : esc_html( $_POST['end_date'] ); // phpcs:ignore
190 $campaign_id = empty( $_POST['campaign_id'] ) ? -1 : intval( $_POST['campaign_id'] ); // phpcs:ignore
191 $category_id = empty( $_POST['category_id'] ) ? 0 : intval( $_POST['category_id'] ); // phpcs:ignore
192 $category_term_name = $category_id > 0 ? get_term_by( 'id', $category_id, 'campaign_category' )->name : '';
193
194 $charitable_reports = Charitable_Reports::get_instance();
195
196 $args = array(
197 'start_date' => $start_date,
198 'end_date' => $end_date,
199 'status' => $status,
200 'campaign_id' => $campaign_id,
201 'category_id' => $category_id,
202 );
203
204 $charitable_reports->init_with_array( 'overview', $args );
205
206 // main activity.
207 $report_activity = $charitable_reports->get_activity();
208
209 // donations.
210 $donations = $charitable_reports->get_donations();
211 $total_count_donations = count( $donations );
212 $total_amount_donations = $charitable_reports->get_donations_total();
213 $donation_breakdown = $charitable_reports->get_donations_by_day();
214 $payment_breakdown = $charitable_reports->get_donations_by_payment();
215 $donation_average = ( $total_amount_donations > 0 && $total_amount_donations > 0 ) ? charitable_format_money( $total_amount_donations / $total_count_donations ) : 0;
216 $donation_total = charitable_format_money( $total_amount_donations );
217
218 // donors.
219 $top_donors = $charitable_reports->get_top_donors_overview();
220 $total_count_donors = $charitable_reports->get_donors_count();
221
222 // refunds.
223 $refunds_data = $charitable_reports->get_refunds();
224 $total_amount_refunds = $refunds_data['total_amount'];
225 $total_count_refunds = $refunds_data['total_count'];
226
227 $args = false;
228 $top_campaigns = Charitable_Campaigns::ordered_by_amount( $args );
229
230 $assets_dir = charitable()->get_path( 'assets', true );
231 $currency_helper = charitable_get_currency_helper();
232
233 $include_icons = $action === 'download' ? false : true;
234 $campaign = $campaign_id > 0 ? charitable_get_campaign( $campaign_id ) : false;
235
236 ob_start();
237
238 charitable_template(
239 'print/overview.php',
240 array(
241 'charitable_admin_2_0_css' => $assets_dir . 'css/admin/charitable-admin-report-' . $action . '.css',
242 'action' => $action,
243 'donations' => $donations,
244 'total_count_donations' => $total_count_donations,
245 'total_amount_donations' => $total_amount_donations,
246 'donation_breakdown' => $donation_breakdown,
247 'payment_breakdown' => $payment_breakdown,
248 'donation_average' => $donation_average,
249 'donation_total' => $donation_total,
250 'total_count_donors' => $total_count_donors,
251 'total_amount_refunds' => $total_amount_refunds,
252 'total_count_refunds' => $total_count_refunds,
253 'donation_breakdown_html' => $charitable_reports->generate_donations_breakdown_rows( $donation_breakdown, $refunds_data ),
254 'activity_list' => $charitable_reports->generate_activity_list( $report_activity, $include_icons ),
255 'top_donors' => $charitable_reports->generate_top_donors( $top_donors, $include_icons ),
256 'top_campaigns' => $charitable_reports->generate_top_campaigns( $top_campaigns, $include_icons ),
257 'payment_methods_list' => $charitable_reports->generate_payment_methods_list( $payment_breakdown, $include_icons ),
258 'headline_chart_options' => array(
259 'donation_axis' => (array) $charitable_reports->donation_axis,
260 'date_axis' => (array) $charitable_reports->date_axis,
261 ),
262 'payment_methods_chart_options' => array(
263 'payment_percentages' => (array) array_reverse( $charitable_reports->payment_percentages ),
264 'payment_labels' => (array) array_reverse( $charitable_reports->payment_labels ),
265 ),
266 'currency_symbol' => $currency_helper->get_currency_symbol(),
267 'start_date' => $start_date,
268 'end_date' => $end_date,
269 'campaign_id' => $campaign_id,
270 'campaign' => $campaign,
271 'category_id' => $category_id,
272 'category_term_name' => $category_term_name,
273 'charitable_chart_js' => charitable()->get_path( 'assets', false ) . 'js/libraries/apexcharts.min.js', // phpcs:ignore
274 )
275 );
276
277 if ( 'print' === $action ) {
278
279 $html = ob_get_clean();
280 echo $html; // phpcs:ignore
281 exit;
282
283 } else {
284
285 die( 'Non-print generation not yet implemented.' );
286
287 }
288
289 exit;
290 }
291
292 /**
293 * Print the CSV document headers.
294 *
295 * @param string $report_type The type of report being exported.
296 * @since 1.8.1
297 *
298 * @return void
299 */
300 protected function print_headers( $report_type = '' ) {
301 ignore_user_abort( true );
302
303 if ( ! charitable_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
304 set_time_limit( 0 ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
305 }
306
307 if ( '' !== $report_type ) {
308 $report_type = '-' . $report_type;
309 }
310
311 nocache_headers();
312 header( 'Content-Type: text/csv; charset=utf-8' );
313 header( 'Content-Disposition: attachment; filename=charitable-report-download' . $report_type . '-' . gmdate( 'm-d-Y' ) . '.csv' );
314 header( 'Expires: 0' );
315 }
316
317 /**
318 * Export the CSV file.
319 *
320 * @since 1.8.1
321 *
322 * @param array $data The data to export.
323 * @param string $report_type The type of report being exported.
324 *
325 * @return void
326 */
327 protected function export( $data = array(), $report_type = '' ) {
328
329 // if $data is an object, convert to array.
330 if ( is_object( $data ) ) {
331 $data = get_object_vars( $data );
332 }
333
334 if ( empty( $data ) ) {
335 die( 'There is no data to export or download.' );
336 }
337
338 $data = array_map( array( $this, 'map_data' ), $data );
339 $data = array_values( $data ); // reset keys.
340
341 $this->print_headers( $report_type );
342
343 /* Create a file pointer connected to the output stream */
344 $output = fopen( 'php://output', 'w' );
345
346 /* Print first row headers. */
347 fputcsv( $output, array_values( $this->columns ) );
348
349 /* Print the data */
350 if ( is_array( $data ) && ! empty( $data ) ) {
351 foreach ( $data as $row ) {
352 fputcsv( $output, $row );
353 }
354 }
355
356 fclose( $output ); // phpcs:ignore
357
358 exit();
359 }
360
361 /**
362 * Map the data to the columns.
363 *
364 * @since 1.8.1
365 * @param array $data The data to map.
366 *
367 * @return array
368 */
369 public function map_data( $data ) {
370 $row = array();
371
372 foreach ( $this->columns as $column_slug => $column_label ) {
373 $row[ $column_slug ] = isset( $data[ $column_slug ] ) ? $data[ $column_slug ] : '';
374 }
375
376 return $row;
377 }
378
379 /**
380 * Generate the HTML for a breakdown of donations, required for some test output.
381 *
382 * @since 1.8.1
383 *
384 * @return void
385 */
386 public function download_donations_breakdown() {
387
388 // check for nonce in $_POST and charitable_report_action variable.
389 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
390 return;
391 }
392
393 // check nonce.
394 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
395 return;
396 }
397
398 // check for admin.
399 if ( ! current_user_can( 'manage_options' ) ) {
400 return;
401 }
402
403 // check for export action.
404 if ( 'charitable_report_download_donation_breakdown' !== $_POST['charitable_report_action'] ) {
405 return;
406 }
407
408 $args = array(
409 'start_date' => isset( $_POST['start_date'] ) ? esc_html( $_POST['start_date'] ) : false, // phpcs:ignore
410 'end_date' => isset( $_POST['end_date'] ) ? esc_html( $_POST['end_date'] ) : false, // phpcs:ignore
411 'campaign_id' => isset( $_POST['campaign_id'] ) ? esc_html( $_POST['campaign_id'] ) : false, // phpcs:ignore
412 'category_id' => isset( $_POST['category_id'] ) ? esc_html( $_POST['category_id'] ) : false, // phpcs:ignore
413 'status' => isset( $_POST['status'] ) ? esc_html( $_POST['status'] ) : false, // phpcs:ignore
414 );
415
416 $defaults = array(
417 'start_date' => false,
418 'end_date' => false,
419 'campaign_id' => false,
420 'category_id' => false,
421 'status' => false,
422 );
423
424 $args = wp_parse_args( $args, $defaults );
425
426 $this->columns = array(
427 'label' => __( 'Date', 'charitable' ),
428 'amount' => __( 'Donations', 'charitable' ),
429 'donors' => __( 'No. of Donors', 'charitable' ),
430 'refunds' => __( 'Refunds', 'charitable' ),
431 'net' => __( 'Net', 'charitable' ),
432 );
433
434 $charitable_reports = Charitable_Reports::get_instance();
435
436 $charitable_reports->init_with_array( 'overview', $args );
437
438 $donation_breakdown = $charitable_reports->get_donations_by_day( true );
439
440 $this->export( $donation_breakdown );
441
442 exit;
443 }
444
445 /**
446 * Generate the HTML for a breakdown of activities.
447 *
448 * @since 1.8.1
449 *
450 * @return void
451 */
452 public function download_activities() {
453
454 // check for nonce in $_POST and charitable_report_action variable.
455 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
456 return;
457 }
458
459 // check nonce.
460 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
461 return;
462 }
463
464 // check for admin.
465 if ( ! current_user_can( 'manage_options' ) ) {
466 return;
467 }
468
469 // check for export action.
470 if ( 'charitable_report_download_activity' !== $_POST['charitable_report_action'] ) {
471 return;
472 }
473
474 $args = array(
475 'start_date' => isset( $_POST['start_date'] ) ? esc_html( $_POST['start_date'] ) : false, // phpcs:ignore
476 'end_date' => isset( $_POST['end_date'] ) ? esc_html( $_POST['end_date'] ) : false, // phpcs:ignore
477 'campaign_id' => isset( $_POST['campaign_id'] ) ? esc_html( $_POST['campaign_id'] ) : false, // phpcs:ignore
478 'activity_type' => isset( $_POST['activity_type'] ) ? esc_html( $_POST['activity_type'] ) : false, // phpcs:ignore
479 );
480
481 $defaults = array(
482 'start_date' => false,
483 'end_date' => false,
484 'campaign_id' => false,
485 'activity_type' => false,
486 );
487
488 $args = wp_parse_args( $args, $defaults );
489
490 $this->columns = array(
491 'action_label' => __( 'Action', 'charitable' ), // "Donation Made"
492 'amount' => __( 'Amount', 'charitable' ), // "$55.00"
493 'campaign_title' => __( 'Campaign', 'charitable' ), // "XYZ Campaign"
494 'donor_id' => __( 'Donor ID', 'charitable' ), // "123"
495 'donor_name' => __( 'Donor Name', 'charitable' ), // "John Doe"
496 'created_by' => __( 'Created By User ID', 'charitable' ), // 1
497 'created_by_name' => __( 'Created By User Name', 'charitable' ), // "John Doe"
498 'date_recorded' => __( 'Date', 'charitable' ), // "March 1, 2023"
499 );
500
501 $charitable_reports = Charitable_Reports::get_instance();
502 $charitable_reports->init_with_array( 'activity', $args );
503 // $charitable_reports->init( 'activity', $args['start_date'], $args['end_date'], false, $args['campaign_id'] );
504 $report_activity = $charitable_reports->get_activity( $args['activity_type'] );
505
506 // convert arrays of objects to arrays of arrays.
507 if ( is_array( $report_activity ) ) {
508 foreach ( $report_activity as $key => $value ) {
509 $report_activity[ $key ] = get_object_vars( $value );
510
511 // format action labels.
512 $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'];
513 $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'];
514
515 // convert to date format example: March 31, 2023 1:30 pm.
516 if ( ! empty( $report_activity[ $key ]['date_recorded'] ) ) :
517 $date_format = empty( $date_format ) ? get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) : $date_format;
518 $report_activity[ $key ]['date_recorded'] = gmdate( $date_format, strtotime( $report_activity[ $key ]['date_recorded'] ) );
519 endif;
520
521 // format money amounts.
522 if ( ! empty( $report_activity[ $key ]['amount'] ) ) :
523 $report_activity[ $key ]['amount'] = html_entity_decode( trim( charitable_format_money( $report_activity[ $key ]['amount'] ) ) );
524 endif;
525
526 }
527
528 }
529
530 $this->export( $report_activity, 'activity' );
531
532 exit;
533 }
534
535 /**
536 * Generate the HTML for a breakdown of donors, required for some reports.
537 *
538 * @since 1.8.1
539 *
540 * @return void
541 */
542 public function download_donors() {
543
544 // check for nonce in $_POST and charitable_report_action variable.
545 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
546 return;
547 }
548
549 // check nonce.
550 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
551 return;
552 }
553
554 // check for admin.
555 if ( ! current_user_can( 'manage_options' ) ) {
556 return;
557 }
558
559 // check for export action.
560 if ( 'charitable_report_download_donors' !== $_POST['charitable_report_action'] ) {
561 return;
562 }
563
564 $args = array(
565 'report_type' => isset( $_POST['report_type'] ) ? esc_html( $_POST['report_type'] ) : false, // phpcs:ignore
566 );
567
568 $defaults = array(
569 'report_type' => 'donors-top',
570 );
571
572 $args = wp_parse_args( $args, $defaults );
573
574 $this->columns = array(
575 'first_name' => __( 'First Name', 'charitable' ),
576 'last_name' => __( 'Last Name', 'charitable' ),
577 'email' => __( 'Email', 'charitable' ),
578 'donor_id' => __( 'Donor ID', 'charitable' ),
579 'user_id' => __( 'User ID', 'charitable' ),
580 'date_joined' => __( 'Date Joined', 'charitable' ),
581 'total_amount' => __( 'Total Donations', 'charitable' ),
582 'average' => __( 'Average Donations', 'charitable' ),
583 'total_count_donations' => __( 'No. of Donations', 'charitable' ),
584 'total_count_campaigns' => __( 'Number of Campaigns', 'charitable' ),
585 'amount' => __( 'Last Donation Amount', 'charitable' ),
586 'last_campaign_title' => __( 'Last Donation Campaign', 'charitable' ),
587 'last_donation_date' => __( 'Last Donation Date', 'charitable' ),
588 );
589
590 $charitable_reports = Charitable_Reports::get_instance();
591 $charitable_reports->init_with_array( $args['report_type'] );
592
593 $donor_data = $charitable_reports->get_donor_report_by_type( $args['report_type'] );
594
595 // convert arrays of objects to arrays of arrays.
596 if ( is_array( $donor_data ) ) {
597 foreach ( $donor_data as $key => $value ) {
598 $donor_data[ $key ] = get_object_vars( $value );
599 }
600 }
601
602 $donor_data = $this->format_donors_download_data( $donor_data );
603
604 $this->export( $donor_data, $args['report_type'] );
605
606 exit;
607 }
608
609 /**
610 * Format the donor data for download.
611 *
612 * @since 1.8.1
613 *
614 * @param array $donor_data The donor data to format.
615 *
616 * @return array
617 */
618 public function format_donors_download_data( $donor_data = false ) {
619
620 if ( ! empty( $donor_data ) ) {
621 $donor_data = charitable_reports_format_money( $donor_data, array( 'total_amount', 'average', 'amount' ), false, false, '', true, true );
622 $donor_data = charitable_reports_format_date( $donor_data, array( 'date_joined', 'last_donation_date' ), false );
623 }
624
625 return $donor_data;
626 }
627
628 /**
629 * Generate the HTML for a breakdown of an advanced report.
630 *
631 * @since 1.8.1
632 *
633 * @return void
634 */
635 public function download_advanced() {
636
637 // check for nonce in $_POST and charitable_report_action variable.
638 if ( ! isset( $_POST['charitable_export_report_nonce'] ) || ! isset( $_POST['charitable_report_action'] ) ) {
639 return;
640 }
641
642 // check nonce.
643 if ( ! wp_verify_nonce( $_POST['charitable_export_report_nonce'], 'charitable_export_report' ) ) { // phpcs:ignore
644 return;
645 }
646
647 // check for admin.
648 if ( ! current_user_can( 'manage_options' ) ) {
649 return;
650 }
651
652 // check for export action.
653 if ( 'charitable_report_download_advanced' !== $_POST['charitable_report_action'] ) {
654 return;
655 }
656
657 $args = array(
658 'report_type' => isset( $_POST['report_type'] ) ? sanitize_title( $_POST['report_type'] ) : false, // phpcs:ignore
659 );
660
661 $defaults = array(
662 'report_type' => 'lybunt',
663 );
664
665 $args = wp_parse_args( $args, $defaults );
666
667 $report_args = charitable_reports_get_advanced_report_args();
668
669 $this->columns = array(
670 'donor_first_name' => __( 'First Name', 'charitable' ),
671 'donor_last_name' => __( 'Last Name', 'charitable' ),
672 'donor_email' => __( 'Email', 'charitable' ),
673 'donor_id' => __( 'Donor ID', 'charitable' ),
674 'user_id' => __( 'User ID', 'charitable' ),
675 'start_date_compare_from' => __( 'Start Date Compare From', 'charitable' ),
676 'end_date_compare_from' => __( 'End Date Compare From', 'charitable' ),
677 'total_donation_count_compare_from' => __( 'Donation Count Compare From', 'charitable' ),
678 'total_donation_amount_compare_from' => __( 'Donation Total Compare From', 'charitable' ),
679 'start_date_compare_to' => __( 'Start Date Compare To', 'charitable' ),
680 'end_date_compare_to' => __( 'End Date Compare To', 'charitable' ),
681 'total_donation_count_compare_to' => __( 'Donation Count Compare To', 'charitable' ),
682 'total_donation_amount_compare_to' => __( 'Donation Total Compare To', 'charitable' ),
683 'last_donation_amount' => __( 'Last Donation Amount', 'charitable' ),
684 'last_donation_campaigns' => __( 'Last Donated Campaign(s)', 'charitable' ),
685 'total_life_amount' => __( 'Lifetime Total', 'charitable' ),
686 );
687
688 $charitable_reports = Charitable_Reports::get_instance();
689 $charitable_reports->init_with_array( $args['report_type'] );
690 $advanced_report_data = $charitable_reports->get_advanced_data_by_report_type( $report_args );
691
692 // convert arrays of objects to arrays of arrays.
693 if ( is_array( $advanced_report_data ) ) {
694 foreach ( $advanced_report_data as $key => $value ) {
695 $advanced_report_data[ $key ] = get_object_vars( $value );
696 }
697 }
698
699 foreach ( $advanced_report_data as $key => $value ) {
700 // merge $report_args with every element of $advanced_report_data.
701 $advanced_report_data[ $key ] = array_merge( $report_args, $value );
702 $advanced_report_data[ $key ]['last_donation_campaigns'] = wp_strip_all_tags( implode( ', ', $value['last_donation_campaign_links'] ) );
703 }
704
705 $advanced_report_data = $this->format_advanced_download_data( $advanced_report_data );
706
707 $this->export( $advanced_report_data, $args['report_type'] );
708
709 exit;
710 }
711
712 /**
713 * Format the donor data for download.
714 *
715 * @since 1.8.1
716 *
717 * @param array $advanced_data The donor data to format.
718 *
719 * @return array
720 */
721 public function format_advanced_download_data( $advanced_data = false ) {
722
723 if ( ! empty( $advanced_data ) ) {
724 $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 );
725 $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' );
726 }
727
728 return $advanced_data;
729 }
730
731 /**
732 * Returns and/or create the single instance of this class.
733 *
734 * @since 1.8.1
735 *
736 * @return Charitable_Reports_Download
737 */
738 public static function get_instance() {
739 if ( is_null( self::$instance ) ) {
740 self::$instance = new self();
741 }
742
743 return self::$instance;
744 }
745 }
746 endif;
747