Settings.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Settings; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\BackgroundProcessing\Queue; |
| 7 | use WPStaging\Framework\SiteInfo; |
| 8 | use WPStaging\Framework\Utils\Sanitize; |
| 9 | use WPStaging\Backup\BackupScheduler; |
| 10 | use WPStaging\Framework\Security\Auth; |
| 11 | |
| 12 | class Settings |
| 13 | { |
| 14 | /** @var string */ |
| 15 | const ACTION_WPSTG_PRO_SETTINGS = 'wpstg.views.pro.settings'; |
| 16 | |
| 17 | /** |
| 18 | * @var array |
| 19 | * Sanitize the options to escape from XSS |
| 20 | */ |
| 21 | private $optionsToSanitize = [ |
| 22 | 'queryLimit' => 'sanitizeInt', |
| 23 | 'querySRLimit' => 'sanitizeInt', |
| 24 | 'fileLimit' => 'sanitizeInt', |
| 25 | 'maxFileSize' => 'sanitizeInt', |
| 26 | 'batchSize' => 'sanitizeInt', |
| 27 | 'delayRequest' => 'sanitizeInt', |
| 28 | 'cpuLoad' => 'sanitizeString', |
| 29 | 'unInstallOnDelete' => 'sanitizeBool', |
| 30 | 'optimizer' => 'sanitizeBool', |
| 31 | 'disableAdminLogin' => 'sanitizeBool', |
| 32 | 'keepPermalinks' => 'sanitizeBool', |
| 33 | 'checkDirectorySize' => 'sanitizeBool', |
| 34 | 'debugMode' => 'sanitizeBool', |
| 35 | 'schedulesErrorReport' => 'sanitizeBool', |
| 36 | 'schedulesReportEmail' => 'sanitizeEmail', |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * @var SiteInfo |
| 41 | */ |
| 42 | private $siteInfo; |
| 43 | |
| 44 | /** |
| 45 | * @var Sanitize |
| 46 | */ |
| 47 | private $sanitize; |
| 48 | |
| 49 | /** @var Queue */ |
| 50 | private $queue; |
| 51 | |
| 52 | /** @var Auth */ |
| 53 | private $auth; |
| 54 | |
| 55 | /** |
| 56 | * @param SiteInfo $siteInfo |
| 57 | * @param Sanitize $sanitize |
| 58 | * @param Queue $queue |
| 59 | * @param Auth $auth |
| 60 | */ |
| 61 | public function __construct(SiteInfo $siteInfo, Sanitize $sanitize, Queue $queue, Auth $auth) |
| 62 | { |
| 63 | $this->siteInfo = $siteInfo; |
| 64 | $this->sanitize = $sanitize; |
| 65 | $this->queue = $queue; |
| 66 | $this->auth = $auth; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return void |
| 71 | */ |
| 72 | public function registerSettings() |
| 73 | { |
| 74 | register_setting("wpstg_settings", "wpstg_settings", [$this, "sanitizeOptions"]); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sanitize options data and delete the cache |
| 79 | * @param array $data |
| 80 | * @return array |
| 81 | */ |
| 82 | public function sanitizeOptions(array $data = []): array |
| 83 | { |
| 84 | // is_array() is required otherwise new clone will fail. |
| 85 | $showErrorToggleStagingSiteCloning = false; |
| 86 | if ($this->siteInfo->isStagingSite() && is_array($data)) { |
| 87 | $isStagingCloneable = isset($data['isStagingSiteCloneable']) ? $data['isStagingSiteCloneable'] : 'false'; |
| 88 | unset($data['isStagingSiteCloneable']); |
| 89 | $showErrorToggleStagingSiteCloning = !$this->toggleStagingSiteCloning($isStagingCloneable === 'true'); |
| 90 | } |
| 91 | |
| 92 | if (WPStaging::isPro() && is_array($data)) { |
| 93 | $isSendBackupErrorReport = isset($data['schedulesErrorReport']) ? 'true' : ''; |
| 94 | $reportEmail = !empty($data['schedulesReportEmail']) ? $this->sanitize->sanitizeEmail($data['schedulesReportEmail']) : ''; |
| 95 | unset($data['schedulesErrorReport']); |
| 96 | $this->setEmailErrorReportOptions($isSendBackupErrorReport, $reportEmail); |
| 97 | } |
| 98 | |
| 99 | $sanitized = $this->sanitizeData($data); |
| 100 | |
| 101 | if ($showErrorToggleStagingSiteCloning) { |
| 102 | add_settings_error("wpstg-notices", '', __("Settings updated. But unable to activate/deactivate the site cloneable status!", "wp-staging"), "warning"); |
| 103 | } else { |
| 104 | add_settings_error("wpstg-notices", '', __("Settings updated.", "wp-staging"), "updated"); |
| 105 | } |
| 106 | |
| 107 | return apply_filters("wpstg-settings", $sanitized, $data); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return null |
| 112 | */ |
| 113 | public function ajaxPurgeQueueTable() |
| 114 | { |
| 115 | if ($this->auth->isAuthenticatedRequest() === false) { |
| 116 | wp_send_json([ |
| 117 | 'success' => false, |
| 118 | 'message' => esc_html__('Error 403: Unauthorized Request', 'wp-staging') |
| 119 | ]); |
| 120 | } |
| 121 | |
| 122 | $result = $this->queue->purgeQueueTable(); |
| 123 | |
| 124 | if ($result === false) { |
| 125 | wp_send_json([ |
| 126 | 'success' => false, |
| 127 | 'message' => esc_html__('Unable to purge queue table', 'wp-staging') |
| 128 | ]); |
| 129 | } |
| 130 | |
| 131 | if ($result === 0) { |
| 132 | wp_send_json([ |
| 133 | 'success' => true, |
| 134 | 'message' => sprintf(esc_html__('Table %s is already empty.', 'wp-staging'), esc_html($this->queue->getTableName())) |
| 135 | ]); |
| 136 | } |
| 137 | |
| 138 | wp_send_json([ |
| 139 | 'success' => true, |
| 140 | 'message' => sprintf(esc_html__('Purged queue table! Removed %s action(s)', 'wp-staging'), esc_html($result)) |
| 141 | ]); |
| 142 | |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param array $data |
| 148 | * @return array |
| 149 | */ |
| 150 | protected function sanitizeData(array $data = []): array |
| 151 | { |
| 152 | $sanitized = []; |
| 153 | |
| 154 | foreach ($data as $key => $value) { |
| 155 | if (is_array($value)) { |
| 156 | $sanitized[$key] = $this->sanitizeData($value); |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | $val = htmlspecialchars($value); |
| 161 | if (array_key_exists($key, $this->optionsToSanitize)) { |
| 162 | $sanitizeMethod = $this->optionsToSanitize[$key]; |
| 163 | $val = $this->sanitize->$sanitizeMethod($val); |
| 164 | } |
| 165 | |
| 166 | $sanitized[$key] = wp_filter_nohtml_kses($val); |
| 167 | } |
| 168 | |
| 169 | return $sanitized; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Toggle staging site cloning |
| 174 | * |
| 175 | * @param bool $isCloneable |
| 176 | * |
| 177 | * @return bool |
| 178 | */ |
| 179 | protected function toggleStagingSiteCloning(bool $isCloneable): bool |
| 180 | { |
| 181 | if ($isCloneable && $this->siteInfo->enableStagingSiteCloning()) { |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | if (!$isCloneable && $this->siteInfo->disableStagingSiteCloning()) { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Set backup schedule error reporting options |
| 194 | * |
| 195 | * @param string $isSendBackupSchedulesErrorReport 'true' if active |
| 196 | * @param string $reportEmail |
| 197 | * @return void |
| 198 | */ |
| 199 | protected function setEmailErrorReportOptions(string $isSendBackupSchedulesErrorReport, string $reportEmail) |
| 200 | { |
| 201 | if (!WPStaging::isPro()) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (!class_exists('WPStaging\Backup\BackupScheduler')) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_ERROR_REPORT, $isSendBackupSchedulesErrorReport); |
| 210 | |
| 211 | update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_REPORT_EMAIL, $reportEmail); |
| 212 | } |
| 213 | } |
| 214 |