1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Handles storage and retrieval of shipping zones |
9
|
|
|
* |
10
|
|
|
* @class WC_Shipping_Zones |
11
|
|
|
* @since 2.6.0 |
12
|
|
|
* @version 2.6.0 |
13
|
|
|
* @package WooCommerce/Classes |
14
|
|
|
* @category Class |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Shipping_Zones { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get shipping zones from the database |
21
|
|
|
* @since 2.6.0 |
22
|
|
|
* @return array of arrays |
23
|
|
|
*/ |
24
|
|
|
public static function get_zones() { |
25
|
|
|
global $wpdb; |
26
|
|
|
|
27
|
|
|
$raw_zones = $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" ); |
28
|
|
|
$zones = array(); |
29
|
|
|
|
30
|
|
|
foreach ( $raw_zones as $raw_zone ) { |
31
|
|
|
$zone = new WC_Shipping_Zone( $raw_zone ); |
32
|
|
|
$zones[ $zone->get_zone_id() ] = $zone->get_data(); |
33
|
|
|
$zones[ $zone->get_zone_id() ]['formatted_zone_location'] = $zone->get_formatted_location(); |
34
|
|
|
$zones[ $zone->get_zone_id() ]['shipping_methods'] = $zone->get_shipping_methods(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $zones; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get shipping zone using it's ID |
42
|
|
|
* @since 2.6.0 |
43
|
|
|
* @param int $zone_id |
44
|
|
|
* @return WC_Shipping_Zone|bool |
45
|
|
|
*/ |
46
|
|
|
public static function get_zone( $zone_id ) { |
47
|
|
|
return self::get_zone_by( 'zone_id', $zone_id ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get shipping zone by an ID. |
52
|
|
|
* @since 2.6.0 |
53
|
|
|
* @param string $by zone_id or instance_id |
54
|
|
|
* @param int $id |
55
|
|
|
* @return WC_Shipping_Zone|bool |
56
|
|
|
*/ |
57
|
|
|
public static function get_zone_by( $by = 'zone_id', $id = 0 ) { |
58
|
|
|
global $wpdb; |
59
|
|
|
|
60
|
|
|
$raw_zone = false; |
61
|
|
|
|
62
|
|
|
switch ( $by ) { |
63
|
|
|
case 'zone_id' : |
64
|
|
|
if ( 0 === $id ) { |
65
|
|
|
return new WC_Shipping_Zone( 0 ); |
66
|
|
|
} else { |
67
|
|
|
$raw_zone = $wpdb->get_row( $wpdb->prepare( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1;", $id ) ); |
68
|
|
|
} |
69
|
|
|
break; |
70
|
|
|
case 'instance_id' : |
71
|
|
|
$zone_id = $wpdb->get_var( $wpdb->prepare( "SELECT zone_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods as methods WHERE methods.instance_id = %d LIMIT 1;", $id ) ); |
72
|
|
|
|
73
|
|
|
if ( false !== $zone_id ) { |
74
|
|
|
return self::get_zone_by( 'zone_id', absint( $zone_id ) ); |
75
|
|
|
} |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $raw_zone ? new WC_Shipping_Zone( $raw_zone ) : false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get shipping zone using it's ID |
84
|
|
|
* @since 2.6.0 |
85
|
|
|
* @param int $zone_id |
|
|
|
|
86
|
|
|
* @return WC_Shipping_Meethod|bool |
87
|
|
|
*/ |
88
|
|
|
public static function get_shipping_method( $instance_id ) { |
89
|
|
|
global $wpdb; |
90
|
|
|
$raw_shipping_method = $wpdb->get_row( $wpdb->prepare( "SELECT instance_id, method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) ); |
91
|
|
|
$wc_shipping = WC_Shipping::instance(); |
92
|
|
|
$allowed_classes = $wc_shipping->get_shipping_method_class_names(); |
93
|
|
|
|
94
|
|
|
if ( in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ) ) ) { |
95
|
|
|
$class_name = $allowed_classes[ $raw_shipping_method->method_id ]; |
96
|
|
|
return new $class_name( $raw_shipping_method->instance_id ); |
97
|
|
|
} |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete a zone using it's ID |
103
|
|
|
* @param int $zone_id |
104
|
|
|
* @since 2.6.0 |
105
|
|
|
*/ |
106
|
|
|
public static function delete_zone( $zone_id ) { |
107
|
|
|
global $wpdb; |
108
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone_id ) ); |
109
|
|
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $zone_id ) ); |
110
|
|
|
WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get postcode wildcards in array format. |
115
|
|
|
* |
116
|
|
|
* Internal use only. |
117
|
|
|
* |
118
|
|
|
* @since 2.6.0 |
119
|
|
|
* @access private |
120
|
|
|
* |
121
|
|
|
* @param string $postcode array of values |
122
|
|
|
* @return string[] Array of postcodes with wildcards |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
private static function _get_wildcard_postcodes( $postcode ) { |
|
|
|
|
125
|
|
|
$postcodes = array( '*', strtoupper( $postcode ), strtoupper( $postcode ) . '*' ); |
126
|
|
|
$postcode_length = strlen( $postcode ); |
127
|
|
|
$wildcard_postcode = strtoupper( $postcode ); |
128
|
|
|
|
129
|
|
|
for ( $i = 0; $i < $postcode_length; $i ++ ) { |
130
|
|
|
$wildcard_postcode = substr( $wildcard_postcode, 0, -1 ); |
131
|
|
|
$postcodes[] = $wildcard_postcode . '*'; |
132
|
|
|
} |
133
|
|
|
return $postcodes; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Find a matching zone for a given package. |
138
|
|
|
* @since 2.6.0 |
139
|
|
|
* @uses wc_make_numeric_postcode() |
140
|
|
|
* @param object $package |
141
|
|
|
* @return WC_Shipping_Zone |
142
|
|
|
*/ |
143
|
|
|
public static function get_zone_matching_package( $package ) { |
144
|
|
|
global $wpdb; |
145
|
|
|
|
146
|
|
|
$country = strtoupper( wc_clean( $package['destination']['country'] ) ); |
147
|
|
|
$state = strtoupper( wc_clean( $package['destination']['state'] ) ); |
148
|
|
|
$continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) ); |
149
|
|
|
$postcode = strtoupper( wc_clean( $package['destination']['postcode'] ) ); |
150
|
|
|
$valid_postcodes = array_map( 'wc_clean', self::_get_wildcard_postcodes( $postcode ) ); |
151
|
|
|
$cache_key = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) ); |
152
|
|
|
$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' ); |
153
|
|
|
|
154
|
|
|
if ( false === $matching_zone_id ) { |
155
|
|
|
|
156
|
|
|
// Work out criteria for our zone search |
157
|
|
|
$criteria = array(); |
158
|
|
|
$criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country ); |
159
|
|
|
$criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state ); |
160
|
|
|
$criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s ) )", $continent ); |
161
|
|
|
|
162
|
|
|
// Postcode range and wildcard matching |
163
|
|
|
$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" ); |
164
|
|
|
|
165
|
|
|
if ( $postcode_locations ) { |
166
|
|
|
$zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) ); |
167
|
|
|
$zone_id_matches = array(); |
168
|
|
|
|
169
|
|
|
foreach ( $postcode_locations as $postcode_location ) { |
170
|
|
|
$postcode_to_match = trim( strtoupper( $postcode_location->location_code ) ); |
171
|
|
|
|
172
|
|
|
// Ranges |
173
|
|
|
if ( strstr( '-', $postcode_to_match ) ) { |
174
|
|
|
$range = array_map( 'trim', explode( '-', $postcode_to_match ) ); |
175
|
|
|
|
176
|
|
|
if ( sizeof( $range ) != 2 ) { |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ( is_numeric( $range[0] ) && is_numeric( $range[1] ) ) { |
181
|
|
|
$encoded_postcode = $postcode; |
182
|
|
|
$min = $range[0]; |
183
|
|
|
$max = $range[1]; |
184
|
|
|
} else { |
185
|
|
|
$min = wc_make_numeric_postcode( $range[0] ); |
186
|
|
|
$max = wc_make_numeric_postcode( $range[1] ); |
187
|
|
|
$min = str_pad( $min, $encoded_postcode_len, '0' ); |
188
|
|
|
$max = str_pad( $max, $encoded_postcode_len, '9' ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ( $encoded_postcode >= $min && $encoded_postcode <= $max ) { |
192
|
|
|
$zone_id_matches[] = absint( $postcode_location->zone_id ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Wildcard/standard |
196
|
|
|
} elseif ( in_array( $postcode_to_match, $valid_postcodes ) ) { |
197
|
|
|
$zone_id_matches[] = absint( $postcode_location->zone_id ); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, $zone_id_matches ) ); |
202
|
|
|
|
203
|
|
|
if ( $do_not_match ) { |
|
|
|
|
204
|
|
|
$criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")"; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Get matching zones |
209
|
|
|
$matching_zone_id = $wpdb->get_var( " |
210
|
|
|
SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones |
211
|
|
|
LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id |
212
|
|
|
WHERE " . implode( ' ', $criteria ) . " |
213
|
|
|
ORDER BY zone_order ASC LIMIT 1 |
214
|
|
|
" ); |
215
|
|
|
|
216
|
|
|
wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 ); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
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.