| Conditions | 109 |
| Paths | > 20000 |
| Total Lines | 383 |
| Code Lines | 210 |
| Lines | 12 |
| Ratio | 3.13 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 367 | public function process_checkout() { |
||
| 368 | try { |
||
| 369 | if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) { |
||
| 370 | WC()->session->set( 'refresh_totals', true ); |
||
| 371 | throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) ); |
||
| 372 | } |
||
| 373 | |||
| 374 | if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
||
| 375 | define( 'WOOCOMMERCE_CHECKOUT', true ); |
||
| 376 | } |
||
| 377 | |||
| 378 | // Prevent timeout |
||
| 379 | @set_time_limit( 0 ); |
||
| 380 | |||
| 381 | do_action( 'woocommerce_before_checkout_process' ); |
||
| 382 | |||
| 383 | if ( WC()->cart->is_empty() ) { |
||
| 384 | throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'shop' ) ) ) ); |
||
| 385 | } |
||
| 386 | |||
| 387 | do_action( 'woocommerce_checkout_process' ); |
||
| 388 | |||
| 389 | // Checkout fields (not defined in checkout_fields) |
||
| 390 | $this->posted['terms'] = isset( $_POST['terms'] ) ? 1 : 0; |
||
| 391 | $this->posted['createaccount'] = isset( $_POST['createaccount'] ) && ! empty( $_POST['createaccount'] ) ? 1 : 0; |
||
| 392 | $this->posted['payment_method'] = isset( $_POST['payment_method'] ) ? stripslashes( $_POST['payment_method'] ) : ''; |
||
| 393 | $this->posted['shipping_method'] = isset( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : ''; |
||
| 394 | $this->posted['ship_to_different_address'] = ! empty( $_POST['ship_to_different_address'] ); |
||
| 395 | |||
| 396 | if ( isset( $_POST['shiptobilling'] ) ) { |
||
| 397 | _deprecated_argument( 'WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. The template files are out of date' ); |
||
| 398 | |||
| 399 | $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true; |
||
| 400 | } |
||
| 401 | |||
| 402 | // Ship to billing only option |
||
| 403 | if ( wc_ship_to_billing_address_only() ) { |
||
| 404 | $this->posted['ship_to_different_address'] = false; |
||
| 405 | } |
||
| 406 | |||
| 407 | // Update customer shipping and payment method to posted method |
||
| 408 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
||
| 409 | |||
| 410 | if ( isset( $this->posted['shipping_method'] ) && is_array( $this->posted['shipping_method'] ) ) { |
||
| 411 | foreach ( $this->posted['shipping_method'] as $i => $value ) { |
||
| 412 | $chosen_shipping_methods[ $i ] = wc_clean( $value ); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
||
| 417 | WC()->session->set( 'chosen_payment_method', $this->posted['payment_method'] ); |
||
| 418 | |||
| 419 | // Note if we skip shipping |
||
| 420 | $skipped_shipping = false; |
||
| 421 | |||
| 422 | // Get posted checkout_fields and do validation |
||
| 423 | foreach ( $this->checkout_fields as $fieldset_key => $fieldset ) { |
||
| 424 | |||
| 425 | // Skip shipping if not needed |
||
| 426 | if ( 'shipping' === $fieldset_key && ( false == $this->posted['ship_to_different_address'] || ! WC()->cart->needs_shipping_address() ) ) { |
||
| 427 | $skipped_shipping = true; |
||
| 428 | continue; |
||
| 429 | } |
||
| 430 | |||
| 431 | // Skip account if not needed |
||
| 432 | if ( 'account' === $fieldset_key && ( is_user_logged_in() || ( false === $this->must_create_account && empty( $this->posted['createaccount'] ) ) ) ) { |
||
| 433 | continue; |
||
| 434 | } |
||
| 435 | |||
| 436 | foreach ( $fieldset as $key => $field ) { |
||
| 437 | |||
| 438 | if ( ! isset( $field['type'] ) ) { |
||
| 439 | $field['type'] = 'text'; |
||
| 440 | } |
||
| 441 | |||
| 442 | // Get Value |
||
| 443 | switch ( $field['type'] ) { |
||
| 444 | case "checkbox" : |
||
| 445 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0; |
||
| 446 | break; |
||
| 447 | case "multiselect" : |
||
| 448 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? implode( ', ', array_map( 'wc_clean', $_POST[ $key ] ) ) : ''; |
||
| 449 | break; |
||
| 450 | case "textarea" : |
||
| 451 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? wp_strip_all_tags( wp_check_invalid_utf8( stripslashes( $_POST[ $key ] ) ) ) : ''; |
||
| 452 | break; |
||
| 453 | default : |
||
| 454 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? ( is_array( $_POST[ $key ] ) ? array_map( 'wc_clean', $_POST[ $key ] ) : wc_clean( $_POST[ $key ] ) ) : ''; |
||
| 455 | break; |
||
| 456 | } |
||
| 457 | |||
| 458 | // Hooks to allow modification of value |
||
| 459 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_' . sanitize_title( $field['type'] ) . '_field', $this->posted[ $key ] ); |
||
| 460 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[ $key ] ); |
||
| 461 | |||
| 462 | // Validation: Required fields |
||
| 463 | if ( isset( $field['required'] ) && $field['required'] && ( ! isset( $this->posted[ $key ] ) || "" === $this->posted[ $key ] ) ) { |
||
| 464 | switch ( $fieldset_key ) { |
||
| 465 | case 'shipping' : |
||
| 466 | $field_label = sprintf( _x( 'Shipping %s', 'Shipping FIELDNAME', 'woocommerce' ), $field['label'] ); |
||
| 467 | break; |
||
| 468 | case 'billing' : |
||
| 469 | $field_label = sprintf( _x( 'Billing %s', 'Billing FIELDNAME', 'woocommerce' ), $field['label'] ); |
||
| 470 | break; |
||
| 471 | default : |
||
| 472 | $field_label = $field['label']; |
||
| 473 | break; |
||
| 474 | } |
||
| 475 | wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( _x( '%s is a required field.', 'FIELDNAME is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ), 'error' ); |
||
| 476 | } |
||
| 477 | |||
| 478 | if ( ! empty( $this->posted[ $key ] ) ) { |
||
| 479 | |||
| 480 | // Validation rules |
||
| 481 | if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) { |
||
| 482 | foreach ( $field['validate'] as $rule ) { |
||
| 483 | switch ( $rule ) { |
||
| 484 | case 'postcode' : |
||
| 485 | $this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) ); |
||
| 486 | |||
| 487 | if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ) ) : |
||
| 488 | wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' ); |
||
| 489 | else : |
||
| 490 | $this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ); |
||
| 491 | endif; |
||
| 492 | break; |
||
| 493 | View Code Duplication | case 'phone' : |
|
| 494 | $this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] ); |
||
| 495 | |||
| 496 | if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) ) |
||
| 497 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' ); |
||
| 498 | break; |
||
| 499 | View Code Duplication | case 'email' : |
|
| 500 | $this->posted[ $key ] = strtolower( $this->posted[ $key ] ); |
||
| 501 | |||
| 502 | if ( ! is_email( $this->posted[ $key ] ) ) |
||
| 503 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' ); |
||
| 504 | break; |
||
| 505 | case 'state' : |
||
| 506 | // Get valid states |
||
| 507 | $valid_states = WC()->countries->get_states( isset( $_POST[ $fieldset_key . '_country' ] ) ? $_POST[ $fieldset_key . '_country' ] : ( 'billing' === $fieldset_key ? WC()->customer->get_billing_country() : WC()->customer->get_shipping_country() ) ); |
||
| 508 | |||
| 509 | if ( ! empty( $valid_states ) && is_array( $valid_states ) ) { |
||
| 510 | $valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) ); |
||
| 511 | |||
| 512 | // Convert value to key if set |
||
| 513 | if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) ) { |
||
| 514 | $this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ]; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | // Only validate if the country has specific state options |
||
| 519 | if ( ! empty( $valid_states ) && is_array( $valid_states ) && sizeof( $valid_states ) > 0 ) { |
||
| 520 | if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) ) { |
||
| 521 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ), 'error' ); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | break; |
||
| 525 | } |
||
| 526 | } |
||
| 527 | } |
||
| 528 | } |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 533 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 534 | WC()->customer->set_billing_country( $this->posted['billing_country'] ); |
||
| 535 | } |
||
| 536 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 537 | WC()->customer->set_billing_state( $this->posted['billing_state'] ); |
||
| 538 | } |
||
| 539 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 540 | WC()->customer->set_billing_postcode( $this->posted['billing_postcode'] ); |
||
| 541 | } |
||
| 542 | |||
| 543 | // Shipping Information |
||
| 544 | if ( ! $skipped_shipping ) { |
||
| 545 | |||
| 546 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 547 | if ( isset( $this->posted['shipping_country'] ) ) { |
||
| 548 | WC()->customer->set_shipping_country( $this->posted['shipping_country'] ); |
||
| 549 | } |
||
| 550 | if ( isset( $this->posted['shipping_state'] ) ) { |
||
| 551 | WC()->customer->set_shipping_state( $this->posted['shipping_state'] ); |
||
| 552 | } |
||
| 553 | if ( isset( $this->posted['shipping_postcode'] ) ) { |
||
| 554 | WC()->customer->set_shipping_postcode( $this->posted['shipping_postcode'] ); |
||
| 555 | } |
||
| 556 | } else { |
||
| 557 | |||
| 558 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 559 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 560 | WC()->customer->set_shipping_country( $this->posted['billing_country'] ); |
||
| 561 | } |
||
| 562 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 563 | WC()->customer->set_shipping_state( $this->posted['billing_state'] ); |
||
| 564 | } |
||
| 565 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 566 | WC()->customer->set_shipping_postcode( $this->posted['billing_postcode'] ); |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | WC()->customer->save(); |
||
| 571 | |||
| 572 | // Update cart totals now we have customer address |
||
| 573 | WC()->cart->calculate_totals(); |
||
| 574 | |||
| 575 | // Terms |
||
| 576 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && wc_get_page_id( 'terms' ) > 0 && apply_filters( 'woocommerce_checkout_show_terms', true ) ) { |
||
| 577 | wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' ); |
||
| 578 | } |
||
| 579 | |||
| 580 | if ( WC()->cart->needs_shipping() ) { |
||
| 581 | $shipping_country = WC()->customer->get_shipping_country(); |
||
| 582 | |||
| 583 | if ( empty( $shipping_country ) ) { |
||
| 584 | wc_add_notice( __( 'Please enter an address to continue.', 'woocommerce' ), 'error' ); |
||
| 585 | } elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) { |
||
| 586 | wc_add_notice( sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ), 'error' ); |
||
| 587 | } |
||
| 588 | |||
| 589 | // Validate Shipping Methods |
||
| 590 | $packages = WC()->shipping->get_packages(); |
||
| 591 | $this->shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); |
||
| 592 | |||
| 593 | foreach ( $packages as $i => $package ) { |
||
| 594 | if ( ! isset( $package['rates'][ $this->shipping_methods[ $i ] ] ) ) { |
||
| 595 | wc_add_notice( __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce' ), 'error' ); |
||
| 596 | $this->shipping_methods[ $i ] = ''; |
||
| 597 | } |
||
| 598 | } |
||
| 599 | } |
||
| 600 | |||
| 601 | if ( WC()->cart->needs_payment() ) { |
||
| 602 | // Payment Method |
||
| 603 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 604 | |||
| 605 | if ( ! isset( $available_gateways[ $this->posted['payment_method'] ] ) ) { |
||
| 606 | $this->payment_method = ''; |
||
| 607 | wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' ); |
||
| 608 | } else { |
||
| 609 | $this->payment_method = $available_gateways[ $this->posted['payment_method'] ]; |
||
| 610 | $this->payment_method->validate_fields(); |
||
| 611 | } |
||
| 612 | } else { |
||
| 613 | $available_gateways = array(); |
||
| 614 | } |
||
| 615 | |||
| 616 | // Action after validation |
||
| 617 | do_action( 'woocommerce_after_checkout_validation', $this->posted ); |
||
| 618 | |||
| 619 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && wc_notice_count( 'error' ) == 0 ) { |
||
| 620 | |||
| 621 | // Customer accounts |
||
| 622 | $this->customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ); |
||
| 623 | |||
| 624 | if ( ! is_user_logged_in() && ( $this->must_create_account || ! empty( $this->posted['createaccount'] ) ) ) { |
||
| 625 | |||
| 626 | $username = ! empty( $this->posted['account_username'] ) ? $this->posted['account_username'] : ''; |
||
| 627 | $password = ! empty( $this->posted['account_password'] ) ? $this->posted['account_password'] : ''; |
||
| 628 | $new_customer = wc_create_new_customer( $this->posted['billing_email'], $username, $password ); |
||
| 629 | |||
| 630 | if ( is_wp_error( $new_customer ) ) { |
||
| 631 | throw new Exception( $new_customer->get_error_message() ); |
||
| 632 | } else { |
||
| 633 | $this->customer_id = absint( $new_customer ); |
||
| 634 | } |
||
| 635 | |||
| 636 | wc_set_customer_auth_cookie( $this->customer_id ); |
||
| 637 | |||
| 638 | // As we are now logged in, checkout will need to refresh to show logged in data |
||
| 639 | WC()->session->set( 'reload_checkout', true ); |
||
| 640 | |||
| 641 | // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering |
||
| 642 | WC()->cart->calculate_totals(); |
||
| 643 | |||
| 644 | // Add customer info from other billing fields |
||
| 645 | if ( $this->posted['billing_first_name'] && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) { |
||
| 646 | $userdata = array( |
||
| 647 | 'ID' => $this->customer_id, |
||
| 648 | 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', |
||
| 649 | 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', |
||
| 650 | 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', |
||
| 651 | ); |
||
| 652 | wp_update_user( apply_filters( 'woocommerce_checkout_customer_userdata', $userdata, $this ) ); |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 656 | // Do a final stock check at this point |
||
| 657 | $this->check_cart_items(); |
||
| 658 | |||
| 659 | // Abort if errors are present |
||
| 660 | if ( wc_notice_count( 'error' ) > 0 ) { |
||
| 661 | return false; |
||
| 662 | } |
||
| 663 | |||
| 664 | $order_id = $this->create_order(); |
||
| 665 | |||
| 666 | if ( is_wp_error( $order_id ) ) { |
||
| 667 | throw new Exception( $order_id->get_error_message() ); |
||
| 668 | } |
||
| 669 | |||
| 670 | do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted ); |
||
| 671 | |||
| 672 | // Process payment |
||
| 673 | if ( WC()->cart->needs_payment() ) { |
||
| 674 | |||
| 675 | // Store Order ID in session so it can be re-used after payment failure |
||
| 676 | WC()->session->order_awaiting_payment = $order_id; |
||
| 677 | |||
| 678 | // Process Payment |
||
| 679 | $result = $available_gateways[ $this->posted['payment_method'] ]->process_payment( $order_id ); |
||
| 680 | |||
| 681 | // Redirect to success/confirmation/payment page |
||
| 682 | if ( isset( $result['result'] ) && 'success' === $result['result'] ) { |
||
| 683 | |||
| 684 | $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); |
||
| 685 | |||
| 686 | if ( is_ajax() ) { |
||
| 687 | wp_send_json( $result ); |
||
| 688 | } else { |
||
| 689 | wp_redirect( $result['redirect'] ); |
||
| 690 | exit; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | } else { |
||
| 694 | |||
| 695 | if ( empty( $order ) ) { |
||
| 696 | $order = wc_get_order( $order_id ); |
||
| 697 | } |
||
| 698 | |||
| 699 | // No payment was required for order |
||
| 700 | $order->payment_complete(); |
||
| 701 | |||
| 702 | // Empty the Cart |
||
| 703 | WC()->cart->empty_cart(); |
||
| 704 | |||
| 705 | // Get redirect |
||
| 706 | $return_url = $order->get_checkout_order_received_url(); |
||
| 707 | |||
| 708 | // Redirect to success/confirmation/payment page |
||
| 709 | if ( is_ajax() ) { |
||
| 710 | wp_send_json( array( |
||
| 711 | 'result' => 'success', |
||
| 712 | 'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ), |
||
| 713 | ) ); |
||
| 714 | } else { |
||
| 715 | wp_safe_redirect( |
||
| 716 | apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ) |
||
| 717 | ); |
||
| 718 | exit; |
||
| 719 | } |
||
| 720 | } |
||
| 721 | } |
||
| 722 | } catch ( Exception $e ) { |
||
| 723 | if ( ! empty( $e ) ) { |
||
| 724 | wc_add_notice( $e->getMessage(), 'error' ); |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | // If we reached this point then there were errors |
||
| 729 | if ( is_ajax() ) { |
||
| 730 | |||
| 731 | // only print notices if not reloading the checkout, otherwise they're lost in the page reload |
||
| 732 | if ( ! isset( WC()->session->reload_checkout ) ) { |
||
| 733 | ob_start(); |
||
| 734 | wc_print_notices(); |
||
| 735 | $messages = ob_get_clean(); |
||
| 736 | } |
||
| 737 | |||
| 738 | $response = array( |
||
| 739 | 'result' => 'failure', |
||
| 740 | 'messages' => isset( $messages ) ? $messages : '', |
||
| 741 | 'refresh' => isset( WC()->session->refresh_totals ) ? 'true' : 'false', |
||
| 742 | 'reload' => isset( WC()->session->reload_checkout ) ? 'true' : 'false', |
||
| 743 | ); |
||
| 744 | |||
| 745 | unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); |
||
| 746 | |||
| 747 | wp_send_json( $response ); |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 829 |
This check marks private properties in classes that are never used. Those properties can be removed.