PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.10.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.10.2
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 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Ajax.php

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

186 lines 5.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 }
21
22 /**
23 * Install the WooCommerce Plugin.
24 */
25 public function install_woocommerce_plugin() {
26 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
27 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
28 include_once ABSPATH . 'wp-admin/includes/file.php';
29 include_once ABSPATH . 'wp-admin/includes/misc.php';
30
31 if ( ! class_exists( 'Plugin_Upgrader' ) ) {
32 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
33 }
34 if ( ! class_exists( 'Plugin_Installer_Skin' ) ) {
35 require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
36 }
37
38 $plugin = 'woocommerce';
39
40 $api = plugins_api(
41 'plugin_information',
42 array(
43 'slug' => $plugin,
44 'fields' => array(
45 'short_description' => false,
46 'sections' => false,
47 'requires' => false,
48 'rating' => false,
49 'ratings' => false,
50 'downloaded' => false,
51 'last_updated' => false,
52 'added' => false,
53 'tags' => false,
54 'compatibility' => false,
55 'homepage' => false,
56 'donate_link' => false,
57 ),
58 )
59 );
60
61 if ( is_wp_error( $api ) ) {
62 wp_die( esc_html( $api->get_error_message() ) );
63 }
64
65 $title = sprintf(
66 // translators: Plugin name and version.
67 __( 'Installing Plugin: %s', 'subscription' ),
68 $api->name . ' ' . $api->version
69 );
70 $nonce = 'install-plugin_' . $plugin;
71 $url = 'update.php?action=install-plugin&plugin=' . urlencode( $plugin );
72
73 $upgrader = new \Plugin_Upgrader( new \Plugin_Installer_Skin( compact( 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
74 $upgrader->install( $api->download_link );
75 wp_send_json(
76 array(
77 'msg' => 'Installed successfully !!',
78 )
79 );
80 }
81
82 /**
83 * Active WooComerce Plugin.
84 */
85 public function activate_woocommerce_plugin() {
86 // add Deprecated notice
87 _deprecated_function( 'Ajax::activate_woocommerce_plugin', '1.5.3', 'Ajax::wps_subscription_activate_woocommerce_plugin' );
88 return $this->wps_subscription_activate_woocommerce_plugin();
89 }
90
91 public function wps_subscription_activate_woocommerce_plugin() {
92 activate_plugin( 'woocommerce/woocommerce.php' );
93 wp_send_json(
94 array(
95 'msg' => 'Activated successfully !!',
96 )
97 );
98 }
99
100 /**
101 * Install and activate a payment gateway plugin from the integrations page.
102 */
103 public function install_integration_plugin() {
104 check_ajax_referer( 'subscrpt_integration_install_nonce', 'nonce' );
105
106 if ( ! current_user_can( 'install_plugins' ) ) {
107 wp_send_json_error( array( 'message' => __( 'Permission denied.', 'subscription' ) ), 403 );
108 }
109
110 $plugin_slug = isset( $_POST['plugin_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_slug'] ) ) : '';
111
112 if ( empty( $plugin_slug ) ) {
113 wp_send_json_error( array( 'message' => __( 'Plugin slug is required.', 'subscription' ) ), 400 );
114 }
115
116 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
117 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
118 include_once ABSPATH . 'wp-admin/includes/file.php';
119 include_once ABSPATH . 'wp-admin/includes/misc.php';
120
121 $cache_key = 'subscrpt_plugin_api_' . sanitize_key( $plugin_slug );
122 $api = get_transient( $cache_key );
123
124 if ( false === $api ) {
125 // Buffer output so any PHP warnings from the API call don't corrupt the JSON response.
126 ob_start();
127
128 $api = plugins_api(
129 'plugin_information',
130 array(
131 'slug' => $plugin_slug,
132 'fields' => array(
133 'short_description' => false,
134 'sections' => false,
135 'requires' => false,
136 'rating' => false,
137 'ratings' => false,
138 'downloaded' => false,
139 'last_updated' => false,
140 'added' => false,
141 'tags' => false,
142 'compatibility' => false,
143 'homepage' => false,
144 'donate_link' => false,
145 ),
146 )
147 );
148
149 ob_end_clean();
150
151 if ( is_wp_error( $api ) ) {
152 wp_send_json_error( array( 'message' => $api->get_error_message() ), 400 );
153 }
154
155 set_transient( $cache_key, $api, 12 * HOUR_IN_SECONDS );
156 }
157
158 $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
159 $result = $upgrader->install( $api->download_link );
160
161 ob_end_clean();
162
163 if ( is_wp_error( $result ) ) {
164 wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 );
165 }
166
167 if ( ! $result ) {
168 wp_send_json_error( array( 'message' => __( 'Plugin installation failed.', 'subscription' ) ), 500 );
169 }
170
171 $plugin_file = $upgrader->plugin_info();
172
173 if ( ! $plugin_file ) {
174 wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) );
175 }
176
177 $activate = activate_plugin( $plugin_file );
178
179 if ( is_wp_error( $activate ) ) {
180 wp_send_json_success( array( 'warning' => __( 'Plugin installed but could not be activated automatically.', 'subscription' ) ) );
181 }
182
183 wp_send_json_success();
184 }
185 }
186