GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 94a725...12fdff )
by Leonardo
03:33
created

ModuleListTable::get_views()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 4
nop 0
dl 0
loc 35
ccs 0
cts 30
cp 0
crap 30
rs 9.2248
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLAPI\GraphQLAPI\Admin\Tables;
6
7
use GraphQLAPI\GraphQLAPI\General\RequestParams;
8
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade;
9
use GraphQLAPI\GraphQLAPI\Admin\MenuPages\ModulesMenuPage;
10
use GraphQLAPI\GraphQLAPI\Admin\MenuPages\SettingsMenuPage;
11
use GraphQLAPI\GraphQLAPI\Facades\ModuleTypeRegistryFacade;
12
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade;
13
use GraphQLAPI\GraphQLAPI\Admin\TableActions\ModuleListTableAction;
14
15
/**
16
 * Module Table
17
 */
18
class ModuleListTable extends AbstractItemListTable
19
{
20
    public const URL_PARAM_MODULE_TYPE = 'module-type';
21
22
    /**
23
     * Singular name of the listed records
24
     *
25
     * @return string
26
     */
27
    public function getItemSingularName(): string
28
    {
29
        return \__('Module', 'graphql-api');
30
    }
31
32
    /**
33
     * Plural name of the listed records
34
     *
35
     * @return string
36
     */
37
    public function getItemPluralName(): string
38
    {
39
        return \__('Modules', 'graphql-api');
40
    }
41
42
    /**
43
     * Return all the items to display on the table
44
     *
45
     * @return array
46
     */
47
    public function getAllItems(): array
48
    {
49
        $items = [];
50
        $moduleRegistry = ModuleRegistryFacade::getInstance();
51
        $moduleTypeRegistry = ModuleTypeRegistryFacade::getInstance();
52
        $modules = $moduleRegistry->getAllModules();
53
        $currentView = $this->getCurrentView();
54
        foreach ($modules as $module) {
55
            $moduleResolver = $moduleRegistry->getModuleResolver($module);
56
            $moduleType = $moduleResolver->getModuleType($module);
57
            $moduleTypeResolver = $moduleTypeRegistry->getModuleTypeResolver($moduleType);
58
            $moduleTypeSlug = $moduleTypeResolver->getSlug($moduleType);
59
            // If filtering the view, only add the items with that module type
60
            if (!$currentView || $currentView == $moduleTypeSlug) {
61
                $isEnabled = $moduleRegistry->isModuleEnabled($module);
62
                $items[] = [
63
                    'module' => $module,
64
                    'module-type' => $moduleTypeSlug,
65
                    'id' => $moduleResolver->getID($module),
66
                    'is-enabled' => $isEnabled,
67
                    'can-be-disabled' => $moduleResolver->canBeDisabled($module),
68
                    'can-be-enabled' => !$isEnabled && $moduleRegistry->canModuleBeEnabled($module),
69
                    'has-settings' => $moduleResolver->hasSettings($module),
70
                    'name' => $moduleResolver->getName($module),
71
                    'description' => $moduleResolver->getDescription($module),
72
                    'depends-on' => $moduleResolver->getDependedModuleLists($module),
73
                    // 'url' => $moduleResolver->getURL($module),
74
                    'slug' => $moduleResolver->getSlug($module),
75
                    'has-docs' => $moduleResolver->hasDocumentation($module),
76
                ];
77
            }
78
        }
79
        return $items;
80
    }
81
82
    /**
83
     * Gets the current filtering view
84
     *
85
     * @return string
86
     */
87
    protected function getCurrentView(): string
88
    {
89
        return !empty($_REQUEST[self::URL_PARAM_MODULE_TYPE]) ? $_REQUEST[self::URL_PARAM_MODULE_TYPE] : '';
90
    }
91
92
    /**
93
     * Gets the list of views available on this table.
94
     *
95
     * @return array
96
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
97
     */
98
    protected function get_views()
99
    {
100
        $views = [];
101
        $currentView = $this->getCurrentView();
102
103
        // All entries
104
        $views['all'] = sprintf(
105
            '<a href="%s" class="%s">%s</a>',
106
            \remove_query_arg(self::URL_PARAM_MODULE_TYPE),
107
            $currentView == '' ? 'current' : '',
108
            \__('All', 'graphql-api')
109
        );
110
111
        // Entries for every module type: retrieve the moduleType from all modules
112
        $moduleRegistry = ModuleRegistryFacade::getInstance();
113
        $moduleTypeRegistry = ModuleTypeRegistryFacade::getInstance();
114
        $modules = $moduleRegistry->getAllModules();
115
        $moduleTypes = [];
116
        foreach ($modules as $module) {
117
            $moduleResolver = $moduleRegistry->getModuleResolver($module);
118
            $moduleTypes[] = $moduleResolver->getModuleType($module);
119
        }
120
        $moduleTypes = array_unique($moduleTypes);
121
        foreach ($moduleTypes as $moduleType) {
122
            $moduleTypeResolver = $moduleTypeRegistry->getModuleTypeResolver($moduleType);
123
            $moduleTypeSlug = $moduleTypeResolver->getSlug($moduleType);
124
            $views[$moduleTypeSlug] = sprintf(
125
                '<a href="%s" class="%s">%s</a>',
126
                \add_query_arg(self::URL_PARAM_MODULE_TYPE, $moduleTypeSlug),
127
                'module-type-view module-type-' . $moduleTypeSlug . ($currentView == $moduleTypeSlug ? ' current' : ''),
128
                $moduleTypeResolver->getName($moduleType)
129
            );
130
        }
131
132
        return $views;
133
    }
134
135
    /**
136
     * List of item data
137
     *
138
     * @param int $per_page
139
     * @param int $page_number
140
     *
141
     * @return mixed
142
     */
143
    public function getItems($per_page = 5, $page_number = 1)
144
    {
145
        $results = $this->getAllItems();
146
        return array_splice(
147
            $results,
148
            ($page_number - 1) * $per_page,
149
            $per_page
150
        );
151
    }
152
153
    /**
154
     * Returns the count of records in the database.
155
     */
156
    public function getRecordCount(): int
157
    {
158
        $results = $this->getAllItems();
159
        return count($results);
160
    }
161
162
    /**
163
     * Render a column when no column specific method exist.
164
     *
165
     * @param object $item
166
     * @param string $column_name
167
     *
168
     * @return mixed
169
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
170
     */
171
    protected function column_default($item, $column_name)
172
    {
173
        switch ($column_name) {
174
            case 'desc':
175
                $actions = [];
176
                // If it has, add a link to the documentation
177
                $instanceManager = InstanceManagerFacade::getInstance();
178
                $modulesMenuPage = $instanceManager->getInstance(ModulesMenuPage::class);
179
                if ($item['has-docs']) {
180
                    $url = \admin_url(sprintf(
181
                        'admin.php?page=%s&%s=%s&%s=%s&TB_iframe=true&width=600&height=550',
182
                        $modulesMenuPage->getScreenID(),
183
                        RequestParams::TAB,
184
                        RequestParams::TAB_DOCS,
185
                        RequestParams::MODULE,
186
                        urlencode($item['module'])
187
                    ));
188
                    $actions['docs'] = \sprintf(
189
                        '<a href="%s" class="%s" data-title="%s">%s</a>',
190
                        \esc_url($url),
191
                        'thickbox open-plugin-details-modal',
192
                        \esc_attr($item['name']),
193
                        \__('View details', 'graphql-api')
194
                    );
195
                }
196
                return sprintf(
197
                    '<div class="plugin-description"><p>%s</p></div><div class="second">%s</div>',
198
                    $item['description'],
199
                    $this->row_actions($actions, true)
200
                );
201
            case 'depends-on':
202
                // Output the list with AND lists of dependencies
203
                // Each list is an OR list of depended modules
204
                // It's formatted like this: module1, module2, ..., module5 or module6
205
                $items = [];
206
                $moduleRegistry = ModuleRegistryFacade::getInstance();
207
                $dependedModuleLists = $item[$column_name];
208
                if (!$dependedModuleLists) {
209
                    return \__('-', 'graphql-api');
210
                }
211
                /**
212
                 * This is a list of lists of modules, as to model both OR and AND conditions
213
                 */
214
                foreach ($dependedModuleLists as $dependedModuleList) {
215
                    if (!$dependedModuleList) {
216
                        continue;
217
                    }
218
                    $dependedModuleListNames = array_map(
219
                        function ($dependedModule) use ($moduleRegistry) {
220
                            $after = '';
221
                            // Check if it has the "inverse" token at the beginning,
222
                            // then it depends on the module being disabled, not enabled
223
                            if ($moduleRegistry->isInverseDependency($dependedModule)) {
224
                                // Revert to the normal module
225
                                $dependedModule = $moduleRegistry->getInverseDependency($dependedModule);
226
                                $after = \__('⇠ as disabled', 'graphql-api');
227
                            }
228
                            $moduleResolver = $moduleRegistry->getModuleResolver($dependedModule);
229
                            return sprintf(
230
                                '%1$s %2$s %3$s',
231
                                '▹',
232
                                $moduleResolver->getName($dependedModule),
233
                                $after
234
                            );
235
                        },
236
                        $dependedModuleList
237
                    );
238
                    if (count($dependedModuleListNames) >= 2) {
239
                        $lastElem = array_pop($dependedModuleListNames);
240
                        $commaElems = implode(
241
                            \__(', ', 'graphql-api'),
242
                            $dependedModuleListNames
243
                        );
244
                        $items[] = sprintf(
245
                            \__('%s or %s', 'graphql-api'),
246
                            $commaElems,
247
                            $lastElem
248
                        );
249
                    } else {
250
                        $items[] = $dependedModuleListNames[0];
251
                    }
252
                }
253
                return implode('<br/>', $items);
254
            case 'enabled':
255
                return \sprintf(
256
                    '<span role="img" aria-label="%s">%s</span>',
257
                    $item['is-enabled'] ? \__('Yes', 'graphql-api') : \__('No', 'graphql-api'),
258
                    $item['is-enabled'] ? '✅' : '❌'
259
                );
260
        }
261
        return '';
262
    }
263
264
    /**
265
     * Render the bulk edit checkbox
266
     *
267
     * @param object $item
268
     *
269
     * @return string
270
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
271
     */
272
    protected function column_cb($item)
273
    {
274
        return sprintf(
275
            '<input type="checkbox" name="%s[]" value="%s" />',
276
            ModuleListTableAction::INPUT_BULK_ACTION_IDS,
277
            $item['id']
278
        );
279
    }
280
281
    /**
282
     * Method for name column
283
     *
284
     * @param array $item an array of DB data
285
     *
286
     * @return string
287
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
288
     */
289
    public function column_name($item)
290
    {
291
        $nonce = \wp_create_nonce('graphql_api_enable_or_disable_module');
292
        $title = '<strong>' . $item['name'] . '</strong>';
293
        $linkPlaceholder = '<a href="?page=%s&action=%s&item=%s&_wpnonce=%s">%s</a>';
294
        $page = esc_attr($_REQUEST['page']);
295
        $actions = [];
296
        if ($item['is-enabled']) {
297
            // If it is enabled, offer to disable it
298
            // Unless the module cannot be disabled
299
            if ($item['can-be-disabled']) {
300
                $actions['disable'] = \sprintf(
301
                    $linkPlaceholder,
302
                    $page,
303
                    ModuleListTableAction::ACTION_DISABLE,
304
                    $item['id'],
305
                    $nonce,
306
                    \__('Disable', 'graphql-api')
307
                );
308
            } else {
309
                $actions['enabled'] = \__('Enabled', 'graphql-api');
310
            }
311
312
            // Maybe add settings links
313
            if ($item['has-settings']) {
314
                $instanceManager = InstanceManagerFacade::getInstance();
315
                $settingsMenuPage = $instanceManager->getInstance(SettingsMenuPage::class);
316
                $actions['settings'] = \sprintf(
317
                    '<a href="%s">%s</a>',
318
                    sprintf(
319
                        \admin_url(sprintf(
320
                            'admin.php?page=%s&tab=%s',
321
                            $settingsMenuPage->getScreenID(),
322
                            $item['id']
323
                        ))
324
                    ),
325
                    \__('Settings', 'graphql-api')
326
                );
327
            }
328
        } elseif ($item['can-be-enabled']) {
329
            // If not enabled and can be enabled, offer to do it
330
            $actions['enable'] = \sprintf(
331
                $linkPlaceholder,
332
                $page,
333
                ModuleListTableAction::ACTION_ENABLE,
334
                $item['id'],
335
                $nonce,
336
                \__('Enable', 'graphql-api')
337
            );
338
        } else {
339
            // Not enabled and can't be enabled, mention requirements not met
340
            // Not enabled for "striped" table style because, without a link, color contrast is not good:
341
            // gray font color over gray background
342
            // if ($this->usePluginTableStyle()) {
343
            $actions['disabled'] = \__('Disabled', 'graphql-api');
344
            // }
345
        }
346
        return $title . $this->row_actions($actions/*, $this->usePluginTableStyle()*/);
347
    }
348
349
    /**
350
     * Indicate if to show the enabled column or not
351
     */
352
    protected function usePluginTableStyle(): bool
353
    {
354
        return true;
355
    }
356
357
    /**
358
     *  Associative array of columns
359
     *
360
     * @return array
361
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
362
     */
363
    public function get_columns()
364
    {
365
        return array_merge(
366
            [
367
                'cb' => '<input type="checkbox" />',
368
                'name' => \__('Module', 'graphql-api'),
369
            ],
370
            $this->usePluginTableStyle() ?
371
                [] :
372
                [
373
                    'enabled' => \__('Enabled', 'graphql-api'),
374
                ],
375
            [
376
                'desc' => \__('Description', 'graphql-api'),
377
                'depends-on' => \__('Depends on', 'graphql-api'),
378
            ]
379
        );
380
    }
381
382
    /**
383
     * Returns an associative array containing the bulk action
384
     *
385
     * @return array
386
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
387
     */
388
    public function get_bulk_actions()
389
    {
390
        return [
391
            ModuleListTableAction::ACTION_ENABLE => \__('Enable', 'graphql-api'),
392
            ModuleListTableAction::ACTION_DISABLE => \__('Disable', 'graphql-api'),
393
        ];
394
    }
395
396
    /**
397
     * Get a list of CSS classes for the WP_List_Table table tag.
398
     *
399
     * @since 3.1.0
400
     *
401
     * @return string[] Array of CSS classes for the table tag.
402
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
403
     */
404
    protected function get_table_classes()
405
    {
406
        // return array_merge(
407
        //     parent::get_table_classes(),
408
        //     [
409
        //         'plugins'
410
        //     ]
411
        // );
412
        if ($this->usePluginTableStyle()) {
413
            return array( 'widefat', 'plugins', $this->_args['plural'] );
414
        }
415
        return array_diff(
416
            parent::get_table_classes(),
417
            [
418
                'fixed'
419
            ]
420
        );
421
    }
422
423
    /**
424
     * Classnames to add to the row for the item
425
     *
426
     * @param object $item The current item
427
     */
428
    protected function getTableStyleRowClassnames($item): string
429
    {
430
        return sprintf(
431
            'module-%s',
432
            $item['module-type']
433
        );
434
    }
435
436
    /**
437
     * Generates content for a single row of the table
438
     *
439
     * @since 3.1.0
440
     *
441
     * @param object $item The current item
442
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
443
     */
444
    public function single_row($item)
445
    {
446
        if ($this->usePluginTableStyle()) {
447
            $classnames = sprintf(
448
                '%s %s',
449
                $this->getTableStyleRowClassnames($item),
450
                $item['is-enabled'] ? 'active' : 'inactive'
451
            );
452
            echo sprintf(
453
                '<tr class="%s">',
454
                $classnames
455
            );
456
            $this->single_row_columns($item);
457
            echo '</tr>';
458
        } else {
459
            parent::single_row($item);
460
        }
461
    }
462
463
    /**
464
     * Handles data query and filter, sorting, and pagination.
465
     *
466
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
467
     */
468
    public function prepare_items()
469
    {
470
        $this->_column_headers = $this->get_column_info();
471
472
        /** Process bulk or single action */
473
        $instanceManager = InstanceManagerFacade::getInstance();
474
        $tableAction = $instanceManager->getInstance(ModuleListTableAction::class);
475
        $tableAction->maybeProcessAction();
476
477
        $per_page = $this->get_items_per_page(
478
            $this->getItemsPerPageOptionName(),
479
            $this->getDefaultItemsPerPage()
480
        );
481
        $current_page = $this->get_pagenum();
482
        $total_items  = $this->getRecordCount();
483
484
        $this->set_pagination_args([
485
            'total_items' => $total_items,
486
            'per_page'    => $per_page,
487
        ]);
488
489
        $this->items = $this->getItems($per_page, $current_page);
490
    }
491
492
    /**
493
     * Enqueue the required assets
494
     *
495
     * @return void
496
     */
497
    public function enqueueAssets(): void
498
    {
499
        parent::enqueueAssets();
500
501
        /**
502
         * Fix the issues with the WP List Table
503
         */
504
        \wp_enqueue_style(
505
            'graphql-api-module-list-table',
506
            \GRAPHQL_API_URL . 'assets/css/module-list-table.css',
507
            array(),
508
            \GRAPHQL_API_VERSION
509
        );
510
    }
511
512
    /**
513
     * Customize the width of the columns
514
     */
515
    public function printStyles(): void
516
    {
517
        parent::printStyles();
518
519
        /*
520
        if ($this->usePluginTableStyle()) {
521
            ?>
522
            <style type="text/css">
523
                .wp-list-table .column-name { width: 25%; }
524
                .wp-list-table .column-description { width: 75%; }
525
            </style>
526
            <?php
527
        } else {
528
            ?>
529
            <style type="text/css">
530
                .wp-list-table .column-name { width: 25%; }
531
                .wp-list-table .column-enabled { width: 10%; }
532
                .wp-list-table .column-description { width: 65%; }
533
            </style>
534
            <?php
535
        }
536
        */
537
    }
538
}
539