Completed
Push — master ( a85a3b...a9e055 )
by Justin
11:13
created

flatrate::flatrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * shipping/flatrate.php
4
 *
5
 * @package WP e-Commerce
6
 */
7
8
9
class flatrate {
0 ignored issues
show
Coding Style introduced by
Class name "flatrate" is not in camel caps format
Loading history...
10
	var $internal_name, $name;
11
12
	/**
13
	 * Constructor
14
	 *
15
	 * @return boolean Always returns true.
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
	 */
17
	public function __construct() {
18
		$this->internal_name = "flatrate";
19
		$this->name= __( "Flat Rate", 'wp-e-commerce' );
20
		$this->is_external = false;
0 ignored issues
show
Bug introduced by
The property is_external does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
		return true;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
22
	}
23
24
	/**
25
	 * Returns i18n-ized name of shipping module.
26
	 *
27
	 * @return string
28
	 */
29
	function getName() {
30
		return $this->name;
31
	}
32
33
	/**
34
	 * Returns internal name of shipping module.
35
	 *
36
	 * @return string
37
	 */
38
	function getInternalName() {
39
		return $this->internal_name;
40
	}
41
42
	/**
43
	 * Returns HTML settings form. Should be a collection of <tr> elements containing two columns.
44
	 *
45
	 * @return string HTML snippet
46
	 */
47
	function getForm() {
48
		global $wpdb;
49
50
		$shipping = wp_parse_args(
51
			get_option( 'flat_rates' ),
52
			array(
53
				'southisland'  => '',
54
				'northisland'  => '',
55
				'continental'  => '',
56
				'all'          => '',
57
				'local'        => '',
58
				'northamerica' => '',
59
				'southamerica' => '',
60
				'asiapacific'  => '',
61
				'europe'       => '',
62
				'africa'       => '',
63
			)
64
		);
65
66
		$output = "<tr><td colspan='2'>";
67
68
		$output .= "<table>";
69
70
		$output .= "<tr><th colspan='2'><strong>" . __( 'Base Local', 'wp-e-commerce' ) . "</strong></th></tr>";
71
		switch ( get_option( 'base_country' ) ) {
72
			case 'NZ':
73
				$output .= $this->settings_form_shipping_price_field( 'southisland', __( 'South Island', 'wp-e-commerce' ),  $shipping['southisland'] );
74
				$output .= $this->settings_form_shipping_price_field( 'northisland', __( 'North Island', 'wp-e-commerce' ),  $shipping['northisland'] );
75
				break;
76
77
			case 'US':
78
				$output .= $this->settings_form_shipping_price_field( 'continental', __( 'Continental 48 States', 'wp-e-commerce' ),  $shipping['continental'] );
79
				$output .= $this->settings_form_shipping_price_field( 'all',         __( 'All 50 States'        , 'wp-e-commerce' ), $shipping['all'] );
80
				break;
81
82
			default:
83
				$output .= $this->settings_form_shipping_price_field( 'local',       __( 'Domestic', 'wp-e-commerce' ),      $shipping['local'] );
84
				break;
85
		}
86
87
		$output .= "<tr><th colspan='2'><strong>" . __( 'Base International', 'wp-e-commerce' ) . "</strong></th></tr>";
88
		$output .= $this->settings_form_shipping_price_field( 'northamerica', __( 'North America', 'wp-e-commerce' ),    $shipping['northamerica'] );
89
		$output .= $this->settings_form_shipping_price_field( 'southamerica', __( 'South America', 'wp-e-commerce' ),    $shipping['southamerica'] );
90
		$output .= $this->settings_form_shipping_price_field( 'asiapacific',  __( 'Asia and Pacific', 'wp-e-commerce' ), $shipping['asiapacific'] );
91
		$output .= $this->settings_form_shipping_price_field( 'europe',       __( 'Europe', 'wp-e-commerce' ),           $shipping['europe'] );
92
		$output .= $this->settings_form_shipping_price_field( 'africa',       __( 'Africa', 'wp-e-commerce' ),           $shipping['africa'] );
93
		$output .= "</table>";
94
95
		$output .= "<br /><p class='description'>" . __( 'If you do not wish to ship to a particular region, leave the field blank. To offer free shipping to a region, enter 0.', 'wp-e-commerce' ) . "</p>";
96
		$output .= "</td></tr>";
97
98
		return $output;
99
	}
100
101
	/**
102
	 * Create shipping price field
103
	 *
104
	 * @return string HTML snippet, a <tr> with two columns.
105
	 */
106
	function settings_form_shipping_price_field( $id, $label, $value ) {
107
		$output = "<tr><td>" . $label . "</td>";
108
		$output .= "<td>";
109
		$output .= esc_attr( wpsc_get_currency_symbol() );
110
		$output .= "<input size='4' type='text' name='shipping[" . esc_attr( $id ) . "]' value='" . esc_attr( $value ) . "'>";
111
		$output .= "</td></tr>";
112
113
		return $output;
114
	}
115
116
	/**
117
	 * Saves shipping module settings.
118
	 *
119
	 * @return boolean Always returns true.
120
	 */
121
	function submit_form() {
122
		if ( ! isset( $_POST['shipping'] ) ) {
123
			$_POST['shipping'] = null;
124
		}
125
126
		if ( $_POST['shipping'] != null ) {
127
			$shipping           = (array) get_option('flat_rates');
128
			$submitted_shipping = (array) $_POST['shipping'];
129
130
			$rates = array();
131
132
			foreach ( $submitted_shipping as $key => $rate ) {
133
				if ( empty( $rate ) || is_numeric( $rate ) ) {
134
					$rates[ $key ] = $rate;
135
				}
136
			}
137
138
			update_option( 'flat_rates', array_merge( $rates, $submitted_shipping ) );
139
		}
140
141
		return true;
142
	}
143
144
	/**
145
	 * returns shipping quotes using this shipping module.
146
	 *
147
	 * @param boolean $for_display (optional) (unused)
148
	 * @return array collection of rates applicable.
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
149
	 */
150
	function getQuote($for_display = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_display is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
		global $wpdb, $wpsc_cart;
152
		$quote_shipping_method = wpsc_get_customer_meta( 'quote_shipping_method' );
153
		$quote_shipping_option = wpsc_get_customer_meta( 'quote_shipping_option' );
154
155
		$country = '';
156
157
		if (isset($_POST['country'])) {
158
			$country = sanitize_text_field( $_POST['country'] );
159
			wpsc_update_customer_meta( 'shipping_country', $country );
160
		} else {
161
			$country = (string) wpsc_get_customer_meta( 'shipping_country' );
162
		}
163
164
		if (is_object($wpsc_cart)) {
165
			$cart_total = $wpsc_cart->calculate_subtotal(true);
166
		}
167
168
		if (get_option('base_country') != $country) {
169
170
			$results = WPSC_Countries::get_continent( $country );
171
172
			$flatrates = get_option('flat_rates');
173
174
			if ($flatrates != '') {
175
				if ( $quote_shipping_method == $this->internal_name && $quote_shipping_option != __( "Flat Rate", 'wp-e-commerce' ) )
176
					wpsc_delete_customer_meta( 'quote_shipping_option' );
177
178
				if ( isset ( $flatrates[$results] ) ) {
179
180
				    if (stristr($flatrates[$results],'%')) {
181
182
					    $shipping_percent = str_replace('%', '', $flatrates[$results]);
183
					    $shipping_amount = $cart_total * ( $shipping_percent / 100 );
0 ignored issues
show
Bug introduced by
The variable $cart_total does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
184
					    $flatrates[$results] = (float)$shipping_amount;
185
186
				    }
187
188
                    return array( __( "Flat Rate", 'wp-e-commerce' ) => (float) $flatrates[$results] );
189
                }
190
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
191
192
		} else {
193
194
			$flatrates = get_option( 'flat_rates' );
195
			$shipping_quotes = array();
196
197
			switch ( $country ) {
198
			case 'NZ':
199
				if ( isset( $flatrates['northisland'] ) && strlen( $flatrates['northisland'] ) > 0 ) {
200
					$shipping_quotes[ __( 'North Island', 'wp-e-commerce' ) ] = esc_attr( $flatrates['northisland'] );
201
				}
202
				if ( isset( $flatrates['southisland'] ) && strlen( $flatrates['southisland'] ) > 0 ) {
203
					$shipping_quotes[ __( 'South Island', 'wp-e-commerce' ) ] = esc_attr( $flatrates['southisland'] );
204
				}
205
				break;
206
207
			case 'US':
208
				if ( isset( $flatrates['continental'] ) && strlen( $flatrates['continental'] ) > 0 ) {
209
					$shipping_quotes[ __( 'Continental 48 States', 'wp-e-commerce' ) ] = esc_attr( $flatrates['continental'] );
210
				}
211
				if ( isset( $flatrates['all'] ) && strlen( $flatrates['all'] ) > 0 ) {
212
					$shipping_quotes[ __( 'All 50 States', 'wp-e-commerce' ) ] = esc_attr( $flatrates['all'] );
213
				}
214
				break;
215
216
			default:
217
				if ( isset( $flatrates['local'] ) && strlen( $flatrates['local'] ) > 0 ) {
218
					$shipping_quotes[ __( 'Local Shipping', 'wp-e-commerce' ) ] = esc_attr( $flatrates['local'] );
219
				}
220
				break;
221
			}
222
223
			// Deal with % shipping rates
224
			foreach (array_keys($shipping_quotes) as $quote_name) {
225
226
					if (stristr($shipping_quotes[$quote_name],'%')) {
227
						$shipping_percent = str_replace('%', '', $shipping_quotes[$quote_name]);
228
						$shipping_amount = $cart_total * ( $shipping_percent / 100 );
229
						$shipping_quotes[$quote_name] = (float)$shipping_amount;
230
					} else {
231
						$shipping_quotes[$quote_name] = (float)$shipping_quotes[$quote_name];
232
					}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
233
234
			}
235
236
			if ( $quote_shipping_method == $this->internal_name ) {
237
238
				$shipping_options = array_keys($shipping_quotes);
239
240
				if ( array_search( $quote_shipping_option, $shipping_options ) === false) {
241
					wpsc_delete_customer_meta( 'quote_shipping_option' );
242
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
243
244
			}
245
246
			return $shipping_quotes;
247
		}
248
249
	}
250
251
	/**
252
	 * calculates shipping price for an individual cart item.
253
	 *
254
	 * @param object $cart_item (reference)
255
	 * @return float price of shipping for the item.
256
	 */
257
	function get_item_shipping(&$cart_item) {
258
259
		global $wpdb, $wpsc_cart;
260
261
		$unit_price = $cart_item->unit_price;
262
		$quantity = $cart_item->quantity;
263
		$weight = $cart_item->weight;
264
		$product_id = $cart_item->product_id;
265
266
		$uses_billing_address = false;
267
		foreach ($cart_item->category_id_list as $category_id) {
268
			$uses_billing_address = (bool)wpsc_get_categorymeta($category_id, 'uses_billing_address');
269
			if ($uses_billing_address === true) {
270
				break; /// just one true value is sufficient
271
			}
272
		}
273
274
		if (is_numeric($product_id) && (get_option('do_not_use_shipping') != 1)) {
275
			if ($uses_billing_address == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
276
				$country_code = $wpsc_cart->selected_country;
277
			} else {
278
				$country_code = $wpsc_cart->delivery_country;
279
			}
280
281
			if ($cart_item->uses_shipping == true) {
282
				//if the item has shipping
283
				$additional_shipping = '';
284
				if (isset($cart_item->meta[0]['shipping'])) {
285
					$shipping_values = $cart_item->meta[0]['shipping'];
286
				}
287
				if (isset($shipping_values['local']) && $country_code == get_option('base_country')) {
288
					$additional_shipping = $shipping_values['local'];
289
				} else {
290
					if (isset($shipping_values['international'])) {
291
						$additional_shipping = $shipping_values['international'];
292
					}
293
				}
294
				$shipping = $quantity * $additional_shipping;
295
			} else {
296
				//if the item does not have shipping
297
				$shipping = 0;
298
			}
299
		} else {
300
			//if the item is invalid or all items do not have shipping
301
			$shipping = 0;
302
		}
303
		return $shipping;
304
	}
305
306
	/**
307
	 *
308
	 *
309
	 * @param unknown $total_price
310
	 * @param unknown $weight
311
	 * @return unknown
312
	 */
313
	function get_cart_shipping($total_price, $weight) {
0 ignored issues
show
Unused Code introduced by
The parameter $total_price is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $weight is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
314
		return $output;
0 ignored issues
show
Bug introduced by
The variable $output does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
315
	}
316
}
317
318
319
$flatrate = new flatrate();
320
$wpsc_shipping_modules[$flatrate->getInternalName()] = $flatrate;
321