Test Failed
Push — develop ( 5be0e5...3f5c00 )
by Paul
14:23
created

ToolsController::importReviews()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Notices\Notices;
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Notices\Notices was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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