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 |