class-wc-connect-options.php
9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
if ( ! class_exists( 'WC_Connect_Options' ) ) {
class WC_Connect_Options {
/**
* An array that maps a grouped option type to an option name.
*
* @var array
*/
private static $grouped_options = array(
'compact' => 'wc_connect_options',
);
/**
* Returns an array of option names for a given type.
*
* @param string $type The type of option to return. Defaults to 'compact'.
*
* @return array
*/
public static function get_option_names( $type = 'compact' ) {
switch ( $type ) {
case 'non_compact':
return array(
'error_notice',
'services',
'services_last_update',
'last_heartbeat',
'origin_address',
'last_rate_request',
'services_last_result_code',
);
case 'shipping_method':
return array(
'form_settings',
'failure_timestamp',
);
}
return array(
'tos_accepted',
'store_guid',
'debug_logging_enabled',
'debug_display_enabled',
'payment_methods',
'account_settings',
'paper_size',
'packages',
'predefined_packages',
'shipping_methods_migrated',
'should_display_nux_after_jp_cxn_banner',
'needs_tax_environment_setup',
'banner_ppec',
);
}
/**
* Deletes all options created by WooCommerce Shipping & Tax, including shipping method options
*/
public static function delete_all_options() {
if ( defined( 'WOOCOMMERCE_CONNECT_DEV_SERVER_URL' ) ) {
return;
}
foreach ( self::$grouped_options as $group_key => $group ) {
// delete legacy options
foreach ( self::get_option_names( $group_key ) as $group_option ) {
delete_option( "wc_connect_$group_option" );
}
delete_option( $group );
}
$non_compacts = self::get_option_names( 'non_compact' );
foreach ( $non_compacts as $non_compact ) {
delete_option( "wc_connect_$non_compact" );
}
self::delete_all_shipping_methods_options();
}
/**
* Returns the requested option. Looks in wc_connect_options or wc_connect_$name as appropriate.
*
* @param string $name Option name
* @param mixed $default (optional)
*
* @return mixed
*/
public static function get_option( $name, $default = false ) {
if ( self::is_valid( $name, 'non_compact' ) ) {
return get_option( "wc_connect_$name", $default );
}
foreach ( array_keys( self::$grouped_options ) as $group ) {
if ( self::is_valid( $name, $group ) ) {
return self::get_grouped_option( $group, $name, $default );
}
}
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax option name: %s', $name ), E_USER_WARNING );
return $default;
}
/**
* Updates the single given option. Updates wc_connect_options or wc_connect_$name as appropriate.
*
* @param string $name Option name
* @param mixed $value Option value
*
* @return bool Was the option successfully updated?
*/
public static function update_option( $name, $value ) {
if ( self::is_valid( $name, 'non_compact' ) ) {
return update_option( "wc_connect_$name", $value );
}
foreach ( array_keys( self::$grouped_options ) as $group ) {
if ( self::is_valid( $name, $group ) ) {
return self::update_grouped_option( $group, $name, $value );
}
}
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax option name: %s', $name ), E_USER_WARNING );
return false;
}
/**
* Deletes the given option. May be passed multiple option names as an array.
* Updates wc_connect_options and/or deletes wc_connect_$name as appropriate.
*
* @param string|array $names
*
* @return bool Was the option successfully deleted?
*/
public static function delete_option( $names ) {
$result = true;
$names = (array) $names;
if ( ! self::is_valid( $names ) ) {
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
return false;
}
foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
if ( ! delete_option( "wc_connect_$name" ) ) {
$result = false;
}
}
foreach ( array_keys( self::$grouped_options ) as $group ) {
if ( ! self::delete_grouped_option( $group, $names ) ) {
$result = false;
}
}
return $result;
}
/**
* Gets a shipping method option
*
* @param $name
* @param $default
* @param $service_id
* @param $service_instance
*
* @return mixed
*/
public static function get_shipping_method_option( $name, $default, $service_id, $service_instance = false ) {
$option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
if ( ! $option_name ) {
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax shipping method option name: %s', $name ), E_USER_WARNING );
return $default;
}
return get_option( $option_name, $default );
}
/**
* Updates a shipping method option
*
* @param $name
* @param $value
* @param $service_id
* @param $service_instance
*
* @return bool
*/
public static function update_shipping_method_option( $name, $value, $service_id, $service_instance = false ) {
$option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
if ( ! $option_name ) {
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax shipping method option name: %s', $name ), E_USER_WARNING );
return false;
}
return update_option( $option_name, $value );
}
/**
* Deletes a shipping method option
*
* @param $name
* @param $service_id
* @param $service_instance
*
* @return bool
*/
public static function delete_shipping_method_option( $name, $service_id, $service_instance = false ) {
$option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
if ( ! $option_name ) {
trigger_error( sprintf( 'Invalid WooCommerce Shipping & Tax shipping method option name: %s', $name ), E_USER_WARNING );
return false;
}
return delete_option( $option_name );
}
/**
* Deletes all options related to a shipping method
*
* @param $service_id
* @param $service_instance
*/
public static function delete_shipping_method_options( $service_id, $service_instance = false ) {
$option_names = self::get_option_names( 'shipping_method' );
foreach ( $option_names as $name ) {
delete_option( self::get_shipping_method_option_name( $name, $service_id, $service_instance ) );
}
}
private static function get_grouped_option( $group, $name, $default ) {
$options = get_option( self::$grouped_options[ $group ] );
if ( is_array( $options ) && isset( $options[ $name ] ) ) {
return $options[ $name ];
}
// make the grouped options backwards-compatible and migrate the old options
$legacy_name = "wc_connect_$name";
$legacy_option = get_option( $legacy_name, false );
if ( ! $legacy_option ) {
return $default;
}
if ( self::update_grouped_option( $group, $name, $legacy_option ) ) {
delete_option( $legacy_name );
}
return $legacy_option;
}
private static function update_grouped_option( $group, $name, $value ) {
$options = get_option( self::$grouped_options[ $group ] );
if ( ! is_array( $options ) ) {
$options = array();
}
$options[ $name ] = $value;
return update_option( self::$grouped_options[ $group ], $options );
}
private static function delete_grouped_option( $group, $names ) {
$options = get_option( self::$grouped_options[ $group ], array() );
$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
if ( $to_delete ) {
foreach ( $to_delete as $name ) {
unset( $options[ $name ] );
}
return update_option( self::$grouped_options[ $group ], $options );
}
return true;
}
/**
* Based on the service id and optional instance, generates the option name
*
* @param $name
* @param $service_id
* @param $service_instance
*
* @return string|bool
*/
private static function get_shipping_method_option_name( $name, $service_id, $service_instance = false ) {
if ( ! in_array( $name, self::get_option_names( 'shipping_method' ) ) ) {
return false;
}
if ( ! $service_instance ) {
return 'woocommerce_' . $service_id . '_' . $name;
}
return 'woocommerce_' . $service_id . '_' . $service_instance . '_' . $name;
}
/**
* Is the option name valid?
*
* @param string $name The name of the option
* @param string $group The name of the group that the option is in. Defaults to compact.
*
* @return bool Is the option name valid?
*/
private static function is_valid( $name, $group = 'non_compact' ) {
$group_keys = array_keys( self::$grouped_options );
if ( is_array( $name ) ) {
$compact_names = array();
foreach ( $group_keys as $_group ) {
$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
}
$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
return empty( $result );
}
if ( is_null( $group ) || 'non_compact' === $group ) {
if ( in_array( $name, self::get_option_names( $group ) ) ) {
return true;
}
}
foreach ( array_keys( self::$grouped_options ) as $_group ) {
if ( is_null( $group ) || $group === $_group ) {
if ( in_array( $name, self::get_option_names( $_group ) ) ) {
return true;
}
}
}
return false;
}
/**
* Deletes all options of all shipping methods
*/
private static function delete_all_shipping_methods_options() {
global $wpdb;
$methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_methods " );
foreach ( (array) $methods as $method ) {
self::delete_shipping_method_options( $method->method_id, $method->instance_id );
}
}
}
}