PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.1.11
Adminify – White Label, Admin Menu Editor, Login Customizer v4.1.11
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.11, at Libs/Addons.php

818 lines 34.0 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 add_action('plugins_loaded', array($this, 'maybe_replace_addons_path'), 1000); // 1000 is important
54 add_action( 'rest_api_init', array( $this , 'jltwp_adminify_addons_rest_routes') );
55 }
56
57 public function jltwp_adminify_addons_rest_routes() {
58 register_rest_route('adminify/v1', '/get-addons-list', array(
59 'methods' => 'GET',
60 'callback' => [$this, 'jltwp_adminify_get_addons_plugins_list'],
61 'permission_callback' => [$this, 'adminify_is_admin_user'],
62 ));
63
64 register_rest_route('adminify/v1', '/install-addons', array(
65 'methods' => 'POST',
66 'callback' => [$this, 'jltwp_adminify_install_addons'],
67 'permission_callback' => [$this, 'adminify_verify_nonce_and_permissions'],
68 ));
69 }
70
71 public function adminify_is_admin_user() {
72 return current_user_can('manage_options');
73 }
74
75 public function adminify_verify_nonce_and_permissions() {
76 // Check user
77 if ( ! current_user_can('manage_options') ) {
78 return new WP_Error('forbidden', 'You are not allowed to do this.', array('status' => 403));
79 }
80
81 // Check nonce from header
82 $nonce = $_SERVER['HTTP_X_WP_NONCE'] ?? '';
83 if ( ! wp_verify_nonce($nonce, 'wp_rest') ) {
84 return new WP_Error('rest_cookie_invalid_nonce', __('Invalid nonce.'), array('status' => 403));
85 }
86 if ( is_multisite() && ! is_super_admin() ) {
87 return new WP_Error('not_allowed', 'Super admin only on multisite.', array('status' => 403));
88 }
89
90 return true;
91 }
92
93
94 public function jltwp_adminify_get_addons_plugins_list() {
95 $plugins = $this->plugins_list;
96 unset($plugins['master-addons']);
97 $all_plugins = get_plugins();
98 $active_plugins = get_option('active_plugins');
99 foreach( $plugins as $slug => $plugin){
100 foreach ($all_plugins as $plugin_file => $plugin_data) {
101 if (strpos($plugin_file, $slug) !== false) {
102 $plugins[$slug]["status"] = 'installed';
103
104 if (in_array($plugin_file, $active_plugins)) {
105 $plugins[$slug]["status"] = 'activated';
106 }
107 break;
108 }
109 }
110 if( !isset($plugins[$slug]["status"])) $plugins[$slug]["status"] = 'not-installed';
111
112 }
113
114 return rest_ensure_response($plugins);
115
116 }
117
118
119 public function jltwp_adminify_install_addons( $request ) {
120 $addons = $request->get_param('addons');
121 if ( empty($addons) || !is_array($addons) ) {
122 return new WP_Error('no_addons', 'No addons were selected.', array('status' => 400));
123 }
124
125 $plugins_list = $this->jltwp_adminify_get_addons_plugins_list()->data;
126 foreach( $addons as $key => $plugin ){
127 if($plugins_list[$plugin]['status'] == "activated") continue;
128 if($plugins_list[$plugin]['status'] == "installed") {
129 $this->jltwp_adminify_activate_plugin_by_slug($plugin);
130 continue;
131 }
132 $params = [
133 'request_type' => 'rest',
134 'plugin' => $plugins_list[$plugin]['download_link'],
135 ];
136
137 $this->jltwp_adminify_addons_upgrade_plugin($params);
138 }
139
140 return rest_ensure_response(['message' => 'Addons installed', 'addons' => $addons]);
141 }
142
143 function jltwp_adminify_activate_plugin_by_slug($slug) {
144 $plugin_path = WP_PLUGIN_DIR . '/' . $slug;
145
146 if (!is_dir($plugin_path)) {
147 return;
148 }
149
150 $plugin_files = glob("$plugin_path/*.php");
151 if (!$plugin_files || empty($plugin_files)) {
152 return;
153 }
154
155 $main_plugin_file = basename($plugin_files[0]);
156 $plugin_relative_path = $slug . '/' . $main_plugin_file;
157
158 if (is_plugin_active($plugin_relative_path)) {
159 return;
160 }
161
162 activate_plugin($plugin_relative_path);
163 }
164
165 public function maybe_replace_addons_path() {
166
167 $addons = [
168 'sidebar-generator/adminify-sidebar-generator.php' => 'adminify-sidebar-generator/adminify-sidebar-generator.php'
169 ];
170
171 foreach ($addons as $old_plugin => $new_plugin) {
172
173 $old_plugin_path = WP_PLUGIN_DIR . '/' . $old_plugin;
174 $new_plugin_path = WP_PLUGIN_DIR . '/' . $new_plugin;
175
176 // Both files exist, delete the old one
177 if ( file_exists($old_plugin_path) && file_exists($new_plugin_path) ) {
178 unlink(dirname($old_plugin_path));
179 continue;
180 }
181
182 // If the old file exists and the new file doesn't exist, rename the old file to the new file
183 if ( file_exists($old_plugin_path) && !file_exists($new_plugin_path) ) {
184
185 // check if the old plugin is active
186 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
187
188 $is_active = is_plugin_active( $old_plugin );
189
190 if ( $is_active ) {
191 // Deactivate the old plugin
192 deactivate_plugins( $old_plugin );
193 // Rename the old plugin to the new plugin
194 rename( dirname($old_plugin_path), dirname($new_plugin_path) );
195
196 if ( file_exists($new_plugin_path) ) {
197 // Clear the plugin cache
198 wp_cache_delete( 'plugins', 'plugins' );
199
200 // Activate the new plugin
201 activate_plugin( $new_plugin );
202 }
203
204 } else {
205 // Rename the old plugin to the new plugin
206 rename( dirname($old_plugin_path), dirname($new_plugin_path) );
207 }
208 }
209
210 }
211
212 }
213
214 /**
215 * Includes
216 *
217 * @author Jewel Theme <support@jeweltheme.com>
218 */
219 public function includes()
220 {
221 // if (!function_exists('install_plugin_install_status')) {
222 // require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
223 require_once(ABSPATH . '/wp-load.php');
224 require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
225 require_once(ABSPATH . 'wp-admin/includes/file.php');
226 require_once(ABSPATH . 'wp-admin/includes/misc.php');
227 require_once(ABSPATH . 'wp-admin/includes/plugin.php');
228 require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
229 // }
230 }
231
232 /**
233 * Menu Items
234 *
235 * @author Jewel Theme <support@jeweltheme.com>
236 */
237 public function menu_items()
238 {
239 return array();
240 }
241
242 /**
243 * Plugins list
244 *
245 * @author Jewel Theme <support@jeweltheme.com>
246 */
247 public function plugins_list()
248 {
249 return array();
250 }
251
252 /**
253 * Admin submenu
254 */
255 public function admin_menu()
256 {
257 }
258
259 /**
260 * Render addons plugins body
261 */
262 public function render_addons_plugins()
263 {
264 ?>
265 <div class='wp-adminify-addons-wrapper'>
266 <?php $this->header(); ?>
267 <?php $this->body(); ?>
268 </div>
269 <?php
270 }
271
272
273 /**
274 * Addons License Header Check
275 *
276 * @return void
277 */
278
279 public function jltwp_adminify_addons_check()
280 {
281
282 $license = jltwp_adminify()->_get_license();
283
284 if (!is_object($license) || !$license->is_valid() || !$license->is_active()) return;
285
286 if ( $this->is_eligible_for_coupon() ) {
287 // Get the coupon
288 $coupon = $this->maybe_create_and_get_coupon();
289 if (!empty($coupon) && !empty($coupon['code'])) {
290 echo sprintf(
291 __('<h3>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></h3> ', 'adminify'),
292 esc_attr($coupon['code'])
293 );
294 }
295 }
296
297 echo '<style>
298 #fs_addons .fs-cards-list{ display: flex; }
299 #fs_addons .fs-cards-list .fs-card .fs-inner .fs-cta .button{
300 top: 112px;
301 right: 12px;
302 line-height: 26px !important;
303 border-radius: 3px !important;
304 }</style>';
305 }
306
307 public function is_eligible_for_coupon() {
308
309 $is_eligible = get_option('wp_adminify_addon__is_eligible_for_coupon', null);
310
311 if ( $is_eligible !== null ) return wp_validate_boolean($is_eligible);
312 $args = [
313 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())),
314 'action' => 'check_eligibility'
315 ];
316
317 $request_uri = add_query_arg($args, $this->server_url);
318
319 $response = wp_remote_get($request_uri);
320
321 if (!is_wp_error($response) && $response['response']['code'] === 200) {
322 $file_contents = wp_remote_retrieve_body($response);
323 $is_eligible = json_decode($file_contents, true);
324 update_option('wp_adminify_addon__is_eligible_for_coupon', wp_validate_boolean($is_eligible));
325 return $is_eligible;
326 }
327
328 return false;
329 }
330
331 public function maybe_delete_corrupted_coupon(){
332 $coupon_delete_check = get_option('wp_adminify_addon__coupon_is_deleted', false);
333 if($coupon_delete_check != true){
334 delete_option('wp_adminify_addon__coupon');
335 update_option('wp_adminify_addon__coupon_is_deleted', true);
336 }
337 }
338
339 public function maybe_create_and_get_coupon()
340 {
341 $this->maybe_delete_corrupted_coupon();
342 $coupon = get_option('wp_adminify_addon__coupon');
343
344 if (!empty($coupon)) return $coupon;
345
346 // communicate hit hserver get coupon
347 $args = [
348 'license' => base64_encode(json_encode(jltwp_adminify()->_get_license())),
349 'action' => 'get_coupon'
350 ];
351
352 $response = wp_remote_get(add_query_arg($args, $this->server_url));
353
354 if (!is_wp_error($response) && $response['response']['code'] === 200) {
355
356 $file_contents = wp_remote_retrieve_body($response);
357 $response_data = json_decode($file_contents, true);
358
359 if (!empty($response_data) && is_array($response_data) && !empty($response_data['id']) && !empty($response_data['code']) ) {
360 $coupon = [
361 'id' => $response_data['id'],
362 'code' => $response_data['code']
363 ];
364 update_option('wp_adminify_addon__coupon', $coupon);
365 }
366 }
367
368 return $coupon;
369 }
370
371
372 /**
373 * Header
374 */
375 public function header()
376 {
377 ?>
378 <div class='wp-adminify-addons-header'>
379 <div class='wp-adminify-addons-title'>
380 <h2>
381 <?php echo esc_html__('Add Ons for WP Adminify', 'adminify'); ?>
382 </h2>
383 <?php $this->jltwp_adminify_addons_check(); ?>
384 </div>
385 <div class='wp-adminify-addons-menu'>
386 <div class="wp-filter">
387 <ul class="filter-links">
388 <?php
389 $i = 0;
390
391 foreach ($this->menu_items as $menu) {
392 $class = str_replace(' ', '-', strtolower($menu['key']));
393 ?>
394 <li class="plugin-install-<?php echo esc_attr($class); ?>">
395 <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>
396 </li>
397 <?php
398 ++$i;
399 }
400 ?>
401 </ul>
402
403 <form class="search-form wp-adminify-search-plugins mr-0" method="get">
404 <input type="hidden" name="tab" value="search">
405 <label class="screen-reader-text" for="search-plugins">
406 <?php echo esc_html__('Search Plugins', 'adminify'); ?>
407 </label>
408 <input type="search" name="s" id="search-plugins" value="" class="wp-filter-search" placeholder="<?php echo esc_html__('Search plugins...', 'adminify'); ?>">
409 <input type="submit" id="search-submit" class="button hide-if-js" value="<?php echo esc_html__('Search Plugins', 'adminify'); ?>">
410 </form>
411 </div>
412 </div>
413 </div>
414 <?php
415 }
416
417 /**
418 * Body
419 */
420 public function body()
421 {
422 ?>
423 <div class="wp-list-table widefat plugin-install">
424 <div id="the-list">
425 <?php
426 $this->plugins();
427 ?>
428 </div>
429 </div>
430 <?php
431 }
432
433 /**
434 * Body
435 */
436 public function plugins()
437 {
438
439 foreach ($this->plugins_list as $key => $plugin) {
440 $install_status = \install_plugin_install_status($plugin);
441 $classes = implode(' ', $plugin['type']);
442
443 $more_details = self_admin_url(
444 'plugin-install.php?tab=plugin-information&amp;plugin=' . esc_attr($plugin['slug']) .
445 '&amp;TB_iframe=true&amp;width=600&amp;height=550'
446 );
447
448 ?>
449 <div class="plugin-card plugin-card-<?php echo esc_attr($key); ?> <?php echo esc_attr($classes); ?>">
450 <div class="plugin-card-top">
451 <div class="name column-name">
452 <h3>
453 <a href="<?php echo esc_url($more_details); ?>" class="thickbox open-plugin-details-modal">
454 <?php echo esc_html($plugin['name']); ?>
455 <img src="<?php echo esc_url($plugin['icon']); ?>" class="plugin-icon" alt="">
456 </a>
457 </h3>
458 </div>
459 <div class="desc column-description">
460 <p><?php echo wp_kses_post($plugin['short_description']); ?></p>
461 </div>
462 <!-- Hover Popup -->
463 <?php if( !empty($plugin['pricing_url']) || !empty($plugin['view_details']) ) { ?>
464 <div class="adminify-plugin-details">
465 <?php if( !empty($plugin['view_details']) ) { ?>
466 <a href="<?php echo esc_url($plugin['view_details']); ?>" target="_blank" class="adminify-view-details"><?php echo esc_html__('View Details', 'adminify'); ?></a>
467 <?php } ?>
468
469 <?php if( !empty($plugin['pricing_url']) ) { ?>
470 <a href="<?php echo esc_url($plugin['pricing_url']); ?>" target="_blank"><?php echo esc_html__('Buy Now', 'adminify'); ?></a>
471 <?php } ?>
472 </div>
473 <?php } ?>
474 </div>
475 <div class="plugin-card-bottom">
476 <div class="column-downloaded">
477 <span class="plugin-status">
478 <?php
479 echo esc_html__('Status:', 'adminify');
480
481 if ('install' === $install_status['status']) {
482 ?>
483 <span class="plugin-status-not-install" data-plugin-url="<?php echo esc_attr($plugin['download_link']); ?>"><?php echo esc_html__('No Installed', 'adminify'); ?></span>
484 <?php
485 } elseif ('update_available' === $install_status['status']) {
486 if (is_plugin_active($install_status['file'])) {
487 ?>
488 <span class="plugin-status-active">
489 <?php echo esc_html__('Active', 'adminify'); ?>
490 </span>
491 <?php
492 } else {
493 ?>
494 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr(esc_attr($install_status['file'])); ?>">
495 <?php echo esc_html__('Inactive', 'adminify'); ?>
496 </span>
497 <?php
498 }
499 } elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) {
500 if (is_plugin_active($install_status['file'])) {
501 ?>
502 <span class="plugin-status-active">
503 <?php echo esc_html__('Active', 'adminify'); ?>
504 </span>
505 <?php
506 } elseif (current_user_can('activate_plugin', $install_status['file'])) {
507 ?>
508 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
509 <?php echo esc_html__('Inactive', 'adminify'); ?>
510 </span>
511 <?php
512 } else {
513 ?>
514 <span class="plugin-status-inactive" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
515 <?php echo esc_html__('Inactive', 'adminify'); ?>
516 </span>
517 <?php
518 }
519 }
520 ?>
521 </span>
522 </div>
523 <div class="column-compatibility">
524 <ul class="plugin-action-buttons">
525 <?php
526 if ('install' === $install_status['status']) {
527 ?>
528 <li>
529 <button class="install-now adminify-btn adminify-btn-outline-primary" data-install-url="<?php echo esc_attr($plugin['download_link']); ?>">
530 <?php echo esc_html__('Install Now', 'adminify'); ?>
531 </button>
532 </li>
533 <?php
534 } elseif ('update_available' === $install_status['status']) {
535 ?>
536 <li class="mr-0">
537 <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']); ?>">
538 <?php echo esc_html__('Update Now', 'adminify'); ?>
539 </button>
540 </li>
541 <?php
542 } elseif (('latest_installed' === $install_status['status']) || ('newer_installed' === $install_status['status'])) {
543 if (is_plugin_active($install_status['file'])) {
544 ?>
545 <li class="mr-0">
546 <button type="button" class="adminify-btn adminify-btn-success" disabled="disabled">
547 <?php echo esc_html__('Activated', 'adminify'); ?>
548 </button>
549 </li>
550 <?php
551 } elseif (current_user_can('activate_plugin', $install_status['file'])) {
552 ?>
553 <button class="button activate-now" data-plugin-file="<?php echo esc_attr($install_status['file']); ?>">
554 <?php echo esc_html__('Activate Now', 'adminify'); ?>
555 </button>
556 <?php
557 } else {
558 ?>
559 <li class="mr-0">
560 <button type="button" class="button button-disabled" disabled="disabled">
561 <?php echo esc_html__('Installed', 'adminify'); ?>
562 </button>
563 </li>
564 <?php
565 }
566 }
567 ?>
568 </ul>
569 </div>
570 </div>
571 </div>
572 <?php
573 }
574 }
575
576 /**
577 * Activate Plugins
578 *
579 * @author Jewel Theme <support@jeweltheme.com>
580 */
581 public function jltwp_adminify_addons_activate_plugin()
582 {
583 if (empty($_POST['plugin'])) {
584 return;
585 }
586 try {
587 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
588
589 if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) {
590 wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify')));
591 }
592
593 // Security check - only administrators can activate plugins
594 if (!current_user_can('activate_plugins')) {
595 wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify')));
596 }
597
598 $plugin = sanitize_text_field(wp_unslash($_POST['plugin']));
599 $plugin_links = array_values(wp_list_pluck($this->plugins_list, 'slug'));
600
601 if (!in_array(dirname($plugin), $plugin_links)) {
602 wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify')));
603 }
604
605 $result = activate_plugin($plugin);
606
607 if (is_wp_error($result)) {
608 wp_send_json_error(
609 array(
610 'mess' => $result->get_error_message(),
611 )
612 );
613 }
614 wp_send_json_success(
615 array(
616 'mess' => __('Activate success', 'adminify'),
617 )
618 );
619 } catch (\Exception $ex) {
620 wp_send_json_error(
621 array(
622 'mess' => __('Error exception.', 'adminify'),
623 array(
624 'error' => $ex,
625 ),
626 )
627 );
628 } catch (\Error $ex) {
629 wp_send_json_error(
630 array(
631 'mess' => __('Error.', 'adminify'),
632 array(
633 'error' => $ex,
634 ),
635 )
636 );
637 }
638 }
639
640 public function get_the_plugin_slug( $plugin ) {
641
642 // If the plugin is like myplugin/myplugin.php
643 if ( ! filter_var($plugin, FILTER_VALIDATE_URL) ) {
644 return dirname($plugin);
645 }
646
647 // If the plugin is from wordpress.org
648 if ( false !== strpos( $plugin, 'https://downloads.wordpress.org/plugin/' ) ) {
649 $plugin = str_replace( 'https://downloads.wordpress.org/plugin/', '', $plugin );
650 return str_replace( '.zip', '', $plugin );
651 }
652
653 // If the plugin is from local store
654 $plugin = explode( 'plugin_slug=', $plugin );
655 $plugin = explode( '&', $plugin[1] );
656 return $plugin[0];
657 }
658
659 /**
660 * Upgrade Plugins required Libraries
661 *
662 * @author Jewel Theme <support@jeweltheme.com>
663 */
664 public function jltwp_adminify_addons_upgrade_plugin( $params = null )
665 {
666 if ($params == null && empty($_POST['plugin'])) {
667 return;
668 }
669
670 try {
671 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
672 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
673 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
674 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
675
676 if($params == null){
677 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
678
679 if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) {
680 wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify')));
681 }
682 $plugin = sanitize_text_field(wp_unslash($_POST['plugin']));
683 }else{
684 $plugin = $params['plugin'];
685 }
686
687 // Security check - only administrators can install plugins
688 if (!current_user_can('install_plugins')) {
689 wp_send_json_error(array('mess' => __('You do not have permission to perform this action.', 'adminify')));
690 }
691
692 $plugin_slug = $this->get_the_plugin_slug( $plugin );
693
694 if ( ! array_key_exists( $plugin_slug, $this->plugins_list) ) {
695 wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify')));
696 }
697
698 if($params == null){
699 $type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'install';
700 }else{
701 $type = 'install';
702 }
703 $skin = new \WP_Ajax_Upgrader_Skin();
704 $upgrader = new \Plugin_Upgrader($skin);
705
706 if ('install' === $type) {
707
708 $result = $upgrader->install($plugin);
709 if ($params == null){
710 if (empty($result) || empty($upgrader->result)) {
711 wp_send_json_error(
712 array(
713 'mess' => 'Something is wrong',
714 )
715 );
716 }
717
718 if (is_wp_error($result)) {
719 wp_send_json_error(
720 array(
721 'mess' => $result->get_error_message(),
722 )
723 );
724 }
725 }else{
726 if(empty($result) || empty($upgrader->result)){
727 return;
728 }
729 }
730
731 $plugins = get_plugins('/' . $upgrader->result['destination_name']);
732 $plugin_data = end($plugins);
733 $plugin_data['slug'] = $upgrader->result['destination_name'];
734 $plugin_data['version'] = $plugin_data['Version'];
735
736 if (!empty($plugin_data) && !is_wp_error($plugin_data)) {
737
738 $install_status = \install_plugin_install_status($plugin_data);
739
740 $active_plugin = activate_plugin($install_status['file']);
741
742 if ($params == null){
743 if (is_wp_error($active_plugin)) {
744 wp_send_json_error(
745 array(
746 'mess' => $active_plugin->get_error_message(),
747 )
748 );
749 } else {
750 wp_send_json_success(
751 array(
752 'mess' => __('Install success', 'adminify'),
753 )
754 );
755 }
756 }
757 } else {
758 if ($params == null){
759 wp_send_json_error(
760 array(
761 'mess' => 'Error',
762 )
763 );
764
765 }
766 }
767 } else {
768
769 $is_active = is_plugin_active($plugin);
770 $result = $upgrader->upgrade($plugin);
771
772 if ($params == null){
773 if ( empty($result) || is_wp_error($result) ) {
774 wp_send_json_error(
775 array(
776 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Couldn\'t upgrade', 'adminify')
777 )
778 );
779 }
780 }
781
782 $active_status = activate_plugin($plugin);
783
784 if ($params == null){
785 if ( empty($active_status) || is_wp_error($active_status) ) {
786 wp_send_json_error(
787 array(
788 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Activation Failed', 'adminify')
789 )
790 );
791 }
792
793 wp_send_json_success(
794 array(
795 'mess' => __('Update success', 'adminify'),
796 'active' => true,
797 )
798 );
799 }
800 }
801
802 } catch (\Exception $ex) {
803 if ($params == null){
804 wp_send_json_error(
805 array(
806 'mess' => __('Error exception.', 'adminify'),
807 array(
808 'error' => $ex,
809 ),
810 )
811 );
812 }
813 }
814 }
815
816 }
817 }
818