-
Notifications
You must be signed in to change notification settings - Fork 27
/
class-connekt-plugin-installer.php
338 lines (302 loc) · 10.1 KB
/
class-connekt-plugin-installer.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
<?php
/**
* Connekt Plugin Installer.
*
* @author Darren Cooney
* @link https://github.com/dcooney/wordpress-plugin-installer
* @link https://connekthq.com
* @version 1.0.2
*
* @package Connekt_Plugin_Installer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'CNKT_INSTALLER_PATH' ) ) {
define( 'CNKT_INSTALLER_PATH', plugins_url( '/', __FILE__ ) );
}
if ( ! defined( 'CNKT_INSTALLER_VERSION' ) ) {
define( 'CNKT_INSTALLER_VERSION', '1.0.2' );
}
if ( ! class_exists( 'Connekt_Plugin_Installer' ) ) {
/**
* Initiate the class.
*/
class Connekt_Plugin_Installer {
/**
* Start installer functionality.
*
* @return void
*/
public function start() {
add_action( 'cnkt_installer_enqueue_scripts', [ &$this, 'cnkt_installer_enqueue_scripts' ] );
add_action( 'wp_ajax_cnkt_plugin_installer', [ &$this, 'cnkt_plugin_installer' ] );
add_action( 'wp_ajax_cnkt_plugin_activation', [ &$this, 'cnkt_plugin_activation' ] );
}
/**
* Initialize the display of the plugins.
*
* @since 1.0
* @param array $plugins The array plugin data for display.
*/
public static function init( $plugins ) {
// Add the required plugin scripts.
do_action( 'cnkt_installer_enqueue_scripts' );
?>
<div class="cnkt-plugin-installer">
<?php
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
foreach ( $plugins as $plugin ) :
$button_classes = 'install button';
$button_text = __( 'Install Now', 'cnkt-installer' );
$api = plugins_api(
'plugin_information',
[
'slug' => sanitize_file_name( $plugin['slug'] ),
'fields' => [
'short_description' => true,
'sections' => false,
'requires' => false,
'downloaded' => true,
'last_updated' => false,
'added' => false,
'tags' => false,
'compatibility' => false,
'homepage' => false,
'donate_link' => false,
'icons' => true,
'banners' => true,
],
]
);
if ( ! is_wp_error( $api ) ) {
$main_plugin_file = self::get_plugin_file( $plugin['slug'] ); // Get main plugin file.
// Plugin is installed.
if ( $main_plugin_file ) {
if ( is_plugin_active( $main_plugin_file ) ) {
// plugin activated.
$button_classes = 'button disabled';
$button_text = __( 'Activated', 'cnkt-installer' );
} else {
// Installed, let's activate it.
$button_classes = 'activate button button-primary';
$button_text = __( 'Activate', 'cnkt-installer' );
}
}
// Send plugin data to template.
self::render_template( $plugin, $api, $button_text, $button_classes );
}
endforeach;
?>
</div>
<?php
}
/**
* Render display template for each plugin.
*
* @since 1.0
* @param array $plugin Original data passed.
* @param array $api Results from plugins_api.
* @param string $button_text Text for the button.
* @param string $button_classes Classnames for the button.
*/
public static function render_template( $plugin, $api, $button_text, $button_classes ) {
?>
<div class="plugin">
<div class="plugin-wrap">
<?php if ( isset( $api->icons ) && isset( $api->icons['1x'] ) ) { ?>
<img src="<?php echo esc_attr( $api->icons['1x'] ); ?>" alt="">
<?php } else { ?>
<img src="https://s.w.org/plugins/geopattern-icon/classic-widgets.svg" alt="">
<?php } ?>
<h2><?php echo wp_kses_post( $api->name ); ?></h2>
<p><?php echo wp_kses_post( $api->short_description ); ?></p>
<p class="plugin-author"><?php esc_attr_e( 'By', 'cnkt-installer' ); ?> <?php echo wp_kses_post( $api->author ); ?></p>
</div>
<ul class="activation-row">
<li>
<a class="<?php echo esc_attr( $button_classes ); ?>" data-slug="<?php echo esc_attr( $api->slug ); ?>" data-name="<?php echo esc_attr( $api->name ); ?>" 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 ); ?>">
<?php echo esc_attr( $button_text ); ?>
</a>
</li>
<li>
<a href="https://wordpress.org/plugins/<?php echo esc_attr( $api->slug ); ?>/" target="_blank">
<?php esc_attr_e( 'More Details', 'cnkt-installer' ); ?>
</a>
</li>
</ul>
</div>
<?php
}
/**
* Ajax method for installing a plugin.
*
* @since 1.0
* @return void|string The plugin data as JSON.
*/
public function cnkt_plugin_installer() {
if ( ! current_user_can( 'install_plugins' ) ) {
// Bail early if requirements not met.
wp_die( esc_attr__( 'Sorry, you are not allowed to install plugins on this site.', 'cnkt-installer' ) );
}
// Get POST data.
$params = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
$nonce = $params['nonce'];
$plugin = $params['plugin'];
// Check our nonce, if they don't match then bounce!
if ( ! wp_verify_nonce( $nonce, 'cnkt_installer_nonce' ) ) {
wp_die( esc_attr__( 'Error - unable to verify nonce, please try again.', 'cnkt-installer' ) );
}
// Required core libs for installation.
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
// Get plugin data.
$api = plugins_api(
'plugin_information',
[
'slug' => $plugin,
'fields' => [
'short_description' => false,
'sections' => false,
'requires' => false,
'rating' => false,
'ratings' => false,
'downloaded' => false,
'last_updated' => false,
'added' => false,
'tags' => false,
'compatibility' => false,
'homepage' => false,
'donate_link' => false,
],
]
);
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$upgrader->install( $api->download_link );
if ( $api->name ) {
$status = 'success';
$msg = $api->name . ' successfully installed.';
} else {
$status = 'failed';
$msg = 'There was an error installing ' . $api->name . '.';
}
$json = [
'status' => $status,
'msg' => $msg,
];
wp_send_json( $json );
}
/**
* Activate plugin via Ajax.
*
* @since 1.0
* @return void|string The plugin data as JSON.
*/
public function cnkt_plugin_activation() {
if ( ! current_user_can( 'install_plugins' ) ) {
// Bail early if requirements not met.
wp_die( esc_attr__( 'Sorry, you are not allowed to activate plugins on this site.', 'cnkt-installer' ) );
}
// Get POST data.
$params = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
$nonce = $params['nonce'];
$plugin = $params['plugin'];
// Check our nonce, if they don't match then bounce!
if ( ! wp_verify_nonce( $nonce, 'cnkt_installer_nonce' ) ) {
wp_die( esc_attr__( 'Error - unable to verify nonce, please try again.', 'cnkt-installer' ) );
}
// Required core WP libs for activation.
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
// Get plugin data.
$api = plugins_api(
'plugin_information',
[
'slug' => $plugin,
'fields' => [
'short_description' => false,
'sections' => false,
'requires' => false,
'rating' => false,
'ratings' => false,
'downloaded' => false,
'last_updated' => false,
'added' => false,
'tags' => false,
'compatibility' => false,
'homepage' => false,
'donate_link' => false,
],
]
);
if ( $api->name ) {
$main_plugin_file = self::get_plugin_file( $plugin );
$status = 'success';
if ( $main_plugin_file ) {
activate_plugin( $main_plugin_file );
$msg = $api->name . ' successfully activated.';
}
} else {
$status = 'failed';
$msg = 'There was an error activating ' . $api->name . '.';
}
$json = [
'status' => $status,
'msg' => $msg,
];
wp_send_json( $json );
}
/**
* A method to get the main plugin file.
*
* @since 1.0
* @param string $plugin_slug The slug of the plugin.
* @return void|string The plugin file.
*/
public static function get_plugin_file( $plugin_slug ) {
// Load core WP plugin lib.
require_once ABSPATH . '/wp-admin/includes/plugin.php';
$plugins = get_plugins();
if ( ! $plugins ) {
return;
}
foreach ( $plugins as $plugin_file => $plugin_info ) {
// Get the basename of the plugin e.g. [askismet]/askismet.php.
$slug = dirname( plugin_basename( $plugin_file ) );
if ( $slug ) {
if ( $slug === $plugin_slug ) {
return $plugin_file;
}
}
}
}
/**
* Enqueue admin scripts and scripts localization.
*
* @since 1.0
*/
public function cnkt_installer_enqueue_scripts() {
wp_enqueue_script( 'plugin-installer', CNKT_INSTALLER_PATH . 'assets/installer.js', [ 'jquery' ], CNKT_INSTALLER_VERSION, false );
wp_localize_script(
'plugin-installer',
'cnkt_installer_localize',
[
'ajax_url' => admin_url( 'admin-ajax.php' ),
'admin_nonce' => wp_create_nonce( 'cnkt_installer_nonce' ),
'install_now' => __( 'Are you sure you want to install this plugin?', 'cnkt-installer' ),
'install_btn' => __( 'Install Now', 'cnkt-installer' ),
'activate_btn' => __( 'Activate', 'cnkt-installer' ),
'installed_btn' => __( 'Activated', 'cnkt-installer' ),
]
);
wp_enqueue_style( 'plugin-installer', CNKT_INSTALLER_PATH . 'assets/installer.css', '', CNKT_INSTALLER_VERSION );
}
}
// Initialize the installer class.
$cnkt_plugin_installer = new Connekt_Plugin_Installer();
$cnkt_plugin_installer->start();
}