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_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 | public function __construct() { |
||
| 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§ion=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 | public function init() { |
||
| 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' ); |
||
| 70 | $this->countries = $this->get_option( 'countries' ); |
||
| 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 ) { |
||
| 151 | $ship_to_countries = $this->countries; |
||
| 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 | if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) { |
||
| 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 |