PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Settings / Settings.php
wp-staging / Framework / Settings Last commit date
DarkMode.php 2 years ago EventLogger.php 1 year ago Settings.php 2 years ago
Settings.php
230 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 ];
36
37 /**
38 * @var SiteInfo
39 */
40 private $siteInfo;
41
42 /**
43 * @var Sanitize
44 */
45 private $sanitize;
46
47 /** @var Queue */
48 private $queue;
49
50 /** @var Auth */
51 private $auth;
52
53 /**
54 * @param SiteInfo $siteInfo
55 * @param Sanitize $sanitize
56 * @param Queue $queue
57 * @param Auth $auth
58 */
59 public function __construct(SiteInfo $siteInfo, Sanitize $sanitize, Queue $queue, Auth $auth)
60 {
61 $this->siteInfo = $siteInfo;
62 $this->sanitize = $sanitize;
63 $this->queue = $queue;
64 $this->auth = $auth;
65 }
66
67 /**
68 * @return void
69 */
70 public function registerSettings()
71 {
72 register_setting("wpstg_settings", "wpstg_settings", [$this, "sanitizeOptions"]);
73 }
74
75 /**
76 * Sanitize options data and delete the cache
77 * @param array $data
78 * @return array
79 */
80 public function sanitizeOptions(array $data = []): array
81 {
82 // is_array() is required otherwise new clone will fail.
83 $showErrorToggleStagingSiteCloning = false;
84 if ($this->siteInfo->isStagingSite() && is_array($data)) {
85 $isStagingCloneable = isset($data['isStagingSiteCloneable']) ? $data['isStagingSiteCloneable'] : 'false';
86 unset($data['isStagingSiteCloneable']);
87 $showErrorToggleStagingSiteCloning = !$this->toggleStagingSiteCloning($isStagingCloneable === 'true');
88 }
89
90 if (is_array($data)) {
91 $optionBackupScheduleErrorReport = isset($data['schedulesErrorReport']) ? 'true' : '';
92 $optionBackupScheduleReportEmail = !empty($data['schedulesReportEmail']) ? $this->sanitize->sanitizeEmail($data['schedulesReportEmail']) : '';
93
94 if (empty($optionBackupScheduleReportEmail)) {
95 $optionBackupScheduleErrorReport = '';
96 }
97
98 unset($data['schedulesErrorReport'], $data['schedulesReportEmail']);
99
100 $optionBackupScheduleSlackErrorReport = isset($data['schedulesSlackErrorReport']) ? 'true' : '';
101 $optionBackupScheduleReportSlackWebhook = !empty($data['schedulesReportSlackWebhook']) ? $this->sanitize->sanitizeUrl($data['schedulesReportSlackWebhook']) : '';
102
103 if (empty($optionBackupScheduleReportSlackWebhook)) {
104 $optionBackupScheduleSlackErrorReport = '';
105 }
106
107 unset($data['schedulesErrorSlackReport'], $data['schedulesReportSlackWebhook']);
108
109 $this->setErrorReportOptions($optionBackupScheduleErrorReport, $optionBackupScheduleReportEmail, $optionBackupScheduleSlackErrorReport, $optionBackupScheduleReportSlackWebhook);
110 }
111
112 $sanitized = $this->sanitizeData($data);
113
114 if ($showErrorToggleStagingSiteCloning) {
115 add_settings_error("wpstg-notices", '', __("Settings updated. But unable to activate/deactivate the site cloneable status!", "wp-staging"), "warning");
116 } else {
117 add_settings_error("wpstg-notices", '', __("Settings updated.", "wp-staging"), "updated");
118 }
119
120 return $sanitized;
121 }
122
123 /**
124 * @return null
125 */
126 public function ajaxPurgeQueueTable()
127 {
128 if ($this->auth->isAuthenticatedRequest() === false) {
129 wp_send_json([
130 'success' => false,
131 'message' => esc_html__('Error 403: Unauthorized Request', 'wp-staging')
132 ]);
133 }
134
135 $result = $this->queue->purgeQueueTable();
136
137 if ($result === false) {
138 wp_send_json([
139 'success' => false,
140 'message' => esc_html__('Unable to purge queue table', 'wp-staging')
141 ]);
142 }
143
144 if ($result === 0) {
145 wp_send_json([
146 'success' => true,
147 'message' => sprintf(esc_html__('Table %s is already empty.', 'wp-staging'), esc_html($this->queue->getTableName()))
148 ]);
149 }
150
151 wp_send_json([
152 'success' => true,
153 'message' => sprintf(esc_html__('Purged queue table! Removed %s action(s)', 'wp-staging'), esc_html($result))
154 ]);
155
156 return null;
157 }
158
159 /**
160 * @param array $data
161 * @return array
162 */
163 protected function sanitizeData(array $data = []): array
164 {
165 $sanitized = [];
166
167 foreach ($data as $key => $value) {
168 if (is_array($value)) {
169 $sanitized[$key] = $this->sanitizeData($value);
170 continue;
171 }
172
173 $val = htmlspecialchars($value);
174 if (array_key_exists($key, $this->optionsToSanitize)) {
175 $sanitizeMethod = $this->optionsToSanitize[$key];
176 $val = $this->sanitize->$sanitizeMethod($val);
177 }
178
179 $sanitized[$key] = wp_filter_nohtml_kses($val);
180 }
181
182 return $sanitized;
183 }
184
185 /**
186 * Toggle staging site cloning
187 *
188 * @param bool $isCloneable
189 *
190 * @return bool
191 */
192 protected function toggleStagingSiteCloning(bool $isCloneable): bool
193 {
194 if ($isCloneable && $this->siteInfo->enableStagingSiteCloning()) {
195 return true;
196 }
197
198 if (!$isCloneable && $this->siteInfo->disableStagingSiteCloning()) {
199 return true;
200 }
201
202 return false;
203 }
204
205 /**
206 * Set backup schedule error reporting options
207 *
208 * @param string $optionBackupScheduleErrorReport 'true' if active
209 * @param string $optionBackupScheduleReportEmail
210 * @param string $optionBackupScheduleSlackErrorReport 'true' if active
211 * @param string $optionBackupScheduleReportSlackWebhook
212 * @return void
213 */
214 protected function setErrorReportOptions(
215 string $optionBackupScheduleErrorReport,
216 string $optionBackupScheduleReportEmail,
217 string $optionBackupScheduleSlackErrorReport,
218 string $optionBackupScheduleReportSlackWebhook
219 ) {
220 if (!class_exists('WPStaging\Backup\BackupScheduler')) {
221 return;
222 }
223
224 update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_ERROR_REPORT, $optionBackupScheduleErrorReport);
225 update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_REPORT_EMAIL, $optionBackupScheduleReportEmail);
226 update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_SLACK_ERROR_REPORT, $optionBackupScheduleSlackErrorReport);
227 update_option(BackupScheduler::OPTION_BACKUP_SCHEDULE_REPORT_SLACK_WEBHOOK, $optionBackupScheduleReportSlackWebhook);
228 }
229 }
230