| Total Complexity | 121 | 
| Total Lines | 775 | 
| 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 | |||
| 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() { | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * Check for GetPaid Ajax request and fire action. | ||
| 59 | */ | ||
| 60 | 	public static function do_wpinv_ajax() { | ||
| 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( urldecode( $_GET['form'] ) ); | ||
| 271 | 		} else if( ! empty( $_GET['invoice'] ) ) { | ||
| 272 | getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) ); | ||
| 273 |         } else { | ||
| 274 | $items = getpaid_convert_items_to_array( urldecode( $_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 | wpinv_clean( $_GET['name'] ) | ||
| 359 | ); | ||
| 360 | |||
| 361 | wp_send_json_success( $html ); | ||
| 362 | exit; | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | } | ||
| 367 | |||
| 368 | exit; | ||
| 369 | } | ||
| 370 | |||
| 371 | /** | ||
| 372 | * Recalculates invoice totals. | ||
| 373 | */ | ||
| 374 |     public static function recalculate_invoice_totals() { | ||
| 427 | } | ||
| 428 | |||
| 429 | /** | ||
| 430 | * Get items belonging to a given invoice. | ||
| 431 | */ | ||
| 432 |     public static function get_invoice_items() { | ||
| 433 | |||
| 434 | // Verify nonce. | ||
| 435 | check_ajax_referer( 'wpinv-nonce' ); | ||
| 436 | |||
| 437 |         if ( ! wpinv_current_user_can_manage_invoicing() ) { | ||
| 438 | exit; | ||
| 439 | } | ||
| 440 | |||
| 441 | // We need an invoice and items. | ||
| 442 |         if ( empty( $_POST['post_id'] ) ) { | ||
| 443 | exit; | ||
| 444 | } | ||
| 445 | |||
| 446 | // Fetch the invoice. | ||
| 447 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); | ||
| 448 | |||
| 449 | // Ensure it exists. | ||
| 450 |         if ( ! $invoice->get_id() ) { | ||
| 451 | exit; | ||
| 452 | } | ||
| 453 | |||
| 454 | // Return an array of invoice items. | ||
| 455 | $items = array(); | ||
| 456 | |||
| 457 |         foreach ( $invoice->get_items() as $item_id => $item ) { | ||
| 458 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency(), $invoice->is_renewal() ); | ||
| 459 | } | ||
| 460 | |||
| 461 | wp_send_json_success( compact( 'items' ) ); | ||
| 462 | } | ||
| 463 | |||
| 464 | /** | ||
| 465 | * Edits an invoice item. | ||
| 466 | */ | ||
| 467 |     public static function edit_invoice_item() { | ||
| 468 | |||
| 469 | // Verify nonce. | ||
| 470 | check_ajax_referer( 'wpinv-nonce' ); | ||
| 471 | |||
| 472 |         if ( ! wpinv_current_user_can_manage_invoicing() ) { | ||
| 473 | exit; | ||
| 474 | } | ||
| 475 | |||
| 476 | // We need an invoice and item details. | ||
| 477 |         if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) { | ||
| 478 | exit; | ||
| 479 | } | ||
| 480 | |||
| 481 | // Fetch the invoice. | ||
| 482 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); | ||
| 483 | |||
| 484 | // Ensure it exists and its not been paid for. | ||
| 485 |         if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { | ||
| 486 | exit; | ||
| 487 | } | ||
| 488 | |||
| 489 | // Format the data. | ||
| 490 | $data = wp_list_pluck( $_POST['data'], 'value', 'field' ); | ||
| 491 | |||
| 492 | // Ensure that we have an item id. | ||
| 493 |         if ( empty( $data['id'] ) ) { | ||
| 494 | exit; | ||
| 495 | } | ||
| 496 | |||
| 497 | // Abort if the invoice does not have the specified item. | ||
| 498 | $item = $invoice->get_item( (int) $data['id'] ); | ||
| 499 | |||
| 500 |         if ( empty( $item ) ) { | ||
| 501 | exit; | ||
| 502 | } | ||
| 503 | |||
| 504 | // Update the item. | ||
| 505 | $item->set_price( $data['price'] ); | ||
| 506 | $item->set_name( $data['name'] ); | ||
| 507 | $item->set_description( $data['description'] ); | ||
| 508 | $item->set_quantity( $data['quantity'] ); | ||
| 509 | |||
| 510 | // Add it to the invoice. | ||
| 511 | $error = $invoice->add_item( $item ); | ||
| 512 | $alert = false; | ||
| 513 |         if ( is_wp_error( $error ) ) { | ||
| 514 | $alert = $error->get_error_message(); | ||
| 515 | } | ||
| 516 | |||
| 517 | // Update totals. | ||
| 518 | $invoice->recalculate_total(); | ||
| 519 | |||
| 520 | // Save the invoice. | ||
| 521 | $invoice->save(); | ||
| 522 | |||
| 523 | // Return an array of invoice items. | ||
| 524 | $items = array(); | ||
| 525 | |||
| 526 |         foreach ( $invoice->get_items() as $item_id => $item ) { | ||
| 527 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); | ||
| 528 | } | ||
| 529 | |||
| 530 | wp_send_json_success( compact( 'items', 'alert' ) ); | ||
| 531 | } | ||
| 532 | |||
| 533 | /** | ||
| 534 | * Deletes an invoice item. | ||
| 535 | */ | ||
| 536 |     public static function remove_invoice_item() { | ||
| 537 | |||
| 538 | // Verify nonce. | ||
| 539 | check_ajax_referer( 'wpinv-nonce' ); | ||
| 540 | |||
| 541 |         if ( ! wpinv_current_user_can_manage_invoicing() ) { | ||
| 542 | exit; | ||
| 543 | } | ||
| 544 | |||
| 545 | // We need an invoice and an item. | ||
| 546 |         if ( empty( $_POST['post_id'] ) || empty( $_POST['item_id'] ) ) { | ||
| 547 | exit; | ||
| 548 | } | ||
| 549 | |||
| 550 | // Fetch the invoice. | ||
| 551 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); | ||
| 552 | |||
| 553 | // Ensure it exists and its not been paid for. | ||
| 554 |         if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { | ||
| 555 | exit; | ||
| 556 | } | ||
| 557 | |||
| 558 | // Abort if the invoice does not have the specified item. | ||
| 559 | $item = $invoice->get_item( (int) $_POST['item_id'] ); | ||
| 560 | |||
| 561 |         if ( empty( $item ) ) { | ||
| 562 | exit; | ||
| 563 | } | ||
| 564 | |||
| 565 | $invoice->remove_item( (int) $_POST['item_id'] ); | ||
| 566 | |||
| 567 | // Update totals. | ||
| 568 | $invoice->recalculate_total(); | ||
| 569 | |||
| 570 | // Save the invoice. | ||
| 571 | $invoice->save(); | ||
| 572 | |||
| 573 | // Return an array of invoice items. | ||
| 574 | $items = array(); | ||
| 575 | |||
| 576 |         foreach ( $invoice->get_items() as $item_id => $item ) { | ||
| 577 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); | ||
| 578 | } | ||
| 579 | |||
| 580 | wp_send_json_success( compact( 'items' ) ); | ||
| 581 | } | ||
| 582 | |||
| 583 | /** | ||
| 584 | * Adds a items to an invoice. | ||
| 585 | */ | ||
| 586 |     public static function add_invoice_items() { | ||
| 587 | |||
| 588 | // Verify nonce. | ||
| 589 | check_ajax_referer( 'wpinv-nonce' ); | ||
| 590 | |||
| 591 |         if ( ! wpinv_current_user_can_manage_invoicing() ) { | ||
| 592 | exit; | ||
| 593 | } | ||
| 594 | |||
| 595 | // We need an invoice and items. | ||
| 596 |         if ( empty( $_POST['post_id'] ) || empty( $_POST['items'] ) ) { | ||
| 597 | exit; | ||
| 598 | } | ||
| 599 | |||
| 600 | // Fetch the invoice. | ||
| 601 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); | ||
| 602 | $alert = false; | ||
| 603 | |||
| 604 | // Ensure it exists and its not been paid for. | ||
| 605 |         if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { | ||
| 606 | exit; | ||
| 607 | } | ||
| 608 | |||
| 609 | // Add the items. | ||
| 610 |         foreach ( $_POST['items'] as $data ) { | ||
| 611 | |||
| 612 | $item = new GetPaid_Form_Item( $data[ 'id' ] ); | ||
| 613 | |||
| 614 |             if ( is_numeric( $data[ 'qty' ] ) && (float) $data[ 'qty' ] > 0 ) { | ||
| 615 | $item->set_quantity( $data[ 'qty' ] ); | ||
| 616 | } | ||
| 617 | |||
| 618 |             if ( $item->get_id() > 0 ) { | ||
| 619 | $error = $invoice->add_item( $item ); | ||
| 620 | |||
| 621 |                 if ( is_wp_error( $error ) ) { | ||
| 622 | $alert = $error->get_error_message(); | ||
| 623 | } | ||
| 624 | |||
| 625 | } | ||
| 626 | |||
| 627 | } | ||
| 628 | |||
| 629 | // Save the invoice. | ||
| 630 | $invoice->recalculate_total(); | ||
| 631 | $invoice->save(); | ||
| 632 | |||
| 633 | // Return an array of invoice items. | ||
| 634 | $items = array(); | ||
| 635 | |||
| 636 |         foreach ( $invoice->get_items() as $item_id => $item ) { | ||
| 637 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); | ||
| 638 | } | ||
| 639 | |||
| 640 | wp_send_json_success( compact( 'items', 'alert' ) ); | ||
| 641 | } | ||
| 642 | |||
| 643 | /** | ||
| 644 | * Retrieves items that should be added to an invoice. | ||
| 645 | */ | ||
| 646 |     public static function get_invoicing_items() { | ||
| 693 | |||
| 694 | } | ||
| 695 | |||
| 696 | /** | ||
| 697 | * Retrieves the states field for AUI forms. | ||
| 698 | */ | ||
| 699 |     public static function get_aui_states_field() { | ||
| 700 | |||
| 701 | // Verify nonce. | ||
| 702 | check_ajax_referer( 'wpinv-nonce' ); | ||
| 703 | |||
| 704 | // We need a country. | ||
| 705 |         if ( empty( $_GET['country'] ) ) { | ||
| 706 | exit; | ||
| 707 | } | ||
| 752 | ) | ||
| 753 | ); | ||
| 754 | |||
| 755 | } | ||
| 756 | |||
| 757 | /** | ||
| 758 | * Refresh prices. | ||
| 759 | * | ||
| 760 | * @since 1.0.19 | ||
| 761 | */ | ||
| 762 |     public static function payment_form_refresh_prices() { | ||
| 793 | WPInv_Ajax::init(); |