PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.1.17
Adminify – White Label, Admin Menu Editor, Login Customizer v4.1.17
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Libs / Addons.php

Addons.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.1.17, at Libs/Addons.php

882 lines 38.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Libs;
4
5 // No, Direct access Sir !!!
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 /*
11 * Addons global class
12 */
13
14 if (!class_exists('Addons')) {
15
16 /**
17 * Addons Class
18 *
19 * Jewel Theme <support@jeweltheme.com>
20 */
21 class Addons
22 {
23 public $menu_items = [];
24 public $plugins_list = [];
25 public $sub_menu;
26 public $menu_order;
27
28 public $server_url = 'https://coupon.wpadminify.com/';
29
30
31 /**
32 * Constructor method
33 *
34 * @param integer $menu_order .
35 * @author Jewel Theme <support@jeweltheme.com>
36 */
37 public function __construct($menu_order = 70)
38 {
39 $this->menu_order = $menu_order;
40 $this->menu_items = $this->menu_items();
41 $this->plugins_list = $this->plugins_list();
42
43 $this->includes();
44
45 // Show Addons menu only on network admin for multisite, or on regular admin for single site
46 if ( is_multisite() ) {
47 add_action('network_admin_menu', array($this, 'admin_menu'), 1000);
48 } else {
49 add_action('admin_menu', array($this, 'admin_menu'), 1000);
50 }
51 add_action('wp_ajax_jltwp_adminify_addons_upgrade_plugin', array($this, 'jltwp_adminify_addons_upgrade_plugin'));
52 add_action('wp_ajax_jltwp_adminify_addons_activate_plugin', array($this, 'jltwp_adminify_addons_activate_plugin'));
53 // Notify the site admin when a renamed legacy addon is detected
54 // alongside its replacement. Per WordPress.org plugin guidelines,
55 // we must not deactivate or activate plugins automatically; the
56 // user has to perform the swap themselves from the Plugins screen.
57 add_action('admin_notices', array($this, 'maybe_renamed_addon_notice'));
58 add_action( 'rest_api_init', array( $this , 'jltwp_adminify_addons_rest_routes') );
59 }
60
61 public function jltwp_adminify_addons_rest_routes() {
62 register_rest_route('adminify/v1', '/get-addons-list', array(
63 'methods' => 'GET',
64 'callback' => [$this, 'jltwp_adminify_get_addons_plugins_list'],
65 'permission_callback' => [$this, 'adminify_is_admin_user'],
66 ));
67
68 register_rest_route('adminify/v1', '/install-addons', array(
69 'methods' => 'POST',
70 'callback' => [$this, 'jltwp_adminify_install_addons'],
71 'permission_callback' => [$this, 'adminify_verify_nonce_and_permissions'],
72 ));
73 }
74
75 public function adminify_is_admin_user() {
76 if ( is_multisite() && ! is_super_admin() ) {
77 return new \WP_Error('rest_forbidden', __('You are not allowed to access this resource.', 'adminify'), array('status' => 403));
78 }
79 if ( ! current_user_can('manage_options') ) {
80 return new \WP_Error('rest_forbidden', __('You are not allowed to access this resource.', 'adminify'), array('status' => 403));
81 }
82 return true;
83 }
84
85 public function adminify_verify_nonce_and_permissions() {
86 // The install-addons endpoint may both install AND activate
87 // addons depending on each addon's current status, so the
88 // caller must hold BOTH capabilities. On multisite this also
89 // requires super admin.
90 if ( is_multisite() && ! is_super_admin() ) {
91 return new \WP_Error('rest_forbidden', __('Super admin required.', 'adminify'), array('status' => 403));
92 }
93 if ( ! current_user_can('install_plugins') ) {
94 return new \WP_Error('rest_forbidden', __('You are not allowed to install plugins.', 'adminify'), array('status' => 403));
95 }
96 if ( ! current_user_can('activate_plugins') ) {
97 return new \WP_Error('rest_forbidden', __('You are not allowed to activate plugins.', 'adminify'), array('status' => 403));
98 }
99
100 // Nonce check from header. Sanitize and unslash before verifying.
101 $nonce = isset($_SERVER['HTTP_X_WP_NONCE'])
102 ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_WP_NONCE'] ) )
103 : '';
104 if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
105 return new \WP_Error('rest_cookie_invalid_nonce', __('Invalid nonce.', 'adminify'), array('status' => 403));
106 }
107
108 return true;
109 }
110
111
112 public function jltwp_adminify_get_addons_plugins_list() {
113 $plugins = $this->plugins_list;
114 unset($plugins['master-addons']);
115 $all_plugins = get_plugins();
116 $active_plugins = get_option('active_plugins');
117 foreach( $plugins as $slug => $plugin){
118 foreach ($all_plugins as $plugin_file => $plugin_data) {
119 if (strpos($plugin_file, $slug) !== false) {
120 $plugins[$slug]["status"] = 'installed';
121
122 if (in_array($plugin_file, $active_plugins)) {
123 $plugins[$slug]["status"] = 'activated';
124 }
125 break;
126 }
127 }
128 if( !isset($plugins[$slug]["status"])) $plugins[$slug]["status"] = 'not-installed';
129
130 }
131
132 return rest_ensure_response($plugins);
133
134 }
135
136
137 public function jltwp_adminify_install_addons( $request ) {
138 $addons = $request->get_param('addons');
139 if ( empty($addons) || ! is_array($addons) ) {
140 return new \WP_Error('no_addons', __('No addons were selected.', 'adminify'), array('status' => 400));
141 }
142
143 $plugins_list = $this->jltwp_adminify_get_addons_plugins_list()->data;
144 foreach( $addons as $key => $plugin ) {
145 $plugin = sanitize_key( $plugin );
146 if ( ! isset( $plugins_list[ $plugin ] ) ) {
147 continue;
148 }
149 if ( $plugins_list[ $plugin ]['status'] === 'activated' ) {
150 continue;
151 }
152 if ( $plugins_list[ $plugin ]['status'] === 'installed' ) {
153 $this->jltwp_adminify_activate_plugin_by_slug( $plugin );
154 continue;
155 }
156 $params = [
157 'request_type' => 'rest',
158 'plugin' => $plugins_list[ $plugin ]['download_link'],
159 ];
160
161 $this->jltwp_adminify_addons_upgrade_plugin( $params );
162 }
163
164 return rest_ensure_response(['message' => __('Addons processed.', 'adminify'), 'addons' => $addons]);
165 }
166
167 function jltwp_adminify_activate_plugin_by_slug($slug) {
168 // Activation requires the activate_plugins capability in
169 // addition to whatever capability gated the calling endpoint.
170 // On multisite, activation must be performed by a super admin.
171 if ( is_multisite() && ! is_super_admin() ) {
172 return new \WP_Error( 'rest_forbidden', __( 'Super admin required to activate plugins.', 'adminify' ), array( 'status' => 403 ) );
173 }
174 if ( ! current_user_can( 'activate_plugins' ) ) {
175 return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to activate plugins.', 'adminify' ), array( 'status' => 403 ) );
176 }
177
178 // Reject any slug containing path separators / traversal so
179 // $slug cannot escape WP_PLUGIN_DIR.
180 if ( ! is_string( $slug ) || $slug === '' || strpbrk( $slug, "/\\" ) !== false || strpos( $slug, '..' ) !== false ) {
181 return new \WP_Error( 'invalid_slug', __( 'Invalid plugin slug.', 'adminify' ), array( 'status' => 400 ) );
182 }
183
184 // Slug must be present in the trusted addons list.
185 if ( ! array_key_exists( $slug, (array) $this->plugins_list ) ) {
186 return new \WP_Error( 'invalid_slug', __( 'Invalid plugin slug.', 'adminify' ), array( 'status' => 400 ) );
187 }
188
189 $plugin_path = WP_PLUGIN_DIR . '/' . $slug;
190
191 if ( ! is_dir( $plugin_path ) ) {
192 return;
193 }
194
195 $installed_plugins = get_plugins( '/' . $slug );
196 if ( empty( $installed_plugins ) ) {
197 return;
198 }
199
200 $plugin_relative_path = $slug . '/' . key( $installed_plugins );
201
202 if ( is_plugin_active( $plugin_relative_path ) ) {
203 return;
204 }
205
206 activate_plugin( $plugin_relative_path );
207 }
208
209 /**
210 * Map of legacy addon slugs that have been renamed to a new slug.
211 *
212 * @return array<string,string>
213 */
214 protected function renamed_addons_map() {
215 return [
216 'sidebar-generator/adminify-sidebar-generator.php' => 'adminify-sidebar-generator/adminify-sidebar-generator.php',
217 ];
218 }
219
220 /**
221 * Show a non-blocking admin notice if a legacy (renamed) addon is
222 * still installed. We never deactivate or activate plugins on the
223 * user's behalf; the notice points them to the Plugins screen so
224 * they can perform the swap themselves.
225 */
226 public function maybe_renamed_addon_notice() {
227 if ( ! current_user_can('activate_plugins') ) {
228 return;
229 }
230
231 $messages = [];
232
233 foreach ($this->renamed_addons_map() as $old_plugin => $new_plugin) {
234 $old_exists = file_exists(WP_PLUGIN_DIR . '/' . $old_plugin);
235 if ( ! $old_exists ) {
236 continue;
237 }
238
239 $messages[] = sprintf(
240 /* translators: 1: old plugin slug, 2: new plugin slug */
241 esc_html__('"%1$s" has been renamed to "%2$s". Please deactivate and remove the old version, then install the new one from the Adminify Addons screen.', 'adminify'),
242 esc_html(dirname($old_plugin)),
243 esc_html(dirname($new_plugin))
244 );
245 }
246
247 if ( empty($messages) ) {
248 return;
249 }
250
251 echo '<div class="notice notice-warning"><p><strong>' . esc_html__('Adminify', 'adminify') . ':</strong> ' . esc_html(implode('<br>', $messages)) . '</p></div>';
252 }
253
254 /**
255 * Includes
256 *
257 * @author Jewel Theme <support@jeweltheme.com>
258 */
259 public function includes()
260 {
261 // wp-load.php must never be required from within a plugin: the
262 // plugin already runs inside WordPress. The wp-admin includes
263 // below are required for plugin install/upgrade APIs used by
264 // this class and are loaded with require_once immediately
265 // before the functions from each file are called.
266 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
267 require_once ABSPATH . 'wp-admin/includes/file.php';
268 require_once ABSPATH . 'wp-admin/includes/misc.php';
269 require_once ABSPATH . 'wp-admin/includes/plugin.php';
270 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
271 }
272
273 /**
274 * Menu Items
275 *
276 * @author Jewel Theme <support@jeweltheme.com>
277 */
278 public function menu_items()
279 {
280 return array();
281 }
282
283 /**
284 * Plugins list
285 *
286 * @author Jewel Theme <support@jeweltheme.com>
287 */
288 public function plugins_list()
289 {
290 return array();
291 }
292
293 /**
294 * Admin submenu
295 */
296 public function admin_menu()
297 {
298 }
299
300 /**
301 * Render addons plugins body
302 */
303 public function render_addons_plugins()
304 {
305 ?>
306 <div class='wp-adminify-addons-wrapper'>
307 <?php $this->header(); ?>
308 <?php $this->body(); ?>
309 </div>
310 <?php
311 }
312
313
314 /**
315 * Addons License Header Check
316 *
317 * @return void
318 */
319
320 public function jltwp_adminify_addons_check()
321 {
322
323 $license = jltwp_adminify()->_get_license();
324
325 if (!is_object($license) || !$license->is_valid() || !$license->is_active()) return;
326
327 if ( $this->is_eligible_for_coupon() ) {
328 // Get the coupon
329 $coupon = $this->maybe_create_and_get_coupon();
330 if (!empty($coupon) && !empty($coupon['code'])) {
331 echo '<h3>' . sprintf(
332 /* translators: %s: Coupon code */
333 esc_html__('Coupon Code: <strong style="color: red">%s</strong> Redeem this coupon code to get free access to all our premium addons (Except Admin Bar Editor, RoleMaster Suite and Master Addons). Learn how to <a href="https://wpadminify.com/redeem-addons-using-coupon-code/" target="_blank">redeem coupon code?</a>', 'adminify'),
334 esc_attr($coupon['code'])
335 ) . '</h3> ';
336 }
337 }
338
339 echo '<style>
340 #fs_addons .fs-cards-list{ display: flex; }
341 #fs_addons .fs-cards-list .fs-card .fs-inner .fs-cta .button{
342 top: 112px;
343 right: 12px;
344 line-height: 26px !important;
345 border-radius: 3px !important;
346 }</style>';
347 }
348
349 public function is_eligible_for_coupon() {
350
351 $is_eligible = get_option('wp_adminify_addon__is_eligible_for_coupon', null);
352
353 if ( $is_eligible !== null ) return wp_validate_boolean($is_eligible);
354 $args = [
355 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())),
356 'action' => 'check_eligibility'
357 ];
358
359 $request_uri = add_query_arg($args, $this->server_url);
360
361 $response = wp_remote_get($request_uri);
362
363 if (!is_wp_error($response) && $response['response']['code'] === 200) {
364 $file_contents = wp_remote_retrieve_body($response);
365 $is_eligible = json_decode($file_contents, true);
366 update_option('wp_adminify_addon__is_eligible_for_coupon', wp_validate_boolean($is_eligible));
367 return $is_eligible;
368 }
369
370 return false;
371 }
372
373 public function maybe_delete_corrupted_coupon(){
374 $coupon_delete_check = get_option('wp_adminify_addon__coupon_is_deleted', false);
375 if($coupon_delete_check != true){
376 delete_option('wp_adminify_addon__coupon');
377 update_option('wp_adminify_addon__coupon_is_deleted', true);
378 }
379 }
380
381 public function maybe_create_and_get_coupon()
382 {
383 $this->maybe_delete_corrupted_coupon();
384 $coupon = get_option('wp_adminify_addon__coupon');
385
386 if (!empty($coupon)) return $coupon;
387
388 // communicate hit hserver get coupon
389 $args = [
390 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())),
391 'action' => 'get_coupon'
392 ];
393
394 $response = wp_remote_get(add_query_arg($args, $this->server_url));
395
396 if (!is_wp_error($response) && $response['response']['code'] === 200) {
397
398 $file_contents = wp_remote_retrieve_body($response);
399 $response_data = json_decode($file_contents, true);
400
401 if (!empty($response_data) && is_array($response_data) && !empty($response_data['id']) && !empty($response_data['code']) ) {
402 $coupon = [
403 'id' => $response_data['id'],
404 'code' => $response_data['code']
405 ];
406 update_option('wp_adminify_addon__coupon', $coupon);
407 }
408 }
409
410 return $coupon;
411 }
412
413
414 /**
415 * Header
416 */
417 public function header()
418 {
419 ?>
420 <div class='wp-adminify-addons-header'>
421 <div class='wp-adminify-addons-title'>
422 <h2>
423 <?php echo esc_html__('Add Ons for Adminify', 'adminify'); ?>
424 </h2>
425 <?php $this->jltwp_adminify_addons_check(); ?>
426 </div>
427 <div class='wp-adminify-addons-menu'>
428 <div class="wp-filter">
429 <ul class="filter-links">
430 <?php
431 $i = 0;
432
433 foreach ($this->menu_items as $menu) {
434 $class = str_replace(' ', '-', strtolower($menu['key']));
435 ?>
436 <li class="plugin-install-<?php echo esc_attr($class); ?>">
437 <a href="#" class="<?php echo esc_attr(0 === $i ? 'current' : ''); ?>" data-type="<?php echo esc_attr($menu['key']); ?>"><?php echo esc_html($menu['label']); ?></a>
438 </li>
439 <?php
440 ++$i;
441 }
442 ?>
443 </ul>
444
445 <form class="search-form wp-adminify-search-plugins mr-0" method="get">
446 <input type="hidden" name="tab" value="search">
447 <label class="screen-reader-text" for="search-plugins">
448 <?php echo esc_html__('Search Plugins', 'adminify'); ?>
449 </label>
450 <input type="search" name="s" id="search-plugins" value="" class="wp-filter-search" placeholder="<?php echo esc_html__('Search plugins...', 'adminify'); ?>">
451 <input type="submit" id="search-submit" class="button hide-if-js" value="<?php echo esc_html__('Search Plugins', 'adminify'); ?>">
452 </form>
453 </div>
454 </div>
455 </div>
456 <?php
457 }
458
459 /**
460 * Body
461 */
462 public function body()
463 {
464 ?>
465 <div class="wp-list-table widefat plugin-install">
466 <div id="the-list">
467 <?php
468 $this->plugins();
469 ?>
470 </div>
471 </div>
472 <?php
473 }
474
475 /**
476 * Body
477 */
478 public function plugins()
479 {
480
481 foreach ($this->plugins_list as $key => $plugin) {
482 $install_status = \install_plugin_install_status($plugin);
483 $classes = implode(' ', $plugin['type']);
484
485 $more_details = self_admin_url(
486 'plugin-install.php?tab=plugin-information&amp;plugin=' . esc_attr($plugin['slug']) .
487 '&amp;TB_iframe=true&amp;width=600&amp;height=550'
488 );
489
490 ?>
491 <div class="plugin-card plugin-card-<?php echo esc_attr($key); ?> <?php echo esc_attr($classes); ?>">
492 <div class="plugin-card-top">
493 <div class="name column-name">
494 <h3>
495 <a href="<?php echo esc_url($more_details); ?>" class="thickbox open-plugin-details-modal">
496 <?php echo esc_html($plugin['name']); ?>
497 <img src="<?php echo esc_url($plugin['icon']); ?>" class="plugin-icon" alt="">
498 </a>
499 </h3>
500 </div>
501 <div class="desc column-description">
502 <p><?php echo wp_kses_post($plugin['short_description']); ?></p>
503 </div>
504 <!-- Hover Popup -->
505 <?php if( !empty($plugin['pricing_url']) || !empty($plugin['view_details']) ) { ?>
506 <div class="adminify-plugin-details">
507 <?php if( !empty($plugin['view_details']) ) { ?>
508 <a href="<?php echo esc_url($plugin['view_details']); ?>" target="_blank" class="adminify-view-details"><?php echo esc_html__('View Details', 'adminify'); ?></a>
509 <?php } ?>
510
511 <?php if( !empty($plugin['pricing_url']) ) { ?>
512 <a href="<?php echo esc_url($plugin['pricing_url']); ?>" target="_blank"><?php echo esc_html__('Buy Now', 'adminify'); ?></a>
513 <?php } ?>
514 </div>
515 <?php } ?>
516 </div>
517 <div class="plugin-card-bottom">
518 <div class="column-downloaded">
519 <span class="plugin-status">
520 <?php
521 echo esc_html__('Status:', 'adminify');
522
523 if ('install' === $install_status['status']) {
524 ?>
525 <span class="plugin-status-not-install" data-plugin-url="<?php echo esc_attr($plugin['download_link']); ?>"><?php echo esc_html__('No Installed', 'adminify'); ?></span>
526 <?php
527 } elseif ('update_available' === $install_status['status']) {
528 if (is_plugin_active($install_status['file'])) {
529 ?>
530 <span class="plugin-status-active">
531 <?php echo esc_html__('Active', 'adminify'); ?>
532 </span>
533 <?php
534 } else {
535 ?>
536 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr(esc_attr($install_status['file'])); ?>">
537 <?php echo esc_html__('Inactive', 'adminify'); ?>
538 </span>
539 <?php
540 }
541 } elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) {
542 if (is_plugin_active($install_status['file'])) {
543 ?>
544 <span class="plugin-status-active">
545 <?php echo esc_html__('Active', 'adminify'); ?>
546 </span>
547 <?php
548 } elseif (current_user_can('activate_plugin', $install_status['file'])) {
549 ?>
550 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
551 <?php echo esc_html__('Inactive', 'adminify'); ?>
552 </span>
553 <?php
554 } else {
555 ?>
556 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
557 <?php echo esc_html__('Inactive', 'adminify'); ?>
558 </span>
559 <?php
560 }
561 }
562 ?>
563 </span>
564 </div>
565 <div class="column-compatibility">
566 <ul class="plugin-action-buttons">
567 <?php
568 if ('install' === $install_status['status']) {
569 ?>
570 <li>
571 <button class="install-now adminify-btn adminify-btn-outline-primary" data-install-url="<?php echo esc_attr($plugin['download_link']); ?>">
572 <?php echo esc_html__('Install Now', 'adminify'); ?>
573 </button>
574 </li>
575 <?php
576 } elseif ('update_available' === $install_status['status']) {
577 ?>
578 <li class="mr-0">
579 <button class="update-now button" data-plugin="<?php echo esc_attr($install_status['file']); ?>" data-slug="<?php echo esc_attr($plugin['slug']); ?>" data-update-url="<?php echo esc_attr($install_status['url']); ?>">
580 <?php echo esc_html__('Update Now', 'adminify'); ?>
581 </button>
582 </li>
583 <?php
584 } elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) {
585 if (is_plugin_active($install_status['file'])) {
586 ?>
587 <li class="mr-0">
588 <button type="button" class="adminify-btn adminify-btn-success" disabled="disabled">
589 <?php echo esc_html__('Activated', 'adminify'); ?>
590 </button>
591 </li>
592 <?php
593 } elseif (current_user_can('activate_plugin', $install_status['file'])) {
594 ?>
595 <button class="button activate-now" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
596 <?php echo esc_html__('Activate Now', 'adminify'); ?>
597 </button>
598 <?php
599 } else {
600 ?>
601 <li class="mr-0">
602 <button type="button" class="button button-disabled" disabled="disabled">
603 <?php echo esc_html__('Installed', 'adminify'); ?>
604 </button>
605 </li>
606 <?php
607 }
608 }
609 ?>
610 </ul>
611 </div>
612 </div>
613 </div>
614 <?php
615 }
616 }
617
618 /**
619 * Activate Plugins
620 *
621 * @author Jewel Theme <support@jeweltheme.com>
622 */
623 public function jltwp_adminify_addons_activate_plugin()
624 {
625 if (empty($_POST['plugin'])) {
626 return;
627 }
628 try {
629 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
630
631 if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) {
632 wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify')));
633 }
634
635 // Security check - only administrators can activate plugins
636 if (!current_user_can('activate_plugins')) {
637 wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify')));
638 }
639
640 $plugin = sanitize_text_field(wp_unslash($_POST['plugin']));
641 $plugin_links = array_values(wp_list_pluck($this->plugins_list, 'slug'));
642
643 if (!in_array(dirname($plugin), $plugin_links)) {
644 wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify')));
645 }
646
647 $result = activate_plugin($plugin);
648
649 if (is_wp_error($result)) {
650 wp_send_json_error(
651 array(
652 'mess' => $result->get_error_message(),
653 )
654 );
655 }
656 wp_send_json_success(
657 array(
658 'mess' => __('Activate success', 'adminify'),
659 )
660 );
661 } catch (\Exception $ex) {
662 wp_send_json_error(
663 array(
664 'mess' => __('Error exception.', 'adminify'),
665 array(
666 'error' => $ex,
667 ),
668 )
669 );
670 } catch (\Error $ex) {
671 wp_send_json_error(
672 array(
673 'mess' => __('Error.', 'adminify'),
674 array(
675 'error' => $ex,
676 ),
677 )
678 );
679 }
680 }
681
682 public function get_the_plugin_slug( $plugin ) {
683
684 // If the plugin is like myplugin/myplugin.php
685 if ( ! filter_var($plugin, FILTER_VALIDATE_URL) ) {
686 return dirname($plugin);
687 }
688
689 // If the plugin is from wordpress.org
690 if ( false !== strpos( $plugin, 'https://downloads.wordpress.org/plugin/' ) ) {
691 $plugin = str_replace( 'https://downloads.wordpress.org/plugin/', '', $plugin );
692 return str_replace( '.zip', '', $plugin );
693 }
694
695 // If the plugin is from local store
696 $plugin = explode( 'plugin_slug=', $plugin );
697 $plugin = explode( '&', $plugin[1] );
698 return $plugin[0];
699 }
700
701 /**
702 * Upgrade Plugins required Libraries
703 *
704 * @author Jewel Theme <support@jeweltheme.com>
705 */
706 public function jltwp_adminify_addons_upgrade_plugin( $params = null )
707 {
708 if ($params == null && empty($_POST['plugin'])) {
709 return;
710 }
711
712 try {
713 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
714 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
715 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
716 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
717
718 if($params == null){
719 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
720
721 if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) {
722 wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify')));
723 }
724 $plugin = sanitize_text_field(wp_unslash($_POST['plugin']));
725 }else{
726 $plugin = $params['plugin'];
727 }
728
729 // Security check - only administrators can install plugins
730 if (!current_user_can('install_plugins')) {
731 wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify')));
732 }
733
734 $plugin_slug = $this->get_the_plugin_slug( $plugin );
735
736 if ( ! array_key_exists( $plugin_slug, $this->plugins_list ) ) {
737 wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify')));
738 }
739
740 // Replace the user-supplied $plugin value with values derived
741 // from our trusted internal addons list, so that arbitrary
742 // input never reaches Plugin_Upgrader::install()/upgrade() or
743 // activate_plugin().
744 $trusted_install_source = isset($this->plugins_list[$plugin_slug]['download_link'])
745 ? $this->plugins_list[$plugin_slug]['download_link']
746 : '';
747
748 if($params == null){
749 $type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'install';
750 }else{
751 $type = 'install';
752 }
753 $skin = new \WP_Ajax_Upgrader_Skin();
754 $upgrader = new \Plugin_Upgrader($skin);
755
756 if ('install' === $type) {
757
758 if ( empty( $trusted_install_source ) ) {
759 wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify')));
760 }
761
762 $result = $upgrader->install( $trusted_install_source );
763 if ($params == null){
764 if (empty($result) || empty($upgrader->result)) {
765 wp_send_json_error(
766 array(
767 'mess' => 'Something is wrong',
768 )
769 );
770 }
771
772 if (is_wp_error($result)) {
773 wp_send_json_error(
774 array(
775 'mess' => $result->get_error_message(),
776 )
777 );
778 }
779 }else{
780 if(empty($result) || empty($upgrader->result)){
781 return;
782 }
783 }
784
785 $plugins = get_plugins('/' . $upgrader->result['destination_name']);
786 $plugin_data = end($plugins);
787 $plugin_data['slug'] = $upgrader->result['destination_name'];
788 $plugin_data['version'] = $plugin_data['Version'];
789
790 if (!empty($plugin_data) && !is_wp_error($plugin_data)) {
791
792 $install_status = \install_plugin_install_status($plugin_data);
793
794 $active_plugin = activate_plugin($install_status['file']);
795
796 if ($params == null){
797 if (is_wp_error($active_plugin)) {
798 wp_send_json_error(
799 array(
800 'mess' => $active_plugin->get_error_message(),
801 )
802 );
803 } else {
804 wp_send_json_success(
805 array(
806 'mess' => __('Install success', 'adminify'),
807 )
808 );
809 }
810 }
811 } else {
812 if ($params == null){
813 wp_send_json_error(
814 array(
815 'mess' => 'Error',
816 )
817 );
818
819 }
820 }
821 } else {
822
823 // Resolve the trusted plugin file path from the validated
824 // slug instead of trusting the raw $_POST value, so that
825 // is_plugin_active(), Plugin_Upgrader::upgrade() and
826 // activate_plugin() never receive attacker-supplied paths.
827 $installed_plugins = get_plugins( '/' . $plugin_slug );
828 if ( empty( $installed_plugins ) ) {
829 wp_send_json_error(array('mess' => __('Plugin not installed.', 'adminify')));
830 }
831 $trusted_plugin_file = $plugin_slug . '/' . key( $installed_plugins );
832
833 $is_active = is_plugin_active( $trusted_plugin_file );
834 $result = $upgrader->upgrade( $trusted_plugin_file );
835
836 if ($params == null){
837 if ( empty($result) || is_wp_error($result) ) {
838 wp_send_json_error(
839 array(
840 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Couldn\'t upgrade', 'adminify')
841 )
842 );
843 }
844 }
845
846 $active_status = activate_plugin( $trusted_plugin_file );
847
848 if ($params == null){
849 if ( empty($active_status) || is_wp_error($active_status) ) {
850 wp_send_json_error(
851 array(
852 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Activation Failed', 'adminify')
853 )
854 );
855 }
856
857 wp_send_json_success(
858 array(
859 'mess' => __('Update success', 'adminify'),
860 'active' => true,
861 )
862 );
863 }
864 }
865
866 } catch (\Exception $ex) {
867 if ($params == null){
868 wp_send_json_error(
869 array(
870 'mess' => __('Error exception.', 'adminify'),
871 array(
872 'error' => $ex,
873 ),
874 )
875 );
876 }
877 }
878 }
879
880 }
881 }
882