|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class AshXML { |
|
4
|
|
|
/** |
|
5
|
|
|
* This function iterates over the keys from an array, if there is any |
|
6
|
|
|
* non-numeric keys, it is associative, else it is a "list" |
|
7
|
|
|
* |
|
8
|
|
|
* @since 0.0.1 |
|
9
|
|
|
* @param array $data |
|
10
|
|
|
* @return boolean |
|
11
|
|
|
*/ |
|
12
|
|
|
function _is_list($data){ |
|
13
|
|
|
$is_num = true; |
|
14
|
|
|
if (!is_array($data)){ return false; } |
|
15
|
|
|
foreach((array)$data as $key=>$value){ |
|
16
|
|
|
if (!is_numeric($key)){ |
|
17
|
|
|
$is_num = false; |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
return $is_num; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Helper function that parses xml attributes from an array into a string |
|
25
|
|
|
* |
|
26
|
|
|
* @since 0.0.1 |
|
27
|
|
|
* @param array $attrs |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
function _parse_attrs($attrs){ |
|
31
|
|
|
$attrString = ""; |
|
32
|
|
|
foreach($attrs as $key=>$value){ |
|
33
|
|
|
$attrString .= sprintf(" %s=\"%s\" ",$key, $value); |
|
34
|
|
|
} |
|
35
|
|
|
return $attrString; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Accepts an associative array and produces an XML document |
|
40
|
|
|
* Attributes are supported by this function, see example. |
|
41
|
|
|
* |
|
42
|
|
|
* @since 0.0.1 |
|
43
|
|
|
* @param array $data Associative array, can be multi-dimensional |
|
44
|
|
|
* @return string The resulting XML document |
|
45
|
|
|
* |
|
46
|
|
|
* Example: (Not a valid USPS Request) |
|
47
|
|
|
* $test = array("RateRequestV4"=>array("@attr"=>array("ID"=>"This is my ID", "PASSWORD"=>"this is my pass") |
|
48
|
|
|
* "Package"=>array(array("Weight"=>1.0, "Service"=>"First Class"), |
|
49
|
|
|
* array("Weight"=>1.0, "Service"=>"PRIORITY")) |
|
50
|
|
|
* ) |
|
51
|
|
|
* ); |
|
52
|
|
|
* $xml_doc = $this->build_message($test); |
|
53
|
|
|
* -- Result -- |
|
54
|
|
|
* <RateRequestV4 ID="This is my ID" PASSWORD="this is my pass"> |
|
55
|
|
|
* <Package> |
|
56
|
|
|
* <Weight>1.0</Weight> |
|
57
|
|
|
* <Service>First Class</Service> |
|
58
|
|
|
* </Package> |
|
59
|
|
|
* <Package> |
|
60
|
|
|
* <Weight>1.0</Weight> |
|
61
|
|
|
* <Service>PRIORITY</Service> |
|
62
|
|
|
* </Package> |
|
63
|
|
|
* </RateRequestV4> |
|
64
|
|
|
*/ |
|
65
|
|
|
function build_message(array $data){ |
|
66
|
|
|
$xmlString = ""; |
|
67
|
|
|
foreach($data as $node=>$value){ |
|
68
|
|
|
if ($node == "@attr") { continue; } |
|
69
|
|
|
$value_is_list = $this->_is_list($value); |
|
70
|
|
|
if (is_array($value) && !$value_is_list){ |
|
71
|
|
|
$attrs = ""; |
|
72
|
|
|
if (array_key_exists("@attr",$value)){ |
|
73
|
|
|
$attrs = $this->_parse_attrs($value["@attr"]); |
|
74
|
|
|
unset($value["@attr"]); |
|
75
|
|
|
} |
|
76
|
|
|
$xmlString .= "<".$node." ".$attrs.">\n"; |
|
77
|
|
|
$xmlString .= $this->build_message($value); |
|
78
|
|
|
$xmlString .= "</".$node.">\n"; |
|
79
|
|
|
|
|
80
|
|
|
}elseif(is_array($value) && $value_is_list){ |
|
81
|
|
|
foreach($value as $iter_node){ |
|
82
|
|
|
$temp = array($node=>$iter_node); |
|
83
|
|
|
$xmlString .= $this->build_message($temp); |
|
84
|
|
|
} |
|
85
|
|
|
}else{ |
|
86
|
|
|
if (trim($value) != ""){ |
|
87
|
|
|
$xmlString .= "<".$node.">".$value."</".$node.">\n"; |
|
88
|
|
|
}else{ |
|
89
|
|
|
$xmlString .= "<".$node."/>\n"; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $xmlString; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sets the header content type to text/xml and displays a given XML doc |
|
98
|
|
|
* |
|
99
|
|
|
* @since 0.0.1 |
|
100
|
|
|
* @param string $xml_doc |
|
101
|
|
|
*/ |
|
102
|
|
|
function show_xml($xml_doc){ |
|
103
|
|
|
header("content-type: text/xml"); |
|
104
|
|
|
print $xml_doc; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* This is a helper function that retrieves an XML element from the |
|
109
|
|
|
* provided document. Since we are trying to keep PHP4 support |
|
110
|
|
|
* I cannot use simpleXML |
|
111
|
|
|
* |
|
112
|
|
|
* @since 0.0.1 |
|
113
|
|
|
* @param string $element The element to find in document |
|
114
|
|
|
* @param string $document The XML Document to search |
|
115
|
|
|
*/ |
|
116
|
|
|
function get($element, $document){ |
|
117
|
|
|
preg_match_all('/<'.$element.'.*?>(.*?)<\/'.$element.'>/', $document, $matches); |
|
118
|
|
|
|
|
119
|
|
|
if (count($matches) > 1){ |
|
120
|
|
|
return $matches[1]; |
|
121
|
|
|
} |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* |
|
129
|
|
|
* This is a helper class for ASH-based / enabled Shipping plugins. |
|
130
|
|
|
* Helpful centralized functions will be stored here for ease of use. |
|
131
|
|
|
* |
|
132
|
|
|
* @since 0.0.1 |
|
133
|
|
|
*/ |
|
134
|
|
|
class ASHTools { |
|
135
|
|
|
/** |
|
136
|
|
|
* Determines if the given zipcode is a US Military APO/AFO zip code |
|
137
|
|
|
* |
|
138
|
|
|
* @since 0.0.1 |
|
139
|
|
|
* @param int $zipcode |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
|
|
function is_military_zip($zipcode){ |
|
143
|
|
|
$zips = array("09302","09734","96201","96202","96203","96204","96205","96206","96206","96207", |
|
144
|
|
|
"96208","96212","96213","96214","96215","96217","96218","96219","96220","96221", |
|
145
|
|
|
"96224","96251","96257","96258","96259","96260","96262","96264","96266","96267", |
|
146
|
|
|
"96269","96271","96275","96276","96278","96283","96284","96297","96306","96309", |
|
147
|
|
|
"96309","96310","96311","96311","96313","96319","96321","96322","96323","96326", |
|
148
|
|
|
"96328","96330","96336","96337","96338","96339","96339","96343","96347","96348", |
|
149
|
|
|
"96349","96350","96351","96351","96362","96365","96367","96368","96370","96372", |
|
150
|
|
|
"96373","96374","96375","96376","96377","96378","96379","96384","96386","96387", |
|
151
|
|
|
"96388","96388","96401","96402","96403","96404","96424","96425","96426","96427", |
|
152
|
|
|
"96490","96507","96507","96511","96515","96515","96517","96517", |
|
153
|
|
|
"96518","96520","96520","96521","96522","96530","96531","96531","96534","96535", |
|
154
|
|
|
"96536","96537","96538","96540","96541","96542","96543","96544","96546","96548", |
|
155
|
|
|
"96549","96550","96551","96553","96554","96555","96557","96557","96595","96595", |
|
156
|
|
|
"96598","96599","96601","96602","96603","96604","96605","96606","96607","96608", |
|
157
|
|
|
"96609","96610","96611","96612","96613","96613","96614","96614","96615","96615", |
|
158
|
|
|
"96616","96616","96617","96617","96619","96619","96620","96620","96621","96622", |
|
159
|
|
|
"96623","96624","96628","96629","96634","96635","96643","96657","96657","96660", |
|
160
|
|
|
"96661","96662","96663","96664","96665","96666","96667","96668","96669","96670", |
|
161
|
|
|
"96671","96672","96673","96674","96675","96677","96678","96679","96681","96681", |
|
162
|
|
|
"96682","96683","96684","96684","96686","96687","96698"); |
|
163
|
|
|
|
|
164
|
|
|
return in_array( $zipcode, $zips ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Given an ISO country code, it will return the full country name |
|
169
|
|
|
* |
|
170
|
|
|
* @since 0.0.1 |
|
171
|
|
|
* @param string $short_country |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
function get_full_country( $short_country ){ |
|
175
|
|
|
$country = new WPSC_Country( $short_country ); |
|
176
|
|
|
return $country->get_name(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Given a WPEC state code (int), will return the state/region name |
|
181
|
|
|
* |
|
182
|
|
|
* @since 0.0.1 |
|
183
|
|
|
* @param int $state_code |
|
184
|
|
|
* |
|
185
|
|
|
* @return string Region for code. |
|
186
|
|
|
*/ |
|
187
|
|
|
function get_state( $state_code ) { |
|
188
|
|
|
$state_code = isset( $_POST['region'] ) ? $_POST['region'] : $state_code; |
|
189
|
|
|
return wpsc_get_region( $state_code ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Retrieves value for given key from $_POST or given session variable |
|
194
|
|
|
* You need to provide the session stub b/c it doenst know where you are looking |
|
195
|
|
|
* |
|
196
|
|
|
* @since 0.0.1 |
|
197
|
|
|
* @param mixed $key |
|
198
|
|
|
* @param array $session |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
|
|
function post_or_session($key, $session){ |
|
202
|
|
|
if (array_key_exists($key, $_POST)){ |
|
203
|
|
|
return $_POST[$key]; |
|
204
|
|
|
}elseif(array_key_exists($key, $session)){ |
|
205
|
|
|
return $session[$key]; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Retrieves the destination from session or post as an array |
|
211
|
|
|
* or "state","country", and "zipcode" |
|
212
|
|
|
* |
|
213
|
|
|
* @since 0.0.1 |
|
214
|
|
|
* @param array $session |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
|
function get_destination($session){ |
|
218
|
|
|
$address = array("state"=>$this->get_state($this->post_or_session("region",$session)), |
|
219
|
|
|
"country"=>$this->post_or_session("country",$session), |
|
220
|
|
|
"zipcode"=>$this->post_or_session("zipcode",$session) |
|
221
|
|
|
); |
|
222
|
|
|
return $address; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Checks if the destination country requires a postal code |
|
227
|
|
|
* |
|
228
|
|
|
* @since 3.8.14 |
|
229
|
|
|
* @param string $iso_code |
|
230
|
|
|
* @return bool |
|
231
|
|
|
*/ |
|
232
|
|
|
function needs_post_code( $iso_code ) { |
|
233
|
|
|
|
|
234
|
|
|
$no_post_code = array(); |
|
235
|
|
|
|
|
236
|
|
|
$no_post_code['AO'] = "Angola"; |
|
237
|
|
|
$no_post_code['AG'] = "Antigua and Barbuda"; |
|
238
|
|
|
$no_post_code['AW'] = "Aruba"; |
|
239
|
|
|
$no_post_code['BS'] = "Bahamas"; |
|
240
|
|
|
$no_post_code['BZ'] = "Belize"; |
|
241
|
|
|
$no_post_code['BJ'] = "Benin"; |
|
242
|
|
|
$no_post_code['BQ'] = "Bonaire, Sint Eustatius and Saba"; |
|
243
|
|
|
$no_post_code['BW'] = "Botswana"; |
|
244
|
|
|
$no_post_code['BF'] = "Burkina Faso"; |
|
245
|
|
|
$no_post_code['BI'] = "Burundi"; |
|
246
|
|
|
$no_post_code['CM'] = "Cameroon"; |
|
247
|
|
|
$no_post_code['CF'] = "Central African Republic"; |
|
248
|
|
|
$no_post_code['KM'] = "Comoros"; |
|
249
|
|
|
$no_post_code['CG'] = "Congo (Brazzaville)"; |
|
250
|
|
|
$no_post_code['CD'] = "Congo, Democratic Republic"; |
|
251
|
|
|
$no_post_code['CK'] = "Cook Islands"; |
|
252
|
|
|
$no_post_code['CI'] = "Côte d'Ivoire (Ivory Coast)"; |
|
253
|
|
|
$no_post_code['CW'] = "Curaçao"; |
|
254
|
|
|
$no_post_code['DJ'] = "Djibouti"; |
|
255
|
|
|
$no_post_code['DM'] = "Dominica"; |
|
256
|
|
|
$no_post_code['TL'] = "East Timor"; |
|
257
|
|
|
$no_post_code['GQ'] = "Equatorial Guinea"; |
|
258
|
|
|
$no_post_code['ER'] = "Eritrea"; |
|
259
|
|
|
$no_post_code['FJ'] = "Fiji"; |
|
260
|
|
|
$no_post_code['TF'] = "French Southern and Antarctic Territories"; |
|
261
|
|
|
$no_post_code['GM'] = "Gambia"; |
|
262
|
|
|
$no_post_code['GH'] = "Ghana"; |
|
263
|
|
|
$no_post_code['GD'] = "Grenada"; |
|
264
|
|
|
$no_post_code['GN'] = "Guinea"; |
|
265
|
|
|
$no_post_code['GY'] = "Guyana"; |
|
266
|
|
|
$no_post_code['HK'] = "Hong Kong"; |
|
267
|
|
|
$no_post_code['IE'] = "Ireland"; |
|
268
|
|
|
$no_post_code['JM'] = "Jamaica"; |
|
269
|
|
|
$no_post_code['KI'] = "Kiribati"; |
|
270
|
|
|
$no_post_code['KP'] = "Korea, North"; |
|
271
|
|
|
$no_post_code['MO'] = "Macau"; |
|
272
|
|
|
$no_post_code['MW'] = "Malawi"; |
|
273
|
|
|
$no_post_code['ML'] = "Mali"; |
|
274
|
|
|
$no_post_code['MR'] = "Mauritania"; |
|
275
|
|
|
$no_post_code['MU'] = "Mauritius"; |
|
276
|
|
|
$no_post_code['MS'] = "Montserrat"; |
|
277
|
|
|
$no_post_code['NR'] = "Nauru"; |
|
278
|
|
|
$no_post_code['NU'] = "Niue"; |
|
279
|
|
|
$no_post_code['QA'] = "Qatar"; |
|
280
|
|
|
$no_post_code['KN'] = "Saint Kitts and Nevis"; |
|
281
|
|
|
$no_post_code['LC'] = "Saint Lucia"; |
|
282
|
|
|
$no_post_code['ST'] = "Sao Tome and Principe"; |
|
283
|
|
|
$no_post_code['SC'] = "Seychelles"; |
|
284
|
|
|
$no_post_code['SX'] = "Sint Maarten"; |
|
285
|
|
|
$no_post_code['SL'] = "Sierra Leone"; |
|
286
|
|
|
$no_post_code['SB'] = "Solomon Islands"; |
|
287
|
|
|
$no_post_code['SO'] = "Somalia"; |
|
288
|
|
|
$no_post_code['SR'] = "Suriname"; |
|
289
|
|
|
$no_post_code['SY'] = "Syria"; |
|
290
|
|
|
$no_post_code['TZ'] = "Tanzania"; |
|
291
|
|
|
$no_post_code['TG'] = "Togo"; |
|
292
|
|
|
$no_post_code['TK'] = "Tokelau"; |
|
293
|
|
|
$no_post_code['TO'] = "Tonga"; |
|
294
|
|
|
$no_post_code['TV'] = "Tuvalu"; |
|
295
|
|
|
$no_post_code['UG'] = "Uganda"; |
|
296
|
|
|
$no_post_code['AE'] = "United Arab Emirates"; |
|
297
|
|
|
$no_post_code['VU'] = "Vanuatu"; |
|
298
|
|
|
$no_post_code['YE'] = "Yemen"; |
|
299
|
|
|
$no_post_code['ZW'] = "Zimbabwe"; |
|
300
|
|
|
|
|
301
|
|
|
return apply_filters( 'wpsc_ash_tools_needs_post_code', ( ! isset( $no_post_code[ $iso_code ] ) ), $no_post_code, $iso_code ); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Object representation of a package from a shipment. |
|
307
|
|
|
* This is the fundamental element of a shipment. |
|
308
|
|
|
* |
|
309
|
|
|
* @since 0.0.1 |
|
310
|
|
|
*/ |
|
311
|
|
|
class ASHPackage { |
|
312
|
|
|
/** |
|
313
|
|
|
* Product ids included in package |
|
314
|
|
|
* @var array |
|
315
|
|
|
*/ |
|
316
|
|
|
var $product_id = array(); |
|
317
|
|
|
/** |
|
318
|
|
|
* Weight in pounds of the package |
|
319
|
|
|
* @var decimal |
|
320
|
|
|
*/ |
|
321
|
|
|
var $weight; |
|
322
|
|
|
/** |
|
323
|
|
|
* The height in inches of the package |
|
324
|
|
|
* @var decimal |
|
325
|
|
|
*/ |
|
326
|
|
|
var $height; |
|
327
|
|
|
/** |
|
328
|
|
|
* The length (longest part) of the package in inches |
|
329
|
|
|
* @var decimal |
|
330
|
|
|
*/ |
|
331
|
|
|
var $length; |
|
332
|
|
|
/** |
|
333
|
|
|
* The width of the package in inches |
|
334
|
|
|
* @var decimal |
|
335
|
|
|
*/ |
|
336
|
|
|
var $width; |
|
337
|
|
|
/** |
|
338
|
|
|
* Girth is defined, for a rectangle as G=2(Height+Width) |
|
339
|
|
|
* is auto calc'ed when you use set_dimensions |
|
340
|
|
|
* @var float |
|
341
|
|
|
*/ |
|
342
|
|
|
var $girth; |
|
343
|
|
|
/** |
|
344
|
|
|
* Whatever you want to describe what is in the package |
|
345
|
|
|
* @var string |
|
346
|
|
|
*/ |
|
347
|
|
|
var $contents; |
|
348
|
|
|
/** |
|
349
|
|
|
* The value/price of the item/package |
|
350
|
|
|
* @var decimal |
|
351
|
|
|
*/ |
|
352
|
|
|
var $value; |
|
353
|
|
|
/** |
|
354
|
|
|
* Flag denotes if the package has hazardous material or not |
|
355
|
|
|
* @var boolean |
|
356
|
|
|
*/ |
|
357
|
|
|
var $hazard = false; |
|
358
|
|
|
/** |
|
359
|
|
|
* Flag denotes if the package is to have insurance added to the quote |
|
360
|
|
|
* @var boolean |
|
361
|
|
|
*/ |
|
362
|
|
|
var $insurance = false; |
|
363
|
|
|
/** |
|
364
|
|
|
* The amount that the package is to be insured for |
|
365
|
|
|
* @var decimal |
|
366
|
|
|
*/ |
|
367
|
|
|
var $insured_amount; |
|
368
|
|
|
/** |
|
369
|
|
|
* The package can't be shipped sideways. |
|
370
|
|
|
* var boolean |
|
371
|
|
|
*/ |
|
372
|
|
|
var $this_side_up = false; |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* The constructor for the ASHPackage class |
|
376
|
|
|
* Accepts an arguments array to fill out the class on initialization |
|
377
|
|
|
* |
|
378
|
|
|
* @since 3.11.3 |
|
379
|
|
|
* @param array $args |
|
380
|
|
|
*/ |
|
381
|
|
|
public function __construct(array $args = array()){ |
|
382
|
|
|
foreach($args as $key=>$value){ |
|
383
|
|
|
$this->$key=$value; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
/** |
|
387
|
|
|
* This is a "magic function" that will be used when I can convert to PHP5 |
|
388
|
|
|
* When a property / function is set to private, this controls access |
|
389
|
|
|
* to outside functions |
|
390
|
|
|
* |
|
391
|
|
|
* @since 0.0.1 |
|
392
|
|
|
* @param string $item |
|
393
|
|
|
* @return mixed |
|
394
|
|
|
*/ |
|
395
|
|
|
function __get($item){ |
|
396
|
|
|
return $this->$item; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* This is a "magic function" that sets a property that has as protected scope |
|
401
|
|
|
* only for php5 |
|
402
|
|
|
* |
|
403
|
|
|
* @since 0.0.1 |
|
404
|
|
|
* @param string $item |
|
405
|
|
|
* @param mixed $value |
|
406
|
|
|
*/ |
|
407
|
|
|
function __set($item, $value){ |
|
408
|
|
|
$this->$item = $value; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
/** |
|
412
|
|
|
* This is a magic function that controls how the string representation of |
|
413
|
|
|
* the class looks / behaves. |
|
414
|
|
|
* |
|
415
|
|
|
* @since 0.0.1 |
|
416
|
|
|
*/ |
|
417
|
|
|
function __toString(){ |
|
418
|
|
|
// Nothing here yet |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Sets the dimensions for the package given an array |
|
423
|
|
|
* array values should be "Height", "Length", "Width" and weight |
|
424
|
|
|
* girth is automatically calculated |
|
425
|
|
|
* |
|
426
|
|
|
* @since 0.0.1 |
|
427
|
|
|
* @param array $dimensions |
|
428
|
|
|
*/ |
|
429
|
|
|
function set_dimensions(array $dimensions){ |
|
430
|
|
|
foreach($dimensions as $key=>$value){ |
|
431
|
|
|
$this->$key = $value; |
|
432
|
|
|
} |
|
433
|
|
|
$this->girth = 2*($this->width+$this->height); |
|
|
|
|
|
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Object representation of a shipment of packages based on |
|
440
|
|
|
* the contents of a shopping cart |
|
441
|
|
|
* |
|
442
|
|
|
* @since 0.0.1 |
|
443
|
|
|
*/ |
|
444
|
|
|
class ASHShipment { |
|
445
|
|
|
/** |
|
446
|
|
|
* An array of ASHPackage objects |
|
447
|
|
|
* @var array |
|
448
|
|
|
*/ |
|
449
|
|
|
var $packages=array(); |
|
450
|
|
|
/** |
|
451
|
|
|
* Flag denotes if there are any hazardous packages in the shipment overall |
|
452
|
|
|
* @var boolean |
|
453
|
|
|
*/ |
|
454
|
|
|
var $hazard = false; |
|
455
|
|
|
/** |
|
456
|
|
|
* The amount of packages in the shipment, automatically increments when |
|
457
|
|
|
* you use the add_package() function |
|
458
|
|
|
* @var int |
|
459
|
|
|
*/ |
|
460
|
|
|
var $package_count=0; |
|
461
|
|
|
/** |
|
462
|
|
|
* An array that represents the destination, (State,Zipode,Country) |
|
463
|
|
|
* @var array |
|
464
|
|
|
*/ |
|
465
|
|
|
var $destination = array(); |
|
466
|
|
|
/** |
|
467
|
|
|
* The overall value of the contents of the shipment (all packages value summed together) |
|
468
|
|
|
* Automatically calculated when you use add_package() |
|
469
|
|
|
* @var decimal |
|
470
|
|
|
*/ |
|
471
|
|
|
var $shipment_value = 0; |
|
472
|
|
|
/** |
|
473
|
|
|
* The overal weight of the contents of the shipment (all packages weight summed together) |
|
474
|
|
|
* Automaticaly calculated when you use add_package() |
|
475
|
|
|
* @var unknown_type |
|
476
|
|
|
*/ |
|
477
|
|
|
var $total_weight = 0; |
|
478
|
|
|
/** |
|
479
|
|
|
* Sets a rate expire date |
|
480
|
|
|
* @var string |
|
481
|
|
|
*/ |
|
482
|
|
|
var $rates_expire = ''; |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Constructor for the ASHShipment class |
|
486
|
|
|
* |
|
487
|
|
|
* @since 3.11.3 |
|
488
|
|
|
*/ |
|
489
|
|
|
public function __construct(){ |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* Sets the destination array using either post, session or provided array |
|
494
|
|
|
* @param string $internal_name internal name of shipping module |
|
495
|
|
|
* @param array $dest optional array if you already know destination. |
|
|
|
|
|
|
496
|
|
|
*/ |
|
497
|
|
|
function set_destination( $internal_name, $dest = false ){ |
|
498
|
|
|
if (!$dest){ |
|
499
|
|
|
$tools = new ASHTools(); |
|
500
|
|
|
$wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); |
|
501
|
|
|
if ( ! $wpec_ash ) |
|
502
|
|
|
$wpec_ash = array(); |
|
503
|
|
|
|
|
504
|
|
|
$session_destination = ( array_key_exists( $internal_name, $wpec_ash ) ? $wpec_ash[$internal_name]["shipment"]["destination"] : array() ); |
|
505
|
|
|
$this->destination = $tools->get_destination($session_destination); |
|
506
|
|
|
}else{ |
|
507
|
|
|
$this->destination = $dest; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
/** |
|
513
|
|
|
* This is a magic function that controls access to protected items |
|
514
|
|
|
* and allows you to retrieve their values (php5) |
|
515
|
|
|
* @param string $item |
|
516
|
|
|
* @return mixed |
|
517
|
|
|
*/ |
|
518
|
|
|
function __get($item){ |
|
519
|
|
|
return $this->$item; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* This function sets the hazard flag on the class |
|
524
|
|
|
* while it seems inane, i am making sure that the values |
|
525
|
|
|
* are truly boolean true or false |
|
526
|
|
|
* @param boolean $flag |
|
527
|
|
|
*/ |
|
528
|
|
|
function set_hazard($flag){ |
|
529
|
|
|
if ($flag === true){ |
|
530
|
|
|
$this->hazard = true; |
|
531
|
|
|
}else{ |
|
532
|
|
|
$this->hazard = false; |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* Use this function to add a package object to the shipment. |
|
538
|
|
|
* it expects an object of class ASHPackage or throws an exception |
|
539
|
|
|
* |
|
540
|
|
|
* @since 0.0.1 |
|
541
|
|
|
* @param ASHPackage $package |
|
542
|
|
|
* @throws ErrorException |
|
543
|
|
|
*/ |
|
544
|
|
|
function add_package($package){ |
|
545
|
|
|
if ($package instanceof ASHPackage){ |
|
546
|
|
|
array_push($this->packages, $package); |
|
547
|
|
|
$this->package_count++; |
|
548
|
|
|
$this->total_weight += $package->weight; |
|
549
|
|
|
$this->shipment_value += $package->value; |
|
550
|
|
|
}else{ |
|
551
|
|
|
$type = gettype($package); |
|
552
|
|
|
throw new ErrorException("ASHSHipment expected object of class ASHPackage, got instance of {$type} instead"); |
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
/** |
|
559
|
|
|
* This is the heart of the Advanced Shipping Helper for WPEC |
|
560
|
|
|
* It is the entrypoint for interaction between ASH and WPEC |
|
561
|
|
|
* |
|
562
|
|
|
*/ |
|
563
|
|
|
class ASH { |
|
564
|
|
|
/** |
|
565
|
|
|
* General constructor for ASH class |
|
566
|
|
|
* |
|
567
|
|
|
*/ |
|
568
|
|
|
public function __construct(){ |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
/** |
|
572
|
|
|
* Builds a shipment object representing the cart contents from WPEC |
|
573
|
|
|
* |
|
574
|
|
|
* @return ASHShipment |
|
575
|
|
|
*/ |
|
576
|
|
|
function get_shipment(){ |
|
577
|
|
|
global $wpdb, $wpsc_cart; |
|
578
|
|
|
|
|
579
|
|
|
$shipment = new ASHShipment(); |
|
580
|
|
|
if (!$wpsc_cart){ |
|
581
|
|
|
return $shipment; |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
foreach($wpsc_cart->cart_items as $cart_item){ |
|
585
|
|
|
$package = new ASHPackage(); |
|
586
|
|
|
//*** Set package dimensions ***\\ |
|
587
|
|
|
$dimensions = get_product_meta($cart_item->product_id, 'product_metadata'); //The 'dimensions' meta doesn't exist. |
|
588
|
|
|
if ( isset( $dimensions[0]['dimensions'] ) ) { |
|
589
|
|
|
$dimensions = $dimensions[0]['dimensions']; |
|
590
|
|
|
} |
|
591
|
|
|
$dim_array = array(); |
|
592
|
|
|
$dim_array["weight"] = $cart_item->weight; |
|
593
|
|
|
$dim_array["height"] = ( !empty( $dimensions["height"] ) && is_numeric( $dimensions["height"] ) ) ? $dimensions["height"] : 1; |
|
594
|
|
|
$dim_array["width"] = ( !empty( $dimensions["width"] ) && is_numeric( $dimensions["width"] ) ) ? $dimensions["width"] : 1; |
|
595
|
|
|
$dim_array["length"] = ( !empty( $dimensions["length"] ) && is_numeric( $dimensions["length"] ) ) ? $dimensions["length"] : 1; |
|
596
|
|
|
$package->set_dimensions($dim_array); |
|
597
|
|
|
|
|
598
|
|
|
/* Set other meta */ |
|
599
|
|
|
$package->hazard = ( get_product_meta( $cart_item->product_id, "ship_hazard", true ) === true) ? true : false; //Fixed ternary evaluation. |
|
600
|
|
|
$package->insurance = ( get_product_meta( $cart_item->product_id, "ship_insurance", true ) === true) ? true : false; //Fixed ternary evaluation. |
|
601
|
|
|
$package->insured_amount = get_product_meta( $cart_item->product_id,"ship_insured_amount", true ); //Fixed ternary evaluation. |
|
602
|
|
|
$package->value = $cart_item->unit_price; |
|
603
|
|
|
$package->contents = $cart_item->product_name; |
|
604
|
|
|
$package->this_side_up = ( get_post_meta( $cart_item->product_id, "h:this_side_up", true ) === true ) ? true : false; //Prod. page hide, prod. UI display |
|
605
|
|
|
if ($shipment->hazard === false and $package->hazard === true){ |
|
606
|
|
|
$shipment->set_hazard(true); |
|
607
|
|
|
} |
|
608
|
|
|
$quantity = (int)$cart_item->quantity; |
|
609
|
|
|
$package->product_id[$cart_item->product_id] = 1; // The product in this package. |
|
610
|
|
|
for($i=1; $i <= $quantity; $i++){ |
|
611
|
|
|
$shipment->add_package($package); |
|
612
|
|
|
} |
|
613
|
|
|
} |
|
614
|
|
|
return $shipment; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
/** |
|
618
|
|
|
* Caches a result table for the given shipping module |
|
619
|
|
|
* |
|
620
|
|
|
* @param string $internal_name |
|
621
|
|
|
* @param array $rate_table |
|
622
|
|
|
* @param ASHShipment $shipment |
|
623
|
|
|
*/ |
|
624
|
|
|
function cache_results($internal_name, $rate_table, $shipment){ |
|
625
|
|
|
$wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); |
|
626
|
|
|
if ( ! is_array( $wpec_ash ) ) |
|
627
|
|
|
$wpec_ash = array(); |
|
628
|
|
|
|
|
629
|
|
|
if ( empty( $wpec_ash[$internal_name] ) || ! is_array( $wpec_ash[$internal_name] ) ) { |
|
630
|
|
|
$wpec_ash[$internal_name] = array(); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
$wpec_ash[$internal_name]["rate_table"] = $rate_table; |
|
634
|
|
|
$shipment_vals = array("package_count"=>$shipment->package_count, |
|
635
|
|
|
"destination" =>$shipment->destination, |
|
636
|
|
|
"total_weight" =>$shipment->total_weight, |
|
637
|
|
|
"rates_expire" =>$shipment->rates_expire ); //Refresh rates after today. |
|
638
|
|
|
$wpec_ash[$internal_name]["shipment"] = $shipment_vals; |
|
639
|
|
|
wpsc_update_customer_meta( 'shipping_ash', $wpec_ash ); |
|
640
|
|
|
} |
|
641
|
|
|
/** |
|
642
|
|
|
* Checks cached results for given shipping module and returns |
|
643
|
|
|
* the cached rates if nothing has changed. |
|
644
|
|
|
* |
|
645
|
|
|
* @param string $internal_name |
|
646
|
|
|
* @param ASHShipment $shipment |
|
647
|
|
|
*/ |
|
648
|
|
|
function check_cache($internal_name, $shipment){ |
|
649
|
|
|
$wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); |
|
650
|
|
|
|
|
651
|
|
|
if ( ! $wpec_ash || ! is_array( $wpec_ash ) ) { //Avoids: Warning: 'array_key_exists' expects array. |
|
652
|
|
|
return false; |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
if ( ! array_key_exists( $internal_name, $wpec_ash ) ) { |
|
656
|
|
|
return false; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
|
|
$cached_shipment = array(); |
|
660
|
|
|
|
|
661
|
|
|
if ( is_object( $wpec_ash[$internal_name]["shipment"] ) ){ |
|
662
|
|
|
$cached_shipment = $wpec_ash[$internal_name]["shipment"]; |
|
663
|
|
|
} else { |
|
664
|
|
|
if ( ! empty( $wpec_ash[$internal_name]["shipment"] ) ){ |
|
665
|
|
|
if ( is_array( $wpec_ash[$internal_name]["shipment"] ) ){ |
|
666
|
|
|
$cached_shipment = $wpec_ash[$internal_name]["shipment"]; |
|
667
|
|
|
} |
|
668
|
|
|
} |
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
$shipment_vals = array("package_count"=>$shipment->package_count, |
|
672
|
|
|
"destination" =>$shipment->destination, |
|
673
|
|
|
"total_weight" =>$shipment->total_weight, |
|
674
|
|
|
"rates_expire" =>$shipment->rates_expire ); //Refresh rates after today. |
|
675
|
|
|
if ($cached_shipment["package_count"] != $shipment->package_count){ |
|
676
|
|
|
return false; |
|
677
|
|
|
}elseif($cached_shipment["destination"] != $shipment_vals["destination"]){ |
|
678
|
|
|
return false; |
|
679
|
|
|
}elseif($cached_shipment["total_weight"] != $shipment_vals["total_weight"]){ |
|
680
|
|
|
return false; |
|
681
|
|
|
}elseif($cached_shipment["rates_expire"] != $shipment_vals["rates_expire"]) { //Refresh rates after today. |
|
682
|
|
|
return false; |
|
683
|
|
|
}else{ |
|
684
|
|
|
return $wpec_ash[$internal_name]; |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
} |
|
690
|
|
|
global $wpec_ash; |
|
691
|
|
|
$wpec_ash = new ASH(); |
|
692
|
|
|
global $wpec_ash_xml; |
|
693
|
|
|
$wpec_ash_xml = new ASHXML(); |
|
694
|
|
|
global $wpec_ash_tools; |
|
695
|
|
|
$wpec_ash_tools = new ASHTools(); |
|
696
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.