Total Complexity | 218 |
Total Lines | 1231 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like WPInv_Ajax often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Ajax, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WPInv_Ajax { |
||
15 | public static function init() { |
||
16 | add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 ); |
||
17 | add_action( 'template_redirect', array( __CLASS__, 'do_wpinv_ajax' ), 0 ); |
||
18 | self::add_ajax_events(); |
||
19 | } |
||
20 | |||
21 | public static function define_ajax() { |
||
22 | if ( !empty( $_GET['wpinv-ajax'] ) ) { |
||
23 | if ( ! defined( 'DOING_AJAX' ) ) { |
||
24 | define( 'DOING_AJAX', true ); |
||
25 | } |
||
26 | if ( ! defined( 'WC_DOING_AJAX' ) ) { |
||
27 | define( 'WC_DOING_AJAX', true ); |
||
28 | } |
||
29 | // Turn off display_errors during AJAX events to prevent malformed JSON |
||
30 | if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) { |
||
31 | /** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 ); |
||
32 | } |
||
33 | $GLOBALS['wpdb']->hide_errors(); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | public static function do_wpinv_ajax() { |
||
38 | global $wp_query; |
||
39 | |||
40 | if ( !empty( $_GET['wpinv-ajax'] ) ) { |
||
41 | $wp_query->set( 'wpinv-ajax', sanitize_text_field( $_GET['wpinv-ajax'] ) ); |
||
42 | } |
||
43 | |||
44 | if ( $action = $wp_query->get( 'wpinv-ajax' ) ) { |
||
45 | self::wpinv_ajax_headers(); |
||
46 | do_action( 'wpinv_ajax_' . sanitize_text_field( $action ) ); |
||
47 | die(); |
||
|
|||
48 | } |
||
49 | } |
||
50 | |||
51 | private static function wpinv_ajax_headers() { |
||
58 | } |
||
59 | |||
60 | public static function add_ajax_events() { |
||
61 | $ajax_events = array( |
||
62 | 'add_note' => false, |
||
63 | 'delete_note' => false, |
||
64 | 'get_states_field' => true, |
||
65 | 'checkout' => false, |
||
66 | 'payment_form_get_taxes' => true, |
||
67 | 'payment_form' => true, |
||
68 | 'get_payment_form_states_field' => true, |
||
69 | 'add_invoice_item' => false, |
||
70 | 'remove_invoice_item' => false, |
||
71 | 'create_invoice_item' => false, |
||
72 | 'get_billing_details' => false, |
||
73 | 'admin_recalculate_totals' => false, |
||
74 | 'admin_apply_discount' => false, |
||
75 | 'admin_remove_discount' => false, |
||
76 | 'check_email' => false, |
||
77 | 'run_tool' => false, |
||
78 | 'apply_discount' => true, |
||
79 | 'remove_discount' => true, |
||
80 | 'buy_items' => true, |
||
81 | ); |
||
82 | |||
83 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
84 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
85 | |||
86 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
87 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
88 | } |
||
89 | |||
90 | if ( $nopriv ) { |
||
91 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
92 | |||
93 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public static function add_note() { |
||
99 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
100 | |||
101 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
102 | die(-1); |
||
103 | } |
||
104 | |||
105 | $post_id = absint( $_POST['post_id'] ); |
||
106 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
107 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
108 | |||
109 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
110 | |||
111 | if ( $post_id > 0 ) { |
||
112 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
113 | |||
114 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
115 | wpinv_get_invoice_note_line_item( $note_id ); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | die(); |
||
120 | } |
||
121 | |||
122 | public static function delete_note() { |
||
123 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
124 | |||
125 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
126 | die(-1); |
||
127 | } |
||
128 | |||
129 | $note_id = (int)$_POST['note_id']; |
||
130 | |||
131 | if ( $note_id > 0 ) { |
||
132 | wp_delete_comment( $note_id, true ); |
||
133 | } |
||
134 | |||
135 | die(); |
||
136 | } |
||
137 | |||
138 | public static function get_states_field() { |
||
139 | echo wpinv_get_states_field(); |
||
140 | |||
141 | die(); |
||
142 | } |
||
143 | |||
144 | public static function checkout() { |
||
145 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
146 | define( 'WPINV_CHECKOUT', true ); |
||
147 | } |
||
148 | |||
149 | wpinv_process_checkout(); |
||
150 | die(0); |
||
151 | } |
||
152 | |||
153 | public static function add_invoice_item() { |
||
154 | global $wpi_userID, $wpinv_ip_address_country; |
||
155 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
156 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
157 | die(-1); |
||
158 | } |
||
159 | |||
160 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
161 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
162 | |||
163 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
164 | die(); |
||
165 | } |
||
166 | |||
167 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
168 | if ( empty( $invoice ) ) { |
||
169 | die(); |
||
170 | } |
||
171 | |||
172 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
173 | die(); // Don't allow modify items for paid invoice. |
||
174 | } |
||
175 | |||
176 | if ( !empty( $_POST['user_id'] ) ) { |
||
177 | $wpi_userID = absint( $_POST['user_id'] ); |
||
178 | } |
||
179 | |||
180 | $item = new WPInv_Item( $item_id ); |
||
181 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
182 | die(); |
||
183 | } |
||
184 | |||
185 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
186 | if ( !empty( $invoice->cart_details ) ) { |
||
187 | $valid = true; |
||
188 | |||
189 | if ( $recurring_item = $invoice->get_recurring() ) { |
||
190 | if ( $recurring_item != $item_id ) { |
||
191 | $valid = false; |
||
192 | } |
||
193 | } else if ( wpinv_is_recurring_item( $item_id ) ) { |
||
194 | $valid = false; |
||
195 | } |
||
196 | |||
197 | if ( !$valid ) { |
||
198 | $response = array(); |
||
199 | $response['success'] = false; |
||
200 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
201 | wp_send_json( $response ); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | $checkout_session = wpinv_get_checkout_session(); |
||
206 | |||
207 | $data = array(); |
||
208 | $data['invoice_id'] = $invoice_id; |
||
209 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
210 | |||
211 | wpinv_set_checkout_session( $data ); |
||
212 | |||
213 | $quantity = wpinv_item_quantities_enabled() && !empty($_POST['qty']) && (int)$_POST['qty'] > 0 ? (int)$_POST['qty'] : 1; |
||
214 | |||
215 | $args = array( |
||
216 | 'id' => $item_id, |
||
217 | 'quantity' => $quantity, |
||
218 | 'item_price' => $item->get_price(), |
||
219 | 'custom_price' => '', |
||
220 | 'tax' => 0.00, |
||
221 | 'discount' => 0, |
||
222 | 'meta' => array(), |
||
223 | 'fees' => array() |
||
224 | ); |
||
225 | |||
226 | $invoice->add_item( $item_id, $args ); |
||
227 | $invoice->save(); |
||
228 | |||
229 | if ( empty( $_POST['country'] ) ) { |
||
230 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
231 | } |
||
232 | if ( empty( $_POST['state'] ) ) { |
||
233 | $_POST['state'] = $invoice->state; |
||
234 | } |
||
235 | |||
236 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
237 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
238 | |||
239 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
240 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
241 | |||
242 | $wpinv_ip_address_country = $invoice->country; |
||
243 | |||
244 | $invoice->recalculate_totals(true); |
||
245 | |||
246 | $response = array(); |
||
247 | $response['success'] = true; |
||
248 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
249 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
250 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
251 | $response['data']['tax'] = $invoice->get_tax(); |
||
252 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
253 | $response['data']['discount'] = $invoice->get_discount(); |
||
254 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
255 | $response['data']['total'] = $invoice->get_total(); |
||
256 | $response['data']['totalf'] = $invoice->get_total(true); |
||
257 | |||
258 | wpinv_set_checkout_session($checkout_session); |
||
259 | |||
260 | wp_send_json( $response ); |
||
261 | } |
||
262 | |||
263 | |||
264 | public static function remove_invoice_item() { |
||
265 | global $wpi_userID, $wpinv_ip_address_country; |
||
266 | |||
267 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
268 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
269 | die(-1); |
||
270 | } |
||
271 | |||
272 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
273 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
274 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
275 | |||
276 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
277 | die(); |
||
278 | } |
||
279 | |||
280 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
281 | if ( empty( $invoice ) ) { |
||
282 | die(); |
||
283 | } |
||
284 | |||
285 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
286 | die(); // Don't allow modify items for paid invoice. |
||
287 | } |
||
288 | |||
289 | if ( !empty( $_POST['user_id'] ) ) { |
||
290 | $wpi_userID = absint( $_POST['user_id'] ); |
||
291 | } |
||
292 | |||
293 | $item = new WPInv_Item( $item_id ); |
||
294 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
295 | die(); |
||
296 | } |
||
297 | |||
298 | $checkout_session = wpinv_get_checkout_session(); |
||
299 | |||
300 | $data = array(); |
||
301 | $data['invoice_id'] = $invoice_id; |
||
302 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
303 | |||
304 | wpinv_set_checkout_session( $data ); |
||
305 | |||
306 | $args = array( |
||
307 | 'id' => $item_id, |
||
308 | 'quantity' => 1, |
||
309 | 'cart_index' => $cart_index |
||
310 | ); |
||
311 | |||
312 | $invoice->remove_item( $item_id, $args ); |
||
313 | $invoice->save(); |
||
314 | |||
315 | if ( empty( $_POST['country'] ) ) { |
||
316 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
317 | } |
||
318 | if ( empty( $_POST['state'] ) ) { |
||
319 | $_POST['state'] = $invoice->state; |
||
320 | } |
||
321 | |||
322 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
323 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
324 | |||
325 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
326 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
327 | |||
328 | $wpinv_ip_address_country = $invoice->country; |
||
329 | |||
330 | $invoice->recalculate_totals(true); |
||
331 | |||
332 | $response = array(); |
||
333 | $response['success'] = true; |
||
334 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
335 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
336 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
337 | $response['data']['tax'] = $invoice->get_tax(); |
||
338 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
339 | $response['data']['discount'] = $invoice->get_discount(); |
||
340 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
341 | $response['data']['total'] = $invoice->get_total(); |
||
342 | $response['data']['totalf'] = $invoice->get_total(true); |
||
343 | |||
344 | wpinv_set_checkout_session($checkout_session); |
||
345 | |||
346 | wp_send_json( $response ); |
||
347 | } |
||
348 | |||
349 | public static function create_invoice_item() { |
||
350 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
351 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
352 | die(-1); |
||
353 | } |
||
354 | |||
355 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
356 | |||
357 | // Find the item |
||
358 | if ( !is_numeric( $invoice_id ) ) { |
||
359 | die(); |
||
360 | } |
||
361 | |||
362 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
363 | if ( empty( $invoice ) ) { |
||
364 | die(); |
||
365 | } |
||
366 | |||
367 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
368 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
369 | $response = array(); |
||
370 | $response['success'] = false; |
||
371 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
372 | wp_send_json( $response ); |
||
373 | } |
||
374 | |||
375 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
376 | |||
377 | $meta = array(); |
||
378 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
379 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
380 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
381 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
382 | |||
383 | $data = array(); |
||
384 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
385 | $data['post_status'] = 'publish'; |
||
386 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
387 | $data['meta'] = $meta; |
||
388 | |||
389 | $item = new WPInv_Item(); |
||
390 | $item->create( $data ); |
||
391 | |||
392 | if ( !empty( $item ) ) { |
||
393 | $_POST['item_id'] = $item->ID; |
||
394 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
395 | |||
396 | self::add_invoice_item(); |
||
397 | } |
||
398 | die(); |
||
399 | } |
||
400 | |||
401 | public static function get_billing_details() { |
||
402 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
403 | |||
404 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
405 | die(-1); |
||
406 | } |
||
407 | |||
408 | $user_id = (int)$_POST['user_id']; |
||
409 | $billing_details = wpinv_get_user_address($user_id); |
||
410 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
411 | |||
412 | if (isset($billing_details['user_id'])) { |
||
413 | unset($billing_details['user_id']); |
||
414 | } |
||
415 | |||
416 | if (isset($billing_details['email'])) { |
||
417 | unset($billing_details['email']); |
||
418 | } |
||
419 | |||
420 | $response = array(); |
||
421 | $response['success'] = true; |
||
422 | $response['data']['billing_details'] = $billing_details; |
||
423 | |||
424 | wp_send_json( $response ); |
||
425 | } |
||
426 | |||
427 | public static function admin_recalculate_totals() { |
||
428 | global $wpi_userID, $wpinv_ip_address_country; |
||
429 | |||
430 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
431 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
432 | die(-1); |
||
433 | } |
||
434 | |||
435 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
436 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
437 | if ( empty( $invoice ) ) { |
||
438 | die(); |
||
439 | } |
||
440 | |||
441 | $checkout_session = wpinv_get_checkout_session(); |
||
442 | |||
443 | $data = array(); |
||
444 | $data['invoice_id'] = $invoice_id; |
||
445 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
446 | |||
447 | wpinv_set_checkout_session( $data ); |
||
448 | |||
449 | if ( !empty( $_POST['user_id'] ) ) { |
||
450 | $wpi_userID = absint( $_POST['user_id'] ); |
||
451 | } |
||
452 | |||
453 | if ( empty( $_POST['country'] ) ) { |
||
454 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
455 | } |
||
456 | |||
457 | $disable_taxes = 0; |
||
458 | if ( ! empty( $_POST['disable_taxes'] ) ) { |
||
459 | $disable_taxes = 1; |
||
460 | } |
||
461 | $invoice->set( 'disable_taxes', $disable_taxes ); |
||
462 | |||
463 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
464 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
465 | if ( isset( $_POST['state'] ) ) { |
||
466 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
467 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
468 | } |
||
469 | |||
470 | $wpinv_ip_address_country = $invoice->country; |
||
471 | |||
472 | $invoice = $invoice->recalculate_totals(true); |
||
473 | |||
474 | $response = array(); |
||
475 | $response['success'] = true; |
||
476 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
477 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
478 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
479 | $response['data']['tax'] = $invoice->get_tax(); |
||
480 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
481 | $response['data']['discount'] = $invoice->get_discount(); |
||
482 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
483 | $response['data']['total'] = $invoice->get_total(); |
||
484 | $response['data']['totalf'] = $invoice->get_total(true); |
||
485 | |||
486 | wpinv_set_checkout_session($checkout_session); |
||
487 | |||
488 | wp_send_json( $response ); |
||
489 | } |
||
490 | |||
491 | public static function admin_apply_discount() { |
||
492 | global $wpi_userID; |
||
493 | |||
494 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
495 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
496 | die(-1); |
||
497 | } |
||
498 | |||
499 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
500 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
501 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
502 | die(); |
||
503 | } |
||
504 | |||
505 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
506 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
507 | die(); |
||
508 | } |
||
509 | |||
510 | $checkout_session = wpinv_get_checkout_session(); |
||
511 | |||
512 | $data = array(); |
||
513 | $data['invoice_id'] = $invoice_id; |
||
514 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
515 | |||
516 | wpinv_set_checkout_session( $data ); |
||
517 | |||
518 | $response = array(); |
||
519 | $response['success'] = false; |
||
520 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
521 | $response['data']['code'] = $discount_code; |
||
522 | |||
523 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
524 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
525 | |||
526 | $response['success'] = true; |
||
527 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
528 | } else { |
||
529 | $errors = wpinv_get_errors(); |
||
530 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
531 | $response['msg'] = $errors['wpinv-discount-error']; |
||
532 | } |
||
533 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
534 | } |
||
535 | |||
536 | wpinv_set_checkout_session($checkout_session); |
||
537 | |||
538 | wp_send_json( $response ); |
||
539 | } |
||
540 | |||
541 | public static function admin_remove_discount() { |
||
542 | global $wpi_userID; |
||
543 | |||
544 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
545 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
546 | die(-1); |
||
547 | } |
||
548 | |||
549 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
550 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
551 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
552 | die(); |
||
553 | } |
||
554 | |||
555 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
556 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
557 | die(); |
||
558 | } |
||
559 | |||
560 | $checkout_session = wpinv_get_checkout_session(); |
||
561 | |||
562 | $data = array(); |
||
563 | $data['invoice_id'] = $invoice_id; |
||
564 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
565 | |||
566 | wpinv_set_checkout_session( $data ); |
||
567 | |||
568 | $response = array(); |
||
569 | $response['success'] = false; |
||
570 | $response['msg'] = NULL; |
||
571 | |||
572 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
573 | $response['success'] = true; |
||
574 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
575 | |||
576 | wpinv_set_checkout_session($checkout_session); |
||
577 | |||
578 | wp_send_json( $response ); |
||
579 | } |
||
580 | |||
581 | public static function check_email() { |
||
582 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
583 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
584 | die(-1); |
||
585 | } |
||
586 | |||
587 | $email = sanitize_text_field( $_POST['email'] ); |
||
588 | |||
589 | $response = array(); |
||
590 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
591 | $user_id = $user_data->ID; |
||
592 | $user_login = $user_data->user_login; |
||
593 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
594 | $billing_details = wpinv_get_user_address($user_id); |
||
595 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
596 | |||
597 | if (isset($billing_details['user_id'])) { |
||
598 | unset($billing_details['user_id']); |
||
599 | } |
||
600 | |||
601 | if (isset($billing_details['email'])) { |
||
602 | unset($billing_details['email']); |
||
603 | } |
||
604 | |||
605 | $response['success'] = true; |
||
606 | $response['data']['id'] = $user_data->ID; |
||
607 | $response['data']['name'] = $user_data->user_email; |
||
608 | $response['data']['billing_details'] = $billing_details; |
||
609 | } |
||
610 | |||
611 | wp_send_json( $response ); |
||
612 | } |
||
613 | |||
614 | public static function run_tool() { |
||
615 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
616 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
617 | die(-1); |
||
618 | } |
||
619 | |||
620 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
621 | |||
622 | do_action( 'wpinv_run_tool' ); |
||
623 | |||
624 | if ( !empty( $tool ) ) { |
||
625 | do_action( 'wpinv_tool_' . $tool ); |
||
626 | } |
||
627 | } |
||
628 | |||
629 | public static function apply_discount() { |
||
676 | } |
||
677 | |||
678 | public static function remove_discount() { |
||
679 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
680 | |||
681 | $response = array(); |
||
682 | |||
683 | if ( isset( $_POST['code'] ) ) { |
||
684 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
685 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
686 | $total = wpinv_get_cart_total( null, $discounts ); |
||
687 | $cart_totals = wpinv_recalculate_tax( true ); |
||
688 | |||
689 | if ( !empty( $cart_totals ) ) { |
||
690 | $response['success'] = true; |
||
691 | $response['data'] = $cart_totals; |
||
692 | $response['data']['code'] = $discount_code; |
||
693 | } else { |
||
694 | $response['success'] = false; |
||
695 | } |
||
696 | |||
697 | // Allow for custom discount code handling |
||
698 | $response = apply_filters( 'wpinv_ajax_discount_response', $response ); |
||
699 | } |
||
700 | |||
701 | wp_send_json( $response ); |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * Payment forms. |
||
706 | * |
||
707 | * @since 1.0.18 |
||
708 | */ |
||
709 | public static function payment_form() { |
||
910 | } |
||
911 | |||
912 | /** |
||
913 | * Payment forms. |
||
914 | * |
||
915 | * @since 1.0.18 |
||
916 | */ |
||
917 | public static function get_payment_form_states_field() { |
||
918 | global $invoicing; |
||
919 | |||
920 | if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
||
921 | exit; |
||
922 | } |
||
923 | |||
924 | $elements = $invoicing->form_elements->get_form_elements( $_GET['form'] ); |
||
925 | |||
926 | if ( empty( $elements ) ) { |
||
927 | exit; |
||
928 | } |
||
929 | |||
930 | $address_fields = array(); |
||
931 | foreach ( $elements as $element ) { |
||
932 | if ( 'address' === $element['type'] ) { |
||
933 | $address_fields = $element; |
||
934 | break; |
||
935 | } |
||
936 | } |
||
937 | |||
938 | if ( empty( $address_fields ) ) { |
||
939 | exit; |
||
940 | } |
||
941 | |||
942 | foreach( $address_fields['fields'] as $address_field ) { |
||
943 | |||
944 | if ( 'wpinv_state' == $address_field['name'] ) { |
||
945 | |||
946 | $label = $address_field['label']; |
||
947 | |||
948 | if ( ! empty( $address_field['required'] ) ) { |
||
949 | $label .= "<span class='text-danger'> *</span>"; |
||
950 | } |
||
951 | |||
952 | $states = wpinv_get_country_states( $_GET['country'] ); |
||
953 | |||
954 | if ( ! empty( $states ) ) { |
||
955 | |||
956 | $html = aui()->select( |
||
957 | array( |
||
958 | 'options' => $states, |
||
959 | 'name' => esc_attr( $address_field['name'] ), |
||
960 | 'id' => esc_attr( $address_field['name'] ), |
||
961 | 'placeholder' => esc_attr( $address_field['placeholder'] ), |
||
962 | 'required' => (bool) $address_field['required'], |
||
963 | 'no_wrap' => true, |
||
964 | 'label' => wp_kses_post( $label ), |
||
965 | 'select2' => false, |
||
966 | ) |
||
967 | ); |
||
968 | |||
969 | } else { |
||
970 | |||
971 | $html = aui()->input( |
||
972 | array( |
||
973 | 'name' => esc_attr( $address_field['name'] ), |
||
974 | 'id' => esc_attr( $address_field['name'] ), |
||
975 | 'required' => (bool) $address_field['required'], |
||
976 | 'label' => wp_kses_post( $label ), |
||
977 | 'no_wrap' => true, |
||
978 | 'type' => 'text', |
||
979 | ) |
||
980 | ); |
||
981 | |||
982 | } |
||
983 | |||
984 | wp_send_json_success( str_replace( 'sr-only', '', $html ) ); |
||
985 | exit; |
||
986 | |||
987 | } |
||
988 | |||
989 | } |
||
990 | |||
991 | exit; |
||
992 | } |
||
993 | |||
994 | /** |
||
995 | * Payment forms. |
||
996 | * |
||
997 | * @since 1.0.18 |
||
998 | */ |
||
999 | public static function payment_form_get_taxes() { |
||
1000 | global $invoicing; |
||
1001 | |||
1002 | // Check nonce. |
||
1003 | check_ajax_referer( 'wpinv_payment_form', 'wpinv_payment_form' ); |
||
1004 | |||
1005 | // Prepare submitted data... |
||
1006 | $data = wp_unslash( $_POST ); |
||
1007 | |||
1008 | // ... form fields... |
||
1009 | if ( empty( $data['form_id'] ) || 'publish' != get_post_status( $data['form_id'] ) ) { |
||
1010 | exit; |
||
1011 | } |
||
1012 | |||
1013 | // ... and form items. |
||
1014 | $items = $invoicing->form_elements->get_form_items( $data['form_id'] ); |
||
1015 | $total = 0; |
||
1016 | $tax = 0; |
||
1017 | $sub_total = 0; |
||
1018 | $country = wpinv_default_billing_country(); |
||
1019 | $state = false; |
||
1020 | |||
1021 | if ( ! empty( $_POST['wpinv_country'] ) ) { |
||
1022 | $country = $_POST['wpinv_country']; |
||
1023 | } |
||
1024 | |||
1025 | if ( ! empty( $_POST['wpinv_state'] ) ) { |
||
1026 | $state = $_POST['wpinv_state']; |
||
1027 | } |
||
1028 | |||
1029 | if ( ! empty( $data['wpinv-items'] ) ) { |
||
1030 | |||
1031 | $selected_items = wpinv_clean( $data['wpinv-items'] ); |
||
1032 | |||
1033 | foreach ( $items as $item ) { |
||
1034 | |||
1035 | if ( ! isset( $selected_items[ $item['id'] ] ) ) { |
||
1036 | continue; |
||
1037 | } |
||
1038 | |||
1039 | $quantity = 1; |
||
1040 | |||
1041 | if ( ! empty( $item['allow_quantities'] ) && ! empty( $data["wpinv-item-{$item['id']}-quantity"] ) ) { |
||
1042 | |||
1043 | $quantity = intval( $data["wpinv-item-{$item['id']}-quantity"] ); |
||
1044 | |||
1045 | if ( 1 > $quantity ) { |
||
1046 | $quantity = 1; |
||
1047 | } |
||
1048 | |||
1049 | } |
||
1050 | |||
1051 | // Custom pricing. |
||
1052 | $price = wpinv_sanitize_amount( $item['price'] ); |
||
1053 | if ( ! empty( $item['custom_price'] ) ) { |
||
1054 | |||
1055 | $minimum_price = wpinv_sanitize_amount( $item['minimum_price'] ); |
||
1056 | $set_price = wpinv_sanitize_amount( $selected_items[ $item['id'] ] ); |
||
1057 | |||
1058 | if ( $set_price < $minimum_price ) { |
||
1059 | $set_price = $minimum_price; |
||
1060 | } |
||
1061 | |||
1062 | $price = wpinv_sanitize_amount( $set_price ); |
||
1063 | |||
1064 | } |
||
1065 | |||
1066 | $price = $quantity * floatval( $price ); |
||
1067 | |||
1068 | if ( wpinv_use_taxes() ) { |
||
1069 | |||
1070 | $rate = wpinv_get_tax_rate( $country, $state, (int) $item['id'] ); |
||
1071 | |||
1072 | if ( wpinv_prices_include_tax() ) { |
||
1073 | $pre_tax = ( $price - $price * $rate * 0.01 ); |
||
1074 | $item_tax = $price - $pre_tax; |
||
1075 | } else { |
||
1076 | $pre_tax = $price; |
||
1077 | $item_tax = $price * $rate * 0.01; |
||
1078 | } |
||
1079 | |||
1080 | $tax = $tax + $item_tax; |
||
1081 | $sub_total = $sub_total + $pre_tax; |
||
1082 | $total = $sub_total + $tax; |
||
1083 | |||
1084 | } else { |
||
1085 | $total = $total + $price; |
||
1086 | } |
||
1087 | |||
1088 | } |
||
1089 | |||
1090 | } |
||
1091 | |||
1092 | wp_send_json_success( array( |
||
1093 | 'total' => wpinv_price( wpinv_format_amount( $total ) ), |
||
1094 | 'tax' => wpinv_price( wpinv_format_amount( $tax ) ), |
||
1095 | 'sub_total' => wpinv_price( wpinv_format_amount( $sub_total ) ), |
||
1096 | )); |
||
1097 | exit; |
||
1098 | } |
||
1099 | |||
1100 | /** |
||
1101 | * Lets users buy items via ajax. |
||
1102 | * |
||
1103 | * @since 1.0.0 |
||
1104 | */ |
||
1105 | public static function buy_items() { |
||
1245 | } |
||
1246 | } |
||
1247 | |||
1248 | WPInv_Ajax::init(); |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.