class-dashboard-controller.php
2 years ago
class-settings-controller.php
2 years ago
class-troubleshooting-controller.php
2 years ago
class-settings-controller.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Config\Capabilities; |
| 8 | use SuperbAddons\Data\Controllers\CacheController; |
| 9 | use SuperbAddons\Data\Controllers\KeyController; |
| 10 | use SuperbAddons\Data\Controllers\OptionController; |
| 11 | use SuperbAddons\Data\Controllers\RestController; |
| 12 | use SuperbAddons\Data\Controllers\SettingsOptionKey; |
| 13 | use Exception; |
| 14 | use SuperbAddons\Data\Controllers\LogController; |
| 15 | use SuperbAddons\Data\Utils\KeyException; |
| 16 | use SuperbAddons\Data\Utils\PluginInstaller; |
| 17 | use SuperbAddons\Data\Utils\SettingsException; |
| 18 | |
| 19 | |
| 20 | class SettingsController |
| 21 | { |
| 22 | const SETTINGS_ROUTE = '/settings'; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | RestController::AddRoute(self::SETTINGS_ROUTE, array( |
| 27 | 'methods' => 'POST', |
| 28 | 'permission_callback' => array($this, 'SettingsCallbackPermissionCheck'), |
| 29 | 'callback' => array($this, 'SettingsRouteCallback'), |
| 30 | )); |
| 31 | } |
| 32 | |
| 33 | public function SettingsCallbackPermissionCheck() |
| 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 SettingsRouteCallback($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 'submit_feedback': |
| 50 | return $this->SubmitFeedbackCallback(); |
| 51 | case 'addkey': |
| 52 | return $this->RegisterKeyCallback($request); |
| 53 | case 'removekey': |
| 54 | return $this->RemoveKeyCallback(); |
| 55 | case 'getelementor': |
| 56 | return $this->InstallElementorCallback(); |
| 57 | case 'toggle_logs': |
| 58 | case 'toggle_share_logs': |
| 59 | case 'clear_cache': |
| 60 | case 'clear_logs': |
| 61 | case 'view_logs': |
| 62 | return $this->SaveSettingsCallback($request['action']); |
| 63 | default: |
| 64 | return new \WP_Error('bad_request_plugin', 'Bad Plugin Request', array('status' => 400)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private function SubmitFeedbackCallback() |
| 69 | { |
| 70 | try { |
| 71 | if (!isset($_POST['spbaddons_reason']) || empty($_POST['spbaddons_reason'])) throw new SettingsException(__('Unable to send feedback. No feedback provided.', 'superbaddons')); |
| 72 | |
| 73 | $message = $_POST['spbaddons_reason'] === 'other' ? $_POST['spbaddons_other'] : $_POST['spbaddons_reason']; |
| 74 | LogController::SendFeedback($message); |
| 75 | |
| 76 | return rest_ensure_response(['success' => true]); |
| 77 | } catch (SettingsException $s_ex) { |
| 78 | return rest_ensure_response(['success' => false, "text" => esc_html($s_ex->getMessage())]); |
| 79 | } catch (Exception $ex) { |
| 80 | LogController::HandleException($ex); |
| 81 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private function RegisterKeyCallback($request) |
| 86 | { |
| 87 | try { |
| 88 | KeyController::RegisterKey($request['key'], true); |
| 89 | return rest_ensure_response(['success' => true]); |
| 90 | } catch (KeyException $k_ex) { |
| 91 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 92 | } catch (Exception $ex) { |
| 93 | LogController::HandleException($ex); |
| 94 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private function RemoveKeyCallback() |
| 99 | { |
| 100 | try { |
| 101 | $removed = KeyController::RemoveKey(); |
| 102 | return rest_ensure_response(['success' => $removed]); |
| 103 | } catch (KeyException $k_ex) { |
| 104 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 105 | } catch (Exception $ex) { |
| 106 | LogController::HandleException($ex); |
| 107 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private function InstallElementorCallback() |
| 112 | { |
| 113 | try { |
| 114 | $installed = PluginInstaller::Install('elementor'); |
| 115 | if (!$installed) { |
| 116 | return rest_ensure_response(['success' => false, "text" => esc_html__('An error occured. Elementor could not be installed.', 'superbaddons')]); |
| 117 | } |
| 118 | |
| 119 | return rest_ensure_response(['success' => true]); |
| 120 | } catch (KeyException $k_ex) { |
| 121 | return rest_ensure_response(['success' => false, "text" => esc_html($k_ex->getMessage())]); |
| 122 | } catch (Exception $ex) { |
| 123 | LogController::HandleException($ex); |
| 124 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public static function GetSettings() |
| 129 | { |
| 130 | return OptionController::GetSettings(); |
| 131 | } |
| 132 | |
| 133 | private function SaveSettingsCallback($action) |
| 134 | { |
| 135 | try { |
| 136 | $option_controller = new OptionController(); |
| 137 | $current_settings = OptionController::GetSettings(); |
| 138 | |
| 139 | switch ($action) { |
| 140 | case 'toggle_logs': |
| 141 | $current_settings[SettingsOptionKey::LOGS_ENABLED] = !$current_settings[SettingsOptionKey::LOGS_ENABLED]; |
| 142 | $option_controller->SaveSettings($current_settings); |
| 143 | break; |
| 144 | case 'toggle_share_logs': |
| 145 | $current_settings[SettingsOptionKey::LOG_SHARE_ENABLED] = !$current_settings[SettingsOptionKey::LOG_SHARE_ENABLED]; |
| 146 | $saved = $option_controller->SaveSettings($current_settings); |
| 147 | if ($saved) { |
| 148 | $current_settings[SettingsOptionKey::LOG_SHARE_ENABLED] ? LogController::MaybeSubscribeCron() : LogController::MaybeUnsubscribeCron(); |
| 149 | } |
| 150 | break; |
| 151 | case 'clear_cache': |
| 152 | $cleared = CacheController::ClearCacheAll(); |
| 153 | if (!$cleared) throw new SettingsException(__('Cache could not be cleared.', 'superbaddons')); |
| 154 | break; |
| 155 | case 'clear_logs': |
| 156 | $cleared = LogController::ClearLogs(); |
| 157 | if (!$cleared) throw new SettingsException(__('Logs could not be cleared.', 'superbaddons')); |
| 158 | break; |
| 159 | case 'view_logs': |
| 160 | return rest_ensure_response(['success' => true, 'content' => LogController::GetLogs()]); |
| 161 | } |
| 162 | |
| 163 | return rest_ensure_response(['success' => true]); |
| 164 | } catch (SettingsException $s_ex) { |
| 165 | return rest_ensure_response(['success' => false, "text" => esc_html($s_ex->getMessage())]); |
| 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 | |
| 173 | class SettingInputKey |
| 174 | { |
| 175 | const ENABLE_LOGS = 'superbaddons_enable_logs'; |
| 176 | const SHARE_LOGS = 'superbaddons_share_logs'; |
| 177 | } |
| 178 |