wizard
1 month ago
class-dashboard-controller.php
1 month ago
class-license-resolve-controller.php
1 month ago
class-newsletter-signup-controller.php
1 month ago
class-notice-controller.php
1 month ago
class-plugin-reset-controller.php
1 month ago
class-rewrite-check-controller.php
1 month ago
class-settings-controller.php
1 month ago
class-troubleshooting-controller.php
1 month ago
class-troubleshooting-controller.php
294 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\Data\Utils\KeyType; |
| 14 | use SuperbAddons\Tours\Controllers\TourController; |
| 15 | |
| 16 | defined('ABSPATH') || exit(); |
| 17 | |
| 18 | class TroubleshootingController |
| 19 | { |
| 20 | const TROUBLESHOOTING_ROUTE = '/troubleshooting'; |
| 21 | const TUTORIAL_ROUTE = '/tutorial'; |
| 22 | |
| 23 | const ENDPOINT_BASE = 'addons-status/'; |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | RestController::AddRoute(self::TROUBLESHOOTING_ROUTE, array( |
| 28 | 'methods' => 'POST', |
| 29 | 'permission_callback' => array($this, 'TroubleshootingCallbackPermissionCheck'), |
| 30 | 'callback' => array($this, 'TroubleshootingRouteCallback'), |
| 31 | )); |
| 32 | RestController::AddRoute(self::TUTORIAL_ROUTE, array( |
| 33 | 'methods' => 'POST', |
| 34 | 'permission_callback' => array($this, 'TutorialCallbackPermissionCheck'), |
| 35 | 'callback' => array($this, 'TutorialRouteCallback'), |
| 36 | )); |
| 37 | } |
| 38 | |
| 39 | public function TroubleshootingCallbackPermissionCheck() |
| 40 | { |
| 41 | // Restrict endpoint to only users who have the proper capability. |
| 42 | if (!current_user_can(Capabilities::ADMIN)) { |
| 43 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401)); |
| 44 | } |
| 45 | |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | public function TutorialCallbackPermissionCheck() |
| 50 | { |
| 51 | // Restrict endpoint to only users who have the proper capability. |
| 52 | if (!current_user_can(Capabilities::CONTRIBUTOR)) { |
| 53 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401)); |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | public function TutorialRouteCallback($request) |
| 60 | { |
| 61 | if (!isset($request['action'])) { |
| 62 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 63 | } |
| 64 | try { |
| 65 | switch ($request['action']) { |
| 66 | case 'elementor-tour': |
| 67 | $url = TourController::GetElementorTourURL(); |
| 68 | return rest_ensure_response(['success' => true, 'url' => esc_url_raw($url)]); |
| 69 | case 'cleanup-elementor-tour-page': |
| 70 | $removed = TourController::CleanUpTourPage($request['tour-nonce']); |
| 71 | return rest_ensure_response(['success' => $removed]); |
| 72 | case 'mark-tour-complete': |
| 73 | $tour = isset($request['tour']) ? sanitize_text_field($request['tour']) : ''; |
| 74 | $allowed = array(TourController::TOUR_DASHBOARD_WELCOME_META, TourController::TOUR_BLOCK_THEME_META); |
| 75 | if (!in_array($tour, $allowed, true)) { |
| 76 | return new \WP_Error('bad_request_plugin', 'Invalid tour', array('status' => 400)); |
| 77 | } |
| 78 | TourController::MarkTourCompleted($tour); |
| 79 | return rest_ensure_response(array('success' => true)); |
| 80 | default: |
| 81 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 82 | } |
| 83 | } catch (Exception $ex) { |
| 84 | LogController::HandleException($ex); |
| 85 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function TroubleshootingRouteCallback($request) |
| 90 | { |
| 91 | if (!isset($request['action'])) { |
| 92 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 93 | } |
| 94 | switch ($request['action']) { |
| 95 | case 'restcheck': |
| 96 | return $this->RestCheckCallback(); |
| 97 | case 'restfix': |
| 98 | return $this->RestFixCallback(); |
| 99 | case 'connection': |
| 100 | return $this->ConnectionCheckCallback(); |
| 101 | case 'domainshift': |
| 102 | return $this->DomainShiftCallback(); |
| 103 | case 'service': |
| 104 | return $this->ServiceCheckCallback(); |
| 105 | case 'keycheck': |
| 106 | return $this->KeyCheckCallback(); |
| 107 | case 'keyverify': |
| 108 | return $this->KeyVerifyCallback(); |
| 109 | case 'cacheclear': |
| 110 | return $this->CacheClearCallback(); |
| 111 | default: |
| 112 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private function RestCheckCallback() |
| 117 | { |
| 118 | try { |
| 119 | // If we reached this endpoint, the REST API is at least partially working. |
| 120 | // Check if rewrite rules are properly configured. |
| 121 | $permalink_structure = get_option('permalink_structure'); |
| 122 | if (empty($permalink_structure)) { |
| 123 | // Plain permalinks — REST API uses ?rest_route= which always works. |
| 124 | return rest_ensure_response(array('success' => true)); |
| 125 | } |
| 126 | |
| 127 | $rules = get_option('rewrite_rules'); |
| 128 | if (empty($rules) || !is_array($rules)) { |
| 129 | return rest_ensure_response(array( |
| 130 | 'success' => false, |
| 131 | 'requiresConsent' => true, |
| 132 | 'text' => esc_html__('WordPress rewrite rules are not configured', 'superb-blocks'), |
| 133 | )); |
| 134 | } |
| 135 | |
| 136 | $rest_prefix = rest_get_url_prefix(); |
| 137 | foreach ($rules as $pattern => $query) { |
| 138 | if (strpos($pattern, $rest_prefix) !== false) { |
| 139 | return rest_ensure_response(array('success' => true)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return rest_ensure_response(array( |
| 144 | 'success' => false, |
| 145 | 'requiresConsent' => true, |
| 146 | 'text' => esc_html__('REST API rewrite rules are missing', 'superb-blocks'), |
| 147 | )); |
| 148 | } catch (Exception $ex) { |
| 149 | LogController::HandleException($ex); |
| 150 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private function RestFixCallback() |
| 155 | { |
| 156 | try { |
| 157 | flush_rewrite_rules(); |
| 158 | |
| 159 | // Verify the fix worked |
| 160 | $rules = get_option('rewrite_rules'); |
| 161 | if (!empty($rules) && is_array($rules)) { |
| 162 | $rest_prefix = rest_get_url_prefix(); |
| 163 | foreach ($rules as $pattern => $query) { |
| 164 | if (strpos($pattern, $rest_prefix) !== false) { |
| 165 | RewriteCheckController::ClearIssue(); |
| 166 | return rest_ensure_response(array('success' => true)); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return rest_ensure_response(array('success' => false)); |
| 172 | } catch (Exception $ex) { |
| 173 | LogController::HandleException($ex); |
| 174 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private function ConnectionCheckCallback() |
| 179 | { |
| 180 | try { |
| 181 | $is_connected = DomainShiftController::GetCurrentConnectionSuccess(); |
| 182 | |
| 183 | return rest_ensure_response(['success' => $is_connected]); |
| 184 | } catch (Exception $ex) { |
| 185 | LogController::HandleException($ex); |
| 186 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private function DomainShiftCallback() |
| 191 | { |
| 192 | try { |
| 193 | $successful_shift = DomainShiftController::FindPreferredAPIDomain(); |
| 194 | |
| 195 | return rest_ensure_response(['success' => $successful_shift]); |
| 196 | } catch (Exception $ex) { |
| 197 | LogController::HandleException($ex); |
| 198 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private function ServiceCheckCallback() |
| 203 | { |
| 204 | try { |
| 205 | $status_arr = DomainShiftController::GetServiceStatus(); |
| 206 | if ($status_arr['online']) { |
| 207 | return rest_ensure_response(['success' => true]); |
| 208 | } |
| 209 | |
| 210 | return rest_ensure_response(['success' => false, "text" => esc_html($status_arr['message'])]); |
| 211 | } catch (Exception $ex) { |
| 212 | LogController::HandleException($ex); |
| 213 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private function KeyCheckCallback() |
| 218 | { |
| 219 | try { |
| 220 | $keytype_label = KeyController::GetCurrentKeyTypeLabel(); |
| 221 | if (!KeyController::HasRegisteredKey()) { |
| 222 | // No need to check for key status if no key is registered. |
| 223 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label)]); |
| 224 | } |
| 225 | |
| 226 | $keyinfo = KeyController::GetKeyStatus(); |
| 227 | |
| 228 | if ($keyinfo['expired']) { |
| 229 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('Subscription Expired', "superb-blocks")]); |
| 230 | } |
| 231 | |
| 232 | if (!$keyinfo['active']) { |
| 233 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key Disabled. Please contact our support team for assistance.', "superb-blocks")]); |
| 234 | } |
| 235 | |
| 236 | if (!$keyinfo['verified']) { |
| 237 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Key Verification Invalid', "superb-blocks")]); |
| 238 | } |
| 239 | |
| 240 | if ($keyinfo['exceeded']) { |
| 241 | return rest_ensure_response(['success' => false, "ignoreResolver" => false, "text" => esc_html__('License Key active on too many domains. Please contact our support team for assistance.', "superb-blocks")]); |
| 242 | } |
| 243 | |
| 244 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label)]); |
| 245 | } catch (KeyException $k_ex) { |
| 246 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 247 | } catch (Exception $ex) { |
| 248 | LogController::HandleException($ex); |
| 249 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | private function KeyVerifyCallback() |
| 255 | { |
| 256 | try { |
| 257 | $keyinfo = KeyController::GetUpdatedLicenseKeyInformation(); |
| 258 | if (($keyinfo['expired'] && $keyinfo['type'] !== KeyType::STANDARD) || !$keyinfo['active'] || !$keyinfo['verified'] || $keyinfo['exceeded']) { |
| 259 | if ($keyinfo['expired'] && $keyinfo['type'] !== KeyType::STANDARD) { |
| 260 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Subscription Expired', "superb-blocks")]); |
| 261 | } |
| 262 | |
| 263 | if ($keyinfo['exceeded']) { |
| 264 | if ($keyinfo['expired']) { |
| 265 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Key active on too many domains. Please renew your subscription, deactivate your license key on some of your domains, or contact our support team for assistance.', "superb-blocks")]); |
| 266 | } |
| 267 | return rest_ensure_response(['success' => false, "text" => esc_html__('License Key active on too many domains. Please contact our support team for assistance.', "superb-blocks")]); |
| 268 | } |
| 269 | |
| 270 | return rest_ensure_response(['success' => false]); |
| 271 | } |
| 272 | $keytype_label = KeyController::GetKeyTypeLabel($keyinfo['type']); |
| 273 | return rest_ensure_response(['success' => true, "text" => esc_html($keytype_label . ' ' . __('Verified', "superb-blocks"))]); |
| 274 | } catch (KeyException $k_ex) { |
| 275 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 276 | } catch (Exception $ex) { |
| 277 | LogController::HandleException($ex); |
| 278 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | private function CacheClearCallback() |
| 283 | { |
| 284 | try { |
| 285 | $cleared = CacheController::ClearCache(CacheOptions::SERVICE_VERSION) && CacheController::ClearCache(ElementorCache::SECTIONS); |
| 286 | |
| 287 | return rest_ensure_response(['success' => $cleared]); |
| 288 | } catch (Exception $ex) { |
| 289 | LogController::HandleException($ex); |
| 290 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 |