| Total Complexity | 156 |
| Total Lines | 826 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 | 'add_invoice_item' => false, |
||
| 67 | 'remove_invoice_item' => false, |
||
| 68 | 'create_invoice_item' => false, |
||
| 69 | 'get_billing_details' => false, |
||
| 70 | 'admin_recalculate_totals' => false, |
||
| 71 | 'admin_apply_discount' => false, |
||
| 72 | 'admin_remove_discount' => false, |
||
| 73 | 'check_email' => false, |
||
| 74 | 'run_tool' => false, |
||
| 75 | 'apply_discount' => true, |
||
| 76 | 'remove_discount' => true, |
||
| 77 | 'buy_items' => true, |
||
| 78 | ); |
||
| 79 | |||
| 80 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 81 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 82 | |||
| 83 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
| 84 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( $nopriv ) { |
||
| 88 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 89 | |||
| 90 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | public static function add_note() { |
||
| 96 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
| 97 | |||
| 98 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 99 | die(-1); |
||
| 100 | } |
||
| 101 | |||
| 102 | $post_id = absint( $_POST['post_id'] ); |
||
| 103 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
| 104 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
| 105 | |||
| 106 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 107 | |||
| 108 | if ( $post_id > 0 ) { |
||
| 109 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
| 110 | |||
| 111 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
| 112 | wpinv_get_invoice_note_line_item( $note_id ); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | die(); |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function delete_note() { |
||
| 120 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
| 121 | |||
| 122 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 123 | die(-1); |
||
| 124 | } |
||
| 125 | |||
| 126 | $note_id = (int)$_POST['note_id']; |
||
| 127 | |||
| 128 | if ( $note_id > 0 ) { |
||
| 129 | wp_delete_comment( $note_id, true ); |
||
| 130 | } |
||
| 131 | |||
| 132 | die(); |
||
| 133 | } |
||
| 134 | |||
| 135 | public static function get_states_field() { |
||
| 136 | echo wpinv_get_states_field(); |
||
| 137 | |||
| 138 | die(); |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function checkout() { |
||
| 142 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
| 143 | define( 'WPINV_CHECKOUT', true ); |
||
| 144 | } |
||
| 145 | |||
| 146 | wpinv_process_checkout(); |
||
| 147 | die(0); |
||
| 148 | } |
||
| 149 | |||
| 150 | public static function add_invoice_item() { |
||
| 258 | } |
||
| 259 | |||
| 260 | public static function remove_invoice_item() { |
||
| 261 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 262 | |||
| 263 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 264 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 343 | } |
||
| 344 | |||
| 345 | public static function create_invoice_item() { |
||
| 346 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 347 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 348 | die(-1); |
||
| 349 | } |
||
| 350 | |||
| 351 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 352 | |||
| 353 | // Find the item |
||
| 354 | if ( !is_numeric( $invoice_id ) ) { |
||
| 355 | die(); |
||
| 356 | } |
||
| 357 | |||
| 358 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 359 | if ( empty( $invoice ) ) { |
||
| 360 | die(); |
||
| 361 | } |
||
| 362 | |||
| 363 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 364 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 365 | $response = array(); |
||
| 366 | $response['success'] = false; |
||
| 367 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 368 | wp_send_json( $response ); |
||
| 369 | } |
||
| 370 | |||
| 371 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 372 | |||
| 373 | $meta = array(); |
||
| 374 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 375 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 376 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 377 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 378 | |||
| 379 | $data = array(); |
||
| 380 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 381 | $data['post_status'] = 'publish'; |
||
| 382 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 383 | $data['meta'] = $meta; |
||
| 384 | |||
| 385 | $item = new WPInv_Item(); |
||
| 386 | $item->create( $data ); |
||
| 387 | |||
| 388 | if ( !empty( $item ) ) { |
||
| 389 | $_POST['item_id'] = $item->ID; |
||
| 390 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 391 | |||
| 392 | self::add_invoice_item(); |
||
| 393 | } |
||
| 394 | die(); |
||
| 395 | } |
||
| 396 | |||
| 397 | public static function get_billing_details() { |
||
| 398 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 399 | |||
| 400 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 401 | die(-1); |
||
| 402 | } |
||
| 403 | |||
| 404 | $user_id = (int)$_POST['user_id']; |
||
| 405 | $billing_details = wpinv_get_user_address($user_id); |
||
| 406 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 407 | |||
| 408 | if (isset($billing_details['user_id'])) { |
||
| 409 | unset($billing_details['user_id']); |
||
| 410 | } |
||
| 411 | |||
| 412 | if (isset($billing_details['email'])) { |
||
| 413 | unset($billing_details['email']); |
||
| 414 | } |
||
| 415 | |||
| 416 | $response = array(); |
||
| 417 | $response['success'] = true; |
||
| 418 | $response['data']['billing_details'] = $billing_details; |
||
| 419 | |||
| 420 | wp_send_json( $response ); |
||
| 421 | } |
||
| 422 | |||
| 423 | public static function admin_recalculate_totals() { |
||
| 424 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 425 | |||
| 426 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 427 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 428 | die(-1); |
||
| 429 | } |
||
| 430 | |||
| 431 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 432 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 433 | if ( empty( $invoice ) ) { |
||
| 434 | die(); |
||
| 435 | } |
||
| 436 | |||
| 437 | $checkout_session = wpinv_get_checkout_session(); |
||
| 438 | |||
| 439 | $data = array(); |
||
| 440 | $data['invoice_id'] = $invoice_id; |
||
| 441 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 442 | |||
| 443 | wpinv_set_checkout_session( $data ); |
||
| 444 | |||
| 445 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 446 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 447 | } |
||
| 448 | |||
| 449 | if ( empty( $_POST['country'] ) ) { |
||
| 450 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 451 | } |
||
| 452 | |||
| 453 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 454 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 455 | if ( isset( $_POST['state'] ) ) { |
||
| 456 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 457 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 458 | } |
||
| 459 | |||
| 460 | $wpinv_ip_address_country = $invoice->country; |
||
| 461 | |||
| 462 | $invoice = $invoice->recalculate_totals(true); |
||
| 463 | |||
| 464 | $response = array(); |
||
| 465 | $response['success'] = true; |
||
| 466 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 467 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 468 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 469 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 470 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 471 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 472 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 473 | $response['data']['total'] = $invoice->get_total(); |
||
| 474 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 475 | |||
| 476 | wpinv_set_checkout_session($checkout_session); |
||
| 477 | |||
| 478 | wp_send_json( $response ); |
||
| 479 | } |
||
| 480 | |||
| 481 | public static function admin_apply_discount() { |
||
| 482 | global $wpi_userID; |
||
| 483 | |||
| 484 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 485 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 486 | die(-1); |
||
| 487 | } |
||
| 488 | |||
| 489 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 490 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 491 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 492 | die(); |
||
| 493 | } |
||
| 494 | |||
| 495 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 496 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 497 | die(); |
||
| 498 | } |
||
| 499 | |||
| 500 | $checkout_session = wpinv_get_checkout_session(); |
||
| 501 | |||
| 502 | $data = array(); |
||
| 503 | $data['invoice_id'] = $invoice_id; |
||
| 504 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 505 | |||
| 506 | wpinv_set_checkout_session( $data ); |
||
| 507 | |||
| 508 | $response = array(); |
||
| 509 | $response['success'] = false; |
||
| 510 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 511 | $response['data']['code'] = $discount_code; |
||
| 512 | |||
| 513 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 514 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 515 | |||
| 516 | $response['success'] = true; |
||
| 517 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 518 | } else { |
||
| 519 | $errors = wpinv_get_errors(); |
||
| 520 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 521 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 522 | } |
||
| 523 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 524 | } |
||
| 525 | |||
| 526 | wpinv_set_checkout_session($checkout_session); |
||
| 527 | |||
| 528 | wp_send_json( $response ); |
||
| 529 | } |
||
| 530 | |||
| 531 | public static function admin_remove_discount() { |
||
| 532 | global $wpi_userID; |
||
| 533 | |||
| 534 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 535 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 536 | die(-1); |
||
| 537 | } |
||
| 538 | |||
| 539 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 540 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 541 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 542 | die(); |
||
| 543 | } |
||
| 544 | |||
| 545 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 546 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 547 | die(); |
||
| 548 | } |
||
| 549 | |||
| 550 | $checkout_session = wpinv_get_checkout_session(); |
||
| 551 | |||
| 552 | $data = array(); |
||
| 553 | $data['invoice_id'] = $invoice_id; |
||
| 554 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 555 | |||
| 556 | wpinv_set_checkout_session( $data ); |
||
| 557 | |||
| 558 | $response = array(); |
||
| 559 | $response['success'] = false; |
||
| 560 | $response['msg'] = NULL; |
||
| 561 | |||
| 562 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 563 | $response['success'] = true; |
||
| 564 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 565 | |||
| 566 | wpinv_set_checkout_session($checkout_session); |
||
| 567 | |||
| 568 | wp_send_json( $response ); |
||
| 569 | } |
||
| 570 | |||
| 571 | public static function check_email() { |
||
| 572 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 573 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 574 | die(-1); |
||
| 575 | } |
||
| 576 | |||
| 577 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 578 | |||
| 579 | $response = array(); |
||
| 580 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 581 | $user_id = $user_data->ID; |
||
| 582 | $user_login = $user_data->user_login; |
||
| 583 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 584 | $billing_details = wpinv_get_user_address($user_id); |
||
| 585 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 586 | |||
| 587 | if (isset($billing_details['user_id'])) { |
||
| 588 | unset($billing_details['user_id']); |
||
| 589 | } |
||
| 590 | |||
| 591 | if (isset($billing_details['email'])) { |
||
| 592 | unset($billing_details['email']); |
||
| 593 | } |
||
| 594 | |||
| 595 | $response['success'] = true; |
||
| 596 | $response['data']['id'] = $user_data->ID; |
||
| 597 | $response['data']['name'] = $user_data->user_email; |
||
| 598 | $response['data']['billing_details'] = $billing_details; |
||
| 599 | } |
||
| 600 | |||
| 601 | wp_send_json( $response ); |
||
| 602 | } |
||
| 603 | |||
| 604 | public static function run_tool() { |
||
| 605 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 606 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 607 | die(-1); |
||
| 608 | } |
||
| 609 | |||
| 610 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 611 | |||
| 612 | do_action( 'wpinv_run_tool' ); |
||
| 613 | |||
| 614 | if ( !empty( $tool ) ) { |
||
| 615 | do_action( 'wpinv_tool_' . $tool ); |
||
| 616 | } |
||
| 617 | } |
||
| 618 | |||
| 619 | public static function apply_discount() { |
||
| 666 | } |
||
| 667 | |||
| 668 | public static function remove_discount() { |
||
| 669 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 670 | |||
| 671 | $response = array(); |
||
| 672 | |||
| 673 | if ( isset( $_POST['code'] ) ) { |
||
| 674 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 675 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 676 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 677 | $cart_totals = wpinv_recalculate_tax( true ); |
||
| 678 | |||
| 679 | if ( !empty( $cart_totals ) ) { |
||
| 680 | $response['success'] = true; |
||
| 681 | $response['data'] = $cart_totals; |
||
| 682 | $response['data']['code'] = $discount_code; |
||
| 683 | } else { |
||
| 684 | $response['success'] = false; |
||
| 685 | } |
||
| 686 | |||
| 687 | // Allow for custom discount code handling |
||
| 688 | $response = apply_filters( 'wpinv_ajax_discount_response', $response ); |
||
| 689 | } |
||
| 690 | |||
| 691 | wp_send_json( $response ); |
||
| 692 | } |
||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * Lets users buy items via ajax. |
||
| 697 | * |
||
| 698 | * @since 1.0.0 |
||
| 699 | */ |
||
| 700 | public static function buy_items() { |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | 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.