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