| Total Complexity | 283 |
| Total Lines | 1654 |
| 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_get_taxes' => true, |
||
| 67 | 'payment_form' => true, |
||
| 68 | 'payment_form_discount' => true, |
||
| 69 | 'get_payment_form' => true, |
||
| 70 | 'get_payment_form_states_field' => true, |
||
| 71 | 'add_invoice_item' => false, |
||
| 72 | 'remove_invoice_item' => false, |
||
| 73 | 'create_invoice_item' => false, |
||
| 74 | 'get_billing_details' => false, |
||
| 75 | 'admin_recalculate_totals' => false, |
||
| 76 | 'admin_apply_discount' => false, |
||
| 77 | 'admin_remove_discount' => false, |
||
| 78 | 'check_email' => false, |
||
| 79 | 'run_tool' => false, |
||
| 80 | 'apply_discount' => true, |
||
| 81 | 'remove_discount' => true, |
||
| 82 | 'buy_items' => true, |
||
| 83 | 'payment_form_refresh_prices' => true, |
||
| 84 | ); |
||
| 85 | |||
| 86 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
| 87 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 88 | add_action( 'wp_ajax_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 89 | |||
| 90 | if ( !defined( 'WPI_AJAX_' . strtoupper( $nopriv ) ) ) { |
||
| 91 | define( 'WPI_AJAX_' . strtoupper( $nopriv ), 1 ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ( $nopriv ) { |
||
| 95 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 96 | add_action( 'wp_ajax_nopriv_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 97 | |||
| 98 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public static function add_note() { |
||
| 104 | check_ajax_referer( 'add-invoice-note', '_nonce' ); |
||
| 105 | |||
| 106 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
| 107 | die(-1); |
||
| 108 | } |
||
| 109 | |||
| 110 | $post_id = absint( $_POST['post_id'] ); |
||
| 111 | $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) ); |
||
| 112 | $note_type = sanitize_text_field( $_POST['note_type'] ); |
||
| 113 | |||
| 114 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 115 | |||
| 116 | if ( $post_id > 0 ) { |
||
| 117 | $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note ); |
||
| 118 | |||
| 119 | if ( $note_id > 0 && !is_wp_error( $note_id ) ) { |
||
| 120 | wpinv_get_invoice_note_line_item( $note_id ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | die(); |
||
| 125 | } |
||
| 126 | |||
| 127 | public static function delete_note() { |
||
| 128 | check_ajax_referer( 'delete-invoice-note', '_nonce' ); |
||
| 129 | |||
| 130 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 131 | die(-1); |
||
| 132 | } |
||
| 133 | |||
| 134 | $note_id = (int)$_POST['note_id']; |
||
| 135 | |||
| 136 | if ( $note_id > 0 ) { |
||
| 137 | wp_delete_comment( $note_id, true ); |
||
| 138 | } |
||
| 139 | |||
| 140 | die(); |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function get_states_field() { |
||
| 144 | echo wpinv_get_states_field(); |
||
| 145 | |||
| 146 | die(); |
||
| 147 | } |
||
| 148 | |||
| 149 | public static function checkout() { |
||
| 150 | if ( ! defined( 'WPINV_CHECKOUT' ) ) { |
||
| 151 | define( 'WPINV_CHECKOUT', true ); |
||
| 152 | } |
||
| 153 | |||
| 154 | wpinv_process_checkout(); |
||
| 155 | die(0); |
||
| 156 | } |
||
| 157 | |||
| 158 | public static function add_invoice_item() { |
||
| 159 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 160 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 161 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 162 | die(-1); |
||
| 163 | } |
||
| 164 | |||
| 165 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 166 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 167 | |||
| 168 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 169 | die(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 173 | if ( empty( $invoice ) ) { |
||
| 174 | die(); |
||
| 175 | } |
||
| 176 | |||
| 177 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 178 | die(); // Don't allow modify items for paid invoice. |
||
| 179 | } |
||
| 180 | |||
| 181 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 182 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 183 | } |
||
| 184 | |||
| 185 | $item = new WPInv_Item( $item_id ); |
||
| 186 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 187 | die(); |
||
| 188 | } |
||
| 189 | |||
| 190 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 191 | if ( !empty( $invoice->cart_details ) ) { |
||
| 192 | $valid = true; |
||
| 193 | |||
| 194 | if ( $recurring_item = $invoice->get_recurring() ) { |
||
| 195 | if ( $recurring_item != $item_id ) { |
||
| 196 | $valid = false; |
||
| 197 | } |
||
| 198 | } else if ( wpinv_is_recurring_item( $item_id ) ) { |
||
| 199 | $valid = false; |
||
| 200 | } |
||
| 201 | |||
| 202 | if ( !$valid ) { |
||
| 203 | $response = array(); |
||
| 204 | $response['success'] = false; |
||
| 205 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 206 | wp_send_json( $response ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | $checkout_session = wpinv_get_checkout_session(); |
||
| 211 | |||
| 212 | $data = array(); |
||
| 213 | $data['invoice_id'] = $invoice_id; |
||
| 214 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 215 | |||
| 216 | wpinv_set_checkout_session( $data ); |
||
| 217 | |||
| 218 | $quantity = wpinv_item_quantities_enabled() && !empty($_POST['qty']) && (int)$_POST['qty'] > 0 ? (int)$_POST['qty'] : 1; |
||
| 219 | |||
| 220 | $args = array( |
||
| 221 | 'id' => $item_id, |
||
| 222 | 'quantity' => $quantity, |
||
| 223 | 'item_price' => $item->get_price(), |
||
| 224 | 'custom_price' => '', |
||
| 225 | 'tax' => 0.00, |
||
| 226 | 'discount' => 0, |
||
| 227 | 'meta' => array(), |
||
| 228 | 'fees' => array() |
||
| 229 | ); |
||
| 230 | |||
| 231 | $invoice->add_item( $item_id, $args ); |
||
| 232 | $invoice->save(); |
||
| 233 | |||
| 234 | if ( empty( $_POST['country'] ) ) { |
||
| 235 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 236 | } |
||
| 237 | if ( empty( $_POST['state'] ) ) { |
||
| 238 | $_POST['state'] = $invoice->state; |
||
| 239 | } |
||
| 240 | |||
| 241 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 242 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 243 | |||
| 244 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 245 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 246 | |||
| 247 | $wpinv_ip_address_country = $invoice->country; |
||
| 248 | |||
| 249 | $invoice->recalculate_totals(true); |
||
| 250 | |||
| 251 | $response = array(); |
||
| 252 | $response['success'] = true; |
||
| 253 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 254 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 255 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 256 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 257 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 258 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 259 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 260 | $response['data']['total'] = $invoice->get_total(); |
||
| 261 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 262 | |||
| 263 | wpinv_set_checkout_session($checkout_session); |
||
| 264 | |||
| 265 | wp_send_json( $response ); |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | public static function remove_invoice_item() { |
||
| 270 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 271 | |||
| 272 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 273 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 274 | die(-1); |
||
| 275 | } |
||
| 276 | |||
| 277 | $item_id = sanitize_text_field( $_POST['item_id'] ); |
||
| 278 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 279 | $cart_index = isset( $_POST['index'] ) && $_POST['index'] >= 0 ? $_POST['index'] : false; |
||
| 280 | |||
| 281 | if ( !is_numeric( $invoice_id ) || !is_numeric( $item_id ) ) { |
||
| 282 | die(); |
||
| 283 | } |
||
| 284 | |||
| 285 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 286 | if ( empty( $invoice ) ) { |
||
| 287 | die(); |
||
| 288 | } |
||
| 289 | |||
| 290 | if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
||
| 291 | die(); // Don't allow modify items for paid invoice. |
||
| 292 | } |
||
| 293 | |||
| 294 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 295 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 296 | } |
||
| 297 | |||
| 298 | $item = new WPInv_Item( $item_id ); |
||
| 299 | if ( !( !empty( $item ) && $item->post_type == 'wpi_item' ) ) { |
||
| 300 | die(); |
||
| 301 | } |
||
| 302 | |||
| 303 | $checkout_session = wpinv_get_checkout_session(); |
||
| 304 | |||
| 305 | $data = array(); |
||
| 306 | $data['invoice_id'] = $invoice_id; |
||
| 307 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 308 | |||
| 309 | wpinv_set_checkout_session( $data ); |
||
| 310 | |||
| 311 | $args = array( |
||
| 312 | 'id' => $item_id, |
||
| 313 | 'quantity' => 1, |
||
| 314 | 'cart_index' => $cart_index |
||
| 315 | ); |
||
| 316 | |||
| 317 | $invoice->remove_item( $item_id, $args ); |
||
| 318 | $invoice->save(); |
||
| 319 | |||
| 320 | if ( empty( $_POST['country'] ) ) { |
||
| 321 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 322 | } |
||
| 323 | if ( empty( $_POST['state'] ) ) { |
||
| 324 | $_POST['state'] = $invoice->state; |
||
| 325 | } |
||
| 326 | |||
| 327 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 328 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 329 | |||
| 330 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 331 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 332 | |||
| 333 | $wpinv_ip_address_country = $invoice->country; |
||
| 334 | |||
| 335 | $invoice->recalculate_totals(true); |
||
| 336 | |||
| 337 | $response = array(); |
||
| 338 | $response['success'] = true; |
||
| 339 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 340 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 341 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 342 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 343 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 344 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 345 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 346 | $response['data']['total'] = $invoice->get_total(); |
||
| 347 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 348 | |||
| 349 | wpinv_set_checkout_session($checkout_session); |
||
| 350 | |||
| 351 | wp_send_json( $response ); |
||
| 352 | } |
||
| 353 | |||
| 354 | public static function create_invoice_item() { |
||
| 355 | check_ajax_referer( 'invoice-item', '_nonce' ); |
||
| 356 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 357 | die(-1); |
||
| 358 | } |
||
| 359 | |||
| 360 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 361 | |||
| 362 | // Find the item |
||
| 363 | if ( !is_numeric( $invoice_id ) ) { |
||
| 364 | die(); |
||
| 365 | } |
||
| 366 | |||
| 367 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 368 | if ( empty( $invoice ) ) { |
||
| 369 | die(); |
||
| 370 | } |
||
| 371 | |||
| 372 | // Validate item before adding to invoice because recurring item must be paid individually. |
||
| 373 | if ( !empty( $invoice->cart_details ) && $invoice->get_recurring() ) { |
||
| 374 | $response = array(); |
||
| 375 | $response['success'] = false; |
||
| 376 | $response['msg'] = __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ); |
||
| 377 | wp_send_json( $response ); |
||
| 378 | } |
||
| 379 | |||
| 380 | $save_item = wp_unslash( $_POST['_wpinv_quick'] ); |
||
| 381 | |||
| 382 | $meta = array(); |
||
| 383 | $meta['type'] = !empty($save_item['type']) ? sanitize_text_field($save_item['type']) : 'custom'; |
||
| 384 | $meta['price'] = !empty($save_item['price']) ? wpinv_sanitize_amount( $save_item['price'] ) : 0; |
||
| 385 | $meta['vat_rule'] = !empty($save_item['vat_rule']) ? sanitize_text_field($save_item['vat_rule']) : 'digital'; |
||
| 386 | $meta['vat_class'] = !empty($save_item['vat_class']) ? sanitize_text_field($save_item['vat_class']) : '_standard'; |
||
| 387 | |||
| 388 | $data = array(); |
||
| 389 | $data['post_title'] = sanitize_text_field($save_item['name']); |
||
| 390 | $data['post_status'] = 'publish'; |
||
| 391 | $data['post_excerpt'] = ! empty( $save_item['excerpt'] ) ? wp_kses_post( $save_item['excerpt'] ) : ''; |
||
| 392 | $data['meta'] = $meta; |
||
| 393 | |||
| 394 | $item = new WPInv_Item(); |
||
| 395 | $item->create( $data ); |
||
| 396 | |||
| 397 | if ( !empty( $item ) ) { |
||
| 398 | $_POST['item_id'] = $item->ID; |
||
| 399 | $_POST['qty'] = !empty($save_item['qty']) && $save_item['qty'] > 0 ? (int)$save_item['qty'] : 1; |
||
| 400 | |||
| 401 | self::add_invoice_item(); |
||
| 402 | } |
||
| 403 | die(); |
||
| 404 | } |
||
| 405 | |||
| 406 | public static function get_billing_details() { |
||
| 407 | check_ajax_referer( 'get-billing-details', '_nonce' ); |
||
| 408 | |||
| 409 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 410 | die(-1); |
||
| 411 | } |
||
| 412 | |||
| 413 | $user_id = (int)$_POST['user_id']; |
||
| 414 | $billing_details = wpinv_get_user_address($user_id); |
||
| 415 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 416 | |||
| 417 | if (isset($billing_details['user_id'])) { |
||
| 418 | unset($billing_details['user_id']); |
||
| 419 | } |
||
| 420 | |||
| 421 | if (isset($billing_details['email'])) { |
||
| 422 | unset($billing_details['email']); |
||
| 423 | } |
||
| 424 | |||
| 425 | $response = array(); |
||
| 426 | $response['success'] = true; |
||
| 427 | $response['data']['billing_details'] = $billing_details; |
||
| 428 | |||
| 429 | wp_send_json( $response ); |
||
| 430 | } |
||
| 431 | |||
| 432 | public static function admin_recalculate_totals() { |
||
| 433 | global $wpi_userID, $wpinv_ip_address_country; |
||
| 434 | |||
| 435 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 436 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 437 | die(-1); |
||
| 438 | } |
||
| 439 | |||
| 440 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 441 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 442 | if ( empty( $invoice ) ) { |
||
| 443 | die(); |
||
| 444 | } |
||
| 445 | |||
| 446 | $checkout_session = wpinv_get_checkout_session(); |
||
| 447 | |||
| 448 | $data = array(); |
||
| 449 | $data['invoice_id'] = $invoice_id; |
||
| 450 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 451 | |||
| 452 | wpinv_set_checkout_session( $data ); |
||
| 453 | |||
| 454 | if ( !empty( $_POST['user_id'] ) ) { |
||
| 455 | $wpi_userID = absint( $_POST['user_id'] ); |
||
| 456 | } |
||
| 457 | |||
| 458 | if ( empty( $_POST['country'] ) ) { |
||
| 459 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
||
| 460 | } |
||
| 461 | |||
| 462 | $disable_taxes = 0; |
||
| 463 | if ( ! empty( $_POST['disable_taxes'] ) ) { |
||
| 464 | $disable_taxes = 1; |
||
| 465 | } |
||
| 466 | $invoice->set( 'disable_taxes', $disable_taxes ); |
||
| 467 | |||
| 468 | $invoice->country = sanitize_text_field( $_POST['country'] ); |
||
| 469 | $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
||
| 470 | if ( isset( $_POST['state'] ) ) { |
||
| 471 | $invoice->state = sanitize_text_field( $_POST['state'] ); |
||
| 472 | $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
||
| 473 | } |
||
| 474 | |||
| 475 | $wpinv_ip_address_country = $invoice->country; |
||
| 476 | |||
| 477 | $invoice = $invoice->recalculate_totals(true); |
||
| 478 | |||
| 479 | $response = array(); |
||
| 480 | $response['success'] = true; |
||
| 481 | $response['data']['items'] = wpinv_admin_get_line_items( $invoice ); |
||
| 482 | $response['data']['subtotal'] = $invoice->get_subtotal(); |
||
| 483 | $response['data']['subtotalf'] = $invoice->get_subtotal(true); |
||
| 484 | $response['data']['tax'] = $invoice->get_tax(); |
||
| 485 | $response['data']['taxf'] = $invoice->get_tax(true); |
||
| 486 | $response['data']['discount'] = $invoice->get_discount(); |
||
| 487 | $response['data']['discountf'] = $invoice->get_discount(true); |
||
| 488 | $response['data']['total'] = $invoice->get_total(); |
||
| 489 | $response['data']['totalf'] = $invoice->get_total(true); |
||
| 490 | |||
| 491 | wpinv_set_checkout_session($checkout_session); |
||
| 492 | |||
| 493 | wp_send_json( $response ); |
||
| 494 | } |
||
| 495 | |||
| 496 | public static function admin_apply_discount() { |
||
| 497 | global $wpi_userID; |
||
| 498 | |||
| 499 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 500 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 501 | die(-1); |
||
| 502 | } |
||
| 503 | |||
| 504 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 505 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 506 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 507 | die(); |
||
| 508 | } |
||
| 509 | |||
| 510 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 511 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 512 | die(); |
||
| 513 | } |
||
| 514 | |||
| 515 | $checkout_session = wpinv_get_checkout_session(); |
||
| 516 | |||
| 517 | $data = array(); |
||
| 518 | $data['invoice_id'] = $invoice_id; |
||
| 519 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 520 | |||
| 521 | wpinv_set_checkout_session( $data ); |
||
| 522 | |||
| 523 | $response = array(); |
||
| 524 | $response['success'] = false; |
||
| 525 | $response['msg'] = __( 'This discount is invalid.', 'invoicing' ); |
||
| 526 | $response['data']['code'] = $discount_code; |
||
| 527 | |||
| 528 | if ( wpinv_is_discount_valid( $discount_code, $invoice->get_user_id() ) ) { |
||
| 529 | $discounts = wpinv_set_cart_discount( $discount_code ); |
||
| 530 | |||
| 531 | $response['success'] = true; |
||
| 532 | $response['msg'] = __( 'Discount has been applied successfully.', 'invoicing' ); |
||
| 533 | } else { |
||
| 534 | $errors = wpinv_get_errors(); |
||
| 535 | if ( !empty( $errors['wpinv-discount-error'] ) ) { |
||
| 536 | $response['msg'] = $errors['wpinv-discount-error']; |
||
| 537 | } |
||
| 538 | wpinv_unset_error( 'wpinv-discount-error' ); |
||
| 539 | } |
||
| 540 | |||
| 541 | wpinv_set_checkout_session($checkout_session); |
||
| 542 | |||
| 543 | wp_send_json( $response ); |
||
| 544 | } |
||
| 545 | |||
| 546 | public static function admin_remove_discount() { |
||
| 547 | global $wpi_userID; |
||
| 548 | |||
| 549 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 550 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 551 | die(-1); |
||
| 552 | } |
||
| 553 | |||
| 554 | $invoice_id = absint( $_POST['invoice_id'] ); |
||
| 555 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 556 | if ( empty( $invoice_id ) || empty( $discount_code ) ) { |
||
| 557 | die(); |
||
| 558 | } |
||
| 559 | |||
| 560 | $invoice = wpinv_get_invoice( $invoice_id ); |
||
| 561 | if ( empty( $invoice ) || ( !empty( $invoice ) && ( $invoice->is_paid() || $invoice->is_refunded() ) ) ) { |
||
| 562 | die(); |
||
| 563 | } |
||
| 564 | |||
| 565 | $checkout_session = wpinv_get_checkout_session(); |
||
| 566 | |||
| 567 | $data = array(); |
||
| 568 | $data['invoice_id'] = $invoice_id; |
||
| 569 | $data['cart_discounts'] = $invoice->get_discounts( true ); |
||
| 570 | |||
| 571 | wpinv_set_checkout_session( $data ); |
||
| 572 | |||
| 573 | $response = array(); |
||
| 574 | $response['success'] = false; |
||
| 575 | $response['msg'] = NULL; |
||
| 576 | |||
| 577 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 578 | $response['success'] = true; |
||
| 579 | $response['msg'] = __( 'Discount has been removed successfully.', 'invoicing' ); |
||
| 580 | |||
| 581 | wpinv_set_checkout_session($checkout_session); |
||
| 582 | |||
| 583 | wp_send_json( $response ); |
||
| 584 | } |
||
| 585 | |||
| 586 | public static function check_email() { |
||
| 587 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 588 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 589 | die(-1); |
||
| 590 | } |
||
| 591 | |||
| 592 | $email = sanitize_text_field( $_POST['email'] ); |
||
| 593 | |||
| 594 | $response = array(); |
||
| 595 | if ( is_email( $email ) && email_exists( $email ) && $user_data = get_user_by( 'email', $email ) ) { |
||
| 596 | $user_id = $user_data->ID; |
||
| 597 | $user_login = $user_data->user_login; |
||
| 598 | $display_name = $user_data->display_name ? $user_data->display_name : $user_login; |
||
| 599 | $billing_details = wpinv_get_user_address($user_id); |
||
| 600 | $billing_details = apply_filters( 'wpinv_fill_billing_details', $billing_details, $user_id ); |
||
| 601 | |||
| 602 | if (isset($billing_details['user_id'])) { |
||
| 603 | unset($billing_details['user_id']); |
||
| 604 | } |
||
| 605 | |||
| 606 | if (isset($billing_details['email'])) { |
||
| 607 | unset($billing_details['email']); |
||
| 608 | } |
||
| 609 | |||
| 610 | $response['success'] = true; |
||
| 611 | $response['data']['id'] = $user_data->ID; |
||
| 612 | $response['data']['name'] = $user_data->user_email; |
||
| 613 | $response['data']['billing_details'] = $billing_details; |
||
| 614 | } |
||
| 615 | |||
| 616 | wp_send_json( $response ); |
||
| 617 | } |
||
| 618 | |||
| 619 | public static function run_tool() { |
||
| 620 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 621 | if ( !wpinv_current_user_can_manage_invoicing() ) { |
||
| 622 | die(-1); |
||
| 623 | } |
||
| 624 | |||
| 625 | $tool = sanitize_text_field( $_POST['tool'] ); |
||
| 626 | |||
| 627 | do_action( 'wpinv_run_tool' ); |
||
| 628 | |||
| 629 | if ( !empty( $tool ) ) { |
||
| 630 | do_action( 'wpinv_tool_' . $tool ); |
||
| 631 | } |
||
| 632 | } |
||
| 633 | |||
| 634 | public static function apply_discount() { |
||
| 681 | } |
||
| 682 | |||
| 683 | public static function remove_discount() { |
||
| 684 | check_ajax_referer( 'wpinv-nonce', '_nonce' ); |
||
| 685 | |||
| 686 | $response = array(); |
||
| 687 | |||
| 688 | if ( isset( $_POST['code'] ) ) { |
||
| 689 | $discount_code = sanitize_text_field( $_POST['code'] ); |
||
| 690 | $discounts = wpinv_unset_cart_discount( $discount_code ); |
||
| 691 | $total = wpinv_get_cart_total( null, $discounts ); |
||
| 692 | $cart_totals = wpinv_recalculate_tax( true ); |
||
| 693 | |||
| 694 | if ( !empty( $cart_totals ) ) { |
||
| 695 | $response['success'] = true; |
||
| 696 | $response['data'] = $cart_totals; |
||
| 697 | $response['data']['code'] = $discount_code; |
||
| 698 | } else { |
||
| 699 | $response['success'] = false; |
||
| 700 | } |
||
| 701 | |||
| 702 | // Allow for custom discount code handling |
||
| 703 | $response = apply_filters( 'wpinv_ajax_discount_response', $response ); |
||
| 704 | } |
||
| 705 | |||
| 706 | wp_send_json( $response ); |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Retrieves the markup for a payment form. |
||
| 711 | */ |
||
| 712 | public static function get_payment_form() { |
||
| 713 | |||
| 714 | // Check nonce. |
||
| 715 | if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( $_GET['nonce'], 'getpaid_ajax_form' ) ) { |
||
| 716 | _e( 'Error: Reload the page and try again.', 'invoicing' ); |
||
| 717 | exit; |
||
| 718 | } |
||
| 719 | |||
| 720 | // Is the request set up correctly? |
||
| 721 | if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) { |
||
| 722 | echo aui()->alert( |
||
| 723 | array( |
||
| 724 | 'type' => 'warning', |
||
| 725 | 'content' => __( 'No payment form or item provided', 'invoicing' ), |
||
| 726 | ) |
||
| 727 | ); |
||
| 728 | exit; |
||
| 729 | } |
||
| 730 | |||
| 731 | // Payment form or button? |
||
| 732 | if ( ! empty( $_GET['form'] ) ) { |
||
| 733 | echo getpaid_display_payment_form( $_GET['form'] ); |
||
| 734 | } else if( $_GET['invoice'] ) { |
||
| 735 | echo getpaid_display_invoice_payment_form( $_GET['invoice'] ); |
||
| 736 | } else { |
||
| 737 | $items = getpaid_convert_items_to_array( $_GET['item'] ); |
||
| 738 | echo getpaid_display_item_payment_form( $items ); |
||
| 739 | } |
||
| 740 | |||
| 741 | exit; |
||
| 742 | |||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Payment forms. |
||
| 747 | * |
||
| 748 | * @since 1.0.18 |
||
| 749 | */ |
||
| 750 | public static function payment_form() { |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Payment forms. |
||
| 1027 | * |
||
| 1028 | * @since 1.0.18 |
||
| 1029 | */ |
||
| 1030 | public static function get_payment_form_states_field() { |
||
| 1031 | global $invoicing; |
||
| 1032 | |||
| 1033 | if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
||
| 1034 | exit; |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | $elements = $invoicing->form_elements->get_form_elements( $_GET['form'] ); |
||
| 1038 | |||
| 1039 | if ( empty( $elements ) ) { |
||
| 1040 | exit; |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | $address_fields = array(); |
||
| 1044 | foreach ( $elements as $element ) { |
||
| 1045 | if ( 'address' === $element['type'] ) { |
||
| 1046 | $address_fields = $element; |
||
| 1047 | break; |
||
| 1048 | } |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | if ( empty( $address_fields ) ) { |
||
| 1052 | exit; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | foreach( $address_fields['fields'] as $address_field ) { |
||
| 1056 | |||
| 1057 | if ( 'wpinv_state' == $address_field['name'] ) { |
||
| 1058 | |||
| 1059 | $label = $address_field['label']; |
||
| 1060 | |||
| 1061 | if ( ! empty( $address_field['required'] ) ) { |
||
| 1062 | $label .= "<span class='text-danger'> *</span>"; |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | $states = wpinv_get_country_states( $_GET['country'] ); |
||
| 1066 | |||
| 1067 | if ( ! empty( $states ) ) { |
||
| 1068 | |||
| 1069 | $html = aui()->select( |
||
| 1070 | array( |
||
| 1071 | 'options' => $states, |
||
| 1072 | 'name' => esc_attr( $address_field['name'] ), |
||
| 1073 | 'id' => esc_attr( $address_field['name'] ), |
||
| 1074 | 'placeholder' => esc_attr( $address_field['placeholder'] ), |
||
| 1075 | 'required' => (bool) $address_field['required'], |
||
| 1076 | 'no_wrap' => true, |
||
| 1077 | 'label' => wp_kses_post( $label ), |
||
| 1078 | 'select2' => false, |
||
| 1079 | ) |
||
| 1080 | ); |
||
| 1081 | |||
| 1082 | } else { |
||
| 1083 | |||
| 1084 | $html = aui()->input( |
||
| 1085 | array( |
||
| 1086 | 'name' => esc_attr( $address_field['name'] ), |
||
| 1087 | 'id' => esc_attr( $address_field['name'] ), |
||
| 1088 | 'required' => (bool) $address_field['required'], |
||
| 1089 | 'label' => wp_kses_post( $label ), |
||
| 1090 | 'no_wrap' => true, |
||
| 1091 | 'type' => 'text', |
||
| 1092 | ) |
||
| 1093 | ); |
||
| 1094 | |||
| 1095 | } |
||
| 1096 | |||
| 1097 | wp_send_json_success( str_replace( 'sr-only', '', $html ) ); |
||
| 1098 | exit; |
||
| 1099 | |||
| 1100 | } |
||
| 1101 | |||
| 1102 | } |
||
| 1103 | |||
| 1104 | exit; |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Apply taxes. |
||
| 1109 | * |
||
| 1110 | * @since 1.0.18 |
||
| 1111 | */ |
||
| 1112 | public static function payment_form_get_taxes() { |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Refresh prices. |
||
| 1261 | * |
||
| 1262 | * @since 1.0.19 |
||
| 1263 | */ |
||
| 1264 | public static function payment_form_refresh_prices() { |
||
| 1265 | |||
| 1266 | // Check nonce. |
||
| 1267 | check_ajax_referer( 'getpaid_form_nonce' ); |
||
| 1268 | |||
| 1269 | // ... form fields... |
||
| 1270 | if ( empty( $_POST['getpaid_payment_form_submission'] ) ) { |
||
| 1271 | _e( 'Error: Reload the page and try again.', 'invoicing' ); |
||
| 1272 | exit; |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | // Load the submission. |
||
| 1276 | $submission = new GetPaid_Payment_Form_Submission(); |
||
| 1277 | |||
| 1278 | // Do we have an error? |
||
| 1279 | if ( ! empty( $submission->last_error ) ) { |
||
| 1280 | echo $submission->last_error; |
||
| 1281 | exit; |
||
| 1282 | } |
||
| 1283 | |||
| 1284 | // Prepare the result. |
||
| 1285 | $result = array( |
||
| 1286 | 'submission_id' => $submission->id, |
||
| 1287 | 'has_recurring' => $submission->has_recurring, |
||
| 1288 | 'is_free' => $submission->get_payment_details(), |
||
| 1289 | 'totals' => array( |
||
| 1290 | 'subtotal' => wpinv_price( wpinv_format_amount( $submission->subtotal_amount ), $submission->get_currency() ), |
||
| 1291 | 'discount' => wpinv_price( wpinv_format_amount( $submission->get_total_discount() ), $submission->get_currency() ), |
||
| 1292 | 'fees' => wpinv_price( wpinv_format_amount( $submission->get_total_fees() ), $submission->get_currency() ), |
||
| 1293 | 'tax' => wpinv_price( wpinv_format_amount( $submission->get_total_tax() ), $submission->get_currency() ), |
||
| 1294 | 'total' => wpinv_price( wpinv_format_amount( $submission->get_total() ), $submission->get_currency() ), |
||
| 1295 | ), |
||
| 1296 | ); |
||
| 1297 | |||
| 1298 | // Add items. |
||
| 1299 | $items = $submission->get_items(); |
||
| 1300 | if ( ! empty( $items ) ) { |
||
| 1301 | $result['items'] = array(); |
||
| 1302 | |||
| 1303 | foreach( $items as $item_id => $item ) { |
||
| 1304 | $result['items']["$item_id"] = wpinv_price( wpinv_format_amount( $item->get_price() * $item->get_qantity() ) ); |
||
| 1305 | } |
||
| 1306 | } |
||
| 1307 | |||
| 1308 | // Add invoice. |
||
| 1309 | if ( $submission->has_invoice() ) { |
||
| 1310 | $result['invoice'] = $submission->get_invoice()->ID; |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | // Add discount code. |
||
| 1314 | if ( $submission->has_discount_code() ) { |
||
| 1315 | $result['discount_code'] = $submission->get_discount_code(); |
||
| 1316 | } |
||
| 1317 | |||
| 1318 | // Filter the result. |
||
| 1319 | $result = apply_filters( 'getpaid_payment_form_ajax_refresh_prices', $result, $submission ); |
||
| 1320 | |||
| 1321 | wp_send_json_success( $result ); |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Apply discounts. |
||
| 1326 | * |
||
| 1327 | * @since 1.0.19 |
||
| 1328 | */ |
||
| 1329 | public static function payment_form_discount() { |
||
| 1330 | |||
| 1331 | // Check nonce. |
||
| 1332 | check_ajax_referer( 'getpaid_form_nonce' ); |
||
| 1333 | |||
| 1334 | // Prepare submitted data... |
||
| 1335 | $data = wp_unslash( $_POST ); |
||
| 1336 | |||
| 1337 | // ... form fields... |
||
| 1338 | if ( empty( $data['form_id'] ) || 'publish' != get_post_status( $data['form_id'] ) ) { |
||
| 1339 | exit; |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | // Do we have a discount? |
||
| 1343 | if ( empty( $data['discount'] ) ) { |
||
| 1344 | _e( 'Please enter your discount code', 'invoicing' ); |
||
| 1345 | exit; |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | // Validate discount. |
||
| 1349 | $data = self::payment_form_validate_discount( $data ); |
||
| 1350 | |||
| 1351 | if ( false === $data ) { |
||
| 1352 | _e( 'There was an error applying your discount code', 'invoicing' ); |
||
| 1353 | exit; |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | if ( is_string( $data ) ) { |
||
| 1357 | echo $data; |
||
| 1358 | exit; |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | $data['total'] = wpinv_price( wpinv_format_amount( $data['total'] ) ); |
||
| 1362 | $data['sub_total'] = wpinv_price( wpinv_format_amount( $data['sub_total'] ) ); |
||
| 1363 | $data['discount'] = wpinv_price( wpinv_format_amount( $data['discount'] ) ); |
||
| 1364 | $data['tax'] = wpinv_price( wpinv_format_amount( $data['tax'] ) ); |
||
| 1365 | |||
| 1366 | wp_send_json_success( $data ); |
||
| 1367 | exit; |
||
| 1368 | |||
| 1369 | } |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * Validates discounts. |
||
| 1373 | * |
||
| 1374 | * @since 1.0.19 |
||
| 1375 | */ |
||
| 1376 | public static function payment_form_validate_discount( $data ) { |
||
| 1520 | |||
| 1521 | } |
||
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Lets users buy items via ajax. |
||
| 1525 | * |
||
| 1526 | * @since 1.0.0 |
||
| 1527 | */ |
||
| 1528 | public static function buy_items() { |
||
| 1668 | } |
||
| 1669 | } |
||
| 1671 | 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.