-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathturbo-admin.php
300 lines (268 loc) · 12.7 KB
/
turbo-admin.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
<?php
/**
* Plugin Name: Turbo Admin
* Plugin URI: https://turbo-admin.com/
* Description: The command palette that gives super fast access to your WordPress Dashboard.
* Author: Ross Wintle
* Author URI: https://rosswintle.uk/
* Text Domain: turbo-admin
* Domain Path: /languages
* Version: 1.15.2
*
* @package Turbo_Admin
*/
/* Todo:
*
* ❓ Composer and autoloading (probably not needed after refactor to JS)
* ✅ Add submenu items
* - Filter items to allow people to hook in their own
* ✅ Up/Down/Select
* - General refactor to objects
* ✅ Check roles/permissions (not needed now we're pure JS)
* ✅ Scroll selected item into view
* ✅ Allow user selectable keyboard shortcut
*/
namespace TurboAdmin;
add_action('admin_enqueue_scripts', 'TurboAdmin\add_admin_scripts', 10, 1);
add_action('wp_enqueue_scripts', 'TurboAdmin\add_admin_scripts', 10, 1);
define('TURBO_ADMIN_HIDE_ICON_META_NAME', 'turbo-admin-hide-icon');
define('TURBO_ADMIN_SHORTCUT_META_NAME', 'turbo-admin-shortcut');
define('TURBO_ADMIN_HIDE_NOTICES_META_NAME', 'turbo-admin-hide-notices');
define('TURBO_ADMIN_ADMIN_BAR_SEARCH_META_NAME', 'turbo-admin-admin-bar-search');
define('TURBO_ADMIN_BARKEEPER_META_NAME', 'turbo-admin-barkeeper');
define('TURBO_ADMIN_LIST_TABLE_SHORTCUTS_NAME', 'turbo-admin-list-table-shortcuts');
define('TURBO_ADMIN_DEBUG_MODE_NAME', 'turbo-admin-debug-mode');
function add_admin_scripts()
{
if (is_user_logged_in()) {
$userShortcutKeys = userShortcutKeys();
$hideNotices = get_user_meta(get_current_user_id(), TURBO_ADMIN_HIDE_NOTICES_META_NAME, true);
$adminBarSearch = get_user_meta(get_current_user_id(), TURBO_ADMIN_ADMIN_BAR_SEARCH_META_NAME, true);
$barkeeper = get_user_meta(get_current_user_id(), TURBO_ADMIN_BARKEEPER_META_NAME, true);
$listTableShortcuts = get_user_meta(get_current_user_id(), TURBO_ADMIN_LIST_TABLE_SHORTCUTS_NAME, true);
$debugMode = get_user_meta(get_current_user_id(), TURBO_ADMIN_DEBUG_MODE_NAME, true);
// We will pass an array of shortcut key objects into the JS
$shortcutKeys = [
$userShortcutKeys
];
wp_enqueue_script('turbo-admin-scripts', plugin_dir_url(__FILE__) . 'dist/main.min.js', [], null, true);
wp_enqueue_style('turbo-admin-styles', plugin_dir_url(__FILE__) . 'turbo-admin.css', []);
wp_localize_script(
'turbo-admin-scripts', 'wpTurboAdmin', [
'keys' => $shortcutKeys,
'hideNotices' => intval($hideNotices) === 1,
'adminBarSearch' => intval($adminBarSearch) === 1,
'barkeeper' => intval($barkeeper) === 1,
'listTableShortcuts' => intval($listTableShortcuts) === 1,
'debugMode' => intval($debugMode) === 1,
]
);
}
}
add_action('admin_bar_menu', 'TurboAdmin\add_admin_bar_item', 1000);
function add_admin_bar_item($admin_bar)
{
if (get_hide_icon(get_current_user_id()) == 1) {
return;
}
$userShortcutKeys = userShortcutKeys();
$keysTextArray = [];
if ($userShortcutKeys['meta']) {
$keysTextArray[] = 'Cmd';
}
if ($userShortcutKeys['alt']) {
$keysTextArray[] = 'Alt';
}
if ($userShortcutKeys['ctrl']) {
$keysTextArray[] = 'Ctrl';
}
if ($userShortcutKeys['shift']) {
$keysTextArray[] = 'Shift';
}
$keysTextArray[] = $userShortcutKeys['key'] === ' ' ? 'Space' : strtoupper($userShortcutKeys['key']);
$keysText = implode('-', $keysTextArray);
$admin_bar->add_menu(
array(
'id' => 'turbo-admin',
'parent' => null,
'group' => null,
'title' => '<span class="ab-icon" style="margin-right: 0;"><img src="' . plugin_dir_url(__FILE__) . '/images/snail.svg' . '" style="display: block; width: 24px; height: 24px;"></span>',
'href' => null,
'meta' => [
'title' => sprintf(__('Turbo admin is installed! Use %s to open the command palette.', 'turbo-admin'), $keysText), //This title will show on hover
]
)
);
}
add_action('show_user_profile', 'TurboAdmin\show_profile_fields');
add_action('edit_user_profile', 'TurboAdmin\show_profile_fields');
function get_hide_icon($user_id)
{
$hideIcon = get_user_meta($user_id, TURBO_ADMIN_HIDE_ICON_META_NAME, true);
if ($hideIcon === "") {
// Not set - use default
$hideIcon = apply_filters('turbo_admin_hide_icon_default', 0, $user_id);
}
return $hideIcon;
}
function show_profile_fields($user)
{
$shortcut = get_user_meta($user->ID, TURBO_ADMIN_SHORTCUT_META_NAME, true);
if (empty($shortcut)) {
$shortcut = defaultShortcutKeys();
}
$hideIcon = get_hide_icon($user->ID);
$hideNotices = get_user_meta(get_current_user_id(), TURBO_ADMIN_HIDE_NOTICES_META_NAME, true);
$adminBarSearch = get_user_meta(get_current_user_id(), TURBO_ADMIN_ADMIN_BAR_SEARCH_META_NAME, true);
$barkeeper = get_user_meta(get_current_user_id(), TURBO_ADMIN_BARKEEPER_META_NAME, true);
$listTableShortcuts = get_user_meta(get_current_user_id(), TURBO_ADMIN_LIST_TABLE_SHORTCUTS_NAME, true);
$debugMode = get_user_meta(get_current_user_id(), TURBO_ADMIN_DEBUG_MODE_NAME, true);
?>
<h3><?php _e('Turbo Admin settings', 'turbo_admin') ?></h3>
<table class="form-table">
<tr>
<th><label for="turbo-admin-shortcut"><?php _e('Keyboard shortcut', 'turbo_admin') ?></label></th>
<td>
<label style="margin-right: 18px;">
<input type="checkbox" name="turbo-admin-meta-key" <?php checked($shortcut['meta']) ?>></input>
<?php _e('Cmd (Mac only)', 'turbo_admin') ?>
</label>
<label style="margin-right: 18px;">
<input type="checkbox" name="turbo-admin-alt-key" <?php checked($shortcut['alt']) ?>></input>
<?php _e('Alt/option', 'turbo_admin') ?>
</label>
<label style="margin-right: 18px;">
<input type="checkbox" name="turbo-admin-ctrl-key" <?php checked($shortcut['ctrl']) ?>></input>
<?php _e('Ctrl', 'turbo_admin') ?>
</label>
<label style="margin-right: 18px;">
<input type="checkbox" name="turbo-admin-shift-key" <?php checked($shortcut['shift']) ?>></input>
<?php _e('Shift', 'turbo_admin') ?>
</label>
<input type="text" required name="turbo-admin-shortcut" id="turbo-admin-shortcut" minLength="1" maxLength="1" value="<?php echo esc_attr($shortcut['key']); ?>" class="regular-text" /><br />
<span class="description"><?php _e(
'Please enter the keyboard shortcut you want to use to activate the Turbo Admin command palette.<br>
Do not choose a keyboard combination that your browser already uses.', 'turbo_admin'
) ?></span>
</td>
</tr>
<tr>
<th><label for="turbo-admin-hide-icon"><?php _e('Hide admin bar icon', 'turbo_admin') ?></label></th>
<td>
<label style="margin-right: 18px;">
<input type="radio" name="turbo-admin-hide-icon" value="1" <?php checked($hideIcon, 1) ?>></input>
<?php _e('Hide icon', 'turbo_admin') ?>
</label>
<label style="margin-right: 18px;">
<input type="radio" name="turbo-admin-hide-icon" value="0" <?php checked($hideIcon, 0) ?>></input>
<?php _e('Show icon', 'turbo_admin') ?>
</label>
</td>
</tr>
<tr>
<th>
<label for="turbo-admin-additional-features"><?php _e('Additional features', 'turbo_admin') ?></label>
</th>
<td>
<label for="turbo-admin-hide-notices">
<input name="turbo-admin-hide-notices" type="checkbox" id="turbo-admin-hide-notices" value="1" <?php checked($hideNotices) ?>>
Hide notices (experimental)
</label>
<br>
<label for="turbo-admin-admin-bar-search">
<input name="turbo-admin-admin-bar-search" type="checkbox" id="turbo-admin-admin-bar-search" value="1" <?php checked($adminBarSearch) ?>>
Admin bar search (moves the palette to the admin bar)
</label>
<br>
<label for="turbo-admin-barkeeper">
<input name="turbo-admin-barkeeper" type="checkbox" id="turbo-admin-barkeeper" value="1" <?php checked($barkeeper) ?>>
Barkeeper (hides items in the admin bar)
</label>
<br>
<label for="turbo-admin-list-table-shortcuts">
<input name="turbo-admin-list-table-shortcuts" type="checkbox" id="turbo-admin-list-table-shortcuts" value="1" <?php checked($listTableShortcuts) ?>>
List table shortcuts (experimental)
</label>
<br>
<label for="turbo-admin-debug-mode">
<input name="turbo-admin-debug-mode" type="checkbox" id="turbo-admin-debug-mode" value="1" <?php checked($debugMode) ?>>
Debug mode (lots of console.log!)
</label>
</td>
</tr>
</table>
<?php
}
add_action('personal_options_update', 'TurboAdmin\save_extra_profile_fields');
add_action('edit_user_profile_update', 'TurboAdmin\save_extra_profile_fields');
function save_extra_profile_fields($user_id)
{
if (!current_user_can('edit_user', $user_id)) {
return false;
}
$shortcut = [];
$shortcut['meta'] = isset($_POST['turbo-admin-meta-key']);
$shortcut['alt'] = isset($_POST['turbo-admin-alt-key']);
$shortcut['ctrl'] = isset($_POST['turbo-admin-ctrl-key']);
$shortcut['shift'] = isset($_POST['turbo-admin-shift-key']);
$shortcut['key'] = isset($_POST['turbo-admin-shortcut']) ? esc_attr($_POST['turbo-admin-shortcut']) : 'P';
update_user_meta($user_id, TURBO_ADMIN_SHORTCUT_META_NAME, $shortcut);
if (isset($_POST['turbo-admin-hide-icon']) && in_array(intval($_POST['turbo-admin-hide-icon']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_HIDE_ICON_META_NAME, $_POST['turbo-admin-hide-icon']);
}
if (isset($_POST['turbo-admin-hide-notices']) && in_array(intval($_POST['turbo-admin-hide-notices']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_HIDE_NOTICES_META_NAME, $_POST['turbo-admin-hide-notices']);
} else {
update_user_meta($user_id, TURBO_ADMIN_HIDE_NOTICES_META_NAME, 0);
}
if (isset($_POST['turbo-admin-admin-bar-search']) && in_array(intval($_POST['turbo-admin-admin-bar-search']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_ADMIN_BAR_SEARCH_META_NAME, $_POST['turbo-admin-admin-bar-search']);
} else {
update_user_meta($user_id, TURBO_ADMIN_ADMIN_BAR_SEARCH_META_NAME, 0);
}
if (isset($_POST['turbo-admin-barkeeper']) && in_array(intval($_POST['turbo-admin-barkeeper']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_BARKEEPER_META_NAME, $_POST['turbo-admin-barkeeper']);
} else {
update_user_meta($user_id, TURBO_ADMIN_BARKEEPER_META_NAME, 0);
}
if (isset($_POST['turbo-admin-list-table-shortcuts']) && in_array(intval($_POST['turbo-admin-list-table-shortcuts']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_LIST_TABLE_SHORTCUTS_NAME, $_POST['turbo-admin-list-table-shortcuts']);
} else {
update_user_meta($user_id, TURBO_ADMIN_LIST_TABLE_SHORTCUTS_NAME, 0);
}
if (isset($_POST['turbo-admin-debug-mode']) && in_array(intval($_POST['turbo-admin-debug-mode']), [0, 1], true) ) {
update_user_meta($user_id, TURBO_ADMIN_DEBUG_MODE_NAME, $_POST['turbo-admin-debug-mode']);
} else {
update_user_meta($user_id, TURBO_ADMIN_DEBUG_MODE_NAME, 0);
}
}
/*
* This returns the user-specified key combo if set, or the default one if not
*/
function userShortcutKeys()
{
$userShortcutKeys = get_user_meta(get_current_user_id(), TURBO_ADMIN_SHORTCUT_META_NAME, true);
if (! $userShortcutKeys) {
return defaultShortcutKeys();
}
return $userShortcutKeys;
}
function defaultShortcutKeys()
{
$shortcut = [
'meta' => false,
'alt' => true,
'ctrl' => true,
'shift' => true,
'key' => 'P',
];
if (isUserOnMacOs()) {
$shortcut['meta'] = true;
$shortcut['ctrl'] = false;
}
return $shortcut;
}
function isUserOnMacOs()
{
return strpos($_SERVER['HTTP_USER_AGENT'], "Mac") !== false;
}