woocommerce/includes/class-wc-shipping-zones.php

223 lines
7.6 KiB
PHP
Raw Normal View History

<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles storage and retrieval of shipping zones
*
* @class WC_Shipping_Zones
* @since 2.6.0
* @version 2.6.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Shipping_Zones {
/**
* Get shipping zones from the database
* @since 2.6.0
2015-12-10 12:31:03 +00:00
* @return array of arrays
*/
public static function get_zones() {
global $wpdb;
2015-12-10 12:31:03 +00:00
$raw_zones = $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" );
$zones = array();
foreach ( $raw_zones as $raw_zone ) {
2015-12-10 17:07:18 +00:00
$zone = new WC_Shipping_Zone( $raw_zone );
$zones[ $zone->get_zone_id() ] = $zone->get_data();
$zones[ $zone->get_zone_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
$zones[ $zone->get_zone_id() ]['shipping_methods'] = $zone->get_shipping_methods();
}
2015-12-16 13:28:55 +00:00
return $zones;
}
/**
* Get shipping zone using it's ID
* @since 2.6.0
* @param int $zone_id
* @return WC_Shipping_Zone|bool
*/
public static function get_zone( $zone_id ) {
return self::get_zone_by( 'zone_id', $zone_id );
}
/**
* Get shipping zone by an ID.
* @since 2.6.0
* @param string $by zone_id or instance_id
* @param int $id
* @return WC_Shipping_Zone|bool
*/
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
global $wpdb;
2015-12-15 17:54:51 +00:00
$raw_zone = false;
switch ( $by ) {
case 'zone_id' :
2015-12-15 18:04:14 +00:00
if ( 0 === $id ) {
return new WC_Shipping_Zone( 0 );
} else {
$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 ) );
}
break;
case 'instance_id' :
2015-12-15 17:54:51 +00:00
$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 ) );
2015-12-15 18:04:14 +00:00
2015-12-15 17:54:51 +00:00
if ( false !== $zone_id ) {
2015-12-15 18:04:14 +00:00
return self::get_zone_by( 'zone_id', absint( $zone_id ) );
2015-12-15 17:54:51 +00:00
}
break;
}
2015-12-15 18:04:14 +00:00
return $raw_zone ? new WC_Shipping_Zone( $raw_zone ) : false;
}
/**
* Get shipping zone using it's ID
* @since 2.6.0
* @param int $zone_id
* @return WC_Shipping_Meethod|bool
*/
public static function get_shipping_method( $instance_id ) {
global $wpdb;
$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 ) );
$wc_shipping = WC_Shipping::instance();
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
if ( in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ) ) ) {
$class_name = $allowed_classes[ $raw_shipping_method->method_id ];
return new $class_name( $raw_shipping_method->instance_id );
}
return false;
}
/**
* Delete a zone using it's ID
* @param int $zone_id
* @since 2.6.0
*/
public static function delete_zone( $zone_id ) {
global $wpdb;
2016-01-08 13:17:13 +00:00
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'zone_id' => $zone_id ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone_id ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $zone_id ) );
2015-12-15 12:39:52 +00:00
WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
}
2015-12-15 12:39:52 +00:00
/**
* Get postcode wildcards in array format.
*
* Internal use only.
*
* @since 2.6.0
* @access private
*
* @param string $postcode array of values
* @return string[] Array of postcodes with wildcards
*/
private static function _get_wildcard_postcodes( $postcode ) {
$postcodes = array( '*', strtoupper( $postcode ), strtoupper( $postcode ) . '*' );
$postcode_length = strlen( $postcode );
$wildcard_postcode = strtoupper( $postcode );
for ( $i = 0; $i < $postcode_length; $i ++ ) {
$wildcard_postcode = substr( $wildcard_postcode, 0, -1 );
$postcodes[] = $wildcard_postcode . '*';
}
return $postcodes;
}
/**
* Find a matching zone for a given package.
* @since 2.6.0
* @uses wc_make_numeric_postcode()
* @param object $package
* @return WC_Shipping_Zone
*/
public static function get_zone_matching_package( $package ) {
global $wpdb;
$country = strtoupper( wc_clean( $package['destination']['country'] ) );
$state = strtoupper( wc_clean( $package['destination']['state'] ) );
$continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
$postcode = strtoupper( wc_clean( $package['destination']['postcode'] ) );
$valid_postcodes = array_map( 'wc_clean', self::_get_wildcard_postcodes( $postcode ) );
$cache_key = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) );
$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' );
if ( false === $matching_zone_id ) {
// Work out criteria for our zone search
$criteria = array();
$criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
$criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
$criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s ) )", $continent );
// Postcode range and wildcard matching
$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
if ( $postcode_locations ) {
$zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
$zone_id_matches = array();
foreach ( $postcode_locations as $postcode_location ) {
$postcode_to_match = trim( strtoupper( $postcode_location->location_code ) );
// Ranges
if ( strstr( '-', $postcode_to_match ) ) {
$range = array_map( 'trim', explode( '-', $postcode_to_match ) );
if ( sizeof( $range ) != 2 ) {
continue;
}
if ( is_numeric( $range[0] ) && is_numeric( $range[1] ) ) {
$encoded_postcode = $postcode;
$min = $range[0];
$max = $range[1];
} else {
$min = wc_make_numeric_postcode( $range[0] );
$max = wc_make_numeric_postcode( $range[1] );
$min = str_pad( $min, $encoded_postcode_len, '0' );
$max = str_pad( $max, $encoded_postcode_len, '9' );
}
if ( $encoded_postcode >= $min && $encoded_postcode <= $max ) {
$zone_id_matches[] = absint( $postcode_location->zone_id );
}
// Wildcard/standard
} elseif ( in_array( $postcode_to_match, $valid_postcodes ) ) {
$zone_id_matches[] = absint( $postcode_location->zone_id );
}
}
$do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, $zone_id_matches ) );
if ( $do_not_match ) {
$criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")";
}
}
// Get matching zones
$matching_zone_id = $wpdb->get_var( "
SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id
WHERE " . implode( ' ', $criteria ) . "
ORDER BY zone_order ASC LIMIT 1
" );
wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' );
}
return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 );
}
}