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