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-ajax.php

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

457 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Reports Ajax.
4 *
5 * @package Charitable/Classes/Charitable_Reports_Ajax
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_Ajax' ) ) :
19
20 /**
21 * Charitable_Reports_Ajax
22 *
23 * @final
24 * @since 1.8.1
25 */
26 class Charitable_Reports_Ajax {
27
28 /**
29 * The single instance of this class.
30 *
31 * @var Charitable_Reports_Ajax|null
32 */
33 private static $instance = null;
34
35 /**
36 * Create object instance.
37 *
38 * @since 1.8.1
39 */
40 public function __construct() {
41 }
42
43 /**
44 * Run things upon init.
45 *
46 * @since 1.8.1
47 */
48 public function init() {
49 }
50
51 /**
52 * Generate and return data that overview report needs.
53 *
54 * @since 1.8.1
55 *
56 * @return void
57 */
58 public function get_overview_data() {
59
60 // security check.
61 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
62 wp_send_json_error( 'Invalid nonce.' );
63 exit;
64 }
65
66 // Current user cannot handle this request.
67 if ( ! current_user_can( 'manage_options' ) ) {
68 return;
69 }
70
71 $start_date = ! empty( $_POST['start_date'] ) ? sanitize_text_field( $_POST['start_date'] ) : false; // phpcs:ignore
72 $end_date = ! empty( $_POST['end_date'] ) ? sanitize_text_field( $_POST['end_date'] ) : false; // phpcs:ignore
73 $campaign_id = ! empty( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : false; // phpcs:ignore
74 $category_id = ! empty( $_POST['campaign_category_id'] ) ? intval( $_POST['campaign_category_id'] ) : false; // phpcs:ignore
75 $filter = ! empty( $_POST['filter'] ) ? sanitize_text_field( $_POST['filter'] ) : false; // phpcs:ignore
76 $report_type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : 'overview'; // phpcs:ignore
77 $status = ! empty( $_POST['status'] ) ? sanitize_text_field( $_POST['status'] ) : false; // phpcs:ignore
78
79 $start_date = str_replace( '/', '-', $start_date );
80 $end_date = str_replace( '/', '-', $end_date );
81
82 $charitable_reports = Charitable_Reports::get_instance();
83
84 $args = array(
85 'start_date' => $start_date,
86 'end_date' => $end_date,
87 'status' => $status,
88 'campaign_id' => $campaign_id,
89 'category_id' => $category_id,
90 );
91
92 $charitable_reports->init_with_array( $report_type, $args );
93
94 $donations = $charitable_reports->get_donations();
95
96 $charitable_reports->init_axis_with_donations( $donations, $start_date, $end_date );
97
98 $donations = $charitable_reports->get_donations();
99 $total_count_donations = count( $donations );
100 $total_amount_donations = $charitable_reports->get_donations_total();
101 $donation_breakdown = $charitable_reports->get_donations_by_day();
102 $donation_average = intval( $total_amount_donations ) > 0 && intval( $total_count_donations ) > 0 ? charitable_format_money( $total_amount_donations / $total_count_donations, 2, true ) : 0;
103 $donation_total = charitable_format_money( $total_amount_donations, 2, true );
104 $total_count_donors = $charitable_reports->get_donors_count();
105
106 $donation_breakdown = $charitable_reports->get_donations_by_day();
107
108 $refunds_data = $charitable_reports->get_refunds();
109 $total_amount_refunds = charitable_format_money( $refunds_data['total_amount'], 2, true );
110 $total_count_refunds = $refunds_data['total_count'];
111
112 $payment_breakdown = $charitable_reports->get_donations_by_payment();
113
114 $top_donors = $charitable_reports->get_top_donors_overview();
115
116 // campaigns.
117 $top_campaigns_args = false;
118 $top_campaigns = Charitable_Campaigns::ordered_by_amount( $top_campaigns_args );
119
120 // main activity.
121 $report_activity = $charitable_reports->get_activity();
122
123 $report_html = array();
124
125 $report_html['html']['donation_breakdown'] = $charitable_reports->generate_donations_breakdown_rows( $donation_breakdown, $refunds_data );
126 $report_html['headline_chart_options']['donation_axis'] = (array) $charitable_reports->donation_axis;
127 $report_html['headline_chart_options']['date_axis'] = (array) $charitable_reports->date_axis;
128 $report_html['donation_amount'] = $donation_total;
129 $report_html['donation_count'] = $total_count_donations;
130 $report_html['donation_average'] = $donation_average;
131 $report_html['donors_count'] = $total_count_donors;
132 $report_html['refund_total_amount'] = $total_amount_refunds;
133 $report_html['refund_count'] = $total_count_refunds;
134 $report_html['payment_methods_chart']['payment_percentages'] = (array) array_reverse( $charitable_reports->payment_percentages );
135 $report_html['payment_methods_chart']['payment_labels'] = (array) array_reverse( $charitable_reports->payment_labels );
136 $report_html['html']['payment_methods_list'] = $charitable_reports->generate_payment_methods_list( $payment_breakdown );
137 $report_html['html']['activities'] = $charitable_reports->generate_activity_list( $report_activity );
138 $report_html['html']['top_donors_list'] = $charitable_reports->generate_top_donors( $top_donors );
139 $report_html['html']['top_campaigns_list'] = $charitable_reports->generate_top_campaigns( $top_campaigns );
140
141 // Cache via transients the 'charitable_report_overview_args' for 1 hour.
142 $charitable_report_overview_args = array(
143 'start_date' => $start_date,
144 'end_date' => $end_date,
145 'filter' => $filter,
146 'campaign_id' => $campaign_id,
147 );
148
149 $was_cached = set_transient( 'charitable-report-overview-args', $charitable_report_overview_args, 60 * 60 );
150
151 $report_html['was_cached'] = $was_cached;
152
153 wp_send_json_success( $report_html );
154
155 exit;
156 }
157
158 /**
159 * Generate and return data that activity report needs.
160 *
161 * @since 1.8.1
162 *
163 * @return void
164 */
165 public function get_activity_data() {
166
167 // security check.
168 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
169 wp_send_json_error( 'Invalid nonce.' );
170 exit;
171 }
172
173 // Current user cannot handle this request.
174 if ( ! current_user_can( 'manage_options' ) ) {
175 return;
176 }
177
178 $report_html = array();
179
180 $types = array( 'campaign', 'donation' );
181 $action = ! empty( $_POST['activity_type'] ) ? esc_attr( $_POST['activity_type'] ) : ''; // phpcs:ignore
182 $campaign_id = ! empty( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : false; // phpcs:ignore
183 $category_id = ! empty( $_POST['category_id'] ) ? intval( $_POST['category_id'] ) : false; // phpcs:ignore
184 $activity_type = ! empty( $_POST['activity_type'] ) ? sanitize_text_field( $_POST['activity_type'] ) : ''; // phpcs:ignore
185 $start_date = ! empty( $_POST['start_date'] ) ? sanitize_text_field( $_POST['start_date'] ) : false; // phpcs:ignore
186 $end_date = ! empty( $_POST['end_date'] ) ? sanitize_text_field( $_POST['end_date'] ) : false; // phpcs:ignore
187 $limit = ! empty( $_POST['limit'] ) ? intval( $_POST['limit'] ) : false; // phpcs:ignore
188
189 $start_date = str_replace( '/', '-', $start_date );
190 $end_date = str_replace( '/', '-', $end_date );
191
192 $admin_activities = new Charitable_Admin_Activities();
193 $activities_args = $admin_activities->get_activity(
194 array(
195 'activity_filter_types' => array( $action ),
196 'campaign_id' => $campaign_id,
197 'category_id' => $category_id,
198 'activity_type' => $activity_type,
199 'start_date' => $start_date,
200 'end_date' => $end_date,
201 'limit' => $limit,
202 )
203 );
204 $report_activity = $admin_activities->get_activity_report_data( $activities_args );
205
206 $charitable_reports = Charitable_Reports::get_instance();
207
208 $report_html['html']['activities'] = $charitable_reports->generate_activity_list( $report_activity );
209
210 // Cache via transients the 'charitable_report_activity_args' for 1 hour.
211 $charitable_report_activity_args = array(
212 'start_date' => $start_date,
213 'end_date' => $end_date,
214 'activity_type' => $activity_type,
215 'campaign_id' => $campaign_id,
216 );
217
218 $was_cached = set_transient( 'charitable-report-activity-args', $charitable_report_activity_args, 60 * 60 );
219
220 $report_html['was_cached'] = $was_cached;
221
222 wp_send_json_success( $report_html );
223
224 exit;
225 }
226
227 /**
228 * Generate and return data that donor report needs.
229 *
230 * @since 1.8.1
231 *
232 * @return void
233 */
234 public function get_donor_data() {
235
236 // security check.
237 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
238 wp_send_json_error( 'Invalid nonce.' );
239 exit;
240 }
241
242 // Current user cannot handle this request.
243 if ( ! current_user_can( 'manage_options' ) ) {
244 return;
245 }
246
247 $report_html = array();
248 $donor_breakdown = array();
249
250 $donor_report_type = isset( $_POST['report_type'] ) ? esc_attr( $_POST['report_type'] ) : 'donors-top'; // phpcs:ignore
251 $limit = isset( $_POST['limit'] ) ? intval( $_POST['limit'] ) : false;
252
253 $args = array();
254 $args['limit'] = ! $limit ? charitable_reports_get_pagination_per_page( $donor_report_type ) : $limit; // phpcs:ignore
255 $args['offset'] = isset( $_POST['offset'] ) ? intval( $_POST['offset'] ) : 0; // phpcs:ignore
256 $args['ppage'] = isset( $_POST['ppage'] ) ? intval( $_POST['ppage'] ) : 1; // phpcs:ignore
257
258 $charitable_reports = Charitable_Reports::get_instance();
259 $charitable_reports->init_with_array( $donor_report_type, $args );
260
261 switch ( $donor_report_type ) {
262 case 'donors-top':
263 $donor_breakdown = $charitable_reports->get_top_donors();
264 break;
265 case 'donors-recurring':
266 $donor_breakdown = $charitable_reports->get_recurring_donors();
267 break;
268 case 'donors-first-time':
269 $donor_breakdown = $charitable_reports->get_first_time_donors();
270 break;
271
272 default:
273 // code...
274 break;
275 }
276
277 $report_html['html']['donors'] = $charitable_reports->generate_donor_breakdown_table_html( $donor_report_type, $donor_breakdown, $args['ppage'], $limit );
278 $report_html['html']['title_card'] = $charitable_reports->generate_title_card_html( $donor_report_type );
279
280 // Cache via transients the 'charitable_report_donor_args' for 1 hour.
281 $charitable_report_donor_args = array(
282 'donor_report_type' => $donor_report_type,
283 'limit' => $limit,
284 );
285
286 $was_cached = set_transient( 'charitable-report-donor-args', $charitable_report_donor_args, 60 * 60 );
287
288 $report_html['was_cached'] = $was_cached;
289
290 wp_send_json_success( $report_html );
291 }
292
293 /**
294 * Generate and return data that advanced screen (UI) needs.
295 * This allows addons and third parties to hook in and change the UI in the top of the advanced report page.
296 *
297 * @since 1.8.1
298 *
299 * @return void
300 */
301 public function get_advanced_report_ui() {
302
303 // security check.
304 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
305 wp_send_json_error( 'Invalid nonce.' );
306 exit;
307 }
308
309 // Current user cannot handle this request.
310 if ( ! current_user_can( 'manage_options' ) ) {
311 return;
312 }
313
314 $args = array();
315 $args['start_date'] = isset( $_POST['start_date'] ) ? sanitize_text_field( $_POST['start_date'] ) : false; // phpcs:ignore
316 $args['end_date'] = isset( $_POST['end_date'] ) ? sanitize_text_field( $_POST['end_date'] ) : false; // phpcs:ignore
317 $args['selected_report_type'] = isset( $_POST['report_type'] ) ? esc_attr( $_POST['report_type'] ) : false; // phpcs:ignore
318 $args['datepickers'] = false;
319
320 $ui_html = apply_filters( 'charitable_report_advanced_ui', array(), $args );
321
322 wp_send_json_success( $ui_html );
323
324 exit;
325 }
326
327 /**
328 * Generate and return data that dashboard needs.
329 *
330 * @since 1.8.1
331 *
332 * @return void
333 */
334 public function get_dashboard_data() {
335
336 // security check.
337 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
338 wp_send_json_error( 'Invalid nonce.' );
339 exit;
340 }
341
342 // Current user cannot handle this request.
343 if ( ! current_user_can( 'manage_options' ) ) {
344 return;
345 }
346
347 $report_html = array();
348
349 $start_date = isset( $_POST['start_date'] ) ? sanitize_text_field( $_POST['start_date'] ) : false; // phpcs:ignore
350 $end_date = isset( $_POST['end_date'] ) ? sanitize_text_field( $_POST['end_date'] ) : false; // phpcs:ignore
351 $days = isset( $_POST['days'] ) ? intval( $_POST['days'] ) : false;
352
353 $charitable_dashboard = Charitable_Dashboard_Legacy::get_instance();
354 $report_html['html'] = $charitable_dashboard->generate_dashboard_report_html( array( 'start_date' => $start_date, 'end_date' => $end_date, 'days' => $days ) ); // phpcs:ignore
355 $report_html['headline_chart_options']['donation_axis'] = (array) $charitable_dashboard->get_donation_axis();
356 $report_html['headline_chart_options']['date_axis'] = (array) $charitable_dashboard->get_date_axis();
357
358 // we go by days with dashbaord so adjust the start_date and end_date accordingly.
359 // Start date should be today's date and end date should be today's date plus the number of days.
360 // If days is 0, then we use the start_date and end_date.
361 if ( 0 === $days ) {
362 $start_date = $charitable_dashboard->get_start_date();
363 $end_date = $charitable_dashboard->get_end_date();
364 } else {
365 $start_date = gmdate( 'Y-m-d', strtotime( 'today' ) );
366 $end_date = gmdate( 'Y-m-d', strtotime( 'today' ) + ( $days * 24 * 60 * 60 ) );
367 }
368 $report_html['start_date'] = $start_date;
369 $report_html['end_date'] = $end_date;
370 $report_html['days'] = $days;
371
372 // Cache via transients the 'charitable_report_donor_args' for 1 hour.
373 $charitable_dashboard_data_args = array(
374 'start_date' => $start_date,
375 'end_date' => $end_date,
376 'days' => $days,
377 'timestamp' => gmdate( 'Y/m/d h:i A', current_time( 'timestamp', 0 ) ),
378 );
379
380 $args_was_cached = set_transient( 'wpch_dashboard_data_args', $charitable_dashboard_data_args, DAY_IN_SECONDS );
381 // $html_was_cached = set_transient( 'wpch_dashboard_data_html', $report_html, 60 * 60 );
382
383 $report_html['args_was_cached'] = $args_was_cached;
384
385 wp_send_json_success( $report_html );
386 }
387
388 /**
389 * Generate and return data that the default advanced report "lybunt" needs.
390 *
391 * @since 1.8.1
392 *
393 * @return void
394 */
395 public function get_lybunt_report() {
396
397 // type check.
398 if ( ! isset( $_POST['report_type'] ) || 'lybunt' !== $_POST['report_type'] ) {
399 return;
400 }
401
402 // security check.
403 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-reporting' ) ) { // phpcs:ignore
404 wp_send_json_error( 'Invalid nonce.' );
405 exit;
406 }
407
408 // Current user cannot handle this request.
409 if ( ! current_user_can( 'manage_options' ) ) {
410 return;
411 }
412
413 $report_args = charitable_reports_get_advanced_report_args();
414
415 $charitable_reports = Charitable_Reports::get_instance();
416
417 if ( charitable_is_pro() ) :
418
419 $charitable_reports->init_with_array( 'lybunt' );
420
421 $advanced_report_data = $charitable_reports->get_advanced_data_by_report_type( $report_args );
422
423 else :
424
425 $advanced_report_data = $charitable_reports->get_data_sample( 'advanced' );
426
427 endif;
428
429 $start_date_compare_from = $report_args['start_date_compare_from'];
430 $end_date_compare_from = $report_args['end_date_compare_from'];
431 $start_date_compare_to = $report_args['start_date_compare_to'];
432 $end_date_compare_to = $report_args['end_date_compare_to'];
433
434 $report_html = array();
435 $report_html['html'] = $charitable_reports->generate_advanced_breakdown_report_html( 'lybunt', $advanced_report_data, $report_args );
436
437 wp_send_json_success( $report_html );
438 }
439
440 /**
441 * Returns and/or create the single instance of this class.
442 *
443 * @since 1.8.1
444 *
445 * @return Charitable_Reports_Ajax
446 */
447 public static function get_instance() {
448 if ( is_null( self::$instance ) ) {
449 self::$instance = new self();
450 }
451
452 return self::$instance;
453 }
454 }
455
456 endif;
457