PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.3.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.3.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
subscription / includes / Ajax.php

Ajax.php in Subscriptions for WooCommerce with Stripe Recurring Payments 2.3.0, at includes/Ajax.php

270 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription;
4
5 /**
6 * The Ajax class
7 *
8 * @package SpringDevs\Subscription
9 */
10 class Ajax {
11
12
13 /**
14 * Initialize the class
15 */
16 public function __construct() {
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' ) );
21 }
22
23 /**
24 * Install the WooCommerce Plugin.
25 */
26 public function install_woocommerce_plugin() {
27 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
28 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
29 include_once ABSPATH . 'wp-admin/includes/file.php';
30 include_once ABSPATH . 'wp-admin/includes/misc.php';
31
32 if ( ! class_exists( 'Plugin_Upgrader' ) ) {
33 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
34 }
35 if ( ! class_exists( 'Plugin_Installer_Skin' ) ) {
36 require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
37 }
38
39 $plugin = 'woocommerce';
40
41 $api = plugins_api(
42 'plugin_information',
43 array(
44 'slug' => $plugin,
45 'fields' => array(
46 'short_description' => false,
47 'sections' => false,
48 'requires' => false,
49 'rating' => false,
50 'ratings' => false,
51 'downloaded' => false,
52 'last_updated' => false,
53 'added' => false,
54 'tags' => false,
55 'compatibility' => false,
56 'homepage' => false,
57 'donate_link' => false,
58 ),
59 )
60 );
61
62 if ( is_wp_error( $api ) ) {
63 wp_die( esc_html( $api->get_error_message() ) );
64 }
65
66 $title = sprintf(
67 // translators: Plugin name and version.
68 __( 'Installing Plugin: %s', 'subscription' ),
69 $api->name . ' ' . $api->version
70 );
71 $nonce = 'install-plugin_' . $plugin;
72 $url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin );
73
74 $upgrader = new \Plugin_Upgrader( new \Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
75 $upgrader->install( $api->download_link );
76 wp_send_json(
77 array(
78 'msg' => 'Installed successfully !!',
79 )
80 );
81 }
82
83 /**
84 * Active WooComerce Plugin.
85 */
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() {
93 activate_plugin( 'woocommerce/woocommerce.php' );
94 wp_send_json(
95 array(
96 'msg' => 'Activated successfully !!',
97 )
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();
268 }
269 }
270