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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-shipping-zone.php
woocommerce / includes Last commit date
abstracts 8 years ago admin 8 years ago api 8 years ago cli 8 years ago customizer 8 years ago data-stores 8 years ago emails 8 years ago export 8 years ago gateways 8 years ago import 8 years ago interfaces 8 years ago legacy 8 years ago libraries 8 years ago log-handlers 8 years ago payment-tokens 9 years ago shipping 8 years ago shortcodes 8 years ago theme-support 8 years ago walkers 8 years ago widgets 8 years ago class-wc-ajax.php 8 years ago class-wc-api.php 8 years ago class-wc-auth.php 8 years ago class-wc-autoloader.php 8 years ago class-wc-background-emailer.php 8 years ago class-wc-background-updater.php 8 years ago class-wc-breadcrumb.php 8 years ago class-wc-cache-helper.php 8 years ago class-wc-cart-fees.php 8 years ago class-wc-cart-session.php 8 years ago class-wc-cart-totals.php 8 years ago class-wc-cart.php 8 years ago class-wc-checkout.php 8 years ago class-wc-cli.php 9 years ago class-wc-comments.php 8 years ago class-wc-countries.php 8 years ago class-wc-coupon.php 8 years ago class-wc-customer-download-log.php 8 years ago class-wc-customer-download.php 8 years ago class-wc-customer.php 8 years ago class-wc-data-exception.php 9 years ago class-wc-data-store.php 8 years ago class-wc-datetime.php 8 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 8 years ago class-wc-discounts.php 8 years ago class-wc-download-handler.php 8 years ago class-wc-emails.php 8 years ago class-wc-embed.php 9 years ago class-wc-form-handler.php 8 years ago class-wc-frontend-scripts.php 8 years ago class-wc-geo-ip.php 9 years ago class-wc-geolocation.php 8 years ago class-wc-https.php 9 years ago class-wc-install.php 8 years ago class-wc-integrations.php 9 years ago class-wc-log-levels.php 9 years ago class-wc-logger.php 8 years ago class-wc-meta-data.php 8 years ago class-wc-order-factory.php 8 years ago class-wc-order-item-coupon.php 8 years ago class-wc-order-item-fee.php 8 years ago class-wc-order-item-meta.php 8 years ago class-wc-order-item-product.php 8 years ago class-wc-order-item-shipping.php 8 years ago class-wc-order-item-tax.php 8 years ago class-wc-order-item.php 8 years ago class-wc-order-query.php 8 years ago class-wc-order-refund.php 9 years ago class-wc-order.php 8 years ago class-wc-payment-gateways.php 8 years ago class-wc-payment-tokens.php 9 years ago class-wc-post-data.php 8 years ago class-wc-post-types.php 8 years ago class-wc-product-attribute.php 8 years ago class-wc-product-download.php 8 years ago class-wc-product-external.php 8 years ago class-wc-product-factory.php 9 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 8 years ago class-wc-product-simple.php 8 years ago class-wc-product-variable.php 8 years ago class-wc-product-variation.php 8 years ago class-wc-query.php 8 years ago class-wc-regenerate-images-request.php 8 years ago class-wc-regenerate-images.php 8 years ago class-wc-register-wp-admin-settings.php 9 years ago class-wc-session-handler.php 8 years ago class-wc-shipping-rate.php 8 years ago class-wc-shipping-zone.php 8 years ago class-wc-shipping-zones.php 8 years ago class-wc-shipping.php 8 years ago class-wc-shortcodes.php 8 years ago class-wc-structured-data.php 8 years ago class-wc-tax.php 8 years ago class-wc-template-loader.php 8 years ago class-wc-tracker.php 8 years ago class-wc-validation.php 8 years ago class-wc-webhook.php 8 years ago class-woocommerce.php 8 years ago wc-account-functions.php 8 years ago wc-attribute-functions.php 8 years ago wc-cart-functions.php 8 years ago wc-conditional-functions.php 8 years ago wc-core-functions.php 8 years ago wc-coupon-functions.php 9 years ago wc-deprecated-functions.php 8 years ago wc-formatting-functions.php 8 years ago wc-notice-functions.php 8 years ago wc-order-functions.php 8 years ago wc-order-item-functions.php 9 years ago wc-page-functions.php 8 years ago wc-product-functions.php 8 years ago wc-rest-functions.php 8 years ago wc-stock-functions.php 8 years ago wc-template-functions.php 8 years ago wc-template-hooks.php 8 years ago wc-term-functions.php 8 years ago wc-update-functions.php 8 years ago wc-user-functions.php 8 years ago wc-webhook-functions.php 8 years ago wc-widget-functions.php 8 years ago
class-wc-shipping-zone.php
436 lines
1 <?php
2 include_once( 'legacy/class-wc-legacy-shipping-zone.php' );
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 /**
9 * Represents a single shipping zone
10 *
11 * @class WC_Shipping_Zone
12 * @since 2.6.0
13 * @version 3.0.0
14 * @package WooCommerce/Classes
15 * @category Class
16 * @author WooCommerce
17 */
18 class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
19
20 /**
21 * Zone ID
22 * @var int|null
23 */
24 protected $id = null;
25
26 /**
27 * This is the name of this object type.
28 * @var string
29 */
30 protected $object_type = 'shipping_zone';
31
32 /**
33 * Zone Data
34 * @var array
35 */
36 protected $data = array(
37 'zone_name' => '',
38 'zone_order' => 0,
39 'zone_locations' => array(),
40 );
41
42 /**
43 * Constructor for zones.
44 *
45 * @param int|object $zone Zone ID to load from the DB or zone object.
46 */
47 public function __construct( $zone = null ) {
48 if ( is_numeric( $zone ) && ! empty( $zone ) ) {
49 $this->set_id( $zone );
50 } elseif ( is_object( $zone ) ) {
51 $this->set_id( $zone->zone_id );
52 } elseif ( 0 === $zone || "0" === $zone ) {
53 $this->set_id( 0 );
54 } else {
55 $this->set_object_read( true );
56 }
57
58 $this->data_store = WC_Data_Store::load( 'shipping-zone' );
59 if ( false === $this->get_object_read() ) {
60 $this->data_store->read( $this );
61 }
62 }
63
64 /*
65 |--------------------------------------------------------------------------
66 | Getters
67 |--------------------------------------------------------------------------
68 */
69
70 /**
71 * Get zone name.
72 *
73 * @param string $context
74 * @return string
75 */
76 public function get_zone_name( $context = 'view' ) {
77 return $this->get_prop( 'zone_name', $context );
78 }
79
80 /**
81 * Get zone order.
82 *
83 * @param string $context
84 * @return int
85 */
86 public function get_zone_order( $context = 'view' ) {
87 return $this->get_prop( 'zone_order', $context );
88 }
89
90 /**
91 * Get zone locations.
92 *
93 * @param string $context
94 * @return array of zone objects
95 */
96 public function get_zone_locations( $context = 'view' ) {
97 return $this->get_prop( 'zone_locations', $context );
98 }
99
100 /**
101 * Return a text string representing what this zone is for.
102 *
103 * @param int $max
104 * @param string $context
105 * @return string
106 */
107 public function get_formatted_location( $max = 10, $context = 'view' ) {
108 $location_parts = array();
109 $all_continents = WC()->countries->get_continents();
110 $all_countries = WC()->countries->get_countries();
111 $all_states = WC()->countries->get_states();
112 $locations = $this->get_zone_locations( $context );
113 $continents = array_filter( $locations, array( $this, 'location_is_continent' ) );
114 $countries = array_filter( $locations, array( $this, 'location_is_country' ) );
115 $states = array_filter( $locations, array( $this, 'location_is_state' ) );
116 $postcodes = array_filter( $locations, array( $this, 'location_is_postcode' ) );
117
118 foreach ( $continents as $location ) {
119 $location_parts[] = $all_continents[ $location->code ]['name'];
120 }
121
122 foreach ( $countries as $location ) {
123 $location_parts[] = $all_countries[ $location->code ];
124 }
125
126 foreach ( $states as $location ) {
127 $location_codes = explode( ':', $location->code );
128 $location_parts[] = $all_states[ $location_codes[0] ][ $location_codes[1] ];
129 }
130
131 foreach ( $postcodes as $location ) {
132 $location_parts[] = $location->code;
133 }
134
135 // Fix display of encoded characters.
136 $location_parts = array_map( 'html_entity_decode', $location_parts );
137
138 if ( sizeof( $location_parts ) > $max ) {
139 $remaining = sizeof( $location_parts ) - $max;
140 // @codingStandardsIgnoreStart
141 return sprintf( _n( '%s and %d other region', '%s and %d other regions', $remaining, 'woocommerce' ), implode( ', ', array_splice( $location_parts, 0, $max ) ), $remaining );
142 // @codingStandardsIgnoreEnd
143 } elseif ( ! empty( $location_parts ) ) {
144 return implode( ', ', $location_parts );
145 } else {
146 return __( 'Everywhere', 'woocommerce' );
147 }
148 }
149
150 /**
151 * Get shipping methods linked to this zone.
152 *
153 * @param bool Only return enabled methods.
154 * @return array of objects
155 */
156 public function get_shipping_methods( $enabled_only = false ) {
157 if ( null === $this->get_id() ) {
158 return array();
159 }
160
161 $raw_methods = $this->data_store->get_methods( $this->get_id(), $enabled_only );
162 $wc_shipping = WC_Shipping::instance();
163 $allowed_classes = $wc_shipping->get_shipping_method_class_names();
164 $methods = array();
165
166 foreach ( $raw_methods as $raw_method ) {
167 if ( in_array( $raw_method->method_id, array_keys( $allowed_classes ), true ) ) {
168 $class_name = $allowed_classes[ $raw_method->method_id ];
169
170 // The returned array may contain instances of shipping methods, as well
171 // as classes. If the "class" is an instance, just use it. If not,
172 // create an instance.
173 if ( is_object( $class_name ) ) {
174 $class_name_of_instance = get_class( $class_name );
175 $methods[ $raw_method->instance_id ] = new $class_name_of_instance( $raw_method->instance_id );
176 } else {
177 // If the class is not an object, it should be a string. It's better
178 // to double check, to be sure (a class must be a string, anything)
179 // else would be useless
180 if ( is_string( $class_name ) && class_exists( $class_name ) ) {
181 $methods[ $raw_method->instance_id ] = new $class_name( $raw_method->instance_id );
182 }
183 }
184
185 // Let's make sure that we have an instance before setting its attributes
186 if ( is_object( $methods[ $raw_method->instance_id ] ) ) {
187 $methods[ $raw_method->instance_id ]->method_order = absint( $raw_method->method_order );
188 $methods[ $raw_method->instance_id ]->enabled = $raw_method->is_enabled ? 'yes' : 'no';
189 $methods[ $raw_method->instance_id ]->has_settings = $methods[ $raw_method->instance_id ]->has_settings();
190 $methods[ $raw_method->instance_id ]->settings_html = $methods[ $raw_method->instance_id ]->supports( 'instance-settings-modal' ) ? $methods[ $raw_method->instance_id ]->get_admin_options_html() : false;
191 $methods[ $raw_method->instance_id ]->method_description = wp_kses_post( wpautop( $methods[ $raw_method->instance_id ]->method_description ) );
192 }
193 }
194 }
195
196 uasort( $methods, 'wc_shipping_zone_method_order_uasort_comparison' );
197
198 return apply_filters( 'woocommerce_shipping_zone_shipping_methods', $methods, $raw_methods, $allowed_classes, $this );
199 }
200
201 /*
202 |--------------------------------------------------------------------------
203 | Setters
204 |--------------------------------------------------------------------------
205 */
206
207 /**
208 * Set zone name.
209 *
210 * @param string $set
211 */
212 public function set_zone_name( $set ) {
213 $this->set_prop( 'zone_name', wc_clean( $set ) );
214 }
215
216 /**
217 * Set zone order.
218 *
219 * @param int $set
220 */
221 public function set_zone_order( $set ) {
222 $this->set_prop( 'zone_order', absint( $set ) );
223 }
224
225 /**
226 * Set zone locations.
227 *
228 * @since 3.0.0
229 * @param array
230 */
231 public function set_zone_locations( $locations ) {
232 if ( 0 !== $this->get_id() ) {
233 $this->set_prop( 'zone_locations', $locations );
234 }
235 }
236
237 /*
238 |--------------------------------------------------------------------------
239 | Other Methods
240 |--------------------------------------------------------------------------
241 */
242
243 /**
244 * Save zone data to the database.
245 *
246 * @return int
247 */
248 public function save() {
249 if ( ! $this->get_zone_name() ) {
250 $this->set_zone_name( $this->generate_zone_name() );
251 }
252 if ( $this->data_store ) {
253 // Trigger action before saving to the DB. Allows you to adjust object props before save.
254 do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
255
256 if ( null === $this->get_id() ) {
257 $this->data_store->create( $this );
258 } else {
259 $this->data_store->update( $this );
260 }
261 return $this->get_id();
262 }
263 }
264
265 /**
266 * Generate a zone name based on location.
267 *
268 * @return string
269 */
270 protected function generate_zone_name() {
271 $zone_name = $this->get_formatted_location();
272
273 if ( empty( $zone_name ) ) {
274 $zone_name = __( 'Zone', 'woocommerce' );
275 }
276
277 return $zone_name;
278 }
279
280 /**
281 * Location type detection.
282 *
283 * @param object $location
284 * @return boolean
285 */
286 private function location_is_continent( $location ) {
287 return 'continent' === $location->type;
288 }
289
290 /**
291 * Location type detection.
292 *
293 * @param object $location
294 * @return boolean
295 */
296 private function location_is_country( $location ) {
297 return 'country' === $location->type;
298 }
299
300 /**
301 * Location type detection.
302 *
303 * @param object $location
304 * @return boolean
305 */
306 private function location_is_state( $location ) {
307 return 'state' === $location->type;
308 }
309
310 /**
311 * Location type detection.
312 *
313 * @param object $location
314 * @return boolean
315 */
316 private function location_is_postcode( $location ) {
317 return 'postcode' === $location->type;
318 }
319
320 /**
321 * Is passed location type valid?
322 *
323 * @param string $type
324 * @return boolean
325 */
326 public function is_valid_location_type( $type ) {
327 return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ) );
328 }
329
330 /**
331 * Add location (state or postcode) to a zone.
332 *
333 * @param string $code
334 * @param string $type state or postcode
335 */
336 public function add_location( $code, $type ) {
337 if ( 0 !== $this->get_id() && $this->is_valid_location_type( $type ) ) {
338 if ( 'postcode' === $type ) {
339 $code = trim( strtoupper( str_replace( chr( 226 ) . chr( 128 ) . chr( 166 ), '...', $code ) ) ); // No normalization - postcodes are matched against both normal and formatted versions to support wildcards.
340 }
341 $location = array(
342 'code' => wc_clean( $code ),
343 'type' => wc_clean( $type ),
344 );
345 $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
346 $zone_locations[] = (object) $location;
347 $this->set_prop( 'zone_locations', $zone_locations );
348 }
349 }
350
351
352 /**
353 * Clear all locations for this zone.
354 *
355 * @param array|string $types of location to clear
356 */
357 public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
358 if ( ! is_array( $types ) ) {
359 $types = array( $types );
360 }
361 $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
362 foreach ( $zone_locations as $key => $values ) {
363 if ( in_array( $values->type, $types ) ) {
364 unset( $zone_locations[ $key ] );
365 }
366 }
367 $zone_locations = array_values( $zone_locations ); // reindex.
368 $this->set_prop( 'zone_locations', $zone_locations );
369 }
370
371 /**
372 * Set locations.
373 *
374 * @param array $locations Array of locations
375 */
376 public function set_locations( $locations = array() ) {
377 $this->clear_locations();
378 foreach ( $locations as $location ) {
379 $this->add_location( $location['code'], $location['type'] );
380 }
381 }
382
383 /**
384 * Add a shipping method to this zone.
385 *
386 * @param string $type shipping method type
387 * @return int new instance_id, 0 on failure
388 */
389 public function add_shipping_method( $type ) {
390 if ( null === $this->get_id() ) {
391 $this->save();
392 }
393
394 $instance_id = 0;
395 $wc_shipping = WC_Shipping::instance();
396 $allowed_classes = $wc_shipping->get_shipping_method_class_names();
397 $count = $this->data_store->get_method_count( $this->get_id() );
398
399 if ( in_array( $type, array_keys( $allowed_classes ) ) ) {
400 $instance_id = $this->data_store->add_method( $this->get_id(), $type, $count + 1 );
401 }
402
403 if ( $instance_id ) {
404 do_action( 'woocommerce_shipping_zone_method_added', $instance_id, $type, $this->get_id() );
405 }
406
407 WC_Cache_Helper::get_transient_version( 'shipping', true );
408
409 return $instance_id;
410 }
411
412 /**
413 * Delete a shipping method from a zone.
414 *
415 * @param int $instance_id
416 * @return True on success, false on failure
417 */
418 public function delete_shipping_method( $instance_id ) {
419 if ( null === $this->get_id() ) {
420 return false;
421 }
422
423 // Get method details.
424 $method = $this->data_store->get_method( $instance_id );
425
426 if ( $method ) {
427 $this->data_store->delete_method( $instance_id );
428 do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $method->method_id, $this->get_id() );
429 }
430
431 WC_Cache_Helper::get_transient_version( 'shipping', true );
432
433 return true;
434 }
435 }
436