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