Completed
Pull Request — master (#9826)
by Mike
15:37
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 Destination', 'woocommerce' ),
101
				'desc'    => __( 'This controls which shipping address is used by default.', 'woocommerce' ),
102
				'id'      => 'woocommerce_ship_to_destination',
103
				'default' => 'billing',
104
				'type'    => 'radio',
105
				'options' => array(
106
					'shipping'     => __( 'Default to shipping address', 'woocommerce' ),
107
					'billing'      => __( 'Default to billing address', 'woocommerce' ),
108
					'billing_only' => __( 'Only ship to the customer\'s billing address', 'woocommerce' ),
109
				),
110
				'autoload'        => false,
111
				'desc_tip'        =>  true,
112
				'show_if_checked' => 'option',
113
			),
114
115
			array(
116
				'title'    => __( 'Restrict shipping to Location(s)', 'woocommerce' ),
117
				'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' ) ),
118
				'id'       => 'woocommerce_ship_to_countries',
119
				'default'  => '',
120
				'type'     => 'select',
121
				'class'    => 'wc-enhanced-select',
122
				'desc_tip' => false,
123
				'options'  => array(
124
					''         => __( 'Ship to all countries you sell to', 'woocommerce' ),
125
					'all'      => __( 'Ship to all countries', 'woocommerce' ),
126
					'specific' => __( 'Ship to specific countries only', 'woocommerce' )
127
				)
128
			),
129
130
			array(
131
				'title'   => __( 'Specific Countries', 'woocommerce' ),
132
				'desc'    => '',
133
				'id'      => 'woocommerce_specific_ship_to_countries',
134
				'css'     => '',
135
				'default' => '',
136
				'type'    => 'multi_select_countries'
137
			),
138
139
			array( 'type' => 'sectionend', 'id' => 'shipping_options' ),
140
141
		) );
142
143
		return apply_filters( 'woocommerce_get_settings_' . $this->id, $settings );
144
	}
145
146
	/**
147
	 * Output the settings.
148
	 */
149 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...
150
		global $current_section;
151
152
		// Load shipping methods so we can show any global options they may have
153
		$shipping_methods = WC()->shipping->load_shipping_methods();
154
155
		if ( $current_section ) {
156
157
 			foreach ( $shipping_methods as $method ) {
158
159
				if ( strtolower( get_class( $method ) ) == strtolower( $current_section ) && $method->has_settings() ) {
160
					$method->admin_options();
161
					break;
162
				}
163
			}
164
 		} else {
165
			$settings = $this->get_settings();
166
167
			WC_Admin_Settings::output_fields( $settings );
168
		}
169
	}
170
171
	/**
172
	 * Save settings.
173
	 */
174
	public function save() {
175
		global $current_section;
176
177
		$wc_shipping = WC_Shipping::instance();
178
179
		if ( ! $current_section ) {
180
			WC_Admin_Settings::save_fields( $this->get_settings() );
181
182
		} else {
183
			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...
184
				if ( $current_section === sanitize_title( get_class( $method ) ) ) {
185
					do_action( 'woocommerce_update_options_' . $this->id . '_' . $method->id );
186
				}
187
			}
188
		}
189
190
		// Increments the transient version to invalidate cache
191
		WC_Cache_Helper::get_transient_version( 'shipping', true );
192
	}
193
}
194
195
endif;
196
197
return new WC_Settings_Shipping();
198