| 1 |
<?php |
| 2 |
|
| 3 |
namespace SuperFrete_API\Helpers; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Security check |
| 7 |
} |
| 8 |
|
| 9 |
class AddressHelper { |
| 10 |
|
| 11 |
private static $cache = []; |
| 12 |
private static $cache_duration = 3600; // 1 hour cache for address data |
| 13 |
|
| 14 |
/** |
| 15 |
* Get district (bairro) from postal code using ViaCEP API |
| 16 |
*/ |
| 17 |
public static function get_district_from_postal_code($postal_code) { |
| 18 |
// Clean postal code - remove any non-numeric characters |
| 19 |
$clean_postal_code = preg_replace('/[^0-9]/', '', $postal_code); |
| 20 |
|
| 21 |
if (strlen($clean_postal_code) !== 8) { |
| 22 |
Logger::log('SuperFrete', 'Invalid postal code format: ' . $postal_code); |
| 23 |
return null; |
| 24 |
} |
| 25 |
|
| 26 |
// Check cache first |
| 27 |
$cache_key = 'viacep_' . $clean_postal_code; |
| 28 |
if (isset(self::$cache[$cache_key]) && |
| 29 |
(time() - self::$cache[$cache_key]['timestamp']) < self::$cache_duration) { |
| 30 |
Logger::log('SuperFrete', 'Using cached address data for CEP: ' . $clean_postal_code); |
| 31 |
return self::$cache[$cache_key]['data']['bairro'] ?? null; |
| 32 |
} |
| 33 |
|
| 34 |
// Make ViaCEP API call |
| 35 |
$viacep_url = "https://viacep.com.br/ws/{$clean_postal_code}/json/"; |
| 36 |
|
| 37 |
Logger::log('SuperFrete', 'Calling ViaCEP API for CEP: ' . $clean_postal_code); |
| 38 |
|
| 39 |
$response = wp_remote_get($viacep_url, [ |
| 40 |
'timeout' => 10, |
| 41 |
'headers' => [ |
| 42 |
'User-Agent' => 'WooCommerce SuperFrete Plugin' |
| 43 |
] |
| 44 |
]); |
| 45 |
|
| 46 |
if (is_wp_error($response)) { |
| 47 |
Logger::log('SuperFrete', 'ViaCEP API error: ' . $response->get_error_message()); |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
$status_code = wp_remote_retrieve_response_code($response); |
| 52 |
if ($status_code !== 200) { |
| 53 |
Logger::log('SuperFrete', 'ViaCEP API returned status: ' . $status_code); |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
$body = wp_remote_retrieve_body($response); |
| 58 |
$data = json_decode($body, true); |
| 59 |
|
| 60 |
if (!$data || isset($data['erro'])) { |
| 61 |
Logger::log('SuperFrete', 'ViaCEP API returned error or no data for CEP: ' . $clean_postal_code); |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
// Cache the response |
| 66 |
self::$cache[$cache_key] = [ |
| 67 |
'data' => $data, |
| 68 |
'timestamp' => time() |
| 69 |
]; |
| 70 |
|
| 71 |
Logger::log('SuperFrete', 'ViaCEP API success for CEP: ' . $clean_postal_code . ' - District: ' . ($data['bairro'] ?? 'N/A')); |
| 72 |
|
| 73 |
return $data['bairro'] ?? null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get complete address data from postal code using ViaCEP API |
| 78 |
*/ |
| 79 |
public static function get_address_from_postal_code($postal_code) { |
| 80 |
// Clean postal code - remove any non-numeric characters |
| 81 |
$clean_postal_code = preg_replace('/[^0-9]/', '', $postal_code); |
| 82 |
|
| 83 |
if (strlen($clean_postal_code) !== 8) { |
| 84 |
Logger::log('SuperFrete', 'Invalid postal code format: ' . $postal_code); |
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
// Check cache first |
| 89 |
$cache_key = 'viacep_' . $clean_postal_code; |
| 90 |
if (isset(self::$cache[$cache_key]) && |
| 91 |
(time() - self::$cache[$cache_key]['timestamp']) < self::$cache_duration) { |
| 92 |
Logger::log('SuperFrete', 'Using cached address data for CEP: ' . $clean_postal_code); |
| 93 |
return self::$cache[$cache_key]['data']; |
| 94 |
} |
| 95 |
|
| 96 |
// Make ViaCEP API call |
| 97 |
$viacep_url = "https://viacep.com.br/ws/{$clean_postal_code}/json/"; |
| 98 |
|
| 99 |
Logger::log('SuperFrete', 'Calling ViaCEP API for CEP: ' . $clean_postal_code); |
| 100 |
|
| 101 |
$response = wp_remote_get($viacep_url, [ |
| 102 |
'timeout' => 10, |
| 103 |
'headers' => [ |
| 104 |
'User-Agent' => 'WooCommerce SuperFrete Plugin' |
| 105 |
] |
| 106 |
]); |
| 107 |
|
| 108 |
if (is_wp_error($response)) { |
| 109 |
Logger::log('SuperFrete', 'ViaCEP API error: ' . $response->get_error_message()); |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
$status_code = wp_remote_retrieve_response_code($response); |
| 114 |
if ($status_code !== 200) { |
| 115 |
Logger::log('SuperFrete', 'ViaCEP API returned status: ' . $status_code); |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
$body = wp_remote_retrieve_body($response); |
| 120 |
$data = json_decode($body, true); |
| 121 |
|
| 122 |
if (!$data || isset($data['erro'])) { |
| 123 |
Logger::log('SuperFrete', 'ViaCEP API returned error or no data for CEP: ' . $clean_postal_code); |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
// Cache the response |
| 128 |
self::$cache[$cache_key] = [ |
| 129 |
'data' => $data, |
| 130 |
'timestamp' => time() |
| 131 |
]; |
| 132 |
|
| 133 |
Logger::log('SuperFrete', 'ViaCEP API success for CEP: ' . $clean_postal_code); |
| 134 |
|
| 135 |
return $data; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Clear address cache |
| 140 |
*/ |
| 141 |
public static function clear_cache() { |
| 142 |
self::$cache = []; |
| 143 |
Logger::log('SuperFrete', 'Address cache cleared'); |
| 144 |
} |
| 145 |
} |