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