Complex classes like ash_ups often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ash_ups, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class ash_ups { |
||
|
|
|||
| 7 | public $internal_name; |
||
| 8 | public $name; |
||
| 9 | public $service_url = ''; |
||
| 10 | public $Services = array(); |
||
| 11 | public $singular_shipping = false; |
||
| 12 | public $shipment; |
||
| 13 | public $is_external = true; |
||
| 14 | public $requires_curl = true; |
||
| 15 | public $requires_weight = true; |
||
| 16 | public $needs_zipcode = true; |
||
| 17 | |||
| 18 | public $drop_types = array(); |
||
| 19 | public $cust_types = array(); |
||
| 20 | |||
| 21 | public function __construct() { |
||
| 22 | $this->internal_name = 'ups'; |
||
| 23 | $this->name = _x( 'UPS', 'Shipping Module', 'wp-e-commerce' ); |
||
| 24 | $this->is_external = true; |
||
| 25 | $this->requires_curl = true; |
||
| 26 | $this->requires_weight = true; |
||
| 27 | $this->needs_zipcode = true; |
||
| 28 | $this->_setServiceURL(); |
||
| 29 | $this->_includeUPSData(); |
||
| 30 | return true; |
||
| 31 | } |
||
| 32 | |||
| 33 | function __autoload ( $name ) { |
||
| 36 | |||
| 37 | function getId () { |
||
| 40 | |||
| 41 | function setId ( $id ) { |
||
| 45 | |||
| 46 | private function _setServiceURL () { |
||
| 47 | global $wpdb; |
||
| 48 | $wpsc_ups_settings = get_option( 'wpsc_ups_settings' ); |
||
| 49 | $wpsc_ups_environment = ( array_key_exists( 'upsenvironment', (array) $wpsc_ups_settings ) ) ? $wpsc_ups_settings['upsenvironment'] : '0'; //(1) testing, else (0) production. |
||
| 50 | |||
| 51 | if ( '1' == $wpsc_ups_environment ){ |
||
| 52 | $this->service_url = 'https://wwwcie.ups.com/ups.app/xml/Rate'; |
||
| 53 | } else { |
||
| 54 | $this->service_url = 'https://www.ups.com/ups.app/xml/Rate'; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | function getName () { |
||
| 61 | |||
| 62 | function getInternalName () { |
||
| 65 | |||
| 66 | private function _includeUPSData () { |
||
| 67 | $this->drop_types = array( |
||
| 68 | '01' => __( 'Daily Pickup', 'wp-e-commerce' ), |
||
| 69 | '03' => __( 'Customer Counter', 'wp-e-commerce' ), |
||
| 70 | '06' => __( 'One Time Pickup', 'wp-e-commerce' ), |
||
| 71 | '07' => __( 'On Call Air', 'wp-e-commerce' ), |
||
| 72 | '19' => __( 'Letter Center', 'wp-e-commerce' ), |
||
| 73 | '20' => __( 'Air Service Center', 'wp-e-commerce' ), |
||
| 74 | '11' => __( 'Suggested Retail Rates (Advanced Config)', 'wp-e-commerce' ), |
||
| 75 | ); |
||
| 76 | |||
| 77 | $this->cust_types = array( |
||
| 78 | '01' => __( 'Daily Pickup, with UPS Account', 'wp-e-commerce' ), |
||
| 79 | '03' => __( 'No Daily Pickup, with No or Other Account', 'wp-e-commerce' ), |
||
| 80 | '04' => __( 'Retail Outlet (Only US origin shipments)', 'wp-e-commerce' ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | $this->Services = array( |
||
| 84 | '14' => __( 'Next Day Air Early AM', 'wp-e-commerce' ), |
||
| 85 | '01' => __( 'Next Day Air', 'wp-e-commerce' ), |
||
| 86 | '13' => __( 'Next Day Air Saver', 'wp-e-commerce' ), |
||
| 87 | '59' => __( '2nd Day Air AM', 'wp-e-commerce' ), |
||
| 88 | '02' => __( '2nd Day Air', 'wp-e-commerce' ), |
||
| 89 | '12' => __( '3 Day Select', 'wp-e-commerce' ), |
||
| 90 | '03' => __( 'Ground', 'wp-e-commerce' ), |
||
| 91 | '11' => __( 'Standard', 'wp-e-commerce' ), |
||
| 92 | '07' => __( 'Worldwide Express', 'wp-e-commerce' ), |
||
| 93 | '54' => __( 'Worldwide Express Plus', 'wp-e-commerce' ), |
||
| 94 | '08' => __( 'Worldwide Expedited', 'wp-e-commerce' ), |
||
| 95 | '65' => __( 'Saver', 'wp-e-commerce' ), |
||
| 96 | '82' => __( 'UPS Today Standard', 'wp-e-commerce' ), |
||
| 97 | '83' => __( 'UPS Today Dedicated Courier', 'wp-e-commerce' ), |
||
| 98 | '84' => __( 'UPS Today Intercity', 'wp-e-commerce' ), |
||
| 99 | '85' => __( 'UPS Today Express', 'wp-e-commerce' ), |
||
| 100 | '86' => __( 'UPS Today Express Saver', 'wp-e-commerce' ) |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | function getForm () { |
||
| 105 | if ( ! isset( $this->Services ) ) { |
||
| 106 | $this->_includeUPSData(); |
||
| 107 | } |
||
| 108 | //__('Your Packaging', 'wp-e-commerce'); <-- use to translate |
||
| 109 | $wpsc_ups_settings = get_option( 'wpsc_ups_settings' ); |
||
| 110 | $wpsc_ups_services = get_option( 'wpsc_ups_services' ); |
||
| 111 | // Defined on page 41 in UPS API documentation RSS_Tool_06_10_09.pdf |
||
| 112 | /*$packaging_options['00'] = __('**UNKNOWN**', 'wp-e-commerce');*/ |
||
| 113 | $packaging_options['01'] = __( 'UPS Letter', 'wp-e-commerce' ); |
||
| 114 | $packaging_options['02'] = __( 'Your Packaging', 'wp-e-commerce' ); |
||
| 115 | $packaging_options['03'] = __( 'UPS Tube', 'wp-e-commerce' ); |
||
| 116 | $packaging_options['04'] = __( 'UPS Pak', 'wp-e-commerce' ); |
||
| 117 | $packaging_options['21'] = __( 'UPS Express Box', 'wp-e-commerce' ); |
||
| 118 | $packaging_options['2a'] = __( 'UPS Express Box - Small', 'wp-e-commerce' ); |
||
| 119 | $packaging_options['2b'] = __( 'UPS Express Box - Medium', 'wp-e-commerce' ); |
||
| 120 | $packaging_options['2c'] = __( 'UPS Express Box - Large', 'wp-e-commerce' ); |
||
| 121 | |||
| 122 | ob_start(); |
||
| 123 | ?> |
||
| 124 | <tr><td><table class='form-table'> |
||
| 125 | <tr> |
||
| 126 | <td><?php _e( 'Destination Type', 'wp-e-commerce' ); ?></td> |
||
| 127 | <td> |
||
| 128 | <label> |
||
| 129 | <input type='radio' <?php checked( '2' != $wpsc_ups_settings['49_residential'] ); ?> value='1' name='wpsc_ups_settings[49_residential]' /> |
||
| 130 | <?php _e( 'Residential Address', 'wp-e-commerce' ); ?> |
||
| 131 | </label><br /> |
||
| 132 | <label><input type='radio' <?php checked( '2' == $wpsc_ups_settings['49_residential'] ); ?>value='2' name='wpsc_ups_settings[49_residential]' /> |
||
| 133 | <?php _e( 'Commercial Address', 'wp-e-commerce' )?> |
||
| 134 | </label> |
||
| 135 | </td> |
||
| 136 | </tr> |
||
| 137 | |||
| 138 | <?php |
||
| 139 | $sel2_drop = ''; |
||
| 140 | if ( empty( $wpsc_ups_settings['DropoffType'] ) ) { |
||
| 141 | $sel2_drop = '01'; |
||
| 142 | } else { |
||
| 143 | $sel2_drop = $wpsc_ups_settings['DropoffType']; |
||
| 144 | } |
||
| 145 | ?> |
||
| 146 | <tr> |
||
| 147 | <td><?php _e( 'Dropoff Type', 'wp-e-commerce' ); ?></td> |
||
| 148 | <td> |
||
| 149 | <script type="text/javascript"> |
||
| 150 | function checkDropValue () { |
||
| 151 | var val = jQuery("#drop_type option:selected").val(); |
||
| 152 | if (val == "11"){ |
||
| 153 | jQuery("#cust_type").removeAttr("disabled"); |
||
| 154 | } else { |
||
| 155 | jQuery("#cust_type").attr("disabled", true); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | </script> |
||
| 159 | <select id='drop_type' name='wpsc_ups_settings[DropoffType]' onChange='checkDropValue()' > |
||
| 160 | <?php foreach ( array_keys( (array) $this->drop_types ) as $dkey ): ?> |
||
| 161 | <option value="<?php echo esc_attr( $dkey ); ?>" <?php selected( $dkey, $sel2_drop ); ?> > |
||
| 162 | <?php echo( $this->drop_types[$dkey] ); ?> |
||
| 163 | </option> |
||
| 164 | <?php endforeach; ?> |
||
| 165 | </select> |
||
| 166 | </td> |
||
| 167 | </tr> |
||
| 168 | |||
| 169 | <?php |
||
| 170 | $sel3_drop = ''; |
||
| 171 | if ( empty( $wpsc_ups_settings['CustomerType'] ) ) { |
||
| 172 | $sel3_drop = '01'; |
||
| 173 | } else { |
||
| 174 | $sel3_drop = $wpsc_ups_settings['CustomerType']; |
||
| 175 | } |
||
| 176 | ?> |
||
| 177 | <tr> |
||
| 178 | <td><?php _e( 'Customer Type', 'wp-e-commerce' ); ?></td> |
||
| 179 | <td> |
||
| 180 | <select id='cust_type' name='wpsc_ups_settings[CustomerType]' <?php disabled( $wpsc_ups_settings['DropoffType'] != '11' ); ?> > |
||
| 181 | <?php foreach( array_keys( $this->cust_types ) as $dkey ): ?> |
||
| 182 | <option value="<?php echo esc_attr( $dkey ); ?>" <?php selected( $sel3_drop == $dkey ); ?> > |
||
| 183 | <?php echo( $this->cust_types[$dkey] ); ?> |
||
| 184 | </option> |
||
| 185 | <?php endforeach; ?> |
||
| 186 | </select> |
||
| 187 | </td> |
||
| 188 | </tr> |
||
| 189 | |||
| 190 | <tr> |
||
| 191 | <td><?php _e( 'Packaging', 'wp-e-commerce' ); ?></td> |
||
| 192 | <td> |
||
| 193 | <select name='wpsc_ups_settings[48_container]'> |
||
| 194 | <?php foreach ( $packaging_options as $key => $name ): ?> |
||
| 195 | <option value='<?php echo esc_attr( $key ); ?>' <?php selected( $key == $wpsc_ups_settings['48_container'] );?>> |
||
| 196 | <?php echo esc_html( $name ); ?> |
||
| 197 | </option> |
||
| 198 | <?php endforeach; ?> |
||
| 199 | </select> |
||
| 200 | </td> |
||
| 201 | </tr> |
||
| 202 | |||
| 203 | <tr> |
||
| 204 | <td><?php _e( 'Shipping Settings', 'wp-e-commerce' ); ?></td> |
||
| 205 | <td> |
||
| 206 | <input type="checkbox" id="ups_env_test" name="wpsc_ups_settings[upsenvironment]" value="1" <?php checked( 1, isset( $wpsc_ups_settings['upsenvironment'] ) && (bool) $wpsc_ups_settings['upsenvironment'] ); ?> /> |
||
| 207 | <label for="ups_env_test" ><?php _e( 'Use Testing Environment', 'wp-e-commerce' ); ?></label> |
||
| 208 | <br /> |
||
| 209 | <input type="checkbox" id="ups_negotiated_rates" name="wpsc_ups_settings[ups_negotiated_rates]" value="1" <?php checked( 1, isset( $wpsc_ups_settings['ups_negotiated_rates'] ) && (bool) $wpsc_ups_settings['ups_negotiated_rates'] ); ?> /> |
||
| 210 | <label for="ups_negotiated_rates" ><?php _e( 'Show UPS negotiated rates', 'wp-e-commerce' ); ?> *</label> |
||
| 211 | <br /> |
||
| 212 | <input type="checkbox" id="ups_insured_shipment" name="wpsc_ups_settings[insured_shipment]" value="1" <?php checked( 1, isset( $wpsc_ups_settings['insured_shipment'] ) && (bool) $wpsc_ups_settings['insured_shipment'] ); ?> /> |
||
| 213 | <label for="ups_insured_shipment" ><?php _e( 'Insure shipment against cart total', 'wp-e-commerce' ); ?> *</label> |
||
| 214 | <br /> |
||
| 215 | <input type="checkbox" id="ups_singular_shipping" name="wpsc_ups_settings[singular_shipping]" value="1" <?php checked( 1, isset( $wpsc_ups_settings['singular_shipping'] ) && (bool) $wpsc_ups_settings['singular_shipping'] ); ?> /> |
||
| 216 | <label for="ups_singular_shipping" ><?php _e( 'Singular Shipping', 'wp-e-commerce' ); ?> *</label> |
||
| 217 | <p class='description'><?php _e( 'Rate each quantity of items in a cart as its own package using dimensions on product', 'wp-e-commerce' ); ?></p> |
||
| 218 | <input type="checkbox" id="ups_intl_rate" name="wpsc_ups_settings[intl_rate]" value="1" <?php checked( 1, isset( $wpsc_ups_settings['intl_rate'] ) && (bool) $wpsc_ups_settings['intl_rate'] ); ?> /> |
||
| 219 | <label for="ups_intl_rate" ><?php _e( 'Disable International Shipping', 'wp-e-commerce' ); ?></label> |
||
| 220 | <p class='description'><?php _e( 'No shipping rates will be displayed if the shipment destination country is different than your base country/region.', 'wp-e-commerce' ); ?></p> |
||
| 221 | </td> |
||
| 222 | </tr> |
||
| 223 | |||
| 224 | <?php |
||
| 225 | ksort( $this->Services ); |
||
| 226 | $first = false; |
||
| 227 | ?> |
||
| 228 | <tr> |
||
| 229 | <td><?php _e( 'UPS Preferred Services', 'wp-e-commerce' ); ?></td> |
||
| 230 | <td> |
||
| 231 | <div class="ui-widget-content multiple-select"> |
||
| 232 | <?php foreach( array_keys( $this->Services ) as $service ): ?> |
||
| 233 | <input type="checkbox" id="wps_ups_srv_<?php echo esc_attr( $service ); ?>" name="wpsc_ups_services[]" value="<?php echo esc_attr( $service ); ?>" <?php checked( is_array( $wpsc_ups_services ) && ( array_search( $service, $wpsc_ups_services ) !== false ) ); ?> /> |
||
| 234 | <label for="wps_ups_srv_<?php echo esc_attr( $service ); ?>"><?php echo esc_html( $this->Services[ $service ] ); ?></label><br /> |
||
| 235 | <?php endforeach; ?> |
||
| 236 | </div> |
||
| 237 | <p class='description'><?php _e( 'Note: All services used if no services selected', 'wp-e-commerce' ); ?></p> |
||
| 238 | </td> |
||
| 239 | </tr> |
||
| 240 | |||
| 241 | <tr> |
||
| 242 | <td colspan='2'><strong><?php _e( 'My UPS', 'wp-e-commerce' ); ?></strong></td> |
||
| 243 | </tr> |
||
| 244 | <tr> |
||
| 245 | <td><?php _e( 'Account Number', 'wp-e-commerce' ); ?> *</td> |
||
| 246 | <td> |
||
| 247 | <input type="text" name='wpsc_ups_settings[upsaccount]' value="<?php echo esc_attr( $wpsc_ups_settings['upsaccount'] ); ?>" /> |
||
| 248 | </td> |
||
| 249 | </tr> |
||
| 250 | <tr> |
||
| 251 | <td><?php _e( 'Username', 'wp-e-commerce' ); ?></td> |
||
| 252 | <td> |
||
| 253 | <input type="text" name='wpsc_ups_settings[upsusername]' value="<?php echo esc_attr( base64_decode( $wpsc_ups_settings['upsusername'] ) ); ?>" /> |
||
| 254 | </td> |
||
| 255 | </tr> |
||
| 256 | <tr> |
||
| 257 | <td><?php _e( 'Password', 'wp-e-commerce' ); ?></td> |
||
| 258 | <td> |
||
| 259 | <input type="password" name='wpsc_ups_settings[upspassword]' value="<?php echo esc_attr( base64_decode( $wpsc_ups_settings['upspassword'] ) ); ?>" /> |
||
| 260 | </td> |
||
| 261 | </tr> |
||
| 262 | <tr> |
||
| 263 | <td><?php _e( 'UPS XML API Key', 'wp-e-commerce' ); ?></td> |
||
| 264 | <td> |
||
| 265 | <input type="text" name='wpsc_ups_settings[upsid]' value="<?php echo esc_attr( base64_decode( $wpsc_ups_settings['upsid'] ) ); ?>" /> |
||
| 266 | <p class='description'><?php printf( __( "Don't have an API login/ID? <a href='%s' target='_blank'>Sign up for My UPS</a>", 'wp-e-commerce' ), esc_url( "https://www.ups.com/upsdeveloperkit?loc=en_US" ) ); ?></p> |
||
| 267 | </td> |
||
| 268 | </tr> |
||
| 269 | <tr> |
||
| 270 | <td colspan='2'> |
||
| 271 | <p class='description'><?php _e( '* For Negotiated rates, you must enter a UPS account number and select "Show UPS negotiated rates" ', 'wp-e-commerce' ); ?></p> |
||
| 272 | <p class='description'><?php printf( __( "For more help configuring UPS, please <a href='%s'>read our documentation</a>", 'wp-e-commerce' ), esc_url( 'http://docs.wpecommerce.org/wiki/documentation/shipping/ups' ) ); ?></p> |
||
| 273 | </td> |
||
| 274 | </tr> |
||
| 275 | </table></td></tr> |
||
| 276 | <?php |
||
| 277 | // End new Code |
||
| 278 | return ob_get_clean(); |
||
| 279 | } |
||
| 280 | |||
| 281 | function submit_form() { |
||
| 282 | /* This function is called when the user hit "submit" in the |
||
| 283 | * UPS settings area under Shipping to update the setttings. |
||
| 284 | */ |
||
| 285 | if (isset( $_POST['wpsc_ups_settings'] ) && !empty( $_POST['wpsc_ups_settings'] ) ) { |
||
| 286 | if ( isset( $_POST['wpsc_ups_services'] ) ) { |
||
| 287 | $wpsc_ups_services = stripslashes_deep( $_POST['wpsc_ups_services'] ); |
||
| 288 | update_option('wpsc_ups_services',$wpsc_ups_services); |
||
| 289 | } |
||
| 290 | $temp = stripslashes_deep( $_POST['wpsc_ups_settings'] ); |
||
| 291 | // base64_encode the information so it isnt stored as plaintext. |
||
| 292 | // base64 is by no means secure but without knowing if the server |
||
| 293 | // has mcrypt installed what can you do really? |
||
| 294 | $temp['upsusername'] = base64_encode( $temp['upsusername'] ); |
||
| 295 | $temp['upspassword'] = base64_encode( $temp['upspassword'] ); |
||
| 296 | $temp['upsid'] = base64_encode( $temp['upsid'] ); |
||
| 297 | |||
| 298 | update_option('wpsc_ups_settings', $temp); |
||
| 299 | } |
||
| 300 | return true; |
||
| 301 | } |
||
| 302 | |||
| 303 | function array2xml( $data ) { |
||
| 304 | $xml = ""; |
||
| 305 | if ( is_array( $data ) ) { |
||
| 306 | foreach( $data as $key => $value ) { |
||
| 307 | $xml .= "<" . trim( $key ) . ">"; |
||
| 308 | $xml .= $this->array2xml($value); |
||
| 309 | $xml .= "</" . trim( $key ) . ">"; |
||
| 310 | } |
||
| 311 | } else if ( is_bool( $data ) ) { |
||
| 312 | if ( $data ){ |
||
| 313 | $xml = "true\n"; |
||
| 314 | } else { |
||
| 315 | $xml = "false\n"; |
||
| 316 | } |
||
| 317 | } else { |
||
| 318 | $xml = trim( $data ) . "\n"; |
||
| 319 | } |
||
| 320 | return $xml; |
||
| 321 | } |
||
| 322 | |||
| 323 | private function _is_large( &$pack, $package ) { |
||
| 333 | |||
| 334 | private function _insured_value( &$pack, $package, $args ){ |
||
| 335 | $monetary_value = $package->value; |
||
| 336 | if ( $package->insurance === TRUE ){ |
||
| 337 | if ( $package->insured_amount ) { |
||
| 338 | $monetary_value = $package->insured_amount; |
||
| 339 | } |
||
| 340 | $pack["PackageServiceOptions"]["InsuredValue"] = array( |
||
| 341 | "CurrencyCode" => $args["currency"], |
||
| 342 | "MonetaryValue" => $package->insured_amount |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | private function _declared_value( &$pack, $package, $args ){ |
||
| 348 | $pack["PackageServiceOptions"]["DeclaredValue"] = array( |
||
| 349 | "CurrencyCode" => $args["currency"], |
||
| 350 | "MonetaryValue" => $package->value //Per package value, not total value |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | private function _build_shipment( &$Shipment, $args ){ |
||
| 355 | |||
| 356 | $cart_shipment = apply_filters( 'wpsc_the_shipment', $this->shipment, $args ); //Filter to allow reprocessing shipment packages. |
||
| 357 | |||
| 358 | foreach ( $cart_shipment->packages as $package ) { |
||
| 359 | $pack = array( |
||
| 360 | "PackagingType" => array( |
||
| 361 | "Code" => "02" |
||
| 362 | ), |
||
| 363 | "Dimensions" => array( |
||
| 364 | "UnitOfMeasurement" => array( |
||
| 365 | "Code" => "IN" |
||
| 366 | ), |
||
| 367 | "Length" => $package->length, |
||
| 368 | "Width" => $package->width, |
||
| 369 | "Height" => $package->height |
||
| 370 | ), |
||
| 371 | "PackageWeight"=>array( |
||
| 372 | "UnitOfMeasurement"=>array( |
||
| 373 | "Code" => "LBS" |
||
| 374 | ), |
||
| 375 | "Weight" => $package->weight |
||
| 376 | ) |
||
| 377 | ); // End Package |
||
| 378 | // handle if the package is "large" or not (UPS standard) |
||
| 379 | $this->_is_large($pack, $package); |
||
| 380 | $this->_insured_value($pack, $package, $args); |
||
| 381 | $this->_declared_value($pack, $package, $args); |
||
| 382 | $Shipment .= $this->array2xml(array("Package"=>$pack)); |
||
| 383 | } // End for each package in shipment |
||
| 384 | } |
||
| 385 | |||
| 386 | private function _buildRateRequest( $args ){ |
||
| 555 | |||
| 556 | private function _makeRateRequest( $message ){ |
||
| 557 | // Make the XML request to the server and retrieve the response |
||
| 558 | $ch = curl_init(); |
||
| 559 | |||
| 560 | curl_setopt( $ch, CURLOPT_URL, $this->service_url ); |
||
| 561 | curl_setopt( $ch, CURLOPT_POST, 1 ); |
||
| 562 | curl_setopt( $ch, CURLOPT_POSTFIELDS, $message ); |
||
| 563 | curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 564 | |||
| 565 | $data = curl_exec( $ch ); |
||
| 566 | curl_close( $ch ); |
||
| 567 | return $data; |
||
| 568 | } |
||
| 569 | |||
| 570 | public function futureDate( $interval ){ |
||
| 571 | //Wed Apr 7 |
||
| 572 | date_default_timezone_set( 'America/Los_Angeles' ); |
||
| 573 | $timestamp = date( 'c' ); |
||
| 574 | $hour = date( "G" ); |
||
| 575 | if ( (int) $hour >= 3 ) { |
||
| 576 | $interval += 1; |
||
| 577 | } |
||
| 578 | |||
| 579 | $date = date( "Y-m-d" ); |
||
| 580 | $interval = " +$interval day"; |
||
| 581 | $final = date( "D M j", strtotime( date( "Y-m-d", strtotime( $date ) ) . $interval ) ); |
||
| 582 | $test = explode(" ", $final); |
||
| 583 | |||
| 584 | if ( $test[0] == "Sat" ) { |
||
| 585 | return $this->futureDate( $interval + 2 ); |
||
| 586 | } else if ( $test[0] == "Sun" ) { |
||
| 587 | return $this->futureDate( $interval + 1 ); |
||
| 588 | } |
||
| 589 | return $final; |
||
| 590 | } |
||
| 591 | |||
| 592 | private function _parseQuote( $raw ){ |
||
| 593 | global $wpdb; |
||
| 594 | |||
| 595 | $config = get_option( 'wpsc_ups_settings', array() ); |
||
| 596 | $debug = ( array_key_exists( 'upsenvironment', $config) ) ? $config['upsenvironment'] : ""; |
||
| 597 | |||
| 598 | $rate_table = array(); |
||
| 599 | $wpsc_ups_services = get_option( "wpsc_ups_services" ); |
||
| 600 | // Initialize a DOM using the XML passed in! |
||
| 601 | $objDOM = new DOMDocument(); |
||
| 602 | if ( $raw != '' ) { |
||
| 603 | $objDOM->loadXML( $raw ); |
||
| 604 | |||
| 605 | // Get the <ResponseStatusCode> from the UPS XML |
||
| 606 | $getStatusNode = $objDOM->getElementsByTagName( "ResponseStatusCode" ); |
||
| 607 | // Get the value of the error code, 1 == No Error, 0 == Error !!! |
||
| 608 | $statusCode = $getStatusNode->item( 0 )->nodeValue; |
||
| 609 | |||
| 610 | if ( $statusCode == "0" ) { |
||
| 611 | // Usually I dont leave debug stuff in but this is handy stuff!! |
||
| 612 | // it will print out the error message returned by UPS! |
||
| 613 | if ( $debug == "1" ) { |
||
| 614 | $getErrorDescNode = $objDOM->getElementsByTagName( "ErrorDescription" ); |
||
| 615 | $ErrorDesc = $getErrorDescNode->item( 0 )->nodeValue; |
||
| 616 | echo "<br />Error : " . $ErrorDesc . "<br />"; |
||
| 617 | } |
||
| 618 | return false; |
||
| 619 | } else { |
||
| 620 | $RateBlocks = $objDOM->getElementsByTagName( 'RatedShipment' ); |
||
| 621 | foreach ( $RateBlocks as $rate_block ) { |
||
| 622 | // Get the <Service> Node from the XML chunk |
||
| 623 | $getServiceNode = $rate_block->getElementsByTagName( 'Service' ); |
||
| 624 | $serviceNode = $getServiceNode->item( 0 ); |
||
| 625 | |||
| 626 | // Get the <Code> Node from the <Service> chunk |
||
| 627 | $getServiceCodeNode = $serviceNode->getElementsByTagName( 'Code' ); |
||
| 628 | // Get the value from <Code> |
||
| 629 | $serviceCode = $getServiceCodeNode->item( 0 )->nodeValue; |
||
| 630 | $go = true; |
||
| 631 | $price = ''; |
||
| 632 | $time = ''; |
||
| 633 | |||
| 634 | //if (array_key_exists('ups_negotiated_rates', $config)){ |
||
| 635 | $getNegotiatedRateNode = $rate_block->getElementsByTagName( 'NegotiatedRates' ); |
||
| 636 | if ( $getNegotiatedRateNode ) { |
||
| 637 | $negotiatedRateNode = $getNegotiatedRateNode->item( 0 ); |
||
| 638 | if ( $negotiatedRateNode ) { |
||
| 639 | $getNetSummaryNode = $negotiatedRateNode->getElementsByTagName( 'NetSummaryCharges' ); |
||
| 640 | $netSummaryNode = $getNetSummaryNode->item( 0 ); |
||
| 641 | |||
| 642 | $getGrandTotalNode = $netSummaryNode->getElementsByTagName( 'GrandTotal' ); |
||
| 643 | $grandTotalNode = $getGrandTotalNode->item( 0 ); |
||
| 644 | |||
| 645 | $getMonetaryNode = $grandTotalNode->getElementsByTagName( 'MonetaryValue' ); |
||
| 646 | $monetaryNode = $getMonetaryNode->item( 0 )->nodeValue; |
||
| 647 | if ( ! empty( $monetaryNode ) ) { |
||
| 648 | $go = false; |
||
| 649 | $price = $monetaryNode; |
||
| 650 | } |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | // Get the <TotalCharges> Node from the XML chunk |
||
| 655 | $getChargeNodes = $rate_block->getElementsByTagName( 'TotalCharges' ); |
||
| 656 | $chargeNode = $getChargeNodes->item( 0 ); |
||
| 657 | |||
| 658 | // Get the <CurrencyCode> from the <TotalCharge> chunk |
||
| 659 | $getCurrNode = $chargeNode->getElementsByTagName( 'CurrencyCode' ); |
||
| 660 | |||
| 661 | // Get the value of <CurrencyCode> |
||
| 662 | $currCode = $getCurrNode->item( 0 )->nodeValue; |
||
| 663 | |||
| 664 | if ( $go == true ){ |
||
| 665 | // Get the <MonetaryValue> from the <TotalCharge> chunk |
||
| 666 | $getMonetaryNode = $chargeNode->getElementsByTagName( 'MonetaryValue' ); |
||
| 667 | // Get the value of <MonetaryValue> |
||
| 668 | $price = $getMonetaryNode->item( 0 )->nodeValue; |
||
| 669 | } |
||
| 670 | // If there are any services specified in the admin area |
||
| 671 | // this will check that list and pass on adding any services that |
||
| 672 | // are not explicitly defined. |
||
| 673 | if ( ! empty( $wpsc_ups_services ) ) { |
||
| 674 | if ( is_array( $wpsc_ups_services ) ) { |
||
| 675 | if ( array_search( $serviceCode, (array) $wpsc_ups_services ) === false ) { |
||
| 676 | continue; |
||
| 677 | } |
||
| 678 | } else if ( $wpsc_ups_services != $serviceCode ) { |
||
| 679 | continue; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | if ( array_key_exists( $serviceCode, (array) $this->Services ) ) { |
||
| 683 | $rate_table[ $this->Services[ $serviceCode ] ] = array( $currCode, $price ); |
||
| 684 | } |
||
| 685 | } // End foreach rated shipment block |
||
| 686 | } |
||
| 687 | } |
||
| 688 | // Reverse sort the rate selection so it is cheapest first! |
||
| 689 | if ( ! empty( $rate_table ) ) { |
||
| 690 | asort( $rate_table ); |
||
| 691 | } |
||
| 692 | return $rate_table; |
||
| 693 | } |
||
| 694 | |||
| 695 | private function _formatTable( $services, $currency = false ) { |
||
| 696 | /* The checkout template expects the array to be in a certain |
||
| 697 | * format. This function will iterate through the provided |
||
| 698 | * services array and format it for use. During the loop |
||
| 699 | * we take advantage of the loop and translate the currency |
||
| 700 | * if necessary based off of what UPS tells us they are giving us |
||
| 701 | * for currency and what is set for the main currency in the settings |
||
| 702 | * area |
||
| 703 | */ |
||
| 704 | $converter = null; |
||
| 705 | if ( $currency ) { |
||
| 706 | $converter = new CURRENCYCONVERTER(); |
||
| 707 | } |
||
| 708 | $finalTable = array(); |
||
| 709 | foreach ( array_keys( $services ) as $service ) { |
||
| 710 | if ( $currency != false && $currency != $services[ $service ][0] ) { |
||
| 711 | $temp = $services[ $service ][1]; |
||
| 712 | $services[ $service ][1] = $converter->convert( |
||
| 713 | $services[ $service ][1], |
||
| 714 | $currency, |
||
| 715 | $services[ $service ][0] |
||
| 716 | ); |
||
| 717 | } |
||
| 718 | $finalTable[ $service ] = $services[ $service ][1]; |
||
| 719 | } |
||
| 720 | return $finalTable; |
||
| 721 | } |
||
| 722 | |||
| 723 | function getQuote() { |
||
| 879 | |||
| 880 | // Empty Function, this exists just b/c it is prototyped elsewhere |
||
| 881 | function get_item_shipping() { |
||
| 883 | } |
||
| 884 | $ash_ups = new ash_ups(); |
||
| 885 | $wpsc_shipping_modules[ $ash_ups->getInternalName() ] = $ash_ups; |
||
| 886 |