Completed
Pull Request — master (#10769)
by Mike
07:56
created

WC_Shipping_Zones   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 149
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_zone() 0 3 1
B get_zone_by() 0 24 6
A get_shipping_method() 0 15 3
A get_zones() 0 15 2
A delete_zone() 0 4 1
B get_zone_matching_package() 0 44 5
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Handles storage and retrieval of shipping zones
9
 *
10
 * @class 		WC_Shipping_Zones
11
 * @since 		2.6.0
12
 * @version		2.6.0
13
 * @package		WooCommerce/Classes
14
 * @category	Class
15
 * @author 		WooThemes
16
 */
17
class WC_Shipping_Zones {
18
19
	/**
20
	 * Get shipping zones from the database
21
	 * @since 2.6.0
22
	 * @return array of arrays
23
	 */
24
	public static function get_zones() {
25
		global $wpdb;
26
27
		$raw_zones = $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" );
28
		$zones     = array();
29
30
		foreach ( $raw_zones as $raw_zone ) {
31
			$zone                                                     = new WC_Shipping_Zone( $raw_zone );
32
			$zones[ $zone->get_zone_id() ]                            = $zone->get_data();
33
			$zones[ $zone->get_zone_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
34
			$zones[ $zone->get_zone_id() ]['shipping_methods']        = $zone->get_shipping_methods();
35
		}
36
37
		return $zones;
38
	}
39
40
	/**
41
	 * Get shipping zone using it's ID
42
	 * @since 2.6.0
43
	 * @param int $zone_id
44
	 * @return WC_Shipping_Zone|bool
45
	 */
46
	public static function get_zone( $zone_id ) {
47
		return self::get_zone_by( 'zone_id', $zone_id );
48
	}
49
50
	/**
51
	 * Get shipping zone by an ID.
52
	 * @since 2.6.0
53
	 * @param string $by zone_id or instance_id
54
	 * @param int $id
55
	 * @return WC_Shipping_Zone|bool
56
	 */
57
	public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
58
		global $wpdb;
59
60
		$raw_zone = false;
61
62
		switch ( $by ) {
63
			case 'zone_id' :
64
				if ( 0 === $id ) {
65
					return new WC_Shipping_Zone( 0 );
66
				} else {
67
					$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 ) );
68
				}
69
			break;
70
			case 'instance_id' :
71
				$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 ) );
72
73
				if ( false !== $zone_id ) {
74
					return self::get_zone_by( 'zone_id', absint( $zone_id ) );
75
				}
76
			break;
77
		}
78
79
		return $raw_zone ? new WC_Shipping_Zone( $raw_zone ) : false;
80
	}
81
82
	/**
83
	 * Get shipping zone using it's ID
84
	 * @since 2.6.0
85
	 * @param int $zone_id
0 ignored issues
show
Bug introduced by
There is no parameter named $zone_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
86
	 * @return WC_Shipping_Meethod|bool
87
	 */
88
	public static function get_shipping_method( $instance_id ) {
89
		global $wpdb;
90
		$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 ) );
91
		$wc_shipping         = WC_Shipping::instance();
92
		$allowed_classes     = $wc_shipping->get_shipping_method_class_names();
93
94
		if ( in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ) ) ) {
95
			$class_name = $allowed_classes[ $raw_shipping_method->method_id ];
96
			if ( is_object( $class_name ) ) {
97
				$class_name = get_class( $class_name );
98
			}
99
			return new $class_name( $raw_shipping_method->instance_id );
100
		}
101
		return false;
102
	}
103
104
	/**
105
	 * Delete a zone using it's ID
106
	 * @param int $zone_id
107
	 * @since 2.6.0
108
	 */
109
	public static function delete_zone( $zone_id ) {
110
		$zone = new WC_Shipping_Zone( $zone_id );
111
		$zone->delete();
112
	}
113
114
	/**
115
	 * Find a matching zone for a given package.
116
	 * @since  2.6.0
117
	 * @uses   wc_make_numeric_postcode()
118
	 * @param  object $package
119
	 * @return WC_Shipping_Zone
120
	 */
121
	public static function get_zone_matching_package( $package ) {
122
		global $wpdb;
123
124
		$country          = strtoupper( wc_clean( $package['destination']['country'] ) );
125
		$state            = strtoupper( wc_clean( $package['destination']['state'] ) );
126
		$continent        = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
127
		$postcode         = strtoupper( wc_clean( $package['destination']['postcode'] ) );
128
		$cache_key        = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) );
129
		$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' );
130
131
		if ( false === $matching_zone_id ) {
132
133
			// Work out criteria for our zone search
134
			$criteria = array();
135
			$criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
136
			$criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
137
			$criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s ) )", $continent );
138
139
			// Postcode range and wildcard matching
140
			$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
141
142
			if ( $postcode_locations ) {
143
				$zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
144
				$matches                      = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code' );
0 ignored issues
show
Unused Code introduced by
$matches is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
145
				$do_not_match                 = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $zone_id_matches ) ) );
146
147
				if ( $do_not_match ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $do_not_match of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
148
					$criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")";
149
				}
150
			}
151
152
			// Get matching zones
153
			$matching_zone_id = $wpdb->get_var( "
154
				SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
155
				LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id
156
				WHERE " . implode( ' ', $criteria ) . "
157
				ORDER BY zone_order ASC LIMIT 1
158
			" );
159
160
			wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' );
161
		}
162
163
		return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 );
164
	}
165
}
166