AverageDonation.php
4 years ago
DonationsVsIncome.php
4 years ago
Endpoint.php
4 years ago
FormPerformance.php
4 years ago
Income.php
4 years ago
IncomeBreakdown.php
4 years ago
PaymentMethods.php
4 years ago
PaymentStatuses.php
4 years ago
RecentDonations.php
4 years ago
TopDonors.php
4 years ago
TotalDonors.php
4 years ago
TotalIncome.php
4 years ago
TotalRefunds.php
4 years ago
Endpoint.php
448 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 | /** |
| 23 | * @since 2.6.1 |
| 24 | * @var WP_REST_Request |
| 25 | */ |
| 26 | protected $request; |
| 27 | |
| 28 | /** |
| 29 | * @var DateTime |
| 30 | */ |
| 31 | protected $startDate; |
| 32 | |
| 33 | /** |
| 34 | * @var DateTime |
| 35 | */ |
| 36 | protected $endDate; |
| 37 | |
| 38 | /** |
| 39 | * @var DateInterval |
| 40 | */ |
| 41 | protected $dateDiff; |
| 42 | |
| 43 | /** |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $endpoint; |
| 47 | |
| 48 | /** |
| 49 | * @var boolean |
| 50 | */ |
| 51 | protected $testMode; |
| 52 | |
| 53 | /** |
| 54 | * @var string |
| 55 | */ |
| 56 | protected $currency; |
| 57 | |
| 58 | /** |
| 59 | * @var array |
| 60 | */ |
| 61 | protected $schema; |
| 62 | |
| 63 | /** |
| 64 | * @inheritDoc |
| 65 | */ |
| 66 | public function registerRoute() |
| 67 | { |
| 68 | register_rest_route( |
| 69 | 'give-api/v2', |
| 70 | '/reports/' . $this->endpoint, |
| 71 | [ |
| 72 | // Here we register the readable endpoint |
| 73 | [ |
| 74 | 'methods' => 'GET', |
| 75 | 'callback' => [$this, 'handleRequest'], |
| 76 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 77 | 'args' => [ |
| 78 | 'start' => [ |
| 79 | 'type' => 'string', |
| 80 | 'required' => true, |
| 81 | 'validate_callback' => [$this, 'validateDate'], |
| 82 | 'sanitize_callback' => [$this, 'sanitizeDate'], |
| 83 | ], |
| 84 | 'end' => [ |
| 85 | 'type' => 'string', |
| 86 | 'required' => true, |
| 87 | 'validate_callback' => [$this, 'validateDate'], |
| 88 | 'sanitize_callback' => [$this, 'sanitizeDate'], |
| 89 | ], |
| 90 | 'currency' => [ |
| 91 | 'type' => 'string', |
| 92 | 'required' => true, |
| 93 | 'validate_callback' => [$this, 'validateCurrency'], |
| 94 | ], |
| 95 | 'testMode' => [ |
| 96 | 'type' => 'boolean', |
| 97 | 'required' => true, |
| 98 | 'sanitize_callback' => [$this, 'sanitizeTestMode'], |
| 99 | ], |
| 100 | ], |
| 101 | ], |
| 102 | // Register our schema callback. |
| 103 | 'schema' => [$this, 'getReportSchema'], |
| 104 | ] |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Handle rest request. |
| 110 | * |
| 111 | * @since 2.6.1 |
| 112 | * |
| 113 | * @param WP_REST_Request $request |
| 114 | * |
| 115 | * @return WP_REST_Response |
| 116 | */ |
| 117 | public function handleRequest($request) |
| 118 | { |
| 119 | // Check if a cached version exists |
| 120 | $cached_report = $this->getCachedReport($request); |
| 121 | if ($cached_report !== null) { |
| 122 | // Bail and return the cached version |
| 123 | return new WP_REST_Response($cached_report); |
| 124 | } |
| 125 | |
| 126 | $this->setupProperties($request); |
| 127 | |
| 128 | $responseData = [ |
| 129 | 'status' => $this->getGiveStatus(), |
| 130 | 'data' => $this->getReport($request), |
| 131 | ]; |
| 132 | |
| 133 | $this->cacheReport($request, $responseData); |
| 134 | |
| 135 | return new WP_REST_Response($responseData); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Setup properties |
| 140 | * |
| 141 | * @since 2.6.1 |
| 142 | * |
| 143 | * @param WP_REST_Request $request |
| 144 | */ |
| 145 | private function setupProperties($request) |
| 146 | { |
| 147 | $this->request = $request; |
| 148 | $this->startDate = date_create($request->get_param('start')); |
| 149 | $this->endDate = date_create($request->get_param('end')); |
| 150 | $this->currency = $request->get_param('currency'); |
| 151 | $this->testMode = $request->get_param('testMode'); |
| 152 | $this->dateDiff = date_diff($this->startDate, $this->endDate); |
| 153 | } |
| 154 | |
| 155 | public function validateDate($param, $request, $key) |
| 156 | { |
| 157 | // Check that date is valid, and formatted YYYY-MM-DD |
| 158 | $exploded = explode('-', $param); |
| 159 | $valid = checkdate($exploded[1], $exploded[2], $exploded[0]); |
| 160 | |
| 161 | // If checking end date, check that it is after start date |
| 162 | if ($key === 'end') { |
| 163 | $start = date_create($request->get_param('start')); |
| 164 | $end = date_create($request->get_param('end')); |
| 165 | $valid = $start <= $end ? $valid : false; |
| 166 | } |
| 167 | |
| 168 | return $valid; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @since 2.9.0 Restrict appended time to only the end date. |
| 173 | * @since 2.6.1 |
| 174 | */ |
| 175 | public function sanitizeDate($param, $request, $key) |
| 176 | { |
| 177 | // Return Date object from parameter |
| 178 | $exploded = explode('-', $param); |
| 179 | |
| 180 | $sanitizedDate = "{$exploded[0]}-{$exploded[1]}-{$exploded[2]}"; |
| 181 | |
| 182 | if ('end' === $key) { |
| 183 | /** |
| 184 | * For the end date manually specify an end time. |
| 185 | */ |
| 186 | $sanitizedDate .= ' 24:00:00'; |
| 187 | } |
| 188 | |
| 189 | return $sanitizedDate; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Validate currency string |
| 194 | * Check if currency code provided to REST APi is valid |
| 195 | * |
| 196 | * @param string $param Currency parameter provided in REST API request |
| 197 | * @param WP_REST_Request $request REST API Request object |
| 198 | * @param string $key REST API Request key being validated (in this case currency) |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | public function validateCurrency($param, $request, $key) |
| 203 | { |
| 204 | return in_array($param, array_keys(give_get_currencies_list())); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sanitize test mode parameter |
| 209 | * Uses filter_var to cast string to variable |
| 210 | * |
| 211 | * @param string $param Validated test mode parameter provided in REST API request |
| 212 | * @param WP_REST_Request $request REST API Request object |
| 213 | * @param string $key REST API Request key being validated (in this case test mode) |
| 214 | */ |
| 215 | public function sanitizeTestMode($param, $request, $key) |
| 216 | { |
| 217 | return filter_var($param, FILTER_VALIDATE_BOOLEAN); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Check permissions |
| 222 | * |
| 223 | * @param WP_REST_Request $request Current request. |
| 224 | * |
| 225 | * @return bool|WP_Error |
| 226 | */ |
| 227 | public function permissionsCheck($request) |
| 228 | { |
| 229 | if ( ! current_user_can('read')) { |
| 230 | return new WP_Error( |
| 231 | 'rest_forbidden', |
| 232 | esc_html__('You cannot view the reports resource.', 'give'), |
| 233 | ['status' => $this->authorizationStatusCode()] |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get report callback |
| 242 | * |
| 243 | * @param WP_REST_Request $request Current request. |
| 244 | * |
| 245 | * @return array |
| 246 | */ |
| 247 | public function getReport($request) |
| 248 | { |
| 249 | return [ |
| 250 | 'data' => [ |
| 251 | 'labels' => ['a', 'b', 'c'], |
| 252 | 'data' => ['1', '4', '3'], |
| 253 | ], |
| 254 | ]; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get our sample schema for a report |
| 259 | */ |
| 260 | public function getReportSchema() |
| 261 | { |
| 262 | if ($this->schema) { |
| 263 | // Since WordPress 5.3, the schema can be cached in the $schema property. |
| 264 | return $this->schema; |
| 265 | } |
| 266 | |
| 267 | $this->schema = [ |
| 268 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 269 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 270 | // The title property marks the identity of the resource. |
| 271 | 'title' => 'report', |
| 272 | 'type' => 'object', |
| 273 | // In JSON Schema you can specify object properties in the properties attribute. |
| 274 | 'properties' => [ |
| 275 | 'data' => [ |
| 276 | 'description' => esc_html__('The data for the report.', 'give'), |
| 277 | 'type' => 'object', |
| 278 | ], |
| 279 | ], |
| 280 | ]; |
| 281 | |
| 282 | return $this->schema; |
| 283 | } |
| 284 | |
| 285 | // Sets up the proper HTTP status code for authorization. |
| 286 | public function authorizationStatusCode() |
| 287 | { |
| 288 | $status = 401; |
| 289 | if (is_user_logged_in()) { |
| 290 | $status = 403; |
| 291 | } |
| 292 | |
| 293 | return $status; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get cached report |
| 298 | * |
| 299 | * @param WP_REST_Request $request Current request. |
| 300 | * |
| 301 | * @return mixed |
| 302 | */ |
| 303 | public function getCachedReport($request) |
| 304 | { |
| 305 | $cache_key = Give_Cache::get_key("api_get_report_{$this->endpoint}", $request->get_params()); |
| 306 | |
| 307 | $cached = Give_Cache::get_db_query($cache_key); |
| 308 | |
| 309 | if ($cached) { |
| 310 | return $cached; |
| 311 | } |
| 312 | |
| 313 | return null; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Cache report |
| 318 | * |
| 319 | * @param WP_REST_Request $request Current request. |
| 320 | * @param array $report |
| 321 | * |
| 322 | * @return bool |
| 323 | */ |
| 324 | public function cacheReport($request, $report) |
| 325 | { |
| 326 | $cache_key = Give_Cache::get_key("api_get_report_{$this->endpoint}", $request->get_params()); |
| 327 | |
| 328 | return Give_Cache::set_db_query($cache_key, $report); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Cache report |
| 333 | * |
| 334 | * @param array $args Query arguments. |
| 335 | * @param Give_Payment[] $payments Payments. |
| 336 | * |
| 337 | * @return bool |
| 338 | */ |
| 339 | private function cachePayments($args, $payments) |
| 340 | { |
| 341 | $cache_key = Give_Cache::get_key('api_report_payments', $args); |
| 342 | |
| 343 | return Give_Cache::set_db_query($cache_key, $payments); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Get cached report |
| 348 | * |
| 349 | * @param array $args Query arguments. |
| 350 | * |
| 351 | * @return mixed |
| 352 | */ |
| 353 | private function getCachedPayments($args) |
| 354 | { |
| 355 | $cache_key = Give_Cache::get_key('api_report_payments', $args); |
| 356 | |
| 357 | $cached = Give_Cache::get_db_query($cache_key); |
| 358 | |
| 359 | if ($cached) { |
| 360 | return $cached; |
| 361 | } |
| 362 | |
| 363 | return null; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Get payment. |
| 368 | * |
| 369 | * @param string $startStr |
| 370 | * @param string $endStr |
| 371 | * @param string $orderBy |
| 372 | * @param int $number |
| 373 | * |
| 374 | * @return mixed |
| 375 | */ |
| 376 | public function getPayments($startStr, $endStr, $orderBy = 'date', $number = -1) |
| 377 | { |
| 378 | $gatewayObjects = give_get_payment_gateways(); |
| 379 | $paymentModeKeyCompare = '!='; |
| 380 | |
| 381 | if ($this->testMode === false) { |
| 382 | unset($gatewayObjects['manual']); |
| 383 | $paymentModeKeyCompare = '='; |
| 384 | } |
| 385 | |
| 386 | $gateway = array_keys($gatewayObjects); |
| 387 | |
| 388 | $args = [ |
| 389 | 'post_status' => [ |
| 390 | 'publish', |
| 391 | 'give_subscription', |
| 392 | ], |
| 393 | 'number' => $number, |
| 394 | 'paged' => 1, |
| 395 | 'orderby' => $orderBy, |
| 396 | 'order' => 'DESC', |
| 397 | 'start_date' => strtotime($startStr), |
| 398 | 'end_date' => strtotime($endStr), |
| 399 | 'gateway' => $gateway, |
| 400 | 'meta_query' => [ |
| 401 | [ |
| 402 | 'key' => '_give_payment_currency', |
| 403 | 'value' => $this->currency, |
| 404 | 'compare' => '=', |
| 405 | ], |
| 406 | [ |
| 407 | 'key' => '_give_payment_mode', |
| 408 | 'value' => 'live', |
| 409 | 'compare' => $paymentModeKeyCompare, |
| 410 | ], |
| 411 | ], |
| 412 | ]; |
| 413 | |
| 414 | // Check if a cached payments exists |
| 415 | $cached_payments = $this->getCachedPayments($args); |
| 416 | |
| 417 | if ($cached_payments !== null) { |
| 418 | // Bail and return the cached payments |
| 419 | return $cached_payments; |
| 420 | } |
| 421 | |
| 422 | $payments = new \Give_Payments_Query($args); |
| 423 | $payments = $payments->get_payments(); |
| 424 | |
| 425 | // Cache the report data |
| 426 | $this->cachePayments($args, $payments); |
| 427 | |
| 428 | return $payments; |
| 429 | } |
| 430 | |
| 431 | public function getGiveStatus() |
| 432 | { |
| 433 | $donations = get_posts( |
| 434 | [ |
| 435 | 'post_type' => ['give_payment'], |
| 436 | 'post_status' => 'publish', |
| 437 | 'numberposts' => 1, |
| 438 | ] |
| 439 | ); |
| 440 | |
| 441 | if (count($donations) > 0) { |
| 442 | return 'donations_found'; |
| 443 | } |
| 444 | |
| 445 | return 'no_donations_found'; |
| 446 | } |
| 447 | } |
| 448 |