PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / trunk
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management vtrunk
1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / api / dashboard-api.php

dashboard-api.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management trunk, at inc/api/dashboard-api.php

312 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard REST API endpoints.
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\API;
9
10 use SureDonation\Inc\Database\Tables\Donations;
11 use SureDonation\Inc\Database\Tables\Donors;
12 use SureDonation\Inc\Payments\Payment_Helper;
13 use WP_REST_Request;
14 use WP_REST_Response;
15 use WP_REST_Server;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Dashboard API class.
24 *
25 * @since 0.0.1
26 */
27 class Dashboard_API {
28 /**
29 * Get dashboard endpoints.
30 *
31 * @return array<string, array<string, mixed>>
32 * @since 0.0.1
33 */
34 public function get_endpoints() {
35 return [
36 // Get dashboard statistics.
37 '/dashboard/stats' => [
38 'methods' => WP_REST_Server::READABLE,
39 'callback' => [ $this, 'get_stats' ],
40 'permission_callback' => [ $this, 'check_permissions' ],
41 ],
42
43 // Get recent donations for dashboard.
44 '/dashboard/recent-donations' => [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'get_recent_donations' ],
47 'permission_callback' => [ $this, 'check_permissions' ],
48 'args' => [
49 'limit' => [
50 'default' => 5,
51 'sanitize_callback' => 'absint',
52 ],
53 ],
54 ],
55
56 // Get top campaigns for dashboard.
57 '/dashboard/top-campaigns' => [
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => [ $this, 'get_top_campaigns' ],
60 'permission_callback' => [ $this, 'check_permissions' ],
61 'args' => [
62 'limit' => [
63 'default' => 5,
64 'sanitize_callback' => 'absint',
65 ],
66 ],
67 ],
68
69 // Get top donors for dashboard.
70 '/dashboard/top-donors' => [
71 'methods' => WP_REST_Server::READABLE,
72 'callback' => [ $this, 'get_top_donors' ],
73 'permission_callback' => [ $this, 'check_permissions' ],
74 'args' => [
75 'limit' => [
76 'default' => 5,
77 'sanitize_callback' => 'absint',
78 ],
79 ],
80 ],
81
82 // Get donation trends for chart.
83 '/dashboard/trends' => [
84 'methods' => WP_REST_Server::READABLE,
85 'callback' => [ $this, 'get_trends' ],
86 'permission_callback' => [ $this, 'check_permissions' ],
87 'args' => [
88 'after' => [
89 'sanitize_callback' => 'sanitize_text_field',
90 'validate_callback' => static function ( $param ) {
91 return empty( $param ) || false !== strtotime( $param );
92 },
93 ],
94 'before' => [
95 'sanitize_callback' => 'sanitize_text_field',
96 'validate_callback' => static function ( $param ) {
97 return empty( $param ) || false !== strtotime( $param );
98 },
99 ],
100 'group' => [
101 'default' => 'day',
102 'enum' => [ 'day', 'week', 'month' ],
103 ],
104 ],
105 ],
106 ];
107 }
108
109 /**
110 * Get dashboard statistics.
111 *
112 * @param WP_REST_Request $request Request object.
113 * @return WP_REST_Response Response object.
114 * @since 0.0.1
115 */
116 public function get_stats( $request ) {
117 unset( $request ); // Unused parameter.
118
119 $stats = Donations::get_dashboard_stats();
120 $currency = Payment_Helper::get_currency();
121
122 // Count active (published) campaigns.
123 $active_campaigns = wp_count_posts( 'suredonation_cmpgn' );
124 $active_count = isset( $active_campaigns->publish ) ? (int) $active_campaigns->publish : 0;
125
126 return new WP_REST_Response(
127 [
128 'success' => true,
129 'stats' => [
130 'total_donations' => (int) $stats['total_donations'],
131 'total_raised' => (float) $stats['total_raised'],
132 'unique_donors' => (int) $stats['unique_donors'],
133 'average_donation' => (float) $stats['average_donation'],
134 'largest_donation' => (float) $stats['largest_donation'],
135 'active_campaigns' => $active_count,
136 'currency' => $currency,
137 ],
138 ],
139 200
140 );
141 }
142
143 /**
144 * Get recent donations.
145 *
146 * @param WP_REST_Request $request Request object.
147 * @return WP_REST_Response Response object.
148 * @since 0.0.1
149 */
150 public function get_recent_donations( $request ) {
151 $limit = $request->get_param( 'limit' );
152 $donations = Donations::get_recent_donations_global( $limit );
153 $currency = Payment_Helper::get_currency();
154
155 $formatted = [];
156 foreach ( $donations as $donation ) {
157 $campaign_id_val = $donation['campaign_id'] ?? 0;
158 $campaign_id = is_numeric( $campaign_id_val ) ? (int) $campaign_id_val : 0;
159 $id_val = $donation['id'] ?? 0;
160 $amount_val = $donation['amount'] ?? 0;
161
162 $formatted[] = [
163 'id' => is_numeric( $id_val ) ? (int) $id_val : 0,
164 'donor_name' => $donation['donor_name'] ?? '',
165 'donor_email' => $donation['donor_email'] ?? '',
166 'amount' => is_numeric( $amount_val ) ? (float) $amount_val : 0.0,
167 'currency' => $donation['currency'] ?? $currency,
168 'campaign_id' => $campaign_id,
169 'campaign_title' => $campaign_id ? get_the_title( $campaign_id ) : '',
170 'is_anonymous' => ! empty( $donation['is_anonymous'] ),
171 'donation_type' => $donation['donation_type'] ?? 'one-time',
172 'gateway' => $donation['gateway'] ?? '',
173 'payment_status' => $donation['payment_status'] ?? 'pending',
174 'created_at' => $donation['created_at'] ?? '',
175 ];
176 }
177
178 return new WP_REST_Response(
179 [
180 'success' => true,
181 'donations' => $formatted,
182 ],
183 200
184 );
185 }
186
187 /**
188 * Get top campaigns.
189 *
190 * @param WP_REST_Request $request Request object.
191 * @return WP_REST_Response Response object.
192 * @since 0.0.1
193 */
194 public function get_top_campaigns( $request ) {
195 $limit = $request->get_param( 'limit' );
196 $campaigns = Donations::get_top_campaigns( $limit );
197 $currency = Payment_Helper::get_currency();
198
199 $formatted = [];
200 foreach ( $campaigns as $campaign ) {
201 $campaign_id = isset( $campaign['campaign_id'] ) ? (int) $campaign['campaign_id'] : 0;
202 $post = get_post( $campaign_id );
203
204 if ( ! $post || SUREDONATION_POST_TYPE !== $post->post_type ) {
205 continue;
206 }
207
208 // Get goal amount from post meta.
209 $goal_amount = get_post_meta( $campaign_id, '_suredonation_goal_amount', true );
210 $goal_amount_float = is_numeric( $goal_amount ) ? (float) $goal_amount : null;
211
212 $formatted[] = [
213 'id' => $campaign_id,
214 'title' => $post->post_title,
215 'total_raised' => (float) $campaign['total_raised'],
216 'donation_count' => (int) $campaign['donation_count'],
217 'unique_donors' => (int) $campaign['unique_donors'],
218 'goal_amount' => $goal_amount_float,
219 'currency' => $currency,
220 ];
221 }
222
223 return new WP_REST_Response(
224 [
225 'success' => true,
226 'campaigns' => $formatted,
227 ],
228 200
229 );
230 }
231
232 /**
233 * Get top donors.
234 *
235 * @param WP_REST_Request $request Request object.
236 * @return WP_REST_Response Response object.
237 * @since 0.0.1
238 */
239 public function get_top_donors( $request ) {
240 $limit = $request->get_param( 'limit' );
241 $donors = Donors::get_top_donors( $limit );
242 $currency = Payment_Helper::get_currency();
243
244 $formatted = [];
245 foreach ( $donors as $donor ) {
246 $id_val = $donor['id'] ?? 0;
247 $donated_val = $donor['total_donated'] ?? 0;
248 $count_val = $donor['donation_count'] ?? 0;
249
250 $formatted[] = [
251 'id' => is_numeric( $id_val ) ? (int) $id_val : 0,
252 'name' => $donor['name'] ?? '',
253 'email' => $donor['email'] ?? '',
254 'total_donated' => is_numeric( $donated_val ) ? (float) $donated_val : 0.0,
255 'donation_count' => is_numeric( $count_val ) ? (int) $count_val : 0,
256 'currency' => $currency,
257 ];
258 }
259
260 return new WP_REST_Response(
261 [
262 'success' => true,
263 'donors' => $formatted,
264 ],
265 200
266 );
267 }
268
269 /**
270 * Get donation trends for chart.
271 *
272 * @param WP_REST_Request $request Request object.
273 * @return WP_REST_Response Response object.
274 * @since 0.0.1
275 */
276 public function get_trends( $request ) {
277 $after = $request->get_param( 'after' );
278 $before = $request->get_param( 'before' );
279 $group = $request->get_param( 'group' );
280 $trends = Donations::get_donation_trends( $after, $before, $group );
281 $currency = Payment_Helper::get_currency();
282
283 $formatted = [];
284 foreach ( $trends as $trend ) {
285 $formatted[] = [
286 'period' => $trend['period'] ?? '',
287 'donation_count' => isset( $trend['donation_count'] ) ? (int) $trend['donation_count'] : 0,
288 'total_amount' => isset( $trend['total_amount'] ) ? (float) $trend['total_amount'] : 0,
289 ];
290 }
291
292 return new WP_REST_Response(
293 [
294 'success' => true,
295 'trends' => $formatted,
296 'currency' => $currency,
297 ],
298 200
299 );
300 }
301
302 /**
303 * Check if user has permission to view dashboard.
304 *
305 * @return bool True if user has permission.
306 * @since 0.0.1
307 */
308 public function check_permissions() {
309 return current_user_can( 'manage_options' );
310 }
311 }
312