Test Failed
Push — main ( 4244ba...f56735 )
by Paul
10:05 queued 12s
created

ToolsController::fetchSystemInfoAjax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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