PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.6.0
GiveWP – Donation Plugin and Fundraising Platform v2.6.0
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
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