Bootstrap.php
4 years ago
Helper.php
4 years ago
Migration.php
4 years ago
Plugin_Usage_Tracker.php
4 years ago
WPDeveloper_Core_Installer.php
4 years ago
WPDeveloper_Notice.php
4 years ago
WPDeveloper_Plugin_Installer.php
4 years ago
WPDeveloper_Setup_Wizard.php
4 years ago
WPDeveloper_Plugin_Installer.php
204 lines
| 1 | <?php |
| 2 | namespace Essential_Addons_Elementor\Classes; |
| 3 | |
| 4 | if (!defined('ABSPATH')) { |
| 5 | exit; |
| 6 | } // Exit if accessed directly. |
| 7 | |
| 8 | use \WP_Error; |
| 9 | |
| 10 | class WPDeveloper_Plugin_Installer |
| 11 | { |
| 12 | public function __construct() |
| 13 | { |
| 14 | add_action('wp_ajax_wpdeveloper_install_plugin', [$this, 'ajax_install_plugin']); |
| 15 | add_action('wp_ajax_wpdeveloper_upgrade_plugin', [$this, 'ajax_upgrade_plugin']); |
| 16 | add_action('wp_ajax_wpdeveloper_activate_plugin', [$this, 'ajax_activate_plugin']); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * get_local_plugin_data |
| 21 | * |
| 22 | * @param mixed $basename |
| 23 | * @return array|false |
| 24 | */ |
| 25 | public function get_local_plugin_data($basename = '') |
| 26 | { |
| 27 | if (empty($basename)) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | if (!function_exists('get_plugins')) { |
| 32 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 33 | } |
| 34 | |
| 35 | $plugins = get_plugins(); |
| 36 | |
| 37 | if (!isset($plugins[$basename])) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | return $plugins[$basename]; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * get_remote_plugin_data |
| 46 | * |
| 47 | * @param mixed $slug |
| 48 | * @return mixed array|WP_Error |
| 49 | */ |
| 50 | public function get_remote_plugin_data($slug = '') |
| 51 | { |
| 52 | if (empty($slug)) { |
| 53 | return new WP_Error('empty_arg', __('Argument should not be empty.')); |
| 54 | } |
| 55 | |
| 56 | $response = wp_remote_post( |
| 57 | 'http://api.wordpress.org/plugins/info/1.0/', |
| 58 | [ |
| 59 | 'body' => [ |
| 60 | 'action' => 'plugin_information', |
| 61 | 'request' => serialize((object) [ |
| 62 | 'slug' => $slug, |
| 63 | 'fields' => [ |
| 64 | 'version' => false, |
| 65 | ], |
| 66 | ]), |
| 67 | ], |
| 68 | ] |
| 69 | ); |
| 70 | |
| 71 | if (is_wp_error($response)) { |
| 72 | return $response; |
| 73 | } |
| 74 | |
| 75 | return unserialize(wp_remote_retrieve_body($response)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * install_plugin |
| 80 | * |
| 81 | * @param mixed $slug |
| 82 | * @param bool $active |
| 83 | * @return mixed bool|WP_Error |
| 84 | */ |
| 85 | public function install_plugin($slug = '', $active = true) |
| 86 | { |
| 87 | if (empty($slug)) { |
| 88 | return new WP_Error('empty_arg', __('Argument should not be empty.')); |
| 89 | } |
| 90 | |
| 91 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 92 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 93 | include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 94 | |
| 95 | $plugin_data = $this->get_remote_plugin_data($slug); |
| 96 | |
| 97 | if (is_wp_error($plugin_data)) { |
| 98 | return $plugin_data; |
| 99 | } |
| 100 | |
| 101 | $upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin()); |
| 102 | |
| 103 | // install plugin |
| 104 | $install = $upgrader->install($plugin_data->download_link); |
| 105 | |
| 106 | if (is_wp_error($install)) { |
| 107 | return $install; |
| 108 | } |
| 109 | |
| 110 | // activate plugin |
| 111 | if ($install === true && $active) { |
| 112 | $active = activate_plugin($upgrader->plugin_info(), '', false, true); |
| 113 | |
| 114 | if (is_wp_error($active)) { |
| 115 | return $active; |
| 116 | } |
| 117 | |
| 118 | return $active === null; |
| 119 | } |
| 120 | |
| 121 | return $install; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * upgrade_plugin |
| 126 | * |
| 127 | * @param mixed $basename |
| 128 | * @return mixed bool|WP_Error |
| 129 | */ |
| 130 | public function upgrade_plugin($basename = '') |
| 131 | { |
| 132 | if (empty($slug)) { |
| 133 | return new WP_Error('empty_arg', __('Argument should not be empty.')); |
| 134 | } |
| 135 | |
| 136 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 137 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 138 | include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 139 | |
| 140 | $upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin()); |
| 141 | |
| 142 | // upgrade plugin |
| 143 | return $upgrader->upgrade($basename); |
| 144 | } |
| 145 | |
| 146 | public function ajax_install_plugin() |
| 147 | { |
| 148 | check_ajax_referer('essential-addons-elementor', 'security'); |
| 149 | |
| 150 | if(!current_user_can( 'install_plugins' )) { |
| 151 | wp_send_json_error(__('you are not allowed to do this action', 'essential-addons-for-elementor-lite')); |
| 152 | } |
| 153 | |
| 154 | $slug = isset( $_POST['slug'] ) ? sanitize_text_field( $_POST['slug'] ) : ''; |
| 155 | $result = $this->install_plugin( $slug ); |
| 156 | |
| 157 | if ( is_wp_error( $result ) ) { |
| 158 | wp_send_json_error( $result->get_error_message() ); |
| 159 | } |
| 160 | |
| 161 | wp_send_json_success(__('Plugin is installed successfully!', 'essential-addons-for-elementor-lite')); |
| 162 | } |
| 163 | |
| 164 | public function ajax_upgrade_plugin() |
| 165 | { |
| 166 | check_ajax_referer('essential-addons-elementor', 'security'); |
| 167 | //check user capabilities |
| 168 | if(!current_user_can( 'update_plugins' )) { |
| 169 | wp_send_json_error(__('you are not allowed to do this action', 'essential-addons-for-elementor-lite')); |
| 170 | } |
| 171 | |
| 172 | $basename = isset( $_POST['basename'] ) ? sanitize_text_field( $_POST['basename'] ) : ''; |
| 173 | $result = $this->upgrade_plugin( $basename ); |
| 174 | |
| 175 | if (is_wp_error($result)) { |
| 176 | wp_send_json_error($result->get_error_message()); |
| 177 | } |
| 178 | |
| 179 | wp_send_json_success(__('Plugin is updated successfully!', 'essential-addons-for-elementor-lite')); |
| 180 | } |
| 181 | |
| 182 | public function ajax_activate_plugin() |
| 183 | { |
| 184 | check_ajax_referer('essential-addons-elementor', 'security'); |
| 185 | |
| 186 | //check user capabilities |
| 187 | if(!current_user_can( 'activate_plugins' )) { |
| 188 | wp_send_json_error(__('you are not allowed to do this action', 'essential-addons-for-elementor-lite')); |
| 189 | } |
| 190 | |
| 191 | $basename = isset( $_POST['basename'] ) ? sanitize_text_field( $_POST['basename'] ) : ''; |
| 192 | $result = activate_plugin( $basename, '', false, true ); |
| 193 | |
| 194 | if ( is_wp_error( $result ) ) { |
| 195 | wp_send_json_error( $result->get_error_message() ); |
| 196 | } |
| 197 | |
| 198 | if ($result === false) { |
| 199 | wp_send_json_error(__('Plugin couldn\'t be activated.', 'essential-addons-for-elementor-lite')); |
| 200 | } |
| 201 | wp_send_json_success(__('Plugin is activated successfully!', 'essential-addons-for-elementor-lite')); |
| 202 | } |
| 203 | } |
| 204 |