PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Controllers / ToolsController.php

ToolsController.php in Site Reviews trunk, at plugin/Controllers/ToolsController.php

438 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Controllers;
4
5 use GeminiLabs\SiteReviews\Commands\ChangeLogLevel;
6 use GeminiLabs\SiteReviews\Commands\ClearConsole;
7 use GeminiLabs\SiteReviews\Commands\ConfigureIpAddressProxy;
8 use GeminiLabs\SiteReviews\Commands\ConvertTableEngine;
9 use GeminiLabs\SiteReviews\Commands\DetectIpAddress;
10 use GeminiLabs\SiteReviews\Commands\DownloadCsvTemplate;
11 use GeminiLabs\SiteReviews\Commands\ExportReviews;
12 use GeminiLabs\SiteReviews\Commands\GeolocateReviews;
13 use GeminiLabs\SiteReviews\Commands\ImportReviews;
14 use GeminiLabs\SiteReviews\Commands\ImportReviewsAttachments;
15 use GeminiLabs\SiteReviews\Commands\ImportReviewsCleanup;
16 use GeminiLabs\SiteReviews\Commands\ImportSettings;
17 use GeminiLabs\SiteReviews\Commands\MigratePlugin;
18 use GeminiLabs\SiteReviews\Commands\ProcessCsvFile;
19 use GeminiLabs\SiteReviews\Commands\RemoveLocationData;
20 use GeminiLabs\SiteReviews\Commands\RepairPermissions;
21 use GeminiLabs\SiteReviews\Commands\RepairReviewRelations;
22 use GeminiLabs\SiteReviews\Commands\ResetAssignedMeta;
23 use GeminiLabs\SiteReviews\Database\OptionManager;
24 use GeminiLabs\SiteReviews\Helpers\Cast;
25 use GeminiLabs\SiteReviews\Modules\Console;
26 use GeminiLabs\SiteReviews\Modules\Html\Builder;
27 use GeminiLabs\SiteReviews\Modules\Migrate;
28 use GeminiLabs\SiteReviews\Modules\Notice;
29 use GeminiLabs\SiteReviews\Modules\SystemInfo;
30 use GeminiLabs\SiteReviews\Request;
31 use GeminiLabs\SiteReviews\Rollback;
32
33 class ToolsController extends AbstractController
34 {
35 /**
36 * @action site-reviews/route/admin/console-level
37 */
38 public function changeConsoleLevel(Request $request): void
39 {
40 $this->execute(new ChangeLogLevel($request));
41 }
42
43 /**
44 * @action site-reviews/route/admin/console-level
45 */
46 public function changeConsoleLevelAjax(Request $request): void
47 {
48 $command = $this->execute(new ChangeLogLevel($request));
49 wp_send_json([
50 'data' => $command->response(),
51 'success' => $command->successful(),
52 ]);
53 }
54
55 /**
56 * @action site-reviews/route/admin/clear-console
57 */
58 public function clearConsole(): void
59 {
60 $this->execute(new ClearConsole());
61 }
62
63 /**
64 * @action site-reviews/route/ajax/clear-console
65 */
66 public function clearConsoleAjax(): void
67 {
68 $command = $this->execute(new ClearConsole());
69 wp_send_json([
70 'data' => $command->response(),
71 'success' => $command->successful(),
72 ]);
73 }
74
75 /**
76 * @action site-reviews/route/admin/convert-table-engine
77 */
78 public function convertTableEngine(Request $request): void
79 {
80 $this->execute(new ConvertTableEngine($request));
81 }
82
83 /**
84 * @action site-reviews/route/ajax/convert-table-engine
85 */
86 public function convertTableEngineAjax(Request $request): void
87 {
88 $command = $this->execute(new ConvertTableEngine($request));
89 wp_send_json([
90 'data' => $command->response(),
91 'success' => $command->successful(),
92 ]);
93 }
94
95 /**
96 * @action site-reviews/route/admin/download-console
97 */
98 public function downloadConsole(): void
99 {
100 if (!glsr()->hasPermission('tools', 'console')) {
101 glsr(Notice::class)->addError(
102 _x('You do not have permission to download the console.', 'admin-text', 'site-reviews')
103 );
104 return;
105 }
106 $this->download(glsr()->id.'-console.txt', glsr(Console::class)->get());
107 }
108
109 /**
110 * @action site-reviews/route/admin/download-csv-template
111 */
112 public function downloadCsvTemplate(): void
113 {
114 $this->execute(new DownloadCsvTemplate());
115 }
116
117 /**
118 * @action site-reviews/route/admin/download-system-info
119 */
120 public function downloadSystemInfo(): void
121 {
122 if (!glsr()->hasPermission('tools', 'system-info')) {
123 glsr(Notice::class)->addError(
124 _x('You do not have permission to download the system info report.', 'admin-text', 'site-reviews')
125 );
126 return;
127 }
128 $this->download(glsr()->id.'-system-info.txt', glsr(SystemInfo::class)->get());
129 }
130
131 /**
132 * @action site-reviews/route/admin/export-reviews
133 */
134 public function exportReviews(Request $request): void
135 {
136 $this->execute(new ExportReviews($request));
137 }
138
139 /**
140 * @action site-reviews/route/admin/export-settings
141 */
142 public function exportSettings(): void
143 {
144 if (!glsr()->hasPermission('settings')) {
145 glsr(Notice::class)->addError(
146 _x('You do not have permission to export settings.', 'admin-text', 'site-reviews')
147 );
148 return;
149 }
150 $settings = glsr(OptionManager::class)->json();
151 $this->download(glsr()->id.'-settings.json', $settings);
152 }
153
154 /**
155 * @action site-reviews/route/admin/fetch-console
156 */
157 public function fetchConsole(): void
158 {
159 // This is only done via the AJAX method
160 }
161
162 /**
163 * @action site-reviews/route/ajax/fetch-console
164 */
165 public function fetchConsoleAjax(): void
166 {
167 if (!glsr()->hasPermission('settings')) {
168 glsr(Notice::class)->addError(
169 _x('You do not have permission to reload the console.', 'admin-text', 'site-reviews')
170 );
171 wp_send_json_error([
172 'notices' => glsr(Notice::class)->get(),
173 ]);
174 }
175 glsr(Notice::class)->addSuccess(
176 _x('Console reloaded.', 'admin-text', 'site-reviews')
177 );
178 wp_send_json_success([
179 'console' => sanitize_textarea_field(glsr(Console::class)->getRaw()),
180 'notices' => glsr(Notice::class)->get(),
181 ]);
182 }
183
184 /**
185 * @action site-reviews/route/ajax/system-info
186 */
187 public function fetchSystemInfoAjax(): void
188 {
189 if (!glsr()->hasPermission('tools', 'system-info')) {
190 glsr(Notice::class)->addError(
191 _x('Your user role does not have permission to view the system info.', 'admin-text', 'site-reviews')
192 );
193 wp_send_json_error([
194 'notices' => glsr(Notice::class)->get(),
195 ]);
196 }
197 $systemInfo = glsr(SystemInfo::class)->get();
198 wp_send_json_success([
199 'data' => sanitize_textarea_field($systemInfo),
200 ]);
201 }
202
203 /**
204 * @param mixed $value
205 *
206 * @return mixed
207 *
208 * @filter site_transient_update_plugins
209 */
210 public function filterUpdatePluginsTransient($value)
211 {
212 if ($version = get_transient(glsr()->prefix.'rollback_version')) {
213 $update = (object) [
214 'new_version' => $version,
215 'package' => sprintf('https://downloads.wordpress.org/plugin/%s.%s.zip', glsr()->id, $version),
216 'slug' => glsr()->id,
217 ];
218 if (is_object($value)) {
219 $value->response[glsr()->basename] = $update;
220 }
221 }
222 return $value;
223 }
224
225 /**
226 * @action site-reviews/route/ajax/geolocate-reviews
227 */
228 public function geolocateReviewsAjax(Request $request): void
229 {
230 $isRemovingLocation = $request->cast('alt', 'bool', false);
231 if (!glsr()->hasPermission('tools', 'general')) {
232 $message = $isRemovingLocation
233 ? _x('You do not have permission to remove location data from reviews.', 'admin-text', 'site-reviews')
234 : _x('You do not have permission to geolocate reviews.', 'admin-text', 'site-reviews');
235 glsr(Notice::class)->addError($message);
236 wp_send_json_error([
237 'notices' => glsr(Notice::class)->get(),
238 ]);
239 }
240 if ($isRemovingLocation) {
241 $command = $this->execute(new RemoveLocationData());
242 } else {
243 $command = $this->execute(new GeolocateReviews());
244 }
245 $command->sendJsonResponse();
246 }
247
248 /**
249 * @action site-reviews/route/ajax/import-reviews
250 */
251 public function importReviewsAjax(Request $request): void
252 {
253 if (!glsr()->hasPermission('tools', 'general')) {
254 glsr(Notice::class)->addError(
255 _x('You do not have permission to import reviews.', 'admin-text', 'site-reviews')
256 );
257 wp_send_json_error([
258 'notices' => glsr(Notice::class)->get(),
259 ]);
260 }
261 $stages = [
262 1 => ProcessCsvFile::class,
263 2 => ImportReviews::class,
264 3 => ImportReviewsAttachments::class,
265 4 => ImportReviewsCleanup::class,
266 ];
267 $stage = $request->cast('stage', 'int');
268 if (array_key_exists($stage, $stages)) {
269 $command = $this->execute(new $stages[$stage]($request));
270 $command->sendJsonResponse();
271 }
272 wp_send_json_success();
273 }
274
275 /**
276 * @action site-reviews/route/admin/import-settings
277 */
278 public function importSettings(): void
279 {
280 if (!glsr()->hasPermission('settings')) {
281 glsr(Notice::class)->addError(
282 _x('You do not have permission to import settings.', 'admin-text', 'site-reviews')
283 );
284 glsr(Notice::class)->store(); // because of the page reload
285 return;
286 }
287 $this->execute(new ImportSettings());
288 glsr(Notice::class)->store(); // because of the page reload
289 }
290
291 /**
292 * @action site-reviews/route/admin/ip-address-detection
293 */
294 public function ipAddressDetection(Request $request): void
295 {
296 if (wp_validate_boolean($request->get('alt', 0))) {
297 $this->execute(new DetectIpAddress());
298 return;
299 }
300 $this->execute(new ConfigureIpAddressProxy($request));
301 }
302
303 /**
304 * @action site-reviews/route/ajax/ip-address-detection
305 */
306 public function ipAddressDetectionAjax(Request $request): void
307 {
308 $command = wp_validate_boolean($request->get('alt', 0))
309 ? $this->execute(new DetectIpAddress())
310 : $this->execute(new ConfigureIpAddressProxy($request));
311 wp_send_json_success($command->response());
312 }
313
314 /**
315 * @action site-reviews/route/admin/migrate-plugin
316 */
317 public function migratePlugin(Request $request): void
318 {
319 $this->execute(new MigratePlugin($request));
320 }
321
322 /**
323 * @action site-reviews/route/ajax/migrate-plugin
324 */
325 public function migratePluginAjax(Request $request): void
326 {
327 $command = $this->execute(new MigratePlugin($request));
328 wp_send_json([
329 'data' => $command->response(),
330 'success' => $command->successful(),
331 ]);
332 }
333
334 /**
335 * @action site-reviews/route/admin/repair-permissions
336 */
337 public function repairPermissions(Request $request): void
338 {
339 $this->execute(new RepairPermissions($request));
340 }
341
342 /**
343 * @action site-reviews/route/ajax/repair-permissions
344 */
345 public function repairPermissionsAjax(Request $request): void
346 {
347 $command = $this->execute(new RepairPermissions($request));
348 if ($command->successful()) {
349 $reloadLink = glsr(Builder::class)->a([
350 'text' => _x('reload the page', 'admin-text', 'site-reviews'),
351 'href' => 'javascript:window.location.reload(1)',
352 ]);
353 glsr(Notice::class)->clear()->addSuccess(
354 /* translators: %s: a link with the text "reload the page" */
355 sprintf(_x('The permissions have been repaired, please %s for them to take effect.', 'admin-text', 'site-reviews'), $reloadLink)
356 );
357 }
358 wp_send_json([
359 'data' => $command->response(),
360 'success' => $command->successful(),
361 ]);
362 }
363
364 /**
365 * @action site-reviews/route/admin/repair-review-relations
366 */
367 public function repairReviewRelations(): void
368 {
369 $this->execute(new RepairReviewRelations());
370 }
371
372 /**
373 * @action site-reviews/route/ajax/repair-review-relations
374 */
375 public function repairReviewRelationsAjax(): void
376 {
377 $command = $this->execute(new RepairReviewRelations());
378 wp_send_json([
379 'data' => $command->response(),
380 'success' => $command->successful(),
381 ]);
382 }
383
384 /**
385 * @action site-reviews/route/admin/reset-assigned-meta
386 */
387 public function resetAssignedMeta(): void
388 {
389 $this->execute(new ResetAssignedMeta());
390 }
391
392 /**
393 * @action site-reviews/route/ajax/reset-assigned-meta
394 */
395 public function resetAssignedMetaAjax(): void
396 {
397 $command = $this->execute(new ResetAssignedMeta());
398 wp_send_json([
399 'data' => $command->response(),
400 'success' => $command->successful(),
401 ]);
402 }
403
404 /**
405 * @action update-custom_rollback-<Application::ID>
406 */
407 public function rollbackPlugin(): void
408 {
409 if (!current_user_can('update_plugins')) {
410 /* translators: %s: plugin name */
411 wp_die(sprintf(_x('Sorry, you are not allowed to rollback %s.', 'Site Reviews (admin-text)', 'site-reviews'), glsr()->name));
412 }
413 // The no-JS fallback of the rollback form, which submits PLAIN fields to
414 // update.php — Request::inputGet() reads only the encrypted token and
415 // would hand check_admin_referer() an empty action the form's own nonce
416 // could never match. The action needs no reading: the update-custom_
417 // hook that fires this route fixes it.
418 check_admin_referer('rollback-'.glsr()->id);
419 $version = Cast::toString(filter_input(\INPUT_GET, 'version'));
420 glsr(Rollback::class)->rollback(sanitize_text_field($version));
421 }
422
423 /**
424 * @action site-reviews/route/ajax/rollback-<Application::ID>
425 */
426 public function rollbackPluginAjax(Request $request): void
427 {
428 if (!current_user_can('update_plugins')) {
429 wp_send_json_error([
430 'error' => _x('You do not have permission to rollback the plugin.', 'admin-text', 'site-reviews'),
431 ]);
432 }
433 wp_send_json_success(
434 glsr(Rollback::class)->rollbackData($request->cast('version', 'string'))
435 );
436 }
437 }
438