Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class WC_Shipping_Legacy_Local_Delivery extends WC_Shipping_Local_Pickup { |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | */ |
||
22 | public function __construct() { |
||
23 | $this->id = 'legacy_local_delivery'; |
||
24 | $this->method_title = __( 'Local Delivery (Legacy)', 'woocommerce' ); |
||
25 | $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-settings&tab=shipping' ) ); |
||
26 | $this->init(); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Process and redirect if disabled. |
||
31 | */ |
||
32 | public function process_admin_options() { |
||
33 | parent::process_admin_options(); |
||
34 | |||
35 | if ( 'no' === $this->settings[ 'enabled' ] ) { |
||
36 | wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=options' ) ); |
||
37 | exit; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Return the name of the option in the WP DB. |
||
43 | * @since 2.6.0 |
||
44 | * @return string |
||
45 | */ |
||
46 | public function get_option_key() { |
||
47 | return $this->plugin_id . 'local_delivery' . '_settings'; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * init function. |
||
52 | */ |
||
53 | public function init() { |
||
54 | |||
55 | // Load the settings. |
||
56 | $this->init_form_fields(); |
||
57 | $this->init_settings(); |
||
58 | |||
59 | // Define user set variables |
||
60 | $this->title = $this->get_option( 'title' ); |
||
61 | $this->type = $this->get_option( 'type' ); |
||
62 | $this->fee = $this->get_option( 'fee' ); |
||
63 | $this->type = $this->get_option( 'type' ); |
||
64 | $this->codes = $this->get_option( 'codes' ); |
||
65 | $this->availability = $this->get_option( 'availability' ); |
||
66 | $this->countries = $this->get_option( 'countries' ); |
||
67 | |||
68 | add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * calculate_shipping function. |
||
73 | * |
||
74 | * @param array $package (default: array()) |
||
75 | */ |
||
76 | public function calculate_shipping( $package = array() ) { |
||
77 | $shipping_total = 0; |
||
78 | |||
79 | switch ( $this->type ) { |
||
80 | case 'fixed' : |
||
81 | $shipping_total = $this->fee; |
||
82 | break; |
||
83 | case 'percent' : |
||
84 | $shipping_total = $package['contents_cost'] * ( $this->fee / 100 ); |
||
85 | break; |
||
86 | case 'product' : |
||
87 | foreach ( $package['contents'] as $item_id => $values ) { |
||
88 | if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) { |
||
89 | $shipping_total += $this->fee * $values['quantity']; |
||
90 | } |
||
91 | } |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | $rate = array( |
||
96 | 'id' => $this->id, |
||
97 | 'label' => $this->title, |
||
98 | 'cost' => $shipping_total, |
||
99 | 'package' => $package, |
||
100 | ); |
||
101 | |||
102 | $this->add_rate( $rate ); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Init form fields. |
||
107 | */ |
||
108 | public function init_form_fields() { |
||
109 | $this->form_fields = array( |
||
110 | 'enabled' => array( |
||
111 | 'title' => __( 'Enable', 'woocommerce' ), |
||
112 | 'type' => 'checkbox', |
||
113 | 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ), |
||
114 | 'default' => 'no' |
||
115 | ), |
||
116 | 'title' => array( |
||
117 | 'title' => __( 'Title', 'woocommerce' ), |
||
118 | 'type' => 'text', |
||
119 | 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), |
||
120 | 'default' => __( 'Local Delivery', 'woocommerce' ), |
||
121 | 'desc_tip' => true, |
||
122 | ), |
||
123 | 'type' => array( |
||
124 | 'title' => __( 'Fee Type', 'woocommerce' ), |
||
125 | 'type' => 'select', |
||
126 | 'class' => 'wc-enhanced-select', |
||
127 | 'description' => __( 'How to calculate delivery charges', 'woocommerce' ), |
||
128 | 'default' => 'fixed', |
||
129 | 'options' => array( |
||
130 | 'fixed' => __( 'Fixed amount', 'woocommerce' ), |
||
131 | 'percent' => __( 'Percentage of cart total', 'woocommerce' ), |
||
132 | 'product' => __( 'Fixed amount per product', 'woocommerce' ), |
||
133 | ), |
||
134 | 'desc_tip' => true, |
||
135 | ), |
||
136 | 'fee' => array( |
||
137 | 'title' => __( 'Delivery Fee', 'woocommerce' ), |
||
138 | 'type' => 'price', |
||
139 | 'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ), |
||
140 | 'default' => '', |
||
141 | 'desc_tip' => true, |
||
142 | 'placeholder' => wc_format_localized_price( 0 ) |
||
143 | ), |
||
144 | 'codes' => array( |
||
145 | 'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ), |
||
146 | 'type' => 'text', |
||
147 | 'desc_tip' => __( 'What ZIP/post codes are available for local delivery?', 'woocommerce' ), |
||
148 | 'default' => '', |
||
149 | 'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ), |
||
150 | 'placeholder' => 'e.g. 12345, 56789' |
||
151 | ), |
||
152 | 'availability' => array( |
||
153 | 'title' => __( 'Method availability', 'woocommerce' ), |
||
154 | 'type' => 'select', |
||
155 | 'default' => 'all', |
||
156 | 'class' => 'availability wc-enhanced-select', |
||
157 | 'options' => array( |
||
158 | 'all' => __( 'All allowed countries', 'woocommerce' ), |
||
159 | 'specific' => __( 'Specific Countries', 'woocommerce' ) |
||
160 | ) |
||
161 | ), |
||
162 | 'countries' => array( |
||
163 | 'title' => __( 'Specific Countries', 'woocommerce' ), |
||
164 | 'type' => 'multiselect', |
||
165 | 'class' => 'wc-enhanced-select', |
||
166 | 'css' => 'width: 450px;', |
||
167 | 'default' => '', |
||
168 | 'options' => WC()->countries->get_shipping_countries(), |
||
169 | 'custom_attributes' => array( |
||
170 | 'data-placeholder' => __( 'Select some countries', 'woocommerce' ) |
||
171 | ) |
||
172 | ) |
||
173 | ); |
||
174 | } |
||
175 | } |
||
176 |