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
295 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Reports base endpoint |
| 5 | * |
| 6 | * @package Give |
| 7 | */ |
| 8 | |
| 9 | namespace Give\API\Endpoints\Reports; |
| 10 | |
| 11 | use \Give_Cache; |
| 12 | |
| 13 | abstract class Endpoint { |
| 14 | |
| 15 | protected $endpoint; |
| 16 | |
| 17 | public function init() { |
| 18 | add_action( 'rest_api_init', array( $this, 'register_route' ) ); |
| 19 | } |
| 20 | |
| 21 | // Register our routes. |
| 22 | public function register_route() { |
| 23 | register_rest_route( |
| 24 | 'give-api/v2', |
| 25 | '/reports/' . $this->endpoint, |
| 26 | array( |
| 27 | // Here we register the readable endpoint |
| 28 | array( |
| 29 | 'methods' => 'GET', |
| 30 | 'callback' => array( $this, 'get_report' ), |
| 31 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 32 | 'args' => array( |
| 33 | 'start' => array( |
| 34 | 'type' => 'string', |
| 35 | 'required' => true, |
| 36 | 'validate_callback' => array( $this, 'validate_date' ), |
| 37 | 'sanitize_callback' => array( $this, 'sanitize_date' ), |
| 38 | ), |
| 39 | 'end' => array( |
| 40 | 'type' => 'string', |
| 41 | 'required' => true, |
| 42 | 'validate_callback' => array( $this, 'validate_date' ), |
| 43 | 'sanitize_callback' => array( $this, 'sanitize_date' ), |
| 44 | ), |
| 45 | ), |
| 46 | ), |
| 47 | // Register our schema callback. |
| 48 | 'schema' => array( $this, 'get_report_schema' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function validate_date( $param, $request, $key ) { |
| 54 | // Check that date is valid, and formatted YYYY-MM-DD |
| 55 | $exploded = explode( '-', $param ); |
| 56 | $valid = checkdate( $exploded[1], $exploded[2], $exploded[0] ); |
| 57 | |
| 58 | // If checking end date, check that it is after start date |
| 59 | if ( $key === 'end' ) { |
| 60 | $start = date_create( $request['start'] ); |
| 61 | $end = date_create( $request['end'] ); |
| 62 | $valid = $start <= $end ? $valid : false; |
| 63 | } |
| 64 | |
| 65 | return $valid; |
| 66 | } |
| 67 | |
| 68 | public function sanitize_date( $param, $request, $key ) { |
| 69 | // Return Date object from parameter |
| 70 | $exploded = explode( '-', $param ); |
| 71 | $date = "{$exploded[0]}-{$exploded[1]}-{$exploded[2]} 24:00:00"; |
| 72 | return $date; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check permissions |
| 77 | * |
| 78 | * @param WP_REST_Request $request Current request. |
| 79 | */ |
| 80 | public function permissions_check( $request ) { |
| 81 | if ( ! current_user_can( 'read' ) ) { |
| 82 | return new \WP_Error( |
| 83 | 'rest_forbidden', |
| 84 | esc_html__( 'You cannot view the reports resource.', 'give' ), |
| 85 | array( |
| 86 | 'status' => $this->authorization_status_code(), |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get report callback |
| 95 | * |
| 96 | * @param WP_REST_Request $request Current request. |
| 97 | */ |
| 98 | public function get_report( $request ) { |
| 99 | return new \WP_REST_Response( |
| 100 | array( |
| 101 | 'data' => array( |
| 102 | 'labels' => [ 'a', 'b', 'c' ], |
| 103 | 'data' => [ '1', '4', '3' ], |
| 104 | ), |
| 105 | ) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get our sample schema for a report |
| 111 | * |
| 112 | * @param WP_REST_Request $request Current request. |
| 113 | */ |
| 114 | public function get_report_schema( $request ) { |
| 115 | |
| 116 | if ( $this->schema ) { |
| 117 | // Since WordPress 5.3, the schema can be cached in the $schema property. |
| 118 | return $this->schema; |
| 119 | } |
| 120 | |
| 121 | $this->schema = array( |
| 122 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 123 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 124 | // The title property marks the identity of the resource. |
| 125 | 'title' => 'report', |
| 126 | 'type' => 'object', |
| 127 | // In JSON Schema you can specify object properties in the properties attribute. |
| 128 | 'properties' => array( |
| 129 | 'data' => array( |
| 130 | 'description' => esc_html__( 'The data for the report.', 'give' ), |
| 131 | 'type' => 'object', |
| 132 | ), |
| 133 | ), |
| 134 | ); |
| 135 | |
| 136 | return $this->schema; |
| 137 | } |
| 138 | |
| 139 | // Sets up the proper HTTP status code for authorization. |
| 140 | public function authorization_status_code() { |
| 141 | |
| 142 | $status = 401; |
| 143 | if ( is_user_logged_in() ) { |
| 144 | $status = 403; |
| 145 | } |
| 146 | |
| 147 | return $status; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get cached report |
| 153 | * |
| 154 | * @param WP_REST_Request $request Current request. |
| 155 | */ |
| 156 | public function get_cached_report( $request ) { |
| 157 | |
| 158 | $start = date_create( $request['start'] ); |
| 159 | $end = date_create(); |
| 160 | |
| 161 | // Do not get cached report for period less than a week |
| 162 | $diff = date_diff( $start, $end ); |
| 163 | if ( $diff->days < 2 ) { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | $query_args = [ |
| 168 | 'start' => $request['start'], |
| 169 | 'end' => $request['end'], |
| 170 | ]; |
| 171 | |
| 172 | $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $query_args ); |
| 173 | |
| 174 | $cached = Give_Cache::get_db_query( $cache_key ); |
| 175 | |
| 176 | if ( $cached ) { |
| 177 | return $cached; |
| 178 | } else { |
| 179 | return null; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Cache report |
| 185 | * |
| 186 | * @param WP_REST_Request $request Current request. |
| 187 | */ |
| 188 | public function cache_report( $request, $report ) { |
| 189 | |
| 190 | $query_args = [ |
| 191 | 'start' => $request['start'], |
| 192 | 'end' => $request['end'], |
| 193 | ]; |
| 194 | |
| 195 | $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $query_args ); |
| 196 | |
| 197 | $result = Give_Cache::set_db_query( $cache_key, $report ); |
| 198 | |
| 199 | return $result; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Cache report |
| 205 | * |
| 206 | * @param WP_REST_Request $request Current request. |
| 207 | */ |
| 208 | public function cache_payments( $startStr, $endStr, $orderBy, $number, $payments ) { |
| 209 | |
| 210 | $query_args = [ |
| 211 | 'start' => $startStr, |
| 212 | 'end' => $endStr, |
| 213 | 'orderby' => $orderBy, |
| 214 | 'number' => $number, |
| 215 | ]; |
| 216 | |
| 217 | $cache_key = Give_Cache::get_key( 'api_report_payments', $query_args ); |
| 218 | |
| 219 | $result = Give_Cache::set_db_query( $cache_key, $payments ); |
| 220 | |
| 221 | return $result; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get cached report |
| 227 | * |
| 228 | * @param WP_REST_Request $request Current request. |
| 229 | */ |
| 230 | public function get_cached_payments( $startStr, $endStr, $orderBy, $number ) { |
| 231 | |
| 232 | $query_args = [ |
| 233 | 'start' => $startStr, |
| 234 | 'end' => $endStr, |
| 235 | 'orderby' => $orderBy, |
| 236 | 'number' => $number, |
| 237 | ]; |
| 238 | |
| 239 | $cache_key = Give_Cache::get_key( 'api_report_payments', $query_args ); |
| 240 | |
| 241 | $cached = Give_Cache::get_db_query( $cache_key ); |
| 242 | |
| 243 | if ( $cached ) { |
| 244 | return $cached; |
| 245 | } else { |
| 246 | return null; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public function get_payments( $startStr, $endStr, $orderBy = 'date', $number = -1 ) { |
| 251 | |
| 252 | // Check if a cached payments exists |
| 253 | $cached_payments = $this->get_cached_payments( $startStr, $endStr, $orderBy, $number ); |
| 254 | if ( $cached_payments !== null ) { |
| 255 | // Bail and return the cached payments |
| 256 | return $cached_payments; |
| 257 | } |
| 258 | |
| 259 | $args = [ |
| 260 | 'number' => $number, |
| 261 | 'paged' => 1, |
| 262 | 'orderby' => $orderBy, |
| 263 | 'order' => 'DESC', |
| 264 | 'start_date' => $startStr, |
| 265 | 'end_date' => $endStr, |
| 266 | ]; |
| 267 | |
| 268 | $payments = new \Give_Payments_Query( $args ); |
| 269 | $payments = $payments->get_payments(); |
| 270 | |
| 271 | // Cache the report data |
| 272 | $result = $this->cache_payments( $startStr, $endStr, $orderBy, $number, $payments ); |
| 273 | |
| 274 | return $payments; |
| 275 | |
| 276 | } |
| 277 | |
| 278 | public function get_give_status() { |
| 279 | |
| 280 | $donations = get_posts( |
| 281 | [ |
| 282 | 'post_type' => array( 'give_payment' ), |
| 283 | 'post_status' => 'publish', |
| 284 | 'numberposts' => 1, |
| 285 | ] |
| 286 | ); |
| 287 | |
| 288 | if ( count( $donations ) > 0 ) { |
| 289 | return 'donations_found'; |
| 290 | } else { |
| 291 | return 'no_donations_found'; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 |