|
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
|
|
|
* @return string|int will be int if wordpress database & wpec are not available |
|
|
|
|
|
|
185
|
|
|
*/ |
|
186
|
|
|
function get_state( $state_code ){ |
|
187
|
|
|
$state_code = isset( $_POST['region'] ) ? $_POST['region'] : $state_code; |
|
188
|
|
|
return wpsc_get_region( $state_code ); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Retrieves value for given key from $_POST or given session variable |
|
193
|
|
|
* You need to provide the session stub b/c it doenst know where you are looking |
|
194
|
|
|
* |
|
195
|
|
|
* @since 0.0.1 |
|
196
|
|
|
* @param mixed $key |
|
197
|
|
|
* @param array $session |
|
198
|
|
|
* @return mixed |
|
199
|
|
|
*/ |
|
200
|
|
|
function post_or_session($key, $session){ |
|
201
|
|
|
if (array_key_exists($key, $_POST)){ |
|
202
|
|
|
return $_POST[$key]; |
|
203
|
|
|
}elseif(array_key_exists($key, $session)){ |
|
204
|
|
|
return $session[$key]; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Retrieves the destination from session or post as an array |
|
210
|
|
|
* or "state","country", and "zipcode" |
|
211
|
|
|
* |
|
212
|
|
|
* @since 0.0.1 |
|
213
|
|
|
* @param array $session |
|
214
|
|
|
* @return array |
|
215
|
|
|
*/ |
|
216
|
|
|
function get_destination($session){ |
|
217
|
|
|
$address = array("state"=>$this->get_state($this->post_or_session("region",$session)), |
|
218
|
|
|
"country"=>$this->post_or_session("country",$session), |
|
219
|
|
|
"zipcode"=>$this->post_or_session("zipcode",$session) |
|
220
|
|
|
); |
|
221
|
|
|
return $address; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Checks if the destination country requires a postal code |
|
226
|
|
|
* |
|
227
|
|
|
* @since 3.8.14 |
|
228
|
|
|
* @param string $iso_code |
|
229
|
|
|
* @return bool |
|
230
|
|
|
*/ |
|
231
|
|
|
function needs_post_code( $iso_code ) { |
|
232
|
|
|
|
|
233
|
|
|
$no_post_code = array(); |
|
234
|
|
|
|
|
235
|
|
|
$no_post_code['AO'] = "Angola"; |
|
236
|
|
|
$no_post_code['AG'] = "Antigua and Barbuda"; |
|
237
|
|
|
$no_post_code['AW'] = "Aruba"; |
|
238
|
|
|
$no_post_code['BS'] = "Bahamas"; |
|
239
|
|
|
$no_post_code['BZ'] = "Belize"; |
|
240
|
|
|
$no_post_code['BJ'] = "Benin"; |
|
241
|
|
|
$no_post_code['BQ'] = "Bonaire, Sint Eustatius and Saba"; |
|
242
|
|
|
$no_post_code['BW'] = "Botswana"; |
|
243
|
|
|
$no_post_code['BF'] = "Burkina Faso"; |
|
244
|
|
|
$no_post_code['BI'] = "Burundi"; |
|
245
|
|
|
$no_post_code['CM'] = "Cameroon"; |
|
246
|
|
|
$no_post_code['CF'] = "Central African Republic"; |
|
247
|
|
|
$no_post_code['KM'] = "Comoros"; |
|
248
|
|
|
$no_post_code['CG'] = "Congo (Brazzaville)"; |
|
249
|
|
|
$no_post_code['CD'] = "Congo, Democratic Republic"; |
|
250
|
|
|
$no_post_code['CK'] = "Cook Islands"; |
|
251
|
|
|
$no_post_code['CI'] = "Côte d'Ivoire (Ivory Coast)"; |
|
252
|
|
|
$no_post_code['CW'] = "Curaçao"; |
|
253
|
|
|
$no_post_code['DJ'] = "Djibouti"; |
|
254
|
|
|
$no_post_code['DM'] = "Dominica"; |
|
255
|
|
|
$no_post_code['TL'] = "East Timor"; |
|
256
|
|
|
$no_post_code['GQ'] = "Equatorial Guinea"; |
|
257
|
|
|
$no_post_code['ER'] = "Eritrea"; |
|
258
|
|
|
$no_post_code['FJ'] = "Fiji"; |
|
259
|
|
|
$no_post_code['TF'] = "French Southern and Antarctic Territories"; |
|
260
|
|
|
$no_post_code['GM'] = "Gambia"; |
|
261
|
|
|
$no_post_code['GH'] = "Ghana"; |
|
262
|
|
|
$no_post_code['GD'] = "Grenada"; |
|
263
|
|
|
$no_post_code['GN'] = "Guinea"; |
|
264
|
|
|
$no_post_code['GY'] = "Guyana"; |
|
265
|
|
|
$no_post_code['HK'] = "Hong Kong"; |
|
266
|
|
|
$no_post_code['IE'] = "Ireland"; |
|
267
|
|
|
$no_post_code['JM'] = "Jamaica"; |
|
268
|
|
|
$no_post_code['KI'] = "Kiribati"; |
|
269
|
|
|
$no_post_code['KP'] = "Korea, North"; |
|
270
|
|
|
$no_post_code['MO'] = "Macau"; |
|
271
|
|
|
$no_post_code['MW'] = "Malawi"; |
|
272
|
|
|
$no_post_code['ML'] = "Mali"; |
|
273
|
|
|
$no_post_code['MR'] = "Mauritania"; |
|
274
|
|
|
$no_post_code['MU'] = "Mauritius"; |
|
275
|
|
|
$no_post_code['MS'] = "Montserrat"; |
|
276
|
|
|
$no_post_code['NR'] = "Nauru"; |
|
277
|
|
|
$no_post_code['NU'] = "Niue"; |
|
278
|
|
|
$no_post_code['QA'] = "Qatar"; |
|
279
|
|
|
$no_post_code['KN'] = "Saint Kitts and Nevis"; |
|
280
|
|
|
$no_post_code['LC'] = "Saint Lucia"; |
|
281
|
|
|
$no_post_code['ST'] = "Sao Tome and Principe"; |
|
282
|
|
|
$no_post_code['SC'] = "Seychelles"; |
|
283
|
|
|
$no_post_code['SX'] = "Sint Maarten"; |
|
284
|
|
|
$no_post_code['SL'] = "Sierra Leone"; |
|
285
|
|
|
$no_post_code['SB'] = "Solomon Islands"; |
|
286
|
|
|
$no_post_code['SO'] = "Somalia"; |
|
287
|
|
|
$no_post_code['SR'] = "Suriname"; |
|
288
|
|
|
$no_post_code['SY'] = "Syria"; |
|
289
|
|
|
$no_post_code['TZ'] = "Tanzania"; |
|
290
|
|
|
$no_post_code['TG'] = "Togo"; |
|
291
|
|
|
$no_post_code['TK'] = "Tokelau"; |
|
292
|
|
|
$no_post_code['TO'] = "Tonga"; |
|
293
|
|
|
$no_post_code['TV'] = "Tuvalu"; |
|
294
|
|
|
$no_post_code['UG'] = "Uganda"; |
|
295
|
|
|
$no_post_code['AE'] = "United Arab Emirates"; |
|
296
|
|
|
$no_post_code['VU'] = "Vanuatu"; |
|
297
|
|
|
$no_post_code['YE'] = "Yemen"; |
|
298
|
|
|
$no_post_code['ZW'] = "Zimbabwe"; |
|
299
|
|
|
|
|
300
|
|
|
return apply_filters( 'wpsc_ash_tools_needs_post_code', ( ! isset( $no_post_code[ $iso_code ] ) ), $no_post_code, $iso_code ); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Object representation of a package from a shipment. |
|
306
|
|
|
* This is the fundamental element of a shipment. |
|
307
|
|
|
* |
|
308
|
|
|
* @since 0.0.1 |
|
309
|
|
|
*/ |
|
310
|
|
|
class ASHPackage { |
|
311
|
|
|
/** |
|
312
|
|
|
* Product ids included in package |
|
313
|
|
|
* @var array |
|
314
|
|
|
*/ |
|
315
|
|
|
var $product_id = array(); |
|
316
|
|
|
/** |
|
317
|
|
|
* Weight in pounds of the package |
|
318
|
|
|
* @var decimal |
|
319
|
|
|
*/ |
|
320
|
|
|
var $weight; |
|
321
|
|
|
/** |
|
322
|
|
|
* The height in inches of the package |
|
323
|
|
|
* @var decimal |
|
324
|
|
|
*/ |
|
325
|
|
|
var $height; |
|
326
|
|
|
/** |
|
327
|
|
|
* The length (longest part) of the package in inches |
|
328
|
|
|
* @var decimal |
|
329
|
|
|
*/ |
|
330
|
|
|
var $length; |
|
331
|
|
|
/** |
|
332
|
|
|
* The width of the package in inches |
|
333
|
|
|
* @var decimal |
|
334
|
|
|
*/ |
|
335
|
|
|
var $width; |
|
336
|
|
|
/** |
|
337
|
|
|
* Girth is defined, for a rectangle as G=2(Height+Width) |
|
338
|
|
|
* is auto calc'ed when you use set_dimensions |
|
339
|
|
|
* @var decimal |
|
340
|
|
|
*/ |
|
341
|
|
|
var $girth; |
|
342
|
|
|
/** |
|
343
|
|
|
* Whatever you want to describe what is in the package |
|
344
|
|
|
* @var string |
|
345
|
|
|
*/ |
|
346
|
|
|
var $contents; |
|
347
|
|
|
/** |
|
348
|
|
|
* The value/price of the item/package |
|
349
|
|
|
* @var decimal |
|
350
|
|
|
*/ |
|
351
|
|
|
var $value; |
|
352
|
|
|
/** |
|
353
|
|
|
* Flag denotes if the package has hazardous material or not |
|
354
|
|
|
* @var boolean |
|
355
|
|
|
*/ |
|
356
|
|
|
var $hazard = FALSE; |
|
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Flag denotes if the package is to have insurance added to the quote |
|
359
|
|
|
* @var boolean |
|
360
|
|
|
*/ |
|
361
|
|
|
var $insurance = FALSE; |
|
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* The amount that the package is to be insured for |
|
364
|
|
|
* @var decimal |
|
365
|
|
|
*/ |
|
366
|
|
|
var $insured_amount; |
|
367
|
|
|
/** |
|
368
|
|
|
* The package can't be shipped sideways. |
|
369
|
|
|
* var boolean |
|
370
|
|
|
*/ |
|
371
|
|
|
var $this_side_up = FALSE; |
|
|
|
|
|
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* The constructor for the ASHPackage class |
|
375
|
|
|
* Accepts an arguments array to fill out the class on initialization |
|
376
|
|
|
* |
|
377
|
|
|
* @since 3.11.3 |
|
378
|
|
|
* @param array $args |
|
379
|
|
|
*/ |
|
380
|
|
|
public function __construct(array $args = array()){ |
|
381
|
|
|
foreach($args as $key=>$value){ |
|
382
|
|
|
$this->$key=$value; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
/** |
|
386
|
|
|
* This is a "magic function" that will be used when I can convert to PHP5 |
|
387
|
|
|
* When a property / function is set to private, this controls access |
|
388
|
|
|
* to outside functions |
|
389
|
|
|
* |
|
390
|
|
|
* @since 0.0.1 |
|
391
|
|
|
* @param string $item |
|
392
|
|
|
* @return mixed |
|
393
|
|
|
*/ |
|
394
|
|
|
function __get($item){ |
|
395
|
|
|
return $this->$item; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* This is a "magic function" that sets a property that has as protected scope |
|
400
|
|
|
* only for php5 |
|
401
|
|
|
* |
|
402
|
|
|
* @since 0.0.1 |
|
403
|
|
|
* @param string $item |
|
404
|
|
|
* @param mixed $value |
|
405
|
|
|
*/ |
|
406
|
|
|
function __set($item, $value){ |
|
407
|
|
|
$this->$item = $value; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* This is a magic function that controls how the string representation of |
|
412
|
|
|
* the class looks / behaves. |
|
413
|
|
|
* |
|
414
|
|
|
* @since 0.0.1 |
|
415
|
|
|
*/ |
|
416
|
|
|
function __toString(){ |
|
417
|
|
|
// Nothing here yet |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** |
|
421
|
|
|
* Sets the dimensions for the package given an array |
|
422
|
|
|
* array values should be "Height", "Length", "Width" and weight |
|
423
|
|
|
* girth is automatically calculated |
|
424
|
|
|
* |
|
425
|
|
|
* @since 0.0.1 |
|
426
|
|
|
* @param array $dimensions |
|
427
|
|
|
*/ |
|
428
|
|
|
function set_dimensions(array $dimensions){ |
|
429
|
|
|
foreach($dimensions as $key=>$value){ |
|
430
|
|
|
$this->$key = $value; |
|
431
|
|
|
} |
|
432
|
|
|
$this->girth = 2*($this->width+$this->height); |
|
|
|
|
|
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Object representation of a shipment of packages based on |
|
439
|
|
|
* the contents of a shopping cart |
|
440
|
|
|
* |
|
441
|
|
|
* @since 0.0.1 |
|
442
|
|
|
*/ |
|
443
|
|
|
class ASHShipment{ |
|
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* An array of ASHPackage objects |
|
446
|
|
|
* @var array |
|
447
|
|
|
*/ |
|
448
|
|
|
var $packages=array(); |
|
449
|
|
|
/** |
|
450
|
|
|
* Flag denotes if there are any hazardous packages in the shipment overall |
|
451
|
|
|
* @var boolean |
|
452
|
|
|
*/ |
|
453
|
|
|
var $hazard = FALSE; |
|
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* The amount of packages in the shipment, automatically increments when |
|
456
|
|
|
* you use the add_package() function |
|
457
|
|
|
* @var int |
|
458
|
|
|
*/ |
|
459
|
|
|
var $package_count=0; |
|
460
|
|
|
/** |
|
461
|
|
|
* An array that represents the destination, (State,Zipode,Country) |
|
462
|
|
|
* @var array |
|
463
|
|
|
*/ |
|
464
|
|
|
var $destination = array(); |
|
465
|
|
|
/** |
|
466
|
|
|
* The overall value of the contents of the shipment (all packages value summed together) |
|
467
|
|
|
* Automatically calculated when you use add_package() |
|
468
|
|
|
* @var decimal |
|
469
|
|
|
*/ |
|
470
|
|
|
var $shipment_value = 0; |
|
471
|
|
|
/** |
|
472
|
|
|
* The overal weight of the contents of the shipment (all packages weight summed together) |
|
473
|
|
|
* Automaticaly calculated when you use add_package() |
|
474
|
|
|
* @var unknown_type |
|
475
|
|
|
*/ |
|
476
|
|
|
var $total_weight = 0; |
|
477
|
|
|
/** |
|
478
|
|
|
* Sets a rate expire date |
|
479
|
|
|
* @var string |
|
480
|
|
|
*/ |
|
481
|
|
|
var $rates_expire = ''; |
|
482
|
|
|
|
|
483
|
|
|
/** |
|
484
|
|
|
* Constructor for the ASHShipment class |
|
485
|
|
|
* |
|
486
|
|
|
* @since 3.11.3 |
|
487
|
|
|
*/ |
|
488
|
|
|
public function __construct(){ |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* Sets the destination array using either post, session or provided array |
|
493
|
|
|
* @param string $internal_name internal name of shipping module |
|
494
|
|
|
* @param array $dest optional array if you already know destination. |
|
|
|
|
|
|
495
|
|
|
*/ |
|
496
|
|
|
function set_destination($internal_name, $dest=FALSE){ |
|
|
|
|
|
|
497
|
|
|
if (!$dest){ |
|
498
|
|
|
$tools = new ASHTools(); |
|
499
|
|
|
$wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); |
|
500
|
|
|
if ( ! $wpec_ash ) |
|
501
|
|
|
$wpec_ash = array(); |
|
502
|
|
|
|
|
503
|
|
|
$session_destination = ( array_key_exists( $internal_name, $wpec_ash ) ? $wpec_ash[$internal_name]["shipment"]["destination"] : array() ); |
|
504
|
|
|
$this->destination = $tools->get_destination($session_destination); |
|
505
|
|
|
}else{ |
|
506
|
|
|
$this->destination = $dest; |
|
507
|
|
|
} |
|
508
|
|
|
|
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
/** |
|
512
|
|
|
* This is a magic function that controls access to protected items |
|
513
|
|
|
* and allows you to retrieve their values (php5) |
|
514
|
|
|
* @param string $item |
|
515
|
|
|
* @return mixed |
|
516
|
|
|
*/ |
|
517
|
|
|
function __get($item){ |
|
518
|
|
|
return $this->$item; |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* This function sets the hazard flag on the class |
|
523
|
|
|
* while it seems inane, i am making sure that the values |
|
524
|
|
|
* are truly boolean true or false |
|
525
|
|
|
* @param boolean $flag |
|
526
|
|
|
*/ |
|
527
|
|
|
function set_hazard($flag){ |
|
528
|
|
|
if ($flag == TRUE){ |
|
|
|
|
|
|
529
|
|
|
$this->hazard = TRUE; |
|
|
|
|
|
|
530
|
|
|
}else{ |
|
531
|
|
|
$this->hazard = FALSE; |
|
|
|
|
|
|
532
|
|
|
} |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* Use this function to add a package object to the shipment. |
|
537
|
|
|
* it expects an object of class ASHPackage or throws an exception |
|
538
|
|
|
* |
|
539
|
|
|
* @since 0.0.1 |
|
540
|
|
|
* @param ASHPackage $package |
|
541
|
|
|
* @throws ErrorException |
|
542
|
|
|
*/ |
|
543
|
|
|
function add_package($package){ |
|
544
|
|
|
if ($package instanceof ASHPackage){ |
|
545
|
|
|
array_push($this->packages, $package); |
|
546
|
|
|
$this->package_count++; |
|
547
|
|
|
$this->total_weight += $package->weight; |
|
548
|
|
|
$this->shipment_value += $package->value; |
|
549
|
|
|
}else{ |
|
550
|
|
|
$type = gettype($package); |
|
551
|
|
|
throw new ErrorException("ASHSHipment expected object of class ASHPackage, got instance of {$type} instead"); |
|
552
|
|
|
} |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* This is the heart of the Advanced Shipping Helper for WPEC |
|
559
|
|
|
* It is the entrypoint for interaction between ASH and WPEC |
|
560
|
|
|
* |
|
561
|
|
|
*/ |
|
562
|
|
|
class ASH{ |
|
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* General constructor for ASH class |
|
565
|
|
|
* |
|
566
|
|
|
*/ |
|
567
|
|
|
public function _construct(){ |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
/** |
|
571
|
|
|
* Builds a shipment object representing the cart contents from WPEC |
|
572
|
|
|
* |
|
573
|
|
|
* @return ASHShipment |
|
574
|
|
|
*/ |
|
575
|
|
|
function get_shipment(){ |
|
576
|
|
|
global $wpdb, $wpsc_cart; |
|
577
|
|
|
|
|
578
|
|
|
$shipment = new ASHShipment(); |
|
579
|
|
|
if (!$wpsc_cart){ |
|
580
|
|
|
return $shipment; |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
foreach($wpsc_cart->cart_items as $cart_item){ |
|
584
|
|
|
$package = new ASHPackage(); |
|
585
|
|
|
//*** Set package dimensions ***\\ |
|
586
|
|
|
$dimensions = get_product_meta($cart_item->product_id, 'product_metadata'); //The 'dimensions' meta doesn't exist. |
|
587
|
|
|
if ( isset( $dimensions[0]['dimensions'] ) ) { |
|
588
|
|
|
$dimensions = $dimensions[0]['dimensions']; |
|
589
|
|
|
} |
|
590
|
|
|
$dim_array = array(); |
|
591
|
|
|
$dim_array["weight"] = $cart_item->weight; |
|
592
|
|
|
$dim_array["height"] = ( !empty( $dimensions["height"] ) && is_numeric( $dimensions["height"] ) ) ? $dimensions["height"] : 1; |
|
593
|
|
|
$dim_array["width"] = ( !empty( $dimensions["width"] ) && is_numeric( $dimensions["width"] ) ) ? $dimensions["width"] : 1; |
|
594
|
|
|
$dim_array["length"] = ( !empty( $dimensions["length"] ) && is_numeric( $dimensions["length"] ) ) ? $dimensions["length"] : 1; |
|
595
|
|
|
$package->set_dimensions($dim_array); |
|
596
|
|
|
|
|
597
|
|
|
/* Set other meta */ |
|
598
|
|
|
$package->hazard = ( get_product_meta( $cart_item->product_id, "ship_hazard", TRUE ) === TRUE) ? TRUE : FALSE; //Fixed ternary evaluation. |
|
|
|
|
|
|
599
|
|
|
$package->insurance = ( get_product_meta( $cart_item->product_id, "ship_insurance", TRUE ) === TRUE) ? TRUE : FALSE; //Fixed ternary evaluation. |
|
|
|
|
|
|
600
|
|
|
$package->insured_amount = get_product_meta( $cart_item->product_id,"ship_insured_amount", TRUE ); //Fixed ternary evaluation. |
|
|
|
|
|
|
601
|
|
|
$package->value = $cart_item->unit_price; |
|
602
|
|
|
$package->contents = $cart_item->product_name; |
|
603
|
|
|
$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 |
|
|
|
|
|
|
604
|
|
|
if ($shipment->hazard === FALSE and $package->hazard === TRUE){ |
|
|
|
|
|
|
605
|
|
|
$shipment->set_hazard(TRUE); |
|
|
|
|
|
|
606
|
|
|
} |
|
607
|
|
|
$quantity = (int)$cart_item->quantity; |
|
608
|
|
|
$package->product_id[$cart_item->product_id] = 1; // The product in this package. |
|
609
|
|
|
for($i=1; $i <= $quantity; $i++){ |
|
610
|
|
|
$shipment->add_package($package); |
|
611
|
|
|
} |
|
612
|
|
|
} |
|
613
|
|
|
return $shipment; |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
/** |
|
617
|
|
|
* Caches a result table for the given shipping module |
|
618
|
|
|
* |
|
619
|
|
|
* @param string $internal_name |
|
620
|
|
|
* @param array $rate_table |
|
621
|
|
|
* @param ASHShipment $shipment |
|
622
|
|
|
*/ |
|
623
|
|
|
function cache_results($internal_name, $rate_table, $shipment){ |
|
624
|
|
|
$wpec_ash = wpsc_get_customer_meta( 'shipping_ash' ); |
|
625
|
|
|
if ( ! is_array( $wpec_ash ) ) |
|
626
|
|
|
$wpec_ash = array(); |
|
627
|
|
|
|
|
628
|
|
|
if ( empty( $wpec_ash[$internal_name] ) || ! is_array( $wpec_ash[$internal_name] ) ) |
|
629
|
|
|
$wpec_ash[$internal_name] = array(); |
|
630
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|