Completed
Pull Request — master (#9826)
by Mike
10:01
created

WC_Shipping_Zones::get_zone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
		switch ( $by ) {
61
			case 'zone_id' :
62
				$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 ) );
63
			break;
64
			case 'instance_id' :
65
				$raw_zone = $wpdb->get_row( $wpdb->prepare( "
66
					SELECT zones.zone_id, zones.zone_name, zones.zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
67
					LEFT JOIN {$wpdb->prefix}woocommerce_shipping_zone_methods as methods ON zones.zone_id = methods.zone_id
68
					WHERE methods.instance_id = %d LIMIT 1;
69
					", $id ) );
70
			break;
71
			default :
72
				$raw_zone = false;
73
			break;
74
		}
75
76
		return $raw_zone ? new WC_Shipping_Zone( $raw_zone ) : false;
77
	}
78
79
	/**
80
	 * Get shipping zone using it's ID
81
	 * @since 2.6.0
82
	 * @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...
83
	 * @return WC_Shipping_Meethod|bool
84
	 */
85
	public static function get_shipping_method( $instance_id ) {
86
		global $wpdb;
87
        $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 ) );
88
		$wc_shipping         = WC_Shipping::instance();
89
		$allowed_classes     = $wc_shipping->get_shipping_method_class_names();
90
91 View Code Duplication
		if ( in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
			$class_name = $allowed_classes[ $raw_shipping_method->method_id ];
93
			return new $class_name( $raw_shipping_method->instance_id );
94
		}
95
		return false;
96
	}
97
98
	/**
99
	 * Delete a zone using it's ID
100
	 * @param int $zone_id
101
	 * @since 2.6.0
102
	 */
103
    public static function delete_zone( $zone_id ) {
104
        global $wpdb;
105
        $wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $zone_id ) );
106
		$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zones', array( 'zone_id' => $zone_id ) );
107
    }
108
}
109