| Total Complexity | 235 |
| Total Lines | 1331 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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_get_taxes' => true, |
||
| 67 | 'payment_form' => true, |
||
| 68 | 'get_payment_form' => true, |
||
| 69 | 'get_payment_form_states_field' => true, |
||
| 70 | 'add_invoice_item' => false, |
||
| 71 | 'remove_invoice_item' => false, |
||
| 72 | 'create_invoice_item' => false, |
||
| 73 | 'get_billing_details' => false, |
||
| 74 | 'admin_recalculate_totals' => false, |
||
| 75 | 'admin_apply_discount' => false, |
||
| 76 | 'admin_remove_discount' => false, |
||
| 77 | 'check_email' => false, |
||
| 78 | 'run_tool' => false, |
||
| 79 | 'apply_discount' => true, |
||
| 80 | 'remove_discount' => true, |
||
| 81 | 'buy_items' => true, |
||
| 82 | ); |
||
| 83 | |||
| 84 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 85 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 86 | |||
| 87 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
| 88 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( $nopriv ) { |
||
| 92 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 93 | |||
| 94 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | public static function add_note() { |
||
| 100 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
| 101 | |||
| 102 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 103 | die(-1); |
||
| 104 | } |
||
| 105 | |||
| 106 | $post_id = absint( $_POST['post_id'] ); |
||
| 107 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
| 108 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
| 109 | |||
| 110 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 111 | |||
| 112 | if ( $post_id > 0 ) { |
||
| 113 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
| 114 | |||
| 115 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
| 116 | wpinv_get_invoice_note_line_item( $note_id ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | die(); |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function delete_note() { |
||
| 124 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
| 125 | |||
| 126 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 127 | die(-1); |
||
| 128 | } |
||
| 129 | |||
| 130 | $note_id = (int)$_POST['note_id']; |
||
| 131 | |||
| 132 | if ( $note_id > 0 ) { |
||
| 133 | wp_delete_comment( $note_id, true ); |
||
| 134 | } |
||
| 135 | |||
| 136 | die(); |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function get_states_field() { |
||
| 140 | echo wpinv_get_states_field(); |
||
| 141 | |||
| 142 | die(); |
||
| 143 | } |
||
| 144 | |||
| 145 | public static function checkout() { |
||
| 146 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
| 147 | define( 'WPINV_CHECKOUT', true ); |
||
| 148 | } |
||
| 149 | |||
| 150 | wpinv_process_checkout(); |
||
| 151 | die(0); |
||
| 152 | } |
||
| 153 | |||
| 154 | public static function add_invoice_item() { |
||
| 155 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 156 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 157 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 158 | die(-1); |
||
| 159 | } |
||
| 160 | |||
| 161 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 162 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 163 | |||
| 164 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 165 | die(); |
||
| 166 | } |
||
| 167 | |||
| 168 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 169 | if ( empty( $invoice ) ) { |
||
| 170 | die(); |
||
| 171 | } |
||
| 172 | |||
| 173 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 174 | die(); // Don't allow modify items for paid invoice. |
||
| 175 | } |
||
| 176 | |||
| 177 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 178 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $item = new WPInv_Item( $item_id ); |
||
| 182 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 183 | die(); |
||
| 184 | } |
||
| 185 | |||
| 186 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 187 | if ( !empty( $invoice->cart_details ) ) { |
||
| 188 | $valid = true; |
||
| 189 | |||
| 190 | if ( $recurring_item = $invoice->get_recurring() ) { |
||
| 191 | if ( $recurring_item != $item_id ) { |
||
| 192 | $valid = false; |
||
| 193 | } |
||
| 194 | } else if ( wpinv_is_recurring_item( $item_id ) ) { |
||
| 195 | $valid = false; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ( !$valid ) { |
||
| 199 | $response = array(); |
||
| 200 | $response['success'] = false; |
||
| 201 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 202 | wp_send_json( $response ); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | $checkout_session = wpinv_get_checkout_session(); |
||
| 207 | |||
| 208 | $data = array(); |
||
| 209 | $data['invoice_id'] = $invoice_id; |
||
| 210 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 211 | |||
| 212 | wpinv_set_checkout_session( $data ); |
||
| 213 | |||
| 214 | $quantity = wpinv_item_quantities_enabled() && !empty($_POST['qty']) && (int)$_POST['qty'] > 0 ? (int)$_POST['qty'] : 1; |
||
| 215 | |||
| 216 | $args = array( |
||
| 217 | 'id' => $item_id, |
||
| 218 | 'quantity' => $quantity, |
||
| 219 | 'item_price' => $item->get_price(), |
||
| 220 | 'custom_price' => '', |
||
| 221 | 'tax' => 0.00, |
||
| 222 | 'discount' => 0, |
||
| 223 | 'meta' => array(), |
||
| 224 | 'fees' => array() |
||
| 225 | ); |
||
| 226 | |||
| 227 | $invoice->add_item( $item_id, $args ); |
||
| 228 | $invoice->save(); |
||
| 229 | |||
| 230 | if ( empty( $_POST['country'] ) ) { |
||
| 231 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 232 | } |
||
| 233 | if ( empty( $_POST['state'] ) ) { |
||
| 234 | $_POST['state'] = $invoice->state; |
||
| 235 | } |
||
| 236 | |||
| 237 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 238 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 239 | |||
| 240 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 241 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 242 | |||
| 243 | $wpinv_ip_address_country = $invoice->country; |
||
| 244 | |||
| 245 | $invoice->recalculate_totals(true); |
||
| 246 | |||
| 247 | $response = array(); |
||
| 248 | $response['success'] = true; |
||
| 249 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 250 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 251 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 252 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 253 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 254 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 255 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 256 | $response['data']['total'] = $invoice->get_total(); |
||
| 257 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 258 | |||
| 259 | wpinv_set_checkout_session($checkout_session); |
||
| 260 | |||
| 261 | wp_send_json( $response ); |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | public static function remove_invoice_item() { |
||
| 266 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 267 | |||
| 268 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 269 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 270 | die(-1); |
||
| 271 | } |
||
| 272 | |||
| 273 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 274 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 275 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
| 276 | |||
| 277 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 278 | die(); |
||
| 279 | } |
||
| 280 | |||
| 281 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 282 | if ( empty( $invoice ) ) { |
||
| 283 | die(); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 287 | die(); // Don't allow modify items for paid invoice. |
||
| 288 | } |
||
| 289 | |||
| 290 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 291 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 292 | } |
||
| 293 | |||
| 294 | $item = new WPInv_Item( $item_id ); |
||
| 295 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 296 | die(); |
||
| 297 | } |
||
| 298 | |||
| 299 | $checkout_session = wpinv_get_checkout_session(); |
||
| 300 | |||
| 301 | $data = array(); |
||
| 302 | $data['invoice_id'] = $invoice_id; |
||
| 303 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 304 | |||
| 305 | wpinv_set_checkout_session( $data ); |
||
| 306 | |||
| 307 | $args = array( |
||
| 308 | 'id' => $item_id, |
||
| 309 | 'quantity' => 1, |
||
| 310 | 'cart_index' => $cart_index |
||
| 311 | ); |
||
| 312 | |||
| 313 | $invoice->remove_item( $item_id, $args ); |
||
| 314 | $invoice->save(); |
||
| 315 | |||
| 316 | if ( empty( $_POST['country'] ) ) { |
||
| 317 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 318 | } |
||
| 319 | if ( empty( $_POST['state'] ) ) { |
||
| 320 | $_POST['state'] = $invoice->state; |
||
| 321 | } |
||
| 322 | |||
| 323 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 324 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 325 | |||
| 326 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 327 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 328 | |||
| 329 | $wpinv_ip_address_country = $invoice->country; |
||
| 330 | |||
| 331 | $invoice->recalculate_totals(true); |
||
| 332 | |||
| 333 | $response = array(); |
||
| 334 | $response['success'] = true; |
||
| 335 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 336 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 337 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 338 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 339 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 340 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 341 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 342 | $response['data']['total'] = $invoice->get_total(); |
||
| 343 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 344 | |||
| 345 | wpinv_set_checkout_session($checkout_session); |
||
| 346 | |||
| 347 | wp_send_json( $response ); |
||
| 348 | } |
||
| 349 | |||
| 350 | public static function create_invoice_item() { |
||
| 351 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 352 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 353 | die(-1); |
||
| 354 | } |
||
| 355 | |||
| 356 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 357 | |||
| 358 | // Find the item |
||
| 359 | if ( !is_numeric( $invoice_id ) ) { |
||
| 360 | die(); |
||
| 361 | } |
||
| 362 | |||
| 363 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 364 | if ( empty( $invoice ) ) { |
||
| 365 | die(); |
||
| 366 | } |
||
| 367 | |||
| 368 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 369 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 370 | $response = array(); |
||
| 371 | $response['success'] = false; |
||
| 372 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 373 | wp_send_json( $response ); |
||
| 374 | } |
||
| 375 | |||
| 376 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 377 | |||
| 378 | $meta = array(); |
||
| 379 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 380 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 381 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 382 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 383 | |||
| 384 | $data = array(); |
||
| 385 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 386 | $data['post_status'] = 'publish'; |
||
| 387 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 388 | $data['meta'] = $meta; |
||
| 389 | |||
| 390 | $item = new WPInv_Item(); |
||
| 391 | $item->create( $data ); |
||
| 392 | |||
| 393 | if ( !empty( $item ) ) { |
||
| 394 | $_POST['item_id'] = $item->ID; |
||
| 395 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 396 | |||
| 397 | self::add_invoice_item(); |
||
| 398 | } |
||
| 399 | die(); |
||
| 400 | } |
||
| 401 | |||
| 402 | public static function get_billing_details() { |
||
| 403 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 404 | |||
| 405 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 406 | die(-1); |
||
| 407 | } |
||
| 408 | |||
| 409 | $user_id = (int)$_POST['user_id']; |
||
| 410 | $billing_details = wpinv_get_user_address($user_id); |
||
| 411 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 412 | |||
| 413 | if (isset($billing_details['user_id'])) { |
||
| 414 | unset($billing_details['user_id']); |
||
| 415 | } |
||
| 416 | |||
| 417 | if (isset($billing_details['email'])) { |
||
| 418 | unset($billing_details['email']); |
||
| 419 | } |
||
| 420 | |||
| 421 | $response = array(); |
||
| 422 | $response['success'] = true; |
||
| 423 | $response['data']['billing_details'] = $billing_details; |
||
| 424 | |||
| 425 | wp_send_json( $response ); |
||
| 426 | } |
||
| 427 | |||
| 428 | public static function admin_recalculate_totals() { |
||
| 429 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 430 | |||
| 431 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 432 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 433 | die(-1); |
||
| 434 | } |
||
| 435 | |||
| 436 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 437 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 438 | if ( empty( $invoice ) ) { |
||
| 439 | die(); |
||
| 440 | } |
||
| 441 | |||
| 442 | $checkout_session = wpinv_get_checkout_session(); |
||
| 443 | |||
| 444 | $data = array(); |
||
| 445 | $data['invoice_id'] = $invoice_id; |
||
| 446 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 447 | |||
| 448 | wpinv_set_checkout_session( $data ); |
||
| 449 | |||
| 450 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 451 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 452 | } |
||
| 453 | |||
| 454 | if ( empty( $_POST['country'] ) ) { |
||
| 455 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 456 | } |
||
| 457 | |||
| 458 | $disable_taxes = 0; |
||
| 459 | if ( ! empty( $_POST['disable_taxes'] ) ) { |
||
| 460 | $disable_taxes = 1; |
||
| 461 | } |
||
| 462 | $invoice->set( 'disable_taxes', $disable_taxes ); |
||
| 463 | |||
| 464 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 465 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 466 | if ( isset( $_POST['state'] ) ) { |
||
| 467 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 468 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 469 | } |
||
| 470 | |||
| 471 | $wpinv_ip_address_country = $invoice->country; |
||
| 472 | |||
| 473 | $invoice = $invoice->recalculate_totals(true); |
||
| 474 | |||
| 475 | $response = array(); |
||
| 476 | $response['success'] = true; |
||
| 477 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 478 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 479 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 480 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 481 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 482 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 483 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 484 | $response['data']['total'] = $invoice->get_total(); |
||
| 485 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 486 | |||
| 487 | wpinv_set_checkout_session($checkout_session); |
||
| 488 | |||
| 489 | wp_send_json( $response ); |
||
| 490 | } |
||
| 491 | |||
| 492 | public static function admin_apply_discount() { |
||
| 493 | global $wpi_userID; |
||
| 494 | |||
| 495 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 496 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 497 | die(-1); |
||
| 498 | } |
||
| 499 | |||
| 500 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 501 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 502 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 503 | die(); |
||
| 504 | } |
||
| 505 | |||
| 506 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 507 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 508 | die(); |
||
| 509 | } |
||
| 510 | |||
| 511 | $checkout_session = wpinv_get_checkout_session(); |
||
| 512 | |||
| 513 | $data = array(); |
||
| 514 | $data['invoice_id'] = $invoice_id; |
||
| 515 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 516 | |||
| 517 | wpinv_set_checkout_session( $data ); |
||
| 518 | |||
| 519 | $response = array(); |
||
| 520 | $response['success'] = false; |
||
| 521 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 522 | $response['data']['code'] = $discount_code; |
||
| 523 | |||
| 524 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 525 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 526 | |||
| 527 | $response['success'] = true; |
||
| 528 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 529 | } else { |
||
| 530 | $errors = wpinv_get_errors(); |
||
| 531 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 532 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 533 | } |
||
| 534 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 535 | } |
||
| 536 | |||
| 537 | wpinv_set_checkout_session($checkout_session); |
||
| 538 | |||
| 539 | wp_send_json( $response ); |
||
| 540 | } |
||
| 541 | |||
| 542 | public static function admin_remove_discount() { |
||
| 543 | global $wpi_userID; |
||
| 544 | |||
| 545 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 546 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 547 | die(-1); |
||
| 548 | } |
||
| 549 | |||
| 550 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 551 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 552 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 553 | die(); |
||
| 554 | } |
||
| 555 | |||
| 556 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 557 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 558 | die(); |
||
| 559 | } |
||
| 560 | |||
| 561 | $checkout_session = wpinv_get_checkout_session(); |
||
| 562 | |||
| 563 | $data = array(); |
||
| 564 | $data['invoice_id'] = $invoice_id; |
||
| 565 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 566 | |||
| 567 | wpinv_set_checkout_session( $data ); |
||
| 568 | |||
| 569 | $response = array(); |
||
| 570 | $response['success'] = false; |
||
| 571 | $response['msg'] = NULL; |
||
| 572 | |||
| 573 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 574 | $response['success'] = true; |
||
| 575 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 576 | |||
| 577 | wpinv_set_checkout_session($checkout_session); |
||
| 578 | |||
| 579 | wp_send_json( $response ); |
||
| 580 | } |
||
| 581 | |||
| 582 | public static function check_email() { |
||
| 583 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 584 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 585 | die(-1); |
||
| 586 | } |
||
| 587 | |||
| 588 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 589 | |||
| 590 | $response = array(); |
||
| 591 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 592 | $user_id = $user_data->ID; |
||
| 593 | $user_login = $user_data->user_login; |
||
| 594 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 595 | $billing_details = wpinv_get_user_address($user_id); |
||
| 596 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 597 | |||
| 598 | if (isset($billing_details['user_id'])) { |
||
| 599 | unset($billing_details['user_id']); |
||
| 600 | } |
||
| 601 | |||
| 602 | if (isset($billing_details['email'])) { |
||
| 603 | unset($billing_details['email']); |
||
| 604 | } |
||
| 605 | |||
| 606 | $response['success'] = true; |
||
| 607 | $response['data']['id'] = $user_data->ID; |
||
| 608 | $response['data']['name'] = $user_data->user_email; |
||
| 609 | $response['data']['billing_details'] = $billing_details; |
||
| 610 | } |
||
| 611 | |||
| 612 | wp_send_json( $response ); |
||
| 613 | } |
||
| 614 | |||
| 615 | public static function run_tool() { |
||
| 616 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 617 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 618 | die(-1); |
||
| 619 | } |
||
| 620 | |||
| 621 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 622 | |||
| 623 | do_action( 'wpinv_run_tool' ); |
||
| 624 | |||
| 625 | if ( !empty( $tool ) ) { |
||
| 626 | do_action( 'wpinv_tool_' . $tool ); |
||
| 627 | } |
||
| 628 | } |
||
| 629 | |||
| 630 | public static function apply_discount() { |
||
| 677 | } |
||
| 678 | |||
| 679 | public static function remove_discount() { |
||
| 680 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 681 | |||
| 682 | $response = array(); |
||
| 683 | |||
| 684 | if ( isset( $_POST['code'] ) ) { |
||
| 685 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 686 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 687 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 703 | } |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Retrieves the markup for a payment form. |
||
| 707 | */ |
||
| 708 | public static function get_payment_form() { |
||
| 709 | |||
| 710 | // Check nonce. |
||
| 711 | if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'getpaid_ajax_form' ) ) { |
||
| 712 | _e( 'Error: Reload the page and try again.', 'invoicing' ); |
||
| 713 | exit; |
||
| 714 | } |
||
| 715 | |||
| 716 | // Is the request set up correctly? |
||
| 717 | if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) { |
||
| 718 | echo aui()->alert( |
||
| 719 | array( |
||
| 720 | 'type' => 'warning', |
||
| 721 | 'content' => __( 'No payment form or item provided', 'invoicing' ), |
||
| 722 | ) |
||
| 723 | ); |
||
| 724 | exit; |
||
| 725 | } |
||
| 726 | |||
| 727 | // Payment form or button? |
||
| 728 | if ( ! empty( $_GET['form'] ) ) { |
||
| 729 | echo getpaid_display_payment_form( $_GET['form'] ); |
||
| 730 | } else if( $_GET['invoice'] ) { |
||
| 731 | echo getpaid_display_invoice_payment_form( $_GET['invoice'] ); |
||
| 732 | } else { |
||
| 733 | $items = getpaid_convert_items_to_array( $_GET['item'] ); |
||
| 734 | echo getpaid_display_item_payment_form( $items ); |
||
| 735 | } |
||
| 736 | |||
| 737 | exit; |
||
| 738 | |||
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Payment forms. |
||
| 743 | * |
||
| 744 | * @since 1.0.18 |
||
| 745 | */ |
||
| 746 | public static function payment_form() { |
||
| 988 | } |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Payment forms. |
||
| 992 | * |
||
| 993 | * @since 1.0.18 |
||
| 994 | */ |
||
| 995 | public static function get_payment_form_states_field() { |
||
| 996 | global $invoicing; |
||
| 997 | |||
| 998 | if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
||
| 999 | exit; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | $elements = $invoicing->form_elements->get_form_elements( $_GET['form'] ); |
||
| 1003 | |||
| 1004 | if ( empty( $elements ) ) { |
||
| 1005 | exit; |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | $address_fields = array(); |
||
| 1009 | foreach ( $elements as $element ) { |
||
| 1010 | if ( 'address' === $element['type'] ) { |
||
| 1011 | $address_fields = $element; |
||
| 1012 | break; |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | if ( empty( $address_fields ) ) { |
||
| 1017 | exit; |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | foreach( $address_fields['fields'] as $address_field ) { |
||
| 1021 | |||
| 1022 | if ( 'wpinv_state' == $address_field['name'] ) { |
||
| 1023 | |||
| 1024 | $label = $address_field['label']; |
||
| 1025 | |||
| 1026 | if ( ! empty( $address_field['required'] ) ) { |
||
| 1027 | $label .= "<span class='text-danger'> *</span>"; |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | $states = wpinv_get_country_states( $_GET['country'] ); |
||
| 1031 | |||
| 1032 | if ( ! empty( $states ) ) { |
||
| 1033 | |||
| 1034 | $html = aui()->select( |
||
| 1035 | array( |
||
| 1036 | 'options' => $states, |
||
| 1037 | 'name' => esc_attr( $address_field['name'] ), |
||
| 1038 | 'id' => esc_attr( $address_field['name'] ), |
||
| 1039 | 'placeholder' => esc_attr( $address_field['placeholder'] ), |
||
| 1040 | 'required' => (bool) $address_field['required'], |
||
| 1041 | 'no_wrap' => true, |
||
| 1042 | 'label' => wp_kses_post( $label ), |
||
| 1043 | 'select2' => false, |
||
| 1044 | ) |
||
| 1045 | ); |
||
| 1046 | |||
| 1047 | } else { |
||
| 1048 | |||
| 1049 | $html = aui()->input( |
||
| 1050 | array( |
||
| 1051 | 'name' => esc_attr( $address_field['name'] ), |
||
| 1052 | 'id' => esc_attr( $address_field['name'] ), |
||
| 1053 | 'required' => (bool) $address_field['required'], |
||
| 1054 | 'label' => wp_kses_post( $label ), |
||
| 1055 | 'no_wrap' => true, |
||
| 1056 | 'type' => 'text', |
||
| 1057 | ) |
||
| 1058 | ); |
||
| 1059 | |||
| 1060 | } |
||
| 1061 | |||
| 1062 | wp_send_json_success( str_replace( 'sr-only', '', $html ) ); |
||
| 1063 | exit; |
||
| 1064 | |||
| 1065 | } |
||
| 1066 | |||
| 1067 | } |
||
| 1068 | |||
| 1069 | exit; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Payment forms. |
||
| 1074 | * |
||
| 1075 | * @since 1.0.18 |
||
| 1076 | */ |
||
| 1077 | public static function payment_form_get_taxes() { |
||
| 1078 | global $invoicing; |
||
| 1079 | |||
| 1080 | // Check nonce. |
||
| 1081 | check_ajax_referer( 'wpinv_payment_form', 'wpinv_payment_form' ); |
||
| 1082 | |||
| 1083 | // Prepare submitted data... |
||
| 1084 | $data = wp_unslash( $_POST ); |
||
| 1085 | |||
| 1086 | // ... form fields... |
||
| 1087 | if ( empty( $data['form_id'] ) || 'publish' != get_post_status( $data['form_id'] ) ) { |
||
| 1088 | exit; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | // For existing invoices. |
||
| 1092 | if ( ! empty( $data['invoice_id'] ) ) { |
||
| 1093 | $invoice = wpinv_get_invoice( $data['invoice_id'] ); |
||
| 1094 | |||
| 1095 | if ( empty( $invoice ) ) { |
||
| 1096 | exit; |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | $items = $invoicing->form_elements->convert_checkout_items( $invoice->cart_details, $invoice ); |
||
| 1100 | $country = $invoice->country; |
||
| 1101 | $state = $invoice->state; |
||
| 1102 | |||
| 1103 | } else { |
||
| 1104 | |||
| 1105 | if ( isset( $data['form_items'] ) ) { |
||
| 1106 | $items = getpaid_convert_items_to_array( $data['form_items'] ); |
||
| 1107 | $items = $invoicing->form_elements->convert_normal_items( $items ); |
||
| 1108 | } else { |
||
| 1109 | $items = $invoicing->form_elements->get_form_items( $data['form_id'] ); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | $country = wpinv_default_billing_country(); |
||
| 1113 | $state = false; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | // What we will calculate. |
||
| 1117 | $total = 0; |
||
| 1118 | $tax = 0; |
||
| 1119 | $sub_total = 0; |
||
| 1120 | |||
| 1121 | if ( ! empty( $data['wpinv_country'] ) ) { |
||
| 1122 | $country = $data['wpinv_country']; |
||
| 1123 | } |
||
| 1124 | |||
| 1125 | if ( ! empty( $data['wpinv_state'] ) ) { |
||
| 1126 | $state = $data['wpinv_state']; |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | if ( ! empty( $data['wpinv-items'] ) ) { |
||
| 1130 | |||
| 1131 | $selected_items = wpinv_clean( $data['wpinv-items'] ); |
||
| 1132 | |||
| 1133 | foreach ( $items as $item ) { |
||
| 1134 | |||
| 1135 | if ( ! isset( $selected_items[ $item['id'] ] ) ) { |
||
| 1136 | continue; |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | $quantity = empty( $item['quantity'] ) ? 1 : absint( $item['quantity'] ); |
||
| 1140 | |||
| 1141 | if ( ! empty( $item['allow_quantities'] ) && ! empty( $data["wpinv-item-{$item['id']}-quantity"] ) ) { |
||
| 1142 | |||
| 1143 | $quantity = intval( $data["wpinv-item-{$item['id']}-quantity"] ); |
||
| 1144 | |||
| 1145 | if ( 1 > $quantity ) { |
||
| 1146 | $quantity = 1; |
||
| 1147 | } |
||
| 1148 | |||
| 1149 | } |
||
| 1150 | |||
| 1151 | // Custom pricing. |
||
| 1152 | $price = wpinv_sanitize_amount( $item['price'] ); |
||
| 1153 | if ( ! empty( $item['custom_price'] ) ) { |
||
| 1154 | |||
| 1155 | $minimum_price = wpinv_sanitize_amount( $item['minimum_price'] ); |
||
| 1156 | $set_price = wpinv_sanitize_amount( $selected_items[ $item['id'] ] ); |
||
| 1157 | |||
| 1158 | if ( $set_price < $minimum_price ) { |
||
| 1159 | $set_price = $minimum_price; |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | $price = wpinv_sanitize_amount( $set_price ); |
||
| 1163 | |||
| 1164 | } |
||
| 1165 | |||
| 1166 | $price = $quantity * floatval( $price ); |
||
| 1167 | |||
| 1168 | if ( wpinv_use_taxes() ) { |
||
| 1169 | |||
| 1170 | $rate = wpinv_get_tax_rate( $country, $state, (int) $item['id'] ); |
||
| 1171 | |||
| 1172 | if ( wpinv_prices_include_tax() ) { |
||
| 1173 | $pre_tax = ( $price - $price * $rate * 0.01 ); |
||
| 1174 | $item_tax = $price - $pre_tax; |
||
| 1175 | } else { |
||
| 1176 | $pre_tax = $price; |
||
| 1177 | $item_tax = $price * $rate * 0.01; |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | $tax = $tax + $item_tax; |
||
| 1181 | $sub_total = $sub_total + $pre_tax; |
||
| 1182 | $total = $sub_total + $tax; |
||
| 1183 | |||
| 1184 | } else { |
||
| 1185 | $total = $total + $price; |
||
| 1186 | } |
||
| 1187 | |||
| 1188 | } |
||
| 1189 | |||
| 1190 | } |
||
| 1191 | |||
| 1192 | wp_send_json_success( array( |
||
| 1193 | 'total' => wpinv_price( wpinv_format_amount( $total ) ), |
||
| 1194 | 'tax' => wpinv_price( wpinv_format_amount( $tax ) ), |
||
| 1195 | 'sub_total' => wpinv_price( wpinv_format_amount( $sub_total ) ), |
||
| 1196 | )); |
||
| 1197 | exit; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Lets users buy items via ajax. |
||
| 1202 | * |
||
| 1203 | * @since 1.0.0 |
||
| 1204 | */ |
||
| 1205 | public static function buy_items() { |
||
| 1345 | } |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | 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.