Complex classes like flatrate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use flatrate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class flatrate { |
||
|
|
|||
| 10 | var $internal_name, $name; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Constructor |
||
| 14 | * |
||
| 15 | * @return boolean Always returns true. |
||
| 16 | */ |
||
| 17 | public function __construct() { |
||
| 18 | $this->internal_name = "flatrate"; |
||
| 19 | $this->name= __( "Flat Rate", 'wp-e-commerce' ); |
||
| 20 | $this->is_external = false; |
||
| 21 | return true; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Returns i18n-ized name of shipping module. |
||
| 26 | * |
||
| 27 | * @return string |
||
| 28 | */ |
||
| 29 | function getName() { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Returns internal name of shipping module. |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | function getInternalName() { |
||
| 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. |
||
| 149 | */ |
||
| 150 | function getQuote($for_display = false) { |
||
| 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 ); |
||
| 184 | $flatrates[$results] = (float)$shipping_amount; |
||
| 185 | |||
| 186 | } |
||
| 187 | |||
| 188 | return array( __( "Flat Rate", 'wp-e-commerce' ) => (float) $flatrates[$results] ); |
||
| 189 | } |
||
| 190 | } |
||
| 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 | } |
||
| 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 | } |
||
| 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) { |
||
| 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) { |
||
| 316 | } |
||
| 317 | |||
| 318 | |||
| 319 | $flatrate = new flatrate(); |
||
| 320 | $wpsc_shipping_modules[$flatrate->getInternalName()] = $flatrate; |
||
| 321 |