| Total Complexity | 207 |
| Total Lines | 1153 |
| 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 | 'add_invoice_item' => false, |
||
| 69 | 'remove_invoice_item' => false, |
||
| 70 | 'create_invoice_item' => false, |
||
| 71 | 'get_billing_details' => false, |
||
| 72 | 'admin_recalculate_totals' => false, |
||
| 73 | 'admin_apply_discount' => false, |
||
| 74 | 'admin_remove_discount' => false, |
||
| 75 | 'check_email' => false, |
||
| 76 | 'run_tool' => false, |
||
| 77 | 'apply_discount' => true, |
||
| 78 | 'remove_discount' => true, |
||
| 79 | 'buy_items' => true, |
||
| 80 | ); |
||
| 81 | |||
| 82 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 83 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 84 | |||
| 85 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
| 86 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ( $nopriv ) { |
||
| 90 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 91 | |||
| 92 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public static function add_note() { |
||
| 98 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
| 99 | |||
| 100 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 101 | die(-1); |
||
| 102 | } |
||
| 103 | |||
| 104 | $post_id = absint( $_POST['post_id'] ); |
||
| 105 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
| 106 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
| 107 | |||
| 108 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 109 | |||
| 110 | if ( $post_id > 0 ) { |
||
| 111 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
| 112 | |||
| 113 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
| 114 | wpinv_get_invoice_note_line_item( $note_id ); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | die(); |
||
| 119 | } |
||
| 120 | |||
| 121 | public static function delete_note() { |
||
| 122 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
| 123 | |||
| 124 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 125 | die(-1); |
||
| 126 | } |
||
| 127 | |||
| 128 | $note_id = (int)$_POST['note_id']; |
||
| 129 | |||
| 130 | if ( $note_id > 0 ) { |
||
| 131 | wp_delete_comment( $note_id, true ); |
||
| 132 | } |
||
| 133 | |||
| 134 | die(); |
||
| 135 | } |
||
| 136 | |||
| 137 | public static function get_states_field() { |
||
| 138 | echo wpinv_get_states_field(); |
||
| 139 | |||
| 140 | die(); |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function checkout() { |
||
| 144 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
| 145 | define( 'WPINV_CHECKOUT', true ); |
||
| 146 | } |
||
| 147 | |||
| 148 | wpinv_process_checkout(); |
||
| 149 | die(0); |
||
| 150 | } |
||
| 151 | |||
| 152 | public static function add_invoice_item() { |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | public static function remove_invoice_item() { |
||
| 264 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 265 | |||
| 266 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 267 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 268 | die(-1); |
||
| 269 | } |
||
| 270 | |||
| 271 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 272 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 273 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
| 274 | |||
| 275 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 276 | die(); |
||
| 277 | } |
||
| 278 | |||
| 279 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 280 | if ( empty( $invoice ) ) { |
||
| 281 | die(); |
||
| 282 | } |
||
| 283 | |||
| 284 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 285 | die(); // Don't allow modify items for paid invoice. |
||
| 286 | } |
||
| 287 | |||
| 288 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 289 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 290 | } |
||
| 291 | |||
| 292 | $item = new WPInv_Item( $item_id ); |
||
| 293 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 294 | die(); |
||
| 295 | } |
||
| 296 | |||
| 297 | $checkout_session = wpinv_get_checkout_session(); |
||
| 298 | |||
| 299 | $data = array(); |
||
| 300 | $data['invoice_id'] = $invoice_id; |
||
| 301 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 302 | |||
| 303 | wpinv_set_checkout_session( $data ); |
||
| 304 | |||
| 305 | $args = array( |
||
| 306 | 'id' => $item_id, |
||
| 307 | 'quantity' => 1, |
||
| 308 | 'cart_index' => $cart_index |
||
| 309 | ); |
||
| 310 | |||
| 311 | $invoice->remove_item( $item_id, $args ); |
||
| 312 | $invoice->save(); |
||
| 313 | |||
| 314 | if ( empty( $_POST['country'] ) ) { |
||
| 315 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 316 | } |
||
| 317 | if ( empty( $_POST['state'] ) ) { |
||
| 318 | $_POST['state'] = $invoice->state; |
||
| 319 | } |
||
| 320 | |||
| 321 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 322 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 323 | |||
| 324 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 325 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 326 | |||
| 327 | $wpinv_ip_address_country = $invoice->country; |
||
| 328 | |||
| 329 | $invoice->recalculate_totals(true); |
||
| 330 | |||
| 331 | $response = array(); |
||
| 332 | $response['success'] = true; |
||
| 333 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 334 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 335 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 336 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 337 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 338 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 339 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 340 | $response['data']['total'] = $invoice->get_total(); |
||
| 341 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 342 | |||
| 343 | wpinv_set_checkout_session($checkout_session); |
||
| 344 | |||
| 345 | wp_send_json( $response ); |
||
| 346 | } |
||
| 347 | |||
| 348 | public static function create_invoice_item() { |
||
| 349 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 350 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 351 | die(-1); |
||
| 352 | } |
||
| 353 | |||
| 354 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 355 | |||
| 356 | // Find the item |
||
| 357 | if ( !is_numeric( $invoice_id ) ) { |
||
| 358 | die(); |
||
| 359 | } |
||
| 360 | |||
| 361 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 362 | if ( empty( $invoice ) ) { |
||
| 363 | die(); |
||
| 364 | } |
||
| 365 | |||
| 366 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 367 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 368 | $response = array(); |
||
| 369 | $response['success'] = false; |
||
| 370 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 371 | wp_send_json( $response ); |
||
| 372 | } |
||
| 373 | |||
| 374 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 375 | |||
| 376 | $meta = array(); |
||
| 377 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 378 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 379 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 380 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 381 | |||
| 382 | $data = array(); |
||
| 383 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 384 | $data['post_status'] = 'publish'; |
||
| 385 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 386 | $data['meta'] = $meta; |
||
| 387 | |||
| 388 | $item = new WPInv_Item(); |
||
| 389 | $item->create( $data ); |
||
| 390 | |||
| 391 | if ( !empty( $item ) ) { |
||
| 392 | $_POST['item_id'] = $item->ID; |
||
| 393 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 394 | |||
| 395 | self::add_invoice_item(); |
||
| 396 | } |
||
| 397 | die(); |
||
| 398 | } |
||
| 399 | |||
| 400 | public static function get_billing_details() { |
||
| 401 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 402 | |||
| 403 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 404 | die(-1); |
||
| 405 | } |
||
| 406 | |||
| 407 | $user_id = (int)$_POST['user_id']; |
||
| 408 | $billing_details = wpinv_get_user_address($user_id); |
||
| 409 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 410 | |||
| 411 | if (isset($billing_details['user_id'])) { |
||
| 412 | unset($billing_details['user_id']); |
||
| 413 | } |
||
| 414 | |||
| 415 | if (isset($billing_details['email'])) { |
||
| 416 | unset($billing_details['email']); |
||
| 417 | } |
||
| 418 | |||
| 419 | $response = array(); |
||
| 420 | $response['success'] = true; |
||
| 421 | $response['data']['billing_details'] = $billing_details; |
||
| 422 | |||
| 423 | wp_send_json( $response ); |
||
| 424 | } |
||
| 425 | |||
| 426 | public static function admin_recalculate_totals() { |
||
| 427 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 428 | |||
| 429 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 430 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 431 | die(-1); |
||
| 432 | } |
||
| 433 | |||
| 434 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 435 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 436 | if ( empty( $invoice ) ) { |
||
| 437 | die(); |
||
| 438 | } |
||
| 439 | |||
| 440 | $checkout_session = wpinv_get_checkout_session(); |
||
| 441 | |||
| 442 | $data = array(); |
||
| 443 | $data['invoice_id'] = $invoice_id; |
||
| 444 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 445 | |||
| 446 | wpinv_set_checkout_session( $data ); |
||
| 447 | |||
| 448 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 449 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 450 | } |
||
| 451 | |||
| 452 | if ( empty( $_POST['country'] ) ) { |
||
| 453 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 454 | } |
||
| 455 | |||
| 456 | $disable_taxes = 0; |
||
| 457 | if ( ! empty( $_POST['disable_taxes'] ) ) { |
||
| 458 | $disable_taxes = 1; |
||
| 459 | } |
||
| 460 | $invoice->set( 'disable_taxes', $disable_taxes ); |
||
| 461 | |||
| 462 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 463 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 464 | if ( isset( $_POST['state'] ) ) { |
||
| 465 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 466 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 467 | } |
||
| 468 | |||
| 469 | $wpinv_ip_address_country = $invoice->country; |
||
| 470 | |||
| 471 | $invoice = $invoice->recalculate_totals(true); |
||
| 472 | |||
| 473 | $response = array(); |
||
| 474 | $response['success'] = true; |
||
| 475 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 476 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 477 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 478 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 479 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 480 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 481 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 482 | $response['data']['total'] = $invoice->get_total(); |
||
| 483 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 484 | |||
| 485 | wpinv_set_checkout_session($checkout_session); |
||
| 486 | |||
| 487 | wp_send_json( $response ); |
||
| 488 | } |
||
| 489 | |||
| 490 | public static function admin_apply_discount() { |
||
| 491 | global $wpi_userID; |
||
| 492 | |||
| 493 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 494 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 495 | die(-1); |
||
| 496 | } |
||
| 497 | |||
| 498 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 499 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 500 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 501 | die(); |
||
| 502 | } |
||
| 503 | |||
| 504 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 505 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 506 | die(); |
||
| 507 | } |
||
| 508 | |||
| 509 | $checkout_session = wpinv_get_checkout_session(); |
||
| 510 | |||
| 511 | $data = array(); |
||
| 512 | $data['invoice_id'] = $invoice_id; |
||
| 513 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 514 | |||
| 515 | wpinv_set_checkout_session( $data ); |
||
| 516 | |||
| 517 | $response = array(); |
||
| 518 | $response['success'] = false; |
||
| 519 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 520 | $response['data']['code'] = $discount_code; |
||
| 521 | |||
| 522 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 523 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 524 | |||
| 525 | $response['success'] = true; |
||
| 526 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 527 | } else { |
||
| 528 | $errors = wpinv_get_errors(); |
||
| 529 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 530 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 531 | } |
||
| 532 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 533 | } |
||
| 534 | |||
| 535 | wpinv_set_checkout_session($checkout_session); |
||
| 536 | |||
| 537 | wp_send_json( $response ); |
||
| 538 | } |
||
| 539 | |||
| 540 | public static function admin_remove_discount() { |
||
| 541 | global $wpi_userID; |
||
| 542 | |||
| 543 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 544 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 545 | die(-1); |
||
| 546 | } |
||
| 547 | |||
| 548 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 549 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 550 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 551 | die(); |
||
| 552 | } |
||
| 553 | |||
| 554 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 555 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 556 | die(); |
||
| 557 | } |
||
| 558 | |||
| 559 | $checkout_session = wpinv_get_checkout_session(); |
||
| 560 | |||
| 561 | $data = array(); |
||
| 562 | $data['invoice_id'] = $invoice_id; |
||
| 563 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 564 | |||
| 565 | wpinv_set_checkout_session( $data ); |
||
| 566 | |||
| 567 | $response = array(); |
||
| 568 | $response['success'] = false; |
||
| 569 | $response['msg'] = NULL; |
||
| 570 | |||
| 571 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 572 | $response['success'] = true; |
||
| 573 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 574 | |||
| 575 | wpinv_set_checkout_session($checkout_session); |
||
| 576 | |||
| 577 | wp_send_json( $response ); |
||
| 578 | } |
||
| 579 | |||
| 580 | public static function check_email() { |
||
| 581 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 582 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 583 | die(-1); |
||
| 584 | } |
||
| 585 | |||
| 586 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 587 | |||
| 588 | $response = array(); |
||
| 589 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 590 | $user_id = $user_data->ID; |
||
| 591 | $user_login = $user_data->user_login; |
||
| 592 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 593 | $billing_details = wpinv_get_user_address($user_id); |
||
| 594 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 595 | |||
| 596 | if (isset($billing_details['user_id'])) { |
||
| 597 | unset($billing_details['user_id']); |
||
| 598 | } |
||
| 599 | |||
| 600 | if (isset($billing_details['email'])) { |
||
| 601 | unset($billing_details['email']); |
||
| 602 | } |
||
| 603 | |||
| 604 | $response['success'] = true; |
||
| 605 | $response['data']['id'] = $user_data->ID; |
||
| 606 | $response['data']['name'] = $user_data->user_email; |
||
| 607 | $response['data']['billing_details'] = $billing_details; |
||
| 608 | } |
||
| 609 | |||
| 610 | wp_send_json( $response ); |
||
| 611 | } |
||
| 612 | |||
| 613 | public static function run_tool() { |
||
| 614 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 615 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 616 | die(-1); |
||
| 617 | } |
||
| 618 | |||
| 619 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 620 | |||
| 621 | do_action( 'wpinv_run_tool' ); |
||
| 622 | |||
| 623 | if ( !empty( $tool ) ) { |
||
| 624 | do_action( 'wpinv_tool_' . $tool ); |
||
| 625 | } |
||
| 626 | } |
||
| 627 | |||
| 628 | public static function apply_discount() { |
||
| 675 | } |
||
| 676 | |||
| 677 | public static function remove_discount() { |
||
| 678 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 679 | |||
| 680 | $response = array(); |
||
| 681 | |||
| 682 | if ( isset( $_POST['code'] ) ) { |
||
| 683 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 684 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 685 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 686 | $cart_totals = wpinv_recalculate_tax( true ); |
||
| 687 | |||
| 688 | if ( !empty( $cart_totals ) ) { |
||
| 689 | $response['success'] = true; |
||
| 690 | $response['data'] = $cart_totals; |
||
| 691 | $response['data']['code'] = $discount_code; |
||
| 692 | } else { |
||
| 693 | $response['success'] = false; |
||
| 694 | } |
||
| 695 | |||
| 696 | // Allow for custom discount code handling |
||
| 697 | $response = apply_filters( 'wpinv_ajax_discount_response', $response ); |
||
| 698 | } |
||
| 699 | |||
| 700 | wp_send_json( $response ); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Payment forms. |
||
| 705 | * |
||
| 706 | * @since 1.0.18 |
||
| 707 | */ |
||
| 708 | public static function payment_form() { |
||
| 909 | } |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Payment forms. |
||
| 913 | * |
||
| 914 | * @since 1.0.18 |
||
| 915 | */ |
||
| 916 | public static function payment_form_get_taxes() { |
||
| 917 | global $invoicing; |
||
| 918 | |||
| 919 | // Check nonce. |
||
| 920 | check_ajax_referer( 'wpinv_payment_form', 'wpinv_payment_form' ); |
||
| 921 | |||
| 922 | // Prepare submitted data... |
||
| 923 | $data = wp_unslash( $_POST ); |
||
| 924 | |||
| 925 | // ... form fields... |
||
| 926 | if ( empty( $data['form_id'] ) || 'publish' != get_post_status( $data['form_id'] ) ) { |
||
| 927 | exit; |
||
| 928 | } |
||
| 929 | |||
| 930 | // ... and form items. |
||
| 931 | $items = $invoicing->form_elements->get_form_items( $data['form_id'] ); |
||
| 932 | $total = 0; |
||
| 933 | $tax = 0; |
||
| 934 | $sub_total = 0; |
||
| 935 | |||
| 936 | if ( ! empty( $data['wpinv-items'] ) ) { |
||
| 937 | |||
| 938 | $selected_items = wpinv_clean( $data['wpinv-items'] ); |
||
| 939 | |||
| 940 | foreach ( $items as $item ) { |
||
| 941 | |||
| 942 | if ( ! isset( $selected_items[ $item['id'] ] ) ) { |
||
| 943 | continue; |
||
| 944 | } |
||
| 945 | |||
| 946 | $quantity = 1; |
||
| 947 | |||
| 948 | if ( ! empty( $item['allow_quantities'] ) && ! empty( $data["wpinv-item-{$item['id']}-quantity"] ) ) { |
||
| 949 | |||
| 950 | $quantity = intval( $data["wpinv-item-{$item['id']}-quantity"] ); |
||
| 951 | |||
| 952 | if ( 1 > $quantity ) { |
||
| 953 | $quantity = 1; |
||
| 954 | } |
||
| 955 | |||
| 956 | } |
||
| 957 | |||
| 958 | // Custom pricing. |
||
| 959 | $price = wpinv_sanitize_amount( $item['price'] ); |
||
| 960 | if ( ! empty( $item['custom_price'] ) ) { |
||
| 961 | |||
| 962 | $minimum_price = wpinv_sanitize_amount( $item['minimum_price'] ); |
||
| 963 | $set_price = wpinv_sanitize_amount( $selected_items[ $item['id'] ] ); |
||
| 964 | |||
| 965 | if ( $set_price < $minimum_price ) { |
||
| 966 | $set_price = $minimum_price; |
||
| 967 | } |
||
| 968 | |||
| 969 | $price = wpinv_sanitize_amount( $set_price ); |
||
| 970 | |||
| 971 | } |
||
| 972 | |||
| 973 | $price = $quantity * floatval( $price ); |
||
| 974 | |||
| 975 | if ( wpinv_use_taxes() ) { |
||
| 976 | |||
| 977 | $rate = wpinv_get_tax_rate( false, false, (int) $item['id'] ); |
||
| 978 | |||
| 979 | if ( ! wpinv_prices_include_tax() ) { |
||
| 980 | $pre_tax = ( $price - $price * $rate * 0.01 ); |
||
| 981 | $item_tax = $price - $pre_tax; |
||
| 982 | } else { |
||
| 983 | $item_tax = $price * $rate * 0.01; |
||
| 984 | } |
||
| 985 | |||
| 986 | $tax = $tax + $item_tax; |
||
| 987 | |||
| 988 | } |
||
| 989 | |||
| 990 | if ( wpinv_use_taxes() ) { |
||
| 991 | |||
| 992 | $rate = wpinv_get_tax_rate( false, false, (int) $item['id'] ); |
||
| 993 | |||
| 994 | if ( wpinv_prices_include_tax() ) { |
||
| 995 | $pre_tax = ( $price - $price * $rate * 0.01 ); |
||
| 996 | $item_tax = $price - $pre_tax; |
||
| 997 | } else { |
||
| 998 | $pre_tax = $price; |
||
| 999 | $item_tax = $price * $rate * 0.01; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | $tax = $tax + $item_tax; |
||
| 1003 | $sub_total = $sub_total + $pre_tax; |
||
| 1004 | $total = $sub_total + $tax; |
||
| 1005 | |||
| 1006 | } else { |
||
| 1007 | $total = $total + $price; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | } |
||
| 1011 | |||
| 1012 | } |
||
| 1013 | |||
| 1014 | wp_send_json_success( array( |
||
| 1015 | 'total' => wpinv_price( wpinv_format_amount( $total ) ), |
||
| 1016 | 'tax' => wpinv_price( wpinv_format_amount( $tax ) ), |
||
| 1017 | 'sub_total' => wpinv_price( wpinv_format_amount( $sub_total ) ), |
||
| 1018 | )); |
||
| 1019 | exit; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Lets users buy items via ajax. |
||
| 1024 | * |
||
| 1025 | * @since 1.0.0 |
||
| 1026 | */ |
||
| 1027 | public static function buy_items() { |
||
| 1167 | } |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | 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.