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

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