PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.13.4
GiveWP – Donation Plugin and Fundraising Platform v2.13.4
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / API / Endpoints / Reports / Endpoint.php
give / src / API / Endpoints / Reports Last commit date
AverageDonation.php 5 years ago DonationsVsIncome.php 5 years ago Endpoint.php 5 years ago FormPerformance.php 5 years ago Income.php 5 years ago IncomeBreakdown.php 5 years ago PaymentMethods.php 5 years ago PaymentStatuses.php 5 years ago RecentDonations.php 5 years ago TopDonors.php 5 years ago TotalDonors.php 5 years ago TotalIncome.php 5 years ago TotalRefunds.php 5 years ago
Endpoint.php
442 lines
1 <?php
2
3 /**
4 * Reports base endpoint
5 *
6 * @package Give
7 */
8
9 namespace Give\API\Endpoints\Reports;
10
11 use DateInterval;
12 use DateTime;
13 use Give\API\RestRoute;
14 use \Give_Cache;
15 use Give_Payment;
16 use WP_Error;
17 use WP_REST_Request;
18 use WP_REST_Response;
19
20 abstract class Endpoint implements RestRoute {
21 /**
22 * @since 2.6.1
23 * @var WP_REST_Request
24 */
25 protected $request;
26
27 /**
28 * @var DateTime
29 */
30 protected $startDate;
31
32 /**
33 * @var DateTime
34 */
35 protected $endDate;
36
37 /**
38 * @var DateInterval
39 */
40 protected $dateDiff;
41
42 /**
43 * @var string
44 */
45 protected $endpoint;
46
47 /**
48 * @var boolean
49 */
50 protected $testMode;
51
52 /**
53 * @var string
54 */
55 protected $currency;
56
57 /**
58 * @var array
59 */
60 protected $schema;
61
62 /**
63 * @inheritDoc
64 */
65 public function registerRoute() {
66 register_rest_route(
67 'give-api/v2',
68 '/reports/' . $this->endpoint,
69 [
70 // Here we register the readable endpoint
71 [
72 'methods' => 'GET',
73 'callback' => [ $this, 'handleRequest' ],
74 'permission_callback' => [ $this, 'permissionsCheck' ],
75 'args' => [
76 'start' => [
77 'type' => 'string',
78 'required' => true,
79 'validate_callback' => [ $this, 'validateDate' ],
80 'sanitize_callback' => [ $this, 'sanitizeDate' ],
81 ],
82 'end' => [
83 'type' => 'string',
84 'required' => true,
85 'validate_callback' => [ $this, 'validateDate' ],
86 'sanitize_callback' => [ $this, 'sanitizeDate' ],
87 ],
88 'currency' => [
89 'type' => 'string',
90 'required' => true,
91 'validate_callback' => [ $this, 'validateCurrency' ],
92 ],
93 'testMode' => [
94 'type' => 'boolean',
95 'required' => true,
96 'sanitize_callback' => [ $this, 'sanitizeTestMode' ],
97 ],
98 ],
99 ],
100 // Register our schema callback.
101 'schema' => [ $this, 'getReportSchema' ],
102 ]
103 );
104 }
105
106 /**
107 * Handle rest request.
108 *
109 * @since 2.6.1
110 *
111 * @param WP_REST_Request $request
112 *
113 * @return WP_REST_Response
114 */
115 public function handleRequest( $request ) {
116 // Check if a cached version exists
117 $cached_report = $this->getCachedReport( $request );
118 if ( $cached_report !== null ) {
119 // Bail and return the cached version
120 return new WP_REST_Response( $cached_report );
121 }
122
123 $this->setupProperties( $request );
124
125 $responseData = [
126 'status' => $this->getGiveStatus(),
127 'data' => $this->getReport( $request ),
128 ];
129
130 $this->cacheReport( $request, $responseData );
131
132 return new WP_REST_Response( $responseData );
133 }
134
135 /**
136 * Setup properties
137 *
138 * @since 2.6.1
139 *
140 * @param WP_REST_Request $request
141 */
142 private function setupProperties( $request ) {
143 $this->request = $request;
144 $this->startDate = date_create( $request->get_param( 'start' ) );
145 $this->endDate = date_create( $request->get_param( 'end' ) );
146 $this->currency = $request->get_param( 'currency' );
147 $this->testMode = $request->get_param( 'testMode' );
148 $this->dateDiff = date_diff( $this->startDate, $this->endDate );
149 }
150
151 public function validateDate( $param, $request, $key ) {
152 // Check that date is valid, and formatted YYYY-MM-DD
153 $exploded = explode( '-', $param );
154 $valid = checkdate( $exploded[1], $exploded[2], $exploded[0] );
155
156 // If checking end date, check that it is after start date
157 if ( $key === 'end' ) {
158 $start = date_create( $request->get_param( 'start' ) );
159 $end = date_create( $request->get_param( 'end' ) );
160 $valid = $start <= $end ? $valid : false;
161 }
162
163 return $valid;
164 }
165
166 /**
167 * @since 2.9.0 Restrict appended time to only the end date.
168 * @since 2.6.1
169 */
170 public function sanitizeDate( $param, $request, $key ) {
171 // Return Date object from parameter
172 $exploded = explode( '-', $param );
173
174 $sanitizedDate = "{$exploded[0]}-{$exploded[1]}-{$exploded[2]}";
175
176 if ( 'end' === $key ) {
177 /**
178 * For the end date manually specify an end time.
179 */
180 $sanitizedDate .= ' 24:00:00';
181 }
182
183 return $sanitizedDate;
184 }
185
186 /**
187 * Validate currency string
188 * Check if currency code provided to REST APi is valid
189 *
190 * @param string $param Currency parameter provided in REST API request
191 * @param WP_REST_Request $request REST API Request object
192 * @param string $key REST API Request key being validated (in this case currency)
193 *
194 * @return bool
195 */
196 public function validateCurrency( $param, $request, $key ) {
197 return in_array( $param, array_keys( give_get_currencies_list() ) );
198 }
199
200 /**
201 * Sanitize test mode parameter
202 * Uses filter_var to cast string to variable
203 *
204 * @param string $param Validated test mode parameter provided in REST API request
205 * @param WP_REST_Request $request REST API Request object
206 * @param string $key REST API Request key being validated (in this case test mode)
207 */
208 public function sanitizeTestMode( $param, $request, $key ) {
209 return filter_var( $param, FILTER_VALIDATE_BOOLEAN );
210 }
211
212 /**
213 * Check permissions
214 *
215 * @param WP_REST_Request $request Current request.
216 *
217 * @return bool|WP_Error
218 */
219 public function permissionsCheck( $request ) {
220 if ( ! current_user_can( 'read' ) ) {
221 return new WP_Error(
222 'rest_forbidden',
223 esc_html__( 'You cannot view the reports resource.', 'give' ),
224 [ 'status' => $this->authorizationStatusCode() ]
225 );
226 }
227
228 return true;
229 }
230
231 /**
232 * Get report callback
233 *
234 * @param WP_REST_Request $request Current request.
235 *
236 * @return array
237 */
238 public function getReport( $request ) {
239 return [
240 'data' => [
241 'labels' => [ 'a', 'b', 'c' ],
242 'data' => [ '1', '4', '3' ],
243 ],
244 ];
245 }
246
247 /**
248 * Get our sample schema for a report
249 */
250 public function getReportSchema() {
251
252 if ( $this->schema ) {
253 // Since WordPress 5.3, the schema can be cached in the $schema property.
254 return $this->schema;
255 }
256
257 $this->schema = [
258 // This tells the spec of JSON Schema we are using which is draft 4.
259 '$schema' => 'http://json-schema.org/draft-04/schema#',
260 // The title property marks the identity of the resource.
261 'title' => 'report',
262 'type' => 'object',
263 // In JSON Schema you can specify object properties in the properties attribute.
264 'properties' => [
265 'data' => [
266 'description' => esc_html__( 'The data for the report.', 'give' ),
267 'type' => 'object',
268 ],
269 ],
270 ];
271
272 return $this->schema;
273 }
274
275 // Sets up the proper HTTP status code for authorization.
276 public function authorizationStatusCode() {
277
278 $status = 401;
279 if ( is_user_logged_in() ) {
280 $status = 403;
281 }
282
283 return $status;
284
285 }
286
287 /**
288 * Get cached report
289 *
290 * @param WP_REST_Request $request Current request.
291 *
292 * @return mixed
293 */
294 public function getCachedReport( $request ) {
295 $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $request->get_params() );
296
297 $cached = Give_Cache::get_db_query( $cache_key );
298
299 if ( $cached ) {
300 return $cached;
301 }
302
303 return null;
304
305 }
306
307 /**
308 * Cache report
309 *
310 * @param WP_REST_Request $request Current request.
311 * @param array $report
312 *
313 * @return bool
314 */
315 public function cacheReport( $request, $report ) {
316 $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $request->get_params() );
317
318 return Give_Cache::set_db_query( $cache_key, $report );
319
320 }
321
322 /**
323 * Cache report
324 *
325 * @param array $args Query arguments.
326 * @param Give_Payment[] $payments Payments.
327 *
328 * @return bool
329 */
330 private function cachePayments( $args, $payments ) {
331 $cache_key = Give_Cache::get_key( 'api_report_payments', $args );
332
333 return Give_Cache::set_db_query( $cache_key, $payments );
334
335 }
336
337 /**
338 * Get cached report
339 *
340 * @param array $args Query arguments.
341 *
342 * @return mixed
343 */
344 private function getCachedPayments( $args ) {
345
346 $cache_key = Give_Cache::get_key( 'api_report_payments', $args );
347
348 $cached = Give_Cache::get_db_query( $cache_key );
349
350 if ( $cached ) {
351 return $cached;
352 }
353
354 return null;
355
356 }
357
358
359 /**
360 * Get payment.
361 *
362 * @param string $startStr
363 * @param string $endStr
364 * @param string $orderBy
365 * @param int $number
366 *
367 * @return mixed
368 */
369 public function getPayments( $startStr, $endStr, $orderBy = 'date', $number = - 1 ) {
370
371 $gatewayObjects = give_get_payment_gateways();
372 $paymentModeKeyCompare = '!=';
373
374 if ( $this->testMode === false ) {
375 unset( $gatewayObjects['manual'] );
376 $paymentModeKeyCompare = '=';
377 }
378
379 $gateway = array_keys( $gatewayObjects );
380
381 $args = [
382 'post_status' => [
383 'publish',
384 'give_subscription',
385 ],
386 'number' => $number,
387 'paged' => 1,
388 'orderby' => $orderBy,
389 'order' => 'DESC',
390 'start_date' => strtotime( $startStr ),
391 'end_date' => strtotime( $endStr ),
392 'gateway' => $gateway,
393 'meta_query' => [
394 [
395 'key' => '_give_payment_currency',
396 'value' => $this->currency,
397 'compare' => '=',
398 ],
399 [
400 'key' => '_give_payment_mode',
401 'value' => 'live',
402 'compare' => $paymentModeKeyCompare,
403 ],
404 ],
405 ];
406
407 // Check if a cached payments exists
408 $cached_payments = $this->getCachedPayments( $args );
409
410 if ( $cached_payments !== null ) {
411 // Bail and return the cached payments
412 return $cached_payments;
413 }
414
415 $payments = new \Give_Payments_Query( $args );
416 $payments = $payments->get_payments();
417
418 // Cache the report data
419 $this->cachePayments( $args, $payments );
420
421 return $payments;
422
423 }
424
425 public function getGiveStatus() {
426
427 $donations = get_posts(
428 [
429 'post_type' => [ 'give_payment' ],
430 'post_status' => 'publish',
431 'numberposts' => 1,
432 ]
433 );
434
435 if ( count( $donations ) > 0 ) {
436 return 'donations_found';
437 }
438
439 return 'no_donations_found';
440 }
441 }
442