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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / admin / helper / class-wc-helper-subscriptions-api.php
woocommerce / includes / admin / helper Last commit date
views 5 months ago class-wc-helper-admin.php 4 months ago class-wc-helper-api.php 1 year ago class-wc-helper-compat.php 5 years ago class-wc-helper-options.php 3 years ago class-wc-helper-orders-api.php 2 years ago class-wc-helper-sanitization.php 1 year ago class-wc-helper-subscriptions-api.php 4 months ago class-wc-helper-updater.php 2 months ago class-wc-helper.php 4 months ago class-wc-plugin-api-updater.php 1 year ago class-wc-product-usage-notice.php 1 year ago class-wc-woo-helper-connection.php 4 months ago class-wc-woo-update-manager-plugin.php 2 years ago
class-wc-helper-subscriptions-api.php
402 lines
1 <?php
2 /**
3 * WooCommerce Admin Helper - React admin interface
4 *
5 * @package WooCommerce\Admin\Helper
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * WC_Helper_Subscriptions_API
14 *
15 * The main entry-point for all things related to the Marketplace Subscriptions API.
16 * The Subscriptions API manages WooCommerce.com Subscriptions.
17 */
18 class WC_Helper_Subscriptions_API {
19
20 /**
21 * Loads the class, runs on init
22 *
23 * @return void
24 */
25 public static function load() {
26 add_filter( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
27 }
28
29 /**
30 * Registers the REST routes for the Marketplace Subscriptions API.
31 * These endpoints are used by the Marketplace Subscriptions React UI.
32 */
33 public static function register_rest_routes() {
34 register_rest_route(
35 'wc/v3',
36 '/marketplace/refresh',
37 array(
38 'methods' => 'POST',
39 'callback' => array( __CLASS__, 'refresh' ),
40 'permission_callback' => array( __CLASS__, 'get_permission' ),
41 )
42 );
43 register_rest_route(
44 'wc/v3',
45 '/marketplace/subscriptions',
46 array(
47 'methods' => 'GET',
48 'callback' => array( __CLASS__, 'get_subscriptions' ),
49 'permission_callback' => array( __CLASS__, 'get_permission' ),
50 )
51 );
52 register_rest_route(
53 'wc/v3',
54 '/marketplace/subscriptions/connect',
55 array(
56 'methods' => 'POST',
57 'callback' => array( __CLASS__, 'connect' ),
58 'permission_callback' => array( __CLASS__, 'get_permission' ),
59 'args' => array(
60 'product_key' => array(
61 'required' => true,
62 'type' => 'string',
63 ),
64 ),
65 )
66 );
67 register_rest_route(
68 'wc/v3',
69 '/marketplace/subscriptions/activate-plugin',
70 array(
71 'methods' => 'POST',
72 'callback' => array( __CLASS__, 'activate_plugin' ),
73 'permission_callback' => array( __CLASS__, 'get_permission' ),
74 'args' => array(
75 'product_key' => array(
76 'required' => true,
77 'type' => 'string',
78 ),
79 ),
80 )
81 );
82 register_rest_route(
83 'wc/v3',
84 '/marketplace/subscriptions/disconnect',
85 array(
86 'methods' => 'POST',
87 'callback' => array( __CLASS__, 'disconnect' ),
88 'permission_callback' => array( __CLASS__, 'get_permission' ),
89 'args' => array(
90 'product_key' => array(
91 'required' => true,
92 'type' => 'string',
93 ),
94 ),
95 )
96 );
97 register_rest_route(
98 'wc/v3',
99 '/marketplace/subscriptions/activate',
100 array(
101 'methods' => 'POST',
102 'callback' => array( __CLASS__, 'activate' ),
103 'permission_callback' => array( __CLASS__, 'get_permission' ),
104 'args' => array(
105 'product_key' => array(
106 'required' => true,
107 'type' => 'string',
108 ),
109 ),
110 )
111 );
112
113 register_rest_route(
114 'wc/v3',
115 '/marketplace/subscriptions/install-url',
116 array(
117 'methods' => 'GET',
118 'callback' => array( __CLASS__, 'install_url' ),
119 'permission_callback' => array( __CLASS__, 'get_permission' ),
120 'args' => array(
121 'product_key' => array(
122 'required' => true,
123 'type' => 'string',
124 ),
125 ),
126 )
127 );
128 }
129
130 /**
131 * The Extensions page can only be accessed by users with the manage_woocommerce
132 * capability. So the API mimics that behavior.
133 */
134 public static function get_permission() {
135 return current_user_can( 'manage_woocommerce' );
136 }
137
138 /**
139 * Fetch subscriptions from WooCommerce.com and serve them
140 * as JSON.
141 */
142 public static function get_subscriptions() {
143 // If the site is connected, mark the time when the my subscriptions tab is first loaded.
144 if ( WC_Helper::is_site_connected() === true && empty( WC_Helper_Options::get( 'my_subscriptions_tab_loaded' ) ) ) {
145 WC_Helper_Options::update( 'my_subscriptions_tab_loaded', date( 'Y-m-d H:i:s' ) );
146 }
147
148 $subscriptions = WC_Helper::get_subscription_list_data();
149 wp_send_json(
150 array_values(
151 $subscriptions
152 )
153 );
154 }
155
156 /**
157 * Refresh account and subscriptions from WooCommerce.com and serve subscriptions
158 * as JSON.
159 */
160 public static function refresh() {
161 try {
162 WC_Helper::refresh_helper_subscriptions();
163 WC_Helper::get_subscriptions();
164 WC_Helper::get_product_usage_notice_rules();
165 WC_Helper::fetch_helper_connection_info();
166 self::get_subscriptions();
167 } catch ( Exception $e ) {
168 wp_send_json_error(
169 array(
170 'message' => $e->getMessage(),
171 ),
172 400
173 );
174 }
175 }
176
177 /**
178 * Connect a WooCommerce.com subscription.
179 *
180 * @param WP_REST_Request $request Request object.
181 */
182 public static function connect( $request ) {
183 $product_key = $request->get_param( 'product_key' );
184 try {
185 $success = WC_Helper::activate_helper_subscription( $product_key );
186 } catch ( Exception $e ) {
187 $error_data = array(
188 'message' => $e->getMessage(),
189 );
190
191 if ( $e instanceof WC_Data_Exception ) {
192 $error_data['code'] = $e->getErrorCode();
193 // Include extra data from the exception so the client can render contextual UI (e.g. maxed out sites list).
194 $error_data['data'] = $e->getErrorData();
195 $status_code = (int) $e->getCode();
196 if ( 100 > $status_code || 599 < $status_code ) {
197 $status_code = 400;
198 }
199 } else {
200 $status_code = 400;
201 }
202
203 wp_send_json_error( $error_data, $status_code );
204 }
205 if ( $success ) {
206 wp_send_json_success(
207 array(
208 'message' => __( 'Your subscription has been connected.', 'woocommerce' ),
209 )
210 );
211 } else {
212 wp_send_json_error(
213 array(
214 'message' => __( 'There was an error connecting your subscription. Please try again.', 'woocommerce' ),
215 ),
216 400
217 );
218 }
219 }
220
221 /**
222 * Activate a plugin for a WooCommerce.com subscription.
223 *
224 * @param WP_REST_Request $request Request object.
225 */
226 public static function activate_plugin( $request ) {
227 $product_key = $request->get_param( 'product_key' );
228 try {
229 $success = WC_Helper::activate_plugin( $product_key );
230 } catch ( Exception $e ) {
231 wp_send_json_error(
232 array(
233 'message' => $e->getMessage(),
234 ),
235 400
236 );
237 }
238 if ( $success ) {
239 wp_send_json_success(
240 array(
241 'message' => __( 'The plugin for your subscription has been activated.', 'woocommerce' ),
242 )
243 );
244 } else {
245 wp_send_json_error(
246 array(
247 'message' => __( 'The plugin for your subscription couldn\'t be activated.', 'woocommerce' ),
248 ),
249 400
250 );
251 }
252 }
253
254 /**
255 * Disconnect a WooCommerce.com subscription.
256 *
257 * @param WP_REST_Request $request Request object.
258 */
259 public static function disconnect( $request ) {
260 $product_key = $request->get_param( 'product_key' );
261 try {
262 $success = WC_Helper::deactivate_helper_subscription( $product_key );
263 } catch ( Exception $e ) {
264 wp_send_json_error(
265 array(
266 'message' => $e->getMessage(),
267 ),
268 400
269 );
270 }
271 if ( $success ) {
272 wp_send_json_success(
273 array(
274 'message' => __( 'Your subscription has been disconnected.', 'woocommerce' ),
275 )
276 );
277 } else {
278 wp_send_json_error(
279 array(
280 'message' => __( 'There was an error disconnecting your subscription. Please try again.', 'woocommerce' ),
281 ),
282 400
283 );
284 }
285 }
286
287 /**
288 * Activate a WooCommerce.com product.
289 * This activates the plugin/theme on the site.
290 *
291 * @param WP_REST_Request $request Request object.
292 */
293 public static function activate( $request ) {
294 $product_key = $request->get_param( 'product_key' );
295 $subscription = WC_Helper::get_subscription( $product_key );
296
297 if ( ! $subscription ) {
298 wp_send_json_error(
299 array(
300 'message' => __( 'We couldn\'t find a subscription for this product.', 'woocommerce' ),
301 ),
302 400
303 );
304 }
305
306 if ( true !== $subscription['local']['installed'] || ! isset( $subscription['local']['active'] ) ) {
307 wp_send_json_error(
308 array(
309 'message' => __( 'This product is not installed.', 'woocommerce' ),
310 ),
311 400
312 );
313 }
314
315 if ( true === $subscription['local']['active'] ) {
316 wp_send_json_success(
317 array(
318 'message' => __( 'This product is already active.', 'woocommerce' ),
319 ),
320 );
321 }
322
323 if ( 'plugin' === $subscription['product_type'] ) {
324 $success = activate_plugin( $subscription['local']['path'] );
325 if ( is_wp_error( $success ) ) {
326 wp_send_json_error(
327 array(
328 'message' => __( 'There was an error activating this plugin.', 'woocommerce' ),
329 ),
330 400
331 );
332 }
333 } elseif ( 'theme' === $subscription['product_type'] ) {
334 switch_theme( $subscription['local']['slug'] );
335 $theme = wp_get_theme();
336 if ( $subscription['local']['slug'] !== $theme->get_stylesheet() ) {
337 wp_send_json_error(
338 array(
339 'message' => __( 'There was an error activating this theme.', 'woocommerce' ),
340 ),
341 400
342 );
343 }
344 }
345
346 wp_send_json_success(
347 array(
348 'message' => __( 'This product has been activated.', 'woocommerce' ),
349 ),
350 );
351 }
352
353 /**
354 * Get the install URL for a WooCommerce.com product.
355 *
356 * @param WP_REST_Request $request Request object.
357 */
358 public static function install_url( $request ) {
359 $product_key = $request->get_param( 'product_key' );
360 $subscription = WC_Helper::get_subscription( $product_key );
361
362 if ( ! $subscription ) {
363 wp_send_json_error(
364 array(
365 'message' => __( 'We couldn\'t find a subscription for this product.', 'woocommerce' ),
366 ),
367 400
368 );
369 }
370
371 if ( true === $subscription['local']['installed'] ) {
372 wp_send_json_success(
373 array(
374 'message' => __( 'This product is already installed.', 'woocommerce' ),
375 ),
376 );
377 }
378
379 $install_url = WC_Helper::get_subscription_install_url(
380 $subscription['product_key'],
381 $subscription['product_slug']
382 );
383
384 if ( ! $install_url ) {
385 wp_send_json_error(
386 array(
387 'message' => __( 'There was an error getting the install URL for this product.', 'woocommerce' ),
388 ),
389 400
390 );
391 }
392
393 wp_send_json_success(
394 array(
395 'url' => $install_url,
396 ),
397 );
398 }
399 }
400
401 WC_Helper_Subscriptions_API::load();
402