| Total Complexity | 181 |
| Total Lines | 979 |
| 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 | 'add_payment_form_item' => false, |
||
| 68 | 'remove_invoice_item' => false, |
||
| 69 | 'create_payment_form_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() { |
||
| 153 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 154 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 155 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 156 | die(-1); |
||
| 157 | } |
||
| 158 | |||
| 159 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 160 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 161 | |||
| 162 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 163 | die(); |
||
| 164 | } |
||
| 165 | |||
| 166 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 167 | if ( empty( $invoice ) ) { |
||
| 168 | die(); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 172 | die(); // Don't allow modify items for paid invoice. |
||
| 173 | } |
||
| 174 | |||
| 175 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 176 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 177 | } |
||
| 178 | |||
| 179 | $item = new WPInv_Item( $item_id ); |
||
| 180 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 181 | die(); |
||
| 182 | } |
||
| 183 | |||
| 184 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 185 | if ( !empty( $invoice->cart_details ) ) { |
||
| 186 | $valid = true; |
||
| 187 | |||
| 188 | if ( $recurring_item = $invoice->get_recurring() ) { |
||
| 189 | if ( $recurring_item != $item_id ) { |
||
| 190 | $valid = false; |
||
| 191 | } |
||
| 192 | } else if ( wpinv_is_recurring_item( $item_id ) ) { |
||
| 193 | $valid = false; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ( !$valid ) { |
||
| 197 | $response = array(); |
||
| 198 | $response['success'] = false; |
||
| 199 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 200 | wp_send_json( $response ); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | $checkout_session = wpinv_get_checkout_session(); |
||
| 205 | |||
| 206 | $data = array(); |
||
| 207 | $data['invoice_id'] = $invoice_id; |
||
| 208 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 209 | |||
| 210 | wpinv_set_checkout_session( $data ); |
||
| 211 | |||
| 212 | $quantity = wpinv_item_quantities_enabled() && !empty($_POST['qty']) && (int)$_POST['qty'] > 0 ? (int)$_POST['qty'] : 1; |
||
| 213 | |||
| 214 | $args = array( |
||
| 215 | 'id' => $item_id, |
||
| 216 | 'quantity' => $quantity, |
||
| 217 | 'item_price' => $item->get_price(), |
||
| 218 | 'custom_price' => '', |
||
| 219 | 'tax' => 0.00, |
||
| 220 | 'discount' => 0, |
||
| 221 | 'meta' => array(), |
||
| 222 | 'fees' => array() |
||
| 223 | ); |
||
| 224 | |||
| 225 | $invoice->add_item( $item_id, $args ); |
||
| 226 | $invoice->save(); |
||
| 227 | |||
| 228 | if ( empty( $_POST['country'] ) ) { |
||
| 229 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 230 | } |
||
| 231 | if ( empty( $_POST['state'] ) ) { |
||
| 232 | $_POST['state'] = $invoice->state; |
||
| 233 | } |
||
| 234 | |||
| 235 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 236 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 237 | |||
| 238 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 239 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 240 | |||
| 241 | $wpinv_ip_address_country = $invoice->country; |
||
| 242 | |||
| 243 | $invoice->recalculate_totals(true); |
||
| 244 | |||
| 245 | $response = array(); |
||
| 246 | $response['success'] = true; |
||
| 247 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 248 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 249 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 250 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 251 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 252 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 253 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 254 | $response['data']['total'] = $invoice->get_total(); |
||
| 255 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 256 | |||
| 257 | wpinv_set_checkout_session($checkout_session); |
||
| 258 | |||
| 259 | wp_send_json( $response ); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Handles the ajax requests to add a payment form item. |
||
| 264 | */ |
||
| 265 | public static function add_payment_form_item() { |
||
| 266 | |||
| 267 | // Check nonce... |
||
| 268 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 269 | |||
| 270 | // ... and user capability. |
||
| 271 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 272 | wp_send_json_error( __( 'You do not have permission to do that.', 'invoicing' ) ); |
||
| 273 | die(-1); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Prepare the item and the invoice. |
||
| 277 | $item_id = absint( $_POST['item_id'] ); |
||
| 278 | $form_id = absint( $_POST['form_id'] ); |
||
| 279 | |||
| 280 | if ( empty( $item_id ) || empty( $form_id ) ) { |
||
| 281 | wp_send_json_error( __( 'Invalid form or item.', 'invoicing' ) ); |
||
| 282 | } |
||
| 283 | |||
| 284 | $new_item = new WPInv_Item( $item_id ); |
||
| 285 | if ( empty( $new_item ) || $new_item->post_type != 'wpi_item' ) { |
||
| 286 | wp_send_json_error( __( 'Invalid item.', 'invoicing' ) ); |
||
| 287 | } |
||
| 288 | |||
| 289 | // Fetch existing items. |
||
| 290 | $items = get_post_meta( $form_id, 'wpinv_payment_form_items', true ); |
||
| 291 | |||
| 292 | if ( empty( $items ) || is_array( $items ) ) { |
||
| 293 | $items = array(); |
||
| 294 | } |
||
| 295 | |||
| 296 | // Only one recurring item per form please. |
||
| 297 | $has_recurring = wpinv_is_recurring_item( $item_id ); |
||
| 298 | foreach ( $items as $index => $item_data ) { |
||
| 299 | |||
| 300 | $id = $item_data['id']; |
||
| 301 | $item = new WPInv_Item( $id ); |
||
| 302 | |||
| 303 | if ( empty( $item ) || $item->post_type != 'wpi_item' ) { |
||
| 304 | unset( $items[ $index ] ); |
||
| 305 | continue; |
||
| 306 | } |
||
| 307 | |||
| 308 | if ( $item_id == $id ) { |
||
| 309 | unset( $items[ $index ] ); |
||
| 310 | } |
||
| 311 | |||
| 312 | if ( $item->is_recurring() ) { |
||
| 313 | $has_recurring = true; |
||
| 314 | } |
||
| 315 | |||
| 316 | } |
||
| 317 | |||
| 318 | $items[] = array( 'id' => $new_item->ID ); |
||
| 319 | |||
| 320 | // One recurring item per form. |
||
| 321 | if ( $has_recurring && 1 < count( $items ) ) { |
||
| 322 | wp_send_json_error( __( 'You can not add item because recurring items must be paid individually!', 'invoicing' ) ); |
||
| 323 | } |
||
| 324 | |||
| 325 | update_post_meta( $form_id, 'wpinv_payment_form_items', $items ); |
||
| 326 | |||
| 327 | ob_start(); |
||
| 328 | WPInv_Meta_Box_Form_Items::output( get_post( $form_id ) ); |
||
| 329 | $items = ob_get_clean(); |
||
| 330 | |||
| 331 | wp_send_json_success( $items ); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Handles the ajax requests to Create a new payment form item. |
||
| 336 | */ |
||
| 337 | public static function create_payment_form_item() { |
||
| 338 | |||
| 339 | // Check nonce... |
||
| 340 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 341 | |||
| 342 | // ... and user capability. |
||
| 343 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 344 | wp_send_json_error( __( 'You do not have permission to do that.', 'invoicing' ) ); |
||
| 345 | die(-1); |
||
| 346 | } |
||
| 347 | |||
| 348 | // Item data. |
||
| 349 | $form_id = absint( $_POST['form_id'] ); |
||
| 350 | $item_price = wpinv_sanitize_amount( $_POST['item_price'] ); |
||
| 351 | $item_name = sanitize_text_field( $_POST['item_name'] ); |
||
| 352 | $item_description = wp_kses_post( $_POST['item_description'] ); |
||
| 353 | |||
| 354 | if ( empty( $item_name ) ) { |
||
| 355 | wp_send_json_error( __( 'Specify a name for your item.', 'invoicing' ) ); |
||
| 356 | } |
||
| 357 | |||
| 358 | // Fetch existing items. |
||
| 359 | $items = get_post_meta( $form_id, 'wpinv_payment_form_items', true ); |
||
| 360 | |||
| 361 | if ( empty( $items ) || is_array( $items ) ) { |
||
| 362 | $items = array(); |
||
| 363 | } |
||
| 364 | |||
| 365 | // Only one recurring item per form please. |
||
| 366 | foreach ( $items as $index => $item_data ) { |
||
| 367 | |||
| 368 | $id = $item_data['id']; |
||
| 369 | $item = new WPInv_Item( $id ); |
||
| 370 | |||
| 371 | if ( empty( $item ) || $item->post_type != 'wpi_item' ) { |
||
| 372 | unset( $items[ $index ] ); |
||
| 373 | continue; |
||
| 374 | } |
||
| 375 | |||
| 376 | if ( $item->is_recurring() ) { |
||
| 377 | wp_send_json_error( __( 'You can not add item because recurring items must be paid individually!', 'invoicing' ) ); |
||
| 378 | } |
||
| 379 | |||
| 380 | } |
||
| 381 | |||
| 382 | $data = array( |
||
| 383 | 'post_title' => $item_name, |
||
| 384 | 'post_excerpt' => $item_description, |
||
| 385 | 'post_status' => 'publish', |
||
| 386 | 'meta' => array( |
||
| 387 | 'type' => 'custom', |
||
| 388 | 'price' => $item_price, |
||
| 389 | 'vat_rule' => 'digital', |
||
| 390 | 'vat_class' => '_standard', |
||
| 391 | ) |
||
| 392 | ); |
||
| 393 | |||
| 394 | $item = new WPInv_Item(); |
||
| 395 | $item->create( $data ); |
||
| 396 | |||
| 397 | if ( empty( $item ) ) { |
||
| 398 | wp_send_json_error( __( 'Error creating item', 'invoicing' ) ); |
||
| 399 | die(); |
||
| 400 | } |
||
| 401 | |||
| 402 | $items[] = array( 'id' => $item->ID ); |
||
| 403 | |||
| 404 | update_post_meta( $form_id, 'wpinv_payment_form_items', $items ); |
||
| 405 | |||
| 406 | ob_start(); |
||
| 407 | WPInv_Meta_Box_Form_Items::output( get_post( $form_id ) ); |
||
| 408 | $items = ob_get_clean(); |
||
| 409 | |||
| 410 | wp_send_json_success( $items ); |
||
| 411 | } |
||
| 412 | |||
| 413 | public static function remove_invoice_item() { |
||
| 414 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 415 | |||
| 416 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 417 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 418 | die(-1); |
||
| 419 | } |
||
| 420 | |||
| 421 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 422 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 423 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
| 424 | |||
| 425 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 426 | die(); |
||
| 427 | } |
||
| 428 | |||
| 429 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 430 | if ( empty( $invoice ) ) { |
||
| 431 | die(); |
||
| 432 | } |
||
| 433 | |||
| 434 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 435 | die(); // Don't allow modify items for paid invoice. |
||
| 436 | } |
||
| 437 | |||
| 438 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 439 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 440 | } |
||
| 441 | |||
| 442 | $item = new WPInv_Item( $item_id ); |
||
| 443 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 444 | die(); |
||
| 445 | } |
||
| 446 | |||
| 447 | $checkout_session = wpinv_get_checkout_session(); |
||
| 448 | |||
| 449 | $data = array(); |
||
| 450 | $data['invoice_id'] = $invoice_id; |
||
| 451 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 452 | |||
| 453 | wpinv_set_checkout_session( $data ); |
||
| 454 | |||
| 455 | $args = array( |
||
| 456 | 'id' => $item_id, |
||
| 457 | 'quantity' => 1, |
||
| 458 | 'cart_index' => $cart_index |
||
| 459 | ); |
||
| 460 | |||
| 461 | $invoice->remove_item( $item_id, $args ); |
||
| 462 | $invoice->save(); |
||
| 463 | |||
| 464 | if ( empty( $_POST['country'] ) ) { |
||
| 465 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 466 | } |
||
| 467 | if ( empty( $_POST['state'] ) ) { |
||
| 468 | $_POST['state'] = $invoice->state; |
||
| 469 | } |
||
| 470 | |||
| 471 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 472 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 473 | |||
| 474 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 475 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 476 | |||
| 477 | $wpinv_ip_address_country = $invoice->country; |
||
| 478 | |||
| 479 | $invoice->recalculate_totals(true); |
||
| 480 | |||
| 481 | $response = array(); |
||
| 482 | $response['success'] = true; |
||
| 483 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 484 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 485 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 486 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 487 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 488 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 489 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 490 | $response['data']['total'] = $invoice->get_total(); |
||
| 491 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 492 | |||
| 493 | wpinv_set_checkout_session($checkout_session); |
||
| 494 | |||
| 495 | wp_send_json( $response ); |
||
| 496 | } |
||
| 497 | |||
| 498 | public static function create_invoice_item() { |
||
| 499 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 500 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 501 | die(-1); |
||
| 502 | } |
||
| 503 | |||
| 504 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 505 | |||
| 506 | // Find the item |
||
| 507 | if ( !is_numeric( $invoice_id ) ) { |
||
| 508 | die(); |
||
| 509 | } |
||
| 510 | |||
| 511 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 512 | if ( empty( $invoice ) ) { |
||
| 513 | die(); |
||
| 514 | } |
||
| 515 | |||
| 516 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 517 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 518 | $response = array(); |
||
| 519 | $response['success'] = false; |
||
| 520 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 521 | wp_send_json( $response ); |
||
| 522 | } |
||
| 523 | |||
| 524 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 525 | |||
| 526 | $meta = array(); |
||
| 527 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 528 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 529 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 530 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 531 | |||
| 532 | $data = array(); |
||
| 533 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 534 | $data['post_status'] = 'publish'; |
||
| 535 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 536 | $data['meta'] = $meta; |
||
| 537 | |||
| 538 | $item = new WPInv_Item(); |
||
| 539 | $item->create( $data ); |
||
| 540 | |||
| 541 | if ( !empty( $item ) ) { |
||
| 542 | $_POST['item_id'] = $item->ID; |
||
| 543 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 544 | |||
| 545 | self::add_invoice_item(); |
||
| 546 | } |
||
| 547 | die(); |
||
| 548 | } |
||
| 549 | |||
| 550 | public static function get_billing_details() { |
||
| 551 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 552 | |||
| 553 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 554 | die(-1); |
||
| 555 | } |
||
| 556 | |||
| 557 | $user_id = (int)$_POST['user_id']; |
||
| 558 | $billing_details = wpinv_get_user_address($user_id); |
||
| 559 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 560 | |||
| 561 | if (isset($billing_details['user_id'])) { |
||
| 562 | unset($billing_details['user_id']); |
||
| 563 | } |
||
| 564 | |||
| 565 | if (isset($billing_details['email'])) { |
||
| 566 | unset($billing_details['email']); |
||
| 567 | } |
||
| 568 | |||
| 569 | $response = array(); |
||
| 570 | $response['success'] = true; |
||
| 571 | $response['data']['billing_details'] = $billing_details; |
||
| 572 | |||
| 573 | wp_send_json( $response ); |
||
| 574 | } |
||
| 575 | |||
| 576 | public static function admin_recalculate_totals() { |
||
| 577 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 578 | |||
| 579 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 580 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 581 | die(-1); |
||
| 582 | } |
||
| 583 | |||
| 584 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 585 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 586 | if ( empty( $invoice ) ) { |
||
| 587 | die(); |
||
| 588 | } |
||
| 589 | |||
| 590 | $checkout_session = wpinv_get_checkout_session(); |
||
| 591 | |||
| 592 | $data = array(); |
||
| 593 | $data['invoice_id'] = $invoice_id; |
||
| 594 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 595 | |||
| 596 | wpinv_set_checkout_session( $data ); |
||
| 597 | |||
| 598 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 599 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 600 | } |
||
| 601 | |||
| 602 | if ( empty( $_POST['country'] ) ) { |
||
| 603 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 604 | } |
||
| 605 | |||
| 606 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 607 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 608 | if ( isset( $_POST['state'] ) ) { |
||
| 609 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 610 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 611 | } |
||
| 612 | |||
| 613 | $wpinv_ip_address_country = $invoice->country; |
||
| 614 | |||
| 615 | $invoice = $invoice->recalculate_totals(true); |
||
| 616 | |||
| 617 | $response = array(); |
||
| 618 | $response['success'] = true; |
||
| 619 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 620 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 621 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 622 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 623 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 624 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 625 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 626 | $response['data']['total'] = $invoice->get_total(); |
||
| 627 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 628 | |||
| 629 | wpinv_set_checkout_session($checkout_session); |
||
| 630 | |||
| 631 | wp_send_json( $response ); |
||
| 632 | } |
||
| 633 | |||
| 634 | public static function admin_apply_discount() { |
||
| 635 | global $wpi_userID; |
||
| 636 | |||
| 637 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 638 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 639 | die(-1); |
||
| 640 | } |
||
| 641 | |||
| 642 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 643 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 644 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 645 | die(); |
||
| 646 | } |
||
| 647 | |||
| 648 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 649 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 650 | die(); |
||
| 651 | } |
||
| 652 | |||
| 653 | $checkout_session = wpinv_get_checkout_session(); |
||
| 654 | |||
| 655 | $data = array(); |
||
| 656 | $data['invoice_id'] = $invoice_id; |
||
| 657 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 658 | |||
| 659 | wpinv_set_checkout_session( $data ); |
||
| 660 | |||
| 661 | $response = array(); |
||
| 662 | $response['success'] = false; |
||
| 663 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 664 | $response['data']['code'] = $discount_code; |
||
| 665 | |||
| 666 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 667 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 668 | |||
| 669 | $response['success'] = true; |
||
| 670 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 671 | } else { |
||
| 672 | $errors = wpinv_get_errors(); |
||
| 673 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 674 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 675 | } |
||
| 676 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 677 | } |
||
| 678 | |||
| 679 | wpinv_set_checkout_session($checkout_session); |
||
| 680 | |||
| 681 | wp_send_json( $response ); |
||
| 682 | } |
||
| 683 | |||
| 684 | public static function admin_remove_discount() { |
||
| 685 | global $wpi_userID; |
||
| 686 | |||
| 687 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 688 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 689 | die(-1); |
||
| 690 | } |
||
| 691 | |||
| 692 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 693 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 694 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 695 | die(); |
||
| 696 | } |
||
| 697 | |||
| 698 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 699 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 700 | die(); |
||
| 701 | } |
||
| 702 | |||
| 703 | $checkout_session = wpinv_get_checkout_session(); |
||
| 704 | |||
| 705 | $data = array(); |
||
| 706 | $data['invoice_id'] = $invoice_id; |
||
| 707 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 708 | |||
| 709 | wpinv_set_checkout_session( $data ); |
||
| 710 | |||
| 711 | $response = array(); |
||
| 712 | $response['success'] = false; |
||
| 713 | $response['msg'] = NULL; |
||
| 714 | |||
| 715 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 716 | $response['success'] = true; |
||
| 717 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 718 | |||
| 719 | wpinv_set_checkout_session($checkout_session); |
||
| 720 | |||
| 721 | wp_send_json( $response ); |
||
| 722 | } |
||
| 723 | |||
| 724 | public static function check_email() { |
||
| 725 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 726 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 727 | die(-1); |
||
| 728 | } |
||
| 729 | |||
| 730 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 731 | |||
| 732 | $response = array(); |
||
| 733 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 734 | $user_id = $user_data->ID; |
||
| 735 | $user_login = $user_data->user_login; |
||
| 736 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 737 | $billing_details = wpinv_get_user_address($user_id); |
||
| 738 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 739 | |||
| 740 | if (isset($billing_details['user_id'])) { |
||
| 741 | unset($billing_details['user_id']); |
||
| 742 | } |
||
| 743 | |||
| 744 | if (isset($billing_details['email'])) { |
||
| 745 | unset($billing_details['email']); |
||
| 746 | } |
||
| 747 | |||
| 748 | $response['success'] = true; |
||
| 749 | $response['data']['id'] = $user_data->ID; |
||
| 750 | $response['data']['name'] = $user_data->user_email; |
||
| 751 | $response['data']['billing_details'] = $billing_details; |
||
| 752 | } |
||
| 753 | |||
| 754 | wp_send_json( $response ); |
||
| 755 | } |
||
| 756 | |||
| 757 | public static function run_tool() { |
||
| 758 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 759 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 760 | die(-1); |
||
| 761 | } |
||
| 762 | |||
| 763 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 764 | |||
| 765 | do_action( 'wpinv_run_tool' ); |
||
| 766 | |||
| 767 | if ( !empty( $tool ) ) { |
||
| 768 | do_action( 'wpinv_tool_' . $tool ); |
||
| 769 | } |
||
| 770 | } |
||
| 771 | |||
| 772 | public static function apply_discount() { |
||
| 819 | } |
||
| 820 | |||
| 821 | public static function remove_discount() { |
||
| 822 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 823 | |||
| 824 | $response = array(); |
||
| 825 | |||
| 826 | if ( isset( $_POST['code'] ) ) { |
||
| 827 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 828 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 829 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 830 | $cart_totals = wpinv_recalculate_tax( true ); |
||
| 831 | |||
| 832 | if ( !empty( $cart_totals ) ) { |
||
| 833 | $response['success'] = true; |
||
| 834 | $response['data'] = $cart_totals; |
||
| 835 | $response['data']['code'] = $discount_code; |
||
| 836 | } else { |
||
| 837 | $response['success'] = false; |
||
| 838 | } |
||
| 839 | |||
| 840 | // Allow for custom discount code handling |
||
| 841 | $response = apply_filters( 'wpinv_ajax_discount_response', $response ); |
||
| 842 | } |
||
| 843 | |||
| 844 | wp_send_json( $response ); |
||
| 845 | } |
||
| 846 | |||
| 847 | |||
| 848 | /** |
||
| 849 | * Lets users buy items via ajax. |
||
| 850 | * |
||
| 851 | * @since 1.0.0 |
||
| 852 | */ |
||
| 853 | public static function buy_items() { |
||
| 993 | } |
||
| 994 | } |
||
| 995 | |||
| 996 | 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.