admin-bar.php
2.76 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* @package Password Protected
* @subpackage Admin Bar
*
* Adds an indicator in the admin if Password Protection is enabled.
*/
namespace Password_Protected;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action( 'plugins_loaded', array( 'Password_Protected\Admin_Bar', 'load' ), 15 );
class Admin_Bar {
/**
* Load
*
* @internal Private. Called via `plugins_loaded` actions.
*/
public static function load() {
add_action( 'wp_head', array( get_class(), 'styles' ) );
add_action( 'admin_head', array( get_class(), 'styles' ) );
add_action( 'wp_before_admin_bar_render', array( get_class(), 'toolbar_item' ) );
}
/**
* Toolbar Item
*
* @internal Private. Called via `wp_before_admin_bar_render` actions.
*/
public static function toolbar_item() {
global $wp_admin_bar;
if ( self::allow_current_user() ) {
$wp_admin_bar->add_menu( array(
'id' => 'password_protected',
'title' => '',
'href' => self::get_toolbar_item_url(),
'meta' => array(
'title' => self::get_toolbar_item_title()
)
) );
}
}
/**
* Get Toolbar Item URL
*
* @return string
*/
private static function get_toolbar_item_url() {
if ( current_user_can( 'manage_options' ) ) {
return admin_url( 'options-general.php?page=password-protected' );
}
return '';
}
/**
* Get Toolbar Item Title
*
* @return string
*/
private static function get_toolbar_item_title() {
if ( self::is_enabled() ) {
return __( 'Password Protection is enabled.', 'password-protected' );
}
return __( 'Password Protection is disabled.', 'password-protected' );
}
/**
* Styles
*
* @internal Private. Called via `wp_head` and `admin_head` actions.
*/
public static function styles() {
if ( self::allow_current_user() ) {
if ( self::is_enabled() ) {
$icon = '\f160'; // Locked
$background = '#46b450';
} else {
$icon = '\f528'; // Unlocked
$background = 'transparent';
}
?>
<style type="text/css">
#wp-admin-bar-password_protected { background-color: <?php echo $background; ?> !important; }
#wp-admin-bar-password_protected > .ab-item { color: #fff !important; }
#wp-admin-bar-password_protected > .ab-item:before { content: "<?php echo $icon; ?>"; top: 2px; color: #fff !important; margin-right: 0px; }
#wp-admin-bar-password_protected:hover > .ab-item { background-color: <?php echo $background; ?> !important; color: #fff; }
</style>
<?php
}
}
/**
* Allow Current User
*
* @return boolean
*/
private static function allow_current_user() {
return is_user_logged_in();
}
/**
* Is Enabled
*
* @return boolean
*/
private static function is_enabled() {
return (bool) get_option( 'password_protected_status' );
}
}