Completed
Push — master ( fb62b9...43af20 )
by Mike
08:19
created

WC_Shipping_Legacy_Free_Shipping::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %
Metric Value
dl 17
loc 17
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 4.

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.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Free Shipping Method.
9
 *
10
 * This class is here for backwards commpatility for methods existing before zones existed.
11
 *
12
 * @deprecated  2.6.0
13
 * @version 2.4.0
14
 * @package WooCommerce/Classes/Shipping
15
 * @author  WooThemes
16
 */
17
class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method {
18
19
	/** @var float Min amount to be valid */
20
	public $min_amount;
21
22
	/** @var string Requires option */
23
	public $requires;
24
25
	/**
26
	 * Constructor.
27
	 */
28 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
		$this->id 			= 'legacy_free_shipping';
30
		$this->method_title = __( 'Free Shipping (Legacy)', 'woocommerce' );
31
		$this->method_description = sprintf( __( '<strong>This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping Zones</a>.</strong>', 'woocommerce' ), admin_url( 'admin.php?page=wc-shipping' ) );
32
		$this->init();
33
	}
34
35
	/**
36
	 * Process and redirect if disabled.
37
	 */
38
	public function process_admin_options() {
39
		parent::process_admin_options();
40
41
		if ( 'no' === $this->settings[ 'enabled' ] ) {
42
			wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) );
43
			exit;
44
		}
45
	}
46
	
47
	/**
48
	 * Return the name of the option in the WP DB.
49
	 * @since 2.6.0
50
	 * @return string
51
	 */
52
	public function get_option_key() {
53
		return $this->plugin_id . 'free_shipping' . '_settings';
54
	}
55
56
	/**
57
	 * init function.
58
	 */
59 View Code Duplication
	public function init() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
61
		// Load the settings.
62
		$this->init_form_fields();
63
		$this->init_settings();
64
65
		// Define user set variables
66
		$this->enabled		= $this->get_option( 'enabled' );
67
		$this->title 		= $this->get_option( 'title' );
68
		$this->min_amount 	= $this->get_option( 'min_amount', 0 );
69
		$this->availability = $this->get_option( 'availability' );
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$availability has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
70
		$this->countries 	= $this->get_option( 'countries' );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->get_option('countries') of type * is incompatible with the declared type array of property $countries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The property WC_Shipping_Method::$countries has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
71
		$this->requires		= $this->get_option( 'requires' );
72
73
		// Actions
74
		add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
75
	}
76
77
	/**
78
	 * Initialise Gateway Settings Form Fields.
79
	 */
80
	public function init_form_fields() {
81
		$this->form_fields = array(
82
			'enabled' => array(
83
				'title' 		=> __( 'Enable/Disable', 'woocommerce' ),
84
				'type' 			=> 'checkbox',
85
				'label' 		=> __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),
86
				'default' 		=> 'no'
87
			),
88
			'title' => array(
89
				'title' 		=> __( 'Method Title', 'woocommerce' ),
90
				'type' 			=> 'text',
91
				'description' 	=> __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
92
				'default'		=> __( 'Free Shipping', 'woocommerce' ),
93
				'desc_tip'		=> true,
94
			),
95
			'availability' => array(
96
				'title' 		=> __( 'Method availability', 'woocommerce' ),
97
				'type' 			=> 'select',
98
				'default' 		=> 'all',
99
				'class'			=> 'availability wc-enhanced-select',
100
				'options'		=> array(
101
					'all' 		=> __( 'All allowed countries', 'woocommerce' ),
102
					'specific' 	=> __( 'Specific Countries', 'woocommerce' )
103
				)
104
			),
105
			'countries' => array(
106
				'title' 		=> __( 'Specific Countries', 'woocommerce' ),
107
				'type' 			=> 'multiselect',
108
				'class'			=> 'wc-enhanced-select',
109
				'css'			=> 'width: 450px;',
110
				'default' 		=> '',
111
				'options'		=> WC()->countries->get_shipping_countries(),
112
				'custom_attributes' => array(
113
					'data-placeholder' => __( 'Select some countries', 'woocommerce' )
114
				)
115
			),
116
			'requires' => array(
117
				'title' 		=> __( 'Free Shipping Requires...', 'woocommerce' ),
118
				'type' 			=> 'select',
119
				'class'         => 'wc-enhanced-select',
120
				'default' 		=> '',
121
				'options'		=> array(
122
					'' 				=> __( 'N/A', 'woocommerce' ),
123
					'coupon'		=> __( 'A valid free shipping coupon', 'woocommerce' ),
124
					'min_amount' 	=> __( 'A minimum order amount (defined below)', 'woocommerce' ),
125
					'either' 		=> __( 'A minimum order amount OR a coupon', 'woocommerce' ),
126
					'both' 			=> __( 'A minimum order amount AND a coupon', 'woocommerce' ),
127
				)
128
			),
129
			'min_amount' => array(
130
				'title' 		=> __( 'Minimum Order Amount', 'woocommerce' ),
131
				'type' 			=> 'price',
132
				'placeholder'	=> wc_format_localized_price( 0 ),
133
				'description' 	=> __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
134
				'default' 		=> '0',
135
				'desc_tip'		=> true
136
			)
137
		);
138
	}
139
140
	/**
141
	 * is_available function.
142
	 * @param array $package
143
	 * @return bool
144
	 */
145
	public function is_available( $package ) {
146
		if ( 'no' == $this->enabled ) {
147
			return false;
148
		}
149
150
		if ( 'specific' == $this->availability ) {
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$availability has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
151
			$ship_to_countries = $this->countries;
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$countries has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
152
		} else {
153
			$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
154
		}
155
156
		if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
157
			return false;
158
		}
159
160
		// Enabled logic
161
		$is_available       = false;
162
		$has_coupon         = false;
163
		$has_met_min_amount = false;
164
165 View Code Duplication
		if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
167
			if ( $coupons = WC()->cart->get_coupons() ) {
168
				foreach ( $coupons as $code => $coupon ) {
169
					if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) {
170
						$has_coupon = true;
171
					}
172
				}
173
			}
174
		}
175
176
		if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) {
177
			if ( WC()->cart->prices_include_tax ) {
178
				$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
179
			} else {
180
				$total = WC()->cart->cart_contents_total;
181
			}
182
183
			if ( $total >= $this->min_amount ) {
184
				$has_met_min_amount = true;
185
			}
186
		}
187
188
		switch ( $this->requires ) {
189
			case 'min_amount' :
190
				if ( $has_met_min_amount ) {
191
					$is_available = true;
192
				}
193
			break;
194
			case 'coupon' :
195
				if ( $has_coupon ) {
196
					$is_available = true;
197
				}
198
			break;
199
			case 'both' :
200
				if ( $has_met_min_amount && $has_coupon ) {
201
					$is_available = true;
202
				}
203
			break;
204
			case 'either' :
205
				if ( $has_met_min_amount || $has_coupon ) {
206
					$is_available = true;
207
				}
208
			break;
209
			default :
210
				$is_available = true;
211
			break;
212
		}
213
214
		return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
215
	}
216
217
	/**
218
	 * calculate_shipping function.
219
	 * @return array
220
	 */
221
	public function calculate_shipping( $package = array() ) {
222
		$args = array(
223
			'id' 	  => $this->id,
224
			'label'   => $this->title,
225
			'cost' 	  => 0,
226
			'taxes'   => false,
227
			'package' => $package,
228
		);
229
		$this->add_rate( $args );
230
	}
231
}
232