PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.2.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.2.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
← All changes | includes/Ajax.php +88 -4 2.1.0 → 2.2.0 View file →
@@ -16,8 +16,9 @@
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' ) );
20 21 }
21 22
22 23 /**
23 24 * Install the WooCommerce Plugin.
@@ -112,13 +113,43 @@
112 113 if ( empty( $plugin_slug ) ) {
113 114 wp_send_json_error( array( 'message' => __( 'Plugin slug is required.', 'subscription' ) ), 400 );
114 115 }
115 116
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 +
116 135 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
117 136 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
118 137 include_once ABSPATH . 'wp-admin/includes/file.php';
119 138 include_once ABSPATH . 'wp-admin/includes/misc.php';
120 139
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 +
121 152 $cache_key = 'subscrpt_plugin_api_' . sanitize_key( $plugin_slug );
122 153 $api = get_transient( $cache_key );
123 154
124 155 if ( false === $api ) {
@@ -154,19 +185,23 @@
154 185
155 186 set_transient( $cache_key, $api, 12 * HOUR_IN_SECONDS );
156 187 }
157 188
158 - $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
189 + $skin = new \WP_Ajax_Upgrader_Skin();
190 + $upgrader = new \Plugin_Upgrader( $skin );
159 191 $result = $upgrader->install( $api->download_link );
160 192
161 - ob_end_clean();
162 -
163 193 if ( is_wp_error( $result ) ) {
164 194 wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 );
165 195 }
166 196
197 + // The skin collects the reason (folder exists, no filesystem access, …)
198 + // while install() itself only returns null.
167 199 if ( ! $result ) {
168 - wp_send_json_error( array( 'message' => __( 'Plugin installation failed.', 'subscription' ) ), 500 );
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 );
169 204 }
170 205
171 206 $plugin_file = $upgrader->plugin_info();
172 207
@@ -177,8 +212,57 @@
177 212 $activate = activate_plugin( $plugin_file );
178 213
179 214 if ( is_wp_error( $activate ) ) {
180 215 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 );
181 265 }
182 266
183 267 wp_send_json_success();
184 268 }