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
235 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 | use SuperbAddons\Tours\Controllers\TourController; |
| 16 | |
| 17 | defined('ABSPATH') || exit(); |
| 18 | |
| 19 | class TroubleshootingController |
| 20 | { |
| 21 | const TROUBLESHOOTING_ROUTE = '/troubleshooting'; |
| 22 | const TUTORIAL_ROUTE = '/tutorial'; |
| 23 | |
| 24 | const ENDPOINT_BASE = 'addons-status/'; |
| 25 | |
| 26 | public function __construct() |
| 27 | { |
| 28 | RestController::AddRoute(self::TROUBLESHOOTING_ROUTE, array( |
| 29 | 'methods' => 'POST', |
| 30 | 'permission_callback' => array($this, 'TroubleshootingCallbackPermissionCheck'), |
| 31 | 'callback' => array($this, 'TroubleshootingRouteCallback'), |
| 32 | )); |
| 33 | RestController::AddRoute(self::TUTORIAL_ROUTE, array( |
| 34 | 'methods' => 'POST', |
| 35 | 'permission_callback' => array($this, 'TutorialCallbackPermissionCheck'), |
| 36 | 'callback' => array($this, 'TutorialRouteCallback'), |
| 37 | )); |
| 38 | } |
| 39 | |
| 40 | public function TroubleshootingCallbackPermissionCheck() |
| 41 | { |
| 42 | // Restrict endpoint to only users who have the proper capability. |
| 43 | if (!current_user_can(Capabilities::ADMIN)) { |
| 44 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', 'superbaddons'), array('status' => 401)); |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | public function TutorialCallbackPermissionCheck() |
| 51 | { |
| 52 | // Restrict endpoint to only users who have the proper capability. |
| 53 | if (!current_user_can(Capabilities::CONTRIBUTOR)) { |
| 54 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', 'superbaddons'), array('status' => 401)); |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | public function TutorialRouteCallback($request) |
| 61 | { |
| 62 | if (!isset($request['action'])) { |
| 63 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 64 | } |
| 65 | try { |
| 66 | switch ($request['action']) { |
| 67 | case 'elementor-tour': |
| 68 | $url = TourController::GetElementorTourURL(); |
| 69 | return rest_ensure_response(['success' => true, 'url' => esc_url_raw($url)]); |
| 70 | case 'cleanup-elementor-tour-page': |
| 71 | $removed = TourController::CleanUpTourPage($request['tour-nonce']); |
| 72 | return rest_ensure_response(['success' => $removed]); |
| 73 | default: |
| 74 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 75 | } |
| 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 | public function TroubleshootingRouteCallback($request) |
| 83 | { |
| 84 | if (!isset($request['action'])) { |
| 85 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 86 | } |
| 87 | switch ($request['action']) { |
| 88 | case 'wordpressversion': |
| 89 | return $this->WordPressVersionCallback(); |
| 90 | case 'elementorversion': |
| 91 | return $this->ElementorVersionCallback(); |
| 92 | case 'connection': |
| 93 | return $this->ConnectionCheckCallback(); |
| 94 | case 'domainshift': |
| 95 | return $this->DomainShiftCallback(); |
| 96 | case 'service': |
| 97 | return $this->ServiceCheckCallback(); |
| 98 | case 'keycheck': |
| 99 | return $this->KeyCheckCallback(); |
| 100 | case 'keyverify': |
| 101 | return $this->KeyVerifyCallback(); |
| 102 | case 'cacheclear': |
| 103 | return $this->CacheClearCallback(); |
| 104 | default: |
| 105 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private function WordPressVersionCallback() |
| 110 | { |
| 111 | try { |
| 112 | $is_compatible = GutenbergController::is_compatible(); |
| 113 | |
| 114 | return rest_ensure_response(['success' => $is_compatible]); |
| 115 | } catch (Exception $ex) { |
| 116 | LogController::HandleException($ex); |
| 117 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private function ElementorVersionCallback() |
| 122 | { |
| 123 | try { |
| 124 | $is_compatible = ElementorController::is_compatible(); |
| 125 | |
| 126 | return rest_ensure_response(['success' => $is_compatible]); |
| 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 ConnectionCheckCallback() |
| 134 | { |
| 135 | try { |
| 136 | $is_connected = DomainShiftController::GetCurrentConnectionSuccess(); |
| 137 | |
| 138 | return rest_ensure_response(['success' => $is_connected]); |
| 139 | } catch (Exception $ex) { |
| 140 | LogController::HandleException($ex); |
| 141 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private function DomainShiftCallback() |
| 146 | { |
| 147 | try { |
| 148 | $successful_shift = DomainShiftController::FindPreferredAPIDomain(); |
| 149 | |
| 150 | return rest_ensure_response(['success' => $successful_shift]); |
| 151 | } catch (Exception $ex) { |
| 152 | LogController::HandleException($ex); |
| 153 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private function ServiceCheckCallback() |
| 158 | { |
| 159 | try { |
| 160 | $status_arr = DomainShiftController::GetServiceStatus(); |
| 161 | if ($status_arr['online']) { |
| 162 | return rest_ensure_response(['success' => true]); |
| 163 | } |
| 164 | |
| 165 | return rest_ensure_response(['success' => false, "text" => esc_html($status_arr['message'])]); |
| 166 | } catch (Exception $ex) { |
| 167 | LogController::HandleException($ex); |
| 168 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private function KeyCheckCallback() |
| 173 | { |
| 174 | try { |
| 175 | $keytype_label = KeyController::GetCurrentKeyTypeLabel(); |
| 176 | if (!KeyController::HasRegisteredKey()) { |
| 177 | // No need to check for key status if no key is registered. |
| 178 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label)]); |
| 179 | } |
| 180 | |
| 181 | $keyinfo = KeyController::GetKeyStatus(); |
| 182 | |
| 183 | if ($keyinfo['expired']) { |
| 184 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key Expired', 'superbaddons')]); |
| 185 | } |
| 186 | |
| 187 | if (!$keyinfo['active']) { |
| 188 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key Disabled. Please contact our support team for assistance.', 'superbaddons')]); |
| 189 | } |
| 190 | |
| 191 | if (!$keyinfo['verified']) { |
| 192 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Key Verification Invalid', 'superbaddons')]); |
| 193 | } |
| 194 | |
| 195 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label)]); |
| 196 | } catch (KeyException $k_ex) { |
| 197 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 198 | } catch (Exception $ex) { |
| 199 | LogController::HandleException($ex); |
| 200 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | private function KeyVerifyCallback() |
| 206 | { |
| 207 | try { |
| 208 | $keyinfo = KeyController::GetUpdatedLicenseKeyInformation(); |
| 209 | |
| 210 | if ($keyinfo['expired'] || !$keyinfo['active'] || !$keyinfo['verified']) { |
| 211 | return rest_ensure_response(['success' => false]); |
| 212 | } |
| 213 | $keytype_label = KeyController::GetKeyTypeLabel($keyinfo['type']); |
| 214 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label . ' ' . __('Verified', 'superbaddons'))]); |
| 215 | } catch (KeyException $k_ex) { |
| 216 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 217 | } catch (Exception $ex) { |
| 218 | LogController::HandleException($ex); |
| 219 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private function CacheClearCallback() |
| 224 | { |
| 225 | try { |
| 226 | $cleared = CacheController::ClearCache(CacheOptions::SERVICE_VERSION) && CacheController::ClearCache(ElementorCache::SECTIONS); |
| 227 | |
| 228 | return rest_ensure_response(['success' => $cleared]); |
| 229 | } catch (Exception $ex) { |
| 230 | LogController::HandleException($ex); |
| 231 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 |