Completed
Pull Request — master (#14)
by Gorka
12:44 queued 10:15
created

AuthorizationChecker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A isNotAdmin() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\WPFoundation;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
final class AuthorizationChecker
20
{
21
    private const ROLE_ADMIN = 'administrator';
22
    private const ROLE_AUTHOR = 'author';
23
    private const ROLE_EDITOR = 'editor';
24
25
    private $user;
26
27
    public function __construct()
28
    {
29
        $this->user = wp_get_current_user();
30
31
        if ($this->isNotAdmin()) {
32
            add_filter('admin_menu', function () {
0 ignored issues
show
Unused Code introduced by
The call to the function add_filter() seems unnecessary as the function has no side-effects.
Loading history...
33
                remove_menu_page('profile.php');
34
                remove_menu_page('tools.php');
35
                remove_menu_page('options-general.php');
36
                remove_menu_page('edit.php');
37
                remove_menu_page('admin.php');
38
                remove_submenu_page('users.php', 'profile.php');
39
            });
40
        }
41
    }
42
43
    private function isNotAdmin() : bool
44
    {
45
        return !in_array(self::ROLE_ADMIN, $this->user->roles, true);
46
    }
47
}
48