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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 6 years ago admin 6 years ago api 6 years ago blocks 7 years ago cli 7 years ago customizer 8 years ago data-stores 6 years ago emails 7 years ago export 7 years ago gateways 7 years ago import 7 years ago interfaces 7 years ago legacy 7 years ago libraries 6 years ago log-handlers 7 years ago payment-tokens 8 years ago queue 7 years ago shipping 7 years ago shortcodes 6 years ago theme-support 7 years ago tracks 6 years ago walkers 8 years ago widgets 6 years ago class-wc-ajax.php 6 years ago class-wc-api.php 7 years ago class-wc-auth.php 7 years ago class-wc-autoloader.php 6 years ago class-wc-background-emailer.php 8 years ago class-wc-background-updater.php 7 years ago class-wc-breadcrumb.php 7 years ago class-wc-cache-helper.php 7 years ago class-wc-cart-fees.php 8 years ago class-wc-cart-session.php 7 years ago class-wc-cart-totals.php 7 years ago class-wc-cart.php 7 years ago class-wc-checkout.php 7 years ago class-wc-cli.php 8 years ago class-wc-comments.php 7 years ago class-wc-countries.php 6 years ago class-wc-coupon.php 7 years ago class-wc-customer-download-log.php 8 years ago class-wc-customer-download.php 8 years ago class-wc-customer.php 7 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 7 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 7 years ago class-wc-download-handler.php 6 years ago class-wc-emails.php 7 years ago class-wc-embed.php 8 years ago class-wc-form-handler.php 6 years ago class-wc-frontend-scripts.php 7 years ago class-wc-geo-ip.php 8 years ago class-wc-geolite-integration.php 7 years ago class-wc-geolocation.php 7 years ago class-wc-https.php 8 years ago class-wc-install.php 6 years ago class-wc-integrations.php 8 years ago class-wc-log-levels.php 7 years ago class-wc-logger.php 7 years ago class-wc-meta-data.php 7 years ago class-wc-order-factory.php 8 years ago class-wc-order-item-coupon.php 7 years ago class-wc-order-item-fee.php 7 years ago class-wc-order-item-meta.php 7 years ago class-wc-order-item-product.php 7 years ago class-wc-order-item-shipping.php 7 years ago class-wc-order-item-tax.php 8 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 7 years ago class-wc-payment-gateways.php 7 years ago class-wc-payment-tokens.php 7 years ago class-wc-post-data.php 7 years ago class-wc-post-types.php 7 years ago class-wc-privacy-background-process.php 8 years ago class-wc-privacy-erasers.php 8 years ago class-wc-privacy-exporters.php 7 years ago class-wc-privacy.php 7 years ago class-wc-product-attribute.php 7 years ago class-wc-product-download.php 7 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 8 years ago class-wc-product-variable.php 7 years ago class-wc-product-variation.php 7 years ago class-wc-query.php 7 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-session-handler.php 7 years ago class-wc-shipping-rate.php 8 years ago class-wc-shipping-zone.php 7 years ago class-wc-shipping-zones.php 8 years ago class-wc-shipping.php 7 years ago class-wc-shortcodes.php 7 years ago class-wc-structured-data.php 6 years ago class-wc-tax.php 7 years ago class-wc-template-loader.php 7 years ago class-wc-tracker.php 6 years ago class-wc-validation.php 6 years ago class-wc-webhook.php 7 years ago class-woocommerce.php 6 years ago wc-account-functions.php 7 years ago wc-attribute-functions.php 7 years ago wc-cart-functions.php 7 years ago wc-conditional-functions.php 6 years ago wc-core-functions.php 7 years ago wc-coupon-functions.php 7 years ago wc-deprecated-functions.php 7 years ago wc-formatting-functions.php 6 years ago wc-notice-functions.php 7 years ago wc-order-functions.php 7 years ago wc-order-item-functions.php 7 years ago wc-page-functions.php 7 years ago wc-product-functions.php 7 years ago wc-rest-functions.php 7 years ago wc-stock-functions.php 7 years ago wc-template-functions.php 7 years ago wc-template-hooks.php 7 years ago wc-term-functions.php 7 years ago wc-update-functions.php 7 years ago wc-user-functions.php 7 years ago wc-webhook-functions.php 7 years ago wc-widget-functions.php 8 years ago
class-wc-shipping-zone.php
446 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 if ( $this->data_store ) {
263 // Trigger action before saving to the DB. Allows you to adjust object props before save.
264 do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
265
266 if ( null === $this->get_id() ) {
267 $this->data_store->create( $this );
268 } else {
269 $this->data_store->update( $this );
270 }
271 return $this->get_id();
272 }
273 }
274
275 /**
276 * Generate a zone name based on location.
277 *
278 * @return string
279 */
280 protected function generate_zone_name() {
281 $zone_name = $this->get_formatted_location();
282
283 if ( empty( $zone_name ) ) {
284 $zone_name = __( 'Zone', 'woocommerce' );
285 }
286
287 return $zone_name;
288 }
289
290 /**
291 * Location type detection.
292 *
293 * @param object $location Location to check.
294 * @return boolean
295 */
296 private function location_is_continent( $location ) {
297 return 'continent' === $location->type;
298 }
299
300 /**
301 * Location type detection.
302 *
303 * @param object $location Location to check.
304 * @return boolean
305 */
306 private function location_is_country( $location ) {
307 return 'country' === $location->type;
308 }
309
310 /**
311 * Location type detection.
312 *
313 * @param object $location Location to check.
314 * @return boolean
315 */
316 private function location_is_state( $location ) {
317 return 'state' === $location->type;
318 }
319
320 /**
321 * Location type detection.
322 *
323 * @param object $location Location to check.
324 * @return boolean
325 */
326 private function location_is_postcode( $location ) {
327 return 'postcode' === $location->type;
328 }
329
330 /**
331 * Is passed location type valid?
332 *
333 * @param string $type Type to check.
334 * @return boolean
335 */
336 public function is_valid_location_type( $type ) {
337 return in_array( $type, apply_filters( 'woocommerce_valid_location_types', array( 'postcode', 'state', 'country', 'continent' ) ), true );
338 }
339
340 /**
341 * Add location (state or postcode) to a zone.
342 *
343 * @param string $code Location code.
344 * @param string $type state or postcode.
345 */
346 public function add_location( $code, $type ) {
347 if ( 0 !== $this->get_id() && $this->is_valid_location_type( $type ) ) {
348 if ( 'postcode' === $type ) {
349 $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.
350 }
351 $location = array(
352 'code' => wc_clean( $code ),
353 'type' => wc_clean( $type ),
354 );
355 $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
356 $zone_locations[] = (object) $location;
357 $this->set_prop( 'zone_locations', $zone_locations );
358 }
359 }
360
361
362 /**
363 * Clear all locations for this zone.
364 *
365 * @param array|string $types of location to clear.
366 */
367 public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
368 if ( ! is_array( $types ) ) {
369 $types = array( $types );
370 }
371 $zone_locations = $this->get_prop( 'zone_locations', 'edit' );
372 foreach ( $zone_locations as $key => $values ) {
373 if ( in_array( $values->type, $types, true ) ) {
374 unset( $zone_locations[ $key ] );
375 }
376 }
377 $zone_locations = array_values( $zone_locations ); // reindex.
378 $this->set_prop( 'zone_locations', $zone_locations );
379 }
380
381 /**
382 * Set locations.
383 *
384 * @param array $locations Array of locations.
385 */
386 public function set_locations( $locations = array() ) {
387 $this->clear_locations();
388 foreach ( $locations as $location ) {
389 $this->add_location( $location['code'], $location['type'] );
390 }
391 }
392
393 /**
394 * Add a shipping method to this zone.
395 *
396 * @param string $type shipping method type.
397 * @return int new instance_id, 0 on failure
398 */
399 public function add_shipping_method( $type ) {
400 if ( null === $this->get_id() ) {
401 $this->save();
402 }
403
404 $instance_id = 0;
405 $wc_shipping = WC_Shipping::instance();
406 $allowed_classes = $wc_shipping->get_shipping_method_class_names();
407 $count = $this->data_store->get_method_count( $this->get_id() );
408
409 if ( in_array( $type, array_keys( $allowed_classes ), true ) ) {
410 $instance_id = $this->data_store->add_method( $this->get_id(), $type, $count + 1 );
411 }
412
413 if ( $instance_id ) {
414 do_action( 'woocommerce_shipping_zone_method_added', $instance_id, $type, $this->get_id() );
415 }
416
417 WC_Cache_Helper::get_transient_version( 'shipping', true );
418
419 return $instance_id;
420 }
421
422 /**
423 * Delete a shipping method from a zone.
424 *
425 * @param int $instance_id Shipping method instance ID.
426 * @return True on success, false on failure
427 */
428 public function delete_shipping_method( $instance_id ) {
429 if ( null === $this->get_id() ) {
430 return false;
431 }
432
433 // Get method details.
434 $method = $this->data_store->get_method( $instance_id );
435
436 if ( $method ) {
437 $this->data_store->delete_method( $instance_id );
438 do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $method->method_id, $this->get_id() );
439 }
440
441 WC_Cache_Helper::get_transient_version( 'shipping', true );
442
443 return true;
444 }
445 }
446