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

WC_Shipping_Zone::get_formatted_location()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 35
rs 8.439
cc 6
eloc 24
nc 32
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 16 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
 * Represents a single shipping zone
9
 *
10
 * @class 		WC_Shipping_Zone
11
 * @version		2.5.0
12
 * @package		WooCommerce/Classes
13
 * @category	Class
14
 * @author 		WooThemes
15
 */
16
class WC_Shipping_Zone {
17
18
	/**
19
	 * Zone Data
20
	 * @var array
21
	 */
22
    private $data = array(
23
		'zone_id'        => 0,
24
		'zone_name'      => '',
25
		'zone_order'     => 0,
26
		'zone_locations' => array()
27
	);
28
29
	/**
30
	 * True when location data needs to be re-saved
31
	 * @var bool
32
	 */
33
	private $_locations_changed = false;
34
35
	/**
36
	 * Constructor for zones
37
	 * @param int|object $zone Zone ID to load from the DB (optional) or already queried data.
38
	 */
39
    public function __construct( $zone = 0 ) {
40
		if ( is_numeric( $zone ) && ! empty( $zone ) ) {
41
        	$this->read( $zone );
42
		} elseif ( is_object( $zone ) ) {
43
			$this->set_zone_id( $zone->zone_id );
44
			$this->set_zone_name( $zone->zone_name );
45
			$this->set_zone_order( $zone->zone_order );
46
			$this->read_zone_locations( $zone->zone_id );
47
		}
48
    }
49
50
	/**
51
	 * Get class data array
52
	 * @return array
53
	 */
54
	public function get_data() {
55
		return $this->data;
56
	}
57
58
	/**
59
	 * Get zone ID
60
	 * @return int
61
	 */
62
    public function get_zone_id() {
63
        return absint( $this->data['zone_id'] );
64
    }
65
66
	/**
67
	 * Get zone name
68
	 * @return string
69
	 */
70
    public function get_zone_name() {
71
        return $this->data['zone_name'];
72
    }
73
74
	/**
75
	 * Get zone order
76
	 * @return int
77
	 */
78
	public function get_zone_order() {
79
        return absint( $this->data['zone_order'] );
80
    }
81
82
	/**
83
	 * Get zone locations
84
	 * @return array of zone objects
85
	 */
86
	public function get_zone_locations() {
87
        return $this->data['zone_locations'];
88
    }
89
90
	/**
91
	 * Return a text string representing what this zone is for.
92
	 * @return string
93
	 */
94
	public function get_formatted_location( $max = 10 ) {
95
		$location_parts = array();
96
		$all_continents = WC()->countries->get_continents();
97
		$all_countries  = WC()->countries->get_countries();
98
		$all_states     = WC()->countries->get_states();
99
		$locations      = $this->get_zone_locations();
100
		$continents     = array_filter( $locations, array( $this, 'location_is_continent' ) );
101
		$countries      = array_filter( $locations, array( $this, 'location_is_country' ) );
102
		$states         = array_filter( $locations, array( $this, 'location_is_state' ) );
103
		$postcodes      = array_filter( $locations, array( $this, 'location_is_postcode' ) );
104
105
		foreach ( $continents as $location ) {
106
			$location_parts[] = $all_continents[ $location->code ]['name'];
107
		}
108
109
		foreach ( $countries as $location ) {
110
			$location_parts[] = $all_countries[ $location->code ];
111
		}
112
113
		foreach ( $states as $location ) {
114
			$location_codes = explode( ':', $location->code );
115
			$location_parts[] = $all_states[ $location_codes[ 0 ] ][ $location_codes[ 1 ] ];
116
		}
117
118
		foreach ( $postcodes as $location ) {
119
			$location_parts[] = $location->code;
120
		}
121
122
		if ( sizeof( $location_parts ) > $max ) {
123
			$remaining = sizeof( $location_parts ) - $max;
124
			return sprintf( _n( '%s and %d other region', '%s and %d other regions', $remaining, 'woocommerce' ), implode( ', ', array_splice( $location_parts, 0, $max ) ), $remaining );
125
		} else {
126
			return implode( ', ', $location_parts );
127
		}
128
	}
129
130
	/**
131
	 * Get shipping methods linked to this zone
132
	 * @return array of objects
133
	 */
134
	public function get_shipping_methods() {
135
		global $wpdb;
136
137
        $raw_methods     = $wpdb->get_results( $wpdb->prepare( "SELECT method_id, method_order, instance_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d order by method_order ASC;", $this->get_zone_id() ) );
138
		$wc_shipping     = WC_Shipping::instance();
139
		$allowed_classes = $wc_shipping->get_shipping_method_class_names();
140
		$methods         = array();
141
142
		foreach ( $raw_methods as $raw_method ) {
143
			if ( in_array( $raw_method->method_id, array_keys( $allowed_classes ) ) ) {
144
				$class_name                          = $allowed_classes[ $raw_method->method_id ];
145
				$methods[ $raw_method->instance_id ] = new $class_name( $raw_method->instance_id );
146
				$methods[ $raw_method->instance_id ]->method_order = absint( $raw_method->method_order );
147
			}
148
		}
149
150
		return $methods;
151
	}
152
153
	/**
154
	 * Location type detection
155
	 * @param  object  $location
156
	 * @return boolean
157
	 */
158
	private function location_is_continent( $location ) {
159
		return 'continent' === $location->type;
160
	}
161
162
	/**
163
	 * Location type detection
164
	 * @param  object  $location
165
	 * @return boolean
166
	 */
167
	private function location_is_country( $location ) {
168
		return 'country' === $location->type;
169
	}
170
171
	/**
172
	 * Location type detection
173
	 * @param  object  $location
174
	 * @return boolean
175
	 */
176
	private function location_is_state( $location ) {
177
		return 'state' === $location->type;
178
	}
179
180
	/**
181
	 * Location type detection
182
	 * @param  object  $location
183
	 * @return boolean
184
	 */
185
	private function location_is_postcode( $location ) {
186
		return 'postcode' === $location->type;
187
	}
188
189
	/**
190
	 * Set zone ID
191
	 * @access private
192
	 * @param int $set
193
	 */
194
    private function set_zone_id( $set ) {
195
        $this->data['zone_id'] = absint( $set );
196
    }
197
198
	/**
199
	 * Set zone name
200
	 * @param string $set
201
	 */
202
    public function set_zone_name( $set ) {
203
		$this->data['zone_name'] = wc_clean( $set );
204
    }
205
206
	/**
207
	 * Set zone order
208
	 * @param int $set
209
	 */
210
	public function set_zone_order( $set ) {
211
        $this->data['zone_order'] = absint( $set );
212
    }
213
214
	/**
215
     * Insert zone into the database
216
     * @access private
217
     * @param int Read zone data from DB
218
     */
219
    private function read( $zone_id ) {
220
		global $wpdb;
221
222
		if ( $zone_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1;", $zone_id ) ) ) {
223
			$this->set_zone_id( $zone_data->zone_id );
224
			$this->set_zone_name( $zone_data->zone_name );
225
			$this->set_zone_order( $zone_data->zone_order );
226
			$this->read_zone_locations( $zone_data->zone_id );
227
		}
228
	}
229
230
	/**
231
	 * Is passed location type valid?
232
	 * @param  string  $type
233
	 * @return boolean
234
	 */
235
	public function is_valid_location_type( $type ) {
236
		return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ) );
