abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
5 years ago
emails
5 years ago
export
5 years ago
gateways
5 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 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
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
5 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 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
5 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
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 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
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
5 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
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
5 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 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 __DIR__ . '/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 |