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

WC_Shipping_Zone::read_zone_locations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 3
eloc 6
nc 2
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
	 * Location type detection
132
	 * @param  object  $location
133
	 * @return boolean
134
	 */
135
	private function location_is_continent( $location ) {
136
		return 'continent' === $location->type;
137
	}
138
139
	/**
140
	 * Location type detection
141
	 * @param  object  $location
142
	 * @return boolean
143
	 */
144
	private function location_is_country( $location ) {
145
		return 'country' === $location->type;
146
	}
147
148
	/**
149
	 * Location type detection
150
	 * @param  object  $location
151
	 * @return boolean
152
	 */
153
	private function location_is_state( $location ) {
154
		return 'state' === $location->type;
155
	}
156
157
	/**
158
	 * Location type detection
159
	 * @param  object  $location
160
	 * @return boolean
161
	 */
162
	private function location_is_postcode( $location ) {
163
		return 'postcode' === $location->type;
164
	}
165
166
	/**
167
	 * Set zone ID
168
	 * @access private
169
	 * @param int $set
170
	 */
171
    private function set_zone_id( $set ) {
172
        $this->data['zone_id'] = absint( $set );
173
    }
174
175
	/**
176
	 * Set zone name
177
	 * @param string $set
178
	 */
179
    public function set_zone_name( $set ) {
180
		$this->data['zone_name'] = wc_clean( $set );
181
    }
182
183
	/**
184
	 * Set zone order
185
	 * @param int $set
186
	 */
187
	public function set_zone_order( $set ) {
188
        $this->data['zone_order'] = absint( $set );
189
    }
190
191
	/**
192
     * Insert zone into the database
193
     * @access private
194
     * @param int Read zone data from DB
195
     */
196
    private function read( $zone_id ) {
197
		global $wpdb;
198
199
		if ( $zone_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zones WHERE zone_id = %d LIMIT 1;", $zone_id ) ) ) {
200
			$this->set_zone_id( $zone_data->zone_id );
201
			$this->set_zone_name( $zone_data->zone_name );
202
			$this->set_zone_order( $zone_data->zone_order );
203
			$this->read_zone_locations( $zone_data->zone_id );
204
		}
205
	}
206
207
	/**
208
	 * Is passed location type valid?
209
	 * @param  string  $type
210
	 * @return boolean
211
	 */
212
	public function is_valid_location_type( $type ) {
213
		return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ) );
214
	}
215
216
	/**
217
	 * Add location (state or postcode) to a zone.
218
	 * @param string $code
219
	 * @param string $type state or postcode
220
	 */
221
	public function add_location( $code, $type ) {
222
		if ( $this->is_valid_location_type( $type ) ) {
223
			$location = array(
224
				'code' => wc_clean( $code ),
225
				'type' => wc_clean( $type )
226
			);
227
			$this->data['zone_locations'][] = (object) $location;
228
			$this->_locations_changed = true;
229
		}
230
	}
231
232
	/**
233
	 * Clear all locations for this zone.
234
	 */
235
	public function clear_locations() {
236
		$this->data['zone_locations'] = array();
237
		$this->_locations_changed = true;
238
	}
239
240
	/**
241
	 * Set locations
242
	 * @param array $locations Array of locations
243
	 */
244
	public function set_locations( $locations = array() ) {
245
		$this->clear_locations();
246
247
		foreach ( $locations as $location ) {
248
			$this->add_location( $location['code'], $location['type'] );
249
		}
250
251
		$this->_locations_changed = true;
252
	}
253
254
	/**
255
	 * Read location data from the database
256
	 * @param  int $zone_id
257
	 */
258
	private function read_zone_locations( $zone_id ) {
259
		global $wpdb;
260
261
		if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d;", $zone_id ) ) ) {
262
			foreach ( $locations as $location ) {
263
				$this->add_location( $location->location_code, $location->location_type );
264
			}
265
		}
266
		$this->_locations_changed = false;
267
	}
268
269
	/**
270
     * Save zone data to the database
271
     * @param array data to save for this zone
272
     */
273
    public function save() {
274
		$data = array(
275
			'zone_name'  => $this->get_zone_name(),
276
			'zone_order' => $this->get_zone_order(),
277
		);
278
279
        if ( ! $this->get_zone_id() ) {
280
			$this->create( $data );
281
        } else {
282
            $this->update( $data );
283
        }
284
285
		$this->save_locations();
286
	}
287
288
	/**
289
	 * Save locations to the DB
290
	 *
291
	 * This function clears old locations, then re-inserts new if any changes are found.
292
	 */
293
	private function save_locations() {
294
		if ( ! $this->get_zone_id() || ! $this->_locations_changed ) {
295
			return false;
296
		}
297
		global $wpdb;
298
		$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $this->get_zone_id() ) );
299
300
		foreach ( $this->get_zone_locations() as $location ) {
301
			$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array(
302
				'zone_id'       => $this->get_zone_id(),
303
				'location_code' => $location->code,
304
				'location_type' => $location->type
305
			) );
306
		}
307
	}
308
309
	/**
310
     * Insert zone into the database
311
     * @access private
312
     * @param array $zone_data data to save for this zone
313
     */
314
    private function create( $zone_data ) {
315
		global $wpdb;
316
		$wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zones', $zone_data );
317
		$this->set_zone_id( $wpdb->insert_id );
318
	}
319
320
    /**
321
     * Update zone in the database
322
	 * @access private
323
     * @param array $zone_data data to save for this zone
324
     */
325
    public function update( $zone_data ) {
326
        global $wpdb;
327
		$wpdb->update( $wpdb->prefix . 'woocommerce_shipping_zones', $zone_data, array( 'zone_id' => $this->get_zone_id() ) );
328
    }
329
}
330