237
	}
238
239
	/**
240
	 * Add location (state or postcode) to a zone.
241
	 * @param string $code
242
	 * @param string $type state or postcode
243
	 */
244
	public function add_location( $code, $type ) {
245
		if ( $this->is_valid_location_type( $type ) ) {
246
			$location = array(
247
				'code' => wc_clean( $code ),
248
				'type' => wc_clean( $type )
249
			);
250
			$this->data['zone_locations'][] = (object) $location;
251
			$this->_locations_changed = true;
252
		}
253
	}
254
255
	/**
256
	 * Clear all locations for this zone.
257
	 */
258
	public function clear_locations() {
259
		$this->data['zone_locations'] = array();
260
		$this->_locations_changed = true;
261
	}
262
263
	/**
264
	 * Set locations
265
	 * @param array $locations Array of locations
266
	 */
267
	public function set_locations( $locations = array() ) {
268
		$this->clear_locations();
269
270
		foreach ( $locations as $location ) {
271
			$this->add_location( $location['code'], $location['type'] );
272
		}
273
274
		$this->_locations_changed = true;
275
	}
276
277
	/**
278
	 * Read location data from the database
279
	 * @param  int $zone_id
280
	 */
281
	private function read_zone_locations( $zone_id ) {
282
		global $wpdb;
283
284
		if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d;", $zone_id ) ) ) {
285
			foreach ( $locations as $location ) {
286
				$this->add_location( $location->location_code, $location->location_type );
287
			}
288
		}
289
		$this->_locations_changed = false;
290
	}
291
292
	/**
293
     * Save zone data to the database
294
     * @param array data to save for this zone
295
     */
296
    public function save() {
297
		$data = array(
298
			'zone_name'  => $this->get_zone_name(),
299
			'zone_order' => $this->get_zone_order(),
300
		);
301
302
        if ( ! $this->get_zone_id() ) {
303
			$this->create( $data );
304
        } else {
305
            $this->update( $data );
306
        }
307
308
		$this->save_locations();
309
		WC_Cache_Helper::incr_cache_prefix( 'shipping_zones' );
310
	}
311
312
	/**
313
	 * Save locations to the DB
314
	 *
315
	 * This function clears old locations, then re-inserts new if any changes are found.
316
	 */
317
	private function save_locations() {
318
		if ( ! $this->get_zone_id() || ! $this->_locations_changed ) {
319
			return false;
320
		}
321
		global $wpdb;
322
		$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $this->get_zone_id() ) );
323
324
		foreach ( $this->get_zone_locations() as $location ) {
325
			$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array(
326
				'zone_id'       => $this->get_zone_id(),
327
				'location_code' => $location->code,
328
				'location_type' => $location->type
329
			) );
330
		}
331
	}
332
333
	/**
334
     * Insert zone into the database
335
     * @access private
336
     * @param array $zone_data data to save for this zone
337
     */
338
    private function create( $zone_data ) {
339
		global $wpdb;
340
		$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zones', $zone_data );
341
		$this->set_zone_id( $wpdb->insert_id );
342
	}
343
344
    /**
345
     * Update zone in the database
346
	 * @access private
347
     * @param array $zone_data data to save for this zone
348
     */
349
    public function update( $zone_data ) {
350
        global $wpdb;
351
		$wpdb->update( $wpdb->prefix . 'woocommerce_shipping_zones', $zone_data, array( 'zone_id' => $this->get_zone_id() ) );
352
    }
353
}
354