| Conditions | 19 |
| Paths | 722 |
| Total Lines | 107 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 19 | public function add( $product_id ) { |
||
| 20 | global $wpsc_cart; |
||
| 21 | |||
| 22 | if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], "wpsc-add-to-cart-{$product_id}" ) ) { |
||
| 23 | wp_die( __( 'Request expired. Please try adding the item to your cart again.', 'wp-e-commerce' ) ); |
||
| 24 | } |
||
| 25 | |||
| 26 | extract( $_REQUEST, EXTR_SKIP ); |
||
|
|
|||
| 27 | |||
| 28 | $defaults = array( |
||
| 29 | 'variation_values' => array(), |
||
| 30 | 'quantity' => 1, |
||
| 31 | 'provided_price' => null, |
||
| 32 | 'comment' => null, |
||
| 33 | 'time_requested' => null, |
||
| 34 | 'custom_message' => '', |
||
| 35 | 'file_data' => null, |
||
| 36 | 'is_customisable' => false, |
||
| 37 | 'meta' => null, |
||
| 38 | ); |
||
| 39 | |||
| 40 | $provided_parameters = array(); |
||
| 41 | $product_id = apply_filters( 'wpsc_add_to_cart_product_id', (int) $product_id ); |
||
| 42 | |||
| 43 | if ( ! empty( $wpsc_product_variations ) ) { |
||
| 44 | |||
| 45 | foreach ( $wpsc_product_variations as $key => $variation ) { |
||
| 46 | $provided_parameters['variation_values'][ (int) $key ] = (int) $variation; |
||
| 47 | } |
||
| 48 | |||
| 49 | $variation_product_id = wpsc_get_child_object_in_terms( $product_id, $provided_parameters['variation_values'], 'wpsc-variation' ); |
||
| 50 | |||
| 51 | if ( $variation_product_id > 0 ) { |
||
| 52 | $product_id = $variation_product_id; |
||
| 53 | } else { |
||
| 54 | $this->message_collection->add( __( 'This variation combination is no longer available. Please choose a different combination.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
||
| 55 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 56 | exit; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if ( ! empty( $quantity ) ) { |
||
| 61 | $provided_parameters['quantity'] = (int) $quantity; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ( ! empty( $is_customisable ) ) { |
||
| 65 | $provided_parameters['is_customisable'] = true; |
||
| 66 | |||
| 67 | if ( isset( $custom_text ) ) { |
||
| 68 | $provided_parameters['custom_message'] = $custom_text; |
||
| 69 | } |
||
| 70 | |||
| 71 | if ( isset( $_FILES['custom_file'] ) ) { |
||
| 72 | $provided_parameters['file_data'] = $_FILES['custom_file']; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if ( isset( $donation_price ) && (float) $donation_price > 0 ) { |
||
| 77 | $provided_parameters['provided_price'] = (float) $donation_price; |
||
| 78 | } |
||
| 79 | |||
| 80 | $parameters = array_merge( $defaults, $provided_parameters ); |
||
| 81 | |||
| 82 | if ( $parameters['quantity'] <= 0 ) { |
||
| 83 | $this->message_collection->add( __( 'Sorry, but the quantity you just entered is not valid. Please try again.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
||
| 84 | return; |
||
| 85 | } |
||
| 86 | |||
| 87 | $product = apply_filters( 'wpsc_add_to_cart_product_object', get_post( $product_id, OBJECT, 'display' ) ); |
||
| 88 | |||
| 89 | $stock = get_post_meta( $product_id, '_wpsc_stock', true ); |
||
| 90 | |||
| 91 | $remaining_quantity = $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'] ); |
||
| 92 | |||
| 93 | if ( $stock !== '' && $remaining_quantity !== true ) { |
||
| 94 | if ( $remaining_quantity <= 0 ) { |
||
| 95 | $message = apply_filters( 'wpsc_add_to_cart_out_of_stock_message', __( 'Sorry, the product "%s" is out of stock.', 'wp-e-commerce' ) ); |
||
| 96 | $this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); |
||
| 97 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 98 | exit; |
||
| 99 | } elseif ( $remaining_quantity < $parameters['quantity'] ) { |
||
| 100 | $message = __( 'Sorry, but the quantity you just specified is larger than the available stock. There are only %d of the item in stock.', 'wp-e-commerce' ); |
||
| 101 | $this->message_collection->add( sprintf( $message, $remaining_quantity ), 'error', 'main', 'flash' ); |
||
| 102 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 103 | exit; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | if ( wpsc_product_has_variations( $product_id ) && is_null( $parameters['variation_values'] ) ) { |
||
| 108 | $message = apply_filters( 'wpsc_add_to_cart_variation_missing_message', sprintf( __( 'This product has several options to choose from.<br /><br /><a href="%s" style="display:inline; float:none; margin: 0; padding: 0;">Visit the product page</a> to select options.', 'wp-e-commerce' ), esc_url( get_permalink( $product_id ) ) ), $product_id ); |
||
| 109 | $this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); |
||
| 110 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 111 | exit; |
||
| 112 | } |
||
| 113 | |||
| 114 | if ( $wpsc_cart->set_item( $product_id, $parameters ) ) { |
||
| 115 | $message = sprintf( __( 'You just added %s to your cart.', 'wp-e-commerce' ), $product->post_title ); |
||
| 116 | $this->message_collection->add( $message, 'success', 'main', 'flash' ); |
||
| 117 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 118 | exit; |
||
| 119 | } else { |
||
| 120 | $this->message_collection->add( __( 'An unknown error just occurred. Please contact the shop administrator.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
||
| 121 | wp_safe_redirect( wpsc_get_cart_url() ); |
||
| 122 | exit; |
||
| 123 | } |
||
| 124 | |||
| 125 | } |
||
| 126 | |||
| 277 |