forked from chrisguitarguy/WP-Plugin-Directories
/
admin.php
413 lines (344 loc) · 8.72 KB
/
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
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
<?php
! defined( 'ABSPATH' ) AND exit();
if ( ! class_exists( 'CD_APD_Admin' ) )
{
add_action( 'plugins_loaded', array( 'CD_APD_Admin', 'instance' ) );
/**
* Admin/Factory
*
* @author Christopher Davis, Franz Josef Kaiser
* @license GPL
* @copyright © Christopher Davis, Franz Josef Kaiser 2011-2012
*
* @package WordPress
* @subpackage Additional Plugin Directories: Admin/Factory
*/
class CD_APD_Admin extends CD_APD_Core
{
/**
* Instance
*
* @since 0.3
* @access protected
* @var object
*/
protected static $instance;
/**
* The container for all of our custom plugins
*
* @since 0.1
* @access protected
* @var array
*/
protected $plugins = array();
/**
* What custom actions are we allowed to handle here?
*
* @since 0.1
* @access protected
* @var array
*/
protected $actions = array();
/**
* The original count of the plugins
*
* @since 0.1
* @access protected
* @var int
*/
protected $all_count = 0;
/**
* Creates a new static instance
*
* @since 0.3
* @static
* @return void
*/
public static function instance()
{
null === self :: $instance AND self :: $instance = new self;
return self :: $instance;
}
/**
* constructor
*
* @since 0.1
* @return void
*/
public function __construct()
{
add_action( 'plugins_loaded', array( $this, 'setup_actions' ), 1 );
add_action( 'load-plugins.php', array( $this, 'init' ) );
}
/**
* Sets up which actions we can handle with this plugin. We'll use this
* to catch activations and deactivations as the normal way won't work.
* Has the filter 'custom_plugin_actions' to allow extensions.
*
* @since 0.1
* @return void
*/
public function setup_actions()
{
$this->actions = apply_filters(
'custom_plugin_actions',
array(
'custom_activate',
'custom_deactivate'
)
);
}
/**
* Makes the magic happen. Loads all the other hooks to modify the plugin list table
*
* @since 0.1
* @return void
*/
public function init()
{
global $wp_plugin_directories;
$screen = get_current_screen();
$this->get_plugins();
$this->handle_actions();
add_filter( "views_{$screen->id}", array( $this, 'views' ) );
// check to see if we're using one of our custom directories
if ( $this->get_plugin_status() )
{
add_filter( "views_{$screen->id}", array( $this, 'views_again' ) );
add_filter( 'all_plugins', array( $this, 'filter_plugins' ) );
// @TODO: support bulk actions
add_filter( "bulk_actions-{$screen->id}", '__return_empty_array' );
add_filter( 'plugin_action_links', array( $this, 'action_links' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
}
/**
* Adds our custom plugin directories to the list of plugin types
*
* @since 0.1
* @param array $views
* @return array $views
*/
public function views( $views )
{
global $wp_plugin_directories;
// bail if we don't have any extra dirs
if ( empty( $wp_plugin_directories ) )
return $views;
// Add our directories to the action links
foreach ( $wp_plugin_directories as $key => $info )
{
$count = count( $this->plugins[ $key ] );
if ( ! $count )
continue;
$views[ $key ] = sprintf(
'<a href="%s"%s>%s <span class="count">(%d)</span></a>',
add_query_arg( 'plugin_status', $key, 'plugins.php' ),
$this->get_plugin_status() == $key ? ' class="current" ' : '',
esc_html( $info['label'] ),
$count
);
}
return $views;
}
/**
* Unset inactive plugin link as it doesn't really work for this view
*
* @since 0.1
* @param array $views
* @return array $views
*/
public function views_again( $views )
{
if ( isset( $views['inactive'] ) )
unset( $views['inactive'] );
return $views;
}
/**
* Filters the plugins list to include all the plugins in our custom directory
*
* @since 0.1
* @param array $plugins
* @return array $plugins
*/
public function filter_plugins( $plugins )
{
$key = $this->get_plugin_status();
if ( $key )
{
$this->all_count = count( $plugins );
$plugins = $this->plugins[ $key] ;
}
return $plugins;
}
/**
* Correct some action links so we can actually "activate" plugins
*
* @since 0.1
* @param array $links
* @param string $plugin_file
* @return array $links
*/
public function action_links( $links, $plugin_file )
{
$context = $this->get_plugin_status();
// let's just start over
$links = array();
$links['activate'] = sprintf(
'<a href="%s" title="Activate this plugin">%s</a>',
wp_nonce_url(
"plugins.php?action=custom_activate&plugin={$plugin_file}&plugin_status=".esc_attr( $context ),
"custom_activate-{$plugin_file}"
),
__( 'Activate' )
);
$active = get_option( "active_plugins_{$context}", array() );
if ( in_array( $plugin_file, $active ) )
{
$links['deactivate'] = sprintf(
'<a href="%s" title="Deactivate this plugin" class="cd-apd-deactivate">%s</a>',
wp_nonce_url(
"plugins.php?action=custom_deactivate&plugin={$plugin_file}&plugin_status=".esc_attr( $context ),
"custom_deactivate-{$plugin_file}"
),
__( 'Deactivate' )
);
}
return $links;
}
/**
* Enqueues on JS file for fun hacks
*
* @since 0.1
* @uses wp_enqueue_script()
* @return void
*/
public function scripts( $screen )
{
if ( 'plugins.php' !== $screen )
return;
wp_enqueue_script(
'cd-apd-js',
CD_APD_URL.'js/apd.js',
array( 'jquery' ),
null
);
wp_localize_script(
'cd-apd-js',
'cd_apd',
array(
'count' => esc_js( $this->all_count )
)
);
}
/**
* Fetch all the custom plugins we have!
*
* @since 0.1
* @uses get_plugins_from_cache() To fetch all the custom plugins
* @return void
*/
public function get_plugins()
{
global $wp_plugin_directories;
empty( $wp_plugin_directories ) AND $wp_plugin_directories = array();
foreach ( array_keys( $wp_plugin_directories ) as $key )
{
$this->plugins[ $key ] = $this->get_plugins_from_cache( $key );
}
}
/**
* Handle activations and deactivations as the standard way will
* fail with "plugin file does not exist
*
* @since 0.1
* @return void
*/
public function handle_actions()
{
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
// not allowed to handle this action? bail.
if ( ! in_array( $action, $this->actions ) ) return;
// Get the plugin we're going to activate
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
if ( ! $plugin )
return;
$context = $this->get_plugin_status();
switch( $action )
{
case 'custom_activate':
if ( ! current_user_can('activate_plugins') )
wp_die( __( 'You do not have sufficient permissions to manage plugins for this site.' ) );
check_admin_referer( "custom_activate-{$plugin}" );
$result = $this->activate_plugin( $plugin, $context );
if ( is_wp_error( $result ) )
{
if ( 'unexpected_output' == $result->get_error_code() )
{
wp_redirect( add_query_arg(
'_error_nonce',
wp_create_nonce( "plugin-activation-error_{$plugin}" ),
add_query_arg(
'plugin_status',
$context,
self_admin_url( 'plugins.php' )
)
) );
exit();
}
else
{
wp_die( $result );
}
}
wp_redirect( add_query_arg(
array( 'plugin_status' => $context, 'activate' => 'true' ),
self_admin_url( 'plugins.php' )
) );
exit();
break;
case 'custom_deactivate':
if ( ! current_user_can( 'activate_plugins' ) )
wp_die( __( 'You do not have sufficient permissions to deactivate plugins for this site.' ) );
check_admin_referer( "custom_deactivate-{$plugin}" );
$this->deactivate_plugins( $plugin, $context );
if ( headers_sent() )
{
printf(
"<meta http-equiv='refresh' content='%s' />",
esc_attr( "0;url=plugins.php?deactivate=true&plugin_status={$status}&paged={$page}&s={$s}" )
);
}
else
{
wp_redirect( self_admin_url( "plugins.php?deactivate=true&plugin_status={$context}" ) );
exit();
}
break;
default:
do_action( "custom_plugin_dir_{$action}" );
break;
}
}
/**
* Utility function to get the current `plugin_status`.
* The key returns FALSE if our key isn't in the the custom directories
*
* @since 0.1
* @return bool|string $rv False on failure, the `$wp_plugin_directories` key on success
*/
public function get_plugin_status()
{
global $wp_plugin_directories;
$rv = false;
if (
isset( $_GET['plugin_status'] )
AND in_array( $_GET['plugin_status'], array_keys( $wp_plugin_directories ) )
)
{
$rv = $_GET['plugin_status'];
}
return $rv;
}
} // END Class CD_APD_Admin
} // endif;