| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Dashboard AJAX Handler. |
| 4 |
* |
| 5 |
* Handles AJAX requests for the dashboard functionality. |
| 6 |
* |
| 7 |
* @package Charitable/Admin/Dashboard |
| 8 |
* @author David Bisset |
| 9 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.8.1 |
| 12 |
* @version 1.8.1 |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! class_exists( 'Charitable_Dashboard_Ajax' ) ) : |
| 21 |
|
| 22 |
/** |
| 23 |
* Charitable Dashboard AJAX Handler. |
| 24 |
* |
| 25 |
* @since 1.8.1 |
| 26 |
*/ |
| 27 |
class Charitable_Dashboard_Ajax { |
| 28 |
|
| 29 |
/** |
| 30 |
* Single instance of this class. |
| 31 |
* |
| 32 |
* @since 1.8.1 |
| 33 |
* |
| 34 |
* @var Charitable_Dashboard_Ajax |
| 35 |
*/ |
| 36 |
private static $instance = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* Create and return the single instance of this class. |
| 40 |
* |
| 41 |
* @since 1.8.1 |
| 42 |
* |
| 43 |
* @return Charitable_Dashboard_Ajax |
| 44 |
*/ |
| 45 |
public static function get_instance() { |
| 46 |
if ( is_null( self::$instance ) ) { |
| 47 |
self::$instance = new self(); |
| 48 |
} |
| 49 |
|
| 50 |
return self::$instance; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Set up the class. |
| 55 |
* |
| 56 |
* @since 1.8.1 |
| 57 |
*/ |
| 58 |
private function __construct() { |
| 59 |
// Constructor intentionally empty - hooks are registered in the hooks file |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get dashboard data via AJAX. |
| 64 |
* |
| 65 |
* @since 1.8.1 |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function get_dashboard_data() { |
| 70 |
|
| 71 |
// Security check. |
| 72 |
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'charitable-dashboard' ) ) { // phpcs:ignore |
| 73 |
wp_send_json_error( 'Invalid nonce.' ); |
| 74 |
exit; |
| 75 |
} |
| 76 |
|
| 77 |
// Current user cannot handle this request. |
| 78 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 79 |
wp_send_json_error( 'Insufficient permissions.' ); |
| 80 |
exit; |
| 81 |
} |
| 82 |
|
| 83 |
$time_period = isset( $_POST['time_period'] ) ? sanitize_text_field( wp_unslash( $_POST['time_period'] ) ) : 'last-7-days'; // phpcs:ignore |
| 84 |
|
| 85 |
// Validate time period. |
| 86 |
$valid_periods = array( 'last-7-days', 'last-14-days', 'last-30-days' ); |
| 87 |
if ( ! in_array( $time_period, $valid_periods, true ) ) { |
| 88 |
wp_send_json_error( 'Invalid time period.' ); |
| 89 |
exit; |
| 90 |
} |
| 91 |
|
| 92 |
try { |
| 93 |
// Get dashboard instance. |
| 94 |
$charitable_dashboard = Charitable_Dashboard::get_instance(); |
| 95 |
|
| 96 |
// If requested via URL or POST, clear stats cache so AJAX reflects latest logic |
| 97 |
$clear_cache = ( isset( $_GET['charitable_clear_stats_cache'] ) && '1' === $_GET['charitable_clear_stats_cache'] ) || |
| 98 |
( isset( $_POST['charitable_clear_stats_cache'] ) && '1' === $_POST['charitable_clear_stats_cache'] ); |
| 99 |
|
| 100 |
$response_data = array(); |
| 101 |
|
| 102 |
if ( $clear_cache ) { // phpcs:ignore |
| 103 |
$charitable_dashboard->clear_dashboard_stats_cache(); |
| 104 |
$response_data['cache_cleared'] = true; |
| 105 |
} |
| 106 |
|
| 107 |
// Get stats data. |
| 108 |
$stats = $charitable_dashboard->get_dashboard_stats( $time_period ); |
| 109 |
|
| 110 |
// Get chart data using legacy dashboard methods. |
| 111 |
$legacy_dashboard = Charitable_Dashboard_Legacy::get_instance(); |
| 112 |
|
| 113 |
// Set up the legacy dashboard with the correct time period. |
| 114 |
$date_range = $charitable_dashboard->get_date_range_for_period( $time_period ); |
| 115 |
$legacy_dashboard->generate_dashboard_report_html( array( |
| 116 |
'start_date' => $date_range['start_date'], |
| 117 |
'end_date' => $date_range['end_date'], |
| 118 |
'days' => $date_range['days'], |
| 119 |
) ); |
| 120 |
|
| 121 |
// Get chart axis data. |
| 122 |
$date_axis = $legacy_dashboard->get_date_axis(); |
| 123 |
|
| 124 |
// For 30-day periods, format dates as M/d instead of M d |
| 125 |
if ( 'last-30-days' === $time_period ) { |
| 126 |
$date_axis = array_map( function( $date ) { |
| 127 |
// Convert "Aug 27" to "8/27" |
| 128 |
$timestamp = strtotime( $date ); |
| 129 |
return date( 'n/j', $timestamp ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 130 |
}, $date_axis ); |
| 131 |
} |
| 132 |
|
| 133 |
$chart_data = array( |
| 134 |
'donation_axis' => $legacy_dashboard->get_donation_axis(), |
| 135 |
'date_axis' => $date_axis, |
| 136 |
); |
| 137 |
|
| 138 |
// Prepare response data. |
| 139 |
$response_data = array_merge( $response_data, array( |
| 140 |
'stats' => $stats, |
| 141 |
'chart' => $chart_data, |
| 142 |
) ); |
| 143 |
|
| 144 |
wp_send_json_success( $response_data ); |
| 145 |
|
| 146 |
} catch ( Exception $e ) { |
| 147 |
wp_send_json_error( 'Failed to load dashboard data: ' . $e->getMessage() ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
endif; |
| 153 |
|