-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-hide-admin-bar.php
executable file
·333 lines (284 loc) · 8.47 KB
/
auto-hide-admin-bar.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
Plugin Name: Auto Hide Admin Bar
Description: Automatically hides the Toolbar. Will show the Toolbar when hovering over the top of the site.
Author: Marcel Bootsman
Version: 1.6.6
Author URI: https://marcelbootsman.nl
Github Plugin URI: https://github.com/mbootsman/auto-hide-admin-bar
Primary Branch: main
Text Domain: auto-hide-admin-bar
Domain Path: /languages/
*/
/* ----------------------------------------------------------------------------
* Global data */
$plugin_file = dirname(__FILE__) . '/auto-hide-admin-bar.php';
$plugin_path = plugin_dir_path($plugin_file);
$keyboard_shortcut_fields = array(
'Ctrl' => 0,
'Alt' => 0,
'Shift' => 0,
'char' => ''
);
/* Define some default values */
define('DEFAULT_SPEED', 200);
define('DEFAULT_DELAY', 1500);
define('DEFAULT_INTERVAL', 100);
define('DEFAULT_MOBILE', 1);
define('DEFAULT_TOGGLE', 1);
define('DEFAULT_ARROW', 1);
define('DEFAULT_ARROW_POS', 'left');
/**
* Returns current plugin version.
*
* @return string Plugin version
*/
function plugin_get_version() {
$plugin_data = get_plugin_data(__FILE__);
$plugin_version = $plugin_data['Version'];
return $plugin_version;
}
/* Load CSS files */
function ahab_admin_styles() {
// only load if a user is logged in
if (is_user_logged_in()) {
wp_enqueue_style('admin-styles', plugin_dir_url(__FILE__) . 'css/ahab.css');
}
}
add_action('wp_enqueue_scripts', 'ahab_admin_styles');
/**
* Include options page for admin area
*
*/
if (is_admin()) {
include_once $plugin_path . 'ahab_options.php';
}
/**
* Add Settings link to plugin page
*
* @param Array $links , filename $file
*
* @return Array $links with new link=
* @author Marcel Bootsman
*
*/
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'ahab_add_plugin_actions_links');
function ahab_add_plugin_actions_links($links) {
$ahab_links = array(
'<a href="options-general.php?page=auto-hide-admin-bar">' . __("Settings", "auto-hide-admin-bar") . '</a>'
);
return array_merge($links, $ahab_links);
}
/**
* Check if ahab is disabled (by user role)
*
* @param None
*
* @return true / false
* @author Marcel Bootsman
*
*/
function is_ahab_disabled() {
$ahab_disabled = false;
// Get options
$options = get_option('ahab_plugin_options');
// check if ahab is disabled for current user role
global $wp_roles, $current_user, $ahab_disabled;
foreach ($wp_roles->roles as $role_key => $role) {
// disabled user roles are stored as a seperate array element
if ($options) { // only continue if options exists
if (!empty($options['disabled_user_roles_' . $role_key])) {
// check if current user role matches the role
if (in_array($role_key, $current_user->roles)) {
$ahab_disabled = true;
// leave the foreach loop
break;
}
} else {
//no role options set (thanks for updating/installing!), enable ahab for everyone.
$ahab_disabled = false;
}
} else {
//no options set, enable ahab for everyone.
$ahab_disabled = false;
}
}
return $ahab_disabled;
}
/* Add Toggle to admin bar */
add_action('admin_bar_menu', 'ahab_admin_bar_item', 0);
function ahab_admin_bar_item(WP_Admin_Bar $admin_bar) {
$options = get_option('ahab_plugin_options');
if (!is_admin()) {
if ((!empty($options['toggle'])) && (2 == $options['toggle'])) {
$admin_bar->add_menu(array(
'id' => 'ahab-toggle',
'parent' => null,
'group' => null,
'title' => '<div class="ahab"><label class="switch">
<input id="toggle-checkbox" type="checkbox">
<span class="slider round"></span>
</label></div>',
'href' => '',
'meta' => [
'title' => __('Toggle lock for the Admin bar', 'auto-hide-admin-bar'), //This title will show on hover
]
));
}
}
}
/**
* The main function. Build JS code and output it.
*
* @param None
*
* @return None
* @author Marcel Bootsman
*
*/
function auto_hide_admin_bar() {
// Get options
$options = get_option('ahab_plugin_options');
global $keyboard_shortcut_fields;
if ((!empty($options['speed'])) && (is_numeric($options['speed']))) {
$ahab_anim_speed = $options['speed'];
} else {
$ahab_anim_speed = DEFAULT_SPEED;
}
if ((!empty($options['delay'])) && (is_numeric($options['delay']))) {
$ahab_delay = $options['delay'];
} else {
$ahab_delay = DEFAULT_DELAY;
}
if ((!empty($options['interval'])) && (is_numeric($options['interval']))) {
$ahab_interval = $options['interval'];
} else {
$ahab_interval = DEFAULT_INTERVAL;
}
if ((!empty($options['arrow'])) && (is_numeric($options['arrow']))) {
$ahab_arrow = $options['arrow'];
} else {
$ahab_arrow = DEFAULT_ARROW;
}
if ((!empty($options['arrow_pos'])) && (is_string($options['arrow_pos']))) {
$ahab_arrow_pos = $options['arrow_pos'];
} else {
$ahab_arrow_pos = DEFAULT_ARROW_POS;
}
if ((!empty($options['mobile'])) && (is_numeric($options['mobile']))) {
$ahab_mobile = $options['mobile'];
} else {
$ahab_mobile = DEFAULT_MOBILE;
}
// get keys and prepare to pass to JS
$ahab_keyboard_shortcut_keys = array();
foreach ($keyboard_shortcut_fields as $key => $value) {
if ($options) { // only continue if options exists
if (!empty($options['keyboard_shortcut_' . $key])) {
if ('' != $options['keyboard_shortcut_' . $key]) {
$ahab_keyboard_shortcut_keys[$key] = $options['keyboard_shortcut_' . $key];
}
}
}
}
/**
* Theme name check - For now only for Twenty Fourteen
* because of the fixed header/menu
**/
if (function_exists('wp_get_theme')) {
$theme_name = (wp_get_theme()->Template);
};
?>
<script type='text/javascript'>
// For passing the variables to the ahab.js file
ahab = {
'theme_name': '<?php echo $theme_name; ?>',
'ahab_anim_speed': <?php echo $ahab_anim_speed; ?>,
'ahab_delay': <?php echo $ahab_delay; ?>,
'ahab_interval': <?php echo $ahab_interval; ?>,
'ahab_mobile': '<?php echo $ahab_mobile; ?>',
'ahab_arrow': '<?php echo $ahab_arrow; ?>',
'ahab_arrow_pos': '<?php echo $ahab_arrow_pos; ?>',
'ahab_keyboard_ctrl': <?php echo array_key_exists('Ctrl', $ahab_keyboard_shortcut_keys) ? '\'' . $ahab_keyboard_shortcut_keys['Ctrl'] . '\'' : 0; ?>,
'ahab_keyboard_alt': <?php echo array_key_exists('Alt', $ahab_keyboard_shortcut_keys) ? '\'' . $ahab_keyboard_shortcut_keys['Alt'] . '\'' : 0; ?>,
'ahab_keyboard_shift': <?php echo array_key_exists('Shift', $ahab_keyboard_shortcut_keys) ? '\'' . $ahab_keyboard_shortcut_keys['Shift'] . '\'' : 0; ?>,
'ahab_keyboard_char': <?php echo array_key_exists('char', $ahab_keyboard_shortcut_keys) ? '\'' . $ahab_keyboard_shortcut_keys['char'] . '\'' : '\'\''; ?>
};
</script>
<?php
}
/**
* Add jQuery and jQuery hoverIntent (No tents were harmed in the process)
*
* @param None
*
* @return None
* @author Marcel Bootsman
*
*/
add_action('wp_footer', 'ahab_add_jquery_stuff');
function ahab_add_jquery_stuff() {
if (is_user_logged_in() && (!is_ahab_disabled())) {
wp_enqueue_script('jquery');
wp_register_script('jquery-hoverintent', plugins_url('js/jquery.hoverIntent.minified.js', __FILE__));
wp_enqueue_script('jquery-hoverintent');
wp_enqueue_script('jquery-hotkeys');
wp_register_script('ahab', plugins_url('js/ahab.js', __FILE__));
wp_enqueue_script('ahab');
}
}
/**
* Hook main function for logged in users
*
* @param None
*
* @return None
* @author Marcel Bootsman
*
*/
add_action('wp_footer', 'ahab_add_my_hide_stuff');
function ahab_add_my_hide_stuff() {
if (is_user_logged_in() && (!is_ahab_disabled())) {
auto_hide_admin_bar();
}
}
/**
* Load Text Domain
*
* @param None
*
* @return None
* @author Marcel Bootsman
*
*/
add_action('plugins_loaded', 'auto_hide_admin_bar_load_textdomain');
function auto_hide_admin_bar_load_textdomain() {
load_plugin_textdomain('auto-hide-admin-bar', false, basename(dirname(__FILE__)) . '/languages/');
}
/**
* Load language file
*
* @param
*/
add_filter('load_textdomain_mofile', 'auto_hide_admin_bar_load_language_files', 10, 2);
function auto_hide_admin_bar_load_language_files($mofile, $domain) {
if ('auto-hide-admin-bar' === $domain && false !== strpos($mofile, WP_LANG_DIR . '/plugins/')) {
$locale = apply_filters('plugin_locale', determine_locale(), $domain);
$mofile = WP_PLUGIN_DIR . '/' . dirname(plugin_basename(__FILE__)) . '/languages/' . $domain . '-' . $locale . '.mo';
}
return $mofile;
}
/**
* Override w.org updates for Git-updater.
*/
add_filter(
'gu_override_dot_org',
function ($overrides) {
return array_merge(
$overrides,
array(
'auto-hide-admin-bar/auto-hide-admin-bar.php', // plugin format
)
);
}
);