PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.4.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.4.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 +191 -8 1.3.1 → 2.4.0 View file →
@@ -3,8 +3,10 @@
3 3 namespace SpringDevs\Subscription;
4 4
5 5 /**
6 6 * The Ajax class
7 + *
8 + * @package SpringDevs\Subscription
7 9 */
8 10 class Ajax {
9 11
10 12
@@ -11,10 +13,12 @@
11 13 /**
12 14 * Initialize the class
13 15 */
14 16 public function __construct() {
15 - add_action( 'wp_ajax_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) );
16 - add_action( 'wp_ajax_activate_woocommerce_plugin', array( $this, 'activate_woocommerce_plugin' ) );
17 + add_action( 'wp_ajax_subscrpt_install_woocommerce_plugin', array( $this, 'install_woocommerce_plugin' ) );
18 + add_action( 'wp_ajax_subscrpt_activate_woocommerce_plugin', array( $this, 'wps_subscription_activate_woocommerce_plugin' ) );
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' ) );
17 21 }
18 22
19 23 /**
20 24 * Install the WooCommerce Plugin.
@@ -19,18 +23,18 @@
19 23 /**
20 24 * Install the WooCommerce Plugin.
21 25 */
22 26 public function install_woocommerce_plugin() {
23 - include ABSPATH . 'wp-admin/includes/plugin-install.php';
24 - include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
27 + include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
28 + include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
25 29 include_once ABSPATH . 'wp-admin/includes/file.php';
26 30 include_once ABSPATH . 'wp-admin/includes/misc.php';
27 31
28 32 if ( ! class_exists( 'Plugin_Upgrader' ) ) {
29 - include ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
33 + require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
30 34 }
31 35 if ( ! class_exists( 'Plugin_Installer_Skin' ) ) {
32 - include ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
36 + require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
33 37 }
34 38
35 39 $plugin = 'woocommerce';
36 40
@@ -55,12 +59,16 @@
55 59 )
56 60 );
57 61
58 62 if ( is_wp_error( $api ) ) {
59 - wp_die( $api );
63 + wp_die( esc_html( $api->get_error_message() ) );
60 64 }
61 65
62 - $title = sprintf( __( 'Installing Plugin: %s', 'sdevs_subscrpt' ), $api->name . ' ' . $api->version );
66 + $title = sprintf(
67 + // translators: Plugin name and version.
68 + __( 'Installing Plugin: %s', 'subscription' ),
69 + $api->name . ' ' . $api->version
70 + );
63 71 $nonce = 'install-plugin_' . $plugin;
64 72 $url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin );
65 73
66 74 $upgrader = new \Plugin_Upgrader( new \Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
@@ -75,8 +83,14 @@
75 83 /**
76 84 * Active WooComerce Plugin.
77 85 */
78 86 public function activate_woocommerce_plugin() {
87 + // add Deprecated notice
88 + _deprecated_function( 'Ajax::activate_woocommerce_plugin', '1.5.3', 'Ajax::wps_subscription_activate_woocommerce_plugin' );
89 + return $this->wps_subscription_activate_woocommerce_plugin();
90 + }
91 +
92 + public function wps_subscription_activate_woocommerce_plugin() {
79 93 activate_plugin( 'woocommerce/woocommerce.php' );
80 94 wp_send_json(
81 95 array(
82 96 'msg' => 'Activated successfully !!',
@@ -81,6 +95,175 @@
81 95 array(
82 96 'msg' => 'Activated successfully !!',
83 97 )
84 98 );
99 + }
100 +
101 + /**
102 + * Install and activate a payment gateway plugin from the integrations page.
103 + */
104 + public function install_integration_plugin() {
105 + check_ajax_referer( 'subscrpt_integration_install_nonce', 'nonce' );
106 +
107 + if ( ! current_user_can( 'install_plugins' ) ) {
108 + wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 );
109 + }
110 +
111 + $plugin_slug = isset( $_POST['plugin_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_slug'] ) ) : '';
112 +
113 + if ( empty( $plugin_slug ) ) {
114 + wp_send_json_error( array( 'message' => __( 'Plugin slug is required.', 'subscription' ) ), 400 );
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 +
135 + include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
136 + include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
137 + include_once ABSPATH . 'wp-admin/includes/file.php';
138 + include_once ABSPATH . 'wp-admin/includes/misc.php';
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 +
152 + $cache_key = 'subscrpt_plugin_api_' . sanitize_key( $plugin_slug );
153 + $api = get_transient( $cache_key );
154 +
155 + if ( false === $api ) {
156 + // Buffer output so any PHP warnings from the API call don't corrupt the JSON response.
157 + ob_start();
158 +
159 + $api = plugins_api(
160 + 'plugin_information',
161 + array(
162 + 'slug' => $plugin_slug,
163 + 'fields' => array(
164 + 'short_description' => false,
165 + 'sections' => false,
166 + 'requires' => false,
167 + 'rating' => false,
168 + 'ratings' => false,
169 + 'downloaded' => false,
170 + 'last_updated' => false,
171 + 'added' => false,
172 + 'tags' => false,
173 + 'compatibility' => false,
174 + 'homepage' => false,
175 + 'donate_link' => false,
176 + ),
177 + )
178 + );
179 +
180 + ob_end_clean();
181 +
182 + if ( is_wp_error( $api ) ) {
183 + wp_send_json_error( array( 'message' => $api->get_error_message() ), 400 );
184 + }
185 +
186 + set_transient( $cache_key, $api, 12 * HOUR_IN_SECONDS );
187 + }
188 +
189 + $skin = new \WP_Ajax_Upgrader_Skin();
190 + $upgrader = new \Plugin_Upgrader( $skin );
191 + $result = $upgrader->install( $api->download_link );
192 +
193 + if ( is_wp_error( $result ) ) {
194 + wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 );
195 + }
196 +
197 + // The skin collects the reason (folder exists, no filesystem access, …)
198 + // while install() itself only returns null.
199 + 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 );
204 + }
205 +
206 + $plugin_file = $upgrader->plugin_info();
207 +
208 + if ( ! $plugin_file ) {
209 + wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) );
210 + }
211 +
212 + $activate = activate_plugin( $plugin_file );
213 +
214 + if ( is_wp_error( $activate ) ) {
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 );
265 + }
266 +
267 + wp_send_json_success();
85 268 }
86 269 }