| Conditions | 106 |
| Paths | > 20000 |
| Total Lines | 373 |
| Code Lines | 195 |
| Lines | 12 |
| Ratio | 3.22 % |
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 |
||
| 352 | public function process_checkout() { |
||
| 353 | try { |
||
| 354 | if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) { |
||
| 355 | WC()->session->set( 'refresh_totals', true ); |
||
| 356 | throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) ); |
||
| 357 | } |
||
| 358 | |||
| 359 | if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
||
| 360 | define( 'WOOCOMMERCE_CHECKOUT', true ); |
||
| 361 | } |
||
| 362 | |||
| 363 | // Prevent timeout |
||
| 364 | @set_time_limit(0); |
||
|
1 ignored issue
–
show
|
|||
| 365 | |||
| 366 | do_action( 'woocommerce_before_checkout_process' ); |
||
| 367 | |||
| 368 | if ( WC()->cart->is_empty() ) { |
||
| 369 | throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce' ), home_url() ) ); |
||
| 370 | } |
||
| 371 | |||
| 372 | do_action( 'woocommerce_checkout_process' ); |
||
| 373 | |||
| 374 | // Checkout fields (not defined in checkout_fields) |
||
| 375 | $this->posted['terms'] = isset( $_POST['terms'] ) ? 1 : 0; |
||
| 376 | $this->posted['createaccount'] = isset( $_POST['createaccount'] ) && ! empty( $_POST['createaccount'] ) ? 1 : 0; |
||
| 377 | $this->posted['payment_method'] = isset( $_POST['payment_method'] ) ? stripslashes( $_POST['payment_method'] ) : ''; |
||
| 378 | $this->posted['shipping_method'] = isset( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : ''; |
||
| 379 | $this->posted['ship_to_different_address'] = isset( $_POST['ship_to_different_address'] ) ? true : false; |
||
| 380 | |||
| 381 | if ( isset( $_POST['shiptobilling'] ) ) { |
||
| 382 | _deprecated_argument( 'WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. The template files are out of date' ); |
||
| 383 | |||
| 384 | $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true; |
||
| 385 | } |
||
| 386 | |||
| 387 | // Ship to billing only option |
||
| 388 | if ( wc_ship_to_billing_address_only() ) { |
||
| 389 | $this->posted['ship_to_different_address'] = false; |
||
| 390 | } |
||
| 391 | |||
| 392 | // Update customer shipping and payment method to posted method |
||
| 393 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
||
| 394 | |||
| 395 | if ( isset( $this->posted['shipping_method'] ) && is_array( $this->posted['shipping_method'] ) ) { |
||
| 396 | foreach ( $this->posted['shipping_method'] as $i => $value ) { |
||
| 397 | $chosen_shipping_methods[ $i ] = wc_clean( $value ); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
||
| 402 | WC()->session->set( 'chosen_payment_method', $this->posted['payment_method'] ); |
||
| 403 | |||
| 404 | // Note if we skip shipping |
||
| 405 | $skipped_shipping = false; |
||
| 406 | |||
| 407 | // Get posted checkout_fields and do validation |
||
| 408 | foreach ( $this->checkout_fields as $fieldset_key => $fieldset ) { |
||
| 409 | |||
| 410 | // Skip shipping if not needed |
||
| 411 | if ( $fieldset_key == 'shipping' && ( $this->posted['ship_to_different_address'] == false || ! WC()->cart->needs_shipping_address() ) ) { |
||
| 412 | $skipped_shipping = true; |
||
| 413 | continue; |
||
| 414 | } |
||
| 415 | |||
| 416 | // Skip account if not needed |
||
| 417 | if ( $fieldset_key == 'account' && ( is_user_logged_in() || ( $this->must_create_account == false && empty( $this->posted['createaccount'] ) ) ) ) { |
||
| 418 | continue; |
||
| 419 | } |
||
| 420 | |||
| 421 | foreach ( $fieldset as $key => $field ) { |
||
| 422 | |||
| 423 | if ( ! isset( $field['type'] ) ) { |
||
| 424 | $field['type'] = 'text'; |
||
| 425 | } |
||
| 426 | |||
| 427 | // Get Value |
||
| 428 | switch ( $field['type'] ) { |
||
| 429 | case "checkbox" : |
||
| 430 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0; |
||
| 431 | break; |
||
| 432 | case "multiselect" : |
||
| 433 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? implode( ', ', array_map( 'wc_clean', $_POST[ $key ] ) ) : ''; |
||
| 434 | break; |
||
| 435 | case "textarea" : |
||
| 436 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? wp_strip_all_tags( wp_check_invalid_utf8( stripslashes( $_POST[ $key ] ) ) ) : ''; |
||
| 437 | break; |
||
| 438 | default : |
||
| 439 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? ( is_array( $_POST[ $key ] ) ? array_map( 'wc_clean', $_POST[ $key ] ) : wc_clean( $_POST[ $key ] ) ) : ''; |
||
| 440 | break; |
||
| 441 | } |
||
| 442 | |||
| 443 | // Hooks to allow modification of value |
||
| 444 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_' . sanitize_title( $field['type'] ) . '_field', $this->posted[ $key ] ); |
||
| 445 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[ $key ] ); |
||
| 446 | |||
| 447 | // Validation: Required fields |
||
| 448 | if ( isset( $field['required'] ) && $field['required'] && empty( $this->posted[ $key ] ) ) { |
||
| 449 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' ); |
||
| 450 | } |
||
| 451 | |||
| 452 | if ( ! empty( $this->posted[ $key ] ) ) { |
||
| 453 | |||
| 454 | // Validation rules |
||
| 455 | if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) { |
||
| 456 | foreach ( $field['validate'] as $rule ) { |
||
| 457 | switch ( $rule ) { |
||
| 458 | case 'postcode' : |
||
| 459 | $this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) ); |
||
| 460 | |||
| 461 | if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ) ) : |
||
| 462 | wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' ); |
||
| 463 | else : |
||
| 464 | $this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ); |
||
| 465 | endif; |
||
| 466 | break; |
||
| 467 | View Code Duplication | case 'phone' : |
|
| 468 | $this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] ); |
||
| 469 | |||
| 470 | if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) ) |
||
| 471 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' ); |
||
| 472 | break; |
||
| 473 | View Code Duplication | case 'email' : |
|
| 474 | $this->posted[ $key ] = strtolower( $this->posted[ $key ] ); |
||
| 475 | |||
| 476 | if ( ! is_email( $this->posted[ $key ] ) ) |
||
| 477 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' ); |
||
| 478 | break; |
||
| 479 | case 'state' : |
||
| 480 | // Get valid states |
||
| 481 | $valid_states = WC()->countries->get_states( isset( $_POST[ $fieldset_key . '_country' ] ) ? $_POST[ $fieldset_key . '_country' ] : ( 'billing' === $fieldset_key ? WC()->customer->get_country() : WC()->customer->get_shipping_country() ) ); |
||
| 482 | |||
| 483 | if ( ! empty( $valid_states ) && is_array( $valid_states ) ) { |
||
| 484 | $valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) ); |
||
| 485 | |||
| 486 | // Convert value to key if set |
||
| 487 | if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) ) { |
||
| 488 | $this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ]; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | // Only validate if the country has specific state options |
||
| 493 | if ( ! empty( $valid_states ) && is_array( $valid_states ) && sizeof( $valid_states ) > 0 ) { |
||
| 494 | if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) ) { |
||
| 495 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ), 'error' ); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | break; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 507 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 508 | WC()->customer->set_country( $this->posted['billing_country'] ); |
||
| 509 | } |
||
| 510 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 511 | WC()->customer->set_state( $this->posted['billing_state'] ); |
||
| 512 | } |
||
| 513 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 514 | WC()->customer->set_postcode( $this->posted['billing_postcode'] ); |
||
| 515 | } |
||
| 516 | |||
| 517 | // Shipping Information |
||
| 518 | if ( ! $skipped_shipping ) { |
||
| 519 | |||
| 520 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 521 | if ( isset( $this->posted['shipping_country'] ) ) { |
||
| 522 | WC()->customer->set_shipping_country( $this->posted['shipping_country'] ); |
||
| 523 | } |
||
| 524 | if ( isset( $this->posted['shipping_state'] ) ) { |
||
| 525 | WC()->customer->set_shipping_state( $this->posted['shipping_state'] ); |
||
| 526 | } |
||
| 527 | if ( isset( $this->posted['shipping_postcode'] ) ) { |
||
| 528 | WC()->customer->set_shipping_postcode( $this->posted['shipping_postcode'] ); |
||
| 529 | } |
||
| 530 | |||
| 531 | } else { |
||
| 532 | |||
| 533 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 534 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 535 | WC()->customer->set_shipping_country( $this->posted['billing_country'] ); |
||
| 536 | } |
||
| 537 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 538 | WC()->customer->set_shipping_state( $this->posted['billing_state'] ); |
||
| 539 | } |
||
| 540 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 541 | WC()->customer->set_shipping_postcode( $this->posted['billing_postcode'] ); |
||
| 542 | } |
||
| 543 | |||
| 544 | } |
||
| 545 | |||
| 546 | // Update cart totals now we have customer address |
||
| 547 | WC()->cart->calculate_totals(); |
||
| 548 | |||
| 549 | // Terms |
||
| 550 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && wc_get_page_id( 'terms' ) > 0 && apply_filters( 'woocommerce_checkout_show_terms', true ) ) { |
||
| 551 | wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' ); |
||
| 552 | } |
||
| 553 | |||
| 554 | if ( WC()->cart->needs_shipping() ) { |
||
| 555 | |||
| 556 | if ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) { |
||
| 557 | 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' ); |
||
| 558 | } |
||
| 559 | |||
| 560 | // Validate Shipping Methods |
||
| 561 | $packages = WC()->shipping->get_packages(); |
||
| 562 | $this->shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
||
| 563 | |||
| 564 | foreach ( $packages as $i => $package ) { |
||
| 565 | if ( ! isset( $package['rates'][ $this->shipping_methods[ $i ] ] ) ) { |
||
| 566 | wc_add_notice( __( 'Invalid shipping method.', 'woocommerce' ), 'error' ); |
||
| 567 | $this->shipping_methods[ $i ] = ''; |
||
| 568 | } |
||
| 569 | } |
||
| 570 | } |
||
| 571 | |||
| 572 | if ( WC()->cart->needs_payment() ) { |
||
| 573 | // Payment Method |
||
| 574 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 575 | |||
| 576 | if ( ! isset( $available_gateways[ $this->posted['payment_method'] ] ) ) { |
||
| 577 | $this->payment_method = ''; |
||
| 578 | wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' ); |
||
| 579 | } else { |
||
| 580 | $this->payment_method = $available_gateways[ $this->posted['payment_method'] ]; |
||
| 581 | $this->payment_method->validate_fields(); |
||
| 582 | } |
||
| 583 | } else { |
||
| 584 | $available_gateways = array(); |
||
| 585 | } |
||
| 586 | |||
| 587 | // Action after validation |
||
| 588 | do_action( 'woocommerce_after_checkout_validation', $this->posted ); |
||
| 589 | |||
| 590 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && wc_notice_count( 'error' ) == 0 ) { |
||
| 591 | |||
| 592 | // Customer accounts |
||
| 593 | $this->customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ); |
||
| 594 | |||
| 595 | if ( ! is_user_logged_in() && ( $this->must_create_account || ! empty( $this->posted['createaccount'] ) ) ) { |
||
| 596 | |||
| 597 | $username = ! empty( $this->posted['account_username'] ) ? $this->posted['account_username'] : ''; |
||
| 598 | $password = ! empty( $this->posted['account_password'] ) ? $this->posted['account_password'] : ''; |
||
| 599 | $new_customer = wc_create_new_customer( $this->posted['billing_email'], $username, $password ); |
||
| 600 | |||
| 601 | if ( is_wp_error( $new_customer ) ) { |
||
| 602 | throw new Exception( $new_customer->get_error_message() ); |
||
| 603 | } |
||
| 604 | |||
| 605 | $this->customer_id = $new_customer; |
||
| 606 | |||
| 607 | wc_set_customer_auth_cookie( $this->customer_id ); |
||
| 608 | |||
| 609 | // As we are now logged in, checkout will need to refresh to show logged in data |
||
| 610 | WC()->session->set( 'reload_checkout', true ); |
||
| 611 | |||
| 612 | // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering |
||
| 613 | WC()->cart->calculate_totals(); |
||
| 614 | |||
| 615 | // Add customer info from other billing fields |
||
| 616 | if ( $this->posted['billing_first_name'] && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) { |
||
| 617 | $userdata = array( |
||
| 618 | 'ID' => $this->customer_id, |
||
| 619 | 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', |
||
| 620 | 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', |
||
| 621 | 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '' |
||
| 622 | ); |
||
| 623 | wp_update_user( apply_filters( 'woocommerce_checkout_customer_userdata', $userdata, $this ) ); |
||
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | // Do a final stock check at this point |
||
| 628 | $this->check_cart_items(); |
||
| 629 | |||
| 630 | // Abort if errors are present |
||
| 631 | if ( wc_notice_count( 'error' ) > 0 ) |
||
| 632 | throw new Exception(); |
||
| 633 | |||
| 634 | $order_id = $this->create_order(); |
||
| 635 | |||
| 636 | if ( is_wp_error( $order_id ) ) { |
||
| 637 | throw new Exception( $order_id->get_error_message() ); |
||
| 638 | } |
||
| 639 | |||
| 640 | do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted ); |
||
| 641 | |||
| 642 | // Process payment |
||
| 643 | if ( WC()->cart->needs_payment() ) { |
||
| 644 | |||
| 645 | // Store Order ID in session so it can be re-used after payment failure |
||
| 646 | WC()->session->order_awaiting_payment = $order_id; |
||
| 647 | |||
| 648 | // Process Payment |
||
| 649 | $result = $available_gateways[ $this->posted['payment_method'] ]->process_payment( $order_id ); |
||
| 650 | |||
| 651 | // Redirect to success/confirmation/payment page |
||
| 652 | if ( isset( $result['result'] ) && 'success' === $result['result'] ) { |
||
| 653 | |||
| 654 | $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); |
||
| 655 | |||
| 656 | if ( is_ajax() ) { |
||
| 657 | wp_send_json( $result ); |
||
| 658 | } else { |
||
| 659 | wp_redirect( $result['redirect'] ); |
||
| 660 | exit; |
||
| 661 | } |
||
| 662 | |||
| 663 | } |
||
| 664 | |||
| 665 | } else { |
||
| 666 | |||
| 667 | if ( empty( $order ) ) { |
||
| 668 | $order = wc_get_order( $order_id ); |
||
| 669 | } |
||
| 670 | |||
| 671 | // No payment was required for order |
||
| 672 | $order->payment_complete(); |
||
| 673 | |||
| 674 | // Empty the Cart |
||
| 675 | WC()->cart->empty_cart(); |
||
| 676 | |||
| 677 | // Get redirect |
||
| 678 | $return_url = $order->get_checkout_order_received_url(); |
||
| 679 | |||
| 680 | // Redirect to success/confirmation/payment page |
||
| 681 | if ( is_ajax() ) { |
||
| 682 | wp_send_json( array( |
||
| 683 | 'result' => 'success', |
||
| 684 | 'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ) |
||
| 685 | ) ); |
||
| 686 | } else { |
||
| 687 | wp_safe_redirect( |
||
| 688 | apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ) |
||
| 689 | ); |
||
| 690 | exit; |
||
| 691 | } |
||
| 692 | |||
| 693 | } |
||
| 694 | |||
| 695 | } |
||
| 696 | |||
| 697 | } catch ( Exception $e ) { |
||
| 698 | if ( ! empty( $e ) ) { |
||
| 699 | wc_add_notice( $e->getMessage(), 'error' ); |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | // If we reached this point then there were errors |
||
| 704 | if ( is_ajax() ) { |
||
| 705 | |||
| 706 | // only print notices if not reloading the checkout, otherwise they're lost in the page reload |
||
| 707 | if ( ! isset( WC()->session->reload_checkout ) ) { |
||
| 708 | ob_start(); |
||
| 709 | wc_print_notices(); |
||
| 710 | $messages = ob_get_clean(); |
||
| 711 | } |
||
| 712 | |||
| 713 | $response = array( |
||
| 714 | 'result' => 'failure', |
||
| 715 | 'messages' => isset( $messages ) ? $messages : '', |
||
| 716 | 'refresh' => isset( WC()->session->refresh_totals ) ? 'true' : 'false', |
||
| 717 | 'reload' => isset( WC()->session->reload_checkout ) ? 'true' : 'false' |
||
| 718 | ); |
||
| 719 | |||
| 720 | unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); |
||
| 721 | |||
| 722 | wp_send_json( $response ); |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 805 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.