-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwp-butler.php
606 lines (516 loc) · 18.9 KB
/
wp-butler.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<?php
/*
Plugin Name: WP Butler
Plugin URI: http://wpbutler.com
Description: WP Butler brings you what you need in the Wordpress Admin. An autocomplete menu to let you jump to all the common tasks you may need to perform, just hit <code>shift+alt+b</code>!
Version: 2.1
Author: Japh
Author URI: http://japh.com.au
License: GPL2
Copyright 2013 Japh (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* WP Butler
*
* WP Butler brings you what you need in the Wordpress Admin. An autocomplete
* menu to let you jump to all the common tasks you may need to perform, just
* hit `shift+alt+b`!
*
* @author Japh <[email protected]>
* @version 2.1
* @package WP-Butler
* @license http://opensource.org/licenses/gpl-2.0.php GPL2
*/
// Check to see if the class already exists!
if ( ! class_exists( 'Japh_Butler' ) ) {
/**
* WP Butler class
*
* @package WP-Butler
* @version 2.1
* @since 1.2
*/
class Japh_Butler {
/**
* Version number for WP Butler
*
* This is for internal reference, should it be needed.
*
* @since 1.2
*/
public $version = '2.1';
/**
* Array of available post types
* @since 1.4
*/
public $post_types = array();
/**
* Array of available taxonomies
* @since 1.5
*/
public $taxonomies = array();
/**
* Constructor method for WP Butler
*
* @since 1.2
*/
function __construct() {
if ( ! is_admin() )
return NULL;
load_plugin_textdomain( 'wp-butler', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$this->post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects', 'and' );
$this->taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects', 'and' );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'admin_footer', array( $this, 'footer' ) );
add_action( 'wp_ajax_wp_butler_actions', array( $this, 'actions' ) );
add_action( 'admin_bar_menu', array( $this, 'enhance_admin_bar' ), 9999 );
}
/**
* Footer HTML output
*
* This is the HTML for the WP Butler dialog.
*
* @since 1.2
*/
function footer() {
echo '<div id="wp-butler-dialog" title="' . __( "What would you like to do?", "wp-butler" ) . '">';
echo ' <p>';
echo ' <form id="wp-butler-form"><input type="text" placeholder="' . __( "Just start typing", "wp-butler" ) . '..." id="wp-butler-field"><input type="hidden" id="wp-butler-nonce" name="wp-butler-nonce" value="' . wp_create_nonce( 'wp_butler_nonce' ) . '" /><input type="hidden" id="wp-butler-context" name="wp-butler-context" value="' . ( is_network_admin() ? 'network' : 'site' ) . '" /></form>';
echo ' </p>';
echo '</div>';
}
/**
* Enqueue scripts and styles
*
* Properly enqueuing all scripts and styles needed by WP Butler.
*
* @since 1.2
*/
function enqueue() {
// Enqueue styles
if ( 'classic' == get_user_option( 'admin_color') ) {
wp_register_style(
'butler-jquery-ui-css',
plugin_dir_url( __FILE__ ) . 'jquery-ui-css/jquery-ui-classic.css',
array(),
FALSE,
'screen'
);
}
else {
wp_register_style(
'butler-jquery-ui-css',
plugin_dir_url( __FILE__ ) . 'jquery-ui-css/jquery-ui-fresh.css',
array(),
FALSE,
'screen'
);
}
wp_register_style(
'wpbutler',
plugins_url( 'wpbutler.css', __FILE__ ),
array( 'butler-jquery-ui-css' ),
FALSE,
'screen'
);
wp_enqueue_style( 'wpbutler' );
// Enqueue scripts
wp_register_script(
'keystroke',
plugins_url( 'keystroke/jquery.keystroke.min.js', __FILE__ ),#
array( 'jquery' ),
'0d77ac267da80cbe0e0ca8e6fe8b5b2bb8ee1bac',
TRUE
);
wp_register_script(
'wpbutler',
plugins_url( 'wpbutler.js', __FILE__ ),
array( 'jquery-ui-autocomplete', 'jquery-ui-dialog', 'keystroke' ),
$this->version,
TRUE
);
wp_enqueue_script( 'wpbutler' );
}
/**
* Generic action methods
*
* @since 1.4
*
* @param Array $actions Array of current actions for WP Butler
*/
function generate_generic_actions( $actions ) {
array_push( $actions, array( "label" => __( "Dashboard" ), "url" => "index.php" ) );
array_push( $actions, array( "label" => __( "Home" ), "url" => "index.php" ) );
if ( current_user_can( 'update_core' ) ) {
array_push( $actions, array( "label" => __( "WordPress Updates" ), "url" => "update-core.php" ) );
}
if ( current_user_can( 'switch_themes' ) ) {
array_push( $actions, array( "label" => __( "Manage Themes" ), "url" => "themes.php" ) );
}
if ( current_user_can( 'install_themes' ) ) {
array_push( $actions, array( "label" => __( "Install Themes" ), "url" => "theme-install.php" ) );
}
if ( current_user_can( 'activate_plugins' ) ) {
array_push( $actions, array( "label" => __( "Plugins" ), "url" => "plugins.php" ) );
array_push( $actions, array( "label" => __( "Update Plugins" ), "url" => "plugins.php" ) );
}
if ( current_user_can( 'edit_plugins' ) ) {
array_push( $actions, array( "label" => __( "Edit Plugins" ), "url" => "plugin-editor.php" ) );
}
if ( current_user_can( 'install_plugins' ) ) {
array_push( $actions, array( "label" => __( "Install Plugins" ), "url" => "plugin-install.php" ) );
}
if ( current_user_can( 'list_users' ) ) {
array_push( $actions, array( "label" => __( "All Users" ), "url" => "users.php" ) );
}
if ( current_user_can( 'create_users' ) ) {
array_push( $actions, array( "label" => __( "Add New User" ), "url" => "user-new.php" ) );
}
array_push( $actions, array( "label" => __( "Profile" ), "url" => "profile.php" ) );
return $actions;
}
/**
* Single Site action methods
*
* @since 1.4
*
* @param Array $actions Array of current actions for WP Butler
*/
function generate_site_actions( $actions ) {
if ( current_user_can( 'upload_files' ) ) {
array_push( $actions, array( "label" => __( "Media Library" ), "url" => "upload.php" ) );
array_push( $actions, array( "label" => __( "Upload New Media" ), "url" => "media-new.php" ) );
}
if ( current_user_can( 'moderate_comments' ) ) {
array_push( $actions, array( "label" => __( "Comments" ), "url" => "edit-comments.php" ) );
}
if ( current_user_can( 'edit_theme_options' ) ) {
array_push( $actions, array( "label" => __( "Widgets" ), "url" => "widgets.php" ) );
array_push( $actions, array( "label" => __( "Menus" ), "url" => "nav-menus.php" ) );
array_push( $actions, array( "label" => __( "Create Menu" ), "url" => "nav-menus.php" ) );
}
if ( current_user_can( 'manage_options' ) ) {
array_push( $actions, array( "label" => __( "General Settings" ), "url" => "options-general.php" ) );
array_push( $actions, array( "label" => __( "Writing Settings" ), "url" => "options-writing.php" ) );
array_push( $actions, array( "label" => __( "Reading Settings" ), "url" => "options-reading.php" ) );
array_push( $actions, array( "label" => __( "Discussion Settings" ), "url" => "options-discussion.php" ) );
array_push( $actions, array( "label" => __( "Media Settings" ), "url" => "options-media.php" ) );
array_push( $actions, array( "label" => __( "Permalink Settings" ), "url" => "options-permalink.php" ) );
}
array_push( $actions, array( "label" => __( "Tools" ), "url" => "tools.php" ) );
if ( current_user_can( 'import' ) ) {
array_push( $actions, array( "label" => __( "Import" ), "url" => "import.php" ) );
}
// check for multisite install
if ( is_multisite() ) {
array_push( $actions, array( "label" => __( "Network Admin Dashboard" ), "url" => "network/index.php" ) );
}
return $actions;
}
/**
* Multisite action methods
*
* @since 1.4
*
* @param Array $actions Array of current actions for WP Butler
*/
function generate_multisite_actions( $actions ) {
if ( current_user_can( 'manage_sites' ) ) {
array_push( $actions, array( "label" => __( "All Sites" ), "url" => "sites.php" ) );
array_push( $actions, array( "label" => __( "Add New Site" ), "url" => "site-new.php" ) );
}
if ( current_user_can( 'manage_network_options' ) ) {
array_push( $actions, array( "label" => __( "Network Settings" ), "url" => "settings.php" ) );
}
if ( current_user_can( 'manage_network' ) ) {
array_push( $actions, array( "label" => __( "Network Setup" ), "url" => "setup.php" ) );
array_push( $actions, array( "label" => __( "Update Network" ), "url" => "upgrade.php" ) );
}
return $actions;
}
/**
* Post Type action methods
*
* @since 1.5
*
* @param Array $actions Array of current actions for WP Butler
*/
function generate_post_type_actions( $actions ) {
foreach ( $this->post_types as $post_type => $post_type_object ) {
$name = ucfirst( $post_type_object->labels->name );
$singular_name = ucfirst( $post_type_object->labels->singular_name );
$new_url = 'post-new.php?post_type=' . $post_type;
$edit_url = 'edit.php?post_type=' . $post_type;
if ( current_user_can( 'edit_posts' ) ) {
array_push( $actions, array( "label" => $singular_name . " -> " . __( "Add" ), "url" => $new_url ) );
array_push( $actions, array( "label" => $name . " -> " . __( "Edit" ), "url" => $edit_url ) );
array_push( $actions, array( "label" => $name . " -> " . __( "View" ), "url" => $edit_url ) );
}
}
return $actions;
}
/**
* Taxonomy action methods
*
* @since 1.5
*
* @param Array $actions Array of current actions for WP Butler
*/
function generate_taxonomy_actions( $actions ) {
foreach ( $this->taxonomies as $taxonomy => $taxonomy_object ) {
$name = ucfirst( $taxonomy_object->labels->name );
$singular_name = ucfirst( $taxonomy_object->labels->singular_name );
$edit_url = 'edit-tags.php?taxonomy=' . $taxonomy . '&post_type=' . $taxonomy_object->object_type[0];
$new_url = $edit_url;
if ( current_user_can( 'manage_categories' ) ) {
array_push( $actions, array( "label" => $singular_name . " -> " . __( "Add" ), "url" => $new_url ) );
array_push( $actions, array( "label" => $name . " -> " . __( "Edit" ), "url" => $edit_url ) );
array_push( $actions, array( "label" => $name . " -> " . __( "View" ), "url" => $edit_url ) );
}
}
return $actions;
}
/**
* Keyword methods
*
* @since 1.7
*
* @param String $term Term for search keyword
* @param Array $actions Array of current actions for WP Butler
*/
function search_keyword( $term, $actions ) {
$term_words = explode( ' ', $_REQUEST['term'] );
$keyword = array_shift( $term_words );
$keyword_synonyms = array( 'search', 'find', 'edit', 'view' );
if ( ! in_array( $keyword, $keyword_synonyms ) ) {
return array( $term, $actions );
}
if ( array_key_exists( $term_words[0], $this->post_types ) ) {
$search_post_type = array_shift( $term_words );
}
else {
$search_post_type = 'any';
}
$term = implode( ' ', $term_words );
$params = array(
's' => $term,
'posts_per_page' => 10,
'post_type' => $search_post_type,
);
$search = new WP_Query( $params );
$actions = array();
while ( $search->have_posts() ) :
$search->next_post();
if ( $keyword == 'view' ) {
array_push( $actions, array( "label" => get_the_title( $search->post->ID ), "url" => get_permalink( $search->post->ID ) ) );
}
else {
array_push( $actions, array( "label" => get_the_title( $search->post->ID ), "url" => get_edit_post_link( $search->post->ID, 'raw' ) ) );
}
endwhile;
return array( $term, $actions );
}
/**
* Keyword for activating plugins
*
* @since 1.8
*
* @param String $term Term for plugin activation keyword
* @param Array $actions Array of current actions for WP Butler
*/
function activate_plugin_keyword( $term, $actions ) {
$term_words = explode( ' ', $_REQUEST['term'] );
$keyword = array_shift( $term_words );
$keyword_synonyms = array( 'activate', 'deactivate' );
if ( ! in_array( $keyword, $keyword_synonyms ) ) {
return array( $term, $actions );
}
$term = implode( ' ', $term_words );
$plugins = get_plugins();
$plugin_matches = array();
$actions = array();
foreach ( $plugins as $plugin_file => $plugin ) {
if ( $keyword == 'deactivate' ) {
if ( preg_match( '/' . $term . '/i', $plugin['Title'] ) && is_plugin_active( $plugin_file ) ) {
array_push ( $actions, array(
'label' => html_entity_decode( "{$plugin['Title']} v{$plugin['Version']}", ENT_QUOTES, get_option( 'blog_charset' ) ),
'url' => add_query_arg( '_wpnonce', wp_create_nonce( 'deactivate-plugin_' . $plugin_file ), "plugins.php?action=deactivate&plugin={$plugin_file}" )
) );
}
}
else {
if ( preg_match( '/' . $term . '/i', $plugin['Title'] ) && ! is_plugin_active( $plugin_file ) ) {
array_push ( $actions, array(
'label' => html_entity_decode( "{$plugin['Title']} v{$plugin['Version']}", ENT_QUOTES, get_option( 'blog_charset' ) ),
'url' => add_query_arg( '_wpnonce', wp_create_nonce( 'activate-plugin_' . $plugin_file ), "plugins.php?action=activate&plugin={$plugin_file}" )
) );
}
}
}
return array( $term, $actions );
}
/**
* Keyword for searching users
*
* @since 1.8
*
* @param String $term Term for user search keyword
* @param Array $actions Array of current actions for WP Butler
*/
function user_search_keyword( $term, $actions ) {
$term_words = explode( ' ', $_REQUEST['term'] );
$keyword = array_shift( $term_words );
$keyword_synonyms = array( 'user' );
if ( ! in_array( $keyword, $keyword_synonyms ) ) {
return array( $term, $actions );
}
$term = implode( ' ', $term_words );
$params = array(
'search' => $term,
'number' => 10,
);
$users = array();
if ( is_multisite() ) {
$blogs = get_blogs_of_user( get_current_user_id() );
foreach ( $blogs as $blog ) {
$params['blog_id'] = $blog->userblog_id;
$blog_users = get_users( $params );
foreach ( $blog_users as $user ) {
if ( ! in_array( $user, $users ) ) {
$users[] = $user;
}
}
}
}
else {
$users = get_users( $params );
}
$actions = array();
foreach ( $users as $user ) {
array_push( $actions, array( "label" => $user->display_name, "url" => 'user-edit.php?user_id=' . $user->ID ) );
}
return array( $term, $actions );
}
/**
* Keyword for switching sites
*
* @since 2.0
*
* @param String $term Term for site switching keyword
* @param Array $actions Array of current actions for WP Butler
*/
function site_switch_keyword( $term, $actions ) {
global $current_site;
$term_words = explode( ' ', $_REQUEST['term'] );
$keyword = array_shift( $term_words );
$keyword_synonyms = array( 'switch' );
if ( ! in_array( $keyword, $keyword_synonyms ) ) {
return array( $term, $actions );
}
$term = implode( ' ', $term_words );
$blogs = get_blogs_of_user( get_current_user_id() );
$actions = array();
foreach ( $blogs as $blog ) {
if ( preg_match( '/' . $term . '/i', $blog->blogname ) && ! empty( $current_site ) ) {
array_push( $actions, array( "label" => $blog->blogname, "url" => get_admin_url( $blog->userblog_id ) ) );
}
}
return array( $term, $actions );
}
/**
* Actions
*
* Build actions and AJAX callback
*
* @since 1.2
*/
function actions() {
require_once( ABSPATH . '/wp-includes/l10n.php' );
$return = array();
$term = $_REQUEST['term'];
$nonce = $_REQUEST['_nonce'];
$context = $_REQUEST['_context'];
$term_words = explode( ' ', $term );
$keyword = $term_words[0];
if ( is_admin() && wp_verify_nonce( $nonce, 'wp_butler_nonce' ) ) {
$butler_actions = array();
switch ( $keyword ) {
default:
$butler_actions = $this->generate_generic_actions( $butler_actions );
if ( is_network_admin() || $context == 'network' ) {
$butler_actions = $this->generate_multisite_actions( $butler_actions );
}
else {
$butler_actions = $this->generate_site_actions( $butler_actions );
}
$butler_actions = $this->generate_post_type_actions( $butler_actions );
$butler_actions = $this->generate_taxonomy_actions( $butler_actions );
$butler_actions = apply_filters( 'wp_butler_ajax_actions', $butler_actions );
list( $term, $butler_actions ) = $this->search_keyword( $term, $butler_actions );
list( $term, $butler_actions ) = $this->user_search_keyword( $term, $butler_actions );
list( $term, $butler_actions ) = $this->activate_plugin_keyword( $term, $butler_actions );
list( $term, $butler_actions ) = $this->site_switch_keyword( $term, $butler_actions );
list( $term, $butler_actions ) = apply_filters( 'wp_butler_ajax_keyword_actions', array( $term, $butler_actions ) );
if ( ! empty( $butler_actions ) ) {
$random_action_url = $butler_actions[mt_rand( 0, count( $butler_actions ) - 1 )]['url'];
array_push( $butler_actions, array( "label" => __( "Surprise me!", "wp-butler" ), "url" => $random_action_url ) );
}
}
foreach ( $butler_actions as $value ) {
if ( preg_match( '/' . $term . '/i', $value['label'] ) ) {
$return[] = array(
'label' => html_entity_decode( $value['label'], ENT_QUOTES, get_option( 'blog_charset' ) ),
'url' => $value['url']
);
}
}
}
wp_die( json_encode( $return ) );
}
/**
* Enhance the admin bar
*
* @param Array $wp_admin_bar Instance of WP_Admin_Bar
* @return void
* @author bueltge
* @since 1.9
*/
public function enhance_admin_bar( $wp_admin_bar ) {
global $wp_admin_bar;
if ( ! is_super_admin() || ! is_admin_bar_showing() )
return NULL;
$classes = apply_filters( 'wp_butler_admin_bar_classes', array() );
$classes = implode( ' ', $classes );
$args = array(
'id' => 'wp-butler',
'parent' => 'top-secondary',
'secondary' => FALSE,
'title' => apply_filters( 'wp_butler_admin_bar_title', __( 'WP Butler', 'wp-butler' ) ),
'meta' => array( 'class' => $classes )
);
$wp_admin_bar->add_node( $args );
}
}
}
/**
* Execute WP Butler
*
* This function ensures we execute WP Butler at the appropriate time.
*
* @package WP-Butler
* @since 1.2
*/
function execute_wp_butler() {
$japh_butler = new Japh_Butler();
}
add_action( 'admin_init', 'execute_wp_butler' );