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