| Total Complexity | 178 |
| Total Lines | 961 |
| 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 | 'payment_form' => true, |
||
| 67 | 'add_invoice_item' => false, |
||
| 68 | 'remove_invoice_item' => false, |
||
| 69 | 'create_invoice_item' => false, |
||
| 70 | 'get_billing_details' => false, |
||
| 71 | 'admin_recalculate_totals' => false, |
||
| 72 | 'admin_apply_discount' => false, |
||
| 73 | 'admin_remove_discount' => false, |
||
| 74 | 'check_email' => false, |
||
| 75 | 'run_tool' => false, |
||
| 76 | 'apply_discount' => true, |
||
| 77 | 'remove_discount' => true, |
||
| 78 | 'buy_items' => true, |
||
| 79 | ); |
||
| 80 | |||
| 81 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 82 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 83 | |||
| 84 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
| 85 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( $nopriv ) { |
||
| 89 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 90 | |||
| 91 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | public static function add_note() { |
||
| 97 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
| 98 | |||
| 99 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 100 | die(-1); |
||
| 101 | } |
||
| 102 | |||
| 103 | $post_id = absint( $_POST['post_id'] ); |
||
| 104 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
| 105 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
| 106 | |||
| 107 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 108 | |||
| 109 | if ( $post_id > 0 ) { |
||
| 110 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
| 111 | |||
| 112 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
| 113 | wpinv_get_invoice_note_line_item( $note_id ); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | die(); |
||
| 118 | } |
||
| 119 | |||
| 120 | public static function delete_note() { |
||
| 121 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
| 122 | |||
| 123 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 124 | die(-1); |
||
| 125 | } |
||
| 126 | |||
| 127 | $note_id = (int)$_POST['note_id']; |
||
| 128 | |||
| 129 | if ( $note_id > 0 ) { |
||
| 130 | wp_delete_comment( $note_id, true ); |
||
| 131 | } |
||
| 132 | |||
| 133 | die(); |
||
| 134 | } |
||
| 135 | |||
| 136 | public static function get_states_field() { |
||
| 137 | echo wpinv_get_states_field(); |
||
| 138 | |||
| 139 | die(); |
||
| 140 | } |
||
| 141 | |||
| 142 | public static function checkout() { |
||
| 143 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
| 144 | define( 'WPINV_CHECKOUT', true ); |
||
| 145 | } |
||
| 146 | |||
| 147 | wpinv_process_checkout(); |
||
| 148 | die(0); |
||
| 149 | } |
||
| 150 | |||
| 151 | public static function add_invoice_item() { |
||
| 152 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 153 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 154 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 155 | die(-1); |
||
| 156 | } |
||
| 157 | |||
| 158 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 159 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 160 | |||
| 161 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 162 | die(); |
||
| 163 | } |
||
| 164 | |||
| 165 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 166 | if ( empty( $invoice ) ) { |
||
| 167 | die(); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 171 | die(); // Don't allow modify items for paid invoice. |
||
| 172 | } |
||
| 173 | |||
| 174 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 175 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $item = new WPInv_Item( $item_id ); |
||
| 179 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 180 | die(); |
||
| 181 | } |
||
| 182 | |||
| 183 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 184 | if ( !empty( $invoice->cart_details ) ) { |
||
| 185 | $valid = true; |
||
| 186 | |||
| 187 | if ( $recurring_item = $invoice->get_recurring() ) { |
||
| 188 | if ( $recurring_item != $item_id ) { |
||
| 189 | $valid = false; |
||
| 190 | } |
||
| 191 | } else if ( wpinv_is_recurring_item( $item_id ) ) { |
||
| 192 | $valid = false; |
||
| 193 | } |
||
| 194 | |||
| 195 | if ( !$valid ) { |
||
| 196 | $response = array(); |
||
| 197 | $response['success'] = false; |
||
| 198 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 199 | wp_send_json( $response ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $checkout_session = wpinv_get_checkout_session(); |
||
| 204 | |||
| 205 | $data = array(); |
||
| 206 | $data['invoice_id'] = $invoice_id; |
||
| 207 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 208 | |||
| 209 | wpinv_set_checkout_session( $data ); |
||
| 210 | |||
| 211 | $quantity = wpinv_item_quantities_enabled() && !empty($_POST['qty']) && (int)$_POST['qty'] > 0 ? (int)$_POST['qty'] : 1; |
||
| 212 | |||
| 213 | $args = array( |
||
| 214 | 'id' => $item_id, |
||
| 215 | 'quantity' => $quantity, |
||
| 216 | 'item_price' => $item->get_price(), |
||
| 217 | 'custom_price' => '', |
||
| 218 | 'tax' => 0.00, |
||
| 219 | 'discount' => 0, |
||
| 220 | 'meta' => array(), |
||
| 221 | 'fees' => array() |
||
| 222 | ); |
||
| 223 | |||
| 224 | $invoice->add_item( $item_id, $args ); |
||
| 225 | $invoice->save(); |
||
| 226 | |||
| 227 | if ( empty( $_POST['country'] ) ) { |
||
| 228 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 229 | } |
||
| 230 | if ( empty( $_POST['state'] ) ) { |
||
| 231 | $_POST['state'] = $invoice->state; |
||
| 232 | } |
||
| 233 | |||
| 234 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 235 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 236 | |||
| 237 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 238 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 239 | |||
| 240 | $wpinv_ip_address_country = $invoice->country; |
||
| 241 | |||
| 242 | $invoice->recalculate_totals(true); |
||
| 243 | |||
| 244 | $response = array(); |
||
| 245 | $response['success'] = true; |
||
| 246 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 247 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 248 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 249 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 250 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 251 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 252 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 253 | $response['data']['total'] = $invoice->get_total(); |
||
| 254 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 255 | |||
| 256 | wpinv_set_checkout_session($checkout_session); |
||
| 257 | |||
| 258 | wp_send_json( $response ); |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | public static function remove_invoice_item() { |
||
| 263 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 264 | |||
| 265 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 266 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 267 | die(-1); |
||
| 268 | } |
||
| 269 | |||
| 270 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 271 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 272 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
| 273 | |||
| 274 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 275 | die(); |
||
| 276 | } |
||
| 277 | |||
| 278 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 279 | if ( empty( $invoice ) ) { |
||
| 280 | die(); |
||
| 281 | } |
||
| 282 | |||
| 283 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 284 | die(); // Don't allow modify items for paid invoice. |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 288 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 289 | } |
||
| 290 | |||
| 291 | $item = new WPInv_Item( $item_id ); |
||
| 292 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 293 | die(); |
||
| 294 | } |
||
| 295 | |||
| 296 | $checkout_session = wpinv_get_checkout_session(); |
||
| 297 | |||
| 298 | $data = array(); |
||
| 299 | $data['invoice_id'] = $invoice_id; |
||
| 300 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 301 | |||
| 302 | wpinv_set_checkout_session( $data ); |
||
| 303 | |||
| 304 | $args = array( |
||
| 305 | 'id' => $item_id, |
||
| 306 | 'quantity' => 1, |
||
| 307 | 'cart_index' => $cart_index |
||
| 308 | ); |
||
| 309 | |||
| 310 | $invoice->remove_item( $item_id, $args ); |
||
| 311 | $invoice->save(); |
||
| 312 | |||
| 313 | if ( empty( $_POST['country'] ) ) { |
||
| 314 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 315 | } |
||
| 316 | if ( empty( $_POST['state'] ) ) { |
||
| 317 | $_POST['state'] = $invoice->state; |
||
| 318 | } |
||
| 319 | |||
| 320 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 321 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 322 | |||
| 323 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 324 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 325 | |||
| 326 | $wpinv_ip_address_country = $invoice->country; |
||
| 327 | |||
| 328 | $invoice->recalculate_totals(true); |
||
| 329 | |||
| 330 | $response = array(); |
||
| 331 | $response['success'] = true; |
||
| 332 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 333 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 334 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 335 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 336 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 337 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 338 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 339 | $response['data']['total'] = $invoice->get_total(); |
||
| 340 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 341 | |||
| 342 | wpinv_set_checkout_session($checkout_session); |
||
| 343 | |||
| 344 | wp_send_json( $response ); |
||
| 345 | } |
||
| 346 | |||
| 347 | public static function create_invoice_item() { |
||
| 348 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 349 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 350 | die(-1); |
||
| 351 | } |
||
| 352 | |||
| 353 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 354 | |||
| 355 | // Find the item |
||
| 356 | if ( !is_numeric( $invoice_id ) ) { |
||
| 357 | die(); |
||
| 358 | } |
||
| 359 | |||
| 360 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 361 | if ( empty( $invoice ) ) { |
||
| 362 | die(); |
||
| 363 | } |
||
| 364 | |||
| 365 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 366 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 367 | $response = array(); |
||
| 368 | $response['success'] = false; |
||
| 369 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 370 | wp_send_json( $response ); |
||
| 371 | } |
||
| 372 | |||
| 373 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 374 | |||
| 375 | $meta = array(); |
||
| 376 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 377 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 378 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 379 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 380 | |||
| 381 | $data = array(); |
||
| 382 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 383 | $data['post_status'] = 'publish'; |
||
| 384 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 385 | $data['meta'] = $meta; |
||
| 386 | |||
| 387 | $item = new WPInv_Item(); |
||
| 388 | $item->create( $data ); |
||
| 389 | |||
| 390 | if ( !empty( $item ) ) { |
||
| 391 | $_POST['item_id'] = $item->ID; |
||
| 392 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 393 | |||
| 394 | self::add_invoice_item(); |
||
| 395 | } |
||
| 396 | die(); |
||
| 397 | } |
||
| 398 | |||
| 399 | public static function get_billing_details() { |
||
| 400 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 401 | |||
| 402 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 403 | die(-1); |
||
| 404 | } |
||
| 405 | |||
| 406 | $user_id = (int)$_POST['user_id']; |
||
| 407 | $billing_details = wpinv_get_user_address($user_id); |
||
| 408 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 409 | |||
| 410 | if (isset($billing_details['user_id'])) { |
||
| 411 | unset($billing_details['user_id']); |
||
| 412 | } |
||
| 413 | |||
| 414 | if (isset($billing_details['email'])) { |
||
| 415 | unset($billing_details['email']); |
||
| 416 | } |
||
| 417 | |||
| 418 | $response = array(); |
||
| 419 | $response['success'] = true; |
||
| 420 | $response['data']['billing_details'] = $billing_details; |
||
| 421 | |||
| 422 | wp_send_json( $response ); |
||
| 423 | } |
||
| 424 | |||
| 425 | public static function admin_recalculate_totals() { |
||
| 426 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 427 | |||
| 428 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 429 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 430 | die(-1); |
||
| 431 | } |
||
| 432 | |||
| 433 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 434 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 435 | if ( empty( $invoice ) ) { |
||
| 436 | die(); |
||
| 437 | } |
||
| 438 | |||
| 439 | $checkout_session = wpinv_get_checkout_session(); |
||
| 440 | |||
| 441 | $data = array(); |
||
| 442 | $data['invoice_id'] = $invoice_id; |
||
| 443 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 444 | |||
| 445 | wpinv_set_checkout_session( $data ); |
||
| 446 | |||
| 447 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 448 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 449 | } |
||
| 450 | |||
| 451 | if ( empty( $_POST['country'] ) ) { |
||
| 452 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 453 | } |
||
| 454 | |||
| 455 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 456 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 457 | if ( isset( $_POST['state'] ) ) { |
||
| 458 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 459 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 460 | } |
||
| 461 | |||
| 462 | $wpinv_ip_address_country = $invoice->country; |
||
| 463 | |||
| 464 | $invoice = $invoice->recalculate_totals(true); |
||
| 465 | |||
| 466 | $response = array(); |
||
| 467 | $response['success'] = true; |
||
| 468 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 469 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 470 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 471 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 472 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 473 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 474 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 475 | $response['data']['total'] = $invoice->get_total(); |
||
| 476 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 477 | |||
| 478 | wpinv_set_checkout_session($checkout_session); |
||
| 479 | |||
| 480 | wp_send_json( $response ); |
||
| 481 | } |
||
| 482 | |||
| 483 | public static function admin_apply_discount() { |
||
| 484 | global $wpi_userID; |
||
| 485 | |||
| 486 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 487 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 488 | die(-1); |
||
| 489 | } |
||
| 490 | |||
| 491 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 492 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 493 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 494 | die(); |
||
| 495 | } |
||
| 496 | |||
| 497 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 498 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 499 | die(); |
||
| 500 | } |
||
| 501 | |||
| 502 | $checkout_session = wpinv_get_checkout_session(); |
||
| 503 | |||
| 504 | $data = array(); |
||
| 505 | $data['invoice_id'] = $invoice_id; |
||
| 506 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 507 | |||
| 508 | wpinv_set_checkout_session( $data ); |
||
| 509 | |||
| 510 | $response = array(); |
||
| 511 | $response['success'] = false; |
||
| 512 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 513 | $response['data']['code'] = $discount_code; |
||
| 514 | |||
| 515 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 516 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 517 | |||
| 518 | $response['success'] = true; |
||
| 519 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 520 | } else { |
||
| 521 | $errors = wpinv_get_errors(); |
||
| 522 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 523 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 524 | } |
||
| 525 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 526 | } |
||
| 527 | |||
| 528 | wpinv_set_checkout_session($checkout_session); |
||
| 529 | |||
| 530 | wp_send_json( $response ); |
||
| 531 | } |
||
| 532 | |||
| 533 | public static function admin_remove_discount() { |
||
| 534 | global $wpi_userID; |
||
| 535 | |||
| 536 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 537 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 538 | die(-1); |
||
| 539 | } |
||
| 540 | |||
| 541 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 542 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 543 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 544 | die(); |
||
| 545 | } |
||
| 546 | |||
| 547 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 548 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 549 | die(); |
||
| 550 | } |
||
| 551 | |||
| 552 | $checkout_session = wpinv_get_checkout_session(); |
||
| 553 | |||
| 554 | $data = array(); |
||
| 555 | $data['invoice_id'] = $invoice_id; |
||
| 556 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 557 | |||
| 558 | wpinv_set_checkout_session( $data ); |
||
| 559 | |||
| 560 | $response = array(); |
||
| 561 | $response['success'] = false; |
||
| 562 | $response['msg'] = NULL; |
||
| 563 | |||
| 564 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 565 | $response['success'] = true; |
||
| 566 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 567 | |||
| 568 | wpinv_set_checkout_session($checkout_session); |
||
| 569 | |||
| 570 | wp_send_json( $response ); |
||
| 571 | } |
||
| 572 | |||
| 573 | public static function check_email() { |
||
| 574 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 575 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 576 | die(-1); |
||
| 577 | } |
||
| 578 | |||
| 579 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 580 | |||
| 581 | $response = array(); |
||
| 582 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 583 | $user_id = $user_data->ID; |
||
| 584 | $user_login = $user_data->user_login; |
||
| 585 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 586 | $billing_details = wpinv_get_user_address($user_id); |
||
| 587 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 588 | |||
| 589 | if (isset($billing_details['user_id'])) { |
||
| 590 | unset($billing_details['user_id']); |
||
| 591 | } |
||
| 592 | |||
| 593 | if (isset($billing_details['email'])) { |
||
| 594 | unset($billing_details['email']); |
||
| 595 | } |
||
| 596 | |||
| 597 | $response['success'] = true; |
||
| 598 | $response['data']['id'] = $user_data->ID; |
||
| 599 | $response['data']['name'] = $user_data->user_email; |
||
| 600 | $response['data']['billing_details'] = $billing_details; |
||
| 601 | } |
||
| 602 | |||
| 603 | wp_send_json( $response ); |
||
| 604 | } |
||
| 605 | |||
| 606 | public static function run_tool() { |
||
| 607 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 608 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 609 | die(-1); |
||
| 610 | } |
||
| 611 | |||
| 612 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 613 | |||
| 614 | do_action( 'wpinv_run_tool' ); |
||
| 615 | |||
| 616 | if ( !empty( $tool ) ) { |
||
| 617 | do_action( 'wpinv_tool_' . $tool ); |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | public static function apply_discount() { |
||
| 668 | } |
||
| 669 | |||
| 670 | public static function remove_discount() { |
||
| 671 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 672 | |||
| 673 | $response = array(); |
||
| 674 | |||
| 675 | if ( isset( $_POST['code'] ) ) { |
||
| 676 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 677 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 678 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Payment forms. |
||
| 698 | * |
||
| 699 | * @since 1.0.18 |
||
| 700 | */ |
||
| 701 | public static function payment_form() { |
||
| 827 | |||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Lets users buy items via ajax. |
||
| 832 | * |
||
| 833 | * @since 1.0.0 |
||
| 834 | */ |
||
| 835 | public static function buy_items() { |
||
| 978 | 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.