| @@ -16,9 +16,8 @@ | ||
| 16 | 16 | public function __construct() { |
| 17 | 17 | add_action( 'wp_ajax_subscrpt_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) ); |
| 18 | 18 | add_action( 'wp_ajax_subscrpt_activate_woocommerce_plugin', array( $this, 'wps_subscription_activate_woocommerce_plugin' ) ); |
| 19 | 19 | add_action( 'wp_ajax_subscrpt_install_integration_plugin', array( $this, 'install_integration_plugin' ) ); |
| 20 | - add_action( 'wp_ajax_subscrpt_activate_integration_plugin', array( $this, 'activate_integration_plugin' ) ); | |
| 21 | 20 | } |
| 22 | 21 | |
| 23 | 22 | /** |
| 24 | 23 | * Install the WooCommerce Plugin. |
| @@ -113,43 +112,13 @@ | ||
| 113 | 112 | if ( empty( $plugin_slug ) ) { |
| 114 | 113 | wp_send_json_error( array( 'message' => __( 'Plugin slug is required.', 'subscription' ) ), 400 ); |
| 115 | 114 | } |
| 116 | 115 | |
| 117 | - // Already installed but inactive: the upgrader would refuse with | |
| 118 | - // "Destination folder already exists", so activate it instead. | |
| 119 | - $installed_file = self::get_installed_plugin_file( $plugin_slug ); | |
| 120 | - | |
| 121 | - if ( $installed_file ) { | |
| 122 | - if ( ! current_user_can( 'activate_plugins' ) ) { | |
| 123 | - wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 ); | |
| 124 | - } | |
| 125 | - | |
| 126 | - $activate = activate_plugin( $installed_file ); | |
| 127 | - | |
| 128 | - if ( is_wp_error( $activate ) ) { | |
| 129 | - wp_send_json_error( array( 'message' => $activate->get_error_message() ), 500 ); | |
| 130 | - } | |
| 131 | - | |
| 132 | - wp_send_json_success(); | |
| 133 | - } | |
| 134 | - | |
| 135 | 116 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 136 | 117 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 137 | 118 | include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 138 | 119 | include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 139 | 120 | |
| 140 | - // Already on disk: installing again fails on the existing folder, so | |
| 141 | - // activate what is there instead. | |
| 142 | - foreach ( array_keys( get_plugins() ) as $installed_file ) { | |
| 143 | - if ( 0 === strpos( $installed_file, $plugin_slug . '/' ) ) { | |
| 144 | - $activate = activate_plugin( $installed_file ); | |
| 145 | - if ( is_wp_error( $activate ) ) { | |
| 146 | - wp_send_json_error( array( 'message' => $activate->get_error_message() ), 500 ); | |
| 147 | - } | |
| 148 | - wp_send_json_success(); | |
| 149 | - } | |
| 150 | - } | |
| 151 | - | |
| 152 | 121 | $cache_key = 'subscrpt_plugin_api_' . sanitize_key( $plugin_slug ); |
| 153 | 122 | $api = get_transient( $cache_key ); |
| 154 | 123 | |
| 155 | 124 | if ( false === $api ) { |
| @@ -185,23 +154,19 @@ | ||
| 185 | 154 | |
| 186 | 155 | set_transient( $cache_key, $api, 12 * HOUR_IN_SECONDS ); |
| 187 | 156 | } |
| 188 | 157 | |
| 189 | - $skin = new \WP_Ajax_Upgrader_Skin(); | |
| 190 | - $upgrader = new \Plugin_Upgrader( $skin ); | |
| 158 | + $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); | |
| 191 | 159 | $result = $upgrader->install( $api->download_link ); |
| 192 | 160 | |
| 161 | + ob_end_clean(); | |
| 162 | + | |
| 193 | 163 | if ( is_wp_error( $result ) ) { |
| 194 | 164 | wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 ); |
| 195 | 165 | } |
| 196 | 166 | |
| 197 | - // The skin collects the reason (folder exists, no filesystem access, …) | |
| 198 | - // while install() itself only returns null. | |
| 199 | 167 | if ( ! $result ) { |
| 200 | - $message = $skin->get_errors()->has_errors() | |
| 201 | - ? $skin->get_error_messages() | |
| 202 | - : __( 'Plugin installation failed.', 'subscription' ); | |
| 203 | - wp_send_json_error( array( 'message' => $message ), 500 ); | |
| 168 | + wp_send_json_error( array( 'message' => __( 'Plugin installation failed.', 'subscription' ) ), 500 ); | |
| 204 | 169 | } |
| 205 | 170 | |
| 206 | 171 | $plugin_file = $upgrader->plugin_info(); |
| 207 | 172 | |
| @@ -212,57 +177,8 @@ | ||
| 212 | 177 | $activate = activate_plugin( $plugin_file ); |
| 213 | 178 | |
| 214 | 179 | if ( is_wp_error( $activate ) ) { |
| 215 | 180 | wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) ); |
| 216 | - } | |
| 217 | - | |
| 218 | - wp_send_json_success(); | |
| 219 | - } | |
| 220 | - | |
| 221 | - /** | |
| 222 | - * Main file of an installed plugin, looked up by its wp.org slug. | |
| 223 | - * | |
| 224 | - * @param string $plugin_slug The wp.org plugin slug, i.e. its directory name. | |
| 225 | - * @return string Plugin file relative to the plugins directory, or an empty string when not installed. | |
| 226 | - */ | |
| 227 | - public static function get_installed_plugin_file( $plugin_slug ) { | |
| 228 | - if ( ! function_exists( 'get_plugins' ) ) { | |
| 229 | - include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 230 | - } | |
| 231 | - | |
| 232 | - foreach ( array_keys( get_plugins() ) as $plugin_file ) { | |
| 233 | - if ( strtok( $plugin_file, '/' ) === $plugin_slug ) { | |
| 234 | - return $plugin_file; | |
| 235 | - } | |
| 236 | - } | |
| 237 | - | |
| 238 | - return ''; | |
| 239 | - } | |
| 240 | - | |
| 241 | - /** | |
| 242 | - * Activate an already installed integration plugin from the integrations page. | |
| 243 | - */ | |
| 244 | - public function activate_integration_plugin() { | |
| 245 | - check_ajax_referer( 'subscrpt_integration_install_nonce', 'nonce' ); | |
| 246 | - | |
| 247 | - if ( ! current_user_can( 'activate_plugins' ) ) { | |
| 248 | - wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 ); | |
| 249 | - } | |
| 250 | - | |
| 251 | - $plugin_file = isset( $_POST['plugin_file'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_file'] ) ) : ''; | |
| 252 | - | |
| 253 | - if ( ! function_exists( 'get_plugins' ) ) { | |
| 254 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 255 | - } | |
| 256 | - | |
| 257 | - if ( empty( $plugin_file ) || ! array_key_exists( $plugin_file, get_plugins() ) ) { | |
| 258 | - wp_send_json_error( array( 'message' => __( 'Plugin is not installed.', 'subscription' ) ), 400 ); | |
| 259 | - } | |
| 260 | - | |
| 261 | - $activate = activate_plugin( $plugin_file ); | |
| 262 | - | |
| 263 | - if ( is_wp_error( $activate ) ) { | |
| 264 | - wp_send_json_error( array( 'message' => $activate->get_error_message() ), 500 ); | |
| 265 | 181 | } |
| 266 | 182 | |
| 267 | 183 | wp_send_json_success(); |
| 268 | 184 | } |