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 |
||
| 20 | class WC_Settings_Shipping extends WC_Settings_Page { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor. |
||
| 24 | */ |
||
| 25 | View Code Duplication | public function __construct() { |
|
| 26 | $this->id = 'shipping'; |
||
| 27 | $this->label = __( 'Shipping', 'woocommerce' ); |
||
| 28 | add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); |
||
| 29 | add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); |
||
| 30 | add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); |
||
| 31 | add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Add this page to settings. |
||
| 36 | */ |
||
| 37 | public function add_settings_page( $pages ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get sections. |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public function get_sections() { |
||
| 47 | $sections = array( |
||
| 48 | '' => __( 'Shipping Zones', 'woocommerce' ), |
||
| 49 | 'options' => __( 'Shipping Options', 'woocommerce' ), |
||
| 50 | 'classes' => __( 'Shipping Classes', 'woocommerce' ) |
||
| 51 | ); |
||
| 52 | |||
| 53 | View Code Duplication | if ( ! defined( 'WC_INSTALLING' ) ) { |
|
| 54 | // Load shipping methods so we can show any global options they may have |
||
| 55 | $shipping_methods = WC()->shipping->load_shipping_methods(); |
||
| 56 | |||
| 57 | foreach ( $shipping_methods as $method ) { |
||
| 58 | if ( ! $method->has_settings() ) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | $title = empty( $method->method_title ) ? ucfirst( $method->id ) : $method->method_title; |
||
| 62 | $sections[ strtolower( $method->id ) ] = esc_html( $title ); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get settings array. |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function get_settings() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Output the settings. |
||
| 123 | */ |
||
| 124 | public function output() { |
||
| 125 | global $current_section, $hide_save_button; |
||
| 126 | |||
| 127 | // Load shipping methods so we can show any global options they may have |
||
| 128 | $shipping_methods = WC()->shipping->load_shipping_methods(); |
||
| 129 | |||
| 130 | if ( '' === $current_section ) { |
||
| 131 | $this->output_zones_screen(); |
||
| 132 | } elseif ( 'options' === $current_section ) { |
||
| 133 | $settings = $this->get_settings(); |
||
| 134 | WC_Admin_Settings::output_fields( $settings ); |
||
| 135 | } elseif ( 'classes' === $current_section ) { |
||
| 136 | $hide_save_button = true; |
||
| 137 | $this->output_shipping_class_screen(); |
||
| 138 | } else { |
||
| 139 | foreach ( $shipping_methods as $method ) { |
||
| 140 | if ( in_array( $current_section, array( $method->id, sanitize_title( get_class( $method ) ) ) ) && $method->has_settings() ) { |
||
| 141 | $method->admin_options(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Save settings. |
||
| 149 | */ |
||
| 150 | public function save() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Handles output of the shipping zones page in admin. |
||
| 177 | */ |
||
| 178 | protected function output_zones_screen() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Show method for a zone |
||
| 194 | * @param int $zone_id |
||
| 195 | */ |
||
| 196 | protected function zone_methods_screen( $zone_id ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Show zones |
||
| 225 | */ |
||
| 226 | protected function zones_screen() { |
||
| 227 | global $wpdb; |
||
| 228 | |||
| 229 | $allowed_countries = WC()->countries->get_allowed_countries(); |
||
|
1 ignored issue
–
show
|
|||
| 230 | $continents = WC()->countries->get_continents(); |
||
|
1 ignored issue
–
show
|
|||
| 231 | $method_count = absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ) ); |
||
|
1 ignored issue
–
show
|
|||
| 232 | |||
| 233 | wp_localize_script( 'wc-shipping-zones', 'shippingZonesLocalizeScript', array( |
||
| 234 | 'zones' => WC_Shipping_Zones::get_zones(), |
||
| 235 | 'default_zone' => array( |
||
| 236 | 'zone_id' => 0, |
||
| 237 | 'zone_name' => '', |
||
| 238 | 'zone_order' => null, |
||
| 239 | ), |
||
| 240 | 'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ), |
||
| 241 | 'strings' => array( |
||
| 242 | 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ), |
||
| 243 | 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ), |
||
| 244 | 'default_zone_name' => __( 'Zone', 'woocommerce' ), |
||
| 245 | ), |
||
| 246 | ) ); |
||
| 247 | wp_enqueue_script( 'wc-shipping-zones' ); |
||
| 248 | |||
| 249 | include_once( 'views/html-admin-page-shipping-zones.php' ); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Show instance settings |
||
| 254 | * @param int $instance_id |
||
| 255 | */ |
||
| 256 | protected function instance_settings_screen( $instance_id ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Handles output of the shipping class settings screen. |
||
| 285 | */ |
||
| 286 | protected function output_shipping_class_screen() { |
||
| 313 | } |
||
| 314 | |||
| 318 |
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.