class-cache-controller.php
10 months ago
class-css-controller.php
10 months ago
class-domainshift-controller.php
10 months ago
class-key-controller.php
10 months ago
class-log-controller.php
10 months ago
class-option-controller.php
10 months ago
class-rest-controller.php
10 months ago
class-domainshift-controller.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use Exception; |
| 8 | use SuperbAddons\Config\Config; |
| 9 | use SuperbAddons\Data\Controllers\OptionController; |
| 10 | use SuperbAddons\Data\Utils\CacheTypes; |
| 11 | |
| 12 | class DomainShiftController |
| 13 | { |
| 14 | const STATUS_ENDPOINT = 'addons-status/status'; |
| 15 | |
| 16 | public static function FindPreferredAPIDomain() |
| 17 | { |
| 18 | try { |
| 19 | $options_controller = new OptionController(); |
| 20 | $idx = 0; |
| 21 | $success = false; |
| 22 | foreach (Config::API_DOMAINS as $available_domain) { |
| 23 | $response = wp_remote_get($available_domain, array('method' => 'HEAD')); |
| 24 | if (RestController::IsAcceptableConnection($response)) { |
| 25 | $success = $options_controller->UpdateAPIDomain($idx); |
| 26 | break; |
| 27 | } |
| 28 | $idx++; |
| 29 | } |
| 30 | |
| 31 | return $success; |
| 32 | } catch (Exception $ex) { |
| 33 | LogController::HandleException($ex); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public static function GetCurrentConnectionSuccess() |
| 39 | { |
| 40 | $options_controller = new OptionController(); |
| 41 | $preferred_domain = $options_controller->GetPreferredDomain(); |
| 42 | $response = wp_remote_get($preferred_domain, array('method' => 'HEAD')); |
| 43 | return RestController::IsAcceptableConnection($response); |
| 44 | } |
| 45 | |
| 46 | public static function GetServiceStatus() |
| 47 | { |
| 48 | $response = self::RemoteGet(self::STATUS_ENDPOINT); |
| 49 | /// |
| 50 | $status_code = wp_remote_retrieve_response_code($response); |
| 51 | |
| 52 | if (!is_array($response) || is_wp_error($response) || $status_code !== 200) { |
| 53 | if ($status_code === 401) { |
| 54 | return array("online" => false, "message" => __("Unauthorized. Please make sure that you are using the latest version of this plugin.", "superb-blocks")); |
| 55 | } |
| 56 | return array("online" => false, "message" => __("Service Unavailable. Please contact our support team.", "superb-blocks")); |
| 57 | } |
| 58 | |
| 59 | $data = json_decode($response['body']); |
| 60 | if (!isset($data->elementor) || !isset($data->gutenberg)) { |
| 61 | return array("online" => false, "message" => __("Service Data Unavailable. Please contact our support team.", "superb-blocks")); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | return array("online" => true, CacheTypes::ELEMENTOR => $data->elementor, CacheTypes::GUTENBERG => $data->gutenberg); |
| 66 | } |
| 67 | |
| 68 | public static function RemoteGet($path, $args = array()) |
| 69 | { |
| 70 | return self::RemoteRequest('wp_remote_get', $path, $args); |
| 71 | } |
| 72 | |
| 73 | public static function RemotePost($path, $args = array()) |
| 74 | { |
| 75 | return self::RemoteRequest('wp_remote_get', $path, $args); |
| 76 | } |
| 77 | |
| 78 | private static function RemoteRequest($method, $path, $args = array()) |
| 79 | { |
| 80 | $options_controller = new OptionController(); |
| 81 | $preferred_domain = $options_controller->GetPreferredDomain(); |
| 82 | $response = $method($preferred_domain . $path, RestController::GetArgsHeadersArray($args)); |
| 83 | if (!RestController::IsAcceptableConnection($response)) { |
| 84 | // Connection failed or blocked -> Find new preferred domain and send the request again. No recursion to avoid loop. |
| 85 | if (self::FindPreferredAPIDomain()) { |
| 86 | $preferred_domain = $options_controller->GetPreferredDomain(true); |
| 87 | $response = $method($preferred_domain . $path, RestController::GetArgsHeadersArray($args)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $response; |
| 92 | } |
| 93 | |
| 94 | public static function RemoteArgs($path) |
| 95 | { |
| 96 | $options_controller = new OptionController(); |
| 97 | $preferred_domain = $options_controller->GetPreferredDomain(); |
| 98 | return |
| 99 | array( |
| 100 | 'url' => $preferred_domain . $path, |
| 101 | 'headers' => RestController::GetArgsHeadersArray()['headers'], |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 |