1
|
|
|
<?php |
|
|
|
|
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() { |
|
|
|
|
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() { |
|
|
|
|
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 ) { |
|
|
|
|
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
|
|
|
|
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.