PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.6.1
GiveWP – Donation Plugin and Fundraising Platform v2.6.1
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 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
345 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 public function init() {
47 add_action( 'rest_api_init', array( $this, 'register_route' ) );
48 }
49
50 // Register our routes.
51 public function register_route() {
52 register_rest_route(
53 'give-api/v2',
54 '/reports/' . $this->endpoint,
55 array(
56 // Here we register the readable endpoint
57 array(
58 'methods' => 'GET',
59 'callback' => array( $this, 'handle_request' ),
60 'permission_callback' => array( $this, 'permissions_check' ),
61 'args' => array(
62 'start' => array(
63 'type' => 'string',
64 'required' => true,
65 'validate_callback' => array( $this, 'validate_date' ),
66 'sanitize_callback' => array( $this, 'sanitize_date' ),
67 ),
68 'end' => array(
69 'type' => 'string',
70 'required' => true,
71 'validate_callback' => array( $this, 'validate_date' ),
72 'sanitize_callback' => array( $this, 'sanitize_date' ),
73 ),
74 ),
75 ),
76 // Register our schema callback.
77 'schema' => array( $this, 'get_report_schema' ),
78 )
79 );
80 }
81
82 /**
83 * Handle rest request.
84 *
85 * @since 2.6.1
86 * @param WP_REST_Request $request
87 *
88 * @return WP_REST_Response
89 */
90 public function handle_request( $request ) {
91 // Check if a cached version exists
92 $cached_report = $this->get_cached_report( $request );
93 if ( $cached_report !== null ) {
94 // Bail and return the cached version
95 return new WP_REST_Response( $cached_report );
96 }
97
98 $this->setupProperties( $request );
99
100 $responseData = array(
101 'status' => $this->get_give_status(),
102 'data' => $this->get_report( $request ),
103 );
104
105 $this->cache_report( $request, $responseData );
106
107 return new WP_REST_Response( $responseData );
108 }
109
110 /**
111 * Setup properties
112 *
113 * @since 2.6.1
114 * @param WP_REST_Request $request
115 */
116 private function setupProperties( $request ) {
117 $this->request = $request;
118 $this->startDate = date_create( $request->get_param( 'start' ) );
119 $this->endDate = date_create( $request->get_param( 'end' ) );
120 $this->dateDiff = date_diff( $this->startDate, $this->endDate );
121 }
122
123 public function validate_date( $param, $request, $key ) {
124 // Check that date is valid, and formatted YYYY-MM-DD
125 $exploded = explode( '-', $param );
126 $valid = checkdate( $exploded[1], $exploded[2], $exploded[0] );
127
128 // If checking end date, check that it is after start date
129 if ( $key === 'end' ) {
130 $start = date_create( $request->get_param( 'start' ) );
131 $end = date_create( $request->get_param( 'end' ) );
132 $valid = $start <= $end ? $valid : false;
133 }
134
135 return $valid;
136 }
137
138 public function sanitize_date( $param, $request, $key ) {
139 // Return Date object from parameter
140 $exploded = explode( '-', $param );
141 $date = "{$exploded[0]}-{$exploded[1]}-{$exploded[2]} 24:00:00";
142 return $date;
143 }
144
145 /**
146 * Check permissions
147 *
148 * @param WP_REST_Request $request Current request.
149 */
150 public function permissions_check( $request ) {
151 if ( ! current_user_can( 'read' ) ) {
152 return new \WP_Error(
153 'rest_forbidden',
154 esc_html__( 'You cannot view the reports resource.', 'give' ),
155 array(
156 'status' => $this->authorization_status_code(),
157 )
158 );
159 }
160 return true;
161 }
162
163 /**
164 * Get report callback
165 *
166 * @param WP_REST_Request $request Current request.
167 *
168 * @return array
169 */
170 public function get_report( $request ) {
171 return array(
172 'data' => array(
173 'labels' => array( 'a', 'b', 'c' ),
174 'data' => array( '1', '4', '3' ),
175 ),
176 );
177 }
178
179 /**
180 * Get our sample schema for a report
181 */
182 public function get_report_schema() {
183
184 if ( $this->schema ) {
185 // Since WordPress 5.3, the schema can be cached in the $schema property.
186 return $this->schema;
187 }
188
189 $this->schema = array(
190 // This tells the spec of JSON Schema we are using which is draft 4.
191 '$schema' => 'http://json-schema.org/draft-04/schema#',
192 // The title property marks the identity of the resource.
193 'title' => 'report',
194 'type' => 'object',
195 // In JSON Schema you can specify object properties in the properties attribute.
196 'properties' => array(
197 'data' => array(
198 'description' => esc_html__( 'The data for the report.', 'give' ),
199 'type' => 'object',
200 ),
201 ),
202 );
203
204 return $this->schema;
205 }
206
207 // Sets up the proper HTTP status code for authorization.
208 public function authorization_status_code() {
209
210 $status = 401;
211 if ( is_user_logged_in() ) {
212 $status = 403;
213 }
214
215 return $status;
216
217 }
218
219 /**
220 * Get cached report
221 *
222 * @param WP_REST_Request $request Current request.
223 *
224 * @return mixed
225 */
226 public function get_cached_report( $request ) {
227 $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $request->get_params() );
228
229 $cached = Give_Cache::get_db_query( $cache_key );
230
231 if ( $cached ) {
232 return $cached;
233 }
234
235 return null;
236
237 }
238
239 /**
240 * Cache report
241 *
242 * @param WP_REST_Request $request Current request.
243 * @param array $report
244 *
245 * @return bool
246 */
247 public function cache_report( $request, $report ) {
248 $cache_key = Give_Cache::get_key( "api_get_report_{$this->endpoint}", $request->get_params() );
249
250 return Give_Cache::set_db_query( $cache_key, $report );
251
252 }
253
254 /**
255 * Cache report
256 *
257 * @param array $args Query arguments.
258 * @param Give_Payment[] $payments Payments.
259 *
260 * @return bool
261 */
262 private function cache_payments( $args, $payments ) {
263 $cache_key = Give_Cache::get_key( 'api_report_payments', $args );
264
265 return Give_Cache::set_db_query( $cache_key, $payments );
266
267 }
268
269 /**
270 * Get cached report
271 *
272 * @param array $args Query arguments.
273 * @return mixed
274 */
275 private function get_cached_payments( $args ) {
276
277 $cache_key = Give_Cache::get_key( 'api_report_payments', $args );
278
279 $cached = Give_Cache::get_db_query( $cache_key );
280
281 if ( $cached ) {
282 return $cached;
283 }
284
285 return null;
286
287 }
288
289
290 /**
291 * Get payment.
292 *
293 * @param string $startStr
294 * @param string $endStr
295 * @param string $orderBy
296 * @param int $number
297 *
298 * @return mixed
299 */
300 public function get_payments( $startStr, $endStr, $orderBy = 'date', $number = -1 ) {
301 $args = array(
302 'number' => $number,
303 'paged' => 1,
304 'orderby' => $orderBy,
305 'order' => 'DESC',
306 'start_date' => $startStr,
307 'end_date' => $endStr,
308 );
309
310 // Check if a cached payments exists
311 $cached_payments = $this->get_cached_payments( $args );
312
313 if ( $cached_payments !== null ) {
314 // Bail and return the cached payments
315 return $cached_payments;
316 }
317
318 $payments = new \Give_Payments_Query( $args );
319 $payments = $payments->get_payments();
320
321 // Cache the report data
322 $this->cache_payments( $args, $payments );
323
324 return $payments;
325
326 }
327
328 public function get_give_status() {
329
330 $donations = get_posts(
331 array(
332 'post_type' => array( 'give_payment' ),
333 'post_status' => 'publish',
334 'numberposts' => 1,
335 )
336 );
337
338 if ( count( $donations ) > 0 ) {
339 return 'donations_found';
340 } else {
341 return 'no_donations_found';
342 }
343 }
344 }
345