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:
Complex classes like WC_Admin_Setup_Wizard 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 WC_Admin_Setup_Wizard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class WC_Admin_Setup_Wizard { |
||
| 20 | |||
| 21 | /** @var string Currenct Step */ |
||
| 22 | private $step = ''; |
||
| 23 | |||
| 24 | /** @var array Steps for the setup wizard */ |
||
| 25 | private $steps = array(); |
||
| 26 | |||
| 27 | /** @var array Tweets user can optionally send after install */ |
||
| 28 | private $tweets = array( |
||
| 29 | 'Someone give me woo-t, I just set up a new store with #WordPress and @WooCommerce!', |
||
| 30 | 'Someone give me high five, I just set up a new store with #WordPress and @WooCommerce!' |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Hook in tabs. |
||
| 35 | */ |
||
| 36 | public function __construct() { |
||
| 37 | if ( apply_filters( 'woocommerce_enable_setup_wizard', true ) && current_user_can( 'manage_woocommerce' ) ) { |
||
| 38 | add_action( 'admin_menu', array( $this, 'admin_menus' ) ); |
||
| 39 | add_action( 'admin_init', array( $this, 'setup_wizard' ) ); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Add admin menus/screens. |
||
| 45 | */ |
||
| 46 | public function admin_menus() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Show the setup wizard. |
||
| 52 | */ |
||
| 53 | public function setup_wizard() { |
||
| 54 | if ( empty( $_GET['page'] ) || 'wc-setup' !== $_GET['page'] ) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | $this->steps = array( |
||
| 58 | 'introduction' => array( |
||
| 59 | 'name' => __( 'Introduction', 'woocommerce' ), |
||
| 60 | 'view' => array( $this, 'wc_setup_introduction' ), |
||
| 61 | 'handler' => '' |
||
| 62 | ), |
||
| 63 | 'pages' => array( |
||
| 64 | 'name' => __( 'Page Setup', 'woocommerce' ), |
||
| 65 | 'view' => array( $this, 'wc_setup_pages' ), |
||
| 66 | 'handler' => array( $this, 'wc_setup_pages_save' ) |
||
| 67 | ), |
||
| 68 | 'locale' => array( |
||
| 69 | 'name' => __( 'Store Locale', 'woocommerce' ), |
||
| 70 | 'view' => array( $this, 'wc_setup_locale' ), |
||
| 71 | 'handler' => array( $this, 'wc_setup_locale_save' ) |
||
| 72 | ), |
||
| 73 | 'shipping_taxes' => array( |
||
| 74 | 'name' => __( 'Shipping & Tax', 'woocommerce' ), |
||
| 75 | 'view' => array( $this, 'wc_setup_shipping_taxes' ), |
||
| 76 | 'handler' => array( $this, 'wc_setup_shipping_taxes_save' ), |
||
| 77 | ), |
||
| 78 | 'payments' => array( |
||
| 79 | 'name' => __( 'Payments', 'woocommerce' ), |
||
| 80 | 'view' => array( $this, 'wc_setup_payments' ), |
||
| 81 | 'handler' => array( $this, 'wc_setup_payments_save' ), |
||
| 82 | ), |
||
| 83 | 'next_steps' => array( |
||
| 84 | 'name' => __( 'Ready!', 'woocommerce' ), |
||
| 85 | 'view' => array( $this, 'wc_setup_ready' ), |
||
| 86 | 'handler' => '' |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | $this->step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : current( array_keys( $this->steps ) ); |
||
| 90 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 91 | |||
| 92 | wp_register_script( 'jquery-blockui', WC()->plugin_url() . '/assets/js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.70', true ); |
||
| 93 | wp_register_script( 'select2', WC()->plugin_url() . '/assets/js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.2' ); |
||
| 94 | wp_register_script( 'wc-enhanced-select', WC()->plugin_url() . '/assets/js/admin/wc-enhanced-select' . $suffix . '.js', array( 'jquery', 'select2' ), WC_VERSION ); |
||
| 95 | wp_localize_script( 'wc-enhanced-select', 'wc_enhanced_select_params', array( |
||
| 96 | 'i18n_matches_1' => _x( 'One result is available, press enter to select it.', 'enhanced select', 'woocommerce' ), |
||
| 97 | 'i18n_matches_n' => _x( '%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce' ), |
||
| 98 | 'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce' ), |
||
| 99 | 'i18n_ajax_error' => _x( 'Loading failed', 'enhanced select', 'woocommerce' ), |
||
| 100 | 'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce' ), |
||
| 101 | 'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce' ), |
||
| 102 | 'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce' ), |
||
| 103 | 'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce' ), |
||
| 104 | 'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce' ), |
||
| 105 | 'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce' ), |
||
| 106 | 'i18n_load_more' => _x( 'Loading more results…', 'enhanced select', 'woocommerce' ), |
||
| 107 | 'i18n_searching' => _x( 'Searching…', 'enhanced select', 'woocommerce' ), |
||
| 108 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 109 | 'search_products_nonce' => wp_create_nonce( 'search-products' ), |
||
| 110 | 'search_customers_nonce' => wp_create_nonce( 'search-customers' ) |
||
| 111 | ) ); |
||
| 112 | wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), WC_VERSION ); |
||
| 113 | wp_enqueue_style( 'wc-setup', WC()->plugin_url() . '/assets/css/wc-setup.css', array( 'dashicons', 'install' ), WC_VERSION ); |
||
| 114 | |||
| 115 | wp_register_script( 'wc-setup', WC()->plugin_url() . '/assets/js/admin/wc-setup.min.js', array( 'jquery', 'wc-enhanced-select', 'jquery-blockui' ), WC_VERSION ); |
||
| 116 | wp_localize_script( 'wc-setup', 'wc_setup_params', array( |
||
| 117 | 'locale_info' => json_encode( include( WC()->plugin_path() . '/i18n/locale-info.php' ) ) |
||
| 118 | ) ); |
||
| 119 | |||
| 120 | if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { |
||
| 121 | call_user_func( $this->steps[ $this->step ]['handler'] ); |
||
| 122 | } |
||
| 123 | |||
| 124 | ob_start(); |
||
| 125 | $this->setup_wizard_header(); |
||
| 126 | $this->setup_wizard_steps(); |
||
| 127 | $this->setup_wizard_content(); |
||
| 128 | $this->setup_wizard_footer(); |
||
| 129 | exit; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function get_next_step_link() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Setup Wizard Header. |
||
| 139 | */ |
||
| 140 | public function setup_wizard_header() { |
||
| 141 | ?> |
||
| 142 | <!DOCTYPE html> |
||
| 143 | <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> |
||
| 144 | <head> |
||
| 145 | <meta name="viewport" content="width=device-width" /> |
||
| 146 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
||
| 147 | <title><?php _e( 'WooCommerce › Setup Wizard', 'woocommerce' ); ?></title> |
||
| 148 | <?php wp_print_scripts( 'wc-setup' ); ?> |
||
| 149 | <?php do_action( 'admin_print_styles' ); ?> |
||
| 150 | <?php do_action( 'admin_head' ); ?> |
||
| 151 | </head> |
||
| 152 | <body class="wc-setup wp-core-ui"> |
||
| 153 | <h1 id="wc-logo"><a href="http://woothemes.com/woocommerce"><img src="<?php echo WC()->plugin_url(); ?>/assets/images/woocommerce_logo.png" alt="WooCommerce" /></a></h1> |
||
| 154 | <?php |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Setup Wizard Footer. |
||
| 159 | */ |
||
| 160 | public function setup_wizard_footer() { |
||
| 161 | ?> |
||
| 162 | <?php if ( 'next_steps' === $this->step ) : ?> |
||
| 163 | <a class="wc-return-to-dashboard" href="<?php echo esc_url( admin_url() ); ?>"><?php _e( 'Return to the WordPress Dashboard', 'woocommerce' ); ?></a> |
||
| 164 | <?php endif; ?> |
||
| 165 | </body> |
||
| 166 | </html> |
||
| 167 | <?php |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Output the steps. |
||
| 172 | */ |
||
| 173 | public function setup_wizard_steps() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Output the content for the current step. |
||
| 193 | */ |
||
| 194 | public function setup_wizard_content() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Introduction step. |
||
| 202 | */ |
||
| 203 | public function wc_setup_introduction() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Page setup. |
||
| 217 | */ |
||
| 218 | public function wc_setup_pages() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Save Page Settings. |
||
| 267 | */ |
||
| 268 | public function wc_setup_pages_save() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Locale settings. |
||
| 278 | */ |
||
| 279 | public function wc_setup_locale() { |
||
| 280 | $user_location = WC_Geolocation::geolocate_ip(); |
||
| 281 | $country = ! empty( $user_location['country'] ) ? $user_location['country'] : 'US'; |
||
| 282 | $state = ! empty( $user_location['state'] ) ? $user_location['state'] : '*'; |
||
| 283 | $state = 'US' === $country && '*' === $state ? 'AL' : $state; |
||
| 284 | |||
| 285 | // Defaults |
||
| 286 | $currency = get_option( 'woocommerce_currency', 'GBP' ); |
||
| 287 | $currency_pos = get_option( 'woocommerce_currency_pos', 'left' ); |
||
| 288 | $decimal_sep = get_option( 'woocommerce_price_decimal_sep', '.' ); |
||
| 289 | $num_decimals = get_option( 'woocommerce_price_num_decimals', '2' ); |
||
| 290 | $thousand_sep = get_option( 'woocommerce_price_thousand_sep', ',' ); |
||
| 291 | $dimension_unit = get_option( 'woocommerce_dimension_unit', 'cm' ); |
||
| 292 | $weight_unit = get_option( 'woocommerce_weight_unit', 'kg' ); |
||
| 293 | ?> |
||
| 294 | <h1><?php _e( 'Store Locale Setup', 'woocommerce' ); ?></h1> |
||
| 295 | <form method="post"> |
||
| 296 | <table class="form-table"> |
||
| 297 | <tr> |
||
| 298 | <th scope="row"><label for="store_location"><?php _e( 'Where is your store based?', 'woocommerce' ); ?></label></th> |
||
| 299 | <td> |
||
| 300 | <select id="store_location" name="store_location" style="width:100%;" required data-placeholder="<?php esc_attr_e( 'Choose a country…', 'woocommerce' ); ?>" class="wc-enhanced-select"> |
||
| 301 | <?php WC()->countries->country_dropdown_options( $country, $state ); ?> |
||
| 302 | </select> |
||
| 303 | </td> |
||
| 304 | </tr> |
||
| 305 | <tr> |
||
| 306 | <th scope="row"><label for="currency_code"><?php _e( 'Which currency will your store use?', 'woocommerce' ); ?></label></th> |
||
| 307 | <td> |
||
| 308 | <select id="currency_code" name="currency_code" style="width:100%;" data-placeholder="<?php esc_attr_e( 'Choose a currency…', 'woocommerce' ); ?>" class="wc-enhanced-select"> |
||
| 309 | <option value=""><?php _e( 'Choose a currency…', 'woocommerce' ); ?></option> |
||
| 310 | <?php |
||
| 311 | foreach ( get_woocommerce_currencies() as $code => $name ) { |
||
| 312 | echo '<option value="' . esc_attr( $code ) . '" ' . selected( $currency, $code, false ) . '>' . esc_html( $name . ' (' . get_woocommerce_currency_symbol( $code ) . ')' ) . '</option>'; |
||
| 313 | } |
||
| 314 | ?> |
||
| 315 | </select> |
||
| 316 | <span class="description"><?php printf( __( 'If your currency is not listed you can %sadd it later%s.', 'woocommerce' ), '<a href="http://docs.woothemes.com/document/add-a-custom-currency-symbol/" target="_blank">', '</a>' ); ?></span> |
||
| 317 | </td> |
||
| 318 | </tr> |
||
| 319 | <tr> |
||
| 320 | <th scope="row"><label for="currency_pos"><?php _e( 'Currency Position', 'woocommerce' ); ?></label></th> |
||
| 321 | <td> |
||
| 322 | <select id="currency_pos" name="currency_pos" class="wc-enhanced-select"> |
||
| 323 | <option value="left" <?php selected( $currency_pos, 'left' ); ?>><?php echo __( 'Left', 'woocommerce' ); ?></option> |
||
| 324 | <option value="right" <?php selected( $currency_pos, 'right' ); ?>><?php echo __( 'Right', 'woocommerce' ); ?></option> |
||
| 325 | <option value="left_space" <?php selected( $currency_pos, 'left_space' ); ?>><?php echo __( 'Left with space', 'woocommerce' ); ?></option> |
||
| 326 | <option value="right_space" <?php selected( $currency_pos, 'right_space' ); ?>><?php echo __( 'Right with space', 'woocommerce' ); ?></option> |
||
| 327 | </select> |
||
| 328 | </td> |
||
| 329 | </tr> |
||
| 330 | <tr> |
||
| 331 | <th scope="row"><label for="thousand_sep"><?php _e( 'Thousand Separator', 'woocommerce' ); ?></label></th> |
||
| 332 | <td> |
||
| 333 | <input type="text" id="thousand_sep" name="thousand_sep" size="2" value="<?php echo esc_attr( $thousand_sep ) ; ?>" /> |
||
| 334 | </td> |
||
| 335 | </tr> |
||
| 336 | <tr> |
||
| 337 | <th scope="row"><label for="decimal_sep"><?php _e( 'Decimal Separator', 'woocommerce' ); ?></label></th> |
||
| 338 | <td> |
||
| 339 | <input type="text" id="decimal_sep" name="decimal_sep" size="2" value="<?php echo esc_attr( $decimal_sep ) ; ?>" /> |
||
| 340 | </td> |
||
| 341 | </tr> |
||
| 342 | <tr> |
||
| 343 | <th scope="row"><label for="num_decimals"><?php _e( 'Number of Decimals', 'woocommerce' ); ?></label></th> |
||
| 344 | <td> |
||
| 345 | <input type="text" id="num_decimals" name="num_decimals" size="2" value="<?php echo esc_attr( $num_decimals ) ; ?>" /> |
||
| 346 | </td> |
||
| 347 | </tr> |
||
| 348 | <tr> |
||
| 349 | <th scope="row"><label for="weight_unit"><?php _e( 'Which unit should be used for product weights?', 'woocommerce' ); ?></label></th> |
||
| 350 | <td> |
||
| 351 | <select id="weight_unit" name="weight_unit" class="wc-enhanced-select"> |
||
| 352 | <option value="kg" <?php selected( $weight_unit, 'kg' ); ?>><?php echo __( 'kg', 'woocommerce' ); ?></option> |
||
| 353 | <option value="g" <?php selected( $weight_unit, 'g' ); ?>><?php echo __( 'g', 'woocommerce' ); ?></option> |
||
| 354 | <option value="lbs" <?php selected( $weight_unit, 'lbs' ); ?>><?php echo __( 'lbs', 'woocommerce' ); ?></option> |
||
| 355 | <option value="oz" <?php selected( $weight_unit, 'oz' ); ?>><?php echo __( 'oz', 'woocommerce' ); ?></option> |
||
| 356 | </select> |
||
| 357 | </td> |
||
| 358 | </tr> |
||
| 359 | <tr> |
||
| 360 | <th scope="row"><label for="dimension_unit"><?php _e( 'Which unit should be used for product dimensions?', 'woocommerce' ); ?></label></th> |
||
| 361 | <td> |
||
| 362 | <select id="dimension_unit" name="dimension_unit" class="wc-enhanced-select"> |
||
| 363 | <option value="m" <?php selected( $dimension_unit, 'm' ); ?>><?php echo __( 'm', 'woocommerce' ); ?></option> |
||
| 364 | <option value="cm" <?php selected( $dimension_unit, 'cm' ); ?>><?php echo __( 'cm', 'woocommerce' ); ?></option> |
||
| 365 | <option value="mm" <?php selected( $dimension_unit, 'mm' ); ?>><?php echo __( 'mm', 'woocommerce' ); ?></option> |
||
| 366 | <option value="in" <?php selected( $dimension_unit, 'in' ); ?>><?php echo __( 'in', 'woocommerce' ); ?></option> |
||
| 367 | <option value="yd" <?php selected( $dimension_unit, 'yd' ); ?>><?php echo __( 'yd', 'woocommerce' ); ?></option> |
||
| 368 | </select> |
||
| 369 | </td> |
||
| 370 | </tr> |
||
| 371 | </table> |
||
| 372 | <p class="wc-setup-actions step"> |
||
| 373 | <input type="submit" class="button-primary button button-large button-next" value="<?php esc_attr_e( 'Continue', 'woocommerce' ); ?>" name="save_step" /> |
||
| 374 | <a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button button-large button-next"><?php _e( 'Skip this step', 'woocommerce' ); ?></a> |
||
| 375 | <?php wp_nonce_field( 'wc-setup' ); ?> |
||
| 376 | </p> |
||
| 377 | </form> |
||
| 378 | <?php |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Save Locale Settings. |
||
| 383 | */ |
||
| 384 | public function wc_setup_locale_save() { |
||
| 385 | check_admin_referer( 'wc-setup' ); |
||
| 386 | |||
| 387 | $store_location = sanitize_text_field( $_POST['store_location'] ); |
||
| 388 | $currency_code = sanitize_text_field( $_POST['currency_code'] ); |
||
| 389 | $currency_pos = sanitize_text_field( $_POST['currency_pos'] ); |
||
| 390 | $decimal_sep = sanitize_text_field( $_POST['decimal_sep'] ); |
||
| 391 | $num_decimals = sanitize_text_field( $_POST['num_decimals'] ); |
||
| 392 | $thousand_sep = sanitize_text_field( $_POST['thousand_sep'] ); |
||
| 393 | $weight_unit = sanitize_text_field( $_POST['weight_unit'] ); |
||
| 394 | $dimension_unit = sanitize_text_field( $_POST['dimension_unit'] ); |
||
| 395 | |||
| 396 | update_option( 'woocommerce_default_country', $store_location ); |
||
| 397 | update_option( 'woocommerce_currency', $currency_code ); |
||
| 398 | update_option( 'woocommerce_currency_pos', $currency_pos ); |
||
| 399 | update_option( 'woocommerce_price_decimal_sep', $decimal_sep ); |
||
| 400 | update_option( 'woocommerce_price_num_decimals', $num_decimals ); |
||
| 401 | update_option( 'woocommerce_price_thousand_sep', $thousand_sep ); |
||
| 402 | update_option( 'woocommerce_weight_unit', $weight_unit ); |
||
| 403 | update_option( 'woocommerce_dimension_unit', $dimension_unit ); |
||
| 404 | |||
| 405 | wp_redirect( esc_url_raw( $this->get_next_step_link() ) ); |
||
| 406 | exit; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Shipping and taxes. |
||
| 411 | */ |
||
| 412 | public function wc_setup_shipping_taxes() { |
||
| 413 | ?> |
||
| 414 | <h1><?php _e( 'Shipping & Tax Setup', 'woocommerce' ); ?></h1> |
||
| 415 | <form method="post"> |
||
| 416 | <p><?php printf( __( 'If you will be charging sales tax, or shipping physical goods to customers, you can configure the basic options below. This is optional and can be changed later via %1$sWooCommerce > Settings > Tax%3$s and %2$sWooCommerce > Settings > Shipping%3$s.', 'woocommerce' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=tax' ) . '" target="_blank">', '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=shipping' ) . '" target="_blank">', '</a>' ); ?></p> |
||
| 417 | <table class="form-table"> |
||
| 418 | <tr class="section_title"> |
||
| 419 | <td colspan="2"> |
||
| 420 | <h2><?php _e( 'Basic Shipping Setup', 'woocommerce' ); ?></h2> |
||
| 421 | </td> |
||
| 422 | </tr> |
||
| 423 | <tr> |
||
| 424 | <th scope="row"><label for="woocommerce_calc_shipping"><?php _e( 'Will you be shipping products?', 'woocommerce' ); ?></label></th> |
||
| 425 | <td> |
||
| 426 | <input type="checkbox" id="woocommerce_calc_shipping" <?php checked( get_option( 'woocommerce_ship_to_countries', '' ) !== 'disabled', true ); ?> name="woocommerce_calc_shipping" class="input-checkbox" value="1" /> |
||
| 427 | <label for="woocommerce_calc_shipping"><?php _e( 'Yes, I will be shipping physical goods to customers', 'woocommerce' ); ?></label> |
||
| 428 | </td> |
||
| 429 | </tr> |
||
| 430 | <tr> |
||
| 431 | <th scope="row"><label for="shipping_cost_domestic"><?php _e( '<strong>Domestic</strong> shipping cost:', 'woocommerce' ); ?></label></th> |
||
| 432 | <td> |
||
| 433 | <?php printf( __( 'A total of %s per order and/or %s per item', 'woocommerce' ), get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_domestic" name="shipping_cost_domestic" size="5" />', get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_domestic_item" name="shipping_cost_domestic_item" size="5" />' ); ?> |
||
| 434 | </td> |
||
| 435 | </tr> |
||
| 436 | <tr> |
||
| 437 | <th scope="row"><label for="shipping_cost_worldwide"><?php _e( '<strong>Worldwide</strong> shipping cost:', 'woocommerce' ); ?></label></th> |
||
| 438 | <td> |
||
| 439 | <?php printf( __( 'A total of %s per order and/or %s per item', 'woocommerce' ), get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_worldwide" name="shipping_cost_worldwide" size="5" />', get_woocommerce_currency_symbol() . ' <input type="text" id="shipping_cost_worldwide_item" name="shipping_cost_worldwide_item" size="5" />' ); ?> |
||
| 440 | </td> |
||
| 441 | </tr> |
||
| 442 | <tr class="section_title"> |
||
| 443 | <td colspan="2"> |
||
| 444 | <h2><?php _e( 'Taxes', 'woocommerce' ); ?></h2> |
||
| 445 | </td> |
||
| 446 | </tr> |
||
| 447 | <tr> |
||
| 448 | <th scope="row"><label for="woocommerce_calc_taxes"><?php _e( 'Will you be charging sales tax?', 'woocommerce' ); ?></label></th> |
||
| 449 | <td> |
||
| 450 | <input type="checkbox" <?php checked( get_option( 'woocommerce_calc_taxes', 'no' ), 'yes' ); ?> id="woocommerce_calc_taxes" name="woocommerce_calc_taxes" class="input-checkbox" value="1" /> |
||
| 451 | <label for="woocommerce_calc_taxes"><?php _e( 'Yes, I will be charging sales tax', 'woocommerce' ); ?></label> |
||
| 452 | </td> |
||
| 453 | </tr> |
||
| 454 | <tr> |
||
| 455 | <th scope="row"><label for="woocommerce_prices_include_tax"><?php _e( 'How will you enter product prices?', 'woocommerce' ); ?></label></th> |
||
| 456 | <td> |
||
| 457 | <label><input type="radio" <?php checked( get_option( 'woocommerce_prices_include_tax', 'no' ), 'yes' ); ?> id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="yes" /> <?php _e( 'I will enter prices inclusive of tax', 'woocommerce' ); ?></label><br/> |
||
| 458 | <label><input type="radio" <?php checked( get_option( 'woocommerce_prices_include_tax', 'no' ), 'no' ); ?> id="woocommerce_prices_include_tax" name="woocommerce_prices_include_tax" class="input-radio" value="no" /> <?php _e( 'I will enter prices exclusive of tax', 'woocommerce' ); ?></label> |
||
| 459 | </td> |
||
| 460 | </tr> |
||
| 461 | <?php |
||
| 462 | $locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' ); |
||
| 463 | $tax_rates = array(); |
||
| 464 | $country = WC()->countries->get_base_country(); |
||
| 465 | $state = WC()->countries->get_base_state(); |
||
| 466 | |||
| 467 | View Code Duplication | if ( isset( $locale_info[ $country ] ) ) { |
|
| 468 | if ( isset( $locale_info[ $country ]['tax_rates'][ $state ] ) ) { |
||
| 469 | $tax_rates = $locale_info[ $country ]['tax_rates'][ $state ]; |
||
| 470 | } elseif ( isset( $locale_info[ $country ]['tax_rates'][''] ) ) { |
||
| 471 | $tax_rates = $locale_info[ $country ]['tax_rates']['']; |
||
| 472 | } |
||
| 473 | if ( isset( $locale_info[ $country ]['tax_rates']['*'] ) ) { |
||
| 474 | $tax_rates = array_merge( $locale_info[ $country ]['tax_rates']['*'], $tax_rates ); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | if ( $tax_rates ) { |
||
| 478 | ?> |
||
| 479 | <tr class="tax-rates"> |
||
| 480 | <td colspan="2"> |
||
| 481 | <p><?php printf( __( 'The following tax rates will be imported automatically for you. You can read more about taxes in %1$sour documentation%2$s.', 'woocommerce' ), '<a href="http://docs.woothemes.com/document/setting-up-taxes-in-woocommerce/" target="_blank">', '</a>' ); ?></p> |
||
| 482 | <div class="importing-tax-rates"> |
||
| 483 | <table class="tax-rates"> |
||
| 484 | <thead> |
||
| 485 | <tr> |
||
| 486 | <th><?php _e( 'Country', 'woocommerce' ); ?></th> |
||
| 487 | <th><?php _e( 'State', 'woocommerce' ); ?></th> |
||
| 488 | <th><?php _e( 'Rate (%)', 'woocommerce' ); ?></th> |
||
| 489 | <th><?php _e( 'Name', 'woocommerce' ); ?></th> |
||
| 490 | </tr> |
||
| 491 | </thead> |
||
| 492 | <tbody> |
||
| 493 | <?php |
||
| 494 | foreach ( $tax_rates as $rate ) { |
||
| 495 | ?> |
||
| 496 | <tr> |
||
| 497 | <td><?php echo esc_attr( $rate['country'] ); ?></td> |
||
| 498 | <td><?php echo esc_attr( $rate['state'] ? $rate['state'] : '*' ); ?></td> |
||
| 499 | <td><?php echo esc_attr( $rate['rate'] ); ?></td> |
||
| 500 | <td><?php echo esc_attr( $rate['name'] ); ?></td> |
||
| 501 | </tr> |
||
| 502 | <?php |
||
| 503 | } |
||
| 504 | ?> |
||
| 505 | </tbody> |
||
| 506 | </table> |
||
| 507 | </div> |
||
| 508 | <p class="description"><?php printf( __( 'You may you need to add/edit rates based on your products or business location which can be done from the %1$stax settings%2$s screen. If in doubt, speak to an accountant.', 'woocommerce' ), '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=tax' ) . '" target="_blank">', '</a>' ); ?></p> |
||
| 509 | </td> |
||
| 510 | </tr> |
||
| 511 | <?php |
||
| 512 | } |
||
| 513 | ?> |
||
| 514 | </table> |
||
| 515 | <p class="wc-setup-actions step"> |
||
| 516 | <input type="submit" class="button-primary button button-large button-next" value="<?php esc_attr_e( 'Continue', 'woocommerce' ); ?>" name="save_step" /> |
||
| 517 | <a href="<?php echo esc_url( $this->get_next_step_link() ); ?>" class="button button-large button-next"><?php _e( 'Skip this step', 'woocommerce' ); ?></a> |
||
| 518 | <?php wp_nonce_field( 'wc-setup' ); ?> |
||
| 519 | </p> |
||
| 520 | </form> |
||
| 521 | <?php |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Save shipping and tax options. |
||
| 526 | */ |
||
| 527 | public function wc_setup_shipping_taxes_save() { |
||
| 528 | check_admin_referer( 'wc-setup' ); |
||
| 529 | |||
| 530 | $enable_shipping = isset( $_POST['woocommerce_calc_shipping'] ); |
||
| 531 | $enable_taxes = isset( $_POST['woocommerce_calc_taxes'] ); |
||
| 532 | |||
| 533 | if ( $enable_shipping ) { |
||
| 534 | update_option( 'woocommerce_ship_to_countries', '' ); |
||
| 535 | } else { |
||
| 536 | update_option( 'woocommerce_ship_to_countries', 'disabled' ); |
||
| 537 | } |
||
| 538 | |||
| 539 | update_option( 'woocommerce_calc_taxes', $enable_taxes ? 'yes' : 'no' ); |
||
| 540 | update_option( 'woocommerce_prices_include_tax', sanitize_text_field( $_POST['woocommerce_prices_include_tax'] ) ); |
||
| 541 | |||
| 542 | if ( $enable_shipping && ! empty( $_POST['shipping_cost_domestic'] ) ) { |
||
| 543 | // Create a domestic shipping zone |
||
| 544 | $zone = new WC_Shipping_Zone( $zone_data['zone_id'] ); |
||
| 545 | $zone->set_zone_name( __( 'Domestic', 'woocommerce' ) ); |
||
| 546 | $zone->set_zone_order( 1 ); |
||
| 547 | $zone->add_location( WC()->countries->get_base_country(), 'country' ); |
||
| 548 | $zone->save(); |
||
| 549 | |||
| 550 | // Add a flat rate shipping method to this domestic zone |
||
| 551 | $instance_id = $zone->add_shipping_method( 'flat_rate' ); |
||
| 552 | $shipping_method = new WC_Shipping_Flat_Rate( $instance_id ); |
||
| 553 | $option_key = $shipping_method->get_instance_option_key(); |
||
| 554 | |||
| 555 | // Update rate settings |
||
| 556 | $costs = array(); |
||
| 557 | $costs[] = wc_format_decimal( sanitize_text_field( $_POST['shipping_cost_domestic'] ) ); |
||
| 558 | if ( $item_cost = sanitize_text_field( $_POST['shipping_cost_domestic_item'] ) ) { |
||
| 559 | $costs[] = $item_cost . ' * [qty]'; |
||
| 560 | } |
||
| 561 | $shipping_method->instance_settings['cost'] = implode( ' + ', array_filter( $costs ) ); |
||
| 562 | $shipping_method->instance_settings['enabled'] = 'yes'; |
||
| 563 | $shipping_method->instance_settings['type'] = 'order'; |
||
| 564 | update_option( $option_key, $shipping_method->instance_settings ); |
||
| 565 | } |
||
| 566 | |||
| 567 | if ( $enable_shipping && ! empty( $_POST['shipping_cost_worldwide'] ) ) { |
||
| 568 | // Add a flat rate shipping method to the worldwide zone |
||
| 569 | $zone = WC_Shipping_Zones::get_zone( 0 ); |
||
| 570 | $instance_id = $zone->add_shipping_method( 'flat_rate' ); |
||
| 571 | $shipping_method = new WC_Shipping_Flat_Rate( $instance_id ); |
||
| 572 | $option_key = $shipping_method->get_instance_option_key(); |
||
| 573 | |||
| 574 | // Update rate settings |
||
| 575 | $costs = array(); |
||
| 576 | $costs[] = wc_format_decimal( sanitize_text_field( $_POST['shipping_cost_worldwide'] ) ); |
||
| 577 | if ( $item_cost = sanitize_text_field( $_POST['shipping_cost_worldwide_item'] ) ) { |
||
| 578 | $costs[] = $item_cost . ' * [qty]'; |
||
| 579 | } |
||
| 580 | $shipping_method->instance_settings['cost'] = implode( ' + ', array_filter( $costs ) ); |
||
| 581 | $shipping_method->instance_settings['enabled'] = 'yes'; |
||
| 582 | $shipping_method->instance_settings['type'] = 'order'; |
||
| 583 | update_option( $option_key, $shipping_method->instance_settings ); |
||
| 584 | } |
||
| 585 | |||
| 586 | if ( $enable_taxes ) { |
||
| 587 | $locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' ); |
||
| 588 | $tax_rates = array(); |
||
| 589 | $country = WC()->countries->get_base_country(); |
||
| 590 | $state = WC()->countries->get_base_state(); |
||
| 591 | |||
| 592 | View Code Duplication | if ( isset( $locale_info[ $country ] ) ) { |
|
| 593 | if ( isset( $locale_info[ $country ]['tax_rates'][ $state ] ) ) { |
||
| 594 | $tax_rates = $locale_info[ $country ]['tax_rates'][ $state ]; |
||
| 595 | } elseif ( isset( $locale_info[ $country ]['tax_rates'][''] ) ) { |
||
| 596 | $tax_rates = $locale_info[ $country ]['tax_rates']['']; |
||
| 597 | } |
||
| 598 | if ( isset( $locale_info[ $country ]['tax_rates']['*'] ) ) { |
||
| 599 | $tax_rates = array_merge( $locale_info[ $country ]['tax_rates']['*'], $tax_rates ); |
||
| 600 | } |
||
| 601 | } |
||
| 602 | if ( $tax_rates ) { |
||
| 603 | $loop = 0; |
||
| 604 | foreach ( $tax_rates as $rate ) { |
||
| 605 | $tax_rate = array( |
||
| 606 | 'tax_rate_country' => $rate['country'], |
||
| 607 | 'tax_rate_state' => $rate['state'], |
||
| 608 | 'tax_rate' => $rate['rate'], |
||
| 609 | 'tax_rate_name' => $rate['name'], |
||
| 610 | 'tax_rate_priority' => isset( $rate['priority'] ) ? absint( $rate['priority'] ) : 1, |
||
| 611 | 'tax_rate_compound' => 0, |
||
| 612 | 'tax_rate_shipping' => $rate['shipping'] ? 1 : 0, |
||
| 613 | 'tax_rate_order' => $loop ++, |
||
| 614 | 'tax_rate_class' => '' |
||
| 615 | ); |
||
| 616 | WC_Tax::_insert_tax_rate( $tax_rate ); |
||
| 617 | } |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | wp_redirect( esc_url_raw( $this->get_next_step_link() ) ); |
||
| 622 | exit; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Payments Step. |
||
| 627 | */ |
||
| 628 | public function wc_setup_payments() { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Payments Step save. |
||
| 686 | */ |
||
| 687 | public function wc_setup_payments_save() { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Actions on the final step. |
||
| 715 | */ |
||
| 716 | private function wc_setup_ready_actions() { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Final step. |
||
| 730 | */ |
||
| 731 | public function wc_setup_ready() { |
||
| 769 | } |
||
| 770 | |||
| 771 | new WC_Admin_Setup_Wizard(); |
||
| 772 |
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.