@@ -16,496 +16,496 @@ |
||
16 | 16 | */ |
17 | 17 | class GetPaid_REST_Date_Based_Controller extends GetPaid_REST_Controller { |
18 | 18 | |
19 | - /** |
|
20 | - * Group response items by day or month. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - public $groupby = 'day'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns an array with arguments to request the previous report. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - public $previous_range = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * The period interval. |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public $interval; |
|
39 | - |
|
40 | - /** |
|
41 | - * Retrieves the before and after dates. |
|
42 | - * |
|
43 | - * @param WP_REST_Request $request Request object. |
|
44 | - * @return array The appropriate date range. |
|
45 | - */ |
|
46 | - public function get_date_range( $request ) { |
|
47 | - |
|
48 | - // Check if the period is x_days. |
|
49 | - if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | - $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | - } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | - $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | - } else { |
|
54 | - $request['period'] = '7_days'; |
|
55 | - $date_range = $this->get_x_days_date_range(); |
|
56 | - } |
|
57 | - |
|
58 | - // 3 months max for day view. |
|
59 | - $before = strtotime( $date_range['before'] ); |
|
60 | - $after = strtotime( $date_range['after'] ); |
|
61 | - if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | - $this->groupby = 'month'; |
|
63 | - } |
|
64 | - |
|
65 | - $this->prepare_interval( $date_range ); |
|
66 | - |
|
67 | - return $date_range; |
|
68 | - |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Groups by month or days. |
|
73 | - * |
|
74 | - * @param array $range Date range. |
|
75 | - * @return array The appropriate date range. |
|
76 | - */ |
|
77 | - public function prepare_interval( $range ) { |
|
78 | - |
|
79 | - $before = strtotime( $range['before'] ); |
|
80 | - $after = strtotime( $range['after'] ); |
|
81 | - if ( 'day' === $this->groupby ) { |
|
82 | - $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | - $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | - return; |
|
85 | - } |
|
86 | - |
|
87 | - $this->interval = 0; |
|
88 | - $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | - |
|
90 | - while ( $min_date <= $before ) { |
|
91 | - $this->interval ++; |
|
92 | - $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | - } |
|
94 | - |
|
95 | - $this->interval = max( 1, $this->interval ); |
|
96 | - |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Retrieves a custom date range. |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Request object. |
|
103 | - * @return array The appropriate date range. |
|
104 | - */ |
|
105 | - public function get_custom_date_range( $request ) { |
|
106 | - |
|
107 | - $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | - $before = gmdate( 'Y-m-d' ); |
|
109 | - |
|
110 | - if ( ! empty( $request['before'] ) ) { |
|
111 | - $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | - } |
|
113 | - |
|
114 | - // Set the previous date range. |
|
115 | - $difference = $before - $after; |
|
116 | - $this->previous_range = array( |
|
117 | - 'period' => 'custom', |
|
118 | - 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | - 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | - ); |
|
121 | - |
|
122 | - // Generate the report. |
|
123 | - return array( |
|
124 | - 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | - 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | - ); |
|
127 | - |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Retrieves todays date range. |
|
132 | - * |
|
133 | - * @return array The appropriate date range. |
|
134 | - */ |
|
135 | - public function get_today_date_range() { |
|
136 | - |
|
137 | - // Set the previous date range. |
|
138 | - $this->previous_range = array( |
|
139 | - 'period' => 'yesterday', |
|
140 | - ); |
|
141 | - |
|
142 | - // Generate the report. |
|
143 | - return array( |
|
144 | - 'before' => gmdate( 'Y-m-d' ), |
|
145 | - 'after' => gmdate( 'Y-m-d' ), |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Retrieves yesterdays date range. |
|
152 | - * |
|
153 | - * @return array The appropriate date range. |
|
154 | - */ |
|
155 | - public function get_yesterday_date_range() { |
|
156 | - |
|
157 | - // Set the previous date range. |
|
158 | - $this->previous_range = array( |
|
159 | - 'period' => 'custom', |
|
160 | - 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | - 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | - ); |
|
163 | - |
|
164 | - // Generate the report. |
|
165 | - return array( |
|
166 | - 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | - 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | - ); |
|
169 | - |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Retrieves this week's date range. |
|
174 | - * |
|
175 | - * @return array The appropriate date range. |
|
176 | - */ |
|
177 | - public function get_week_date_range() { |
|
178 | - |
|
179 | - // Set the previous date range. |
|
180 | - $this->previous_range = array( |
|
181 | - 'period' => 'last_week', |
|
182 | - ); |
|
183 | - |
|
184 | - // Generate the report. |
|
185 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | - return array( |
|
187 | - 'before' => gmdate( 'Y-m-d' ), |
|
188 | - 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | - ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Retrieves last week's date range. |
|
194 | - * |
|
195 | - * @return array The appropriate date range. |
|
196 | - */ |
|
197 | - public function get_last_week_date_range() { |
|
198 | - |
|
199 | - $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | - $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | - $date_range = array( |
|
202 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | - ); |
|
205 | - |
|
206 | - // Set the previous date range. |
|
207 | - $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | - $this->previous_range = array( |
|
209 | - 'period' => 'custom', |
|
210 | - 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | - 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | - ); |
|
213 | - |
|
214 | - // Generate the report. |
|
215 | - return $date_range; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Retrieves last x days date range. |
|
220 | - * |
|
221 | - * @return array The appropriate date range. |
|
222 | - */ |
|
223 | - public function get_x_days_date_range( $days = 7 ) { |
|
224 | - |
|
225 | - $days--; |
|
226 | - |
|
227 | - $date_range = array( |
|
228 | - 'before' => gmdate( 'Y-m-d' ), |
|
229 | - 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | - ); |
|
231 | - |
|
232 | - $days++; |
|
233 | - |
|
234 | - // Set the previous date range. |
|
235 | - $this->previous_range = array( |
|
236 | - 'period' => 'custom', |
|
237 | - 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | - 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | - ); |
|
240 | - |
|
241 | - // Generate the report. |
|
242 | - return $date_range; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Retrieves this month date range. |
|
247 | - * |
|
248 | - * @return array The appropriate date range. |
|
249 | - */ |
|
250 | - public function get_month_date_range() { |
|
251 | - |
|
252 | - // Set the previous date range. |
|
253 | - $this->previous_range = array( |
|
254 | - 'period' => 'last_month', |
|
255 | - ); |
|
256 | - |
|
257 | - // Generate the report. |
|
258 | - return array( |
|
259 | - 'after' => gmdate( 'Y-m-01' ), |
|
260 | - 'before' => gmdate( 'Y-m-t' ), |
|
261 | - ); |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Retrieves last month's date range. |
|
267 | - * |
|
268 | - * @return array The appropriate date range. |
|
269 | - */ |
|
270 | - public function get_last_month_date_range() { |
|
271 | - |
|
272 | - // Set the previous date range. |
|
273 | - $this->previous_range = array( |
|
274 | - 'period' => 'custom', |
|
275 | - 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | - 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | - ); |
|
278 | - |
|
279 | - // Generate the report. |
|
280 | - return array( |
|
281 | - 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | - 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | - ); |
|
284 | - |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Retrieves this quarter date range. |
|
289 | - * |
|
290 | - * @return array The available quarters. |
|
291 | - */ |
|
292 | - public function get_quarters() { |
|
293 | - |
|
294 | - $year = (int) gmdate( 'Y' ); |
|
295 | - $last_year = (int) $year - 1; |
|
296 | - return array( |
|
297 | - |
|
298 | - // Third quarter of previous year: July 1st to September 30th |
|
299 | - array( |
|
300 | - 'before' => "{$last_year}-09-30", |
|
301 | - 'after' => "{$last_year}-07-01", |
|
302 | - ), |
|
303 | - |
|
304 | - // Last quarter of previous year: October 1st to December 31st |
|
305 | - array( |
|
306 | - 'before' => "{$last_year}-12-31", |
|
307 | - 'after' => "{$last_year}-10-01", |
|
308 | - ), |
|
309 | - |
|
310 | - // First quarter: January 1st to March 31st |
|
311 | - array( |
|
312 | - 'before' => "{$year}-03-31", |
|
313 | - 'after' => "{$year}-01-01", |
|
314 | - ), |
|
315 | - |
|
316 | - // Second quarter: April 1st to June 30th |
|
317 | - array( |
|
318 | - 'before' => "{$year}-06-30", |
|
319 | - 'after' => "{$year}-04-01", |
|
320 | - ), |
|
321 | - |
|
322 | - // Third quarter: July 1st to September 30th |
|
323 | - array( |
|
324 | - 'before' => "{$year}-09-30", |
|
325 | - 'after' => "{$year}-07-01", |
|
326 | - ), |
|
327 | - |
|
328 | - // Fourth quarter: October 1st to December 31st |
|
329 | - array( |
|
330 | - 'before' => "{$year}-12-31", |
|
331 | - 'after' => "{$year}-10-01", |
|
332 | - ), |
|
333 | - ); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Retrieves the current quater. |
|
338 | - * |
|
339 | - * @return int The current quarter. |
|
340 | - */ |
|
341 | - public function get_quarter() { |
|
342 | - |
|
343 | - $month = (int) gmdate( 'n' ); |
|
344 | - $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | - return $quarters[ $month - 1 ]; |
|
346 | - |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Retrieves this quarter date range. |
|
351 | - * |
|
352 | - * @return array The appropriate date range. |
|
353 | - */ |
|
354 | - public function get_quarter_date_range() { |
|
355 | - |
|
356 | - // Set the previous date range. |
|
357 | - $this->previous_range = array( |
|
358 | - 'period' => 'last_quarter', |
|
359 | - ); |
|
360 | - |
|
361 | - // Generate the report. |
|
362 | - $quarter = $this->get_quarter(); |
|
363 | - $quarters = $this->get_quarters(); |
|
364 | - return $quarters[ $quarter + 1 ]; |
|
365 | - |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Retrieves last quarter's date range. |
|
370 | - * |
|
371 | - * @return array The appropriate date range. |
|
372 | - */ |
|
373 | - public function get_last_quarter_date_range() { |
|
374 | - |
|
375 | - $quarters = $this->get_quarters(); |
|
376 | - $quarter = $this->get_quarter(); |
|
377 | - |
|
378 | - // Set the previous date range. |
|
379 | - $this->previous_range = array_merge( |
|
380 | - $quarters[ $quarter - 1 ], |
|
381 | - array( 'period' => 'custom' ) |
|
382 | - ); |
|
383 | - |
|
384 | - // Generate the report. |
|
385 | - return $quarters[ $quarter ]; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Retrieves this year date range. |
|
391 | - * |
|
392 | - * @return array The appropriate date range. |
|
393 | - */ |
|
394 | - public function get_year_date_range() { |
|
395 | - |
|
396 | - // Set the previous date range. |
|
397 | - $this->previous_range = array( |
|
398 | - 'period' => 'last_year', |
|
399 | - ); |
|
400 | - |
|
401 | - // Generate the report. |
|
402 | - return array( |
|
403 | - 'after' => gmdate( 'Y-01-01' ), |
|
404 | - 'before' => gmdate( 'Y-12-31' ), |
|
405 | - ); |
|
406 | - |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Retrieves last year date range. |
|
411 | - * |
|
412 | - * @return array The appropriate date range. |
|
413 | - */ |
|
414 | - public function get_last_year_date_range() { |
|
415 | - |
|
416 | - // Set the previous date range. |
|
417 | - $this->previous_range = array( |
|
418 | - 'period' => 'custom', |
|
419 | - 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | - 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | - ); |
|
422 | - |
|
423 | - // Generate the report. |
|
424 | - return array( |
|
425 | - 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | - 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | - ); |
|
428 | - |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Prepare a the request date for SQL usage. |
|
433 | - * |
|
434 | - * @param WP_REST_Request $request Request object. |
|
435 | - * @param string $date_field The date field. |
|
436 | - * @return string The appropriate SQL. |
|
437 | - */ |
|
438 | - public function get_date_range_sql( $request, $date_field ) { |
|
439 | - global $wpdb; |
|
440 | - |
|
441 | - $sql = '1=1'; |
|
442 | - $range = $this->get_date_range( $request ); |
|
443 | - |
|
444 | - if ( ! empty( $range['after'] ) ) { |
|
445 | - $sql .= ' AND ' . $wpdb->prepare( |
|
446 | - "$date_field >= %s", |
|
447 | - $range['after'] |
|
448 | - ); |
|
449 | - } |
|
450 | - |
|
451 | - if ( ! empty( $range['before'] ) ) { |
|
452 | - $sql .= ' AND ' . $wpdb->prepare( |
|
453 | - "$date_field <= %s", |
|
454 | - $range['before'] |
|
455 | - ); |
|
456 | - } |
|
457 | - |
|
458 | - return $sql; |
|
459 | - |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Prepares a group by query. |
|
464 | - * |
|
465 | - * @param string $date_field The date field. |
|
466 | - * @return string The appropriate SQL. |
|
467 | - */ |
|
468 | - public function get_group_by_sql( $date_field ) { |
|
469 | - |
|
470 | - if ( 'day' === $this->groupby ) { |
|
471 | - return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | - } |
|
473 | - |
|
474 | - return "YEAR($date_field), MONTH($date_field)"; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * Get the query params for collections. |
|
479 | - * |
|
480 | - * @return array |
|
481 | - */ |
|
482 | - public function get_collection_params() { |
|
483 | - return array( |
|
484 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | - 'period' => array( |
|
486 | - 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | - 'type' => 'string', |
|
488 | - 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | - 'validate_callback' => 'rest_validate_request_arg', |
|
490 | - 'sanitize_callback' => 'sanitize_text_field', |
|
491 | - 'default' => '7_days', |
|
492 | - ), |
|
493 | - 'after' => array( |
|
494 | - /* translators: %s: date format */ |
|
495 | - 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | - 'type' => 'string', |
|
497 | - 'validate_callback' => 'rest_validate_request_arg', |
|
498 | - 'sanitize_callback' => 'sanitize_text_field', |
|
499 | - 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | - ), |
|
501 | - 'before' => array( |
|
502 | - /* translators: %s: date format */ |
|
503 | - 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | - 'type' => 'string', |
|
505 | - 'validate_callback' => 'rest_validate_request_arg', |
|
506 | - 'sanitize_callback' => 'sanitize_text_field', |
|
507 | - 'default' => gmdate( 'Y-m-d' ), |
|
508 | - ), |
|
509 | - ); |
|
510 | - } |
|
19 | + /** |
|
20 | + * Group response items by day or month. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + public $groupby = 'day'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns an array with arguments to request the previous report. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + public $previous_range = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * The period interval. |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public $interval; |
|
39 | + |
|
40 | + /** |
|
41 | + * Retrieves the before and after dates. |
|
42 | + * |
|
43 | + * @param WP_REST_Request $request Request object. |
|
44 | + * @return array The appropriate date range. |
|
45 | + */ |
|
46 | + public function get_date_range( $request ) { |
|
47 | + |
|
48 | + // Check if the period is x_days. |
|
49 | + if ( preg_match( '/^(\d+)_days$/', $request['period'], $matches ) ) { |
|
50 | + $date_range = $this->get_x_days_date_range( absint( $matches[1] ) ); |
|
51 | + } elseif ( is_callable( array( $this, 'get_' . $request['period'] . '_date_range' ) ) ) { |
|
52 | + $date_range = call_user_func( array( $this, 'get_' . $request['period'] . '_date_range' ), $request ); |
|
53 | + } else { |
|
54 | + $request['period'] = '7_days'; |
|
55 | + $date_range = $this->get_x_days_date_range(); |
|
56 | + } |
|
57 | + |
|
58 | + // 3 months max for day view. |
|
59 | + $before = strtotime( $date_range['before'] ); |
|
60 | + $after = strtotime( $date_range['after'] ); |
|
61 | + if ( floor( ( $before - $after ) / MONTH_IN_SECONDS ) > 2 ) { |
|
62 | + $this->groupby = 'month'; |
|
63 | + } |
|
64 | + |
|
65 | + $this->prepare_interval( $date_range ); |
|
66 | + |
|
67 | + return $date_range; |
|
68 | + |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Groups by month or days. |
|
73 | + * |
|
74 | + * @param array $range Date range. |
|
75 | + * @return array The appropriate date range. |
|
76 | + */ |
|
77 | + public function prepare_interval( $range ) { |
|
78 | + |
|
79 | + $before = strtotime( $range['before'] ); |
|
80 | + $after = strtotime( $range['after'] ); |
|
81 | + if ( 'day' === $this->groupby ) { |
|
82 | + $difference = max( DAY_IN_SECONDS, ( DAY_IN_SECONDS + $before - $after ) ); // Prevent division by 0; |
|
83 | + $this->interval = absint( ceil( max( 1, $difference / DAY_IN_SECONDS ) ) ); |
|
84 | + return; |
|
85 | + } |
|
86 | + |
|
87 | + $this->interval = 0; |
|
88 | + $min_date = strtotime( gmdate( 'Y-m-01', $after ) ); |
|
89 | + |
|
90 | + while ( $min_date <= $before ) { |
|
91 | + $this->interval ++; |
|
92 | + $min_date = strtotime( '+1 MONTH', $min_date ); |
|
93 | + } |
|
94 | + |
|
95 | + $this->interval = max( 1, $this->interval ); |
|
96 | + |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Retrieves a custom date range. |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Request object. |
|
103 | + * @return array The appropriate date range. |
|
104 | + */ |
|
105 | + public function get_custom_date_range( $request ) { |
|
106 | + |
|
107 | + $after = max( strtotime( '-20 years' ), strtotime( sanitize_text_field( $request['after'] ) ) ); |
|
108 | + $before = gmdate( 'Y-m-d' ); |
|
109 | + |
|
110 | + if ( ! empty( $request['before'] ) ) { |
|
111 | + $before = min( strtotime( $before ), strtotime( sanitize_text_field( $request['before'] ) ) ); |
|
112 | + } |
|
113 | + |
|
114 | + // Set the previous date range. |
|
115 | + $difference = $before - $after; |
|
116 | + $this->previous_range = array( |
|
117 | + 'period' => 'custom', |
|
118 | + 'before' => gmdate( 'Y-m-d', $before - $difference - DAY_IN_SECONDS ), |
|
119 | + 'after' => gmdate( 'Y-m-d', $after - $difference - DAY_IN_SECONDS ), |
|
120 | + ); |
|
121 | + |
|
122 | + // Generate the report. |
|
123 | + return array( |
|
124 | + 'before' => gmdate( 'Y-m-d', $before ), |
|
125 | + 'after' => gmdate( 'Y-m-d', $after ), |
|
126 | + ); |
|
127 | + |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Retrieves todays date range. |
|
132 | + * |
|
133 | + * @return array The appropriate date range. |
|
134 | + */ |
|
135 | + public function get_today_date_range() { |
|
136 | + |
|
137 | + // Set the previous date range. |
|
138 | + $this->previous_range = array( |
|
139 | + 'period' => 'yesterday', |
|
140 | + ); |
|
141 | + |
|
142 | + // Generate the report. |
|
143 | + return array( |
|
144 | + 'before' => gmdate( 'Y-m-d' ), |
|
145 | + 'after' => gmdate( 'Y-m-d' ), |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Retrieves yesterdays date range. |
|
152 | + * |
|
153 | + * @return array The appropriate date range. |
|
154 | + */ |
|
155 | + public function get_yesterday_date_range() { |
|
156 | + |
|
157 | + // Set the previous date range. |
|
158 | + $this->previous_range = array( |
|
159 | + 'period' => 'custom', |
|
160 | + 'before' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
161 | + 'after' => gmdate( 'Y-m-d', strtotime( '-2 days' ) ), |
|
162 | + ); |
|
163 | + |
|
164 | + // Generate the report. |
|
165 | + return array( |
|
166 | + 'before' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
167 | + 'after' => gmdate( 'Y-m-d', strtotime( '-1 day' ) ), |
|
168 | + ); |
|
169 | + |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Retrieves this week's date range. |
|
174 | + * |
|
175 | + * @return array The appropriate date range. |
|
176 | + */ |
|
177 | + public function get_week_date_range() { |
|
178 | + |
|
179 | + // Set the previous date range. |
|
180 | + $this->previous_range = array( |
|
181 | + 'period' => 'last_week', |
|
182 | + ); |
|
183 | + |
|
184 | + // Generate the report. |
|
185 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
186 | + return array( |
|
187 | + 'before' => gmdate( 'Y-m-d' ), |
|
188 | + 'after' => gmdate( 'Y-m-d', strtotime( 'next Sunday -' . ( 7 - $week_starts ) . ' days' ) ), |
|
189 | + ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Retrieves last week's date range. |
|
194 | + * |
|
195 | + * @return array The appropriate date range. |
|
196 | + */ |
|
197 | + public function get_last_week_date_range() { |
|
198 | + |
|
199 | + $week_starts = absint( get_option( 'start_of_week' ) ); |
|
200 | + $week_starts = strtotime( 'last Sunday -' . ( 7 - $week_starts ) . ' days' ); |
|
201 | + $date_range = array( |
|
202 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
203 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
204 | + ); |
|
205 | + |
|
206 | + // Set the previous date range. |
|
207 | + $week_starts = $week_starts - 7 * DAY_IN_SECONDS; |
|
208 | + $this->previous_range = array( |
|
209 | + 'period' => 'custom', |
|
210 | + 'before' => gmdate( 'Y-m-d', $week_starts + 6 * DAY_IN_SECONDS ), |
|
211 | + 'after' => gmdate( 'Y-m-d', $week_starts ), |
|
212 | + ); |
|
213 | + |
|
214 | + // Generate the report. |
|
215 | + return $date_range; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Retrieves last x days date range. |
|
220 | + * |
|
221 | + * @return array The appropriate date range. |
|
222 | + */ |
|
223 | + public function get_x_days_date_range( $days = 7 ) { |
|
224 | + |
|
225 | + $days--; |
|
226 | + |
|
227 | + $date_range = array( |
|
228 | + 'before' => gmdate( 'Y-m-d' ), |
|
229 | + 'after' => gmdate( 'Y-m-d', strtotime( "-$days days" ) ), |
|
230 | + ); |
|
231 | + |
|
232 | + $days++; |
|
233 | + |
|
234 | + // Set the previous date range. |
|
235 | + $this->previous_range = array( |
|
236 | + 'period' => 'custom', |
|
237 | + 'before' => gmdate( 'Y-m-d', strtotime( $date_range['before'] ) - $days * DAY_IN_SECONDS ), |
|
238 | + 'after' => gmdate( 'Y-m-d', strtotime( $date_range['after'] ) - $days * DAY_IN_SECONDS ), |
|
239 | + ); |
|
240 | + |
|
241 | + // Generate the report. |
|
242 | + return $date_range; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Retrieves this month date range. |
|
247 | + * |
|
248 | + * @return array The appropriate date range. |
|
249 | + */ |
|
250 | + public function get_month_date_range() { |
|
251 | + |
|
252 | + // Set the previous date range. |
|
253 | + $this->previous_range = array( |
|
254 | + 'period' => 'last_month', |
|
255 | + ); |
|
256 | + |
|
257 | + // Generate the report. |
|
258 | + return array( |
|
259 | + 'after' => gmdate( 'Y-m-01' ), |
|
260 | + 'before' => gmdate( 'Y-m-t' ), |
|
261 | + ); |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Retrieves last month's date range. |
|
267 | + * |
|
268 | + * @return array The appropriate date range. |
|
269 | + */ |
|
270 | + public function get_last_month_date_range() { |
|
271 | + |
|
272 | + // Set the previous date range. |
|
273 | + $this->previous_range = array( |
|
274 | + 'period' => 'custom', |
|
275 | + 'after' => gmdate( 'Y-m-01', strtotime( '-2 months' ) ), |
|
276 | + 'before' => gmdate( 'Y-m-t', strtotime( '-2 months' ) ), |
|
277 | + ); |
|
278 | + |
|
279 | + // Generate the report. |
|
280 | + return array( |
|
281 | + 'after' => gmdate( 'Y-m-01', strtotime( 'last month' ) ), |
|
282 | + 'before' => gmdate( 'Y-m-t', strtotime( 'last month' ) ), |
|
283 | + ); |
|
284 | + |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Retrieves this quarter date range. |
|
289 | + * |
|
290 | + * @return array The available quarters. |
|
291 | + */ |
|
292 | + public function get_quarters() { |
|
293 | + |
|
294 | + $year = (int) gmdate( 'Y' ); |
|
295 | + $last_year = (int) $year - 1; |
|
296 | + return array( |
|
297 | + |
|
298 | + // Third quarter of previous year: July 1st to September 30th |
|
299 | + array( |
|
300 | + 'before' => "{$last_year}-09-30", |
|
301 | + 'after' => "{$last_year}-07-01", |
|
302 | + ), |
|
303 | + |
|
304 | + // Last quarter of previous year: October 1st to December 31st |
|
305 | + array( |
|
306 | + 'before' => "{$last_year}-12-31", |
|
307 | + 'after' => "{$last_year}-10-01", |
|
308 | + ), |
|
309 | + |
|
310 | + // First quarter: January 1st to March 31st |
|
311 | + array( |
|
312 | + 'before' => "{$year}-03-31", |
|
313 | + 'after' => "{$year}-01-01", |
|
314 | + ), |
|
315 | + |
|
316 | + // Second quarter: April 1st to June 30th |
|
317 | + array( |
|
318 | + 'before' => "{$year}-06-30", |
|
319 | + 'after' => "{$year}-04-01", |
|
320 | + ), |
|
321 | + |
|
322 | + // Third quarter: July 1st to September 30th |
|
323 | + array( |
|
324 | + 'before' => "{$year}-09-30", |
|
325 | + 'after' => "{$year}-07-01", |
|
326 | + ), |
|
327 | + |
|
328 | + // Fourth quarter: October 1st to December 31st |
|
329 | + array( |
|
330 | + 'before' => "{$year}-12-31", |
|
331 | + 'after' => "{$year}-10-01", |
|
332 | + ), |
|
333 | + ); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Retrieves the current quater. |
|
338 | + * |
|
339 | + * @return int The current quarter. |
|
340 | + */ |
|
341 | + public function get_quarter() { |
|
342 | + |
|
343 | + $month = (int) gmdate( 'n' ); |
|
344 | + $quarters = array( 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ); |
|
345 | + return $quarters[ $month - 1 ]; |
|
346 | + |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Retrieves this quarter date range. |
|
351 | + * |
|
352 | + * @return array The appropriate date range. |
|
353 | + */ |
|
354 | + public function get_quarter_date_range() { |
|
355 | + |
|
356 | + // Set the previous date range. |
|
357 | + $this->previous_range = array( |
|
358 | + 'period' => 'last_quarter', |
|
359 | + ); |
|
360 | + |
|
361 | + // Generate the report. |
|
362 | + $quarter = $this->get_quarter(); |
|
363 | + $quarters = $this->get_quarters(); |
|
364 | + return $quarters[ $quarter + 1 ]; |
|
365 | + |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Retrieves last quarter's date range. |
|
370 | + * |
|
371 | + * @return array The appropriate date range. |
|
372 | + */ |
|
373 | + public function get_last_quarter_date_range() { |
|
374 | + |
|
375 | + $quarters = $this->get_quarters(); |
|
376 | + $quarter = $this->get_quarter(); |
|
377 | + |
|
378 | + // Set the previous date range. |
|
379 | + $this->previous_range = array_merge( |
|
380 | + $quarters[ $quarter - 1 ], |
|
381 | + array( 'period' => 'custom' ) |
|
382 | + ); |
|
383 | + |
|
384 | + // Generate the report. |
|
385 | + return $quarters[ $quarter ]; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Retrieves this year date range. |
|
391 | + * |
|
392 | + * @return array The appropriate date range. |
|
393 | + */ |
|
394 | + public function get_year_date_range() { |
|
395 | + |
|
396 | + // Set the previous date range. |
|
397 | + $this->previous_range = array( |
|
398 | + 'period' => 'last_year', |
|
399 | + ); |
|
400 | + |
|
401 | + // Generate the report. |
|
402 | + return array( |
|
403 | + 'after' => gmdate( 'Y-01-01' ), |
|
404 | + 'before' => gmdate( 'Y-12-31' ), |
|
405 | + ); |
|
406 | + |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Retrieves last year date range. |
|
411 | + * |
|
412 | + * @return array The appropriate date range. |
|
413 | + */ |
|
414 | + public function get_last_year_date_range() { |
|
415 | + |
|
416 | + // Set the previous date range. |
|
417 | + $this->previous_range = array( |
|
418 | + 'period' => 'custom', |
|
419 | + 'after' => gmdate( 'Y-01-01', strtotime( '-2 years' ) ), |
|
420 | + 'before' => gmdate( 'Y-12-31', strtotime( '-2 years' ) ), |
|
421 | + ); |
|
422 | + |
|
423 | + // Generate the report. |
|
424 | + return array( |
|
425 | + 'after' => gmdate( 'Y-01-01', strtotime( 'last year' ) ), |
|
426 | + 'before' => gmdate( 'Y-12-31', strtotime( 'last year' ) ), |
|
427 | + ); |
|
428 | + |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Prepare a the request date for SQL usage. |
|
433 | + * |
|
434 | + * @param WP_REST_Request $request Request object. |
|
435 | + * @param string $date_field The date field. |
|
436 | + * @return string The appropriate SQL. |
|
437 | + */ |
|
438 | + public function get_date_range_sql( $request, $date_field ) { |
|
439 | + global $wpdb; |
|
440 | + |
|
441 | + $sql = '1=1'; |
|
442 | + $range = $this->get_date_range( $request ); |
|
443 | + |
|
444 | + if ( ! empty( $range['after'] ) ) { |
|
445 | + $sql .= ' AND ' . $wpdb->prepare( |
|
446 | + "$date_field >= %s", |
|
447 | + $range['after'] |
|
448 | + ); |
|
449 | + } |
|
450 | + |
|
451 | + if ( ! empty( $range['before'] ) ) { |
|
452 | + $sql .= ' AND ' . $wpdb->prepare( |
|
453 | + "$date_field <= %s", |
|
454 | + $range['before'] |
|
455 | + ); |
|
456 | + } |
|
457 | + |
|
458 | + return $sql; |
|
459 | + |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Prepares a group by query. |
|
464 | + * |
|
465 | + * @param string $date_field The date field. |
|
466 | + * @return string The appropriate SQL. |
|
467 | + */ |
|
468 | + public function get_group_by_sql( $date_field ) { |
|
469 | + |
|
470 | + if ( 'day' === $this->groupby ) { |
|
471 | + return "YEAR($date_field), MONTH($date_field), DAY($date_field)"; |
|
472 | + } |
|
473 | + |
|
474 | + return "YEAR($date_field), MONTH($date_field)"; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * Get the query params for collections. |
|
479 | + * |
|
480 | + * @return array |
|
481 | + */ |
|
482 | + public function get_collection_params() { |
|
483 | + return array( |
|
484 | + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
485 | + 'period' => array( |
|
486 | + 'description' => __( 'Limit to results of a specific period.', 'invoicing' ), |
|
487 | + 'type' => 'string', |
|
488 | + 'enum' => array( 'custom', 'today', 'yesterday', 'week', 'last_week', '7_days', '30_days', '60_days', '90_days', '180_days', 'month', 'last_month', 'quarter', 'last_quarter', 'year', 'last_year', 'quarter', 'last_quarter' ), |
|
489 | + 'validate_callback' => 'rest_validate_request_arg', |
|
490 | + 'sanitize_callback' => 'sanitize_text_field', |
|
491 | + 'default' => '7_days', |
|
492 | + ), |
|
493 | + 'after' => array( |
|
494 | + /* translators: %s: date format */ |
|
495 | + 'description' => sprintf( __( 'Limit to results after a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
496 | + 'type' => 'string', |
|
497 | + 'validate_callback' => 'rest_validate_request_arg', |
|
498 | + 'sanitize_callback' => 'sanitize_text_field', |
|
499 | + 'default' => gmdate( 'Y-m-d', strtotime( '-7 days' ) ), |
|
500 | + ), |
|
501 | + 'before' => array( |
|
502 | + /* translators: %s: date format */ |
|
503 | + 'description' => sprintf( __( 'Limit to results before a specific date, the date needs to be in the %s format.', 'invoicing' ), 'YYYY-MM-DD' ), |
|
504 | + 'type' => 'string', |
|
505 | + 'validate_callback' => 'rest_validate_request_arg', |
|
506 | + 'sanitize_callback' => 'sanitize_text_field', |
|
507 | + 'default' => gmdate( 'Y-m-d' ), |
|
508 | + ), |
|
509 | + ); |
|
510 | + } |
|
511 | 511 | } |
@@ -12,185 +12,185 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Checkout { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Payment_Form_Submission |
|
17 | - */ |
|
18 | - protected $payment_form_submission; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
24 | - */ |
|
25 | - public function __construct( $submission ) { |
|
26 | - $this->payment_form_submission = $submission; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Processes the checkout. |
|
31 | - * |
|
32 | - */ |
|
33 | - public function process_checkout() { |
|
34 | - |
|
35 | - // Validate the submission. |
|
36 | - $this->validate_submission(); |
|
37 | - |
|
38 | - // Prepare the invoice. |
|
39 | - $items = $this->get_submission_items(); |
|
40 | - $invoice = $this->get_submission_invoice(); |
|
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | - |
|
44 | - $this->prepare_billing_info( $invoice ); |
|
45 | - |
|
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | - |
|
48 | - // Save the invoice. |
|
49 | - $invoice->set_is_viewed( true ); |
|
50 | - $invoice->recalculate_total(); |
|
15 | + /** |
|
16 | + * @var GetPaid_Payment_Form_Submission |
|
17 | + */ |
|
18 | + protected $payment_form_submission; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
24 | + */ |
|
25 | + public function __construct( $submission ) { |
|
26 | + $this->payment_form_submission = $submission; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Processes the checkout. |
|
31 | + * |
|
32 | + */ |
|
33 | + public function process_checkout() { |
|
34 | + |
|
35 | + // Validate the submission. |
|
36 | + $this->validate_submission(); |
|
37 | + |
|
38 | + // Prepare the invoice. |
|
39 | + $items = $this->get_submission_items(); |
|
40 | + $invoice = $this->get_submission_invoice(); |
|
41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | + |
|
44 | + $this->prepare_billing_info( $invoice ); |
|
45 | + |
|
46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | + |
|
48 | + // Save the invoice. |
|
49 | + $invoice->set_is_viewed( true ); |
|
50 | + $invoice->recalculate_total(); |
|
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
54 | 54 | |
55 | - // Send to the gateway. |
|
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | - } |
|
55 | + // Send to the gateway. |
|
56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Validates the submission. |
|
61 | - * |
|
62 | - */ |
|
63 | - protected function validate_submission() { |
|
59 | + /** |
|
60 | + * Validates the submission. |
|
61 | + * |
|
62 | + */ |
|
63 | + protected function validate_submission() { |
|
64 | 64 | |
65 | - $submission = $this->payment_form_submission; |
|
66 | - $data = $submission->get_data(); |
|
65 | + $submission = $this->payment_form_submission; |
|
66 | + $data = $submission->get_data(); |
|
67 | 67 | |
68 | - // Do we have an error? |
|
68 | + // Do we have an error? |
|
69 | 69 | if ( ! empty( $submission->last_error ) ) { |
70 | - wp_send_json_error( $submission->last_error ); |
|
70 | + wp_send_json_error( $submission->last_error ); |
|
71 | 71 | } |
72 | 72 | |
73 | - // We need a billing email. |
|
73 | + // We need a billing email. |
|
74 | 74 | if ( ! $submission->has_billing_email() ) { |
75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | - } |
|
78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | + } |
|
82 | 82 | |
83 | - // Ensure the gateway is active. |
|
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | - } |
|
83 | + // Ensure the gateway is active. |
|
84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | + wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | + } |
|
87 | 87 | |
88 | - // Clear any existing errors. |
|
89 | - wpinv_clear_errors(); |
|
88 | + // Clear any existing errors. |
|
89 | + wpinv_clear_errors(); |
|
90 | 90 | |
91 | - // Allow themes and plugins to hook to errors |
|
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
91 | + // Allow themes and plugins to hook to errors |
|
92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
93 | 93 | |
94 | - // Do we have any errors? |
|
94 | + // Do we have any errors? |
|
95 | 95 | if ( wpinv_get_errors() ) { |
96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
97 | - } |
|
97 | + } |
|
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Retrieves submission items. |
|
103 | - * |
|
104 | - * @return GetPaid_Form_Item[] |
|
105 | - */ |
|
106 | - protected function get_submission_items() { |
|
101 | + /** |
|
102 | + * Retrieves submission items. |
|
103 | + * |
|
104 | + * @return GetPaid_Form_Item[] |
|
105 | + */ |
|
106 | + protected function get_submission_items() { |
|
107 | 107 | |
108 | - $items = $this->payment_form_submission->get_items(); |
|
108 | + $items = $this->payment_form_submission->get_items(); |
|
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
113 | - } |
|
114 | - |
|
115 | - return $items; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Retrieves submission invoice. |
|
120 | - * |
|
121 | - * @return WPInv_Invoice |
|
122 | - */ |
|
123 | - protected function get_submission_invoice() { |
|
124 | - $submission = $this->payment_form_submission; |
|
125 | - |
|
126 | - if ( ! $submission->has_invoice() ) { |
|
127 | - $invoice = new WPInv_Invoice(); |
|
128 | - $invoice->set_created_via( 'payment_form' ); |
|
129 | - return $invoice; |
|
130 | 113 | } |
131 | 114 | |
132 | - $invoice = $submission->get_invoice(); |
|
115 | + return $items; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Retrieves submission invoice. |
|
120 | + * |
|
121 | + * @return WPInv_Invoice |
|
122 | + */ |
|
123 | + protected function get_submission_invoice() { |
|
124 | + $submission = $this->payment_form_submission; |
|
133 | 125 | |
134 | - // Make sure that it is neither paid or refunded. |
|
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | - } |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->set_created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
138 | 131 | |
139 | - return $invoice; |
|
140 | - } |
|
132 | + $invoice = $submission->get_invoice(); |
|
141 | 133 | |
142 | - /** |
|
143 | - * Processes the submission invoice. |
|
144 | - * |
|
145 | - * @param WPInv_Invoice $invoice |
|
146 | - * @param GetPaid_Form_Item[] $items |
|
147 | - * @return WPInv_Invoice |
|
148 | - */ |
|
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
134 | + // Make sure that it is neither paid or refunded. |
|
135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | + } |
|
150 | 138 | |
151 | - $submission = $this->payment_form_submission; |
|
139 | + return $invoice; |
|
140 | + } |
|
152 | 141 | |
153 | - // Set-up the invoice details. |
|
154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | - $invoice->set_submission_id( $submission->id ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
142 | + /** |
|
143 | + * Processes the submission invoice. |
|
144 | + * |
|
145 | + * @param WPInv_Invoice $invoice |
|
146 | + * @param GetPaid_Form_Item[] $items |
|
147 | + * @return WPInv_Invoice |
|
148 | + */ |
|
149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
150 | + |
|
151 | + $submission = $this->payment_form_submission; |
|
152 | + |
|
153 | + // Set-up the invoice details. |
|
154 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
155 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
156 | + $invoice->set_submission_id( $submission->id ); |
|
157 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | 158 | $invoice->set_items( $items ); |
159 | 159 | $invoice->set_fees( $submission->get_fees() ); |
160 | 160 | $invoice->set_taxes( $submission->get_taxes() ); |
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | - $invoice->set_currency( $submission->get_currency() ); |
|
161 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | + $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
163 | + $invoice->set_currency( $submission->get_currency() ); |
|
164 | 164 | |
165 | - if ( $submission->has_shipping() ) { |
|
166 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | - } |
|
165 | + if ( $submission->has_shipping() ) { |
|
166 | + $invoice->set_shipping( $submission->get_shipping() ); |
|
167 | + } |
|
168 | 168 | |
169 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
169 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
170 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
171 | 171 | |
172 | - if ( $submission->has_discount_code() ) { |
|
172 | + if ( $submission->has_discount_code() ) { |
|
173 | 173 | $invoice->set_discount_code( $submission->get_discount_code() ); |
174 | - } |
|
175 | - |
|
176 | - getpaid_maybe_add_default_address( $invoice ); |
|
177 | - return $invoice; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Retrieves the submission's customer. |
|
182 | - * |
|
183 | - * @return int The customer id. |
|
184 | - */ |
|
185 | - protected function get_submission_customer() { |
|
186 | - $submission = $this->payment_form_submission; |
|
187 | - |
|
188 | - // If this is an existing invoice... |
|
189 | - if ( $submission->has_invoice() ) { |
|
190 | - return $submission->get_invoice()->get_user_id(); |
|
191 | - } |
|
192 | - |
|
193 | - // (Maybe) create the user. |
|
174 | + } |
|
175 | + |
|
176 | + getpaid_maybe_add_default_address( $invoice ); |
|
177 | + return $invoice; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Retrieves the submission's customer. |
|
182 | + * |
|
183 | + * @return int The customer id. |
|
184 | + */ |
|
185 | + protected function get_submission_customer() { |
|
186 | + $submission = $this->payment_form_submission; |
|
187 | + |
|
188 | + // If this is an existing invoice... |
|
189 | + if ( $submission->has_invoice() ) { |
|
190 | + return $submission->get_invoice()->get_user_id(); |
|
191 | + } |
|
192 | + |
|
193 | + // (Maybe) create the user. |
|
194 | 194 | $user = get_current_user_id(); |
195 | 195 | |
196 | 196 | if ( empty( $user ) ) { |
@@ -198,16 +198,16 @@ discard block |
||
198 | 198 | } |
199 | 199 | |
200 | 200 | if ( empty( $user ) ) { |
201 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | - $name = implode( '', array_filter( $name ) ); |
|
201 | + $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
202 | + $name = implode( '', array_filter( $name ) ); |
|
203 | 203 | $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
204 | 204 | |
205 | - // (Maybe) send new user notification. |
|
206 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | - wp_send_new_user_notifications( $user, 'user' ); |
|
209 | - } |
|
210 | - } |
|
205 | + // (Maybe) send new user notification. |
|
206 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
207 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
208 | + wp_send_new_user_notifications( $user, 'user' ); |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | 212 | if ( is_wp_error( $user ) ) { |
213 | 213 | wp_send_json_error( $user->get_error_message() ); |
@@ -215,49 +215,49 @@ discard block |
||
215 | 215 | |
216 | 216 | if ( is_numeric( $user ) ) { |
217 | 217 | return $user; |
218 | - } |
|
218 | + } |
|
219 | 219 | |
220 | - return $user->ID; |
|
220 | + return $user->ID; |
|
221 | 221 | |
222 | - } |
|
222 | + } |
|
223 | 223 | |
224 | - /** |
|
224 | + /** |
|
225 | 225 | * Prepares submission data for saving to the database. |
226 | 226 | * |
227 | - * @return array |
|
227 | + * @return array |
|
228 | 228 | */ |
229 | 229 | public function prepare_submission_data_for_saving() { |
230 | 230 | |
231 | - $submission = $this->payment_form_submission; |
|
231 | + $submission = $this->payment_form_submission; |
|
232 | 232 | |
233 | - // Prepared submission details. |
|
233 | + // Prepared submission details. |
|
234 | 234 | $prepared = array( |
235 | - 'all' => array(), |
|
236 | - 'meta' => array(), |
|
237 | - ); |
|
235 | + 'all' => array(), |
|
236 | + 'meta' => array(), |
|
237 | + ); |
|
238 | 238 | |
239 | 239 | // Raw submission details. |
240 | - $data = $submission->get_data(); |
|
240 | + $data = $submission->get_data(); |
|
241 | 241 | |
242 | - // Loop through the submitted details. |
|
242 | + // Loop through the submitted details. |
|
243 | 243 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
244 | 244 | |
245 | - // Skip premade fields. |
|
245 | + // Skip premade fields. |
|
246 | 246 | if ( ! empty( $field['premade'] ) ) { |
247 | 247 | continue; |
248 | 248 | } |
249 | 249 | |
250 | - // Ensure address is provided. |
|
251 | - if ( 'address' === $field['type'] ) { |
|
250 | + // Ensure address is provided. |
|
251 | + if ( 'address' === $field['type'] ) { |
|
252 | 252 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
253 | 253 | |
254 | - foreach ( $field['fields'] as $address_field ) { |
|
254 | + foreach ( $field['fields'] as $address_field ) { |
|
255 | 255 | |
256 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | - } |
|
259 | - } |
|
260 | - } |
|
256 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
257 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
258 | + } |
|
259 | + } |
|
260 | + } |
|
261 | 261 | |
262 | 262 | // If it is required and not set, abort. |
263 | 263 | if ( ! $submission->is_required_field_set( $field ) ) { |
@@ -267,31 +267,31 @@ discard block |
||
267 | 267 | // Handle misc fields. |
268 | 268 | if ( isset( $data[ $field['id'] ] ) ) { |
269 | 269 | |
270 | - // Uploads. |
|
271 | - if ( 'file_upload' === $field['type'] ) { |
|
272 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
270 | + // Uploads. |
|
271 | + if ( 'file_upload' === $field['type'] ) { |
|
272 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
273 | 273 | |
274 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | - } |
|
274 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
275 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
276 | + } |
|
277 | 277 | |
278 | - $value = array(); |
|
278 | + $value = array(); |
|
279 | 279 | |
280 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | - $value[] = sprintf( |
|
282 | - '<a href="%s" target="_blank">%s</a>', |
|
283 | - esc_url_raw( $url ), |
|
284 | - esc_html( $name ) |
|
285 | - ); |
|
286 | - } |
|
280 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
281 | + $value[] = sprintf( |
|
282 | + '<a href="%s" target="_blank">%s</a>', |
|
283 | + esc_url_raw( $url ), |
|
284 | + esc_html( $name ) |
|
285 | + ); |
|
286 | + } |
|
287 | 287 | |
288 | - $value = implode( ' | ', $value ); |
|
288 | + $value = implode( ' | ', $value ); |
|
289 | 289 | |
290 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | - } else { |
|
293 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | - } |
|
290 | + } elseif ( 'checkbox' === $field['type'] ) { |
|
291 | + $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
292 | + } else { |
|
293 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
294 | + } |
|
295 | 295 | |
296 | 296 | $label = $field['id']; |
297 | 297 | |
@@ -299,192 +299,192 @@ discard block |
||
299 | 299 | $label = $field['label']; |
300 | 300 | } |
301 | 301 | |
302 | - if ( ! empty( $field['add_meta'] ) ) { |
|
303 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | - } |
|
305 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
302 | + if ( ! empty( $field['add_meta'] ) ) { |
|
303 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
304 | + } |
|
305 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
306 | 306 | |
307 | 307 | } |
308 | - } |
|
308 | + } |
|
309 | 309 | |
310 | - return $prepared; |
|
310 | + return $prepared; |
|
311 | 311 | |
312 | - } |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
314 | + /** |
|
315 | 315 | * Retrieves address details. |
316 | 316 | * |
317 | - * @return array |
|
318 | - * @param WPInv_Invoice $invoice |
|
319 | - * @param string $type |
|
317 | + * @return array |
|
318 | + * @param WPInv_Invoice $invoice |
|
319 | + * @param string $type |
|
320 | 320 | */ |
321 | 321 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
322 | 322 | |
323 | - $data = $this->payment_form_submission->get_data(); |
|
324 | - $type = sanitize_key( $type ); |
|
325 | - $address = array(); |
|
326 | - $prepared = array(); |
|
323 | + $data = $this->payment_form_submission->get_data(); |
|
324 | + $type = sanitize_key( $type ); |
|
325 | + $address = array(); |
|
326 | + $prepared = array(); |
|
327 | 327 | |
328 | - if ( ! empty( $data[ $type ] ) ) { |
|
329 | - $address = $data[ $type ]; |
|
330 | - } |
|
328 | + if ( ! empty( $data[ $type ] ) ) { |
|
329 | + $address = $data[ $type ]; |
|
330 | + } |
|
331 | 331 | |
332 | - // Clean address details. |
|
333 | - foreach ( $address as $key => $value ) { |
|
334 | - $key = sanitize_key( $key ); |
|
335 | - $key = str_replace( 'wpinv_', '', $key ); |
|
336 | - $value = wpinv_clean( $value ); |
|
337 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | - } |
|
332 | + // Clean address details. |
|
333 | + foreach ( $address as $key => $value ) { |
|
334 | + $key = sanitize_key( $key ); |
|
335 | + $key = str_replace( 'wpinv_', '', $key ); |
|
336 | + $value = wpinv_clean( $value ); |
|
337 | + $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
338 | + } |
|
339 | 339 | |
340 | - // Filter address details. |
|
341 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
340 | + // Filter address details. |
|
341 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
342 | 342 | |
343 | - // Remove non-whitelisted values. |
|
344 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
343 | + // Remove non-whitelisted values. |
|
344 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
345 | 345 | |
346 | - } |
|
346 | + } |
|
347 | 347 | |
348 | - /** |
|
348 | + /** |
|
349 | 349 | * Prepares the billing details. |
350 | 350 | * |
351 | - * @return array |
|
352 | - * @param WPInv_Invoice $invoice |
|
351 | + * @return array |
|
352 | + * @param WPInv_Invoice $invoice |
|
353 | 353 | */ |
354 | 354 | protected function prepare_billing_info( &$invoice ) { |
355 | 355 | |
356 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
357 | 357 | |
358 | - // Update the invoice with the billing details. |
|
359 | - $invoice->set_props( $billing_address ); |
|
358 | + // Update the invoice with the billing details. |
|
359 | + $invoice->set_props( $billing_address ); |
|
360 | 360 | |
361 | - } |
|
361 | + } |
|
362 | 362 | |
363 | - /** |
|
363 | + /** |
|
364 | 364 | * Prepares the shipping details. |
365 | 365 | * |
366 | - * @return array |
|
367 | - * @param WPInv_Invoice $invoice |
|
366 | + * @return array |
|
367 | + * @param WPInv_Invoice $invoice |
|
368 | 368 | */ |
369 | 369 | protected function prepare_shipping_info( $invoice ) { |
370 | 370 | |
371 | - $data = $this->payment_form_submission->get_data(); |
|
371 | + $data = $this->payment_form_submission->get_data(); |
|
372 | 372 | |
373 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | - } |
|
373 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
374 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
375 | + } |
|
376 | 376 | |
377 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
378 | 378 | |
379 | - } |
|
379 | + } |
|
380 | 380 | |
381 | - /** |
|
382 | - * Confirms the submission is valid and send users to the gateway. |
|
383 | - * |
|
384 | - * @param WPInv_Invoice $invoice |
|
385 | - * @param array $prepared_payment_form_data |
|
386 | - * @param array $shipping |
|
387 | - */ |
|
388 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
381 | + /** |
|
382 | + * Confirms the submission is valid and send users to the gateway. |
|
383 | + * |
|
384 | + * @param WPInv_Invoice $invoice |
|
385 | + * @param array $prepared_payment_form_data |
|
386 | + * @param array $shipping |
|
387 | + */ |
|
388 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
389 | 389 | |
390 | - // Ensure the invoice exists. |
|
390 | + // Ensure the invoice exists. |
|
391 | 391 | if ( ! $invoice->exists() ) { |
392 | 392 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
393 | 393 | } |
394 | 394 | |
395 | - // Save payment form data. |
|
396 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
395 | + // Save payment form data. |
|
396 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
397 | 397 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
398 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
398 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
399 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
400 | 400 | |
401 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | - } |
|
401 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
402 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
403 | + } |
|
404 | 404 | |
405 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | - } |
|
408 | - } |
|
405 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
406 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
407 | + } |
|
408 | + } |
|
409 | 409 | |
410 | - // Save payment form data. |
|
411 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
410 | + // Save payment form data. |
|
411 | + $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
412 | 412 | if ( ! empty( $shipping ) ) { |
413 | 413 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
414 | - } |
|
414 | + } |
|
415 | 415 | |
416 | - // Backwards compatibility. |
|
416 | + // Backwards compatibility. |
|
417 | 417 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
418 | 418 | |
419 | - try { |
|
420 | - $this->process_payment( $invoice ); |
|
421 | - } catch ( Exception $e ) { |
|
422 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | - } |
|
419 | + try { |
|
420 | + $this->process_payment( $invoice ); |
|
421 | + } catch ( Exception $e ) { |
|
422 | + wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
423 | + } |
|
424 | 424 | |
425 | 425 | // If we are here, there was an error. |
426 | - wpinv_send_back_to_checkout( $invoice ); |
|
426 | + wpinv_send_back_to_checkout( $invoice ); |
|
427 | 427 | |
428 | - } |
|
428 | + } |
|
429 | 429 | |
430 | - /** |
|
431 | - * Processes the actual payment. |
|
432 | - * |
|
433 | - * @param WPInv_Invoice $invoice |
|
434 | - */ |
|
435 | - protected function process_payment( $invoice ) { |
|
430 | + /** |
|
431 | + * Processes the actual payment. |
|
432 | + * |
|
433 | + * @param WPInv_Invoice $invoice |
|
434 | + */ |
|
435 | + protected function process_payment( $invoice ) { |
|
436 | 436 | |
437 | - // Clear any checkout errors. |
|
438 | - wpinv_clear_errors(); |
|
437 | + // Clear any checkout errors. |
|
438 | + wpinv_clear_errors(); |
|
439 | 439 | |
440 | - // No need to send free invoices to the gateway. |
|
441 | - if ( $invoice->is_free() ) { |
|
442 | - $this->process_free_payment( $invoice ); |
|
443 | - } |
|
440 | + // No need to send free invoices to the gateway. |
|
441 | + if ( $invoice->is_free() ) { |
|
442 | + $this->process_free_payment( $invoice ); |
|
443 | + } |
|
444 | 444 | |
445 | - $submission = $this->payment_form_submission; |
|
445 | + $submission = $this->payment_form_submission; |
|
446 | 446 | |
447 | - // Fires before sending to the gateway. |
|
448 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
447 | + // Fires before sending to the gateway. |
|
448 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
449 | 449 | |
450 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | - $submission_data = $submission->get_data(); |
|
452 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
450 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
451 | + $submission_data = $submission->get_data(); |
|
452 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
453 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
454 | 454 | |
455 | - // Validate the currency. |
|
456 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | - wpinv_set_error( 'invalid_currency' ); |
|
458 | - } |
|
455 | + // Validate the currency. |
|
456 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
457 | + wpinv_set_error( 'invalid_currency' ); |
|
458 | + } |
|
459 | 459 | |
460 | - // Check to see if we have any errors. |
|
461 | - if ( wpinv_get_errors() ) { |
|
462 | - wpinv_send_back_to_checkout( $invoice ); |
|
463 | - } |
|
460 | + // Check to see if we have any errors. |
|
461 | + if ( wpinv_get_errors() ) { |
|
462 | + wpinv_send_back_to_checkout( $invoice ); |
|
463 | + } |
|
464 | 464 | |
465 | - // Send info to the gateway for payment processing |
|
466 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
465 | + // Send info to the gateway for payment processing |
|
466 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
467 | 467 | |
468 | - // Backwards compatibility. |
|
469 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
468 | + // Backwards compatibility. |
|
469 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
470 | 470 | |
471 | - } |
|
471 | + } |
|
472 | 472 | |
473 | - /** |
|
474 | - * Marks the invoice as paid in case the checkout is free. |
|
475 | - * |
|
476 | - * @param WPInv_Invoice $invoice |
|
477 | - */ |
|
478 | - protected function process_free_payment( $invoice ) { |
|
473 | + /** |
|
474 | + * Marks the invoice as paid in case the checkout is free. |
|
475 | + * |
|
476 | + * @param WPInv_Invoice $invoice |
|
477 | + */ |
|
478 | + protected function process_free_payment( $invoice ) { |
|
479 | 479 | |
480 | - $invoice->set_gateway( 'none' ); |
|
481 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | - $invoice->mark_paid(); |
|
483 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
480 | + $invoice->set_gateway( 'none' ); |
|
481 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
482 | + $invoice->mark_paid(); |
|
483 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
484 | 484 | |
485 | - } |
|
485 | + } |
|
486 | 486 | |
487 | - /** |
|
487 | + /** |
|
488 | 488 | * Sends a redrect response to payment details. |
489 | 489 | * |
490 | 490 | */ |