| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Config\Endpoints; |
| 6 |
use SyncBasalam\Logger\Logger; |
| 7 |
use SyncBasalam\Services\ApiServiceManager; |
| 8 |
use SyncBasalam\Services\ForceUpdateGate; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class FetchVersionDetail |
| 13 |
{ |
| 14 |
private $apiService; |
| 15 |
private $version; |
| 16 |
|
| 17 |
public function __construct($version) |
| 18 |
{ |
| 19 |
$this->apiService = syncBasalamContainer()->get(ApiServiceManager::class); |
| 20 |
$this->version = $version; |
| 21 |
} |
| 22 |
|
| 23 |
public function Fetch() |
| 24 |
{ |
| 25 |
$url = Endpoints::HAMSALAM_VERSION_DETAIL . '?site_url=' . get_site_url() . '¤t_version=' . $this->version; |
| 26 |
try { |
| 27 |
$response = $this->apiService->get($url); |
| 28 |
return $response; |
| 29 |
} catch (\Throwable $e) { |
| 30 |
Logger::error('خطا در دریافت اطلاعات نسخه: ' . $e->getMessage()); |
| 31 |
return null; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public function checkForceUpdate() |
| 36 |
{ |
| 37 |
if (wp_get_environment_type() === 'local') { |
| 38 |
delete_option('sync_basalam_force_update'); |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
try { |
| 43 |
$response = $this->Fetch(); |
| 44 |
|
| 45 |
if (!is_array($response) || !isset($response['body'])) return false; |
| 46 |
|
| 47 |
$data = json_decode($response['body'], true); |
| 48 |
|
| 49 |
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data) || !array_key_exists('force_update', $data)) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
if (ForceUpdateGate::shouldHonorRemoteFlag( |
| 54 |
$data['force_update'], |
| 55 |
$this->version, |
| 56 |
isset($data['latest_version']) ? (string) $data['latest_version'] : null |
| 57 |
)) { |
| 58 |
update_option('sync_basalam_force_update', true); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
delete_option('sync_basalam_force_update'); |
| 63 |
return false; |
| 64 |
} catch (\Throwable $e) { |
| 65 |
Logger::error('خطا در بررسی force update: ' . $e->getMessage()); |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|