Completed
Pull Request — master (#9826)
by Mike
10:44
created

WC_Settings_Shipping::shipping_methods_setting()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 54
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 54
rs 8.745
cc 6
eloc 29
nc 9
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 12.

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.

Loading history...
2
/**
3
 * WooCommerce Shipping Settings
4
 *
5
 * @author      WooThemes
6
 * @category    Admin
7
 * @package     WooCommerce/Admin
8
 * @version     2.1.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit; // Exit if accessed directly
13
}
14
15
if ( ! class_exists( 'WC_Settings_Shipping' ) ) :
16
17
/**
18
 * WC_Settings_Shipping.
19
 */
20
class WC_Settings_Shipping extends WC_Settings_Page {
21
22
	/**
23
	 * Constructor.
24
	 */
25
	public function __construct() {
26
		$this->id    = 'shipping';
27
		$this->label = __( 'Shipping', 'woocommerce' );
28
29
		add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
30
		add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
31
		add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
32
		add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
33
	}
34
35
	/**
36
	 * Get sections.
37
	 *
38
	 * @return array
39
	 */
40 View Code Duplication
	public function get_sections() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
		$sections = array(
42
			'' => __( 'Shipping Options', 'woocommerce' )
43
		);
44
45
		if ( ! defined( 'WC_INSTALLING' ) ) {
46
			// Load shipping methods so we can show any global options they may have
47
			$shipping_methods = WC()->shipping->load_shipping_methods();
48
49
			foreach ( $shipping_methods as $method ) {
50
				if ( ! $method->has_settings() ) {
51
					continue;
52
				}
53
				$title = empty( $method->method_title ) ? ucfirst( $method->id ) : $method->method_title;
54
				$sections[ strtolower( get_class( $method ) ) ] = esc_html( $title );
55
			}
56
		}
57
58
		return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
59
	}
60
61
	/**
62
	 * Get settings array.
63
	 *
64
	 * @return array
65
	 */
66
	public function get_settings() {
67
68
		$settings = apply_filters('woocommerce_shipping_settings', array(
69
70
			array( 'title' => __( 'Shipping Options', 'woocommerce' ), 'type' => 'title', 'id' => 'shipping_options' ),
71
72
			array(
73
				'title'         => __( 'Shipping Calculations', 'woocommerce' ),
74
				'desc'          => __( 'Enable shipping', 'woocommerce' ),
75
				'id'            => 'woocommerce_calc_shipping',
76
				'default'       => 'no',
77
				'type'          => 'checkbox',
78
				'checkboxgroup' => 'start'
79
			),
80
81
			array(
82
				'desc'          => __( 'Enable the shipping calculator on the cart page', 'woocommerce' ),
83
				'id'            => 'woocommerce_enable_shipping_calc',
84
				'default'       => 'yes',
85
				'type'          => 'checkbox',
86
				'checkboxgroup' => '',
87
				'autoload'      => false
88
			),
89
90
			array(
91
				'desc'          => __( 'Hide shipping costs until an address is entered', 'woocommerce' ),
92
				'id'            => 'woocommerce_shipping_cost_requires_address',
93
				'default'       => 'no',
94
				'type'          => 'checkbox',
95
				'checkboxgroup' => 'end',
96
				'autoload'      => false
97
			),
98
99
			array(
100
				'title'    => __( 'Shipping Display Mode', 'woocommerce' ),
101
				'desc'     => __( 'This controls how multiple shipping methods are displayed on the frontend.', 'woocommerce' ),
102
				'id'       => 'woocommerce_shipping_method_format',
103
				'default'  => '',
104
				'type'     => 'radio',
105
				'options'  => array(
106
					''       => __( 'Display shipping methods with "radio" buttons', 'woocommerce' ),
107
					'select' => __( 'Display shipping methods in a dropdown', 'woocommerce' ),
108
				),
109
				'desc_tip' =>  true,
110
				'autoload' => false
111
			),
112
113
			array(
114
				'title'   => __( 'Shipping Destination', 'woocommerce' ),
115
				'desc'    => __( 'This controls which shipping address is used by default.', 'woocommerce' ),
116
				'id'      => 'woocommerce_ship_to_destination',
117
				'default' => 'billing',
118
				'type'    => 'radio',
119
				'options' => array(
120
					'shipping'     => __( 'Default to shipping address', 'woocommerce' ),
121
					'billing'      => __( 'Default to billing address', 'woocommerce' ),
122
					'billing_only' => __( 'Only ship to the customer\'s billing address', 'woocommerce' ),
123
				),
124
				'autoload'        => false,
125
				'desc_tip'        =>  true,
126
				'show_if_checked' => 'option',
127
			),
128
129
			array(
130
				'title'    => __( 'Restrict shipping to Location(s)', 'woocommerce' ),
131
				'desc'     => sprintf( __( 'Choose which countries you want to ship to, or choose to ship to all <a href="%s">locations you sell to</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=general' ) ),
132
				'id'       => 'woocommerce_ship_to_countries',
133
				'default'  => '',
134
				'type'     => 'select',
135
				'class'    => 'wc-enhanced-select',
136
				'desc_tip' => false,
137
				'options'  => array(
138
					''         => __( 'Ship to all countries you sell to', 'woocommerce' ),
139
					'all'      => __( 'Ship to all countries', 'woocommerce' ),
140
					'specific' => __( 'Ship to specific countries only', 'woocommerce' )
141
				)
142
			),
143
144
			array(
145
				'title'   => __( 'Specific Countries', 'woocommerce' ),
146
				'desc'    => '',
147
				'id'      => 'woocommerce_specific_ship_to_countries',
148
				'css'     => '',
149
				'default' => '',
150
				'type'    => 'multi_select_countries'
151
			),
152
153
			array( 'type' => 'sectionend', 'id' => 'shipping_options' ),
154
155
		) );
156
157
		return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings );
158
	}
159
160
	/**
161
	 * Output the settings.
162
	 */
163 View Code Duplication
	public function output() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
		global $current_section;
165
166
		// Load shipping methods so we can show any global options they may have
167
		$shipping_methods = WC()->shipping->load_shipping_methods();
168
169
		if ( $current_section ) {
170
171
 			foreach ( $shipping_methods as $method ) {
172
173
				if ( strtolower( get_class( $method ) ) == strtolower( $current_section ) && $method->has_settings() ) {
174
					$method->admin_options();
175
					break;
176
				}
177
			}
178
 		} else {
179
			$settings = $this->get_settings();
180
181
			WC_Admin_Settings::output_fields( $settings );
182
		}
183
	}
184
185
	/**
186
	 * Save settings.
187
	 */
188
	public function save() {
189
		global $current_section;
190
191
		$wc_shipping = WC_Shipping::instance();
192
193
		if ( ! $current_section ) {
194
			WC_Admin_Settings::save_fields( $this->get_settings() );
195
196
		} else {
197
			foreach ( $wc_shipping->get_shipping_methods() as $method_id => $method ) {
0 ignored issues
show
Bug introduced by
The expression $wc_shipping->get_shipping_methods() of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
198
				if ( $current_section === sanitize_title( get_class( $method ) ) ) {
199
					do_action( 'woocommerce_update_options_' . $this->id . '_' . $method->id );
200
				}
201
			}
202
		}
203
204
		// Increments the transient version to invalidate cache
205
		WC_Cache_Helper::get_transient_version( 'shipping', true );
206
	}
207
}
208
209
endif;
210
211
return new WC_Settings_Shipping();
212