1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains the ajax handlers. |
4
|
|
|
* |
5
|
|
|
* @since 1.0.0 |
6
|
|
|
* @package Invoicing |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* WPInv_Ajax class. |
13
|
|
|
*/ |
14
|
|
|
class WPInv_Ajax { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Hook in ajax handlers. |
18
|
|
|
*/ |
19
|
|
|
public static function init() { |
20
|
|
|
add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 ); |
21
|
|
|
add_action( 'template_redirect', array( __CLASS__, 'do_wpinv_ajax' ), 0 ); |
22
|
|
|
self::add_ajax_events(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set GetPaid AJAX constant and headers. |
27
|
|
|
*/ |
28
|
|
|
public static function define_ajax() { |
29
|
|
|
|
30
|
|
|
if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
31
|
|
|
getpaid_maybe_define_constant( 'DOING_AJAX', true ); |
32
|
|
|
getpaid_maybe_define_constant( 'WPInv_DOING_AJAX', true ); |
33
|
|
|
if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) { |
34
|
|
|
/** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 ); |
35
|
|
|
} |
36
|
|
|
$GLOBALS['wpdb']->hide_errors(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Send headers for GetPaid Ajax Requests. |
43
|
|
|
* |
44
|
|
|
* @since 1.0.18 |
45
|
|
|
*/ |
46
|
|
|
private static function wpinv_ajax_headers() { |
47
|
|
|
if ( ! headers_sent() ) { |
48
|
|
|
send_origin_headers(); |
49
|
|
|
send_nosniff_header(); |
50
|
|
|
nocache_headers(); |
51
|
|
|
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
52
|
|
|
header( 'X-Robots-Tag: noindex' ); |
53
|
|
|
status_header( 200 ); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check for GetPaid Ajax request and fire action. |
59
|
|
|
*/ |
60
|
|
|
public static function do_wpinv_ajax() { |
61
|
|
|
global $wp_query; |
62
|
|
|
|
63
|
|
|
if ( ! empty( $_GET['wpinv-ajax'] ) ) { |
64
|
|
|
$wp_query->set( 'wpinv-ajax', sanitize_text_field( wp_unslash( $_GET['wpinv-ajax'] ) ) ); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$action = $wp_query->get( 'wpinv-ajax' ); |
68
|
|
|
|
69
|
|
|
if ( $action ) { |
70
|
|
|
self::wpinv_ajax_headers(); |
71
|
|
|
$action = sanitize_text_field( $action ); |
72
|
|
|
do_action( 'wpinv_ajax_' . $action ); |
73
|
|
|
wp_die(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Hook in ajax methods. |
80
|
|
|
*/ |
81
|
|
|
public static function add_ajax_events() { |
82
|
|
|
|
83
|
|
|
// array( 'event' => is_frontend ) |
84
|
|
|
$ajax_events = array( |
85
|
|
|
'add_note' => false, |
86
|
|
|
'delete_note' => false, |
87
|
|
|
'get_states_field' => true, |
88
|
|
|
'get_aui_states_field' => true, |
89
|
|
|
'payment_form' => true, |
90
|
|
|
'get_payment_form' => true, |
91
|
|
|
'get_payment_form_states_field' => true, |
92
|
|
|
'get_invoicing_items' => false, |
93
|
|
|
'get_invoice_items' => false, |
94
|
|
|
'add_invoice_items' => false, |
95
|
|
|
'edit_invoice_item' => false, |
96
|
|
|
'remove_invoice_item' => false, |
97
|
|
|
'get_billing_details' => false, |
98
|
|
|
'recalculate_invoice_totals' => false, |
99
|
|
|
'check_new_user_email' => false, |
100
|
|
|
'run_tool' => false, |
101
|
|
|
'payment_form_refresh_prices' => true, |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
foreach ( $ajax_events as $ajax_event => $nopriv ) { |
105
|
|
|
add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
106
|
|
|
add_action( 'wp_ajax_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
107
|
|
|
|
108
|
|
|
if ( $nopriv ) { |
109
|
|
|
add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
110
|
|
|
add_action( 'wp_ajax_nopriv_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
111
|
|
|
add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function add_note() { |
117
|
|
|
check_ajax_referer( 'add-invoice-note', '_nonce' ); |
118
|
|
|
|
119
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
120
|
|
|
die(-1); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$post_id = absint( $_POST['post_id'] ); |
124
|
|
|
$note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
125
|
|
|
$note_type = sanitize_text_field( $_POST['note_type'] ); |
126
|
|
|
|
127
|
|
|
$is_customer_note = $note_type == 'customer' ? 1 : 0; |
128
|
|
|
|
129
|
|
|
if ( $post_id > 0 ) { |
130
|
|
|
$note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
|
|
|
|
131
|
|
|
|
132
|
|
|
if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
133
|
|
|
wpinv_get_invoice_note_line_item( $note_id ); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
die(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function delete_note() { |
141
|
|
|
check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
142
|
|
|
|
143
|
|
|
if ( !wpinv_current_user_can_manage_invoicing() ) { |
144
|
|
|
die(-1); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$note_id = (int)$_POST['note_id']; |
148
|
|
|
|
149
|
|
|
if ( $note_id > 0 ) { |
150
|
|
|
wp_delete_comment( $note_id, true ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
die(); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public static function get_states_field() { |
157
|
|
|
echo wpinv_get_states_field(); |
158
|
|
|
|
159
|
|
|
die(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retrieves a given user's billing address. |
164
|
|
|
*/ |
165
|
|
|
public static function get_billing_details() { |
166
|
|
|
|
167
|
|
|
// Verify nonce. |
168
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
169
|
|
|
|
170
|
|
|
// Can the user manage the plugin? |
171
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
172
|
|
|
die(-1); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Do we have a user id? |
176
|
|
|
$user_id = $_GET['user_id']; |
177
|
|
|
|
178
|
|
|
if ( empty( $user_id ) || ! is_numeric( $user_id ) ) { |
179
|
|
|
die(-1); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Fetch the billing details. |
183
|
|
|
$billing_details = wpinv_get_user_address( $user_id ); |
184
|
|
|
$billing_details = apply_filters( 'wpinv_ajax_billing_details', $billing_details, $user_id ); |
185
|
|
|
|
186
|
|
|
// unset the user id and email. |
187
|
|
|
$to_ignore = array( 'user_id', 'email' ); |
188
|
|
|
|
189
|
|
|
foreach ( $to_ignore as $key ) { |
190
|
|
|
if ( isset( $billing_details[ $key ] ) ) { |
191
|
|
|
unset( $billing_details[ $key ] ); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
wp_send_json_success( $billing_details ); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Checks if a new users email is valid. |
201
|
|
|
*/ |
202
|
|
|
public static function check_new_user_email() { |
203
|
|
|
|
204
|
|
|
// Verify nonce. |
205
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
206
|
|
|
|
207
|
|
|
// Can the user manage the plugin? |
208
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
209
|
|
|
die(-1); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// We need an email address. |
213
|
|
|
if ( empty( $_GET['email'] ) ) { |
214
|
|
|
_e( "Provide the new user's email address", 'invoicing' ); |
215
|
|
|
exit; |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Ensure the email is valid. |
219
|
|
|
$email = sanitize_text_field( $_GET['email'] ); |
220
|
|
|
if ( ! is_email( $email ) ) { |
221
|
|
|
_e( 'Invalid email address', 'invoicing' ); |
222
|
|
|
exit; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// And it does not exist. |
226
|
|
|
if ( email_exists( $email ) ) { |
227
|
|
|
_e( 'A user with this email address already exists', 'invoicing' ); |
228
|
|
|
exit; |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
wp_send_json_success( true ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public static function run_tool() { |
235
|
|
|
check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
236
|
|
|
if ( !wpinv_current_user_can_manage_invoicing() ) { |
237
|
|
|
die(-1); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$tool = sanitize_text_field( $_POST['tool'] ); |
241
|
|
|
|
242
|
|
|
do_action( 'wpinv_run_tool' ); |
243
|
|
|
|
244
|
|
|
if ( !empty( $tool ) ) { |
245
|
|
|
do_action( 'wpinv_tool_' . $tool ); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Retrieves the markup for a payment form. |
251
|
|
|
*/ |
252
|
|
|
public static function get_payment_form() { |
253
|
|
|
|
254
|
|
|
// Check nonce. |
255
|
|
|
check_ajax_referer( 'getpaid_form_nonce' ); |
256
|
|
|
|
257
|
|
|
// Is the request set up correctly? |
258
|
|
|
if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) { |
259
|
|
|
echo aui()->alert( |
260
|
|
|
array( |
261
|
|
|
'type' => 'warning', |
262
|
|
|
'content' => __( 'No payment form or item provided', 'invoicing' ), |
263
|
|
|
) |
264
|
|
|
); |
265
|
|
|
exit; |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Payment form or button? |
269
|
|
|
if ( ! empty( $_GET['form'] ) ) { |
270
|
|
|
getpaid_display_payment_form( $_GET['form'] ); |
271
|
|
|
} else if( ! empty( $_GET['invoice'] ) ) { |
272
|
|
|
getpaid_display_invoice_payment_form( $_GET['invoice'] ); |
273
|
|
|
} else { |
274
|
|
|
$items = getpaid_convert_items_to_array( $_GET['item'] ); |
275
|
|
|
getpaid_display_item_payment_form( $items ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
exit; |
|
|
|
|
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Payment forms. |
284
|
|
|
* |
285
|
|
|
* @since 1.0.18 |
286
|
|
|
*/ |
287
|
|
|
public static function payment_form() { |
288
|
|
|
|
289
|
|
|
// Check nonce. |
290
|
|
|
check_ajax_referer( 'getpaid_form_nonce' ); |
291
|
|
|
|
292
|
|
|
// ... form fields... |
293
|
|
|
if ( empty( $_POST['getpaid_payment_form_submission'] ) ) { |
294
|
|
|
_e( 'Error: Reload the page and try again.', 'invoicing' ); |
295
|
|
|
exit; |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// Process the payment form. |
299
|
|
|
$checkout_class = apply_filters( 'getpaid_checkout_class', 'GetPaid_Checkout' ); |
300
|
|
|
$checkout = new $checkout_class( new GetPaid_Payment_Form_Submission() ); |
301
|
|
|
$checkout->process_checkout(); |
302
|
|
|
|
303
|
|
|
exit; |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Payment forms. |
308
|
|
|
* |
309
|
|
|
* @since 1.0.18 |
310
|
|
|
*/ |
311
|
|
|
public static function get_payment_form_states_field() { |
312
|
|
|
|
313
|
|
|
if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
314
|
|
|
exit; |
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$elements = getpaid_get_payment_form_elements( $_GET['form'] ); |
318
|
|
|
|
319
|
|
|
if ( empty( $elements ) ) { |
320
|
|
|
exit; |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$address_fields = array(); |
324
|
|
|
foreach ( $elements as $element ) { |
325
|
|
|
if ( 'address' === $element['type'] ) { |
326
|
|
|
$address_fields = $element; |
327
|
|
|
break; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
if ( empty( $address_fields ) ) { |
332
|
|
|
exit; |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
foreach ( $address_fields['fields'] as $address_field ) { |
336
|
|
|
|
337
|
|
|
if ( 'wpinv_state' == $address_field['name'] ) { |
338
|
|
|
|
339
|
|
|
$wrap_class = getpaid_get_form_element_grid_class( $address_field ); |
340
|
|
|
$wrap_class = esc_attr( "$wrap_class getpaid-address-field-wrapper" ); |
341
|
|
|
$placeholder = empty( $address_field['placeholder'] ) ? '' : esc_attr( $address_field['placeholder'] ); |
342
|
|
|
$description = empty( $address_field['description'] ) ? '' : wp_kses_post( $address_field['description'] ); |
343
|
|
|
$value = is_user_logged_in() ? get_user_meta( get_current_user_id(), '_wpinv_state', true ) : ''; |
344
|
|
|
$label = empty( $address_field['label'] ) ? '' : wp_kses_post( $address_field['label'] ); |
345
|
|
|
|
346
|
|
|
if ( ! empty( $address_field['required'] ) ) { |
347
|
|
|
$label .= "<span class='text-danger'> *</span>"; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$html = getpaid_get_states_select_markup ( |
351
|
|
|
sanitize_text_field( $_GET['country'] ), |
352
|
|
|
$value, |
353
|
|
|
$placeholder, |
354
|
|
|
$label, |
355
|
|
|
$description, |
356
|
|
|
! empty( $address_field['required'] ), |
357
|
|
|
$wrap_class |
358
|
|
|
); |
359
|
|
|
|
360
|
|
|
wp_send_json_success( $html ); |
361
|
|
|
exit; |
|
|
|
|
362
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
exit; |
|
|
|
|
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Recalculates invoice totals. |
372
|
|
|
*/ |
373
|
|
|
public static function recalculate_invoice_totals() { |
374
|
|
|
|
375
|
|
|
// Verify nonce. |
376
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
377
|
|
|
|
378
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
379
|
|
|
exit; |
|
|
|
|
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
// We need an invoice. |
383
|
|
|
if ( empty( $_POST['post_id'] ) ) { |
384
|
|
|
exit; |
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
// Fetch the invoice. |
388
|
|
|
$invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
389
|
|
|
|
390
|
|
|
// Ensure it exists. |
391
|
|
|
if ( ! $invoice->get_id() ) { |
392
|
|
|
exit; |
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
// Maybe set the country, state, currency. |
396
|
|
|
foreach ( array( 'country', 'state', 'currency' ) as $key ) { |
397
|
|
|
if ( isset( $_POST[ $key ] ) ) { |
398
|
|
|
$method = "set_$key"; |
399
|
|
|
$invoice->$method( $_POST[ $key ] ); |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
// Maybe disable taxes. |
404
|
|
|
$invoice->set_disable_taxes( ! empty( $_POST['taxes'] ) ); |
405
|
|
|
|
406
|
|
|
// Recalculate totals. |
407
|
|
|
$invoice->recalculate_total(); |
408
|
|
|
|
409
|
|
|
$total = wpinv_price( wpinv_format_amount( $invoice->get_total() ), $invoice->get_currency() ); |
410
|
|
|
|
411
|
|
|
if ( $invoice->is_recurring() && $invoice->is_parent() && $invoice->get_total() != $invoice->get_recurring_total() ) { |
412
|
|
|
$recurring_total = wpinv_price( wpinv_format_amount( $invoice->get_recurring_total() ), $invoice->get_currency() ); |
413
|
|
|
$total .= '<small class="form-text text-muted">' . sprintf( __( 'Recurring Price: %s', 'invoicing' ), $recurring_total ) . '</small>'; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
$totals = array( |
417
|
|
|
'subtotal' => wpinv_price( wpinv_format_amount( $invoice->get_subtotal() ), $invoice->get_currency() ), |
418
|
|
|
'discount' => wpinv_price( wpinv_format_amount( $invoice->get_total_discount() ), $invoice->get_currency() ), |
419
|
|
|
'tax' => wpinv_price( wpinv_format_amount( $invoice->get_total_tax() ), $invoice->get_currency() ), |
420
|
|
|
'total' => $total, |
421
|
|
|
); |
422
|
|
|
|
423
|
|
|
$totals = apply_filters( 'getpaid_invoice_totals', $totals, $invoice ); |
424
|
|
|
|
425
|
|
|
wp_send_json_success( compact( 'totals' ) ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Get items belonging to a given invoice. |
430
|
|
|
*/ |
431
|
|
|
public static function get_invoice_items() { |
432
|
|
|
|
433
|
|
|
// Verify nonce. |
434
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
435
|
|
|
|
436
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
437
|
|
|
exit; |
|
|
|
|
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
// We need an invoice and items. |
441
|
|
|
if ( empty( $_POST['post_id'] ) ) { |
442
|
|
|
exit; |
|
|
|
|
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
// Fetch the invoice. |
446
|
|
|
$invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
447
|
|
|
|
448
|
|
|
// Ensure it exists. |
449
|
|
|
if ( ! $invoice->get_id() ) { |
450
|
|
|
exit; |
|
|
|
|
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
// Return an array of invoice items. |
454
|
|
|
$items = array(); |
455
|
|
|
|
456
|
|
|
foreach ( $invoice->get_items() as $item_id => $item ) { |
457
|
|
|
$items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
wp_send_json_success( compact( 'items' ) ); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Edits an invoice item. |
465
|
|
|
*/ |
466
|
|
|
public static function edit_invoice_item() { |
467
|
|
|
|
468
|
|
|
// Verify nonce. |
469
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
470
|
|
|
|
471
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
472
|
|
|
exit; |
|
|
|
|
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
// We need an invoice and item details. |
476
|
|
|
if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) { |
477
|
|
|
exit; |
|
|
|
|
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
// Fetch the invoice. |
481
|
|
|
$invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
482
|
|
|
|
483
|
|
|
// Ensure it exists and its not been paid for. |
484
|
|
|
if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
485
|
|
|
exit; |
|
|
|
|
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
// Format the data. |
489
|
|
|
$data = wp_list_pluck( $_POST['data'], 'value', 'field' ); |
490
|
|
|
|
491
|
|
|
// Ensure that we have an item id. |
492
|
|
|
if ( empty( $data['id'] ) ) { |
493
|
|
|
exit; |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
// Abort if the invoice does not have the specified item. |
497
|
|
|
$item = $invoice->get_item( (int) $data['id'] ); |
498
|
|
|
|
499
|
|
|
if ( empty( $item ) ) { |
500
|
|
|
exit; |
|
|
|
|
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// Update the item. |
504
|
|
|
$item->set_price( $data['price'] ); |
505
|
|
|
$item->set_name( $data['name'] ); |
506
|
|
|
$item->set_description( $data['description'] ); |
507
|
|
|
$item->set_quantity( $data['quantity'] ); |
508
|
|
|
|
509
|
|
|
// Add it to the invoice. |
510
|
|
|
$error = $invoice->add_item( $item ); |
511
|
|
|
$alert = false; |
512
|
|
|
if ( is_wp_error( $error ) ) { |
513
|
|
|
$alert = $error->get_error_message(); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// Update totals. |
517
|
|
|
$invoice->recalculate_total(); |
518
|
|
|
|
519
|
|
|
// Save the invoice. |
520
|
|
|
$invoice->save(); |
521
|
|
|
|
522
|
|
|
// Return an array of invoice items. |
523
|
|
|
$items = array(); |
524
|
|
|
|
525
|
|
|
foreach ( $invoice->get_items() as $item_id => $item ) { |
526
|
|
|
$items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
wp_send_json_success( compact( 'items', 'alert' ) ); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Deletes an invoice item. |
534
|
|
|
*/ |
535
|
|
|
public static function remove_invoice_item() { |
536
|
|
|
|
537
|
|
|
// Verify nonce. |
538
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
539
|
|
|
|
540
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
541
|
|
|
exit; |
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
// We need an invoice and an item. |
545
|
|
|
if ( empty( $_POST['post_id'] ) || empty( $_POST['item_id'] ) ) { |
546
|
|
|
exit; |
|
|
|
|
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
// Fetch the invoice. |
550
|
|
|
$invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
551
|
|
|
|
552
|
|
|
// Ensure it exists and its not been paid for. |
553
|
|
|
if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
554
|
|
|
exit; |
|
|
|
|
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
// Abort if the invoice does not have the specified item. |
558
|
|
|
$item = $invoice->get_item( (int) $_POST['item_id'] ); |
559
|
|
|
|
560
|
|
|
if ( empty( $item ) ) { |
561
|
|
|
exit; |
|
|
|
|
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
$invoice->remove_item( (int) $_POST['item_id'] ); |
565
|
|
|
|
566
|
|
|
// Update totals. |
567
|
|
|
$invoice->recalculate_total(); |
568
|
|
|
|
569
|
|
|
// Save the invoice. |
570
|
|
|
$invoice->save(); |
571
|
|
|
|
572
|
|
|
// Return an array of invoice items. |
573
|
|
|
$items = array(); |
574
|
|
|
|
575
|
|
|
foreach ( $invoice->get_items() as $item_id => $item ) { |
576
|
|
|
$items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
wp_send_json_success( compact( 'items' ) ); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Adds a items to an invoice. |
584
|
|
|
*/ |
585
|
|
|
public static function add_invoice_items() { |
586
|
|
|
|
587
|
|
|
// Verify nonce. |
588
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
589
|
|
|
|
590
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
591
|
|
|
exit; |
|
|
|
|
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
// We need an invoice and items. |
595
|
|
|
if ( empty( $_POST['post_id'] ) || empty( $_POST['items'] ) ) { |
596
|
|
|
exit; |
|
|
|
|
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
// Fetch the invoice. |
600
|
|
|
$invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
601
|
|
|
$alert = false; |
602
|
|
|
|
603
|
|
|
// Ensure it exists and its not been paid for. |
604
|
|
|
if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
605
|
|
|
exit; |
|
|
|
|
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
// Add the items. |
609
|
|
|
foreach ( $_POST['items'] as $data ) { |
610
|
|
|
|
611
|
|
|
$item = new GetPaid_Form_Item( $data[ 'id' ] ); |
612
|
|
|
|
613
|
|
|
if ( is_numeric( $data[ 'qty' ] ) && (int) $data[ 'qty' ] > 0 ) { |
614
|
|
|
$item->set_quantity( $data[ 'qty' ] ); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
if ( $item->get_id() > 0 ) { |
618
|
|
|
$error = $invoice->add_item( $item ); |
619
|
|
|
|
620
|
|
|
if ( is_wp_error( $error ) ) { |
621
|
|
|
$alert = $error->get_error_message(); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
// Save the invoice. |
629
|
|
|
$invoice->recalculate_total(); |
630
|
|
|
$invoice->save(); |
631
|
|
|
|
632
|
|
|
// Return an array of invoice items. |
633
|
|
|
$items = array(); |
634
|
|
|
|
635
|
|
|
foreach ( $invoice->get_items() as $item_id => $item ) { |
636
|
|
|
$items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
wp_send_json_success( compact( 'items', 'alert' ) ); |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Retrieves items that should be added to an invoice. |
644
|
|
|
*/ |
645
|
|
|
public static function get_invoicing_items() { |
646
|
|
|
|
647
|
|
|
// Verify nonce. |
648
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
649
|
|
|
|
650
|
|
|
if ( ! wpinv_current_user_can_manage_invoicing() ) { |
651
|
|
|
exit; |
|
|
|
|
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
// We need a search term. |
655
|
|
|
if ( empty( $_GET['search'] ) ) { |
656
|
|
|
wp_send_json_success( array() ); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
// Retrieve items. |
660
|
|
|
$item_args = array( |
661
|
|
|
'post_type' => 'wpi_item', |
662
|
|
|
'orderby' => 'title', |
663
|
|
|
'order' => 'ASC', |
664
|
|
|
'posts_per_page' => -1, |
665
|
|
|
'post_status' => array( 'publish' ), |
666
|
|
|
's' => trim( $_GET['search'] ), |
667
|
|
|
'meta_query' => array( |
668
|
|
|
array( |
669
|
|
|
'key' => '_wpinv_type', |
670
|
|
|
'compare' => '!=', |
671
|
|
|
'value' => 'package' |
672
|
|
|
) |
673
|
|
|
) |
674
|
|
|
); |
675
|
|
|
|
676
|
|
|
$items = get_posts( apply_filters( 'getpaid_ajax_invoice_items_query_args', $item_args ) ); |
677
|
|
|
$data = array(); |
678
|
|
|
|
679
|
|
|
|
680
|
|
|
$is_payment_form = ( ! empty( $_GET['post_id'] ) && 'wpi_payment_form' == get_post_type( $_GET['post_id'] ) ); |
681
|
|
|
|
682
|
|
|
foreach ( $items as $item ) { |
683
|
|
|
$item = new GetPaid_Form_Item( $item ); |
684
|
|
|
$data[] = array( |
685
|
|
|
'id' => (int) $item->get_id(), |
686
|
|
|
'text' => strip_tags( $item->get_name() ), |
687
|
|
|
'form_data' => $is_payment_form ? $item->prepare_data_for_use( false ) : '', |
688
|
|
|
); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
wp_send_json_success( $data ); |
692
|
|
|
|
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Retrieves the states field for AUI forms. |
697
|
|
|
*/ |
698
|
|
|
public static function get_aui_states_field() { |
699
|
|
|
|
700
|
|
|
// Verify nonce. |
701
|
|
|
check_ajax_referer( 'wpinv-nonce' ); |
702
|
|
|
|
703
|
|
|
// We need a country. |
704
|
|
|
if ( empty( $_GET['country'] ) ) { |
705
|
|
|
exit; |
|
|
|
|
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
$states = wpinv_get_country_states( trim( $_GET['country'] ) ); |
709
|
|
|
$state = isset( $_GET['state'] ) ? trim( $_GET['state'] ) : wpinv_get_default_state(); |
710
|
|
|
|
711
|
|
|
if ( empty( $states ) ) { |
712
|
|
|
|
713
|
|
|
$html = aui()->input( |
714
|
|
|
array( |
715
|
|
|
'type' => 'text', |
716
|
|
|
'id' => 'wpinv_state', |
717
|
|
|
'name' => 'wpinv_state', |
718
|
|
|
'label' => __( 'State', 'invoicing' ), |
719
|
|
|
'label_type' => 'vertical', |
720
|
|
|
'placeholder' => 'Liège', |
721
|
|
|
'class' => 'form-control-sm', |
722
|
|
|
'value' => $state, |
723
|
|
|
) |
724
|
|
|
); |
725
|
|
|
|
726
|
|
|
} else { |
727
|
|
|
|
728
|
|
|
$html = aui()->select( |
729
|
|
|
array( |
730
|
|
|
'id' => 'wpinv_state', |
731
|
|
|
'name' => 'wpinv_state', |
732
|
|
|
'label' => __( 'State', 'invoicing' ), |
733
|
|
|
'label_type' => 'vertical', |
734
|
|
|
'placeholder' => __( 'Select a state', 'invoicing' ), |
735
|
|
|
'class' => 'form-control-sm', |
736
|
|
|
'value' => $state, |
737
|
|
|
'options' => $states, |
738
|
|
|
'data-allow-clear' => 'false', |
739
|
|
|
'select2' => true, |
740
|
|
|
) |
741
|
|
|
); |
742
|
|
|
|
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
wp_send_json_success( |
746
|
|
|
array( |
747
|
|
|
'html' => $html, |
748
|
|
|
'select' => ! empty ( $states ) |
749
|
|
|
) |
750
|
|
|
); |
751
|
|
|
|
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Refresh prices. |
756
|
|
|
* |
757
|
|
|
* @since 1.0.19 |
758
|
|
|
*/ |
759
|
|
|
public static function payment_form_refresh_prices() { |
760
|
|
|
|
761
|
|
|
// Check nonce. |
762
|
|
|
check_ajax_referer( 'getpaid_form_nonce' ); |
763
|
|
|
|
764
|
|
|
// ... form fields... |
765
|
|
|
if ( empty( $_POST['getpaid_payment_form_submission'] ) ) { |
766
|
|
|
_e( 'Error: Reload the page and try again.', 'invoicing' ); |
767
|
|
|
exit; |
|
|
|
|
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
// Load the submission. |
771
|
|
|
$submission = new GetPaid_Payment_Form_Submission(); |
772
|
|
|
|
773
|
|
|
// Do we have an error? |
774
|
|
|
if ( ! empty( $submission->last_error ) ) { |
775
|
|
|
echo $submission->last_error; |
776
|
|
|
exit; |
|
|
|
|
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
// Prepare the response. |
780
|
|
|
$response = new GetPaid_Payment_Form_Submission_Refresh_Prices( $submission ); |
781
|
|
|
|
782
|
|
|
// Filter the response. |
783
|
|
|
$response = apply_filters( 'getpaid_payment_form_ajax_refresh_prices', $response->response, $submission ); |
784
|
|
|
|
785
|
|
|
wp_send_json_success( $response ); |
786
|
|
|
} |
787
|
|
|
|
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
WPInv_Ajax::init(); |