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