-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisableAdminBar.php
111 lines (75 loc) · 2.04 KB
/
DisableAdminBar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace AccessManagement;
use AccessManagement\Traits\IsHooked;
use WPValetBoilerplate\Debug;
class DisableAdminBar {
use IsHooked;
private $show_for_user_ids = [];
private $show_for_roles = [];
private $show_for_capabilities = [];
public function __construct() {
$this->set_number_of_args( 1 );
}
public function show_for_user_id( $user_id ) {
$this->show_for_user_ids = array_merge(
$this->show_for_user_ids,
(array) $user_id
);
}
public function show_for_role( $user_role ) {
$this->show_for_roles = array_merge(
$this->show_for_roles,
(array) $user_role
);
}
public function show_for_capability( $capability ) {
$this->show_for_capabilities = array_merge(
$this->show_for_capabilities,
(array) $capability
);
}
public function init() {
add_filter( 'show_admin_bar', [ $this, '_disable_toolbar' ], $this->priority, $this->number_of_args );
}
public function disable() {
remove_filter( 'show_admin_bar', [ $this, '_disable_toolbar' ], $this->priority );
}
public function _disable_toolbar( $show_admin_bar ) {
$show_admin_bar = false;
$current_user = wp_get_current_user();
if ( $this->show_for_user_ids and $this->user_has_id( $current_user ) ) {
return true;
}
if ( $this->show_for_roles and $this->user_has_role( $current_user ) ) {
return true;
}
if ( $this->show_for_capabilities and $this->user_has_capability( $current_user ) ) {
return true;
}
return $show_admin_bar;
}
private function user_has_id( \WP_User $user ) {
return (bool) in_array( $user->ID, $this->show_for_user_ids );
}
/**
* @param \WP_User $user
*
* @return bool
*/
private function user_has_role( \WP_User $user ) {
return (bool) array_intersect( $user->roles, $this->show_for_roles );
}
/**
* @param \WP_User $user
*
* @return bool
*/
private function user_has_capability( \WP_User $user ) {
foreach ( array_unique( $this->show_for_capabilities ) as $capability ) {
if ( $user->has_cap( $capability ) ) {
return true;
}
}
return false;
}
}