| @@ -41,215 +41,62 @@ | ||
| 41 | 41 | $this->plugins_list = $this->plugins_list(); |
| 42 | 42 | |
| 43 | 43 | $this->includes(); |
| 44 | 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 | - } | |
| 45 | + add_action('admin_menu', array($this, 'admin_menu'), 1000); | |
| 46 | + // add_action('network_admin_menu', array($this, 'admin_menu'), $this->menu_order); | |
| 51 | 47 | add_action('wp_ajax_jltwp_adminify_addons_upgrade_plugin', array($this, 'jltwp_adminify_addons_upgrade_plugin')); |
| 52 | 48 | 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') ); | |
| 49 | + add_action('plugins_loaded', array($this, 'maybe_replace_addons_path'), 1000); // 1000 is important | |
| 59 | 50 | } |
| 60 | 51 | |
| 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 | - )); | |
| 52 | + public function maybe_replace_addons_path() { | |
| 67 | 53 | |
| 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 | - } | |
| 54 | + $addons = [ | |
| 55 | + 'sidebar-generator/adminify-sidebar-generator.php' => 'adminify-sidebar-generator/adminify-sidebar-generator.php' | |
| 56 | + ]; | |
| 74 | 57 | |
| 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 | - } | |
| 58 | + foreach ($addons as $old_plugin => $new_plugin) { | |
| 84 | 59 | |
| 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 | - } | |
| 60 | + $old_plugin_path = WP_PLUGIN_DIR . '/' . $old_plugin; | |
| 61 | + $new_plugin_path = WP_PLUGIN_DIR . '/' . $new_plugin; | |
| 99 | 62 | |
| 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 ] ) ) { | |
| 63 | + // Both files exist, delete the old one | |
| 64 | + if ( file_exists($old_plugin_path) && file_exists($new_plugin_path) ) { | |
| 65 | + unlink(dirname($old_plugin_path)); | |
| 147 | 66 | continue; |
| 148 | 67 | } |
| 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 | 68 | |
| 161 | - $this->jltwp_adminify_addons_upgrade_plugin( $params ); | |
| 162 | - } | |
| 69 | + // If the old file exists and the new file doesn't exist, rename the old file to the new file | |
| 70 | + if ( file_exists($old_plugin_path) && !file_exists($new_plugin_path) ) { | |
| 163 | 71 | |
| 164 | - return rest_ensure_response(['message' => __('Addons processed.', 'adminify'), 'addons' => $addons]); | |
| 165 | - } | |
| 72 | + // check if the old plugin is active | |
| 73 | + include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); | |
| 166 | 74 | |
| 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 | - } | |
| 75 | + $is_active = is_plugin_active( $old_plugin ); | |
| 177 | 76 | |
| 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 | - } | |
| 77 | + if ( $is_active ) { | |
| 78 | + // Deactivate the old plugin | |
| 79 | + deactivate_plugins( $old_plugin ); | |
| 80 | + // Rename the old plugin to the new plugin | |
| 81 | + rename( dirname($old_plugin_path), dirname($new_plugin_path) ); | |
| 183 | 82 | |
| 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 | - } | |
| 83 | + if ( file_exists($new_plugin_path) ) { | |
| 84 | + // Clear the plugin cache | |
| 85 | + wp_cache_delete( 'plugins', 'plugins' ); | |
| 188 | 86 | |
| 189 | - $plugin_path = WP_PLUGIN_DIR . '/' . $slug; | |
| 87 | + // Activate the new plugin | |
| 88 | + activate_plugin( $new_plugin ); | |
| 89 | + } | |
| 190 | 90 | |
| 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; | |
| 91 | + } else { | |
| 92 | + // Rename the old plugin to the new plugin | |
| 93 | + rename( dirname($old_plugin_path), dirname($new_plugin_path) ); | |
| 94 | + } | |
| 237 | 95 | } |
| 238 | 96 | |
| 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 | 97 | } |
| 246 | 98 | |
| 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 | 99 | } |
| 253 | 100 | |
| 254 | 101 | /** |
| 255 | 102 | * Includes |
| @@ -257,18 +104,17 @@ | ||
| 257 | 104 | * @author Jewel Theme <support@jeweltheme.com> |
| 258 | 105 | */ |
| 259 | 106 | public function includes() |
| 260 | 107 | { |
| 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'; | |
| 108 | + // if (!function_exists('install_plugin_install_status')) { | |
| 109 | + // require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 110 | + require_once(ABSPATH . '/wp-load.php'); | |
| 111 | + require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); | |
| 112 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
| 113 | + require_once(ABSPATH . 'wp-admin/includes/misc.php'); | |
| 114 | + require_once(ABSPATH . 'wp-admin/includes/plugin.php'); | |
| 115 | + require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); | |
| 116 | + // } | |
| 271 | 117 | } |
| 272 | 118 | |
| 273 | 119 | /** |
| 274 | 120 | * Menu Items |
| @@ -327,13 +173,12 @@ | ||
| 327 | 173 | if ( $this->is_eligible_for_coupon() ) { |
| 328 | 174 | // Get the coupon |
| 329 | 175 | $coupon = $this->maybe_create_and_get_coupon(); |
| 330 | 176 | 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'), | |
| 177 | + echo sprintf( | |
| 178 | + __('<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'), | |
| 334 | 179 | esc_attr($coupon['code']) |
| 335 | - ) . '</h3> '; | |
| 180 | + ); | |
| 336 | 181 | } |
| 337 | 182 | } |
| 338 | 183 | |
| 339 | 184 | echo '<style> |
| @@ -419,9 +264,9 @@ | ||
| 419 | 264 | ?> |
| 420 | 265 | <div class='wp-adminify-addons-header'> |
| 421 | 266 | <div class='wp-adminify-addons-title'> |
| 422 | 267 | <h2> |
| 423 | - <?php echo esc_html__('Add Ons for Adminify', 'adminify'); ?> | |
| 268 | + <?php echo esc_html__('Add Ons for WP Adminify', 'adminify'); ?> | |
| 424 | 269 | </h2> |
| 425 | 270 | <?php $this->jltwp_adminify_addons_check(); ?> |
| 426 | 271 | </div> |
| 427 | 272 | <div class='wp-adminify-addons-menu'> |
| @@ -631,12 +476,11 @@ | ||
| 631 | 476 | if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { |
| 632 | 477 | wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); |
| 633 | 478 | } |
| 634 | 479 | |
| 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 | - } | |
| 480 | + // if ((is_multisite() && !is_network_admin()) || !current_user_can('install_plugins')) { | |
| 481 | + // wp_send_json_error(array('mess' => __('Invalid access', 'adminify'))); | |
| 482 | + // } | |
| 639 | 483 | |
| 640 | 484 | $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); |
| 641 | 485 | $plugin_links = array_values(wp_list_pluck($this->plugins_list, 'slug')); |
| 642 | 486 | |
| @@ -702,11 +546,11 @@ | ||
| 702 | 546 | * Upgrade Plugins required Libraries |
| 703 | 547 | * |
| 704 | 548 | * @author Jewel Theme <support@jeweltheme.com> |
| 705 | 549 | */ |
| 706 | - public function jltwp_adminify_addons_upgrade_plugin( $params = null ) | |
| 550 | + public function jltwp_adminify_addons_upgrade_plugin() | |
| 707 | 551 | { |
| 708 | - if ($params == null && empty($_POST['plugin'])) { | |
| 552 | + if (empty($_POST['plugin'])) { | |
| 709 | 553 | return; |
| 710 | 554 | } |
| 711 | 555 | |
| 712 | 556 | try { |
| @@ -714,73 +558,48 @@ | ||
| 714 | 558 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 715 | 559 | require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 716 | 560 | require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 717 | 561 | |
| 718 | - if($params == null){ | |
| 719 | - $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; | |
| 562 | + $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; | |
| 720 | 563 | |
| 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']; | |
| 564 | + if (!wp_verify_nonce($nonce, 'jltwp_adminify_addons_nonce')) { | |
| 565 | + wp_send_json_error(array('mess' => __('Nonce is invalid', 'adminify'))); | |
| 727 | 566 | } |
| 728 | 567 | |
| 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 | - } | |
| 568 | + // if ((is_multisite() && !is_network_admin()) || !current_user_can('install_plugins')) { | |
| 569 | + // wp_send_json_error(array('mess' => __('Invalid access', 'adminify'))); | |
| 570 | + // } | |
| 733 | 571 | |
| 572 | + $plugin = sanitize_text_field(wp_unslash($_POST['plugin'])); | |
| 573 | + | |
| 734 | 574 | $plugin_slug = $this->get_the_plugin_slug( $plugin ); |
| 735 | 575 | |
| 736 | - if ( ! array_key_exists( $plugin_slug, $this->plugins_list ) ) { | |
| 576 | + if ( ! array_key_exists( $plugin_slug, $this->plugins_list) ) { | |
| 737 | 577 | wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); |
| 738 | 578 | } |
| 739 | 579 | |
| 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 | - } | |
| 580 | + $type = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'install'; | |
| 753 | 581 | $skin = new \WP_Ajax_Upgrader_Skin(); |
| 754 | 582 | $upgrader = new \Plugin_Upgrader($skin); |
| 755 | 583 | |
| 756 | 584 | if ('install' === $type) { |
| 757 | 585 | |
| 758 | - if ( empty( $trusted_install_source ) ) { | |
| 759 | - wp_send_json_error(array('mess' => __('Invalid plugin', 'adminify'))); | |
| 586 | + $result = $upgrader->install($plugin); | |
| 587 | + | |
| 588 | + if (empty($result) || empty($upgrader->result)) { | |
| 589 | + wp_send_json_error( | |
| 590 | + array( | |
| 591 | + 'mess' => 'Something is wrong', | |
| 592 | + ) | |
| 593 | + ); | |
| 760 | 594 | } |
| 761 | 595 | |
| 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 | - } | |
| 596 | + if (is_wp_error($result)) { | |
| 597 | + wp_send_json_error( | |
| 598 | + array( | |
| 599 | + 'mess' => $result->get_error_message(), | |
| 600 | + ) | |
| 601 | + ); | |
| 783 | 602 | } |
| 784 | 603 | |
| 785 | 604 | $plugins = get_plugins('/' . $upgrader->result['destination_name']); |
| 786 | 605 | $plugin_data = end($plugins); |
| @@ -791,91 +610,69 @@ | ||
| 791 | 610 | |
| 792 | 611 | $install_status = \install_plugin_install_status($plugin_data); |
| 793 | 612 | |
| 794 | 613 | $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){ | |
| 614 | + | |
| 615 | + if (is_wp_error($active_plugin)) { | |
| 813 | 616 | wp_send_json_error( |
| 814 | 617 | array( |
| 815 | - 'mess' => 'Error', | |
| 618 | + 'mess' => $active_plugin->get_error_message(), | |
| 816 | 619 | ) |
| 817 | 620 | ); |
| 621 | + } else { | |
| 622 | + wp_send_json_success( | |
| 623 | + array( | |
| 624 | + 'mess' => __('Install success', 'adminify'), | |
| 625 | + ) | |
| 626 | + ); | |
| 627 | + } | |
| 628 | + } else { | |
| 818 | 629 | |
| 819 | - } | |
| 630 | + wp_send_json_error( | |
| 631 | + array( | |
| 632 | + 'mess' => 'Error', | |
| 633 | + ) | |
| 634 | + ); | |
| 820 | 635 | } |
| 821 | 636 | } else { |
| 822 | 637 | |
| 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 ); | |
| 638 | + $is_active = is_plugin_active($plugin); | |
| 639 | + $result = $upgrader->upgrade($plugin); | |
| 832 | 640 | |
| 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 | - } | |
| 641 | + if ( empty($result) || is_wp_error($result) ) { | |
| 642 | + wp_send_json_error( | |
| 643 | + array( | |
| 644 | + 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Couldn\'t upgrade', 'adminify') | |
| 645 | + ) | |
| 646 | + ); | |
| 844 | 647 | } |
| 845 | 648 | |
| 846 | - $active_status = activate_plugin( $trusted_plugin_file ); | |
| 649 | + $active_status = activate_plugin($plugin); | |
| 847 | 650 | |
| 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( | |
| 651 | + if ( empty($active_status) || is_wp_error($active_status) ) { | |
| 652 | + wp_send_json_error( | |
| 858 | 653 | array( |
| 859 | - 'mess' => __('Update success', 'adminify'), | |
| 860 | - 'active' => true, | |
| 654 | + 'mess' => is_wp_error($result) ? $result->get_error_message() : __('Activation Failed', 'adminify') | |
| 861 | 655 | ) |
| 862 | 656 | ); |
| 863 | 657 | } |
| 864 | - } | |
| 865 | - | |
| 866 | - } catch (\Exception $ex) { | |
| 867 | - if ($params == null){ | |
| 868 | - wp_send_json_error( | |
| 658 | + | |
| 659 | + wp_send_json_success( | |
| 869 | 660 | array( |
| 870 | - 'mess' => __('Error exception.', 'adminify'), | |
| 871 | - array( | |
| 872 | - 'error' => $ex, | |
| 873 | - ), | |
| 661 | + 'mess' => __('Update success', 'adminify'), | |
| 662 | + 'active' => true, | |
| 874 | 663 | ) |
| 875 | 664 | ); |
| 876 | 665 | } |
| 666 | + } catch (\Exception $ex) { | |
| 667 | + wp_send_json_error( | |
| 668 | + array( | |
| 669 | + 'mess' => __('Error exception.', 'adminify'), | |
| 670 | + array( | |
| 671 | + 'error' => $ex, | |
| 672 | + ), | |
| 673 | + ) | |
| 674 | + ); | |
| 877 | 675 | } |
| 878 | 676 | } |
| 879 | - | |
| 880 | 677 | } |
| 881 | 678 | } |