| Total Complexity | 139 |
| Total Lines | 896 |
| 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_customers' => false, |
||
| 94 | 'get_invoice_items' => false, |
||
| 95 | 'add_invoice_items' => false, |
||
| 96 | 'edit_invoice_item' => false, |
||
| 97 | 'create_invoice_item' => false, |
||
| 98 | 'remove_invoice_item' => false, |
||
| 99 | 'get_billing_details' => false, |
||
| 100 | 'recalculate_invoice_totals' => false, |
||
| 101 | 'check_new_user_email' => false, |
||
| 102 | 'run_tool' => false, |
||
| 103 | 'payment_form_refresh_prices' => true, |
||
| 104 | ); |
||
| 105 | |||
| 106 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 107 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 108 | add_action( 'wp_ajax_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 109 | |||
| 110 | if ( $nopriv ) { |
||
| 111 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 112 | add_action( 'wp_ajax_nopriv_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 113 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | public static function add_note() { |
||
| 140 | } |
||
| 141 | |||
| 142 | public static function delete_note() { |
||
| 156 | } |
||
| 157 | |||
| 158 | public static function get_states_field() { |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Retrieves a given user's billing address. |
||
| 166 | */ |
||
| 167 | public static function get_billing_details() { |
||
| 168 | |||
| 169 | // Verify nonce. |
||
| 170 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 171 | |||
| 172 | // Can the user manage the plugin? |
||
| 173 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 174 | die(-1); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Do we have a user id? |
||
| 178 | $user_id = $_GET['user_id']; |
||
| 179 | |||
| 180 | if ( empty( $user_id ) || ! is_numeric( $user_id ) ) { |
||
| 181 | die(-1); |
||
| 182 | } |
||
| 183 | |||
| 184 | // Fetch the billing details. |
||
| 185 | $billing_details = wpinv_get_user_address( $user_id ); |
||
| 186 | $billing_details = apply_filters( 'wpinv_ajax_billing_details', $billing_details, $user_id ); |
||
| 187 | |||
| 188 | // unset the user id and email. |
||
| 189 | $to_ignore = array( 'user_id', 'email' ); |
||
| 190 | |||
| 191 | foreach ( $to_ignore as $key ) { |
||
| 192 | if ( isset( $billing_details[ $key ] ) ) { |
||
| 193 | unset( $billing_details[ $key ] ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | wp_send_json_success( $billing_details ); |
||
| 198 | |||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Checks if a new users email is valid. |
||
| 203 | */ |
||
| 204 | public static function check_new_user_email() { |
||
| 205 | |||
| 206 | // Verify nonce. |
||
| 207 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 208 | |||
| 209 | // Can the user manage the plugin? |
||
| 210 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 211 | die(-1); |
||
| 212 | } |
||
| 213 | |||
| 214 | // We need an email address. |
||
| 215 | if ( empty( $_GET['email'] ) ) { |
||
| 216 | _e( "Provide the new user's email address", 'invoicing' ); |
||
| 217 | exit; |
||
| 218 | } |
||
| 219 | |||
| 220 | // Ensure the email is valid. |
||
| 221 | $email = sanitize_text_field( $_GET['email'] ); |
||
| 222 | if ( ! is_email( $email ) ) { |
||
| 223 | _e( 'Invalid email address', 'invoicing' ); |
||
| 224 | exit; |
||
| 225 | } |
||
| 226 | |||
| 227 | // And it does not exist. |
||
| 228 | $id = email_exists( $email ); |
||
| 229 | if ( $id ) { |
||
| 230 | wp_send_json_success( compact( 'id' ) ); |
||
| 231 | } |
||
| 232 | |||
| 233 | wp_send_json_success( true ); |
||
| 234 | } |
||
| 235 | |||
| 236 | public static function run_tool() { |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Retrieves the markup for a payment form. |
||
| 253 | */ |
||
| 254 | public static function get_payment_form() { |
||
| 255 | |||
| 256 | // Check nonce. |
||
| 257 | check_ajax_referer( 'getpaid_form_nonce' ); |
||
| 258 | |||
| 259 | // Is the request set up correctly? |
||
| 260 | if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) { |
||
| 261 | echo aui()->alert( |
||
| 262 | array( |
||
| 263 | 'type' => 'warning', |
||
| 264 | 'content' => __( 'No payment form or item provided', 'invoicing' ), |
||
| 265 | ) |
||
| 266 | ); |
||
| 267 | exit; |
||
| 268 | } |
||
| 269 | |||
| 270 | // Payment form or button? |
||
| 271 | if ( ! empty( $_GET['form'] ) ) { |
||
| 272 | getpaid_display_payment_form( urldecode( $_GET['form'] ) ); |
||
| 273 | } else if( ! empty( $_GET['invoice'] ) ) { |
||
| 274 | getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) ); |
||
| 275 | } else { |
||
| 276 | $items = getpaid_convert_items_to_array( urldecode( $_GET['item'] ) ); |
||
| 277 | getpaid_display_item_payment_form( $items ); |
||
| 278 | } |
||
| 279 | |||
| 280 | exit; |
||
| 281 | |||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Payment forms. |
||
| 286 | * |
||
| 287 | * @since 1.0.18 |
||
| 288 | */ |
||
| 289 | public static function payment_form() { |
||
| 290 | |||
| 291 | // Check nonce. |
||
| 292 | check_ajax_referer( 'getpaid_form_nonce' ); |
||
| 293 | |||
| 294 | // ... form fields... |
||
| 295 | if ( empty( $_POST['getpaid_payment_form_submission'] ) ) { |
||
| 296 | _e( 'Error: Reload the page and try again.', 'invoicing' ); |
||
| 297 | exit; |
||
| 298 | } |
||
| 299 | |||
| 300 | // Process the payment form. |
||
| 301 | $checkout_class = apply_filters( 'getpaid_checkout_class', 'GetPaid_Checkout' ); |
||
| 302 | $checkout = new $checkout_class( new GetPaid_Payment_Form_Submission() ); |
||
| 303 | $checkout->process_checkout(); |
||
| 304 | |||
| 305 | exit; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Payment forms. |
||
| 310 | * |
||
| 311 | * @since 1.0.18 |
||
| 312 | */ |
||
| 313 | public static function get_payment_form_states_field() { |
||
| 314 | |||
| 315 | if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
||
| 316 | exit; |
||
| 317 | } |
||
| 318 | |||
| 319 | $elements = getpaid_get_payment_form_elements( $_GET['form'] ); |
||
| 320 | |||
| 321 | if ( empty( $elements ) ) { |
||
| 322 | exit; |
||
| 323 | } |
||
| 324 | |||
| 325 | $address_fields = array(); |
||
| 326 | foreach ( $elements as $element ) { |
||
| 327 | if ( 'address' === $element['type'] ) { |
||
| 328 | $address_fields = $element; |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | if ( empty( $address_fields ) ) { |
||
| 334 | exit; |
||
| 335 | } |
||
| 336 | |||
| 337 | foreach ( $address_fields['fields'] as $address_field ) { |
||
| 338 | |||
| 339 | if ( 'wpinv_state' == $address_field['name'] ) { |
||
| 340 | |||
| 341 | $wrap_class = getpaid_get_form_element_grid_class( $address_field ); |
||
| 342 | $wrap_class = esc_attr( "$wrap_class getpaid-address-field-wrapper" ); |
||
| 343 | $placeholder = empty( $address_field['placeholder'] ) ? '' : esc_attr( $address_field['placeholder'] ); |
||
| 344 | $description = empty( $address_field['description'] ) ? '' : wp_kses_post( $address_field['description'] ); |
||
| 345 | $value = is_user_logged_in() ? get_user_meta( get_current_user_id(), '_wpinv_state', true ) : ''; |
||
| 346 | $label = empty( $address_field['label'] ) ? '' : wp_kses_post( $address_field['label'] ); |
||
| 347 | |||
| 348 | if ( ! empty( $address_field['required'] ) ) { |
||
| 349 | $label .= "<span class='text-danger'> *</span>"; |
||
| 350 | } |
||
| 351 | |||
| 352 | $html = getpaid_get_states_select_markup ( |
||
| 353 | sanitize_text_field( $_GET['country'] ), |
||
| 354 | $value, |
||
| 355 | $placeholder, |
||
| 356 | $label, |
||
| 357 | $description, |
||
| 358 | ! empty( $address_field['required'] ), |
||
| 359 | $wrap_class, |
||
| 360 | wpinv_clean( $_GET['name'] ) |
||
| 361 | ); |
||
| 362 | |||
| 363 | wp_send_json_success( $html ); |
||
| 364 | exit; |
||
| 365 | |||
| 366 | } |
||
| 367 | |||
| 368 | } |
||
| 369 | |||
| 370 | exit; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Recalculates invoice totals. |
||
| 375 | */ |
||
| 376 | public static function recalculate_invoice_totals() { |
||
| 377 | |||
| 378 | // Verify nonce. |
||
| 379 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 380 | |||
| 381 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 382 | exit; |
||
| 383 | } |
||
| 384 | |||
| 385 | // We need an invoice. |
||
| 386 | if ( empty( $_POST['post_id'] ) ) { |
||
| 387 | exit; |
||
| 388 | } |
||
| 389 | |||
| 390 | // Fetch the invoice. |
||
| 391 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 392 | |||
| 393 | // Ensure it exists. |
||
| 394 | if ( ! $invoice->get_id() ) { |
||
| 395 | exit; |
||
| 396 | } |
||
| 397 | |||
| 398 | // Maybe set the country, state, currency. |
||
| 399 | foreach ( array( 'country', 'state', 'currency', 'vat_number', 'discount_code' ) as $key ) { |
||
| 400 | if ( isset( $_POST[ $key ] ) ) { |
||
| 401 | $method = "set_$key"; |
||
| 402 | $invoice->$method( sanitize_text_field( $_POST[ $key ] ) ); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | // Maybe disable taxes. |
||
| 407 | $invoice->set_disable_taxes( ! empty( $_POST['taxes'] ) ); |
||
| 408 | |||
| 409 | // Discount code. |
||
| 410 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
| 411 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
| 412 | if ( $discount->exists() ) { |
||
| 413 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
| 414 | } else { |
||
| 415 | $invoice->remove_discount( 'discount_code' ); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | // Recalculate totals. |
||
| 420 | $invoice->recalculate_total(); |
||
| 421 | |||
| 422 | $total = wpinv_price( $invoice->get_total(), $invoice->get_currency() ); |
||
| 423 | $suscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
||
| 424 | if ( is_a( $suscriptions, 'WPInv_Subscription' ) && $invoice->is_recurring() && $invoice->is_parent() && $invoice->get_total() != $invoice->get_recurring_total() ) { |
||
| 425 | $recurring_total = wpinv_price( $invoice->get_recurring_total(), $invoice->get_currency() ); |
||
| 426 | $total .= '<small class="form-text text-muted">' . sprintf( __( 'Recurring Price: %s', 'invoicing' ), $recurring_total ) . '</small>'; |
||
| 427 | } |
||
| 428 | |||
| 429 | $totals = array( |
||
| 430 | 'subtotal' => wpinv_price( $invoice->get_subtotal(), $invoice->get_currency() ), |
||
| 431 | 'discount' => wpinv_price( $invoice->get_total_discount(), $invoice->get_currency() ), |
||
| 432 | 'tax' => wpinv_price( $invoice->get_total_tax(), $invoice->get_currency() ), |
||
| 433 | 'total' => $total, |
||
| 434 | ); |
||
| 435 | |||
| 436 | $totals = apply_filters( 'getpaid_invoice_totals', $totals, $invoice ); |
||
| 437 | |||
| 438 | wp_send_json_success( compact( 'totals' ) ); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get items belonging to a given invoice. |
||
| 443 | */ |
||
| 444 | public static function get_invoice_items() { |
||
| 445 | |||
| 446 | // Verify nonce. |
||
| 447 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 448 | |||
| 449 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 450 | exit; |
||
| 451 | } |
||
| 452 | |||
| 453 | // We need an invoice and items. |
||
| 454 | if ( empty( $_POST['post_id'] ) ) { |
||
| 455 | exit; |
||
| 456 | } |
||
| 457 | |||
| 458 | // Fetch the invoice. |
||
| 459 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 460 | |||
| 461 | // Ensure it exists. |
||
| 462 | if ( ! $invoice->get_id() ) { |
||
| 463 | exit; |
||
| 464 | } |
||
| 465 | |||
| 466 | // Return an array of invoice items. |
||
| 467 | $items = array(); |
||
| 468 | |||
| 469 | foreach ( $invoice->get_items() as $item ) { |
||
| 470 | $items[] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency(), $invoice->is_renewal() ); |
||
| 471 | } |
||
| 472 | |||
| 473 | wp_send_json_success( compact( 'items' ) ); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Edits an invoice item. |
||
| 478 | */ |
||
| 479 | public static function edit_invoice_item() { |
||
| 480 | |||
| 481 | // Verify nonce. |
||
| 482 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 483 | |||
| 484 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 485 | exit; |
||
| 486 | } |
||
| 487 | |||
| 488 | // We need an invoice and item details. |
||
| 489 | if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) { |
||
| 490 | exit; |
||
| 491 | } |
||
| 492 | |||
| 493 | // Fetch the invoice. |
||
| 494 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 495 | |||
| 496 | // Ensure it exists and its not been paid for. |
||
| 497 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 498 | exit; |
||
| 499 | } |
||
| 500 | |||
| 501 | // Format the data. |
||
| 502 | $data = wp_unslash( wp_list_pluck( $_POST['data'], 'value', 'field' ) ); |
||
| 503 | |||
| 504 | // Ensure that we have an item id. |
||
| 505 | if ( empty( $data['id'] ) ) { |
||
| 506 | exit; |
||
| 507 | } |
||
| 508 | |||
| 509 | // Abort if the invoice does not have the specified item. |
||
| 510 | $item = $invoice->get_item( (int) $data['id'] ); |
||
| 511 | |||
| 512 | if ( empty( $item ) ) { |
||
| 513 | exit; |
||
| 514 | } |
||
| 515 | |||
| 516 | // Update the item. |
||
| 517 | $item->set_price( floatval( $data['price'] ) ); |
||
| 518 | $item->set_name( sanitize_text_field( $data['name'] ) ); |
||
| 519 | $item->set_description( wp_kses_post( $data['description'] ) ); |
||
| 520 | $item->set_quantity( floatval( $data['quantity'] ) ); |
||
| 521 | |||
| 522 | // Add it to the invoice. |
||
| 523 | $error = $invoice->add_item( $item ); |
||
| 524 | $alert = false; |
||
| 525 | if ( is_wp_error( $error ) ) { |
||
| 526 | $alert = $error->get_error_message(); |
||
| 527 | } |
||
| 528 | |||
| 529 | // Update totals. |
||
| 530 | $invoice->recalculate_total(); |
||
| 531 | |||
| 532 | // Save the invoice. |
||
| 533 | $invoice->save(); |
||
| 534 | |||
| 535 | // Return an array of invoice items. |
||
| 536 | $items = array(); |
||
| 537 | |||
| 538 | foreach ( $invoice->get_items() as $item ) { |
||
| 539 | $items[] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
| 540 | } |
||
| 541 | |||
| 542 | wp_send_json_success( compact( 'items', 'alert' ) ); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Creates an invoice item. |
||
| 547 | */ |
||
| 548 | public static function create_invoice_item() { |
||
| 549 | |||
| 550 | // Verify nonce. |
||
| 551 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 552 | |||
| 553 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 554 | exit; |
||
| 555 | } |
||
| 556 | |||
| 557 | // We need an invoice and item details. |
||
| 558 | if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) { |
||
| 559 | exit; |
||
| 560 | } |
||
| 561 | |||
| 562 | // Fetch the invoice. |
||
| 563 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 564 | |||
| 565 | // Ensure it exists and its not been paid for. |
||
| 566 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 567 | exit; |
||
| 568 | } |
||
| 569 | |||
| 570 | // Format the data. |
||
| 571 | $data = wp_unslash( wp_list_pluck( $_POST['data'], 'value', 'field' ) ); |
||
| 572 | |||
| 573 | $item = new WPInv_Item(); |
||
| 574 | $item->set_price( floatval( $data['price'] ) ); |
||
| 575 | $item->set_name( sanitize_text_field( $data['name'] ) ); |
||
| 576 | $item->set_description( wp_kses_post( $data['description'] ) ); |
||
| 577 | $item->set_status( 'publish' ); |
||
| 578 | $item->save(); |
||
| 579 | |||
| 580 | if ( ! $item->exists() ) { |
||
| 581 | $alert = __( 'Could not create invoice item. Please try again.', 'invoicing' ); |
||
| 582 | } else { |
||
| 583 | $item = new GetPaid_Form_Item( $item->get_id() ); |
||
| 584 | $item->set_quantity( floatval( $data['quantity'] ) ); |
||
| 585 | |||
| 586 | // Add it to the invoice. |
||
| 587 | $error = $invoice->add_item( $item ); |
||
| 588 | $alert = false; |
||
| 589 | if ( is_wp_error( $error ) ) { |
||
| 590 | $alert = $error->get_error_message(); |
||
| 591 | } |
||
| 592 | |||
| 593 | // Update totals. |
||
| 594 | $invoice->recalculate_total(); |
||
| 595 | |||
| 596 | // Save the invoice. |
||
| 597 | $invoice->save(); |
||
| 598 | |||
| 599 | } |
||
| 600 | |||
| 601 | // Return an array of invoice items. |
||
| 602 | $items = array(); |
||
| 603 | |||
| 604 | foreach ( $invoice->get_items() as $item ) { |
||
| 605 | $items[] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
| 606 | } |
||
| 607 | |||
| 608 | wp_send_json_success( compact( 'items', 'alert' ) ); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Deletes an invoice item. |
||
| 613 | */ |
||
| 614 | public static function remove_invoice_item() { |
||
| 615 | |||
| 616 | // Verify nonce. |
||
| 617 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 618 | |||
| 619 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 620 | exit; |
||
| 621 | } |
||
| 622 | |||
| 623 | // We need an invoice and an item. |
||
| 624 | if ( empty( $_POST['post_id'] ) || empty( $_POST['item_id'] ) ) { |
||
| 625 | exit; |
||
| 626 | } |
||
| 627 | |||
| 628 | // Fetch the invoice. |
||
| 629 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 630 | |||
| 631 | // Ensure it exists and its not been paid for. |
||
| 632 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 633 | exit; |
||
| 634 | } |
||
| 635 | |||
| 636 | // Abort if the invoice does not have the specified item. |
||
| 637 | $item = $invoice->get_item( (int) $_POST['item_id'] ); |
||
| 638 | |||
| 639 | if ( empty( $item ) ) { |
||
| 640 | exit; |
||
| 641 | } |
||
| 642 | |||
| 643 | $invoice->remove_item( (int) $_POST['item_id'] ); |
||
| 644 | |||
| 645 | // Update totals. |
||
| 646 | $invoice->recalculate_total(); |
||
| 647 | |||
| 648 | // Save the invoice. |
||
| 649 | $invoice->save(); |
||
| 650 | |||
| 651 | // Return an array of invoice items. |
||
| 652 | $items = array(); |
||
| 653 | |||
| 654 | foreach ( $invoice->get_items() as $item ) { |
||
| 655 | $items[] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
| 656 | } |
||
| 657 | |||
| 658 | wp_send_json_success( compact( 'items' ) ); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Adds a items to an invoice. |
||
| 663 | */ |
||
| 664 | public static function add_invoice_items() { |
||
| 665 | |||
| 666 | // Verify nonce. |
||
| 667 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 668 | |||
| 669 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 670 | exit; |
||
| 671 | } |
||
| 672 | |||
| 673 | // We need an invoice and items. |
||
| 674 | if ( empty( $_POST['post_id'] ) || empty( $_POST['items'] ) ) { |
||
| 675 | exit; |
||
| 676 | } |
||
| 677 | |||
| 678 | // Fetch the invoice. |
||
| 679 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
| 680 | $alert = false; |
||
| 681 | |||
| 682 | // Ensure it exists and its not been paid for. |
||
| 683 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 684 | exit; |
||
| 685 | } |
||
| 686 | |||
| 687 | // Add the items. |
||
| 688 | foreach ( $_POST['items'] as $data ) { |
||
| 689 | |||
| 690 | $item = new GetPaid_Form_Item( $data[ 'id' ] ); |
||
| 691 | |||
| 692 | if ( is_numeric( $data[ 'qty' ] ) && (float) $data[ 'qty' ] > 0 ) { |
||
| 693 | $item->set_quantity( $data[ 'qty' ] ); |
||
| 694 | } |
||
| 695 | |||
| 696 | if ( $item->get_id() > 0 ) { |
||
| 697 | $error = $invoice->add_item( $item ); |
||
| 698 | |||
| 699 | if ( is_wp_error( $error ) ) { |
||
| 700 | $alert = $error->get_error_message(); |
||
| 701 | } |
||
| 702 | |||
| 703 | } |
||
| 704 | |||
| 705 | } |
||
| 706 | |||
| 707 | // Save the invoice. |
||
| 708 | $invoice->recalculate_total(); |
||
| 709 | $invoice->save(); |
||
| 710 | |||
| 711 | // Return an array of invoice items. |
||
| 712 | $items = array(); |
||
| 713 | |||
| 714 | foreach ( $invoice->get_items() as $item ) { |
||
| 715 | $items[] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
| 716 | } |
||
| 717 | |||
| 718 | wp_send_json_success( compact( 'items', 'alert' ) ); |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Retrieves items that should be added to an invoice. |
||
| 723 | */ |
||
| 724 | public static function get_invoicing_items() { |
||
| 725 | |||
| 726 | // Verify nonce. |
||
| 727 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 728 | |||
| 729 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 730 | exit; |
||
| 731 | } |
||
| 732 | |||
| 733 | // We need a search term. |
||
| 734 | if ( empty( $_GET['search'] ) ) { |
||
| 735 | wp_send_json_success( array() ); |
||
| 736 | } |
||
| 737 | |||
| 738 | // Retrieve items. |
||
| 739 | $item_args = array( |
||
| 740 | 'post_type' => 'wpi_item', |
||
| 741 | 'orderby' => 'title', |
||
| 742 | 'order' => 'ASC', |
||
| 743 | 'posts_per_page' => -1, |
||
| 744 | 'post_status' => array( 'publish' ), |
||
| 745 | 's' => trim( $_GET['search'] ), |
||
| 746 | 'meta_query' => array( |
||
| 747 | array( |
||
| 748 | 'key' => '_wpinv_type', |
||
| 749 | 'compare' => '!=', |
||
| 750 | 'value' => 'package' |
||
| 751 | ) |
||
| 752 | ) |
||
| 753 | ); |
||
| 754 | |||
| 755 | $items = get_posts( apply_filters( 'getpaid_ajax_invoice_items_query_args', $item_args ) ); |
||
| 756 | $data = array(); |
||
| 757 | |||
| 758 | $is_payment_form = ( ! empty( $_GET['post_id'] ) && 'wpi_payment_form' == get_post_type( $_GET['post_id'] ) ); |
||
| 759 | |||
| 760 | foreach ( $items as $item ) { |
||
| 761 | $item = new GetPaid_Form_Item( $item ); |
||
| 762 | $data[] = array( |
||
| 763 | 'id' => (int) $item->get_id(), |
||
| 764 | 'text' => strip_tags( $item->get_name() ), |
||
| 765 | 'form_data' => $is_payment_form ? $item->prepare_data_for_use( false ) : '', |
||
| 766 | ); |
||
| 767 | } |
||
| 768 | |||
| 769 | wp_send_json_success( $data ); |
||
| 770 | |||
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Retrieves items that should be added to an invoice. |
||
| 775 | */ |
||
| 776 | public static function get_customers() { |
||
| 777 | |||
| 778 | // Verify nonce. |
||
| 779 | check_ajax_referer( 'wpinv-nonce' ); |
||
| 780 | |||
| 781 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 782 | exit; |
||
| 783 | } |
||
| 784 | |||
| 785 | // We need a search term. |
||
| 786 | if ( empty( $_GET['search'] ) ) { |
||
| 787 | wp_send_json_success( array() ); |
||
| 788 | } |
||
| 789 | |||
| 790 | // Retrieve customers. |
||
| 791 | |||
| 792 | $customer_args = array( |
||
| 793 | 'fields' => array( 'ID', 'user_email', 'display_name' ), |
||
| 794 | 'orderby' => 'display_name', |
||
| 795 | 'search' => '*' . sanitize_text_field( $_GET['search'] ) . '*', |
||
| 796 | 'search_columns' => array( 'user_login', 'user_email', 'display_name' ), |
||
| 797 | ); |
||
| 798 | |||
| 799 | $customers = get_users( apply_filters( 'getpaid_ajax_invoice_customers_query_args', $customer_args ) ); |
||
| 800 | $data = array(); |
||
| 801 | |||
| 802 | foreach ( $customers as $customer ) { |
||
| 803 | $data[] = array( |
||
| 804 | 'id' => (int) $customer->ID, |
||
| 805 | 'text' => strip_tags( sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ) ), |
||
| 806 | ); |
||
| 807 | } |
||
| 808 | |||
| 809 | wp_send_json_success( $data ); |
||
| 810 | |||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Retrieves the states field for AUI forms. |
||
| 815 | */ |
||
| 816 | public static function get_aui_states_field() { |
||
| 869 | ) |
||
| 870 | ); |
||
| 871 | |||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Refresh prices. |
||
| 876 | * |
||
| 877 | * @since 1.0.19 |
||
| 878 | */ |
||
| 879 | public static function payment_form_refresh_prices() { |
||
| 910 | } |
||
| 911 | |||
| 912 | } |
||
| 913 | |||
| 914 | WPInv_Ajax::init(); |