class-dashboard-controller.php
2 years ago
class-settings-controller.php
2 years ago
class-troubleshooting-controller.php
2 years ago
class-troubleshooting-controller.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Config\Capabilities; |
| 6 | use SuperbAddons\Data\Controllers\CacheController; |
| 7 | use SuperbAddons\Data\Controllers\DomainShiftController; |
| 8 | use SuperbAddons\Data\Controllers\KeyController; |
| 9 | use SuperbAddons\Data\Controllers\RestController; |
| 10 | use SuperbAddons\Data\Utils\CacheOptions; |
| 11 | use SuperbAddons\Data\Utils\ElementorCache; |
| 12 | use SuperbAddons\Data\Utils\KeyException; |
| 13 | use SuperbAddons\Elementor\Controllers\ElementorController; |
| 14 | use SuperbAddons\Gutenberg\Controllers\GutenbergController; |
| 15 | |
| 16 | defined('ABSPATH') || exit(); |
| 17 | |
| 18 | class TroubleshootingController |
| 19 | { |
| 20 | const TROUBLESHOOTING_ROUTE = '/troubleshooting'; |
| 21 | |
| 22 | const ENDPOINT_BASE = 'addons-status/'; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | RestController::AddRoute(self::TROUBLESHOOTING_ROUTE, array( |
| 27 | 'methods' => 'POST', |
| 28 | 'permission_callback' => array($this, 'TroubleshootingCallbackPermissionCheck'), |
| 29 | 'callback' => array($this, 'TroubleshootingRouteCallback'), |
| 30 | )); |
| 31 | } |
| 32 | |
| 33 | public function TroubleshootingCallbackPermissionCheck() |
| 34 | { |
| 35 | // Restrict endpoint to only users who have the proper capability. |
| 36 | if (!current_user_can(Capabilities::ADMIN)) { |
| 37 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', 'superbaddons'), array('status' => 401)); |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | public function TroubleshootingRouteCallback($request) |
| 44 | { |
| 45 | if (!isset($request['action'])) { |
| 46 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 47 | } |
| 48 | switch ($request['action']) { |
| 49 | case 'wordpressversion': |
| 50 | return $this->WordPressVersionCallback(); |
| 51 | case 'elementorversion': |
| 52 | return $this->ElementorVersionCallback(); |
| 53 | case 'connection': |
| 54 | return $this->ConnectionCheckCallback(); |
| 55 | case 'domainshift': |
| 56 | return $this->DomainShiftCallback(); |
| 57 | case 'service': |
| 58 | return $this->ServiceCheckCallback(); |
| 59 | case 'keycheck': |
| 60 | return $this->KeyCheckCallback(); |
| 61 | case 'keyverify': |
| 62 | return $this->KeyVerifyCallback(); |
| 63 | case 'cacheclear': |
| 64 | return $this->CacheClearCallback(); |
| 65 | default: |
| 66 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private function WordPressVersionCallback() |
| 71 | { |
| 72 | try { |
| 73 | $is_compatible = GutenbergController::is_compatible(); |
| 74 | |
| 75 | return rest_ensure_response(['success' => $is_compatible]); |
| 76 | } catch (Exception $ex) { |
| 77 | LogController::HandleException($ex); |
| 78 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private function ElementorVersionCallback() |
| 83 | { |
| 84 | try { |
| 85 | $is_compatible = ElementorController::is_compatible(); |
| 86 | |
| 87 | return rest_ensure_response(['success' => $is_compatible]); |
| 88 | } catch (Exception $ex) { |
| 89 | LogController::HandleException($ex); |
| 90 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private function ConnectionCheckCallback() |
| 95 | { |
| 96 | try { |
| 97 | $is_connected = DomainShiftController::GetCurrentConnectionSuccess(); |
| 98 | |
| 99 | return rest_ensure_response(['success' => $is_connected]); |
| 100 | } catch (Exception $ex) { |
| 101 | LogController::HandleException($ex); |
| 102 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private function DomainShiftCallback() |
| 107 | { |
| 108 | try { |
| 109 | $successful_shift = DomainShiftController::FindPreferredAPIDomain(); |
| 110 | |
| 111 | return rest_ensure_response(['success' => $successful_shift]); |
| 112 | } catch (Exception $ex) { |
| 113 | LogController::HandleException($ex); |
| 114 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private function ServiceCheckCallback() |
| 119 | { |
| 120 | try { |
| 121 | $status_arr = DomainShiftController::GetServiceStatus(); |
| 122 | if ($status_arr['online']) { |
| 123 | return rest_ensure_response(['success' => true]); |
| 124 | } |
| 125 | |
| 126 | return rest_ensure_response(['success' => false, "text" => esc_html($status_arr['message'])]); |
| 127 | } catch (Exception $ex) { |
| 128 | LogController::HandleException($ex); |
| 129 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private function KeyCheckCallback() |
| 134 | { |
| 135 | try { |
| 136 | $keyinfo = KeyController::GetKeyStatus(); |
| 137 | |
| 138 | if ($keyinfo['expired']) { |
| 139 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key Expired', 'superbaddons')]); |
| 140 | } |
| 141 | |
| 142 | if (!$keyinfo['active']) { |
| 143 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key Disabled. Please contact our support team for assistance.', 'superbaddons')]); |
| 144 | } |
| 145 | |
| 146 | if (!$keyinfo['verified']) { |
| 147 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Key Verification Invalid', 'superbaddons')]); |
| 148 | } |
| 149 | |
| 150 | $keytype_label = KeyController::GetCurrentKeyTypeLabel(); |
| 151 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label)]); |
| 152 | } catch (KeyException $k_ex) { |
| 153 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 154 | } catch (Exception $ex) { |
| 155 | LogController::HandleException($ex); |
| 156 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | private function KeyVerifyCallback() |
| 162 | { |
| 163 | try { |
| 164 | $keyinfo = KeyController::GetUpdatedLicenseKeyInformation(); |
| 165 | |
| 166 | if ($keyinfo['expired'] || !$keyinfo['active'] || !$keyinfo['verified']) { |
| 167 | return rest_ensure_response(['success' => false]); |
| 168 | } |
| 169 | $keytype_label = KeyController::GetKeyTypeLabel($keyinfo['type']); |
| 170 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label . ' ' . __('Verified', 'superbaddons'))]); |
| 171 | } catch (KeyException $k_ex) { |
| 172 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 173 | } catch (Exception $ex) { |
| 174 | LogController::HandleException($ex); |
| 175 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private function CacheClearCallback() |
| 180 | { |
| 181 | try { |
| 182 | $cleared = CacheController::ClearCache(CacheOptions::SERVICE_VERSION) && CacheController::ClearCache(ElementorCache::SECTIONS); |
| 183 | |
| 184 | return rest_ensure_response(['success' => $cleared]); |
| 185 | } catch (Exception $ex) { |
| 186 | LogController::HandleException($ex); |
| 187 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |