/** * Process address fields so they can be stored correctly */ public function sanitize_settings($settings) { // Prevent this from running except on the main settings page if (isset($_POST['wootax_address1'])) { // Fetch all addresses and dump into array $new_addresses = array(); $address_count = count($_POST['wootax_address1']); for ($i = 0; $i < $address_count; $i++) { $address = array('address_1' => $_POST['wootax_address1'][$i], 'address_2' => $_POST['wootax_address2'][$i], 'country' => 'United States', 'state' => $_POST['wootax_state'][$i], 'city' => $_POST['wootax_city'][$i], 'zip5' => $_POST['wootax_zip5'][$i], 'zip4' => $_POST['wootax_zip4'][$i]); $new_addresses[] = $address; } $taxcloud_id = trim($_POST['woocommerce_wootax_tc_id']); $taxcloud_key = trim($_POST['woocommerce_wootax_tc_key']); $usps_id = trim($_POST['woocommerce_wootax_usps_id']); // Validate addresses using USPS Web Tools API if possible if ($taxcloud_id && $taxcloud_key && $usps_id) { $taxcloud = TaxCloud($taxcloud_id, $taxcloud_key); foreach ($new_addresses as $key => $address) { $req = array('uspsUserID' => $usps_id, 'Address1' => strtolower($address['address_1']), 'Address2' => strtolower($address['address_2']), 'Country' => 'US', 'City' => $address['city'], 'State' => $address['state'], 'Zip5' => $address['zip5'], 'Zip4' => $address['zip4']); // Attempt to verify address $response = $taxcloud->send_request('VerifyAddress', $req); if ($response !== false) { $new_address = array(); $properties = array('Address1' => 'address_1', 'Address2' => 'address_2', 'Country' => 'country', 'City' => 'city', 'State' => 'state', 'Zip5' => 'zip5', 'Zip4' => 'zip4'); foreach ($properties as $property => $k) { if (isset($response->{$property})) { $new_address[$k] = $response->{$property}; } } // Reset country field if (!isset($new_address['country'])) { $new_address['country'] = $req['Country']; } $new_addresses[$key] = $new_address; } } } // Set addresses option $settings['addresses'] = $new_addresses; // Next, update the default address $settings['default_address'] = empty($_POST['wootax_default_address']) ? 0 : $_POST['wootax_default_address']; // Set settings_changed flag to "true" so WooTax reloads settings array WC_WooTax::$settings_changed = true; } // Enforce default settings for log_requests/capture_immediately/exempt_roles if (!isset($_POST['woocommerce_wootax_log_requests'])) { $settings['log_requests'] = 'yes'; } if (!isset($_POST['woocommerce_wootax_capture_immediately'])) { $settings['capture_immediately'] = 'no'; } if (!isset($_POST['woocommerce_wootax_exempt_roles'])) { $settings['exempt_roles'] = array('exempt-customer'); } return $settings; }
/** * Get the value of a WooTax option * * @since 4.2 * @param (mixed) $key the key of the option to be fetched * @return (mixed) requested option or boolean false if it isn't set */ public static function get_option($key) { if (count(self::$settings) == 0 || self::$settings_changed) { self::$settings = get_option(self::$settings_key); self::$settings_changed = false; } if (!isset(self::$settings[$key]) || !self::$settings[$key]) { return false; } else { return self::$settings[$key]; } }