|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Connekt_Plugin_Installer |
|
4
|
|
|
* |
|
5
|
|
|
* @author Darren Cooney |
|
6
|
|
|
* @link https://github.com/dcooney/wordpress-plugin-installer |
|
7
|
|
|
* @link https://connekthq.com |
|
8
|
|
|
* @version 1.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
if (!defined('ABSPATH')) exit; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
if( !class_exists('Connekt_Plugin_Installer') ) { |
|
17
|
|
|
|
|
18
|
|
|
class Connekt_Plugin_Installer { |
|
19
|
|
|
|
|
20
|
|
|
public function start(){ |
|
21
|
|
|
if(!defined('CNKT_INSTALLER_PATH')){ |
|
22
|
|
|
// Update this constant to use outside the plugins directory |
|
23
|
|
|
define('CNKT_INSTALLER_PATH', plugins_url('/', __FILE__)); |
|
24
|
|
|
} |
|
25
|
|
|
add_action( 'admin_enqueue_scripts', array(&$this, 'enqueue_scripts' )); // Enqueue scripts and Localize |
|
26
|
|
|
add_action( 'wp_ajax_cnkt_plugin_installer', array(&$this, 'cnkt_plugin_installer' )); // Install plugin |
|
27
|
|
|
add_action( 'wp_ajax_cnkt_plugin_activation', array(&$this, 'cnkt_plugin_activation' )); // Activate plugin |
|
28
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/* |
|
35
|
|
|
* init |
|
36
|
|
|
* Initialize the display of the plugins. |
|
37
|
|
|
* |
|
38
|
|
|
* |
|
39
|
|
|
* @param $plugin Array - plugin data |
|
40
|
|
|
* |
|
41
|
|
|
* @since 1.0 |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function init($plugins){ ?> |
|
44
|
|
|
|
|
45
|
|
|
<div class="cnkt-plugin-installer"> |
|
46
|
|
|
<?php |
|
47
|
|
|
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
48
|
|
|
|
|
49
|
|
|
foreach($plugins as $plugin) : |
|
50
|
|
|
|
|
51
|
|
|
$button_classes = 'install button'; |
|
52
|
|
|
$button_text = __('Install Now', 'framework'); |
|
53
|
|
|
|
|
54
|
|
|
$api = plugins_api( 'plugin_information', |
|
55
|
|
|
array( |
|
56
|
|
|
'slug' => sanitize_file_name($plugin['slug']), |
|
57
|
|
|
'fields' => array( |
|
58
|
|
|
'short_description' => true, |
|
59
|
|
|
'sections' => false, |
|
60
|
|
|
'requires' => false, |
|
61
|
|
|
'downloaded' => true, |
|
62
|
|
|
'last_updated' => false, |
|
63
|
|
|
'added' => false, |
|
64
|
|
|
'tags' => false, |
|
65
|
|
|
'compatibility' => false, |
|
66
|
|
|
'homepage' => false, |
|
67
|
|
|
'donate_link' => false, |
|
68
|
|
|
'icons' => true, |
|
69
|
|
|
'banners' => true, |
|
70
|
|
|
), |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
//echo '<pre>'; |
|
75
|
|
|
//print_r($api); |
|
76
|
|
|
//echo '</pre>'; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
if ( !is_wp_error( $api ) ) { // confirm error free |
|
80
|
|
|
|
|
81
|
|
|
$main_plugin_file = Connekt_Plugin_Installer::get_plugin_file($plugin['slug']); // Get main plugin file |
|
82
|
|
|
//echo $main_plugin_file; |
|
83
|
|
|
if(self::check_file_extension($main_plugin_file)){ // check file extension |
|
84
|
|
|
if(is_plugin_active($main_plugin_file)){ |
|
85
|
|
|
// plugin activation, confirmed! |
|
86
|
|
|
$button_classes = 'button disabled'; |
|
87
|
|
|
$button_text = __('Activated', 'framework'); |
|
88
|
|
|
} else { |
|
89
|
|
|
// It's installed, let's activate it |
|
90
|
|
|
$button_classes = 'activate button button-primary'; |
|
91
|
|
|
$button_text = __('Activate', 'framework'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Send plugin data to template |
|
96
|
|
|
self::render_template($plugin, $api, $button_text, $button_classes); |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
endforeach; |
|
101
|
|
|
?> |
|
102
|
|
|
</div> |
|
103
|
|
|
<?php |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/* |
|
110
|
|
|
* render_template |
|
111
|
|
|
* Render display template for each plugin. |
|
112
|
|
|
* |
|
113
|
|
|
* |
|
114
|
|
|
* @param $plugin Array - Original data passed to init() |
|
115
|
|
|
* @param $api Array - Results from plugins_api |
|
116
|
|
|
* @param $button_text String - text for the button |
|
117
|
|
|
* @param $button_classes String - classnames for the button |
|
118
|
|
|
* |
|
119
|
|
|
* @since 1.0 |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function render_template($plugin, $api, $button_text, $button_classes){ |
|
|
|
|
|
|
122
|
|
|
?> |
|
123
|
|
|
<div class="plugin"> |
|
124
|
|
|
<div class="plugin-wrap"> |
|
125
|
|
|
<img src="<?php echo $api->icons['1x']; ?>" alt=""> |
|
126
|
|
|
<h2><?php echo $api->name; ?></h2> |
|
127
|
|
|
<p><?php echo $api->short_description; ?></p> |
|
128
|
|
|
|
|
129
|
|
|
<p class="plugin-author"><?php _e('By', 'framework'); ?> <?php echo $api->author; ?></p> |
|
130
|
|
|
</div> |
|
131
|
|
|
<ul class="activation-row"> |
|
132
|
|
|
<li> |
|
133
|
|
|
<a class="<?php echo $button_classes; ?>" |
|
134
|
|
|
data-slug="<?php echo $api->slug; ?>" |
|
135
|
|
|
data-name="<?php echo $api->name; ?>" |
|
136
|
|
|
href="<?php echo get_admin_url(); ?>/update.php?action=install-plugin&plugin=<?php echo $api->slug; ?>&_wpnonce=<?php echo wp_create_nonce('install-plugin_'. $api->slug) ?>"> |
|
137
|
|
|
<?php echo $button_text; ?> |
|
138
|
|
|
</a> |
|
139
|
|
|
</li> |
|
140
|
|
|
<li> |
|
141
|
|
|
<a href="https://wordpress.org/plugins/<?php echo $api->slug; ?>/" target="_blank"> |
|
142
|
|
|
<?php _e('More Details', 'frameworks'); ?> |
|
143
|
|
|
</a> |
|
144
|
|
|
</li> |
|
145
|
|
|
</ul> |
|
146
|
|
|
</div> |
|
147
|
|
|
<?php |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/* |
|
154
|
|
|
* cnkt_plugin_installer |
|
155
|
|
|
* An Ajax method for installing plugin. |
|
156
|
|
|
* |
|
157
|
|
|
* @return $json |
|
158
|
|
|
* |
|
159
|
|
|
* @since 1.0 |
|
160
|
|
|
*/ |
|
161
|
|
|
public function cnkt_plugin_installer(){ |
|
162
|
|
|
|
|
163
|
|
|
if ( ! current_user_can('install_plugins') ) |
|
164
|
|
|
wp_die( __( 'Sorry, you are not allowed to install plugins on this site.', 'framework' ) ); |
|
165
|
|
|
|
|
166
|
|
|
$nonce = $_POST["nonce"]; |
|
167
|
|
|
$plugin = $_POST["plugin"]; |
|
168
|
|
|
|
|
169
|
|
|
// Check our nonce, if they don't match then bounce! |
|
170
|
|
|
if (! wp_verify_nonce( $nonce, 'cnkt_installer_nonce' )) |
|
171
|
|
|
wp_die( __( 'Error - unable to verify nonce, please try again.', 'framework') ); |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
// Include required libs for installation |
|
175
|
|
|
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
176
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
177
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php' ); |
|
178
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' ); |
|
179
|
|
|
|
|
180
|
|
|
// Get Plugin Info |
|
181
|
|
|
$api = plugins_api( 'plugin_information', |
|
182
|
|
|
array( |
|
183
|
|
|
'slug' => $plugin, |
|
184
|
|
|
'fields' => array( |
|
185
|
|
|
'short_description' => false, |
|
186
|
|
|
'sections' => false, |
|
187
|
|
|
'requires' => false, |
|
188
|
|
|
'rating' => false, |
|
189
|
|
|
'ratings' => false, |
|
190
|
|
|
'downloaded' => false, |
|
191
|
|
|
'last_updated' => false, |
|
192
|
|
|
'added' => false, |
|
193
|
|
|
'tags' => false, |
|
194
|
|
|
'compatibility' => false, |
|
195
|
|
|
'homepage' => false, |
|
196
|
|
|
'donate_link' => false, |
|
197
|
|
|
), |
|
198
|
|
|
) |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
$skin = new WP_Ajax_Upgrader_Skin(); |
|
202
|
|
|
$upgrader = new Plugin_Upgrader( $skin ); |
|
203
|
|
|
$upgrader->install($api->download_link); |
|
204
|
|
|
|
|
205
|
|
|
if($api->name){ |
|
206
|
|
|
$status = 'success'; |
|
207
|
|
|
$msg = $api->name .' successfully installed.'; |
|
208
|
|
|
} else { |
|
209
|
|
|
$status = 'failed'; |
|
210
|
|
|
$msg = 'There was an error installing '. $api->name .'.'; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$json = array( |
|
214
|
|
|
'status' => $status, |
|
215
|
|
|
'msg' => $msg, |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
wp_send_json($json); |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
/* |
|
226
|
|
|
* cnkt_plugin_activation |
|
227
|
|
|
* Activate plugin via Ajax. |
|
228
|
|
|
* |
|
229
|
|
|
* @return $json |
|
230
|
|
|
* |
|
231
|
|
|
* @since 1.0 |
|
232
|
|
|
*/ |
|
233
|
|
|
public function cnkt_plugin_activation(){ |
|
234
|
|
|
if ( ! current_user_can('install_plugins') ) |
|
235
|
|
|
wp_die( __( 'Sorry, you are not allowed to activate plugins on this site.', 'framework' ) ); |
|
236
|
|
|
|
|
237
|
|
|
$nonce = $_POST["nonce"]; |
|
238
|
|
|
$plugin = $_POST["plugin"]; |
|
239
|
|
|
|
|
240
|
|
|
// Check our nonce, if they don't match then bounce! |
|
241
|
|
|
if (! wp_verify_nonce( $nonce, 'cnkt_installer_nonce' )) |
|
242
|
|
|
die( __( 'Error - unable to verify nonce, please try again.', 'framework' ) ); |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
// Include required libs for activation |
|
246
|
|
|
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
|
247
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
|
248
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' ); |
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
// Get Plugin Info |
|
252
|
|
|
$api = plugins_api( 'plugin_information', |
|
253
|
|
|
array( |
|
254
|
|
|
'slug' => $plugin, |
|
255
|
|
|
'fields' => array( |
|
256
|
|
|
'short_description' => false, |
|
257
|
|
|
'sections' => false, |
|
258
|
|
|
'requires' => false, |
|
259
|
|
|
'rating' => false, |
|
260
|
|
|
'ratings' => false, |
|
261
|
|
|
'downloaded' => false, |
|
262
|
|
|
'last_updated' => false, |
|
263
|
|
|
'added' => false, |
|
264
|
|
|
'tags' => false, |
|
265
|
|
|
'compatibility' => false, |
|
266
|
|
|
'homepage' => false, |
|
267
|
|
|
'donate_link' => false, |
|
268
|
|
|
), |
|
269
|
|
|
) |
|
270
|
|
|
); |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
if($api->name){ |
|
274
|
|
|
$main_plugin_file = Connekt_Plugin_Installer::get_plugin_file($plugin); |
|
275
|
|
|
$status = 'success'; |
|
276
|
|
|
if($main_plugin_file){ |
|
277
|
|
|
activate_plugin($main_plugin_file); |
|
278
|
|
|
$msg = $api->name .' successfully activated.'; |
|
279
|
|
|
} |
|
280
|
|
|
} else { |
|
281
|
|
|
$status = 'failed'; |
|
282
|
|
|
$msg = 'There was an error activating '. $api->name .'.'; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
$json = array( |
|
286
|
|
|
'status' => $status, |
|
287
|
|
|
'msg' => $msg, |
|
|
|
|
|
|
288
|
|
|
); |
|
289
|
|
|
|
|
290
|
|
|
wp_send_json($json); |
|
291
|
|
|
|
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/* |
|
298
|
|
|
* get_plugin_file |
|
299
|
|
|
* A method to get the main plugin file. |
|
300
|
|
|
* |
|
301
|
|
|
* |
|
302
|
|
|
* @param $plugin_slug String - The slug of the plugin |
|
303
|
|
|
* @return $plugin_file |
|
304
|
|
|
* |
|
305
|
|
|
* @since 1.0 |
|
306
|
|
|
*/ |
|
307
|
|
|
|
|
308
|
|
|
public static function get_plugin_file( $plugin_slug ) { |
|
309
|
|
|
require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); // Load plugin lib |
|
310
|
|
|
$plugins = get_plugins(); |
|
311
|
|
|
|
|
312
|
|
|
foreach( $plugins as $plugin_file => $plugin_info ) { |
|
313
|
|
|
|
|
314
|
|
|
// Get the basename of the plugin e.g. [askismet]/askismet.php |
|
315
|
|
|
$slug = dirname( plugin_basename( $plugin_file ) ); |
|
316
|
|
|
|
|
317
|
|
|
if($slug){ |
|
318
|
|
|
if ( $slug == $plugin_slug ) { |
|
319
|
|
|
return $plugin_file; // If $slug = $plugin_name |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
return null; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
|
|
329
|
|
|
/* |
|
330
|
|
|
* check_file_extension |
|
331
|
|
|
* A helper to check file extension |
|
332
|
|
|
* |
|
333
|
|
|
* |
|
334
|
|
|
* @param $filename String - The filename of the plugin |
|
335
|
|
|
* @return boolean |
|
336
|
|
|
* |
|
337
|
|
|
* @since 1.0 |
|
338
|
|
|
*/ |
|
339
|
|
|
public static function check_file_extension( $filename ) { |
|
340
|
|
|
if( substr( strrchr($filename, '.' ), 1 ) === 'php' ){ |
|
341
|
|
|
// has .php exension |
|
342
|
|
|
return true; |
|
343
|
|
|
} else { |
|
344
|
|
|
// ./wp-content/plugins |
|
345
|
|
|
return false; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
|
|
352
|
|
|
/* |
|
353
|
|
|
* enqueue_scripts |
|
354
|
|
|
* Enqueue admin scripts and scripts localization |
|
355
|
|
|
* |
|
356
|
|
|
* |
|
357
|
|
|
* @since 1.0 |
|
358
|
|
|
*/ |
|
359
|
|
|
public function enqueue_scripts(){ |
|
360
|
|
|
wp_enqueue_script( 'plugin-installer', CNKT_INSTALLER_PATH. 'assets/installer.js', array( 'jquery' )); |
|
361
|
|
|
wp_localize_script( 'plugin-installer', 'cnkt_installer_localize', array( |
|
362
|
|
|
'ajax_url' => admin_url('admin-ajax.php'), |
|
363
|
|
|
'admin_nonce' => wp_create_nonce('cnkt_installer_nonce'), |
|
364
|
|
|
'install_now' => __('Are you sure you want to install this plugin?', 'framework'), |
|
365
|
|
|
'install_btn' => __('Install Now', 'framework'), |
|
366
|
|
|
'activate_btn' => __('Activate', 'framework'), |
|
367
|
|
|
'installed_btn' => __('Activated', 'framework') |
|
368
|
|
|
)); |
|
369
|
|
|
|
|
370
|
|
|
wp_enqueue_style( 'plugin-installer', CNKT_INSTALLER_PATH. 'assets/installer.css'); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
// initialize |
|
377
|
|
|
$connekt_plugin_installer = new Connekt_Plugin_Installer(); |
|
378
|
|
|
$connekt_plugin_installer->start(); |
|
379
|
|
|
} |
|
380
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.