diff --git a/includes/class-wc-shipping-zones.php b/includes/class-wc-shipping-zones.php index 6c5f03665d3..44102053513 100644 --- a/includes/class-wc-shipping-zones.php +++ b/includes/class-wc-shipping-zones.php @@ -21,10 +21,10 @@ class WC_Shipping_Zones { * @since 2.6.0 * @return array of arrays */ - public static function get_zones() { + public static function get_zones() { global $wpdb; - $raw_zones = $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" ); + $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 ) { @@ -35,7 +35,7 @@ class WC_Shipping_Zones { } return $zones; - } + } /** * Get shipping zone using it's ID @@ -106,32 +106,9 @@ class WC_Shipping_Zones { * @param int $zone_id * @since 2.6.0 */ - public static function delete_zone( $zone_id ) { + public static function delete_zone( $zone_id ) { $zone = new WC_Shipping_Zone( $zone_id ); $zone->delete(); - } - - /** - * 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; } /** @@ -148,7 +125,6 @@ class WC_Shipping_Zones { $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' ); @@ -165,41 +141,8 @@ class WC_Shipping_Zones { 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 ) ); + $matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code' ); + $do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $zone_id_matches ) ) ); if ( $do_not_match ) { $criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")"; diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index 3f6144db8b7..3b18f20265f 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -234,12 +234,11 @@ class WC_Tax { } $postcode = wc_clean( $postcode ); - $valid_postcodes = self::_get_wildcard_postcodes( $postcode ); $cache_key = WC_Cache_Helper::get_cache_prefix( 'taxes' ) . 'wc_tax_rates_' . md5( sprintf( '%s+%s+%s+%s+%s', $country, $state, $city, $postcode, $tax_class ) ); $matched_tax_rates = wp_cache_get( $cache_key, 'taxes' ); if ( false === $matched_tax_rates ) { - $matched_tax_rates = self::get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class, $valid_postcodes ); + $matched_tax_rates = self::get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class ); wp_cache_set( $cache_key, $matched_tax_rates, 'taxes' ); } @@ -275,49 +274,63 @@ class WC_Tax { * @param string $postcode * @param string $city * @param string $tax_class - * @param string[] $valid_postcodes * @return array */ - private static function get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class, $valid_postcodes ) { + private static function get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class ) { global $wpdb; - $valid_postcodes = array_map( 'esc_sql', array_map( 'wc_clean', $valid_postcodes ) ); - $match_country = esc_sql( strtoupper( wc_clean( $country ) ) ); - $match_state = esc_sql( strtoupper( wc_clean( $state ) ) ); - $match_tax_class = esc_sql( sanitize_title( $tax_class ) ); - $match_city = esc_sql( strtoupper( wc_clean( $city ) ) ); - $found_rates = $wpdb->get_results( " + // Query criteria - these will be ANDed + $criteria = array(); + $criteria[] = $wpdb->prepare( "tax_rate_country IN ( %s, '' )", strtoupper( $country ) ); + $criteria[] = $wpdb->prepare( "tax_rate_state IN ( %s, '' )", strtoupper( $state ) ); + $criteria[] = $wpdb->prepare( "tax_rate_class = %s", sanitize_title( $tax_class ) ); + + // Pre-query postcode ranges for PHP based matching. + $postcode_search = wc_get_wildcard_postcodes( $postcode ); + $postcode_ranges = $wpdb->get_results( "SELECT tax_rate_id, location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE location_type = 'postcode' AND location_code LIKE '%-%';" ); + + if ( $postcode_ranges ) { + $matches = wc_postcode_location_matcher( $postcode, $postcode_ranges, 'tax_rate_id', 'location_code' ); + $postcode_search = array_unique( array_merge( $postcode_search, array_values( $matches ) ) ); + } + + /** + * Location matching criteria - ORed + * Needs to match: + * - rates with no postcodes and cities + * - rates with a matching postcode and city + * - rates with matching postcode, no city + * - rates with matching city, no postcode + */ + $locations_criteria = array(); + $locations_criteria[] = "locations.location_type IS NULL"; + $locations_criteria[] = " + locations.location_type = 'postcode' AND locations.location_code IN ('" . implode( "','", array_map( 'esc_sql', $postcode_search ) ) . "') + AND ( + ( locations2.location_type = 'city' AND locations2.location_code = '" . esc_sql( strtoupper( $city ) ) . "' ) + OR NOT EXISTS ( + SELECT sub.tax_rate_id FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sub + WHERE sub.location_type = 'city' + AND sub.tax_rate_id = tax_rates.tax_rate_id + ) + ) + "; + $locations_criteria[] = " + locations.location_type = 'city' AND locations.location_code = '" . esc_sql( strtoupper( $city ) ) . "' + AND NOT EXISTS ( + SELECT sub.tax_rate_id FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sub + WHERE sub.location_type = 'postcode' + AND sub.tax_rate_id = tax_rates.tax_rate_id + ) + "; + $criteria[] = '( ( ' . implode( ' ) OR ( ', $locations_criteria ) . ' ) )'; + + $found_rates = $wpdb->get_results( " SELECT tax_rates.* FROM {$wpdb->prefix}woocommerce_tax_rates as tax_rates LEFT OUTER JOIN {$wpdb->prefix}woocommerce_tax_rate_locations as locations ON tax_rates.tax_rate_id = locations.tax_rate_id LEFT OUTER JOIN {$wpdb->prefix}woocommerce_tax_rate_locations as locations2 ON tax_rates.tax_rate_id = locations2.tax_rate_id - WHERE tax_rate_country IN ( '{$match_country}', '' ) - AND tax_rate_state IN ( '{$match_state}', '' ) - AND tax_rate_class = '{$match_tax_class}' - AND ( - locations.location_type IS NULL - OR ( - locations.location_type = 'postcode' - AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') - AND ( - locations2.location_type = 'city' AND locations2.location_code = '{$match_city}' - OR 0 = ( - SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations - WHERE sublocations.location_type = 'city' - AND sublocations.tax_rate_id = tax_rates.tax_rate_id - ) - ) - ) - OR ( - locations.location_type = 'city' - AND locations.location_code = '{$match_city}' - AND 0 = ( - SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations - WHERE sublocations.location_type = 'postcode' - AND sublocations.tax_rate_id = tax_rates.tax_rate_id - ) - ) - ) + WHERE 1=1 AND " . implode( ' AND ', $criteria ) . " GROUP BY tax_rate_id ORDER BY tax_rate_priority, tax_rate_order " ); @@ -809,7 +822,6 @@ class WC_Tax { $postcodes = explode( ';', $postcodes ); } $postcodes = array_filter( array_diff( array_map( array( __CLASS__, 'format_tax_rate_postcode' ), $postcodes ), array( '*' ) ) ); - $postcodes = self::_get_expanded_numeric_ranges_from_array( $postcodes ); self::_update_tax_rate_locations( $tax_rate_id, $postcodes, 'postcode' ); } @@ -870,59 +882,6 @@ class WC_Tax { WC_Cache_Helper::incr_cache_prefix( 'taxes' ); } - /** - * Expands ranges in an array (used for zipcodes). e.g. 101-105 would expand to 101, 102, 103, 104, 105. - * - * Internal use only. - * - * @since 2.3.0 - * @access private - * - * @param array $values array of values - * @return array expanded values - */ - private static function _get_expanded_numeric_ranges_from_array( $values = array() ) { - $expanded = array(); - foreach ( $values as $value ) { - if ( strstr( $value, '-' ) ) { - $parts = array_map( 'absint', array_map( 'trim', explode( '-', $value ) ) ); - - for ( $expanded_value = $parts[0]; $expanded_value <= $parts[1]; $expanded_value ++ ) { - if ( strlen( $expanded_value ) < strlen( $parts[0] ) ) { - $expanded_value = str_pad( $expanded_value, strlen( $parts[0] ), "0", STR_PAD_LEFT ); - } - $expanded[] = $expanded_value; - } - } else { - $expanded[] = trim( $value ); - } - } - return array_filter( $expanded ); - } - - /** - * Get postcode wildcards in array format. - * - * Internal use only. - * - * @since 2.3.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; - } - /** * Used by admin settings page. * diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 5274fdc0ac4..876c9f19821 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -1223,3 +1223,69 @@ function wc_help_tip( $tip, $allow_html = false ) { return ''; } + +/** + * Return a list of potential postcodes for wildcard searching. + * @since 2.6.0 + * @param string $postcode + * @return array + */ +function wc_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; +} + +/** + * Used by shipping zones and taxes to compare a given $postcode to stored + * postcodes to find matches for numerical ranges, and wildcards. + * @since 2.6.0 + * @param string $postcode Postcode you want to match against stored postcodes + * @param array $objects Array of postcode objects from Database + * @param string $object_compare_key DB column name for the ID. + * @param string $object_compare_key DB column name for the value. + * @return array Array of matching object ID and values. + */ +function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key ) { + $matches = array(); + $wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode ) ); + $postcodes = wp_list_pluck( $objects, $object_compare_key, $object_id_key ); + + foreach ( $postcodes as $object_id => $compare_against ) { + $compare = $postcode; + + // Handle postcodes containing ranges + if ( strstr( $compare_against, '-' ) ) { + $range = array_map( 'trim', explode( '-', $compare_against ) ); + + if ( 2 !== sizeof( $range ) ) { + continue; + } + + list( $min, $max ) = $range; + + // If the postcode is non-numeric, make it numeric + if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) { + $compare = wc_make_numeric_postcode( $compare ); + $min = str_pad( wc_make_numeric_postcode( $min ), strlen( $encoded_postcode ), '0' ); + $max = str_pad( wc_make_numeric_postcode( $max ), strlen( $encoded_postcode ), '0' ); + } + + if ( $compare >= $min && $compare <= $max ) { + $matches[ $object_id ] = $compare_against; + } + + // Wildcard and standard comparison + } elseif ( in_array( $compare_against, $wildcard_postcodes ) ) { + $matches[ $object_id ] = $compare_against; + } + } + + return $matches; +}